all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Alan Mackenzie <acm@muc.de>
Cc: Drew Adams <drew.adams@oracle.com>, emacs-devel@gnu.org
Subject: Re: Macro expansion: Why doesn't the invoked macro see (let (variables))from the invoking one?
Date: Wed, 08 Feb 2012 14:52:45 -0500	[thread overview]
Message-ID: <jwvpqdp6roe.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <20120208192800.GA31761@acm.acm> (Alan Mackenzie's message of "Wed, 8 Feb 2012 19:28:00 +0000")

> (defmacro run-hooks-here ()
>    (setq hooks-called t)        <================= flag variable
>   `(run-hooks ',hook (if ,mode ',hook-on ',hook-off)))

> (defmacro define-minor-mode (....)
> ....
>   (let (... hooks-run)
> ....
>     ,@body                  <================= expand invoker's forms

This comment is wrong: ",@body" just plugs in the `body' without
macro-expanding it.

>                             <====== There may be (run-hooks-here) here.
>     ,@(unless hooks-run `((run-hooks-here))) <========= test flag

You can do that, but you then need to make sure the `body' gets
macro-expanded while the `let' is live, i.e. during the expansion of the
call to `define-minor-mode'.
You can do it with something like

   (defmacro define-minor-mode (....)
   ....
     (let (... hooks-run)
   ....
       ,@(macroexpand-all body) <================= expand invoker's forms
                               <====== There may be (run-hooks-here) here.
       ,@(unless hooks-run `((run-hooks-here))) <========= test flag


But note that this counts as ugly.  We use such tricks in cl-macs.el to
figure out whether `body' uses `return-from' within a `block' (in order
to optimize away the `catch' that's otherwise needed), but it's ugly,
inefficient, and brittle.
An :after-hook (or :late-code or some other name you prefer) is much
better in this regard.


        Stefan



  reply	other threads:[~2012-02-08 19:52 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-08 17:26 Macro expansion: Why doesn't the invoked macro see (let (variables)) from the invoking one? Alan Mackenzie
2012-02-08 17:46 ` Macro expansion: Why doesn't the invoked macro see (let (variables))from " Drew Adams
2012-02-08 19:28   ` Alan Mackenzie
2012-02-08 19:52     ` Stefan Monnier [this message]
2012-02-08 20:09       ` Alan Mackenzie
2012-02-10  5:23         ` PJ Weisberg
2012-02-10 16:58           ` Alan Mackenzie
2012-02-08 20:16     ` Andreas Schwab
2012-02-08 18:05 ` Macro expansion: Why doesn't the invoked macro see (let (variables)) from " Tassilo Horn

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

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

  git send-email \
    --in-reply-to=jwvpqdp6roe.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=acm@muc.de \
    --cc=drew.adams@oracle.com \
    --cc=emacs-devel@gnu.org \
    /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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.