Subject: RE: bug#33601: 26; Add macro `with-hook-added'

From: Drew Adams <[email-masked]>
Date: Tue, 4 Dec 2018 11:18:29 -0800 (PST)

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