emacs-devel
[Top][All Lists]
Advanced

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

Re: Macro expansion: Why doesn't the invoked macro see (let (variables))


From: Alan Mackenzie
Subject: Re: Macro expansion: Why doesn't the invoked macro see (let (variables))from the invoking one?
Date: Wed, 8 Feb 2012 19:28:00 +0000
User-agent: Mutt/1.5.21 (2010-09-15)

Hello, Drew!

On Wed, Feb 08, 2012 at 09:46:45AM -0800, Drew Adams wrote:
> > (defmacro BAR ()
> >   (message (if (boundp 'asdf) "asdf" "no asdf"))
> >   '(message "bar"))

> > (defmacro FOO () (let (asdf) `(BAR)))

> > One macro FOO binds a let variable, then invokes another 
> > macro BAR.  BAR doesn't see this let variable.  Why not?

> That `let' binding is evaluated only when FOO is expanded.

This is what I want.

> It is not part of the resulting expansion (which is then evaluated).



> > Is there anything I can do about this?

> 1. Don't use side effects in the macro definition.

I need side effects during macro expansion (see below).

> 2. You might be looking for something like this (dunno):

> (defmacro FOO () `(let (asdf) ,(BAR)))

> When `(FOO)' is expanded, the expansion includes a `let' binding.

> This is the result of `(macroexpand '(FOO))':

> (let (asdf) "bar")

> Not sure what you're really trying to do, though.

OK, here it is in grisly detail.  I want to amend define-minor-mode so
that the position of calling the mode hooks can be specified by d-m-m's
invoker.  To do this, the invoker should insert the macro

(run-hooks-here)

at the appropriate place.  During its expansion, run-hooks-here needs to
set a flag for define-minor-mode meaning "hook expansion already done".
Should this flag not get set, d-m-m inserts the hook calls in the default
place.

What I was trying to do looks like this:


(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
                            <====== There may be (run-hooks-here) here.

    ,@(unless hooks-run `((run-hooks-here))) <========= test flag


Should run-hooks-here appear in ,@body, it should inhibit the later
expansion of r-h-h.

########################################################################

So, we've got a dynamically scoped language.  run-hooks-here is invoked
from define-minor-mode.  A variable let-bound in the latter should be
dynamically available in the former.  It isn't.

What am I missing here?

-- 
Alan Mackenzie (Nuremberg, Germany).



reply via email to

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