[Top][All Lists]

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

Re: Idempotency of add-hook wrt lambda expressions

From: M Jared Finder
Subject: Re: Idempotency of add-hook wrt lambda expressions
Date: Thu, 05 Mar 2009 09:22:37 -0800
User-agent: Mozilla-Thunderbird (X11/20090103)

Lennart Borgman wrote:
On Wed, Mar 4, 2009 at 10:23 PM, Stefan Monnier
<address@hidden> 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

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

(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 via email to

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