help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: elisp macros problem


From: Barry Fishman
Subject: Re: elisp macros problem
Date: Tue, 27 Jul 2004 23:22:42 GMT
User-agent: Gnus/5.110003 (No Gnus v0.3) Emacs/21.3.50 (gnu/linux)

David Kastrup <address@hidden> writes:

> Lowell Kirsh <address@hidden> 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




reply via email to

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