1

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?

Community
  • 1
  • 1
RNA
  • 146,987
  • 15
  • 52
  • 70

2 Answers2

1

Since (setq c 4) from my-add changes the environments, not given parameter, I think you cannot get what you want.

My best bet is following ugly way using special variable:

(defvar *special*)
(defun my-add (a b &optional c)
  (+ a b)
  (unless c
    (setq c 4)
    (setq *special* 4)))

(defadvice my-add (after my-log-on (a b &optional c) activate)
  (message "my-add a: %s" a)
  (if (null c)
      (message "my-add *special*: %s" *special*)
    (message "my-add: %s" c)))
Chul-Woong Yang
  • 1,223
  • 10
  • 17
1

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.

Stefan
  • 27,908
  • 4
  • 53
  • 82