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

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

Re: Macro Expansion Inconsistency


From: Nicolas Richard
Subject: Re: Macro Expansion Inconsistency
Date: Wed, 17 Dec 2014 11:04:58 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

Alexander Shukaev <haroogan@gmail.com> writes:

> I currently have the following macro baked:
>
> (defmacro bm-define-flag
>     (name index character foreground)
>   (let* ((symbol           (intern (format "bm-%s-flag"
>                                            (symbol-name name))))
>          (character-symbol (intern (format "%s-character"
>                                            (symbol-name symbol)))))
>     (put symbol 'index     index)
>     (put symbol 'character character-symbol)
>     (eval `(defcustom ,character-symbol
>              ,character
>              "Character for flag."
>              :tag "BM Flag Character"
>              :group 'buffer-manager
>              :type 'character))
>     (eval `(defface ,symbol
>              `((t :foreground ,foreground
>                   :weight     bold))
>              "Face for flag."
>              :tag "BM Flag Face"
>              :group 'buffer-manager-faces))))
> [...]
> causes the error: (void-variable bm-xxx-flag) in `eval'
> [...]
> How to overcome it?

Quick'n'very-dirty : add nil or t (or anything that can be evaluated
twice safely) at the end of your macro definition.

> Do you have any further recommendations on this macro?

It's awful :) But don't worry : it's easy to write awful things, and
it's good that you post about it here.

In your case I'd point out these problems :
(i) it uses explicit eval for no good reason
(ii) it doesn't return code (that's the error you see)
(iii) it relies on dynamical binding (because of ',foreground' in a
second layer of quasi-quoting, which is only evalled by the explicit
eval.).

I think this works (lightly tested) :

(defmacro bm-define-flag (name index character foreground)
  (let* ((symbol           (intern (format "bm-%s-flag"
                                           (symbol-name name))))
         (character-symbol (intern (format "%s-character"
                                           (symbol-name symbol)))))
    (put symbol 'index     index)
    (put symbol 'character character-symbol)
    `(progn ;; this is the code we're going to return after expansion.
            ;; Emacs will eval the whole progn form.
       (defcustom ,character-symbol
         ,character
         "Character for flag."
         :tag "BM Flag Character"
         :group 'buffer-manager
         :type 'character)
       (defface ,symbol
         '((t :foreground ,foreground ;; the comma can be used,
                                      ;; even after normal quoting
               :weight     bold))
         "Face for flag."
         :tag "BM Flag Face"
         :group 'buffer-manager-faces))))


If you'd like to read more about macros, I wrote about them at 
http://lists.gnu.org/archive/html/help-gnu-emacs/2014-09/msg00473.html
(there's a typo in the first paragraph, apply
`sed s/evaluation time/expansion time/' please)

HTH,

-- 
Nicolas Richard



reply via email to

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