scheme interaction mode
[bpt/emacs.git] / lisp / repeat.el
CommitLineData
df96ab1e 1;;; repeat.el --- convenient way to repeat the previous command -*- lexical-binding: t -*-
fd51b1bc 2
ba318903 3;; Copyright (C) 1998, 2001-2014 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\".
790aceec
LI
112A value of `ignore' will disable such messages. To customize
113display, assign a function that takes one string as an arg and
10c75ee3
LI
114displays it however you want.
115If this variable is nil, the normal `message' function will be
116used to display the messages.")
fd51b1bc 117
0a8cbe68
RS
118(defcustom repeat-on-final-keystroke t
119 "Allow `repeat' to re-execute for repeating lastchar of a key sequence.
120If this variable is t, `repeat' determines what key sequence
fd51b1bc
RS
121it was invoked by, extracts the final character of that sequence, and
122re-executes as many times as that final character is hit; so for example
0a8cbe68 123if `repeat' is bound to C-x z, typing C-x z z z repeats the previous command
fd51b1bc 1243 times. If this variable is a sequence of characters, then re-execution
0a8cbe68 125only occurs if the final character by which `repeat' was invoked is a
9dc0cb3d
RS
126member of that sequence. If this variable is nil, no re-execution occurs."
127 :group 'convenience
2c587104
GM
128 :type '(choice (const :tag "Repeat for all keys" t)
129 (const :tag "Don't repeat" nil)
130 (sexp :tag "Repeat for specific keys")))
f1180544 131
fd51b1bc
RS
132;;;;; ****************** HACKS TO THE REST OF EMACS ******************* ;;;;;
133
134;; The basic strategy is to use last-command, a variable built in to Emacs.
135;; There are 2 issues that complicate this strategy. The first is that
136;; last-command is given a bogus value when any kill command is executed;
0a8cbe68 137;; this is done to make it easy for `yank-pop' to know that it's being invoked
fd51b1bc 138;; after a kill command. The second is that the meaning of the command is
0a8cbe68 139;; often altered by the prefix arg, but although Emacs (19.34) has a
fd51b1bc
RS
140;; builtin prefix-arg specifying the arg for the next command, as well as a
141;; builtin current-prefix-arg, it has no builtin last-prefix-arg.
142
143;; There's a builtin (this-command-keys), the return value of which could be
144;; executed with (command-execute), but there's no (last-command-keys).
145;; Using (last-command-keys) if it existed wouldn't be optimal, however,
0a8cbe68 146;; since it would complicate checking membership in repeat-too-dangerous.
fd51b1bc
RS
147
148;; It would of course be trivial to implement last-prefix-arg &
149;; true-last-command by putting something in post-command-hook, but that
150;; entails a performance hit; the approach taken below avoids that.
151
fd51b1bc
RS
152;; Coping with strings of self-insert commands gets hairy when they interact
153;; with auto-filling. Most problems are eliminated by remembering what we're
154;; self-inserting, so we only need to get it from the undo information once.
155
392abfd2
MR
156;; With Emacs 22.2 the variable `last-repeatable-command' stores the
157;; most recently executed command that was not bound to an input event.
158;; `repeat' now repeats that command instead of `real-last-command' to
159;; avoid a "... must be bound to an event with parameters" error.
160
0a8cbe68 161;;;;; *************** ANALOGOUS HACKS TO `repeat' ITSELF **************** ;;;;;
fd51b1bc
RS
162
163;; That mechanism of checking num-input-keys to figure out what's really
164;; going on can be useful to other commands that need to fine-tune their
0a8cbe68
RS
165;; interaction with repeat. Instead of requiring them to advise repeat, we
166;; can just defvar the value they need here, & setq it in the repeat command:
fd51b1bc 167
0a8cbe68
RS
168(defvar repeat-num-input-keys-at-repeat -1
169 "# key sequences read in Emacs session when `repeat' last invoked.")
fd51b1bc
RS
170
171;; Also, we can assign a name to the test for which that variable is
172;; intended, which thereby documents here how to use it, & makes code that
173;; uses it self-documenting:
174
0a8cbe68
RS
175(defsubst repeat-is-really-this-command ()
176 "Return t if this command is happening because user invoked `repeat'.
fd51b1bc
RS
177Usually, when a command is executing, the Emacs builtin variable
178`this-command' identifies the command the user invoked. Some commands modify
0a8cbe68 179that variable on the theory they're doing more good than harm; `repeat' does
fd51b1bc 180that, and usually does do more good than harm. However, like all do-gooders,
0a8cbe68 181sometimes `repeat' gets surprising results from its altruism. The value of
fd51b1bc 182this function is always whether the value of `this-command' would've been
0a8cbe68
RS
183'repeat if `repeat' hadn't modified it."
184 (= repeat-num-input-keys-at-repeat num-input-keys))
fd51b1bc 185
0a8cbe68 186;; An example of the use of (repeat-is-really-this-command) may still be
fd51b1bc
RS
187;; available in <http://www.eskimo.com/~seldon/dotemacs.el>; search for
188;; "defun wm-switch-buffer".
189
0a8cbe68 190;;;;; ******************* THE REPEAT COMMAND ITSELF ******************* ;;;;;
fd51b1bc 191
7d6a2ca4
DL
192(defvar repeat-previous-repeated-command nil
193 "The previous repeated command.")
194
fd51b1bc 195;;;###autoload
0a8cbe68 196(defun repeat (repeat-arg)
fd51b1bc 197 "Repeat most recently executed command.
c88b867f
CY
198If REPEAT-ARG is non-nil (interactively, with a prefix argument),
199supply a prefix argument to that command. Otherwise, give the
200command the same prefix argument it was given before, if any.
fd51b1bc 201
392abfd2
MR
202If this command is invoked by a multi-character key sequence, it
203can then be repeated by repeating the final character of that
204sequence. This behavior can be modified by the global variable
205`repeat-on-final-keystroke'.
206
207`repeat' ignores commands bound to input events. Hence the term
208\"most recently executed command\" shall be read as \"most
209recently executed command not bound to an input event\"."
fd51b1bc
RS
210 ;; The most recently executed command could be anything, so surprises could
211 ;; result if it were re-executed in a context where new dynamically
212 ;; localized variables were shadowing global variables in a `let' clause in
213 ;; here. (Remember that GNU Emacs 19 is dynamically localized.)
214 ;; To avoid that, I tried the `lexical-let' of the Common Lisp extensions,
215 ;; but that entails a very noticeable performance hit, so instead I use the
0a8cbe68 216 ;; "repeat-" prefix, reserved by this package, for *local* variables that
fd51b1bc
RS
217 ;; might be visible to re-executed commands, including this function's arg.
218 (interactive "P")
392abfd2
MR
219 (when (eq last-repeatable-command 'repeat)
220 (setq last-repeatable-command repeat-previous-repeated-command))
221 (cond
222 ((null last-repeatable-command)
7d6a2ca4 223 (error "There is nothing to repeat"))
392abfd2
MR
224 ((eq last-repeatable-command 'mode-exit)
225 (error "last-repeatable-command is mode-exit & can't be repeated"))
226 ((memq last-repeatable-command repeat-too-dangerous)
227 (error "Command %S too dangerous to repeat automatically"
228 last-repeatable-command)))
229 (setq this-command last-repeatable-command
230 repeat-previous-repeated-command last-repeatable-command
231 repeat-num-input-keys-at-repeat num-input-keys)
0a8cbe68
RS
232 (when (null repeat-arg)
233 (setq repeat-arg last-prefix-arg))
fd51b1bc 234 ;; Now determine whether to loop on repeated taps of the final character
0a8cbe68 235 ;; of the key sequence that invoked repeat. The Emacs global
8989a920 236 ;; last-command-event contains the final character now, but may not still
fd51b1bc
RS
237 ;; contain it after the previous command is repeated, so the character
238 ;; needs to be saved.
0a8cbe68
RS
239 (let ((repeat-repeat-char
240 (if (eq repeat-on-final-keystroke t)
8989a920 241 last-command-event
df96ab1e 242 ;; Allow only specified final keystrokes.
8989a920 243 (car (memq last-command-event
fd51b1bc 244 (listify-key-sequence
0a8cbe68 245 repeat-on-final-keystroke))))))
392abfd2
MR
246 (if (memq last-repeatable-command '(exit-minibuffer
247 minibuffer-complete-and-exit
248 self-insert-and-exit))
0a8cbe68
RS
249 (let ((repeat-command (car command-history)))
250 (repeat-message "Repeating %S" repeat-command)
251 (eval repeat-command))
252 (if (null repeat-arg)
392abfd2
MR
253 (repeat-message "Repeating command %S" last-repeatable-command)
254 (setq current-prefix-arg repeat-arg)
255 (repeat-message
256 "Repeating command %S %S" repeat-arg last-repeatable-command))
df96ab1e
SM
257 (when (eq last-repeatable-command 'self-insert-command)
258 ;; We used to use a much more complex code to try and figure out
259 ;; what key was used to run that self-insert-command:
260 ;; (if (<= (- num-input-keys
261 ;; repeat-num-input-keys-at-self-insert)
262 ;; 1)
263 ;; repeat-last-self-insert
264 ;; (let ((range (nth 1 buffer-undo-list)))
265 ;; (condition-case nil
266 ;; (setq repeat-last-self-insert
267 ;; (buffer-substring (car range)
268 ;; (cdr range)))
269 ;; (error (error "%s %s %s" ;Danger, Will Robinson!
270 ;; "repeat can't intuit what you"
271 ;; "inserted before auto-fill"
272 ;; "clobbered it, sorry")))))
273 (setq last-command-event (char-before)))
274 (let ((indirect (indirect-function last-repeatable-command)))
275 (if (or (stringp indirect)
276 (vectorp indirect))
277 ;; Bind last-repeatable-command so that executing the macro does
278 ;; not alter it.
279 (let ((last-repeatable-command last-repeatable-command))
280 (execute-kbd-macro last-repeatable-command))
281 (call-interactively last-repeatable-command))))
0a8cbe68 282 (when repeat-repeat-char
8cd22a08 283 (set-transient-map
df96ab1e
SM
284 (let ((map (make-sparse-keymap)))
285 (define-key map (vector repeat-repeat-char)
286 (if (null repeat-message-function) 'repeat
287 ;; If repeat-message-function is let-bound, preserve it for the
288 ;; next "iterations of the loop".
289 (let ((fun repeat-message-function))
290 (lambda ()
291 (interactive)
292 (let ((repeat-message-function fun))
293 (setq this-command 'repeat)
d20d69c0
SM
294 ;; Beware: messing with `real-this-command' is *bad*, but we
295 ;; need it so `last-repeatable-command' can be recognized
296 ;; later (bug#12232).
297 (setq real-this-command 'repeat)
df96ab1e
SM
298 (call-interactively 'repeat))))))
299 map)))))
9dc0cb3d 300
0a8cbe68
RS
301(defun repeat-message (format &rest args)
302 "Like `message' but displays with `repeat-message-function' if non-nil."
fd51b1bc 303 (let ((message (apply 'format format args)))
0a8cbe68
RS
304 (if repeat-message-function
305 (funcall repeat-message-function message)
fd51b1bc
RS
306 (message "%s" message))))
307
308;; OK, there's one situation left where that doesn't work correctly: when the
309;; most recent self-insertion provoked an auto-fill. The problem is that
ee7683eb 310;; unraveling the undo information after an auto-fill is too hard, since all
fd51b1bc
RS
311;; kinds of stuff can get in there as a result of comment prefixes etc. It'd
312;; be possible to advise do-auto-fill to record the most recent
313;; self-insertion before it does its thing, but that's a performance hit on
314;; auto-fill, which already has performance problems; so it's better to just
315;; leave it like this. If text didn't provoke an auto-fill when the user
316;; typed it, this'll correctly repeat its self-insertion, even if the
317;; repetition does cause auto-fill.
318
319;; If you wanted perfection, probably it'd be necessary to hack do-auto-fill
320;; into 2 functions, maybe-do-auto-fill & really-do-auto-fill, because only
321;; really-do-auto-fill should be advised. As things are, either the undo
322;; information would need to be scanned on every do-auto-fill invocation, or
323;; the code at the top of do-auto-fill deciding whether filling is necessary
324;; would need to be duplicated in the advice, wasting execution time when
325;; filling does turn out to be necessary.
326
327;; I thought maybe this story had a moral, something about functional
328;; decomposition; but now I'm not even sure of that, since a function
329;; call per se is a performance hit, & even the code that would
330;; correspond to really-do-auto-fill has performance problems that
331;; can make it necessary to stop typing while Emacs catches up.
332;; Maybe the real moral is that perfection is a chimera.
333
334;; Ah, hell, it's all going to fall into a black hole someday anyway.
335
336;;;;; ************************* EMACS CONTROL ************************* ;;;;;
337
0a8cbe68 338(provide 'repeat)
fd51b1bc 339
0a8cbe68 340;;; repeat.el ends here