I had a look over the weekend, and I cannot see an easy way to check whether a function is advised.
I must be missing something.
HELP! :)
I had a look over the weekend, and I cannot see an easy way to check whether a function is advised.
I must be missing something.
HELP! :)
As lawlist correctly points out in a comment, the Emacs Help system, invoked via C-hfNAME
RET (M-xdescribe-function
RETNAME
RET) in this case, is a quick way to interactively check whether there is any advice currently active on the function named NAME
. This is doubly convenient because the name of the advising function is hyperlinked as a button, allowing you to jump to and back from its definition.
To programmatically determine whether the function definition of a particular symbol (e.g. fn
) contains any advice[1], you can write:
(advice--p (advice--symbol-function 'fn))
Note, however, that the double hyphens indicate this is an internal API and thus subject to breaking change.
[1]: This assumes the new nadvice.el
system in Emacs 24; see (elisp) Porting old advice
.
If you want to do it from Elisp, advice-mapc
will loop through all the advice functions on a function, and call a callback for each one.
So if you're just interested in a boolean "does this function have advice on it?", you can just have the callback set a boolean a variable:
(defun has-advice-p (symbol)
(let ((has-advice nil))
(advice-mapc (lambda (&rest _) (setq has-advice t)) symbol)
has-advice))
(Note that if you want to check for a specific advice function rather than looping through all of them, you can use advice-member-p
instead.)
(Sadly, there seems to be no API to determine "where" on a function the advice is - did it get added with :before
, :around
, :filter-return
, [...]? There no public API for that information. In fact it seems that after advice is applied, the information only exists in a form that isn't super trivial to get back out - for a clue, look at advice--where-alist
and advice--where
: it looks like the only way to know is by comparing that raw bytecode. How does the help text shown by describe-function
(C-h f) know this information? It's added into the docstring by advice--make-single-doc
.)