all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* defadvice and called-interactively-p
@ 2012-06-06  7:03 Frank Fischer
  2012-06-06  7:26 ` Eric Abrahamsen
  0 siblings, 1 reply; 8+ messages in thread
From: Frank Fischer @ 2012-06-06  7:03 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,

I have a problem when advising a function/command that uses
`called-interactively-p' but I have not been able to find a solution.

Suppose you have a command calling `called-interactively-p'

(defun myfunc ()
  (interactive)
  (message "MYFUNC %s" (called-interactively-p 'any)))

Now, when executing M-x myfunc RET the message line shows "MYFUNC t"
as expected. But when that function is advised

(defadvice myfunc (around around-myfunc activate)
  ad-do-it)

executing M-x myfunc RET again shows "MYFUNC nil" this time. The
disassembled code of the advised function reads

0            constant  nil
1            varbind     ad-return-value
2            constant  ad-Orig-myfunc
3            call        0
4            dup
5            varset       ad-return-value
6            unbind         1
7            return

If I interpret that code correctly (I'm not sure) then the original
definition of `myfunc' is called in line 3 using something similar to
(funcall 'ad-Orig-myfunc) where `ad-Orig-myfunc' is the renamed
original function. This would explain (IMO) why
`called-interactivly-p' returns nil -- the original body has *not*
been called interactively but using a usual non-interactive function
call.

But of course, if the (interactive) behavior of `myfunc' depends on
the return value of `called-interactively-p' this would make the
advised function unusable in interactive calls.

Is there some way to make `called-interactively-p' behave "correctly"
in the advised body?

Thanks is advance,
Frank





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

* Re: defadvice and called-interactively-p
  2012-06-06  7:03 defadvice and called-interactively-p Frank Fischer
@ 2012-06-06  7:26 ` Eric Abrahamsen
  2012-06-06  7:35   ` Frank Fischer
  0 siblings, 1 reply; 8+ messages in thread
From: Eric Abrahamsen @ 2012-06-06  7:26 UTC (permalink / raw)
  To: help-gnu-emacs

On Wed, Jun 06 2012, Frank Fischer wrote:

> Hi,
>
> I have a problem when advising a function/command that uses
> `called-interactively-p' but I have not been able to find a solution.
>
> Suppose you have a command calling `called-interactively-p'
>
> (defun myfunc ()
>   (interactive)
>   (message "MYFUNC %s" (called-interactively-p 'any)))
>
> Now, when executing M-x myfunc RET the message line shows "MYFUNC t"
> as expected. But when that function is advised
>
> (defadvice myfunc (around around-myfunc activate)
>   ad-do-it)

While you're waiting for an answer from someone who really understand
how all this works, I've found that I need to add an (interactive)
statement to the defadvice form as well, in order to preserve
interactive state. Give it a shot, anyway!

Eric

> executing M-x myfunc RET again shows "MYFUNC nil" this time. The
> disassembled code of the advised function reads
>
> 0            constant  nil
> 1            varbind     ad-return-value
> 2            constant  ad-Orig-myfunc
> 3            call        0
> 4            dup
> 5            varset       ad-return-value
> 6            unbind         1
> 7            return
>
> If I interpret that code correctly (I'm not sure) then the original
> definition of `myfunc' is called in line 3 using something similar to
> (funcall 'ad-Orig-myfunc) where `ad-Orig-myfunc' is the renamed
> original function. This would explain (IMO) why
> `called-interactivly-p' returns nil -- the original body has *not*
> been called interactively but using a usual non-interactive function
> call.
>
> But of course, if the (interactive) behavior of `myfunc' depends on
> the return value of `called-interactively-p' this would make the
> advised function unusable in interactive calls.
>
> Is there some way to make `called-interactively-p' behave "correctly"
> in the advised body?
>
> Thanks is advance,
> Frank
>
>
>
>

-- 
GNU Emacs 24.1.50.1 (i686-pc-linux-gnu, GTK+ Version 2.24.10)
 of 2012-06-06 on pellet




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

* Re: defadvice and called-interactively-p
  2012-06-06  7:26 ` Eric Abrahamsen
@ 2012-06-06  7:35   ` Frank Fischer
  2012-06-06  7:55     ` Eric Abrahamsen
  0 siblings, 1 reply; 8+ messages in thread
From: Frank Fischer @ 2012-06-06  7:35 UTC (permalink / raw)
  To: help-gnu-emacs

On 2012-06-06, Eric Abrahamsen <eric@ericabrahamsen.net> wrote:
> On Wed, Jun 06 2012, Frank Fischer wrote:
>
>> Hi,
>>
>> I have a problem when advising a function/command that uses
>> `called-interactively-p' but I have not been able to find a solution.
>>
>> Suppose you have a command calling `called-interactively-p'
>>
>> (defun myfunc ()
>>   (interactive)
>>   (message "MYFUNC %s" (called-interactively-p 'any)))
>>
>> Now, when executing M-x myfunc RET the message line shows "MYFUNC t"
>> as expected. But when that function is advised
>>
>> (defadvice myfunc (around around-myfunc activate)
>>   ad-do-it)
>
> While you're waiting for an answer from someone who really understand
> how all this works, I've found that I need to add an (interactive)
> statement to the defadvice form as well, in order to preserve
> interactive state. Give it a shot, anyway!

I've tried this and it does not make a difference. And according to the
elisp manual it should not do: 17.10 states 

      The interactive form is present if the original function or some
      piece of advice specifies one.  
      
Anyway, the result is the same.

Frank




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

* Re: defadvice and called-interactively-p
  2012-06-06  7:35   ` Frank Fischer
@ 2012-06-06  7:55     ` Eric Abrahamsen
  2012-06-06  8:11       ` Frank Fischer
  0 siblings, 1 reply; 8+ messages in thread
From: Eric Abrahamsen @ 2012-06-06  7:55 UTC (permalink / raw)
  To: help-gnu-emacs

On Wed, Jun 06 2012, Frank Fischer wrote:

> On 2012-06-06, Eric Abrahamsen <eric@ericabrahamsen.net> wrote:
>> On Wed, Jun 06 2012, Frank Fischer wrote:
>>
>>> Hi,
>>>
>>> I have a problem when advising a function/command that uses
>>> `called-interactively-p' but I have not been able to find a solution.
>>>
>>> Suppose you have a command calling `called-interactively-p'
>>>
>>> (defun myfunc ()
>>>   (interactive)
>>>   (message "MYFUNC %s" (called-interactively-p 'any)))
>>>
>>> Now, when executing M-x myfunc RET the message line shows "MYFUNC t"
>>> as expected. But when that function is advised
>>>
>>> (defadvice myfunc (around around-myfunc activate)
>>>   ad-do-it)
>>
>> While you're waiting for an answer from someone who really understand
>> how all this works, I've found that I need to add an (interactive)
>> statement to the defadvice form as well, in order to preserve
>> interactive state. Give it a shot, anyway!
>
> I've tried this and it does not make a difference. And according to the
> elisp manual it should not do: 17.10 states 
>
>       The interactive form is present if the original function or some
>       piece of advice specifies one.  
>       
> Anyway, the result is the same.

Yup, it was just a guess. Another thing you're probably already
perfectly aware of: the docstring for called-interactively-p says,

"This function is meant for implementing advice and other
function-modifying features."

If called-interactively-p is meant to go in the advice, not the
original functions, the behavior you're seeing is probably the whole
point of how it works.

Okay, enough from me… 


-- 
GNU Emacs 24.1.50.1 (i686-pc-linux-gnu, GTK+ Version 2.24.10)
 of 2012-06-06 on pellet




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

* Re: defadvice and called-interactively-p
  2012-06-06  7:55     ` Eric Abrahamsen
@ 2012-06-06  8:11       ` Frank Fischer
  2012-06-08  0:03         ` Juanma Barranquero
  0 siblings, 1 reply; 8+ messages in thread
From: Frank Fischer @ 2012-06-06  8:11 UTC (permalink / raw)
  To: help-gnu-emacs

On 2012-06-06, Eric Abrahamsen <eric@ericabrahamsen.net> wrote:
> Yup, it was just a guess. Another thing you're probably already
> perfectly aware of: the docstring for called-interactively-p says,
>
> "This function is meant for implementing advice and other
> function-modifying features."
>
> If called-interactively-p is meant to go in the advice, not the
> original functions, the behavior you're seeing is probably the whole
> point of how it works.

Yes I know this docstring. On the other hand the elisp manual says in 21.4

   The above method with the additional argument is usually best,
   because it allows callers to say "treat this call as interactive."
   But you can also do the job by testing `called-interactively-p'.
   
So even if using `called-interactively-p' is not the "recommended"
way, it sounds to me as if it is a completely valid one. Bug there's
nothing that suggests using `called-interactively-p' is dangerous when
used in an advised function (at least I did not find something in the
whole manual).

But the problem is that I cannot change/redefine the command that uses
`called-interactively-p' (that's the whole point of using an advice at
all), so changing this function is not an option. Of course, the
question arises if a function using `called-interactively-p' should be
considered being broken ... ?

Thanks for you comments,
Frank




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

* Re: defadvice and called-interactively-p
  2012-06-06  8:11       ` Frank Fischer
@ 2012-06-08  0:03         ` Juanma Barranquero
  2012-06-08  6:45           ` Frank Fischer
  0 siblings, 1 reply; 8+ messages in thread
From: Juanma Barranquero @ 2012-06-08  0:03 UTC (permalink / raw)
  To: Frank Fischer; +Cc: help-gnu-emacs

On Wed, Jun 6, 2012 at 10:11 AM, Frank Fischer
<frank-fischer@shadow-soft.de> wrote:

> But the problem is that I cannot change/redefine the command that uses
> `called-interactively-p' (that's the whole point of using an advice at
> all), so changing this function is not an option.

As an horrible kludge, if you really need a way to make it work and
don't mind the ugliness:

(defadvice myfunc (around around-myfunc activate)
  (if (called-interactively-p 'any)
      (call-interactively (ad-get-orig-definition 'myfunc))
    ad-do-it))

    Juanma



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

* Re: defadvice and called-interactively-p
  2012-06-08  0:03         ` Juanma Barranquero
@ 2012-06-08  6:45           ` Frank Fischer
  2012-06-08  7:02             ` Juanma Barranquero
  0 siblings, 1 reply; 8+ messages in thread
From: Frank Fischer @ 2012-06-08  6:45 UTC (permalink / raw)
  To: help-gnu-emacs

On 2012-06-08, Juanma Barranquero <lekktu@gmail.com> wrote:
> As an horrible kludge, if you really need a way to make it work and
> don't mind the ugliness:
>
> (defadvice myfunc (around around-myfunc activate)
>   (if (called-interactively-p 'any)
>       (call-interactively (ad-get-orig-definition 'myfunc))
>     ad-do-it))

Thanks a lot, this is exactly what I'm looking for. No idea why I
didn't find `ad-get-orig-definition' on my own :)

Frank




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

* Re: defadvice and called-interactively-p
  2012-06-08  6:45           ` Frank Fischer
@ 2012-06-08  7:02             ` Juanma Barranquero
  0 siblings, 0 replies; 8+ messages in thread
From: Juanma Barranquero @ 2012-06-08  7:02 UTC (permalink / raw)
  To: Frank Fischer; +Cc: help-gnu-emacs

On Fri, Jun 8, 2012 at 8:45 AM, Frank Fischer
<frank-fischer@shadow-soft.de> wrote:

> Thanks a lot, this is exactly what I'm looking for. No idea why I
> didn't find `ad-get-orig-definition' on my own :)

Likely because your brain is up to higher aesthetic standards than mine.

    Juanma



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

end of thread, other threads:[~2012-06-08  7:02 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-06-06  7:03 defadvice and called-interactively-p Frank Fischer
2012-06-06  7:26 ` Eric Abrahamsen
2012-06-06  7:35   ` Frank Fischer
2012-06-06  7:55     ` Eric Abrahamsen
2012-06-06  8:11       ` Frank Fischer
2012-06-08  0:03         ` Juanma Barranquero
2012-06-08  6:45           ` Frank Fischer
2012-06-08  7:02             ` Juanma Barranquero

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.