[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Idempotency of add-hook wrt lambda expressions

From: Alan Mackenzie
Subject: Re: Idempotency of add-hook wrt lambda expressions
Date: Thu, 5 Mar 2009 16:12:06 +0000
User-agent: Mutt/1.5.9i

Hi, Geoff, Hi, Emacs!

On Wed, Mar 04, 2009 at 09:10:45PM +0900, Geoff Gole wrote:
> Say a file contains an add-hook form with a lambda argument:

>   (add-hook 'foo-mode (lambda () (bar)))

> Annoyingly, the function will be added to the hook twice if the file
> is byte compiled, loaded, then reevaluated (such as with eval-buffer).
> This can be worked around in any number of ways (don't byte compile,
> don't use lambdas in hooks, restart emacs on every change), but I
> wonder if add-hook can be made to do the right thing.

That begs the question as to what the Right Thing actually is.  Surely
this is a case of "you asked for it, you got it".

Also, is there really anything special about hooks here?  You're going to
be having this sort of problem wherever you use a lambda when you "really
ought" to be using a symbol.

> One way is to change the hook membership test from:

>   (member function hook-value)

> to something like

>   (ignore-errors
>     (let ((bc-function (byte-compile function)))
>       (or (member function hook-value)
>         (member bc-function hook-value))))

> But that's pretty horrible. In any case, if there is no fix shouldn't
> this wrinkle be mentioned in the add-hook docstring?

No, it's not "pretty horrible", it's utterly horrific.  ;-)

I'm not convinced anything's really needed in the add-hook docstring or
manual entry, but if so, I'd suggest something along the lines "you'd
best use a symbol here unless you really know what you're doing".

Alan Mackenzie (Nuremberg, Germany).

reply via email to

[Prev in Thread] Current Thread [Next in Thread]