Search code examples
emacselispadvising-functionsdefadvice

Emacs Lisp, how to mapcar a macro, and evaluate one of the arguments


Bob Glickstein describes in "Writing GNU Emacs Extensions", chapter 3, a way to advise scroll functions. (He suggests to make them reversible, so we have to save the state before scrolling.)

E.g. for scroll-up-command this advising is done like so

(defadvice scroll-up-command (before reversibilate activate compile)
   "If it wants to be reversible, it must save the former state."
   (save-before-scroll))

Well. I of course have to do this to all of the scroll commands. So I'd like to make a sequence of them, and want to advise them together.

(setq reversible-scroll-commands 
  [scroll-up-command 
   scroll-down-command 
   scroll-left-command 
   scroll-right-command])

(I use a vector to save 5 quotes.)

But now I'm stuck.

(mapcar 
  (lambda (fun-name)
    (defadvice fun-name (before reversibilate activate compile)
       "If it wants to be reversible, it must save the former state."
       (save-before-scroll)))
   reversible-scroll-commands)

will advise the (non existing) function fun-name four times, as defadvice is a macro, and doesn't evaluate fun-name.

Is there any way to do it?

(I'm using Emacs 24)


Solution

  • Untested:

    (mapcar 
      (lambda (fun-name)
        (eval `(defadvice ,fun-name (before reversibilate activate compile)
                 "If it wants to be reversible, it must save the former state."
                 (save-before-scroll))))
       reversible-scroll-commands)
    

    See the section about backquotes in the elisp manual.