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