r/emacs • u/Ysmiraak • 6d ago
how to send a prefix key sequence in a command?
i tried with unread-command-events but it doesn't behave quite right.
(defun my-ctl-x-r ()
(interactive)
(setq unread-command-events '(?\C-x ?r)))
now M-x my-ctl-x-r then t indeed runs C-x r t (string-rectangle)
however the prefix echo (before hitting t) says C-x- (C-h for help)
and hitting C-h indeed shows the help for C-x and not C-x r
i tried with (set--this-command-keys "\C-xr") to no avail.
emacs version 31.1
edit:
(defun my-ctl-x-r ()
(interactive)
(unless executing-kbd-macro
(setq unread-command-events
'((t . ?\C-x)
(t . ?r)))))
- the prefix echo & help work as intended, also with
which-key - works in a keyboard macro
12
Upvotes
5
u/eleven_cupfuls 5d ago
Is this just a point of curiosity or can you explain your overall goal? There might be another way to accomplish it.
2
u/floss_gang 6d ago
how does it work? no clue! i just copied it from
which-key-reload-key-sequence.3
u/arthurno1 5d ago
Why would you do that?
Bind the command to a key on a prefix or without a prefix on a key you want would be easiest thing to do, whatever you are trying to achieve. Are you trying to save typing prefix key for several commands? Emacs has repeat-mode for that.
If you really want to simulate "send C-x r" to ctl-x-map, than set ctl-x-map as transient:
or
so it stays "sticky" as long as you don't press a key outside the keymap.
But I am just guessing, since your question is typical xy-question. You should have answered the other commenter and tell what you actually want to achieve with this.
1
u/CoffeeAndCandidates 5d ago
`unread-command-events` feeds keys after your command returns, so the echo/help behavior is a bit odd; try `(setq unread-command-events (listify-key-sequence (kbd "C-x r")))` instead