Emacs Lisp: How to use ad-get-arg and ad-get-args?

I'm not sure if I am using ad-get-args and ad-get-arg correctly.

For example, the following code doesn't work.

(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 throws an error:

Debugger entered--Lisp error: (void-function ad-get-args).

      

The following does not work either.

(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)

      

Defossing gives 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))()

      

I tried to use defadvice to view the start-process arguments for debugging purposes and I found that my way of using ad-get-arg was not working.

Update: answer,

From the answers it turns out that I should have used (ad-get-args 0)

instead (ad-get-args)

in (defadvice my-add ..)

, and I should have used ad-do-it

instead (ad-do-it)

in (defadvice my-substract ..)

. And it's better to use trace-function

.

+2


source to share


3 answers


You have two problems in your code. At first (as you noted) you are using it ad-get-args

wrong. The docs say:

(ad-get-args <position>)

will return a list of actual arguments starting at <position>

.

It looks like what you want:

(defadvice my-add (after my-log-on activate)
  (message "my-add: %s" (ad-get-args 0)))

      

Your my-subtract

problem is in your use ad-do-it

, you are surrounded by parentheses, it shouldn't be. This is the correct usage:



(defadvice my-substract (around my-log-on activate)
  (message "my-substract: %s" (ad-get-arg 0))
  ad-do-it)

      

From the docs in the tips library:

The tip can be used to specify where the wrapped or enclosed forms should come with a special keyword ad-do-it

to be replaced with a progn

containing enclosed code forms.

The best tutorial and introduction to advice I have found is in the Soviet library itself (in the comments at the beginning).

M-x find-library advice RET

      

+7


source


It works:

(defun my-add (a b) 
  (+ a b))

(defadvice my-add (after my-log-on activate) 
  (message "my-add: %d %d" (ad-get-arg 0) (ad-get-arg 1)))

(my-add 1 2) 

      

You should consider the type of argument you are retrieving when passing it to the message function. I think the errors you are getting are hidden due to the fact that they occur in the tips. If the errors were not on the tip, you will see a clearer message indicating the type mismatch.



If you are in doubt or when you pass the arg argument, which is not a line in message

use(prin1-to-string arg)

like this:

(defadvice my-add (after my-log-on activate) 
  (message "my-add: %s %s"
           (prin1-to-string (ad-get-arg 0))
           (prin1-to-string (ad-get-arg 1))))

      

+3


source


There is no need to use ad-get-arg , you can use the same names in the body of the advice:

(defun my-add (a b)
  (+ a b))
(defadvice my-add (after my-add-log activate)
  (message "my-add: %d %d" a b))

      

Update

If you just want to track function calls for debugging purposes, emacs can generate a consult for you:

(defun my-add (a b)
  (+ a b))
(trace-function 'my-add) 

      

+3


source







All Articles