提供emacs交互功能:之前 [英] Advising an emacs interactive function: before
问题描述
我想提前咨询一些使用交互式参数的功能,例如 find-dired
:
(defadvice find-dired(before eab- find-dired activate)
(消息before!)
(setq find-args'(-iname'**'10)))
但是,emacs仅在 find-dired
交互式会话后执行此建议,我无法设置 find-args
之前。如何解决矛盾?
更新。请注意, defadvice
宏是 deprecated 。
artscan 用可行的答案回答了自己的问题,但有点不完整和误导。这也涉及 'interactive
,这可能会混淆在一起,因为它看起来像在命令的正文内部定义,但实际上是在之前使用的功能是输入 - 在执行任何建议之前(除非该建议具有'交互式
调用...)
a href =http://www.gnu.org/software/emacs/manual/html_mono/elisp.html#Combined-Definition =nofollow noreferrer>文档的建议缺少一些细节,在这种情况下会有所帮助,所以看起来更好的地方实际上是来源: advice.el
。看看,并找到评论部分 @ Foo游戏:建议教程
。您也可以使用 Mx find-library advice RET 在Emacs中找到源代码。
具体来说,对于这个问题,在 advice.el
标签为 @@提供互动行为:
- 因为这正是你想要做的。 / p>
如果仔细阅读,您会发现建议不需要以 / code>,但也可以
之前的,并且可以在
之后 - 虽然这只是问为了麻烦这是因为
交互式
是(并且必须被)特别处理。
所以,以下代码工作(请注意之前的):
(defadvice find-dired -find-dired(dir args)activate)
ignore find-args,hard code \-iname'**'\
(interactive
(list目录名在目录中运行find:nilt)
(read-string运行find(with args):'(-iname'**'10)
' (find-args-history。1))))))
正如其他人建议的,正在编写自己的功能,我认为最简单的是 Lindydancer 的answer 。
建议是一个非常诱人的工具,但容易过度使用。我不会说这是危险的,但应该谨慎使用。在编写自己的函数不起作用时,似乎最好用 - 例如,改变不能修改的代码调用的函数的行为。我认为这种情况的很好的例子可以在这里,这里和这里(嘟嘟我自己的角)。
I want to before-advice some function, which uses interactive arguments, e.g. find-dired
:
(defadvice find-dired (before eab-find-dired activate)
(message "before!")
(setq find-args '("-iname '**'" . 10)))
But emacs executes this advice only after find-dired
interactive session and I can't setup find-args
before. How to resolve the contradiction?
Upd. Note that defadvice
macro is deprecated.
artscan answered his own question with a workable answer, but it's a bit incomplete and misleading. This also involves 'interactive
, which can be confusing in and of itself - in that it looks like it is defined inside the body of the command, but is actually used before the function is entered - and before any advice is executed (unless that advice has 'interactive
calls...)
The documentation for advice lacks a number of details that would help in this situation, so the better place to look is actually the source: 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.
Specifically, for this problem, look at the section in advice.el
labeled @@ Advising interactive behavior:
- because that's exactly what you're trying to do.
If you read it closely, you'll notice that the advice does not need to be of the form around
, but can be before
as well, and it can be after
- though that's just asking for trouble. This is because the interactive
is (and has to be) treated special.
So, the following code works (note the 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)))))
Probably a cleaner way to do this, as others suggested, is writing your own function, and I think the easiest is Lindydancer's answer.
Advice is a pretty enticing tool, but is easy to overuse. I wouldn't go as far as saying it is dangerous, but should be used sparingly. It seems to be best used when writing your own function doesn't work - for instance, changing the behavior of a function that is called by code you can't modify. I think good examples of this situation can be found here, here, and here (to toot my own horn).
这篇关于提供emacs交互功能:之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!