all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Arthur Miller <arthur.miller@live.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: Stefan Monnier via Users list for the GNU Emacs text editor
	<help-gnu-emacs@gnu.org>
Subject: Re: Eval keymapp in a macros
Date: Thu, 05 Aug 2021 18:04:30 +0200	[thread overview]
Message-ID: <AM9PR09MB4977588462DB6B19C6FDFF1696F29@AM9PR09MB4977.eurprd09.prod.outlook.com> (raw)
In-Reply-To: <jwvlf5grovd.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Thu, 05 Aug 2021 09:53:16 -0400")

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> (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)))))
>
> Your `map` variable above has a misleading name since it won't hold
> a map but a string

yes, I know, that was just me being lazy, that wasn't ment for other
people, so I used shorter name for the variable that I would type later
on :).
> PS: Personally I'd recommend against the `string-match-p` dance, since
>     all it saves you is the typing of `-map` but in exchange it prevents
>     you from using your macro with keymaps that have a name that doesn't
>     end in `-map` and it "obfuscates" the code a little, preventing
>     things like ElDoc and Xref from understanding what's going on.

And same reason explains -map obfuscation :). I thought from the
beginning it was cool to omit it but after a while realized it looked a
bit dumb, so now when I am changing to ordinary list instead of cons, I
planned to drop that. I agree it obscures the code with not much of
repetitive typing removed.

>     (defun my--with-key-map (map defs)
>       (dolist (def defs)
>         (let ((key (car def)))
>           (define-key map
>                       (if (vectorp key) key (read-kbd-macro key))
>                       (cdr def)))))
>     
>     (defmacro my-with-key-map (mapname &rest body)
>       `(my--with-key-map ,(if (string-match-p "-map\\'" (symbol-name mapname))
>                               mapname
>                             (intern (format "%s-map" mapname)))
>                          ',body))
>
> Look ma!  No `eval-and-compile` and no `symbol-value`.

When I said I would write it differently, I was thinking in different
direction indeed. This is simpler than what I had in mind. However :-)
... if we drop the -map suffix thing, we have:

(defun my--with-key-map (map defs)
(dolist (def defs)
  (let ((key (car def)))
    (define-key map
      (if (vectorp key) key (read-kbd-macro key))
      (cdr def)))))

(defmacro with-key-map (mapname &rest body)
  `(my--with-key-map ,mapname ',body))

This still does not handle the case with indirect keymap:

(setq pkg-ops-map
      (let ((map (make-sparse-keymap "Packages")))
        (with-key-map map
                      ("h" . '("describe" . describe-package))
                      ("a" . '("autoremove" . package-autoremove))
                      ("d" . '("delete" . package-delete))
                      ("i" . '("install" . package-install))
                      ("s" . '("selected" . package-install-selected-packages))
                      ("r" . '("refresh" . package-refresh-contents))
                      ("l" . '("list" . list-packages)))
        map))

(with-key-map global-map ("C-c p" . pkg-ops-map))

The last line will eval, but at runtime gives error: Wrong type
argument: commandp, pkg-ops-map.

The "evals" came to fix that case.

I tried with keymmapp in my--with-key-map but it still won't work
without eval, I think for the reasons as Michael summarized.




  reply	other threads:[~2021-08-05 16:04 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-02 12:16 Eval keymapp in a macros Arthur Miller
2021-08-02 20:33 ` Michael Heerdegen
2021-08-02 20:53   ` [External] : " Drew Adams
2021-08-03 21:20   ` Arthur Miller
2021-08-04  0:18     ` Michael Heerdegen
2021-08-04 10:52       ` Arthur Miller
2021-08-04 23:56         ` Michael Heerdegen
2021-08-05  6:03           ` Arthur Miller
2021-08-06  3:54             ` Michael Heerdegen
2021-08-12 20:28               ` Arthur Miller
2021-08-04  4:54     ` Yuri Khan
2021-08-04  9:38       ` Arthur Miller
2021-08-04 15:37         ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-08-05  6:12           ` Arthur Miller
2021-08-05 13:53             ` Stefan Monnier
2021-08-05 16:04               ` Arthur Miller [this message]
2021-08-05 16:34                 ` Stefan Monnier
2021-08-06  4:17                   ` Michael Heerdegen
2021-08-12 20:21                     ` Arthur Miller
2021-08-05  0:03         ` Michael Heerdegen
2021-08-05  6:15           ` Arthur Miller
2021-08-06  3:18             ` Michael Heerdegen

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AM9PR09MB4977588462DB6B19C6FDFF1696F29@AM9PR09MB4977.eurprd09.prod.outlook.com \
    --to=arthur.miller@live.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.