2

Can someone explain and show the right/preferred way of advising functions in Emacs these days.

Official documentation says to use advice-add, but I've seen people using define-advice and I checked, they are both part of nadvice.el (which is as I understood ships with Emacs).

I'm not sure, but If I remember it right I think there were more ways to advice (some of them are now considered obsolete).

Also it's known that using lambdas to add hooks is kinda frowned upon. Is that the same for advising? Then why something like this is okay (or is it?):

(define-advice mu4e~stop (:after nil kill-mu4e-layout-after-mu4e~stop)
        (when mu4e-spacemacs-kill-layout-on-exit
          (persp-kill mu4e-spacemacs-layout-name)))

It seems define-advice is a macro that uses advice-add behind the scenes, but I don't see how it makes it easier to advise functions, what am I missing?

This all is very confusing.

1

I tried pp-macroexpand-last-sexp on define-advice block and it turns out that I was right, it is a macro that uses advice-add behind the scenes, but it implicitly creates a defun (since using lambdas for hooks and advising is a "no-no"), so you don't have to do it yourself.

It's confusing because frankly define-advice doesn't seem to reduce the code that needs to be written too much.

  • define-advice is a wrapper of advice-add and defun, it provides two benefits: 1) when you supply name, it will define a function name SYMBOL@NAME, so the prefix name convention is respected automatically. 2) The code is more compact since it is defun + advice-add. – xuchunyang Jul 8 at 4:20
  • I haven't used nadvice much, but I guess the point of the define-advice form is partly that it's analogous to defadvice. (And that being the case, it could also eliminate the issue of nadvice defuns not being explicitly and obviously advice when you're browsing the code, in the way that defadvice was.) – phils Jul 8 at 9:36

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.