[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: macro temp variables
Pascal J. Bourguignon
Re: macro temp variables
Sun, 21 Sep 2014 22:59:47 +0200
Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)
Eric Abrahamsen <address@hidden> writes:
> "Pascal J. Bourguignon" <address@hidden> writes:
>> Eric Abrahamsen <address@hidden> writes:
>>> I've never actually needed to write a macro that provided temporary
>>> local variables, and consequently am not very good at it. Despite having
>>> read the docs and basically followed the examples there, my attempt is
>>> producing errors.
>> The problem is that you want your body to access those variables. So
>> the body must know their names. But you are computing new names that
>> are uninterned, and therefore unaccessible. Therefore there's no way to
>> access those temporary variables, from the body. Only code generated by
>> your macro could access those variables (since the macro has their name
>> stored in its head .. tags variables.
> Wow, thanks so much to both of you, this is an excellent lesson in using
> macros. Richard, the walk-through was much appreciated -- things like
> that always start out seeming obvious, but then by step three or so I'm
> perplexed. pp-macroexpand-* will be helpful in the future.
> So I think I've got it. The behavior that everyone's trying to avoid by
> using make-symbol is in fact the precise behavior I want: leaking
> symbols from the macro into the body code. I'm going to hold my brain
> perfectly still until that sinks in.
> I had suspected that I'd have to do something along the lines of
> Pascal's solution -- explicitly providing the args to be bound on each
> pass -- but I'm inclined to go with Richard's version, since the whole
> point of this function is to be a *scratch*-buffer way of doing one-off
> things with Org headings, and I'd like it to be as easy to write as
Then you would just bind those normal, interned, symbols, and document
that your macro does this binding:
(defmacro org-iterating-headings (&rest body)
"Binds the following lexical variables: head item todo tags body-ars
to the corresponding org headings, while iterating headings."
(destructuring-bind (head item todo tags body-pars) vars
(lambda (head item todo tags body-pars) ,@body))))
Notice the advantage of factoring yout the call-org-iterating-heading
function: you can easily change the user interface of your macro after
the fact, and even provide both.
__Pascal Bourguignon__ http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk