From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Barry Fishman Newsgroups: gmane.emacs.help Subject: Re: elisp macros problem Date: Tue, 27 Jul 2004 23:22:42 GMT Organization: AT&T Worldnet Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: References: NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1090970833 4547 80.91.224.253 (27 Jul 2004 23:27:13 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Tue, 27 Jul 2004 23:27:13 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Jul 28 01:27:05 2004 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1BpbLc-0001KH-00 for ; Wed, 28 Jul 2004 01:27:05 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1BpbOi-0006Pj-QH for geh-help-gnu-emacs@m.gmane.org; Tue, 27 Jul 2004 19:30:16 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!logbridge.uoregon.edu!feed1.news.rcn.net!rcn!wn12feed!worldnet.att.net!bgtnsc05-news.ops.worldnet.att.net.POSTED!53ab2750!not-for-mail Original-Followup-To: comp.lang.lisp Original-Newsgroups: gnu.emacs.help,comp.lang.lisp User-Agent: Gnus/5.110003 (No Gnus v0.3) Emacs/21.3.50 (gnu/linux) Cancel-Lock: sha1:F1ajiJztc99LXzJ7s5lVR5SfZM8= Original-Lines: 61 Original-NNTP-Posting-Host: 12.78.46.252 Original-X-Complaints-To: abuse@worldnet.att.net Original-X-Trace: bgtnsc05-news.ops.worldnet.att.net 1090970562 12.78.46.252 (Tue, 27 Jul 2004 23:22:42 GMT) Original-NNTP-Posting-Date: Tue, 27 Jul 2004 23:22:42 GMT Original-Xref: shelby.stanford.edu gnu.emacs.help:124512 comp.lang.lisp:144293 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:19847 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:19847 David Kastrup writes: > Lowell Kirsh writes: > >> This still doesn't seem to work. With the following defun and call: >> >> (defmacro my-add-hooks (hooks &rest body) >> `(dolist (hook ',hooks) >> (my-add-hook hook ,@body))) >> >> (my-add-hooks (inferior-lisp lisp emacs-lisp lisp-interaction) >> (imenu-add-to-menubar "Symbols")) >> >> > you hoping to achieve that you would not be better off doing by a >> > proper function instead of a macro? > > In short, I still consider this sort of thing a crock, since you'd be > better off using a function instead of a macro. You are unable to > comprehend what your macros do, and it shows. Macros are just not > sensible for this sort of thing. Use functions instead. You'll need > to use some quotes at the outer level, but you'll understand what > happens. > > To fix the above, you'd need to write something like > > (defmacro my-add-hooks (hooks &rest body) > (dolist (hook hooks) > `(my-add-hook ,hook ,@body))) The body of the defmacro has to return some code. The dolist will return just NIL. Assuming that you want my-add-hooks to create a series of my-add-hook calls, one would need something like: (defmacro my-add-hooks (hooks &rest body) (cons 'progn (mapcar (lambda (h) `(my-add-hook ,h ,@body)) hooks))) Its usually easier to do the work in a function and use a macro to create the syntax you want. For example, using a function like: (defun my-add-hooks-fn (hooks func) (dolist (hook hooks) (add-hook (intern (concat (symbol-name hook) "-mode-hook")) func))) One can give it the syntax you want with a macro like: (defmacro my-add-hooks (hooks &rest body) `(my-add-hooks-2 ',hooks (lambda () ,@body))) This has the advantage of creating a single lambda function rather than multiple lambda functions with the same contents. This also allows one to build a more complex my-add-hooks-fn without dealing with the extra complexity of structuring it as a macro. -- Barry Fishman