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
.
where
. See my answer. That'll have to be the extent of my contribution for quite some time - I'm not sure when I'll get around to actually proposing that as a patch, but at least now we know more of what that patch might look like.
– mtraceur
Dec 29 '23 at 04:31
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.
advice-member-p
has the undocumented feature that when it returns non-nil
, the return value is advice information!
So:
If you're just interested in a boolean "does this function have *any" advice on it?":
(defun has-advice-p (symbol)
(let ((has-advice nil))
(advice-mapc (lambda (&rest _) (setq has-advice t)) symbol)
has-advice))
If you want to check how a function was advised (:before
, :around
, :filter-return
, [...]?) by a specific piece of advice:
(defun advice-how (function symbol)
(when-let (advice (advice-member-p function symbol))
(aref (aref advice 2) 2)))
(In earlier versions of Emacs this was known as "where" instead of "how".)
If you want to know all of the advice for a function, combine advice-mapc
with the above advice-how
:
(defun advice-list (symbol)
(let ((advice-list ()))
(advice-mapc
(apply-partially
(lambda (symbol function properties)
(let ((how (advice-how function symbol))
(advice ()))
(when properties
(push properties advice))
(push function advice)
(push how advice)
(push advice advice-list)))
symbol)
symbol)
advice-list))
(Amusingly, the help text shown by describe-function
(C-h f) doesn't actually query advice information, it's just added into the docstring by advice--make-single-doc
.)
M-x describe-function
akaC-h f
– lawlist Jan 08 '18 at 17:47advice-add
/advice-remove
updates function's documentation accordingly, thusC-h f
works. From Lisp, take a look atadvice-mapc
andadvice-member-p
. – xuchunyang Jan 08 '18 at 18:15advice-add
) or the old (e.g.,defadvice
). – Drew Jan 08 '18 at 19:16