3

Documentation for defadvice says:

around-advice is wrapped around the execution of the function

This explanation is not clear to me. So I decided to test, how it works, using this code:

(defun fun ()
  (message "hi"))

(fun)

(defadvice fun (around around-fun activate)
  (message "3"))

(fun)

Output:

hi
3

What is happening here? Why don't I see "hi" message after the advice is defined? Is function executed at all? Or the code, defined in advice is executed instead of the function?

user4035
  • 22,508
  • 11
  • 59
  • 94

1 Answers1

3

Around means that the advice is executed instead of the function. You can still call the original with ad-do-it. See info

Just to add a small example:

(defun foo (x)
  (* 2 x))

(defadvice foo (around bar activate)
  (setq ad-return-value
        (if (= x 1)
            42
          (+ 1 ad-do-it))))

(foo 1)
;; 42
(foo 2)
;; 5
(foo 3)
;; 7
abo-abo
  • 20,038
  • 3
  • 50
  • 71