all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* proper use of add-function
@ 2018-05-22 22:58 Eric Abrahamsen
  2018-05-22 23:18 ` Noam Postavsky
  2018-05-22 23:31 ` Michael Heerdegen
  0 siblings, 2 replies; 8+ messages in thread
From: Eric Abrahamsen @ 2018-05-22 22:58 UTC (permalink / raw
  To: help-gnu-emacs

And another very basic question:

I can't get `add-function' to do its thing. I want to add a :filter-args
function to #'canonically-space-region, and because this is a minor
mode, I want it set locally. I've tried everything I can think of, but
either get no results (my advice function isn't called) or an error
saying that it wants a symbol, but I gave it a lambda. If I give it a
symbol, nothing happens.

(add-function
   :filter-args
   (local 'canonically-space-region)
   #'my-canonical-space-region)

(defun my-canonical-space-region (bounds)
  ;; etc
  (list (car bounds) (nth 1 bounds)))

What's wrong with this?

Thanks,
Eric




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

* Re: proper use of add-function
  2018-05-22 22:58 proper use of add-function Eric Abrahamsen
@ 2018-05-22 23:18 ` Noam Postavsky
  2018-05-22 23:31 ` Michael Heerdegen
  1 sibling, 0 replies; 8+ messages in thread
From: Noam Postavsky @ 2018-05-22 23:18 UTC (permalink / raw
  To: Eric Abrahamsen; +Cc: Help Gnu Emacs mailing list

On 22 May 2018 at 18:58, Eric Abrahamsen <eric@ericabrahamsen.net> wrote:

> (add-function
>    :filter-args
>    (local 'canonically-space-region)
>    #'my-canonical-space-region)

You're adding to the function value in the (non-existent) buffer-local
variable `canonically-space-region'

canonically-space-region’s value is
#f(advice-wrapper :filter-args #f(compiled-function
(&rest args)
#<bytecode 0x15054ad>)
my-canonical-space-region)
Local in buffer *scratch*; globally void

You probably want advice-add instead:

(advice-add 'canonically-space-region
        :filter-args
        #'my-canonical-space-region)

Note: this affects canonically-space-region globally, so you have to
change the implementation of my-canonical-space-region so that it's a
nop except when operating in the relevant buffers.



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

* Re: proper use of add-function
  2018-05-22 22:58 proper use of add-function Eric Abrahamsen
  2018-05-22 23:18 ` Noam Postavsky
@ 2018-05-22 23:31 ` Michael Heerdegen
  2018-05-22 23:36   ` Eric Abrahamsen
  1 sibling, 1 reply; 8+ messages in thread
From: Michael Heerdegen @ 2018-05-22 23:31 UTC (permalink / raw
  To: Eric Abrahamsen; +Cc: help-gnu-emacs

Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> And another very basic question:
>
> I can't get `add-function' to do its thing. I want to add a :filter-args
> function to #'canonically-space-region, and because this is a minor
> mode, I want it set locally.

I guess there is a problem with what you want to achieve: we have no
buffer local function bindings of symbols.

> (add-function
>    :filter-args
>    (local 'canonically-space-region)
>    #'my-canonical-space-region)
>
> (defun my-canonical-space-region (bounds)
>   ;; etc
>   (list (car bounds) (nth 1 bounds)))

Technically ok, but this tries to bind the (value cell of the) symbol
canonically-space-region.

What I typically do in this (quite common) scenario is to install the
advice (globally) and check for the mode in the `current-buffer' in the
advice.  I guess there is no alternative unless you find a different way
to reach what you want (like advising `canonical-space-region-function',
which doesn't exist).


Michael.



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

* Re: proper use of add-function
  2018-05-22 23:31 ` Michael Heerdegen
@ 2018-05-22 23:36   ` Eric Abrahamsen
  2018-05-22 23:58     ` Michael Heerdegen
  0 siblings, 1 reply; 8+ messages in thread
From: Eric Abrahamsen @ 2018-05-22 23:36 UTC (permalink / raw
  To: help-gnu-emacs

Michael Heerdegen <michael_heerdegen@web.de> writes:

> Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>
>> And another very basic question:
>>
>> I can't get `add-function' to do its thing. I want to add a :filter-args
>> function to #'canonically-space-region, and because this is a minor
>> mode, I want it set locally.
>
> I guess there is a problem with what you want to achieve: we have no
> buffer local function bindings of symbols.
>
>> (add-function
>>    :filter-args
>>    (local 'canonically-space-region)
>>    #'my-canonical-space-region)
>>
>> (defun my-canonical-space-region (bounds)
>>   ;; etc
>>   (list (car bounds) (nth 1 bounds)))
>
> Technically ok, but this tries to bind the (value cell of the) symbol
> canonically-space-region.
>
> What I typically do in this (quite common) scenario is to install the
> advice (globally) and check for the mode in the `current-buffer' in the
> advice.  I guess there is no alternative unless you find a different way
> to reach what you want (like advising `canonical-space-region-function',
> which doesn't exist).

Okay, I see what you and Noam are saying, and in fact what the docstring
is saying. It's just pretty weird that `add-function' works on
variables, and `advice-add' works on functions. It's counterintuitive,
and I wonder if the docs couldn't make that more explicit. It's also a
bummer that my minor mode clobbers things globally, but I guess there's
no great harm done.

Thanks to both of you for being on call!

Eric




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

* Re: proper use of add-function
  2018-05-22 23:36   ` Eric Abrahamsen
@ 2018-05-22 23:58     ` Michael Heerdegen
  2018-05-23  0:09       ` Eric Abrahamsen
  0 siblings, 1 reply; 8+ messages in thread
From: Michael Heerdegen @ 2018-05-22 23:58 UTC (permalink / raw
  To: Eric Abrahamsen; +Cc: help-gnu-emacs

Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> Okay, I see what you and Noam are saying, and in fact what the docstring
> is saying. It's just pretty weird that `add-function' works on
> variables, and `advice-add' works on functions.

Actually `add-function' works for "places" (including
`symbol-function'), so it's the more general and more low-level tool.
`advice-add' is higher-level and specialized on function names.

> It's counterintuitive, and I wonder if the docs couldn't make that
> more explicit.

I always found the doc ok (and explicit), but maybe the names are a bit
arbitrary.


Michael.



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

* Re: proper use of add-function
  2018-05-22 23:58     ` Michael Heerdegen
@ 2018-05-23  0:09       ` Eric Abrahamsen
  2018-05-23  0:19         ` Noam Postavsky
  0 siblings, 1 reply; 8+ messages in thread
From: Eric Abrahamsen @ 2018-05-23  0:09 UTC (permalink / raw
  To: help-gnu-emacs

Michael Heerdegen <michael_heerdegen@web.de> writes:

> Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>
>> Okay, I see what you and Noam are saying, and in fact what the docstring
>> is saying. It's just pretty weird that `add-function' works on
>> variables, and `advice-add' works on functions.
>
> Actually `add-function' works for "places" (including
> `symbol-function'), so it's the more general and more low-level tool.
> `advice-add' is higher-level and specialized on function names.

I guess that's why I kept trying to make this work -- I thought the
`symbol-function' place would allow me to apply my advice to
'canonically-space-region. Why doesn't that work?




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

* Re: proper use of add-function
  2018-05-23  0:09       ` Eric Abrahamsen
@ 2018-05-23  0:19         ` Noam Postavsky
  2018-05-23  0:29           ` Eric Abrahamsen
  0 siblings, 1 reply; 8+ messages in thread
From: Noam Postavsky @ 2018-05-23  0:19 UTC (permalink / raw
  To: Eric Abrahamsen; +Cc: Help Gnu Emacs mailing list

On 22 May 2018 at 20:09, Eric Abrahamsen <eric@ericabrahamsen.net> wrote:
> Michael Heerdegen <michael_heerdegen@web.de> writes:
>
>> Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>>
>>> Okay, I see what you and Noam are saying, and in fact what the docstring
>>> is saying. It's just pretty weird that `add-function' works on
>>> variables, and `advice-add' works on functions.

Yeah, this isn't the first time I've seen confusion over this (e.g.,
Bug#30241). The docstring is clear enough when you already know what
it says, but I've just added an extra note that should help guide
people toward advice-add.

[1: e3f00f5637]: 2018-05-22 20:08:01 -0400
  Clarify when to use advice-add vs add-function
  https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=e3f00f5637a2790923a9c4c1d4b7dbf65027d8ce

>> Actually `add-function' works for "places" (including
>> `symbol-function'), so it's the more general and more low-level tool.
>> `advice-add' is higher-level and specialized on function names.
>
> I guess that's why I kept trying to make this work -- I thought the
> `symbol-function' place would allow me to apply my advice to
> 'canonically-space-region. Why doesn't that work?

(add-function
   :filter-args
   (symbol-function 'canonically-space-region)
   #'my-canonical-space-region)

This seems to work for me (although advice-add is preferable for
reasons listed in the manual).



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

* Re: proper use of add-function
  2018-05-23  0:19         ` Noam Postavsky
@ 2018-05-23  0:29           ` Eric Abrahamsen
  0 siblings, 0 replies; 8+ messages in thread
From: Eric Abrahamsen @ 2018-05-23  0:29 UTC (permalink / raw
  To: Noam Postavsky; +Cc: Help Gnu Emacs mailing list


On 05/22/18 20:19 PM, Noam Postavsky wrote:
> On 22 May 2018 at 20:09, Eric Abrahamsen <eric@ericabrahamsen.net> wrote:
>> Michael Heerdegen <michael_heerdegen@web.de> writes:
>>
>>> Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>>>
>>>> Okay, I see what you and Noam are saying, and in fact what the docstring
>>>> is saying. It's just pretty weird that `add-function' works on
>>>> variables, and `advice-add' works on functions.
>
> Yeah, this isn't the first time I've seen confusion over this (e.g.,
> Bug#30241). The docstring is clear enough when you already know what
> it says, but I've just added an extra note that should help guide
> people toward advice-add.
>
> [1: e3f00f5637]: 2018-05-22 20:08:01 -0400
>   Clarify when to use advice-add vs add-function
>   https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=e3f00f5637a2790923a9c4c1d4b7dbf65027d8ce

Thanks for doing that. That was definitely a "makes sense when you
already understand it" situation.

>>> Actually `add-function' works for "places" (including
>>> `symbol-function'), so it's the more general and more low-level tool.
>>> `advice-add' is higher-level and specialized on function names.
>>
>> I guess that's why I kept trying to make this work -- I thought the
>> `symbol-function' place would allow me to apply my advice to
>> 'canonically-space-region. Why doesn't that work?
>
> (add-function
>    :filter-args
>    (symbol-function 'canonically-space-region)
>    #'my-canonical-space-region)
>
> This seems to work for me (although advice-add is preferable for
> reasons listed in the manual).

Right, I get that now. I was only stuck on `add-function' because I was
under the impression that it would get me buffer-local behavior, which I
understand now it won't.

Thanks,
Eric



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

end of thread, other threads:[~2018-05-23  0:29 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-05-22 22:58 proper use of add-function Eric Abrahamsen
2018-05-22 23:18 ` Noam Postavsky
2018-05-22 23:31 ` Michael Heerdegen
2018-05-22 23:36   ` Eric Abrahamsen
2018-05-22 23:58     ` Michael Heerdegen
2018-05-23  0:09       ` Eric Abrahamsen
2018-05-23  0:19         ` Noam Postavsky
2018-05-23  0:29           ` Eric Abrahamsen

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.