unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Lowell Kirsh <lkirsh@cs.ubc.ca>
Subject: Re: elisp macros problem
Date: Tue, 27 Jul 2004 01:10:51 -0700	[thread overview]
Message-ID: <ce52me$q1m$1@mughi.cs.ubc.ca> (raw)
In-Reply-To: <x5zn5lvs43.fsf@lola.goethe.zz>

Hmmm, I was only trying to use macros because I thought what I was 
trying wasn't possible with functions. Would this be a more appropriate 
approach?

(defun my-add-hook (hook &rest body)
   (add-hook (intern (concat (symbol-name hook) "-mode-hook"))
             `(lambda () ,@body)))

and then call it like:

(my-add-hook 'lisp-interaction
              '(my-translate-paren-types)
              '(imenu-add-to-menubar "Symbols")
              )

This seems a little more verbose with all the quotes, but I guess it 
could be the 'better' approach since it avoids using macros.

Perhaps my macro approach was a crock. The only reason I defended it was 
becuase I thought you meant that creating the my-add-hook functionality 
itself was the crock.

Well, I'm going with functions now, and I'm also going to get back to 
reading Paul Graham's 'On Lisp', the only comprehensive lisp macros book 
I know of (are there others?)

Lowell


David Kastrup wrote:

> Lowell Kirsh <lkirsh@cs.ubc.ca> 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"))
>>
>>I get the following macroexpansion, which looks correct to me, but
>>doesn't actually do anything:
>>
>>(cl-block-wrapper
>>  (catch (quote --cl-block-nil--)
>>    (let ((--dolist-temp--20870 (quote (inferior-lisp lisp emacs-lisp
>>    lisp-interaction)))
>>          hook)
>>      (while --dolist-temp--20870
>>        (setq hook (car --dolist-temp--20870))
>>        (my-add-hook hook
>>                     (imenu-add-to-menubar "Symbols"))
>>        (setq --dolist-temp--20870 (cdr --dolist-temp--20870)))
>>      nil)))
> 
> 
> That's because my-add-hook is a macro, too, and so you just add stuff
> to hook-mode-hook three times.
> 
> 
>>>Apart from that, I consider this sort of thing a crock.  What are
>>>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)))
> 
> 

  reply	other threads:[~2004-07-27  8:10 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-24 18:39 elisp macros problem Lowell Kirsh
2004-07-24 19:00 ` David Kastrup
2004-07-24 22:08   ` Lowell Kirsh
2004-07-24 23:41     ` David Kastrup
2004-07-25  1:58       ` Lowell Kirsh
2004-07-25 10:59         ` Kalle Olavi Niemitalo
2004-07-24 19:15 ` Barry Margolin
2004-07-24 19:59   ` Pascal Bourguignon
2004-07-24 20:50     ` David Kastrup
2004-07-24 21:49       ` Lowell Kirsh
2004-07-26  1:35 ` Lowell Kirsh
2004-07-26  1:55   ` Rahul Jain
2004-07-26  2:53     ` Lowell Kirsh
2004-07-26  4:05       ` Pascal Bourguignon
2004-07-26  4:13         ` Lowell Kirsh
2004-07-26  2:54     ` Lowell Kirsh
2004-07-26 22:16       ` Kalle Olavi Niemitalo
2004-07-26 22:58       ` Kevin Rodgers
2004-07-26  2:03   ` Barry Margolin
2004-07-26  3:06     ` Lowell Kirsh
2004-07-26  4:49       ` Barry Margolin
2004-07-26  5:20         ` Pascal Bourguignon
2004-07-26  6:10       ` Oliver Scholz
2004-07-26  5:55   ` David Kastrup
2004-07-27  6:38     ` Lowell Kirsh
2004-07-27  6:54     ` Lowell Kirsh
2004-07-27  7:14       ` David Kastrup
2004-07-27  8:10         ` Lowell Kirsh [this message]
2004-07-27 12:05           ` Pascal Bourguignon
2004-07-28  5:03             ` Rob Warnock
2004-07-31 20:46             ` Lowell Kirsh
2004-08-03 18:44               ` Michael Slass
2004-07-27 23:22         ` Barry Fishman

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to='ce52me$q1m$1@mughi.cs.ubc.ca' \
    --to=lkirsh@cs.ubc.ca \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).