I want to add advice to an alias to a function in an internal elisp package at work, gating the logic on a variable.

Both the function and the alias is marked autoload.


(defun something-get ()

(defalias 'something 'something-get)

What I want is:

(defvar enable-something-advice-p nil)

(and enable-something-advice-p
     (defadvice something (before fix-something enable)
       ;;; do stuff


  1. How do I make sure the advice takes effect? Just mark autoload?
  2. How do I make the advice conditional?

Don't make the advice conditional, but only its effect:

(defadvice something (before fix-something enable)
  (when enable-something-advice-p
    ;;; do stuff

Tho of course I'd use advice-add instead:

(advice-add 'something :before
  (lambda ()
    (when enable-something-advice-p
      ;;; do stuff

Pieces of advice can be installed before the function is defined (they even survive the function's redefinition), so the fact that the function is autoloaded is not a problem at all.

  • Does the (defadvice) or (advise-add) need to be marked `;;;autoload' to be made generally available? – Realraptor Jan 8 '18 at 17:39
  • No, unless you want that advice to be enabled before the file in which it's located is loaded (this would be very unusual). – Stefan Jan 8 '18 at 23:18
  • I still don't understand. (something) is autoloaded. If this is called before a call to require(), is the advice attached? – Realraptor Jan 10 '18 at 19:20
  • 1
    @Realraptor: Advising as-yet-undefined functions is known as "forward advice", and it basically Just Works (i.e. you don't need to care whether or not the target function is defined). When a function gets defined, any forward advice for it will be activated. See also the commentary in advice.el under the heading "Forward advice, automatic advice activation". – phils Aug 20 '18 at 23:22
  • ...and an autoload definition is still just an autoload definition -- "advising an autoload definition" isn't a problem you need to worry about. When the autoload is triggered and the real function is defined, that's when the forward advice is processed. – phils Aug 20 '18 at 23:35

The solution to the problem was to put a conditional form around the function:

(when (fboundp 'my-function-generating-compilation-errors)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.