help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Meta-code confusion


From: Aurélien Aptel
Subject: Re: Meta-code confusion
Date: Mon, 13 Aug 2012 18:44:00 +0200

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 via email to

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