Search code examples
emacselispadvising-functionsdefadvice

can defadvice use variables in the original function?


Let me take the modified example from Emacs Lisp: How to use ad-get-arg and ad-get-args?

(defun my-add (a b &optional c)
  (+ a b)
  (unless c
    (setq c 4)))
(defadvice my-add (after my-log-on (a b &optional c) activate)
  (message "my-add a: %s" a)
  (message "my-add: %s" c))

(my-add 1 2 3) will run well with output:

my-add a: 1
my-add: 3

but (my-add 1 2) cannot get the c value from original function:

my-add a: 1
my-add: nil

My question is how can I use optional arguments not provided (or more generally, the variables in the original function) in the defadvice body?


Solution

  • In general, you can't. defadvice (as well as the new advice-add) is designed to add code around a function, but does not give you access to its internals.