Emacs Lisp,如何映射宏,并评估其中一个参数

标签 emacs elisp advising-functions defadvice

Bob Glickstein 在“编写 GNU Emacs 扩展”的第 3 章中描述了一种建议滚动功能的方法。 (他建议让它们可逆,所以我们必须在滚动之前保存状态。)

例如。对于向上滚动命令,此建议是这样完成的

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

好。我当然必须对所有滚动命令执行此操作。所以我想对它们进行排序,并想一起为它们提供建议。
(setq reversible-scroll-commands 
  [scroll-up-command 
   scroll-down-command 
   scroll-left-command 
   scroll-right-command])

(我使用一个向量来保存 5 个引号。)

但现在我被困住了。
(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)

将建议(不存在的)函数 fun-name 四次,因为 defadvice 是一个宏,并且不评估 fun-name。

有什么办法吗?

(我正在使用 Emacs 24)

最佳答案

未经测试:

(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)

section about backquotes在elisp手册中。

关于Emacs Lisp,如何映射宏,并评估其中一个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11341000/

相关文章:

emacs - 在组织 latex 导出中禁用标题

Emacs 声称我引用了一个 lambda,但我不是

emacs - 我使用的是 ispell 还是 aspell,本地字典保存在哪里?

perl - 在 Emacs 中执行 Perl,最基本的

Emacs/AUCTeX : Rewriting the Okular-make-url function to work with new synctex (full path + "./") syntax

regex - 是否可以更改 emacs 的正则表达式语法?

emacs - 如何在 Emacs 中自动切换到发生缓冲区?

python - elisp 相当于 python shlex.split?

Emacs 术语模式 : stop setting remote default-directory on some hosts