Last active
November 5, 2018 06:45
-
-
Save mookid/64941602b840ca5ce81e2b3017d1e0a5 to your computer and use it in GitHub Desktop.
emacs: repeatable interactive commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; I happen to use a lot the command `zap-to-char', which prompts for | |
;; a character, and deletes from the point to the character, | |
;; included. However, it is not repetition friendly: when you call the | |
;; `repeat' command, you are prompted again for the character. | |
;; So, if you would like to zap to the character # four times, you | |
;; have to press | |
;; M-z # M-z # M-z # M-z # ; call repeatedly `zap-to-char'. | |
;; or | |
;; M-z # C-x z # z # z # ; zap-to-char, the call repeatedly `repeat'. | |
;; Hopefully, there is another way, without redefining `zap-to-char': | |
(let (my-zap-to-char-last-arg) | |
(defun my-zap-to-char () ; make sure the command has no interactive argument | |
"Repetition friendly version of `zap-to-char'." | |
(interactive) | |
(let ((arg (if (eq last-repeatable-command 'my-zap-to-char) | |
my-zap-to-char-last-arg | |
(setq my-zap-to-char-last-arg (read-char "zap to char: "))))) | |
(zap-to-char 1 arg)))) | |
(define-key global-map [remap zap-to-char] 'my-zap-to-char) | |
;; This works only in lexical binding mode. Otherwise you need to | |
;; define a global variable. | |
;; Now you can press: | |
;; M-z # M-z M-z M-z | |
;; or | |
;; M-z # C-x z z z |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Weird, I thought repeat worked even for functions that took interactive input. This is disappointing.
It seems C-x ESC ESC can repeat zap-to-char with its input also. The issue is, C-x ESC ESC is itself not repeatable. Would have been nice otherwise.