There are already some questions on the topic of repeatable emacs commands (i.e. the behaviour of C-x z
[repeat-command
], where each subsequent z
repeats the last command), however, none of the automatic solutions can cope with non-prefix keybindings (My terminology: C-c p
is a prefix keybinding with prefix C-c
, the keystroke M-s-+
on the other hand is a non-prefix keybinding).
In my setup I have bound M-s-+
to text-scale-increase
and M-s--
to text-scale-decrease
. It would be nice to just hit +
or -
for repeated zooming after the initial command. This can be achieved by the following piece of elisp:
(defvar text-scale-temp-keymap (make-sparse-keymap))
(define-key text-scale-temp-keymap (kbd "+") 'text-scale-increase)
(define-key text-scale-temp-keymap (kbd "-") 'text-scale-decrease)
(defun text-scale-increase-rep (inc)
(interactive "p")
(text-scale-increase inc)
(set-temporary-overlay-map text-scale-temp-keymap t))
(defun text-scale-decrease-rep (inc)
(interactive "p")
(text-scale-decrease inc)
(set-temporary-overlay-map text-scale-temp-keymap t))
(global-set-key (kbd "M-s-+") 'text-scale-increase-rep)
(global-set-key (kbd "M-s--") 'text-scale-decrease-rep)
However, to repeat this code every time I want to create a repeatable keybinding is cumbersome and unnecessary. I'm asking for a way to automatise the task. Image this code
(make-repeatable-command 'text-scale-increase '(("+" . text-scale-increase)
("-" . text-scale-decrease)))
would create the command named text-scale-increase-rep
and an overlay keymap named text-scale-increase-temporary-map
with the corresponding keys.
I assume this is possible, but how?