repeat.el 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. ;;; repeat.el --- convenient way to repeat the previous command -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1998, 2001-2017 Free Software Foundation, Inc.
  3. ;; Author: Will Mengarini <seldon@eskimo.com>
  4. ;; Created: Mo 02 Mar 98
  5. ;; Version: 0.51
  6. ;; Keywords: convenience, vi, repeat
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Sometimes the fastest way to get something done is just to lean on a key;
  20. ;; moving forward through a series of words by leaning on M-f is an example.
  21. ;; But 'forward-page is orthodoxly bound to C-x ], so moving forward through
  22. ;; several pages requires
  23. ;; Loop until desired page is reached:
  24. ;; Hold down control key with left pinkie.
  25. ;; Tap <x>.
  26. ;; Lift left pinkie off control key.
  27. ;; Tap <]>.
  28. ;; This is a pain in the ass.
  29. ;; This package defines a command that repeats the preceding command,
  30. ;; whatever that was, including its arguments, whatever they were.
  31. ;; This command is connected to the key C-x z.
  32. ;; To repeat the previous command once, type C-x z.
  33. ;; To repeat it a second time immediately after, type just z.
  34. ;; By typing z again and again, you can repeat the command over and over.
  35. ;; This works correctly inside a keyboard macro as far as recording and
  36. ;; playback go, but `edit-kbd-macro' gets it wrong. That shouldn't really
  37. ;; matter; if you need to edit something like
  38. ;; C-x ] ;; forward-page
  39. ;; C-x z ;; repeat
  40. ;; zz ;; self-insert-command * 2
  41. ;; C-x ;; Control-X-prefix
  42. ;; you can just kill the bogus final 2 lines, then duplicate the repeat line
  43. ;; as many times as it's really needed. Also, `edit-kbd-macro' works
  44. ;; correctly if `repeat' is invoked through a rebinding to a single keystroke
  45. ;; and the global variable repeat-on-final-keystroke is set to a value
  46. ;; that doesn't include that keystroke. For example, the lines
  47. ;; (global-set-key "\C-z" 'repeat)
  48. ;; (setq repeat-on-final-keystroke "z")
  49. ;; in your .emacs would allow `edit-kbd-macro' to work correctly when C-z was
  50. ;; used in a keyboard macro to invoke `repeat', but would still allow C-x z
  51. ;; to be used for `repeat' elsewhere. The real reason for documenting this
  52. ;; isn't that anybody would need it for the `edit-kbd-macro' problem, but
  53. ;; that there might be other unexpected ramifications of re-executing on
  54. ;; repetitions of the final keystroke, and this shows how to do workarounds.
  55. ;; If the preceding command had a prefix argument, that argument is applied
  56. ;; to the repeat command, unless the repeat command is given a new prefix
  57. ;; argument, in which case it applies that new prefix argument to the
  58. ;; preceding command. This means a key sequence like C-u - C-x C-t can be
  59. ;; repeated. (It shoves the preceding line upward in the buffer.)
  60. ;; Here are some other key sequences with which repeat might be useful:
  61. ;; C-u - C-t [shove preceding character backward in line]
  62. ;; C-u - M-t [shove preceding word backward in sentence]
  63. ;; C-x ^ enlarge-window [one line] (assuming frame has > 1 window)
  64. ;; C-u - C-x ^ [shrink window one line]
  65. ;; C-x ` next-error
  66. ;; C-u - C-x ` [previous error]
  67. ;; C-x DEL backward-kill-sentence
  68. ;; C-x e call-last-kbd-macro
  69. ;; C-x r i insert-register
  70. ;; C-x r t string-rectangle
  71. ;; C-x TAB indent-rigidly [one character]
  72. ;; C-u - C-x TAB [outdent rigidly one character]
  73. ;; C-x { shrink-window-horizontally
  74. ;; C-x } enlarge-window-horizontally
  75. ;; This command was first called `vi-dot', because
  76. ;; it was inspired by the `.' command in the vi editor,
  77. ;; but it was renamed to make its name more meaningful.
  78. ;;; Code:
  79. ;;;;; ************************* USER OPTIONS ************************** ;;;;;
  80. (defcustom repeat-too-dangerous '(kill-this-buffer)
  81. "Commands too dangerous to repeat with \\[repeat]."
  82. :group 'convenience
  83. :type '(repeat function))
  84. ;; If the last command was self-insert-command, the char to be inserted was
  85. ;; obtained by that command from last-command-event, which has now been
  86. ;; clobbered by the command sequence that invoked `repeat'. We could get it
  87. ;; from (recent-keys) & set last-command-event to that, "unclobbering" it, but
  88. ;; this has the disadvantage that if the user types a sequence of different
  89. ;; chars then invokes repeat, only the final char will be inserted. In vi,
  90. ;; the dot command can reinsert the entire most-recently-inserted sequence.
  91. (defvar repeat-message-function nil
  92. "If non-nil, function used by `repeat' command to say what it's doing.
  93. Message is something like \"Repeating command glorp\".
  94. A value of `ignore' will disable such messages. To customize
  95. display, assign a function that takes one string as an arg and
  96. displays it however you want.
  97. If this variable is nil, the normal `message' function will be
  98. used to display the messages.")
  99. (defcustom repeat-on-final-keystroke t
  100. "Allow `repeat' to re-execute for repeating lastchar of a key sequence.
  101. If this variable is t, `repeat' determines what key sequence
  102. it was invoked by, extracts the final character of that sequence, and
  103. re-executes as many times as that final character is hit; so for example
  104. if `repeat' is bound to C-x z, typing C-x z z z repeats the previous command
  105. 3 times. If this variable is a sequence of characters, then re-execution
  106. only occurs if the final character by which `repeat' was invoked is a
  107. member of that sequence. If this variable is nil, no re-execution occurs."
  108. :group 'convenience
  109. :type '(choice (const :tag "Repeat for all keys" t)
  110. (const :tag "Don't repeat" nil)
  111. (sexp :tag "Repeat for specific keys")))
  112. ;;;;; ****************** HACKS TO THE REST OF EMACS ******************* ;;;;;
  113. ;; The basic strategy is to use last-command, a variable built in to Emacs.
  114. ;; There are 2 issues that complicate this strategy. The first is that
  115. ;; last-command is given a bogus value when any kill command is executed;
  116. ;; this is done to make it easy for `yank-pop' to know that it's being invoked
  117. ;; after a kill command. The second is that the meaning of the command is
  118. ;; often altered by the prefix arg, but although Emacs (19.34) has a
  119. ;; builtin prefix-arg specifying the arg for the next command, as well as a
  120. ;; builtin current-prefix-arg, it has no builtin last-prefix-arg.
  121. ;; There's a builtin (this-command-keys), the return value of which could be
  122. ;; executed with (command-execute), but there's no (last-command-keys).
  123. ;; Using (last-command-keys) if it existed wouldn't be optimal, however,
  124. ;; since it would complicate checking membership in repeat-too-dangerous.
  125. ;; It would of course be trivial to implement last-prefix-arg &
  126. ;; true-last-command by putting something in post-command-hook, but that
  127. ;; entails a performance hit; the approach taken below avoids that.
  128. ;; Coping with strings of self-insert commands gets hairy when they interact
  129. ;; with auto-filling. Most problems are eliminated by remembering what we're
  130. ;; self-inserting, so we only need to get it from the undo information once.
  131. ;; With Emacs 22.2 the variable `last-repeatable-command' stores the
  132. ;; most recently executed command that was not bound to an input event.
  133. ;; `repeat' now repeats that command instead of `real-last-command' to
  134. ;; avoid a "... must be bound to an event with parameters" error.
  135. ;;;;; *************** ANALOGOUS HACKS TO `repeat' ITSELF **************** ;;;;;
  136. ;; That mechanism of checking num-input-keys to figure out what's really
  137. ;; going on can be useful to other commands that need to fine-tune their
  138. ;; interaction with repeat. Instead of requiring them to advise repeat, we
  139. ;; can just defvar the value they need here, & setq it in the repeat command:
  140. (defvar repeat-num-input-keys-at-repeat -1
  141. "# key sequences read in Emacs session when `repeat' last invoked.")
  142. ;; Also, we can assign a name to the test for which that variable is
  143. ;; intended, which thereby documents here how to use it, & makes code that
  144. ;; uses it self-documenting:
  145. (defsubst repeat-is-really-this-command ()
  146. "Return t if this command is happening because user invoked `repeat'.
  147. Usually, when a command is executing, the Emacs builtin variable
  148. `this-command' identifies the command the user invoked. Some commands modify
  149. that variable on the theory they're doing more good than harm; `repeat' does
  150. that, and usually does do more good than harm. However, like all do-gooders,
  151. sometimes `repeat' gets surprising results from its altruism. The value of
  152. this function is always whether the value of `this-command' would've been
  153. 'repeat if `repeat' hadn't modified it."
  154. (= repeat-num-input-keys-at-repeat num-input-keys))
  155. ;; An example of the use of (repeat-is-really-this-command) may still be
  156. ;; available in <http://www.eskimo.com/~seldon/dotemacs.el>; search for
  157. ;; "defun wm-switch-buffer".
  158. ;;;;; ******************* THE REPEAT COMMAND ITSELF ******************* ;;;;;
  159. (defvar repeat-previous-repeated-command nil
  160. "The previous repeated command.")
  161. ;;;###autoload
  162. (defun repeat (repeat-arg)
  163. "Repeat most recently executed command.
  164. If REPEAT-ARG is non-nil (interactively, with a prefix argument),
  165. supply a prefix argument to that command. Otherwise, give the
  166. command the same prefix argument it was given before, if any.
  167. If this command is invoked by a multi-character key sequence, it
  168. can then be repeated by repeating the final character of that
  169. sequence. This behavior can be modified by the global variable
  170. `repeat-on-final-keystroke'.
  171. `repeat' ignores commands bound to input events. Hence the term
  172. \"most recently executed command\" shall be read as \"most
  173. recently executed command not bound to an input event\"."
  174. ;; The most recently executed command could be anything, so surprises could
  175. ;; result if it were re-executed in a context where new dynamically
  176. ;; localized variables were shadowing global variables in a `let' clause in
  177. ;; here. (Remember that GNU Emacs 19 is dynamically localized.)
  178. ;; To avoid that, I tried the `lexical-let' of the Common Lisp extensions,
  179. ;; but that entails a very noticeable performance hit, so instead I use the
  180. ;; "repeat-" prefix, reserved by this package, for *local* variables that
  181. ;; might be visible to re-executed commands, including this function's arg.
  182. (interactive "P")
  183. (when (eq last-repeatable-command 'repeat)
  184. (setq last-repeatable-command repeat-previous-repeated-command))
  185. (cond
  186. ((null last-repeatable-command)
  187. (error "There is nothing to repeat"))
  188. ((eq last-repeatable-command 'mode-exit)
  189. (error "last-repeatable-command is mode-exit & can't be repeated"))
  190. ((memq last-repeatable-command repeat-too-dangerous)
  191. (error "Command %S too dangerous to repeat automatically"
  192. last-repeatable-command)))
  193. (setq this-command last-repeatable-command
  194. repeat-previous-repeated-command last-repeatable-command
  195. repeat-num-input-keys-at-repeat num-input-keys)
  196. (when (null repeat-arg)
  197. (setq repeat-arg last-prefix-arg))
  198. ;; Now determine whether to loop on repeated taps of the final character
  199. ;; of the key sequence that invoked repeat. The Emacs global
  200. ;; last-command-event contains the final character now, but may not still
  201. ;; contain it after the previous command is repeated, so the character
  202. ;; needs to be saved.
  203. (let ((repeat-repeat-char
  204. (if (eq repeat-on-final-keystroke t)
  205. last-command-event
  206. ;; Allow only specified final keystrokes.
  207. (car (memq last-command-event
  208. (listify-key-sequence
  209. repeat-on-final-keystroke))))))
  210. (if (memq last-repeatable-command '(exit-minibuffer
  211. minibuffer-complete-and-exit
  212. self-insert-and-exit))
  213. (let ((repeat-command (car command-history)))
  214. (repeat-message "Repeating %S" repeat-command)
  215. (eval repeat-command))
  216. (if (null repeat-arg)
  217. (repeat-message "Repeating command %S" last-repeatable-command)
  218. (setq current-prefix-arg repeat-arg)
  219. (repeat-message
  220. "Repeating command %S %S" repeat-arg last-repeatable-command))
  221. (when (eq last-repeatable-command 'self-insert-command)
  222. ;; We used to use a much more complex code to try and figure out
  223. ;; what key was used to run that self-insert-command:
  224. ;; (if (<= (- num-input-keys
  225. ;; repeat-num-input-keys-at-self-insert)
  226. ;; 1)
  227. ;; repeat-last-self-insert
  228. ;; (let ((range (nth 1 buffer-undo-list)))
  229. ;; (condition-case nil
  230. ;; (setq repeat-last-self-insert
  231. ;; (buffer-substring (car range)
  232. ;; (cdr range)))
  233. ;; (error (error "%s %s %s" ;Danger, Will Robinson!
  234. ;; "repeat can't intuit what you"
  235. ;; "inserted before auto-fill"
  236. ;; "clobbered it, sorry")))))
  237. (setq last-command-event (char-before)))
  238. (let ((indirect (indirect-function last-repeatable-command)))
  239. (if (or (stringp indirect)
  240. (vectorp indirect))
  241. ;; Bind last-repeatable-command so that executing the macro does
  242. ;; not alter it.
  243. (let ((last-repeatable-command last-repeatable-command))
  244. (execute-kbd-macro last-repeatable-command))
  245. (call-interactively last-repeatable-command))))
  246. (when repeat-repeat-char
  247. (set-transient-map
  248. (let ((map (make-sparse-keymap)))
  249. (define-key map (vector repeat-repeat-char)
  250. (if (null repeat-message-function) 'repeat
  251. ;; If repeat-message-function is let-bound, preserve it for the
  252. ;; next "iterations of the loop".
  253. (let ((fun repeat-message-function))
  254. (lambda ()
  255. (interactive)
  256. (let ((repeat-message-function fun))
  257. (setq this-command 'repeat)
  258. ;; Beware: messing with `real-this-command' is *bad*, but we
  259. ;; need it so `last-repeatable-command' can be recognized
  260. ;; later (bug#12232).
  261. (setq real-this-command 'repeat)
  262. (call-interactively 'repeat))))))
  263. map)))))
  264. (defun repeat-message (format &rest args)
  265. "Like `message' but displays with `repeat-message-function' if non-nil."
  266. (let ((message (apply 'format format args)))
  267. (if repeat-message-function
  268. (funcall repeat-message-function message)
  269. (message "%s" message))))
  270. ;; OK, there's one situation left where that doesn't work correctly: when the
  271. ;; most recent self-insertion provoked an auto-fill. The problem is that
  272. ;; unraveling the undo information after an auto-fill is too hard, since all
  273. ;; kinds of stuff can get in there as a result of comment prefixes etc. It'd
  274. ;; be possible to advise do-auto-fill to record the most recent
  275. ;; self-insertion before it does its thing, but that's a performance hit on
  276. ;; auto-fill, which already has performance problems; so it's better to just
  277. ;; leave it like this. If text didn't provoke an auto-fill when the user
  278. ;; typed it, this'll correctly repeat its self-insertion, even if the
  279. ;; repetition does cause auto-fill.
  280. ;; If you wanted perfection, probably it'd be necessary to hack do-auto-fill
  281. ;; into 2 functions, maybe-do-auto-fill & really-do-auto-fill, because only
  282. ;; really-do-auto-fill should be advised. As things are, either the undo
  283. ;; information would need to be scanned on every do-auto-fill invocation, or
  284. ;; the code at the top of do-auto-fill deciding whether filling is necessary
  285. ;; would need to be duplicated in the advice, wasting execution time when
  286. ;; filling does turn out to be necessary.
  287. ;; I thought maybe this story had a moral, something about functional
  288. ;; decomposition; but now I'm not even sure of that, since a function
  289. ;; call per se is a performance hit, & even the code that would
  290. ;; correspond to really-do-auto-fill has performance problems that
  291. ;; can make it necessary to stop typing while Emacs catches up.
  292. ;; Maybe the real moral is that perfection is a chimera.
  293. ;; Ah, hell, it's all going to fall into a black hole someday anyway.
  294. ;;;;; ************************* EMACS CONTROL ************************* ;;;;;
  295. (provide 'repeat)
  296. ;;; repeat.el ends here