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: Arthur Miller
Subject: Re: Eval keymapp in a macros
Date: Thu, 05 Aug 2021 08:12:41 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Stefan Monnier via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org> writes:

>>> * If you sacrifice a quote and a pair of braces, you can have your
>>> typing saving as a simple function:
>>>
>>>     (my-define-keys global-map
>>>       '(("C-<insert>" . term-toggle)
>>>         ("<insert>" . term-toggle-eshell)
>>>         …))
>>
>> I know, but I wish the least noise as possible :-):
>
> In most cases, macros are best defined in 2 steps:
>
> 1- provide a function-based solution
> 2- provide a simple macro that expands to a call to the function-based
>    solution, with a very simple massaging (typically adding a few quotes
>    or wrapping with some `lambda`).
>
> That also makes debugging easier.
>
Agree. When I wrote this more than a year ago, I wasn't yet red On
Lisp. Since then I have actually got (almost) through On Lisp and Let
Over Lambda, and have start doing so. If I wrote it today I would
probably wrote a defun to return something I evaluate in macro, and
I would use that macro as "entrance" so I skip quoting. But I had this
6-lines small macro to just save some typing, and thought it would be
easier to fix. Actually for my own use, this is from my init file

(defmacro with-key-map (mapname &rest body)
  `(let ((map (eval-and-compile (if (string-match-p "-map$" (symbol-name 
',mapname))
                                    (symbol-name ',mapname)
                                  (concat (symbol-name ',mapname) "-map"))))
         (defs '(,@body)))
     (dolist (def defs)
       (define-key (symbol-value (intern map))
         (if (vectorp (car def)) (car def)
           (read-kbd-macro (car def)))
         (cdr def)))))


I am still good enough with that one, because I don't have so much more
need that to define some bindings in few places, but it was a bit of
challenge to adapt to the use case as pointed in the reddit thread.



reply via email to

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