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/