all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: M Jared Finder <jared@hpalace.com>
To: emacs-devel@gnu.org
Subject: Re: Idempotency of add-hook wrt lambda expressions
Date: Thu, 05 Mar 2009 09:22:37 -0800	[thread overview]
Message-ID: <gop1ou$j0r$1@ger.gmane.org> (raw)
In-Reply-To: <e01d8a50903041327ke59f82bufb5833cc6d8754a6@mail.gmail.com>

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





  reply	other threads:[~2009-03-05 17:22 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='gop1ou$j0r$1@ger.gmane.org' \
    --to=jared@hpalace.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.