For variety, here's a solution using the :around
advice.
Copy the below test snippet to the *scratch* buffer and evaluate the progn
form.
(progn
(defvar last-enabled-foo nil)
(setq last-enabled-foo nil)
(defun enable-foo (foo)
(message "last-enabled-foo = %S" last-enabled-foo))
(defun adv/enable-foo (orig-fun &rest args)
(setq last-enabled-foo args)
(apply orig-fun args))
(advice-add 'enable-foo :around #'adv/enable-foo)
;; Comment the below line to see the advice in effect
(advice-remove 'enable-foo #'adv/enable-foo)
(enable-foo 9))
With the way it is right now, you will always see the output as:
last-enabled-foo = nil
This is because we are setting the last-enabled-foo
value to nil
and the value 9
passed to enable-foo
is not being assigned to last-enabled-foo
.
(setq last-enabled-foo nil)
(defun enable-foo (foo)
(message "last-enabled-foo = %S" last-enabled-foo))
(enable-foo 9)
The advice code is ineffective as we are adding an advice and then again removing the same.
Now comment out the (advice-remove ..)
line. Now the advice will be in effect and you will see the magic when you re-evaluate that same progn
form.
Now, the output will be:
last-enabled-foo = (9)
Now, even though the setq
form is setting the last-enabled-foo
value to nil
during each progn
evaluation, it is being set to the enable-foo
argument args
inside the advice function adv/enable-foo
.
(defun adv/enable-foo (orig-fun &rest args)
(setq last-enabled-foo args)
(apply orig-fun args))
References: