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

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

Re: Eval keymapp in a macros


From: Michael Heerdegen
Subject: Re: Eval keymapp in a macros
Date: Mon, 02 Aug 2021 22:33:04 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Arthur Miller <arthur.miller@live.com> writes:

> #+begin_src emacs-lisp
>
> (defmacro with-key-map (mapname &rest body)
>   `(let ((map ,mapname))
>      (dolist (def '(,@body))
>         (define-key map
>       (if (vectorp (car def)) (car def)
>         (read-kbd-macro (car def)))
>       (if (or (listp (cdr def))
>               (functionp (cdr def)))
>           (cdr def)
>         (if (eval `(keymapp ,(cdr def)))
>             (eval (cdr def))))))))
>
> #+end_src

Unfortunately you didn't show how you use it.  What's the purpose of
your macro?  Anyway, `macroexpand' or `macroexpand-1' your call to see
what happens.

My hypothesis is that (cdr def) evals to a symbol (that is bound to a
keymap) - like `global-map', and not a keymap.  So the expansion of a
macro call would actually test e.g.

  (keymapp 'global-map)  --> nil

Note that nothing in BODY is ever evaluated (it's behind a quote).

My tip when writing a macro (do you really need one btw?): Write an
example call and then the desired expansion down.  Only after that write
down the macro implementation that offers exactly that expansion.  You
can later do that in your head, but if you skip that step you get all
the surprises and pitfalls that macros are known for.

Michael.




reply via email to

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