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 - 如何在 Emacs 中找到建议功能的 defadvice?

控制台/终端模式菜单中的 Emacs

emacs - 我什么时候在 emacs lisp 中使用 "when"关键字(或表达式)

macos - 如何在没有GUI的Mac OSX上安装emacs23

emacs - 何时在 Emacs Lisp 中引用符号

emacs - 当且仅当区域在 Emacs 中处于事件状态时,标记是否处于事件状态?

emacs - 建议emacs交互功能: before

emacs - 如何将缓冲区重命名为类似的名称

ruby - 从一开始的多少行内必须放置各种方向?