all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Idempotency of add-hook wrt lambda expressions
@ 2009-03-04 12:10 Geoff Gole
  2009-03-04 14:18 ` Stefan Monnier
  2009-03-05 16:12 ` Alan Mackenzie
  0 siblings, 2 replies; 24+ messages in thread
From: Geoff Gole @ 2009-03-04 12:10 UTC (permalink / raw)
  To: emacs-devel

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




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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-04 12:10 Idempotency of add-hook wrt lambda expressions Geoff Gole
@ 2009-03-04 14:18 ` Stefan Monnier
  2009-03-04 20:13   ` xah lee
  2009-03-04 21:00   ` David Reitter
  2009-03-05 16:12 ` Alan Mackenzie
  1 sibling, 2 replies; 24+ messages in thread
From: Stefan Monnier @ 2009-03-04 14:18 UTC (permalink / raw)
  To: Geoff Gole; +Cc: emacs-devel

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

As a general rule, you should not put a lambda but a function name
(i.e. a symbol) instead, to avoid all those problems (and be able to
replace the function with a newer version of it).  But occasionally
a lambda is really exactly what you want, of course.


        Stefan





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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-04 14:18 ` Stefan Monnier
@ 2009-03-04 20:13   ` xah lee
  2009-03-04 21:23     ` Stefan Monnier
  2009-03-04 21:00   ` David Reitter
  1 sibling, 1 reply; 24+ messages in thread
From: xah lee @ 2009-03-04 20:13 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Geoff Gole, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 941 bytes --]

On Wed, Mar 4, 2009 at 6:18 AM, Stefan Monnier <monnier@iro.umontreal.ca>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).
>
> As a general rule, you should not put a lambda but a function name
> (i.e. a symbol) instead, to avoid all those problems (and be able to
> replace the function with a newer version of it).  But occasionally
> a lambda is really exactly what you want, of course.
>

i prefer this be transparent to the user as Geoff Gole suggested.

My reason is that from a user point of view, lambda serves the same purpose
as function name, and one less thing to worry about problems that might come
from byte compiling.

if the code suggested does fix this without any other problems,  why not?

cheers,

  Xah

