r/emacs • • 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

10 comments sorted by

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/Ysmiraak 5d ago

my goal is to turn 3 keystrokes C-x r into 1 r in region modal editing, with special preps for the rectangle commands, e.g. skipping the final empty line. i want the prefix echo & help, preferably also to work with which-key, without too much hassle.

2

u/shipmints 5d ago

Yes, this.

1

u/arthurno1 5d ago

Yes, definitely an xy-question. They should explain what they are trying to achieve.

2

u/floss_gang 6d ago
(defun my-ctl-x-r ()
  (interactive)
  (setq prefix-arg current-prefix-arg
        unread-command-events '((t . ?\C-x) (t . ?r))))

how does it work? no clue! i just copied it from which-key-reload-key-sequence.

1

u/Ysmiraak 5d ago

ah nice. thanks! i should have read the doc on unread-command-events further. it explains the usage of (t . evt)

1

u/floss_gang 5d ago

wow i should've too, i tried to do this exact same thing a while ago (can't remember what for) and eventually gave up.

1

u/Ysmiraak 5d ago

for the command to work in a keyboard macro, we have to use ('no-record . evt) but then the prefix echo doesn't work :(

3

u/arthurno1 5d ago

how to send a prefix key sequence in a command?

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:

(defun my-ctl-x-r ()
  (interactive)
  (set-transient-map ctl-x-r-map))

or

(set-transient-map ctl-x-r-map t) 

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