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