Emacs Lisp:如何使用ad-get-arg和ad-get-args? [英] Emacs Lisp: How to use ad-get-arg and ad-get-args?
问题描述
我不知道我在使用 ad-get-args and ad-get-arg right。
I'm not sure I am using ad-get-args and ad-get-arg right.
例如,以下代码不
(defun my-add (a b)
(+ a b))
(defadvice my-add (after my-log-on activate)
(message "my-add: %s" (ad-get-args)))
(my-add 1 2)
最后一个表达式导致错误:
The last expression causes an error:
Debugger entered--Lisp error: (void-function ad-get-args).
以下内容不起作用。
(defun my-substract (a b)
(- a b))
(defadvice my-substract (around my-log-on activate)
(message "my-substract: %s" (ad-get-arg 0))
(ad-do-it))
(my-substract 10 1)
defadvice发出警告:
The defadvice gives a warning:
Warning: `(setq ad-return-value (ad-Orig-my-substract a b))' is a malformed
function
最后一个表达式出现错误:
And the last expression gives an error:
Debugger entered--Lisp error: (invalid-function (setq ad-return-value (ad-Orig-my-substract a b)))
(setq ad-return-value (ad-Orig-my-substract a b))()
我正在尝试使用defadvice监视开始进程参数进行调试,我发现我使用ad-get -arg没有工作。
I was trying to use defadvice to watch start-process arguments for debugging purposes and I found my way of using ad-get-arg didn't work.
更新:答案
从回答事实证明,我应该使用(ad-get-args 0)
而不是(ad-get-args)
在(defadvice my-add ..)
中,我应该使用 ad-do-it
而不是(ad-do-it)
in (defadvice my-substract ..)
。
最好使用 trace-function
。
From the answers it turns out that I should have used (ad-get-args 0)
instead of (ad-get-args)
in (defadvice my-add ..)
, and I should have used ad-do-it
instead of (ad-do-it)
in in (defadvice my-substract ..)
.
And it's better to use trace-function
.
推荐答案
你的代码有两个问题。
首先(如你所说),你正在使用 ad-get-args
。文件说:
You have two problems in your code.
First (as you noted), you're using ad-get-args
incorrectly. The docs say:
(ad-get-args< position>)
返回从< position>
开始的实际参数
的列表。
(ad-get-args <position>)
will return the list of actual arguments supplied starting at<position>
.
看起来你想要的是:
(defadvice my-add (after my-log-on activate)
(message "my-add: %s" (ad-get-args 0)))
在您的 my-subtract
中,问题是您使用 ad-do-it
,你把它包围着圆括号,不应该是。这是正确的用法:
In your my-subtract
, the problem is your use of ad-do-it
, you have it surrounded by parentheses, it should not be. This is the correct use:
(defadvice my-substract (around my-log-on activate)
(message "my-substract: %s" (ad-get-arg 0))
ad-do-it)
从咨询库中的文档:
一个周围的建议可以指定
形式的包裹或包围
表单应与特殊的
关键字ad-do-it
一起运行,这将是
替换为$ $ c> progn 包含
包围的代码的形式。
An around advice can specify where the forms of the wrapped or surrounded forms should go with the special keyword
ad-do-it
, which will be substituted with aprogn
containing the forms of the surrounded code.
最好的教程和我发现的建议介绍是在建议库本身(在开头的评论中)。
The best tutorial and introduction to advice I've found is in the advice library itself (in the comments in the beginning).
M-x find-library advice RET
这篇关于Emacs Lisp:如何使用ad-get-arg和ad-get-args?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!