1 ;;; repeat.el --- convenient way to repeat the previous command
3 ;; Copyright (C) 1998, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;; Author: Will Mengarini <seldon@eskimo.com>
7 ;; Created: Mo 02 Mar 98
8 ;; Version: 0.51, We 13 May 98
9 ;; Keywords: convenience, vi, repeat
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; Sometimes the fastest way to get something done is just to lean on a key;
29 ;; moving forward through a series of words by leaning on M-f is an example.
30 ;; But 'forward-page is orthodoxily bound to C-x ], so moving forward through
31 ;; several pages requires
32 ;; Loop until desired page is reached:
33 ;; Hold down control key with left pinkie.
35 ;; Lift left pinkie off control key.
37 ;; This is a pain in the ass.
39 ;; This package defines a command that repeats the preceding command,
40 ;; whatever that was, including its arguments, whatever they were.
41 ;; This command is connected to the key C-x z.
42 ;; To repeat the previous command once, type C-x z.
43 ;; To repeat it a second time immediately after, type just z.
44 ;; By typing z again and again, you can repeat the command over and over.
46 ;; This works correctly inside a keyboard macro as far as recording and
47 ;; playback go, but `edit-kbd-macro' gets it wrong. That shouldn't really
48 ;; matter; if you need to edit something like
49 ;; C-x ] ;; forward-page
51 ;; zz ;; self-insert-command * 2
52 ;; C-x ;; Control-X-prefix
53 ;; you can just kill the bogus final 2 lines, then duplicate the repeat line
54 ;; as many times as it's really needed. Also, `edit-kbd-macro' works
55 ;; correctly if `repeat' is invoked through a rebinding to a single keystroke
56 ;; and the global variable repeat-on-final-keystroke is set to a value
57 ;; that doesn't include that keystroke. For example, the lines
58 ;; (global-set-key "\C-z" 'repeat)
59 ;; (setq repeat-on-final-keystroke "z")
60 ;; in your .emacs would allow `edit-kbd-macro' to work correctly when C-z was
61 ;; used in a keyboard macro to invoke `repeat', but would still allow C-x z
62 ;; to be used for `repeat' elsewhere. The real reason for documenting this
63 ;; isn't that anybody would need it for the `edit-kbd-macro' problem, but
64 ;; that there might be other unexpected ramifications of re-executing on
65 ;; repetitions of the final keystroke, and this shows how to do workarounds.
67 ;; If the preceding command had a prefix argument, that argument is applied
68 ;; to the repeat command, unless the repeat command is given a new prefix
69 ;; argument, in which case it applies that new prefix argument to the
70 ;; preceding command. This means a key sequence like C-u - C-x C-t can be
71 ;; repeated. (It shoves the preceding line upward in the buffer.)
73 ;; Here are some other key sequences with which repeat might be useful:
74 ;; C-u - C-t [shove preceding character backward in line]
75 ;; C-u - M-t [shove preceding word backward in sentence]
76 ;; C-x ^ enlarge-window [one line] (assuming frame has > 1 window)
77 ;; C-u - C-x ^ [shrink window one line]
79 ;; C-u - C-x ` [previous error]
80 ;; C-x DEL backward-kill-sentence
81 ;; C-x e call-last-kbd-macro
82 ;; C-x r i insert-register
83 ;; C-x r t string-rectangle
84 ;; C-x TAB indent-rigidly [one character]
85 ;; C-u - C-x TAB [outdent rigidly one character]
86 ;; C-x { shrink-window-horizontally
87 ;; C-x } enlarge-window-horizontally
89 ;; This command was first called `vi-dot', because
90 ;; it was inspired by the `.' command in the vi editor,
91 ;; but it was renamed to make its name more meaningful.
95 ;;;;; ************************* USER OPTIONS ************************** ;;;;;
97 (defcustom repeat-too-dangerous
'(kill-this-buffer)
98 "Commands too dangerous to repeat with \\[repeat]."
100 :type
'(repeat function
))
102 ;; If the last command was self-insert-command, the char to be inserted was
103 ;; obtained by that command from last-command-event, which has now been
104 ;; clobbered by the command sequence that invoked `repeat'. We could get it
105 ;; from (recent-keys) & set last-command-event to that, "unclobbering" it, but
106 ;; this has the disadvantage that if the user types a sequence of different
107 ;; chars then invokes repeat, only the final char will be inserted. In vi,
108 ;; the dot command can reinsert the entire most-recently-inserted sequence.
110 (defvar repeat-message-function nil
111 "If non-nil, function used by `repeat' command to say what it's doing.
112 Message is something like \"Repeating command glorp\".
113 To disable such messages, set this variable to `ignore'. To customize
114 display, assign a function that takes one string as an arg and displays
115 it however you want.")
117 (defcustom repeat-on-final-keystroke t
118 "Allow `repeat' to re-execute for repeating lastchar of a key sequence.
119 If this variable is t, `repeat' determines what key sequence
120 it was invoked by, extracts the final character of that sequence, and
121 re-executes as many times as that final character is hit; so for example
122 if `repeat' is bound to C-x z, typing C-x z z z repeats the previous command
123 3 times. If this variable is a sequence of characters, then re-execution
124 only occurs if the final character by which `repeat' was invoked is a
125 member of that sequence. If this variable is nil, no re-execution occurs."
127 :type
'(choice (const :tag
"Repeat for all keys" t
)
128 (const :tag
"Don't repeat" nil
)
129 (sexp :tag
"Repeat for specific keys")))
131 ;;;;; ****************** HACKS TO THE REST OF EMACS ******************* ;;;;;
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;
136 ;; this is done to make it easy for `yank-pop' to know that it's being invoked
137 ;; after a kill command. The second is that the meaning of the command is
138 ;; often altered by the prefix arg, but although Emacs (19.34) has a
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.
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,
145 ;; since it would complicate checking membership in repeat-too-dangerous.
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.
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.
155 ;; With Emacs 22.2 the variable `last-repeatable-command' stores the
156 ;; most recently executed command that was not bound to an input event.
157 ;; `repeat' now repeats that command instead of `real-last-command' to
158 ;; avoid a "... must be bound to an event with parameters" error.
160 (defvar repeat-last-self-insert nil
161 "If last repeated command was `self-insert-command', it inserted this.")
163 ;; That'll require another keystroke count so we know we're in a string of
164 ;; repetitions of self-insert commands:
166 (defvar repeat-num-input-keys-at-self-insert -
1
167 "# key sequences read in Emacs session when `self-insert-command' repeated.")
169 ;;;;; *************** ANALOGOUS HACKS TO `repeat' ITSELF **************** ;;;;;
171 ;; That mechanism of checking num-input-keys to figure out what's really
172 ;; going on can be useful to other commands that need to fine-tune their
173 ;; interaction with repeat. Instead of requiring them to advise repeat, we
174 ;; can just defvar the value they need here, & setq it in the repeat command:
176 (defvar repeat-num-input-keys-at-repeat -
1
177 "# key sequences read in Emacs session when `repeat' last invoked.")
179 ;; Also, we can assign a name to the test for which that variable is
180 ;; intended, which thereby documents here how to use it, & makes code that
181 ;; uses it self-documenting:
183 (defsubst repeat-is-really-this-command
()
184 "Return t if this command is happening because user invoked `repeat'.
185 Usually, when a command is executing, the Emacs builtin variable
186 `this-command' identifies the command the user invoked. Some commands modify
187 that variable on the theory they're doing more good than harm; `repeat' does
188 that, and usually does do more good than harm. However, like all do-gooders,
189 sometimes `repeat' gets surprising results from its altruism. The value of
190 this function is always whether the value of `this-command' would've been
191 'repeat if `repeat' hadn't modified it."
192 (= repeat-num-input-keys-at-repeat num-input-keys
))
194 ;; An example of the use of (repeat-is-really-this-command) may still be
195 ;; available in <http://www.eskimo.com/~seldon/dotemacs.el>; search for
196 ;; "defun wm-switch-buffer".
198 ;;;;; ******************* THE REPEAT COMMAND ITSELF ******************* ;;;;;
200 (defvar repeat-previous-repeated-command nil
201 "The previous repeated command.")
203 ;; The following variable counts repeated self-insertions. The idea is
204 ;; that repeating a self-insertion command and subsequently undoing it
205 ;; should have almost the same effect as if the characters were inserted
206 ;; manually. The basic difference is that we leave in one undo-boundary
207 ;; between the original insertion and its first repetition.
208 (defvar repeat-undo-count nil
209 "Number of self-insertions since last `undo-boundary'.")
212 (defun repeat (repeat-arg)
213 "Repeat most recently executed command.
214 With prefix arg, apply new prefix arg to that command; otherwise,
215 use the prefix arg that was used before (if any).
216 This command is like the `.' command in the vi editor.
218 If this command is invoked by a multi-character key sequence, it
219 can then be repeated by repeating the final character of that
220 sequence. This behavior can be modified by the global variable
221 `repeat-on-final-keystroke'.
223 `repeat' ignores commands bound to input events. Hence the term
224 \"most recently executed command\" shall be read as \"most
225 recently executed command not bound to an input event\"."
226 ;; The most recently executed command could be anything, so surprises could
227 ;; result if it were re-executed in a context where new dynamically
228 ;; localized variables were shadowing global variables in a `let' clause in
229 ;; here. (Remember that GNU Emacs 19 is dynamically localized.)
230 ;; To avoid that, I tried the `lexical-let' of the Common Lisp extensions,
231 ;; but that entails a very noticeable performance hit, so instead I use the
232 ;; "repeat-" prefix, reserved by this package, for *local* variables that
233 ;; might be visible to re-executed commands, including this function's arg.
235 (when (eq last-repeatable-command
'repeat
)
236 (setq last-repeatable-command repeat-previous-repeated-command
))
238 ((null last-repeatable-command
)
239 (error "There is nothing to repeat"))
240 ((eq last-repeatable-command
'mode-exit
)
241 (error "last-repeatable-command is mode-exit & can't be repeated"))
242 ((memq last-repeatable-command repeat-too-dangerous
)
243 (error "Command %S too dangerous to repeat automatically"
244 last-repeatable-command
)))
245 (setq this-command last-repeatable-command
246 repeat-previous-repeated-command last-repeatable-command
247 repeat-num-input-keys-at-repeat num-input-keys
)
248 (when (null repeat-arg
)
249 (setq repeat-arg last-prefix-arg
))
250 ;; Now determine whether to loop on repeated taps of the final character
251 ;; of the key sequence that invoked repeat. The Emacs global
252 ;; last-command-event contains the final character now, but may not still
253 ;; contain it after the previous command is repeated, so the character
254 ;; needs to be saved.
255 (let ((repeat-repeat-char
256 (if (eq repeat-on-final-keystroke t
)
258 ;; allow only specified final keystrokes
259 (car (memq last-command-event
260 (listify-key-sequence
261 repeat-on-final-keystroke
))))))
262 (if (memq last-repeatable-command
'(exit-minibuffer
263 minibuffer-complete-and-exit
264 self-insert-and-exit
))
265 (let ((repeat-command (car command-history
)))
266 (repeat-message "Repeating %S" repeat-command
)
267 (eval repeat-command
))
268 (if (null repeat-arg
)
269 (repeat-message "Repeating command %S" last-repeatable-command
)
270 (setq current-prefix-arg repeat-arg
)
272 "Repeating command %S %S" repeat-arg last-repeatable-command
))
273 (if (eq last-repeatable-command
'self-insert-command
)
275 (if (<= (- num-input-keys
276 repeat-num-input-keys-at-self-insert
)
278 repeat-last-self-insert
279 (let ((range (nth 1 buffer-undo-list
)))
281 (setq repeat-last-self-insert
282 (buffer-substring (car range
)
284 (error (error "%s %s %s" ;Danger, Will Robinson!
285 "repeat can't intuit what you"
286 "inserted before auto-fill"
287 "clobbered it, sorry")))))))
288 (setq repeat-num-input-keys-at-self-insert num-input-keys
)
289 ;; If the self-insert had a repeat count, INSERTION
290 ;; includes that many copies of the same character.
291 ;; So use just the first character
292 ;; and repeat it the right number of times.
293 (setq insertion
(substring insertion -
1))
294 (let ((count (prefix-numeric-value repeat-arg
))
296 ;; Run pre- and post-command hooks for self-insertion too.
297 (run-hooks 'pre-command-hook
)
299 ((not repeat-undo-count
))
300 ((< repeat-undo-count
20)
301 ;; Don't make an undo-boundary here.
302 (setq repeat-undo-count
(1+ repeat-undo-count
)))
304 ;; Make an undo-boundary after 20 repetitions only.
306 (setq repeat-undo-count
1)))
308 (repeat-self-insert insertion
)
310 (run-hooks 'post-command-hook
)))
311 (let ((indirect (indirect-function last-repeatable-command
)))
312 ;; Make each repetition undo separately.
314 (if (or (stringp indirect
)
316 ;; Bind real-last-command so that executing the macro does
317 ;; not alter it. Do the same for last-repeatable-command.
318 (let ((real-last-command real-last-command
)
319 (last-repeatable-command last-repeatable-command
))
320 (execute-kbd-macro last-repeatable-command
))
321 (run-hooks 'pre-command-hook
)
322 (call-interactively last-repeatable-command
)
323 (run-hooks 'post-command-hook
)))))
324 (when repeat-repeat-char
325 ;; A simple recursion here gets into trouble with max-lisp-eval-depth
326 ;; on long sequences of repetitions of a command like `forward-word'
327 ;; (only 32 repetitions are possible given the default value of 200 for
328 ;; max-lisp-eval-depth), but if I now locally disable the repeat char I
329 ;; can iterate indefinitely here around a single level of recursion.
330 (let (repeat-on-final-keystroke
331 ;; Bind `undo-inhibit-record-point' to t in order to avoid
332 ;; recording point in `buffer-undo-list' here. We have to
333 ;; do this since the command loop does not set the last
334 ;; position of point thus confusing the point recording
335 ;; mechanism when inserting or deleting text.
336 (undo-inhibit-record-point t
))
337 (setq real-last-command
'repeat
)
338 (setq repeat-undo-count
1)
340 (while (let ((evt (read-key)))
341 ;; For clicks, we need to strip the meta-data to
342 ;; check the underlying event name.
343 (eq (or (car-safe evt
) evt
)
344 (or (car-safe repeat-repeat-char
)
345 repeat-repeat-char
)))
347 ;; Make sure `repeat-undo-count' is reset.
348 (setq repeat-undo-count nil
))
349 (setq unread-command-events
(list last-input-event
))))))
351 (defun repeat-self-insert (string)
353 (while (< i
(length string
))
354 (let ((last-command-event (aref string i
)))
355 (self-insert-command 1))
358 (defun repeat-message (format &rest args
)
359 "Like `message' but displays with `repeat-message-function' if non-nil."
360 (let ((message (apply 'format format args
)))
361 (if repeat-message-function
362 (funcall repeat-message-function message
)
363 (message "%s" message
))))
365 ;; OK, there's one situation left where that doesn't work correctly: when the
366 ;; most recent self-insertion provoked an auto-fill. The problem is that
367 ;; unravelling the undo information after an auto-fill is too hard, since all
368 ;; kinds of stuff can get in there as a result of comment prefixes etc. It'd
369 ;; be possible to advise do-auto-fill to record the most recent
370 ;; self-insertion before it does its thing, but that's a performance hit on
371 ;; auto-fill, which already has performance problems; so it's better to just
372 ;; leave it like this. If text didn't provoke an auto-fill when the user
373 ;; typed it, this'll correctly repeat its self-insertion, even if the
374 ;; repetition does cause auto-fill.
376 ;; If you wanted perfection, probably it'd be necessary to hack do-auto-fill
377 ;; into 2 functions, maybe-do-auto-fill & really-do-auto-fill, because only
378 ;; really-do-auto-fill should be advised. As things are, either the undo
379 ;; information would need to be scanned on every do-auto-fill invocation, or
380 ;; the code at the top of do-auto-fill deciding whether filling is necessary
381 ;; would need to be duplicated in the advice, wasting execution time when
382 ;; filling does turn out to be necessary.
384 ;; I thought maybe this story had a moral, something about functional
385 ;; decomposition; but now I'm not even sure of that, since a function
386 ;; call per se is a performance hit, & even the code that would
387 ;; correspond to really-do-auto-fill has performance problems that
388 ;; can make it necessary to stop typing while Emacs catches up.
389 ;; Maybe the real moral is that perfection is a chimera.
391 ;; Ah, hell, it's all going to fall into a black hole someday anyway.
393 ;;;;; ************************* EMACS CONTROL ************************* ;;;;;
397 ;; arch-tag: cd569600-a1ad-4fa7-9062-bb91dfeaf1db
398 ;;; repeat.el ends here