add-log font-lock fix for mutilple authors (bug#8644)
[emacs.git] / lisp / repeat.el
blobb33039b609baac9b8222407b018d0a7bb650f05f
1 ;;; repeat.el --- convenient way to repeat the previous command
3 ;; Copyright (C) 1998, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: Will Mengarini <seldon@eskimo.com>
6 ;; Created: Mo 02 Mar 98
7 ;; Version: 0.51
8 ;; Keywords: convenience, vi, repeat
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs 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 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs 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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
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.
29 ;; But 'forward-page is orthodoxily bound to C-x ], so moving forward through
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.
38 ;; This package defines a command that repeats the preceding command,
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.
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
49 ;; C-x z ;; repeat
50 ;; zz ;; self-insert-command * 2
51 ;; C-x ;; Control-X-prefix
52 ;; you can just kill the bogus final 2 lines, then duplicate the repeat line
53 ;; as many times as it's really needed. Also, `edit-kbd-macro' works
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
56 ;; that doesn't include that keystroke. For example, the lines
57 ;; (global-set-key "\C-z" 'repeat)
58 ;; (setq repeat-on-final-keystroke "z")
59 ;; in your .emacs would allow `edit-kbd-macro' to work correctly when C-z was
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
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.
66 ;; If the preceding command had a prefix argument, that argument is applied
67 ;; to the repeat command, unless the repeat command is given a new prefix
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.)
72 ;; Here are some other key sequences with which repeat might be useful:
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
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.
92 ;;; Code:
94 ;;;;; ************************* USER OPTIONS ************************** ;;;;;
96 (defcustom repeat-too-dangerous '(kill-this-buffer)
97 "Commands too dangerous to repeat with \\[repeat]."
98 :group 'convenience
99 :type '(repeat function))
101 ;; If the last command was self-insert-command, the char to be inserted was
102 ;; obtained by that command from last-command-event, which has now been
103 ;; clobbered by the command sequence that invoked `repeat'. We could get it
104 ;; from (recent-keys) & set last-command-event to that, "unclobbering" it, but
105 ;; this has the disadvantage that if the user types a sequence of different
106 ;; chars then invokes repeat, only the final char will be inserted. In vi,
107 ;; the dot command can reinsert the entire most-recently-inserted sequence.
109 (defvar repeat-message-function nil
110 "If non-nil, function used by `repeat' command to say what it's doing.
111 Message is something like \"Repeating command glorp\".
112 To disable such messages, set this variable to `ignore'. To customize
113 display, assign a function that takes one string as an arg and displays
114 it however you want.")
116 (defcustom repeat-on-final-keystroke t
117 "Allow `repeat' to re-execute for repeating lastchar of a key sequence.
118 If this variable is t, `repeat' determines what key sequence
119 it was invoked by, extracts the final character of that sequence, and
120 re-executes as many times as that final character is hit; so for example
121 if `repeat' is bound to C-x z, typing C-x z z z repeats the previous command
122 3 times. If this variable is a sequence of characters, then re-execution
123 only occurs if the final character by which `repeat' was invoked is a
124 member of that sequence. If this variable is nil, no re-execution occurs."
125 :group 'convenience
126 :type 'boolean)
128 ;;;;; ****************** HACKS TO THE REST OF EMACS ******************* ;;;;;
130 ;; The basic strategy is to use last-command, a variable built in to Emacs.
131 ;; There are 2 issues that complicate this strategy. The first is that
132 ;; last-command is given a bogus value when any kill command is executed;
133 ;; this is done to make it easy for `yank-pop' to know that it's being invoked
134 ;; after a kill command. The second is that the meaning of the command is
135 ;; often altered by the prefix arg, but although Emacs (19.34) has a
136 ;; builtin prefix-arg specifying the arg for the next command, as well as a
137 ;; builtin current-prefix-arg, it has no builtin last-prefix-arg.
139 ;; There's a builtin (this-command-keys), the return value of which could be
140 ;; executed with (command-execute), but there's no (last-command-keys).
141 ;; Using (last-command-keys) if it existed wouldn't be optimal, however,
142 ;; since it would complicate checking membership in repeat-too-dangerous.
144 ;; It would of course be trivial to implement last-prefix-arg &
145 ;; true-last-command by putting something in post-command-hook, but that
146 ;; entails a performance hit; the approach taken below avoids that.
148 ;; Coping with strings of self-insert commands gets hairy when they interact
149 ;; with auto-filling. Most problems are eliminated by remembering what we're
150 ;; self-inserting, so we only need to get it from the undo information once.
152 ;; With Emacs 22.2 the variable `last-repeatable-command' stores the
153 ;; most recently executed command that was not bound to an input event.
154 ;; `repeat' now repeats that command instead of `real-last-command' to
155 ;; avoid a "... must be bound to an event with parameters" error.
157 (defvar repeat-last-self-insert nil
158 "If last repeated command was `self-insert-command', it inserted this.")
160 ;; That'll require another keystroke count so we know we're in a string of
161 ;; repetitions of self-insert commands:
163 (defvar repeat-num-input-keys-at-self-insert -1
164 "# key sequences read in Emacs session when `self-insert-command' repeated.")
166 ;;;;; *************** ANALOGOUS HACKS TO `repeat' ITSELF **************** ;;;;;
168 ;; That mechanism of checking num-input-keys to figure out what's really
169 ;; going on can be useful to other commands that need to fine-tune their
170 ;; interaction with repeat. Instead of requiring them to advise repeat, we
171 ;; can just defvar the value they need here, & setq it in the repeat command:
173 (defvar repeat-num-input-keys-at-repeat -1
174 "# key sequences read in Emacs session when `repeat' last invoked.")
176 ;; Also, we can assign a name to the test for which that variable is
177 ;; intended, which thereby documents here how to use it, & makes code that
178 ;; uses it self-documenting:
180 (defsubst repeat-is-really-this-command ()
181 "Return t if this command is happening because user invoked `repeat'.
182 Usually, when a command is executing, the Emacs builtin variable
183 `this-command' identifies the command the user invoked. Some commands modify
184 that variable on the theory they're doing more good than harm; `repeat' does
185 that, and usually does do more good than harm. However, like all do-gooders,
186 sometimes `repeat' gets surprising results from its altruism. The value of
187 this function is always whether the value of `this-command' would've been
188 'repeat if `repeat' hadn't modified it."
189 (= repeat-num-input-keys-at-repeat num-input-keys))
191 ;; An example of the use of (repeat-is-really-this-command) may still be
192 ;; available in <http://www.eskimo.com/~seldon/dotemacs.el>; search for
193 ;; "defun wm-switch-buffer".
195 ;;;;; ******************* THE REPEAT COMMAND ITSELF ******************* ;;;;;
197 (defvar repeat-previous-repeated-command nil
198 "The previous repeated command.")
200 ;; The following variable counts repeated self-insertions. The idea is
201 ;; that repeating a self-insertion command and subsequently undoing it
202 ;; should have almost the same effect as if the characters were inserted
203 ;; manually. The basic difference is that we leave in one undo-boundary
204 ;; between the original insertion and its first repetition.
205 (defvar repeat-undo-count nil
206 "Number of self-insertions since last `undo-boundary'.")
208 ;;;###autoload
209 (defun repeat (repeat-arg)
210 "Repeat most recently executed command.
211 With prefix arg, apply new prefix arg to that command; otherwise,
212 use the prefix arg that was used before (if any).
213 This command is like the `.' command in the vi editor.
215 If this command is invoked by a multi-character key sequence, it
216 can then be repeated by repeating the final character of that
217 sequence. This behavior can be modified by the global variable
218 `repeat-on-final-keystroke'.
220 `repeat' ignores commands bound to input events. Hence the term
221 \"most recently executed command\" shall be read as \"most
222 recently executed command not bound to an input event\"."
223 ;; The most recently executed command could be anything, so surprises could
224 ;; result if it were re-executed in a context where new dynamically
225 ;; localized variables were shadowing global variables in a `let' clause in
226 ;; here. (Remember that GNU Emacs 19 is dynamically localized.)
227 ;; To avoid that, I tried the `lexical-let' of the Common Lisp extensions,
228 ;; but that entails a very noticeable performance hit, so instead I use the
229 ;; "repeat-" prefix, reserved by this package, for *local* variables that
230 ;; might be visible to re-executed commands, including this function's arg.
231 (interactive "P")
232 (when (eq last-repeatable-command 'repeat)
233 (setq last-repeatable-command repeat-previous-repeated-command))
234 (cond
235 ((null last-repeatable-command)
236 (error "There is nothing to repeat"))
237 ((eq last-repeatable-command 'mode-exit)
238 (error "last-repeatable-command is mode-exit & can't be repeated"))
239 ((memq last-repeatable-command repeat-too-dangerous)
240 (error "Command %S too dangerous to repeat automatically"
241 last-repeatable-command)))
242 (setq this-command last-repeatable-command
243 repeat-previous-repeated-command last-repeatable-command
244 repeat-num-input-keys-at-repeat num-input-keys)
245 (when (null repeat-arg)
246 (setq repeat-arg last-prefix-arg))
247 ;; Now determine whether to loop on repeated taps of the final character
248 ;; of the key sequence that invoked repeat. The Emacs global
249 ;; last-command-event contains the final character now, but may not still
250 ;; contain it after the previous command is repeated, so the character
251 ;; needs to be saved.
252 (let ((repeat-repeat-char
253 (if (eq repeat-on-final-keystroke t)
254 last-command-event
255 ;; allow only specified final keystrokes
256 (car (memq last-command-event
257 (listify-key-sequence
258 repeat-on-final-keystroke))))))
259 (if (memq last-repeatable-command '(exit-minibuffer
260 minibuffer-complete-and-exit
261 self-insert-and-exit))
262 (let ((repeat-command (car command-history)))
263 (repeat-message "Repeating %S" repeat-command)
264 (eval repeat-command))
265 (if (null repeat-arg)
266 (repeat-message "Repeating command %S" last-repeatable-command)
267 (setq current-prefix-arg repeat-arg)
268 (repeat-message
269 "Repeating command %S %S" repeat-arg last-repeatable-command))
270 (if (eq last-repeatable-command 'self-insert-command)
271 (let ((insertion
272 (if (<= (- num-input-keys
273 repeat-num-input-keys-at-self-insert)
275 repeat-last-self-insert
276 (let ((range (nth 1 buffer-undo-list)))
277 (condition-case nil
278 (setq repeat-last-self-insert
279 (buffer-substring (car range)
280 (cdr range)))
281 (error (error "%s %s %s" ;Danger, Will Robinson!
282 "repeat can't intuit what you"
283 "inserted before auto-fill"
284 "clobbered it, sorry")))))))
285 (setq repeat-num-input-keys-at-self-insert num-input-keys)
286 ;; If the self-insert had a repeat count, INSERTION
287 ;; includes that many copies of the same character.
288 ;; So use just the first character
289 ;; and repeat it the right number of times.
290 (setq insertion (substring insertion -1))
291 (let ((count (prefix-numeric-value repeat-arg))
292 (i 0))
293 ;; Run pre- and post-command hooks for self-insertion too.
294 (run-hooks 'pre-command-hook)
295 (cond
296 ((not repeat-undo-count))
297 ((< repeat-undo-count 20)
298 ;; Don't make an undo-boundary here.
299 (setq repeat-undo-count (1+ repeat-undo-count)))
301 ;; Make an undo-boundary after 20 repetitions only.
302 (undo-boundary)
303 (setq repeat-undo-count 1)))
304 (while (< i count)
305 (repeat-self-insert insertion)
306 (setq i (1+ i)))
307 (run-hooks 'post-command-hook)))
308 (let ((indirect (indirect-function last-repeatable-command)))
309 ;; Make each repetition undo separately.
310 (undo-boundary)
311 (if (or (stringp indirect)
312 (vectorp indirect))
313 ;; Bind real-last-command so that executing the macro does
314 ;; not alter it. Do the same for last-repeatable-command.
315 (let ((real-last-command real-last-command)
316 (last-repeatable-command last-repeatable-command))
317 (execute-kbd-macro last-repeatable-command))
318 (run-hooks 'pre-command-hook)
319 (call-interactively last-repeatable-command)
320 (run-hooks 'post-command-hook)))))
321 (when repeat-repeat-char
322 ;; A simple recursion here gets into trouble with max-lisp-eval-depth
323 ;; on long sequences of repetitions of a command like `forward-word'
324 ;; (only 32 repetitions are possible given the default value of 200 for
325 ;; max-lisp-eval-depth), but if I now locally disable the repeat char I
326 ;; can iterate indefinitely here around a single level of recursion.
327 (let (repeat-on-final-keystroke
328 ;; Bind `undo-inhibit-record-point' to t in order to avoid
329 ;; recording point in `buffer-undo-list' here. We have to
330 ;; do this since the command loop does not set the last
331 ;; position of point thus confusing the point recording
332 ;; mechanism when inserting or deleting text.
333 (undo-inhibit-record-point t))
334 (setq real-last-command 'repeat)
335 (setq repeat-undo-count 1)
336 (unwind-protect
337 (while (let ((evt (read-key)))
338 ;; For clicks, we need to strip the meta-data to
339 ;; check the underlying event name.
340 (eq (or (car-safe evt) evt)
341 (or (car-safe repeat-repeat-char)
342 repeat-repeat-char)))
343 (repeat repeat-arg))
344 ;; Make sure `repeat-undo-count' is reset.
345 (setq repeat-undo-count nil))
346 (setq unread-command-events (list last-input-event))))))
348 (defun repeat-self-insert (string)
349 (let ((i 0))
350 (while (< i (length string))
351 (let ((last-command-event (aref string i)))
352 (self-insert-command 1))
353 (setq i (1+ i)))))
355 (defun repeat-message (format &rest args)
356 "Like `message' but displays with `repeat-message-function' if non-nil."
357 (let ((message (apply 'format format args)))
358 (if repeat-message-function
359 (funcall repeat-message-function message)
360 (message "%s" message))))
362 ;; OK, there's one situation left where that doesn't work correctly: when the
363 ;; most recent self-insertion provoked an auto-fill. The problem is that
364 ;; unravelling the undo information after an auto-fill is too hard, since all
365 ;; kinds of stuff can get in there as a result of comment prefixes etc. It'd
366 ;; be possible to advise do-auto-fill to record the most recent
367 ;; self-insertion before it does its thing, but that's a performance hit on
368 ;; auto-fill, which already has performance problems; so it's better to just
369 ;; leave it like this. If text didn't provoke an auto-fill when the user
370 ;; typed it, this'll correctly repeat its self-insertion, even if the
371 ;; repetition does cause auto-fill.
373 ;; If you wanted perfection, probably it'd be necessary to hack do-auto-fill
374 ;; into 2 functions, maybe-do-auto-fill & really-do-auto-fill, because only
375 ;; really-do-auto-fill should be advised. As things are, either the undo
376 ;; information would need to be scanned on every do-auto-fill invocation, or
377 ;; the code at the top of do-auto-fill deciding whether filling is necessary
378 ;; would need to be duplicated in the advice, wasting execution time when
379 ;; filling does turn out to be necessary.
381 ;; I thought maybe this story had a moral, something about functional
382 ;; decomposition; but now I'm not even sure of that, since a function
383 ;; call per se is a performance hit, & even the code that would
384 ;; correspond to really-do-auto-fill has performance problems that
385 ;; can make it necessary to stop typing while Emacs catches up.
386 ;; Maybe the real moral is that perfection is a chimera.
388 ;; Ah, hell, it's all going to fall into a black hole someday anyway.
390 ;;;;; ************************* EMACS CONTROL ************************* ;;;;;
392 (provide 'repeat)
394 ;;; repeat.el ends here