GNU bug report logs -
#33601
26; Add macro `with-hook-added'
Previous Next
Reported by: Drew Adams <drew.adams <at> oracle.com>
Date: Mon, 3 Dec 2018 18:48:02 UTC
Severity: wishlist
Tags: wontfix
Done: Lars Ingebrigtsen <larsi <at> gnus.org>
Bug is archived. No further changes may be made.
To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 33601 in the body.
You can then email your comments to 33601 AT debbugs.gnu.org in the normal way.
Toggle the display of automated, internal messages from the tracker.
Report forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#33601
; Package
emacs
.
(Mon, 03 Dec 2018 18:48:02 GMT)
Full text and
rfc822 format available.
Acknowledgement sent
to
Drew Adams <drew.adams <at> oracle.com>
:
New bug report received and forwarded. Copy sent to
bug-gnu-emacs <at> gnu.org
.
(Mon, 03 Dec 2018 18:48:02 GMT)
Full text and
rfc822 format available.
Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):
Enhancement request: Please consider adding a macro such as this, to
facilitate temporary use of a hook function.
(defmacro with-hook-added (hook function &rest body)
"Evaluate forms in BODY with FUNCTION added to HOOK, then remove FUNCTION."
(declare (indent 1) (debug t))
`(unwind-protect (progn (add-hook ',hook ',function) ,@body)
(remove-hook ',hook ',function)))
In GNU Emacs 26.1 (build 1, x86_64-w64-mingw32)
of 2018-05-30
Repository revision: 07f8f9bc5a51f5aa94eb099f3e15fbe0c20ea1ea
Windowing system distributor `Microsoft Corp.', version 10.0.16299
Configured using:
`configure --without-dbus --host=x86_64-w64-mingw32
--without-compress-install 'CFLAGS=-O2 -static -g3''
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#33601
; Package
emacs
.
(Mon, 03 Dec 2018 19:02:01 GMT)
Full text and
rfc822 format available.
Message #8 received at 33601 <at> debbugs.gnu.org (full text, mbox):
Drew Adams wrote:
> Enhancement request:
Please write "Severity: wishlist" as the first line of the message body.
Also, FYI your customized bug reporting is (I assume) messing up the
version number in the subject of your reports.
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#33601
; Package
emacs
.
(Mon, 03 Dec 2018 19:04:02 GMT)
Full text and
rfc822 format available.
Message #11 received at 33601 <at> debbugs.gnu.org (full text, mbox):
Maybe a better name would be `with-function-on-hook' or
`with-hook-function-added'. It is the function, not the
hook, that is added (to the hook).
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#33601
; Package
emacs
.
(Mon, 03 Dec 2018 20:59:01 GMT)
Full text and
rfc822 format available.
Message #14 received at 33601 <at> debbugs.gnu.org (full text, mbox):
> FYI your customized bug reporting is (I assume) messing up the
> version number in the subject of your reports.
Thanks. Yes, in Emacs 26 vanilla emacsbug.el changed the
way it reports the version number in the Subject line.
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#33601
; Package
emacs
.
(Mon, 03 Dec 2018 22:39:02 GMT)
Full text and
rfc822 format available.
Message #17 received at 33601 <at> debbugs.gnu.org (full text, mbox):
On 2018-12-04 08:03, Drew Adams wrote:
> Maybe a better name would be `with-function-on-hook' or
> `with-hook-function-added'. It is the function, not the
> hook, that is added (to the hook).
True, but `with-hook-added' seems consistent with the name of
`add-hook' itself (which likewise isn't for creating hooks).
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#33601
; Package
emacs
.
(Mon, 03 Dec 2018 22:51:02 GMT)
Full text and
rfc822 format available.
Message #20 received at 33601 <at> debbugs.gnu.org (full text, mbox):
> > Maybe a better name would be `with-function-on-hook' or
> > `with-hook-function-added'. It is the function, not the
> > hook, that is added (to the hook).
>
> True, but `with-hook-added' seems consistent with the name of
> `add-hook' itself (which likewise isn't for creating hooks).
Yeah, that's why I used that name. I'm OK with any
of those names mentioned so far, and probably with
other alternatives too.
The real question is whether such a macro is useful.
I think it can be. I gave a couple examples in bug
#33595. It was those examples that made me think
such a macro might be useful.
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#33601
; Package
emacs
.
(Mon, 03 Dec 2018 23:14:01 GMT)
Full text and
rfc822 format available.
Message #23 received at 33601 <at> debbugs.gnu.org (full text, mbox):
On 2018-12-04 11:50, Drew Adams wrote:
> The real question is whether such a macro is useful.
> I think it can be. I gave a couple examples in bug
> #33595. It was those examples that made me think
> such a macro might be useful.
I certainly think it can be useful.
On a couple of occasions I've used code which does a similar thing
with advice -- adding temporary advice, running a body, and removing
the advice again.
In both cases one could argue for a more persistent hook or advice
which performs its own check to see whether it should do things, but
that might also entail writing a new wrapper to perform the test, so
there's appeal in a with-* approach (particularly for things which are
wanted only very occasionally).
-Phil
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#33601
; Package
emacs
.
(Mon, 03 Dec 2018 23:52:02 GMT)
Full text and
rfc822 format available.
Message #26 received at 33601 <at> debbugs.gnu.org (full text, mbox):
> > The real question is whether such a macro is useful.
> > I think it can be. I gave a couple examples in bug
> > #33595. It was those examples that made me think
> > such a macro might be useful.
>
> I certainly think it can be useful.
>
> On a couple of occasions I've used code which does a similar thing
> with advice -- adding temporary advice, running a body, and removing
> the advice again.
>
> In both cases one could argue for a more persistent hook or advice
> which performs its own check to see whether it should do things, but
> that might also entail writing a new wrapper to perform the test, so
> there's appeal in a with-* approach (particularly for things which are
> wanted only very occasionally).
Yes to all that. A propos, I don't think advice (even
nadvice) replaces the special utility of a hook, for
reasons similar to those I gave in bug #33595 for why
a hook is handier for that use case than is using
`:exit-function' on `completion-extra-properties'.
Each such function behavior-modifying tool has a
purpose, even if sometimes they can substitute for
each other.
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#33601
; Package
emacs
.
(Tue, 04 Dec 2018 18:47:02 GMT)
Full text and
rfc822 format available.
Message #29 received at 33601 <at> debbugs.gnu.org (full text, mbox):
Hello, Drew.
In article <mailman.5097.1543862895.1284.bug-gnu-emacs <at> gnu.org> you wrote:
> Enhancement request: Please consider adding a macro such as this, to
> facilitate temporary use of a hook function.
> (defmacro with-hook-added (hook function &rest body)
> "Evaluate forms in BODY with FUNCTION added to HOOK, then remove FUNCTION."
> (declare (indent 1) (debug t))
> `(unwind-protect (progn (add-hook ',hook ',function) ,@body)
> (remove-hook ',hook ',function)))
Just a big point: you need to test whether FUNCTION is already on HOOK
at the start, and if so, not remove it at the end.
Apologies if somebody else has already pointed this out.
> In GNU Emacs 26.1 (build 1, x86_64-w64-mingw32)
> of 2018-05-30
> Repository revision: 07f8f9bc5a51f5aa94eb099f3e15fbe0c20ea1ea
> Windowing system distributor `Microsoft Corp.', version 10.0.16299
> Configured using:
> `configure --without-dbus --host=x86_64-w64-mingw32
> --without-compress-install 'CFLAGS=-O2 -static -g3''
--
Alan Mackenzie (Nuremberg, Germany).
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#33601
; Package
emacs
.
(Tue, 04 Dec 2018 19:19:02 GMT)
Full text and
rfc822 format available.
Message #32 received at 33601 <at> debbugs.gnu.org (full text, mbox):
> > Enhancement request: Please consider adding a macro such as this, to
> > facilitate temporary use of a hook function.
>
> > (defmacro with-hook-added (hook function &rest body)
> > "Evaluate forms in BODY with FUNCTION added to HOOK, then remove FUNCTION."
> > (declare (indent 1) (debug t))
> > `(unwind-protect (progn (add-hook ',hook ',function) ,@body)
> > (remove-hook ',hook ',function)))
>
> Just a big point: you need to test whether FUNCTION is already on HOOK
> at the start, and if so, not remove it at the end.
Hi Alan,
A big point? Need to?
That wasn't the behavior I had in mind for this, but it's
another possibility. I intended to provide only for the
behavior of always removing at the end.
There are in fact several different possibilities for such
a macro. We could provide also for `add-hook' args APPEND
and LOCAL. I left that out as well.
Choices:
1. Provide a single macro for all such possibilities, with
3 (mandatory) args for APPEND, LOCAL and whether to remove
FUNCTION at the end if it was already present at the outset.
2. Provide multiple macros, each specific for a given case.
#2 would mean 8 macros, to cover all the combinations
(nil or t for each of the 3 args).
Another possibility would be to accept a single arg for
the BODY code, instead of that being a &rest parameter,
and so be able to provide those 3 behavior-specifying
args as optional. In that case, we'd want to decide on
the best order among those args, e.g., based on which we
expect to be used most often.
I'm not sure what the right approach is. I think the
most common use case would be the one I wrote up (but
I don't know that):
. Always remove FUNCTION at the end
. Prepend, not append.
. Global, not local.
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#33601
; Package
emacs
.
(Tue, 04 Dec 2018 20:16:01 GMT)
Full text and
rfc822 format available.
Message #35 received at 33601 <at> debbugs.gnu.org (full text, mbox):
Hello, Drew.
On Tue, Dec 04, 2018 at 11:18:29 -0800, Drew Adams wrote:
> > > Enhancement request: Please consider adding a macro such as this, to
> > > facilitate temporary use of a hook function.
> > > (defmacro with-hook-added (hook function &rest body)
> > > "Evaluate forms in BODY with FUNCTION added to HOOK, then remove FUNCTION."
> > > (declare (indent 1) (debug t))
> > > `(unwind-protect (progn (add-hook ',hook ',function) ,@body)
> > > (remove-hook ',hook ',function)))
> > Just a big point: you need to test whether FUNCTION is already on HOOK
> > at the start, and if so, not remove it at the end.
> Hi Alan,
> A big point? Need to?
I think so, yes. The essence of the `with-...' macros is that they
temporarily change something, then evaluate ,@body, and at the end, the
something is restored to what it was.
If with-hook-added didn't preserve the hook, it would be an anomaly, an
outlier, and quite possibly a PITA.
> That wasn't the behavior I had in mind for this, but it's
> another possibility. I intended to provide only for the
> behavior of always removing at the end.
Why? What's the use case?
> There are in fact several different possibilities for such
> a macro. We could provide also for `add-hook' args APPEND
> and LOCAL. I left that out as well.
> Choices:
> 1. Provide a single macro for all such possibilities, with
> 3 (mandatory) args for APPEND, LOCAL and whether to remove
> FUNCTION at the end if it was already present at the outset.
> 2. Provide multiple macros, each specific for a given case.
> #2 would mean 8 macros, to cover all the combinations
> (nil or t for each of the 3 args).
How many of these would actually be of any use?
> Another possibility would be to accept a single arg for
> the BODY code, instead of that being a &rest parameter,
> and so be able to provide those 3 behavior-specifying
> args as optional. In that case, we'd want to decide on
> the best order among those args, e.g., based on which we
> expect to be used most often.
> I'm not sure what the right approach is. I think the
> most common use case would be the one I wrote up (but
> I don't know that):
> . Always remove FUNCTION at the end
> . Prepend, not append.
> . Global, not local.
--
Alan Mackenzie (Nuremberg, Germany).
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#33601
; Package
emacs
.
(Tue, 04 Dec 2018 21:23:02 GMT)
Full text and
rfc822 format available.
Message #38 received at 33601 <at> debbugs.gnu.org (full text, mbox):
> > > Just a big point: you need to test whether FUNCTION is already on
> > > HOOK at the start, and if so, not remove it at the end.
>
> > A big point? Need to?
>
> I think so, yes. The essence of the `with-...' macros is that they
> temporarily change something, then evaluate ,@body, and at the end, the
> something is restored to what it was.
>
> If with-hook-added didn't preserve the hook, it would
> be an anomaly, an outlier, and quite possibly a PITA.
If there's an unwritten rule that `with-*' macros
preserve and restore everything then clearly the
macro I proposed was not a `with-*' macro. Another
name for it would be fine, if that's the rule. ;-)
> > That wasn't the behavior I had in mind for this, but it's
> > another possibility. I intended to provide only for the
> > behavior of always removing at the end.
>
> Why? What's the use case?
The kind of thing I showed (with two examples).
Yes, I would use them where I either know that the
function is not on the hook already or I don't
care - cases where I would want it removed at the
end in any case.
And yes, given your question, the doc of such a
macro would need to make clear that it always
removes FUNCTION from the hook afterward.
> > Choices:
> > 1. Provide a single macro for all such possibilities, with
> > 3 (mandatory) args for APPEND, LOCAL and whether to remove
> > FUNCTION at the end if it was already present at the outset.
> > 2. Provide multiple macros, each specific for a given case.
> >
> > #2 would mean 8 macros, to cover all the combinations
> > (nil or t for each of the 3 args).
>
> How many of these would actually be of any use?
All of the behaviors are useful. If you meant to ask
whether it is useful to have 8 individual macros, then
my answer is probably not.
> > Another possibility would be to accept a single arg for
> > the BODY code, instead of that being a &rest parameter,
> > and so be able to provide those 3 behavior-specifying
> > args as optional. In that case, we'd want to decide on
> > the best order among those args, e.g., based on which we
> > expect to be used most often.
>
> > I'm not sure what the right approach is. I think the
> > most common use case would be the one I wrote up (but
> > I don't know that):
> >
> > . Always remove FUNCTION at the end
> > . Prepend, not append.
> > . Global, not local.
I really have no problem with what you requested:
not removing FUNCTION at the end if it was present
beforehand.
However, now you've gone beyond that to suggest
that we should restore the original hook value:
"preserve the hook".
Should it preserve the hook? Maybe, maybe not.
How strict is your unwritten `with-*' rule? Does
it apply only to undoing the particular change _it_
makes, or does it also undo changes that might be
made by the BODY? In this case, the BODY might
itself make changes to the hook.
If I had to choose only one macro, it would
probably be a general one. I guess it would, as
you first requested, remove FUNCTION only if it
was not already present. And it would accept
args for APPEND and LOCAL. But it would not
restore the original hook value.
Whether it should require args APPEND and LOCAL
or it should instead make you wrap body sexps in
a progn is another choice. Dunno which I'd prefer,
but probably the former (probably less error prone).
Other ideas are welcome. What is your preference
(besides always restoring the original hook value)?
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#33601
; Package
emacs
.
(Sat, 12 Feb 2022 08:47:02 GMT)
Full text and
rfc822 format available.
Message #41 received at 33601 <at> debbugs.gnu.org (full text, mbox):
Drew Adams <drew.adams <at> oracle.com> writes:
> Enhancement request: Please consider adding a macro such as this, to
> facilitate temporary use of a hook function.
>
> (defmacro with-hook-added (hook function &rest body)
> "Evaluate forms in BODY with FUNCTION added to HOOK, then remove FUNCTION."
> (declare (indent 1) (debug t))
> `(unwind-protect (progn (add-hook ',hook ',function) ,@body)
> (remove-hook ',hook ',function)))
I don't think this is generally useful enough, so I'm closing this bug
report.
--
(domestic pets only, the antidote for overdose, milk.)
bloggy blog: http://lars.ingebrigtsen.no
Added tag(s) wontfix.
Request was from
Lars Ingebrigtsen <larsi <at> gnus.org>
to
control <at> debbugs.gnu.org
.
(Sat, 12 Feb 2022 08:47:02 GMT)
Full text and
rfc822 format available.
bug closed, send any further explanations to
33601 <at> debbugs.gnu.org and Drew Adams <drew.adams <at> oracle.com>
Request was from
Lars Ingebrigtsen <larsi <at> gnus.org>
to
control <at> debbugs.gnu.org
.
(Sat, 12 Feb 2022 08:47:02 GMT)
Full text and
rfc822 format available.
bug archived.
Request was from
Debbugs Internal Request <help-debbugs <at> gnu.org>
to
internal_control <at> debbugs.gnu.org
.
(Sat, 12 Mar 2022 12:24:04 GMT)
Full text and
rfc822 format available.
This bug report was last modified 2 years and 39 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.