[-- Attachment #2: Type: text/html, Size: 1576 bytes --]

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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-04 14:18 ` Stefan Monnier
  2009-03-04 20:13   ` xah lee
@ 2009-03-04 21:00   ` David Reitter
  2009-03-05  1:27     ` Stephen J. Turnbull
  2009-03-05  1:54     ` Stefan Monnier
  1 sibling, 2 replies; 24+ messages in thread
From: David Reitter @ 2009-03-04 21:00 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Geoff Gole, emacs-devel

On 4 Mar 2009, at 09:18, Stefan Monnier wrote:

> As a general rule, you should not put a lambda but a function name
> (i.e. a symbol) instead, to avoid all those problems (and be able to
> replace the function with a newer version of it).  But occasionally
> a lambda is really exactly what you want, of course.


People haven't adopted this, and this is pretty annoying.

A similar case are commands bound to menu items (or any other keys): C- 
h k does not bring up something useful for keys that are just bound to  
a lambda term.

As for hooks, is a lambda expression ever suitable to be added to a  
hook?
Would it make sense to change add-hook such that only true function  
names are allowed?





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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-04 20:13   ` xah lee
@ 2009-03-04 21:23     ` Stefan Monnier
  2009-03-04 21:27       ` Lennart Borgman
  0 siblings, 1 reply; 24+ messages in thread
From: Stefan Monnier @ 2009-03-04 21:23 UTC (permalink / raw)
  To: xah; +Cc: Geoff Gole, emacs-devel

> if the code suggested does fix this without any other problems, why not?

Because it's a hack.  So it takes care of 80% of the cases but still
doesn't fix the remaining cases.  Fixing the remaining cases requires
using a symbol, so just use a symbol and get on with your life.
I'd much rather add a patch that complains when you pass a lambda to
add-hook.


        Stefan




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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-04 21:23     ` Stefan Monnier
@ 2009-03-04 21:27       ` Lennart Borgman
  2009-03-05 17:22         ` M Jared Finder
  0 siblings, 1 reply; 24+ messages in thread
From: Lennart Borgman @ 2009-03-04 21:27 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: xah, Geoff Gole, emacs-devel

On Wed, Mar 4, 2009 at 10:23 PM, Stefan Monnier
<monnier@iro.umontreal.ca> wrote:
>> if the code suggested does fix this without any other problems, why not?
>
> Because it's a hack.  So it takes care of 80% of the cases but still
> doesn't fix the remaining cases.  Fixing the remaining cases requires
> using a symbol, so just use a symbol and get on with your life.
> I'd much rather add a patch that complains when you pass a lambda to
> add-hook.

Please do and do the same for menus. This would make it much easier to
search for errors.




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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-04 21:00   ` David Reitter
@ 2009-03-05  1:27     ` Stephen J. Turnbull
  2009-03-05  1:54     ` Stefan Monnier
  1 sibling, 0 replies; 24+ messages in thread
From: Stephen J. Turnbull @ 2009-03-05  1:27 UTC (permalink / raw)
  To: David Reitter; +Cc: Geoff Gole, Stefan Monnier, emacs-devel

David Reitter writes:

 > On 4 Mar 2009, at 09:18, Stefan Monnier wrote:
 > 
 > > As a general rule, you should not put a lambda but a function name
 > > (i.e. a symbol) instead, to avoid all those problems (and be able to
 > > replace the function with a newer version of it).  But occasionally
 > > a lambda is really exactly what you want, of course.

 > People haven't adopted this, and this is pretty annoying.
 > 
 > A similar case are commands bound to menu items (or any other keys): C- 
 > h k does not bring up something useful for keys that are just bound to  
 > a lambda term.
 > 
 > As for hooks, is a lambda expression ever suitable to be added to a  
 > hook?
 > Would it make sense to change add-hook such that only true function  
 > names are allowed?

In all these cases the problem that you consistently run into is that
Emacs Lisp doesn't have namespaces/packages.  It's very common to want
to make a small tweak to a command's default arguments when it's used
as a hook or menu item, and adding more symbols for that just feels
wrong.  Not to mention getting in the way of completion.

lambdas may have docstrings, and probably things like C-h k would find
them if provided.  Hm, easy enough to check:

  (define-key global-map [(control c) z]
              (lambda () "Found doc!" (interactive) (message "yowza")))

C-h k C-c z ==>
C-c z runs (lambda nil Found doc! (interactive) (message yowza))

Found doc!

So the obvious workaround is to report a bug if a lambda in a menu
item doesn't have a docstring.  Maybe there should be a describe-hook
help function that returns a buffer like

----------------------------------------------------------------
foo-hook runs, in this order:

1. default-foo-hook-function
Drink foo in bar.

2. (lambda ...)
Call auxiliary-foo-hook-function with argument "hair of dog".
----------------------------------------------------------------




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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-04 21:00   ` David Reitter
  2009-03-05  1:27     ` Stephen J. Turnbull
@ 2009-03-05  1:54     ` Stefan Monnier
  2009-03-05  8:23       ` Geoff Gole
  1 sibling, 1 reply; 24+ messages in thread
From: Stefan Monnier @ 2009-03-05  1:54 UTC (permalink / raw)
  To: David Reitter; +Cc: Geoff Gole, emacs-devel

> A similar case are commands bound to menu items (or any other keys): C-
> h k does not bring up something useful for keys that are just bound to
> a lambda term.

C-h k does give useful info, provided the lambda term has a docstring.
Just as is the case for symbols.


        Stefan




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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-05  1:54     ` Stefan Monnier
@ 2009-03-05  8:23       ` Geoff Gole
  2009-03-05  9:49         ` David Kastrup
  0 siblings, 1 reply; 24+ messages in thread
From: Geoff Gole @ 2009-03-05  8:23 UTC (permalink / raw)
  To: Stefan Monnier, emacs-devel, david.reitter

> I'd much rather add a patch that complains when you pass a lambda to
add-hook.

Sounds good to me.




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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-05  8:23       ` Geoff Gole
@ 2009-03-05  9:49         ` David Kastrup
  2009-03-05 11:41           ` tomas
                             ` (3 more replies)
  0 siblings, 4 replies; 24+ messages in thread
From: David Kastrup @ 2009-03-05  9:49 UTC (permalink / raw)
  To: emacs-devel

Geoff Gole <geoffgole@gmail.com> writes:

>> I'd much rather add a patch that complains when you pass a lambda to
> add-hook.
>
> Sounds good to me.

Does not jibe with existing practise.

For example, (info "(emacs) Init Examples") carefully explains

   * Turn on Auto Fill mode automatically in Text mode and related
     modes.

          (add-hook 'text-mode-hook
            '(lambda () (auto-fill-mode 1)))

     This shows how to add a hook function to a normal hook variable
     (*note Hooks::).  The function we supply is a list starting with
     `lambda', with a single-quote in front of it to make it a list
     constant rather than an expression.

     It's beyond the scope of this manual to explain Lisp functions,
     but for this example it is enough to know that the effect is to
     execute `(auto-fill-mode 1)' when Text mode is entered.  You can
     replace that with any other expression that you like, or with
     several expressions in a row.

Sure, you can "fix" the manual here.  But you can't expect that the real
world has never used an idiom explicitly documented and used as an
example.

Also, in some cases you can't avoid lambda, namely when using computed
functions.

-- 
David Kastrup





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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-05  9:49         ` David Kastrup
@ 2009-03-05 11:41           ` tomas
  2009-03-05 11:53           ` Geoff Gole
                             ` (2 subsequent siblings)
  3 siblings, 0 replies; 24+ messages in thread
From: tomas @ 2009-03-05 11:41 UTC (permalink / raw)
  To: David Kastrup; +Cc: emacs-devel

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Thu, Mar 05, 2009 at 10:49:14AM +0100, David Kastrup wrote:

[...]

> Also, in some cases you can't avoid lambda, namely when using computed
> functions.

Yes, and one particularly useful example would be "poor-man's currying",
where you want to use the same function with slight variations in
bindings.

Regards
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFJr7p0Bcgs9XrR2kYRAktQAJ9tApSRFowHTetKEtZ02g6e2u1o4gCffDP/
GsXiJd4jAChs/oMEb4dlqww=
=IX1U
-----END PGP SIGNATURE-----




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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-05  9:49         ` David Kastrup
  2009-03-05 11:41           ` tomas
@ 2009-03-05 11:53           ` Geoff Gole
  2009-03-05 16:45             ` Alan Mackenzie
  2009-03-05 16:38           ` Alan Mackenzie
  2009-03-05 20:44           ` Reiner Steib
  3 siblings, 1 reply; 24+ messages in thread
From: Geoff Gole @ 2009-03-05 11:53 UTC (permalink / raw)
  To: David Kastrup, emacs-devel

> Also, in some cases you can't avoid lambda, namely when using computed
> functions.

Can't you avoid this with (add-hook 'hook (progn (fset symbol
calculated-definition) symbol)) or similar?




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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-04 12:10 Idempotency of add-hook wrt lambda expressions Geoff Gole
  2009-03-04 14:18 ` Stefan Monnier
@ 2009-03-05 16:12 ` Alan Mackenzie
  2009-03-05 16:37   ` Stefan Monnier
  1 sibling, 1 reply; 24+ messages in thread
From: Alan Mackenzie @ 2009-03-05 16:12 UTC (permalink / raw)
  To: Geoff Gole; +Cc: emacs-devel

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




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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-05 16:45             ` Alan Mackenzie
@ 2009-03-05 16:33               ` Helmut Eller
  2009-03-05 16:38               ` Stefan Monnier
  2009-03-05 16:50               ` Geoff Gole
  2 siblings, 0 replies; 24+ messages in thread
From: Helmut Eller @ 2009-03-05 16:33 UTC (permalink / raw)
  To: emacs-devel

* Alan Mackenzie [2009-03-05 17:45+0100] writes:

> One of the reasons for using a lambda is to save having to think up a
> function name, and to save cluttering up one's .emacs, or whatever.
> Compare
>
>     (add-hook 'foo-mode-hook (lambda (some-minor-mode 1)))
>
> with
>
>     (defun switch-some-minor-mode-on ()
>       (some-minor-mode 1))
>     (add-hook 'foo-mode-hook 'switch-some-minor-mode-on)
>

I've seen this idiom in several places:

(add-hook 'foo-mode-hook 
          (defun switch-some-minor-mode-on () 
            (some-minor-mode 1)))


Helmut.





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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-05 16:12 ` Alan Mackenzie
@ 2009-03-05 16:37   ` Stefan Monnier
  0 siblings, 0 replies; 24+ messages in thread
From: Stefan Monnier @ 2009-03-05 16:37 UTC (permalink / raw)
  To: Alan Mackenzie; +Cc: Geoff Gole, emacs-devel

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

I'm glad we agree.


        Stefan




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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-05 16:45             ` Alan Mackenzie
  2009-03-05 16:33               ` Helmut Eller
@ 2009-03-05 16:38               ` Stefan Monnier
  2009-03-05 16:50               ` Geoff Gole
  2 siblings, 0 replies; 24+ messages in thread
From: Stefan Monnier @ 2009-03-05 16:38 UTC (permalink / raw)
  To: Alan Mackenzie; +Cc: David Kastrup, Geoff Gole, emacs-devel

> One of the reasons for using a lambda is to save having to think up a
> function name, and to save cluttering up one's .emacs, or whatever.
> Compare

>     (add-hook 'foo-mode-hook (lambda (some-minor-mode 1)))

> with

>     (defun switch-some-minor-mode-on ()
>       (some-minor-mode 1))
>     (add-hook 'foo-mode-hook 'switch-some-minor-mode-on)

FWIW, I use the second form in my .emacs, because I try to make my
.emacs idempotent (so I can just reload it any number of time, and that
won't add N copies of my lambda on that magic hook).


        Stefan





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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-05  9:49         ` David Kastrup
  2009-03-05 11:41           ` tomas
  2009-03-05 11:53           ` Geoff Gole
@ 2009-03-05 16:38           ` Alan Mackenzie
  2009-03-05 20:44           ` Reiner Steib
  3 siblings, 0 replies; 24+ messages in thread
From: Alan Mackenzie @ 2009-03-05 16:38 UTC (permalink / raw)
  To: David Kastrup; +Cc: emacs-devel

On Thu, Mar 05, 2009 at 10:49:14AM +0100, David Kastrup wrote:
> Geoff Gole <geoffgole@gmail.com> writes:

> >> I'd much rather add a patch that complains when you pass a lambda to
> > add-hook.

> > Sounds good to me.

> Does not jibe with existing practise.

"Me too!".

> For example, (info "(emacs) Init Examples") carefully explains

>    * Turn on Auto Fill mode automatically in Text mode and related
>      modes.

>           (add-hook 'text-mode-hook
>             '(lambda () (auto-fill-mode 1)))

>      This shows how to add a hook function to a normal hook variable
>      (*note Hooks::).  The function we supply is a list starting with
>      `lambda', with a single-quote in front of it to make it a list
>      constant rather than an expression.

>      It's beyond the scope of this manual to explain Lisp functions,
>      but for this example it is enough to know that the effect is to
>      execute `(auto-fill-mode 1)' when Text mode is entered.  You can
>      replace that with any other expression that you like, or with
>      several expressions in a row.

> Sure, you can "fix" the manual here.  But you can't expect that the
> real world has never used an idiom explicitly documented and used as an
> example.

I have stuff like the following in my setup:

    (global-set-key [f1] (lambda () "Switch to frame F1"
                          (interactive)
                          (select-frame-acm-no 0)))

That `global-set-key' might easily have been an `add-hook'.  I think it
would get tedious very quickly if warnings were given with add-hook +
lambda.  To outlaw it would be the Wrong Thing completely.

With all due respect to Geoff, I don't think this is really a problem.

> Also, in some cases you can't avoid lambda, namely when using computed
> functions.

Yes.

> David Kastrup

-- 
Alan Mackenzie (Nuremberg, Germany).




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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-05 11:53           ` Geoff Gole
@ 2009-03-05 16:45             ` Alan Mackenzie
  2009-03-05 16:33               ` Helmut Eller
                                 ` (2 more replies)
  0 siblings, 3 replies; 24+ messages in thread
From: Alan Mackenzie @ 2009-03-05 16:45 UTC (permalink / raw)
  To: Geoff Gole; +Cc: David Kastrup, emacs-devel

On Thu, Mar 05, 2009 at 08:53:23PM +0900, Geoff Gole wrote:
> > Also, in some cases you can't avoid lambda, namely when using computed
> > functions.

> Can't you avoid this with (add-hook 'hook (progn (fset symbol
> calculated-definition) symbol)) or similar?

Probably, yes.  But who'd want to?

One of the reasons for using a lambda is to save having to think up a
function name, and to save cluttering up one's .emacs, or whatever.
Compare

    (add-hook 'foo-mode-hook (lambda (some-minor-mode 1)))

with

    (defun switch-some-minor-mode-on ()
      (some-minor-mode 1))
    (add-hook 'foo-mode-hook 'switch-some-minor-mode-on)

.

-- 
Alan Mackenzie (Nuremberg, Germany).




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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-05 16:45             ` Alan Mackenzie
  2009-03-05 16:33               ` Helmut Eller
  2009-03-05 16:38               ` Stefan Monnier
@ 2009-03-05 16:50               ` Geoff Gole
  2 siblings, 0 replies; 24+ messages in thread
From: Geoff Gole @ 2009-03-05 16:50 UTC (permalink / raw)
  To: Alan Mackenzie, emacs-devel

> But who'd want to?

Somebody who wants the convenience of an idempotent init file?




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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-04 21:27       ` Lennart Borgman
@ 2009-03-05 17:22         ` M Jared Finder
  0 siblings, 0 replies; 24+ messages in thread
From: M Jared Finder @ 2009-03-05 17:22 UTC (permalink / raw)
  To: emacs-devel

Lennart Borgman wrote:
> On Wed, Mar 4, 2009 at 10:23 PM, Stefan Monnier
> <monnier@iro.umontreal.ca> wrote:
>>> if the code suggested does fix this without any other problems, why not?
>> Because it's a hack.  So it takes care of 80% of the cases but still
>> doesn't fix the remaining cases.  Fixing the remaining cases requires
>> using a symbol, so just use a symbol and get on with your life.
>> I'd much rather add a patch that complains when you pass a lambda to
>> add-hook.
> 
> Please do and do the same for menus. This would make it much easier to
> search for errors.
> 
> 
> 

I'm a user of Emacs and have created the following macro for my usage. 
I've been using it since Emacs 21.3 with no issues.  Maybe it'd be 
useful to add to Emacs?

   -- MJF

;;; Used like this:
;;;
;;; (hook-mode c-mode-common-hook
;;;   c-subword-mode
;;;  (c-set-offset 'case-label '+)
;;;  (setf (local-key-binding (kbd "C-c M-<right>"))
;;;        'c-forward-conditional
;;         (local-key-binding (kbd "C-c M-<left>"))
;;         'c-backward-conditional))

(defmacro hook-mode (hook &body modes)
   "Hook each member of MODES on HOOK.  If the member is a symbol,
call (member 1); if it is a list, just execute it directly.

This allows you to declaratively hook in minor modes on a major mode."
   (let ((body (loop for expr in modes
                     if (and (symbolp expr) (fboundp expr))
                       collect (list expr 1)
                     else if (listp expr)
                       collect expr
                     else do (error "%s does not appear to name a minor 
mode." expr))))
     `(hook-mode-attach ',hook (lambda () "Auto-generated by `hook-mode'"
                                       ,@body))))

(defvar hook-mode-*hooks* (make-hash-table :test #'eq))
(defun hook-mode-attach (hook function)
   (when (gethash hook hook-mode-*hooks*)
     (remove-hook hook (gethash hook hook-mode-*hooks*)))
   (add-hook hook function)
   (setf (gethash hook hook-mode-*hooks*) function))





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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-05  9:49         ` David Kastrup
                             ` (2 preceding siblings ...)
  2009-03-05 16:38           ` Alan Mackenzie
@ 2009-03-05 20:44           ` Reiner Steib
  2009-03-05 20:59             ` Edward O'Connor
  3 siblings, 1 reply; 24+ messages in thread
From: Reiner Steib @ 2009-03-05 20:44 UTC (permalink / raw)
  To: David Kastrup; +Cc: emacs-devel

On Thu, Mar 05 2009, David Kastrup wrote:

> For example, (info "(emacs) Init Examples") carefully explains
>
>    * Turn on Auto Fill mode automatically in Text mode and related
>      modes.
>
>           (add-hook 'text-mode-hook
>             '(lambda () (auto-fill-mode 1)))

Maybe we should provide turn-on-auto-fill-mode and use it here?

Bye, Reiner.
-- 
       ,,,
      (o o)
---ooO-(_)-Ooo---  |  PGP key available  |  http://rsteib.home.pages.de/




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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-05 20:44           ` Reiner Steib
@ 2009-03-05 20:59             ` Edward O'Connor
  2009-03-05 21:08               ` Reiner Steib
  0 siblings, 1 reply; 24+ messages in thread
From: Edward O'Connor @ 2009-03-05 20:59 UTC (permalink / raw)
  To: Reiner Steib, David Kastrup, emacs-devel

>> For example, (info "(emacs) Init Examples") carefully explains
>>
>>    * Turn on Auto Fill mode automatically in Text mode and related
>>      modes.
>>
>>           (add-hook 'text-mode-hook
>>             '(lambda () (auto-fill-mode 1)))
>
> Maybe we should provide turn-on-auto-fill-mode and use it here?

`turn-on-auto-fill' and `turn-off-auto-fill' are already defined in
simple.el. The docs should just be updated to use them.


-- 
Ted




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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-05 20:59             ` Edward O'Connor
@ 2009-03-05 21:08               ` Reiner Steib
  2009-03-06  9:36                 ` David Kastrup
  0 siblings, 1 reply; 24+ messages in thread
From: Reiner Steib @ 2009-03-05 21:08 UTC (permalink / raw)
  To: Edward O'Connor; +Cc: David Kastrup, emacs-devel

On Thu, Mar 05 2009, Edward O'Connor wrote:

>> Maybe we should provide turn-on-auto-fill-mode and use it here?
>
> `turn-on-auto-fill' and `turn-off-auto-fill' are already defined in
> simple.el. 

Oh, stupid me.  I thought it should be interactive, so I only checked
`M-x turn-on-a TAB' and got "No match".

> The docs should just be updated to use them.

Agreed.

Bye, Reiner.
-- 
       ,,,
      (o o)
---ooO-(_)-Ooo---  |  PGP key available  |  http://rsteib.home.pages.de/




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

* Re: Idempotency of add-hook wrt lambda expressions
  2009-03-05 21:08               ` Reiner Steib
@ 2009-03-06  9:36                 ` David Kastrup
  0 siblings, 0 replies; 24+ messages in thread
From: David Kastrup @ 2009-03-06  9:36 UTC (permalink / raw)
  To: Edward O'Connor; +Cc: emacs-devel

Reiner Steib <reinersteib+gmane@imap.cc> writes:

> On Thu, Mar 05 2009, Edward O'Connor wrote:
>
>>> Maybe we should provide turn-on-auto-fill-mode and use it here?
>>
>> `turn-on-auto-fill' and `turn-off-auto-fill' are already defined in
>> simple.el. 
>
> Oh, stupid me.  I thought it should be interactive, so I only checked
> `M-x turn-on-a TAB' and got "No match".
>
>> The docs should just be updated to use them.
>
> Agreed.

They already mention this as a simpler variant, right after the passage
I quoted.  But that does not change that the "complex" variant is
presented first, and with the full intent of showing the use of lambda
in a hook.

-- 
David Kastrup




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

end of thread, other threads:[~2009-03-06  9:36 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-04 12:10 Idempotency of add-hook wrt lambda expressions Geoff Gole
2009-03-04 14:18 ` Stefan Monnier
2009-03-04 20:13   ` xah lee
2009-03-04 21:23     ` Stefan Monnier
2009-03-04 21:27       ` Lennart Borgman
2009-03-05 17:22         ` M Jared Finder
2009-03-04 21:00   ` David Reitter
2009-03-05  1:27     ` Stephen J. Turnbull
2009-03-05  1:54     ` Stefan Monnier
2009-03-05  8:23       ` Geoff Gole
2009-03-05  9:49         ` David Kastrup
2009-03-05 11:41           ` tomas
2009-03-05 11:53           ` Geoff Gole
2009-03-05 16:45             ` Alan Mackenzie
2009-03-05 16:33               ` Helmut Eller
2009-03-05 16:38               ` Stefan Monnier
2009-03-05 16:50               ` Geoff Gole
2009-03-05 16:38           ` Alan Mackenzie
2009-03-05 20:44           ` Reiner Steib
2009-03-05 20:59             ` Edward O'Connor
2009-03-05 21:08               ` Reiner Steib
2009-03-06  9:36                 ` David Kastrup
2009-03-05 16:12 ` Alan Mackenzie
2009-03-05 16:37   ` Stefan Monnier

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.