unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: "Aurélien Aptel" <aurelien.aptel+emacs@gmail.com>
To: Doug Lewan <dougl@shubertticketing.com>
Cc: "help-gnu-emacs@gnu.org" <help-gnu-emacs@gnu.org>
Subject: Re: Meta-code confusion
Date: Mon, 13 Aug 2012 18:44:00 +0200	[thread overview]
Message-ID: <CA+5B0FNiekB84bJeE-TfqBidKbpdzHGJ_-67rbaaN8yoDGMrsg@mail.gmail.com> (raw)
In-Reply-To: <155DEC68569B714B86C2C7075F5EDA98268CCDA0@DAKIYA1.pegasus.local>

On Mon, Aug 13, 2012 at 4:04 PM, Doug Lewan <dougl@shubertticketing.com> wrote:
>> But more likely, you probably need to define your own macro rather than
>> a function.
>
> And I do suppose that this would be more correct (and satisfying). I'm not fluent in macros and try to avoid them, but maybe the time has come....

It's really simpler using a macro. Are you familiar with the backquote?

This is a backquote form:
`(a b c)
=> (a b c)

It's almost the same behaviour as the regular quote *but* if you
prefix an element with a comma, it is evaluated.

'(1 (+ 1 1) 3)    ;; regular quote
=> (1 (+ 1 1) 3)

`(1 (+ 1 1) 3)    ;; backquote
=> (1 (+ 1 1) 3)

`(1 ,(+ 1 1) 3)    ;; backquote with a comma
=> (1 2 3)

Notice how the expression after the comma is evaluated.

Here's a simple macro that does what you want:

(defmacro ppm-define-vars (name abbrev)
  (let ((var (intern (format "*pp-%s-start-re*" abbrev)))
        (fmt (format "^%s\\s-+" (regexp-quote name))))
    `(defvar ,var (concat ,fmt *pp-symbol-re*))))

No nested list call and way more readable, right?

When the macro is evaluated, it is first "expanded" and the resulting
form is then evaluated. You can see the result (expansion) of the
macro *before* it gets evaluated with the macroexpand function.

(macroexpand '(ppm-define-vars "douglas" "dug"))
=> (defvar *pp-dug-start-re* (concat "^douglas\\s-+" *pp-symbol-re*))

There's more in the manual if you're interested. Look at (info
"(elisp) Macros") or [1].
Good luck!

1: http://www.gnu.org/software/emacs/manual/html_node/elisp/Macros.html#Macros



      reply	other threads:[~2012-08-13 16:44 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-10 19:12 Meta-code confusion Doug Lewan
2012-08-10 21:38 ` Doug Lewan
     [not found] ` <mailman.6744.1344634688.855.help-gnu-emacs@gnu.org>
2012-08-10 22:02   ` Barry Margolin
2012-08-13 14:04     ` Doug Lewan
2012-08-13 16:44       ` Aurélien Aptel [this message]

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=CA+5B0FNiekB84bJeE-TfqBidKbpdzHGJ_-67rbaaN8yoDGMrsg@mail.gmail.com \
    --to=aurelien.aptel+emacs@gmail.com \
    --cc=dougl@shubertticketing.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.
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).