[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to get advised function name form inside of the advice
From: |
Stefan Monnier |
Subject: |
Re: How to get advised function name form inside of the advice |
Date: |
Mon, 07 Mar 2016 23:22:08 -0500 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/25.1.50 (gnu/linux) |
> (defun tt-add (fun &rest args)
> (message "running advice for: %s" how_to_get_tt_name_here_)
> (apply fun args))
> (advice-add 'tt :around 'tt-add)
> I think my use case is a good reason to have such a feature. Otherwise
> one would need a macro to define separate advice function per advice
> or pass a closure as an advice. Both ways are overkill for such
> a basic requirement.
I'm not sure I understand the details of the use case. I can see two
situations:
- this tt-add advice is only used for `tt`, in which case it might be OK
for the the message to emit "tt-add" rather then "tt".
For that it would be desirable to have some kind of `current-defun-name`
macro, which could be useful more generally (not only for advices).
- this tt-add advice is used on several functions. In that
case the effort of writing something like
(defun tt-add (name fun &rest args)
(message "running advice for: %s" name)
(apply fun args))
(dolist (f '(tt tt2 tt3 tt4))
(advice-add f :around (apply-partially #'tt-add f)))
isn't that terrible.
This said, if you're motivated enough, it should be possible to write
a `current-advised-function-name` function, using the same kind of
implementation hack as called-interactively-p (i.e. walking up the
backtrace) and with similar limitations.
Stefan