unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* How to get advised function name form inside of the advice
@ 2016-03-04 13:36 Vitalie Spinu
  2016-03-04 14:16 ` Marcin Borkowski
  0 siblings, 1 reply; 7+ messages in thread
From: Vitalie Spinu @ 2016-03-04 13:36 UTC (permalink / raw)
  To: emacs-devel



Hi,

How to retrieve the name of the original function from the inside of "new"
advice? I need to condition-case a function and show a meaningful error message
with function name instead of an error.

For concreteness:

    
    (defun tt (a b)
      (message "in: (%s . %d)" a b))
    
    (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)


Thanks,

  Vitalie




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to get advised function name form inside of the advice
  2016-03-04 13:36 How to get advised function name form inside of the advice Vitalie Spinu
@ 2016-03-04 14:16 ` Marcin Borkowski
  2016-03-04 16:56   ` Vitalie Spinu
  0 siblings, 1 reply; 7+ messages in thread
From: Marcin Borkowski @ 2016-03-04 14:16 UTC (permalink / raw)
  To: Vitalie Spinu; +Cc: emacs-devel


On 2016-03-04, at 14:36, Vitalie Spinu <spinuvit@gmail.com> wrote:

> Hi,
>
> How to retrieve the name of the original function from the inside of "new"
> advice? I need to condition-case a function and show a meaningful error message
> with function name instead of an error.

It seems that you can't do that (and for good reasons).  See here:
https://lists.gnu.org/archive/html/help-gnu-emacs/2016-02/msg00218.html

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to get advised function name form inside of the advice
  2016-03-04 14:16 ` Marcin Borkowski
@ 2016-03-04 16:56   ` Vitalie Spinu
  2016-03-04 18:30     ` Michael Heerdegen
                       ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Vitalie Spinu @ 2016-03-04 16:56 UTC (permalink / raw)
  To: Marcin Borkowski; +Cc: emacs-devel


>> On Fri, Mar 04 2016 15:16, Marcin Borkowski wrote:

> On 2016-03-04, at 14:36, Vitalie Spinu <spinuvit@gmail.com> wrote:

>> Hi,
>>
>> How to retrieve the name of the original function from the inside of "new"
>> advice? I need to condition-case a function and show a meaningful error message
>> with function name instead of an error.

> It seems that you can't do that (and for good reasons).  See here:
> https://lists.gnu.org/archive/html/help-gnu-emacs/2016-02/msg00218.html


Hm, I thought so. Thanks.

What are those good reasons though? I don't see any reasons given in that
thread.

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.

 Vitalie



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to get advised function name form inside of the advice
  2016-03-04 16:56   ` Vitalie Spinu
@ 2016-03-04 18:30     ` Michael Heerdegen
  2016-03-04 18:32     ` Marcin Borkowski
  2016-03-08  4:22     ` Stefan Monnier
  2 siblings, 0 replies; 7+ messages in thread
From: Michael Heerdegen @ 2016-03-04 18:30 UTC (permalink / raw)
  To: emacs-devel

Vitalie Spinu <spinuvit@gmail.com> writes:

> What are those good reasons though? I don't see any reasons given in that
> thread.

I remember that I gave a reason why this is not a genuine feature of
nadvice's advice implemention.

But of course it could make sense to add something like that to Emacs.
What do you think would be the typical use cases?


Michael.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to get advised function name form inside of the advice
  2016-03-04 16:56   ` Vitalie Spinu
  2016-03-04 18:30     ` Michael Heerdegen
@ 2016-03-04 18:32     ` Marcin Borkowski
  2016-03-08  4:22     ` Stefan Monnier
  2 siblings, 0 replies; 7+ messages in thread
From: Marcin Borkowski @ 2016-03-04 18:32 UTC (permalink / raw)
  To: Vitalie Spinu; +Cc: emacs-devel


On 2016-03-04, at 17:56, Vitalie Spinu <spinuvit@gmail.com> wrote:

>>> On Fri, Mar 04 2016 15:16, Marcin Borkowski wrote:
>
>> On 2016-03-04, at 14:36, Vitalie Spinu <spinuvit@gmail.com> wrote:
>
>>> Hi,
>>>
>>> How to retrieve the name of the original function from the inside of "new"
>>> advice? I need to condition-case a function and show a meaningful error message
>>> with function name instead of an error.
>
>> It seems that you can't do that (and for good reasons).  See here:
>> https://lists.gnu.org/archive/html/help-gnu-emacs/2016-02/msg00218.html
>
>
> Hm, I thought so. Thanks.
>
> What are those good reasons though? I don't see any reasons given in that
> thread.

Because they are not given there.  The main reason is: this is Lisp,
a function can have one name, a few names (neither of which is "main" or
"canonical"), or no name.

> 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.

This is exactly what I have done.  See here:
https://lists.gnu.org/archive/html/help-gnu-emacs/2016-02/msg00219.html
for my approach (including a stupid mistake of using `make-symbol'
instead if `intern', promptly corrected by Michael H.)

>  Vitalie

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to get advised function name form inside of the advice
  2016-03-04 16:56   ` Vitalie Spinu
  2016-03-04 18:30     ` Michael Heerdegen
  2016-03-04 18:32     ` Marcin Borkowski
@ 2016-03-08  4:22     ` Stefan Monnier
  2016-03-21  1:33       ` Vitalie Spinu
  2 siblings, 1 reply; 7+ messages in thread
From: Stefan Monnier @ 2016-03-08  4:22 UTC (permalink / raw)
  To: emacs-devel

> (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




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to get advised function name form inside of the advice
  2016-03-08  4:22     ` Stefan Monnier
@ 2016-03-21  1:33       ` Vitalie Spinu
  0 siblings, 0 replies; 7+ messages in thread
From: Vitalie Spinu @ 2016-03-21  1:33 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel


Hi Stefan,

Sorry for coming late on this. Your reply didn't reach my mailbox.

Yes. The use case is your second example of multiple adviced functions and it
indeed solves the problem in a very acceptable way.

Thanks!

  Vitalie

>> On Mon, Mar 07 2016 23:22, Stefan Monnier wrote:

>> (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



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2016-03-21  1:33 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-04 13:36 How to get advised function name form inside of the advice Vitalie Spinu
2016-03-04 14:16 ` Marcin Borkowski
2016-03-04 16:56   ` Vitalie Spinu
2016-03-04 18:30     ` Michael Heerdegen
2016-03-04 18:32     ` Marcin Borkowski
2016-03-08  4:22     ` Stefan Monnier
2016-03-21  1:33       ` Vitalie Spinu

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).