From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: M Jared Finder Newsgroups: gmane.emacs.devel Subject: Re: Idempotency of add-hook wrt lambda expressions Date: Thu, 05 Mar 2009 09:22:37 -0800 Message-ID: References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1236273826 19721 80.91.229.12 (5 Mar 2009 17:23:46 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 5 Mar 2009 17:23:46 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Mar 05 18:25:03 2009 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1LfHJR-0005Ca-FX for ged-emacs-devel@m.gmane.org; Thu, 05 Mar 2009 18:24:49 +0100 Original-Received: from localhost ([127.0.0.1]:42648 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LfHI5-0000tg-TV for ged-emacs-devel@m.gmane.org; Thu, 05 Mar 2009 12:23:26 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1LfHHW-0000c1-Qj for emacs-devel@gnu.org; Thu, 05 Mar 2009 12:22:50 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1LfHHV-0000bK-GD for emacs-devel@gnu.org; Thu, 05 Mar 2009 12:22:50 -0500 Original-Received: from [199.232.76.173] (port=59965 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LfHHV-0000b8-66 for emacs-devel@gnu.org; Thu, 05 Mar 2009 12:22:49 -0500 Original-Received: from main.gmane.org ([80.91.229.2]:43938 helo=ciao.gmane.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1LfHHU-000832-Jg for emacs-devel@gnu.org; Thu, 05 Mar 2009 12:22:49 -0500 Original-Received: from list by ciao.gmane.org with local (Exim 4.43) id 1LfHHT-0004yn-IM for emacs-devel@gnu.org; Thu, 05 Mar 2009 17:22:47 +0000 Original-Received: from adsl-76-254-15-242.dsl.pltn13.sbcglobal.net ([76.254.15.242]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 05 Mar 2009 17:22:47 +0000 Original-Received: from jared by adsl-76-254-15-242.dsl.pltn13.sbcglobal.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 05 Mar 2009 17:22:47 +0000 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 53 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: adsl-76-254-15-242.dsl.pltn13.sbcglobal.net User-Agent: Mozilla-Thunderbird 2.0.0.19 (X11/20090103) In-Reply-To: X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:109479 Archived-At: Lennart Borgman wrote: > On Wed, Mar 4, 2009 at 10:23 PM, Stefan Monnier > 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-")) ;;; 'c-forward-conditional ;; (local-key-binding (kbd "C-c M-")) ;; '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))