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