all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: macro temp variables
Date: Sun, 21 Sep 2014 22:59:47 +0200	[thread overview]
Message-ID: <87iokgwuyk.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: mailman.9302.1411299483.1147.help-gnu-emacs@gnu.org

Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> "Pascal J. Bourguignon" <pjb@informatimago.com> writes:
>
>> Eric Abrahamsen <eric@ericabrahamsen.net> 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
> possible.


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
    `(call-org-iterating-headings
      (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


  parent reply	other threads:[~2014-09-21 20:59 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.9181.1411123560.1147.help-gnu-emacs@gnu.org>
2014-09-19 17:33 ` macro temp variables Pascal J. Bourguignon
2014-09-19 17:45   ` Stefan Monnier
     [not found]   ` <mailman.9214.1411148764.1147.help-gnu-emacs@gnu.org>
2014-09-19 18:51     ` Pascal J. Bourguignon
2014-09-21  4:37   ` Eric Abrahamsen
     [not found]   ` <mailman.9302.1411299483.1147.help-gnu-emacs@gnu.org>
2014-09-21 20:59     ` Pascal J. Bourguignon [this message]
2014-09-19 10:49 Eric Abrahamsen
2014-09-19 12:11 ` Nicolas Richard
2014-09-19 15:48   ` Nicolas Richard

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=87iokgwuyk.fsf@kuiper.lan.informatimago.com \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@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.