artscan https://stackoverflow.com/users/1937596/artscan用可行的答案回答了他自己的问题,但它有点不完整且具有误导性。这还涉及到'interactive http://www.gnu.org/software/emacs/manual/html_mono/elisp.html#Using-Interactive,它本身可能会令人困惑 - 因为它看起来像是在命令体内定义的,但实际上是使用的before输入函数 - 并且在执行任何建议之前(除非该建议已'interactive
打电话...)
The documentation for advice http://www.gnu.org/software/emacs/manual/html_mono/elisp.html#Combined-Definition lacks a number of details that would help in this situation, so the better place to look is actually the source: advice.el http://git.savannah.gnu.org/cgit/emacs.git/tree/lisp/emacs-lisp/advice.el. Look at that and find the comment section @ Foo games: An advice tutorial
. You can also find the source in your Emacs itself with M-x find-library advice RET.
具体来说,对于这个问题,请查看中的部分advice.el
贴上标签@@ Advising interactive behavior:
- 因为这正是你想要做的。
如果您仔细阅读,您会发现该建议确实not需要采用以下形式around
,但可以是before
以及,并且它可以是after
——虽然那只是自找麻烦。这是因为interactive
是(并且必须)受到特殊对待的。
所以,下面的代码可以工作(注意before
):
(defadvice find-dired (before eab-find-dired (dir args) activate)
"ignore find-args, hard code \"-iname '**'\""
(interactive
(list (read-directory-name "Run find in directory: " nil "" t)
(read-string "Run find (with args): " '("-iname '**'" . 10)
'(find-args-history . 1)))))
正如其他人所建议的,可能更干净的方法是编写自己的函数,我认为最简单的是林迪舞者 https://stackoverflow.com/users/623133/lindydancer's answer https://stackoverflow.com/a/14607894/6148.
建议是一个非常诱人的工具,但很容易被过度使用。我不会说它很危险,但应该谨慎使用。当编写自己的函数不起作用时,它似乎是最好的使用 - 例如,更改由您无法修改的代码调用的函数的行为。我认为这种情况可以找到很好的例子here https://stackoverflow.com/a/334600/6148, here https://stackoverflow.com/a/607866/6148, and here http://trey-jackson.blogspot.com/2010/10/emacs-tip-38-automatically-diff-binary.html(自吹自擂)。