]> code.delx.au - gnu-emacs/blob - lisp/repeat.el
Update copyright notice.
[gnu-emacs] / lisp / repeat.el
1 ;;; vi-dot.el --- convenient way to repeat the previous command
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
8 ;; Keywords: convenience, abbrev, vi, universal argument, typematic, repeat
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,
41 ;; whatever that was. The command is called `vi-dot' because the vi editor,
42 ;; Emacs's arch-rival among the Great Unwashed, does that when "." is pressed
43 ;; in its command mode.
44
45 ;; Starting with Emacs 20.3, this package is part of Emacs, and the
46 ;; `vi-dot' command is bound to the key sequence C-x z. (You can actually
47 ;; keep repeating the most recent command by just repeating the z after the
48 ;; first C-x z.) However, you can use this package with older versions of
49 ;; Emacs. Make the binding with
50 ;; (require 'vi-dot)
51 ;; (global-set-key "\C-xz" 'vi-dot)
52 ;; in your .emacs to give the command its orthodox binding of C-x z.
53
54 ;; Since the whole point of vi-dot is to let you repeat commands that are
55 ;; bound to multiple keystrokes by leaning on a *single* key, it seems not to
56 ;; make sense to bind vi-dot itself to a multiple-character key sequence, but
57 ;; there aren't any appropriate single characters left in the orthodox global
58 ;; map. (Meta characters don't count because they require two keystrokes if
59 ;; you don't have a real meta key, and things like function keys can't be
60 ;; relied on to be available to all users. We considered rebinding C-z,
61 ;; since C-x C-z is also bound to the same command, but RMS decided too many
62 ;; users were accustomed to the orthodox meaning of C-z.) So the vi-dot
63 ;; command checks what key sequence it was invoked by, and allows you to
64 ;; repeat the final key in that sequence to keep repeating the command.
65 ;; For example, C-x ] C-x z z z will move forward 4 pages.
66
67 ;; This works correctly inside a keyboard macro as far as recording and
68 ;; playback go, but `edit-kbd-macro' gets it wrong. That shouldn't really
69 ;; matter; if you need to edit something like
70 ;; C-x ] ;; forward-page
71 ;; C-x z ;; vi-dot
72 ;; zz ;; self-insert-command * 2
73 ;; C-x ;; Control-X-prefix
74 ;; you can just kill the bogus final 2 lines, then duplicate the vi-dot line
75 ;; as many times as it's really needed. Also, `edit-kbd-macro' works
76 ;; correctly if `vi-dot' is invoked through a rebinding to a single keystroke
77 ;; and the global variable vi-dot-repeat-on-final-keystroke is set to a value
78 ;; that doesn't include that keystroke. For example, the lines
79 ;; (global-set-key "\C-z" 'vi-dot)
80 ;; (setq vi-dot-repeat-on-final-keystroke "z")
81 ;; in your .emacs would allow `edit-kbd-macro' to work correctly when C-z was
82 ;; used in a keyboard macro to invoke `vi-dot', but would still allow C-x z
83 ;; to be used for `vi-dot' elsewhere. The real reason for documenting this
84 ;; isn't that anybody would need it for the `edit-kbd-macro' problem, but
85 ;; that there might be other unexpected ramifications of re-executing on
86 ;; repetitions of the final keystroke, and this shows how to do workarounds.
87
88 ;; If the preceding command had a prefix argument, that argument is applied
89 ;; to the vi-dot command, unless the vi-dot command is given a new prefix
90 ;; argument, in which case it applies that new prefix argument to the
91 ;; preceding command. This means a key sequence like C-u - C-x C-t can be
92 ;; repeated. (It shoves the preceding line upward in the buffer.)
93
94 ;; Here are some other key sequences with which vi-dot might be useful:
95 ;; C-u - C-t [shove preceding character backward in line]
96 ;; C-u - M-t [shove preceding word backward in sentence]
97 ;; C-x ^ enlarge-window [one line] (assuming frame has > 1 window)
98 ;; C-u - C-x ^ [shrink window one line]
99 ;; C-x ` next-error
100 ;; C-u - C-x ` [previous error]
101 ;; C-x DEL backward-kill-sentence
102 ;; C-x e call-last-kbd-macro
103 ;; C-x r i insert-register
104 ;; C-x r t string-rectangle
105 ;; C-x TAB indent-rigidly [one character]
106 ;; C-u - C-x TAB [outdent rigidly one character]
107 ;; C-x { shrink-window-horizontally
108 ;; C-x } enlarge-window-horizontally
109
110 ;; Using vi-dot.el doesn't entail a performance hit. There's a
111 ;; straightforward way to implement a package like this that would save some
112 ;; data about each command as it was executed, but that Lisp would need to be
113 ;; interpreted on every keystroke, which is Bad. This implementation doesn't
114 ;; do it that way; the peformance impact on almost all keystrokes is 0.
115
116 ;; Buried in the implementation is a reference to a function in my
117 ;; typematic.el package, which isn't part of GNU Emacs. However, that
118 ;; package is *not* required by vi-dot; the reference allows it to be used,
119 ;; but doesn't require it.
120
121 ;;; Code:
122
123 (eval-when-compile (require 'cl))
124
125 ;;;;; ************************* USER OPTIONS ************************** ;;;;;
126
127 (defcustom vi-dot-too-dangerous '(kill-this-buffer)
128 "Commands too dangerous to repeat with `vi-dot'."
129 :group 'convenience
130 :type '(repeat function))
131
132 ;; If the last command was self-insert-command, the char to be inserted was
133 ;; obtained by that command from last-command-char, which has now been
134 ;; clobbered by the command sequence that invoked vi-dot. We could get it
135 ;; from (recent-keys) & set last-command-char to that, "unclobbering" it, but
136 ;; this has the disadvantage that if the user types a sequence of different
137 ;; chars then invokes vi-dot, only the final char will be inserted. In vi,
138 ;; the dot command can reinsert the entire most-recently-inserted sequence.
139
140 (defvar vi-dot-message-function nil
141 "If non-nil, function used by `vi-dot' command to say what it's doing.
142 Message is something like \"Repeating command glorp\".
143 To disable such messages, set this variable to `ignore'. To customize
144 display, assign a function that takes one string as an arg and displays
145 it however you want.")
146
147 (defcustom vi-dot-repeat-on-final-keystroke t
148 "Allow `vi-dot' to re-execute for repeating lastchar of a key sequence.
149 If this variable is t, `vi-dot' determines what key sequence
150 it was invoked by, extracts the final character of that sequence, and
151 re-executes as many times as that final character is hit; so for example
152 if `vi-dot' is bound to C-x z, typing C-x z z z repeats the previous command
153 3 times. If this variable is a sequence of characters, then re-execution
154 only occurs if the final character by which `vi-dot' was invoked is a
155 member of that sequence. If this variable is nil, no re-execution occurs."
156 :group 'convenience
157 :type 'boolean)
158
159 ;;;;; ****************** HACKS TO THE REST OF EMACS ******************* ;;;;;
160
161 ;; The basic strategy is to use last-command, a variable built in to Emacs.
162 ;; There are 2 issues that complicate this strategy. The first is that
163 ;; last-command is given a bogus value when any kill command is executed;
164 ;; this is done to make it easy for 'yank-pop to know that it's being invoked
165 ;; after a kill command. The second is that the meaning of the command is
166 ;; often altered by the prefix arg, but although Emacs (GNU 19.34) has a
167 ;; builtin prefix-arg specifying the arg for the next command, as well as a
168 ;; builtin current-prefix-arg, it has no builtin last-prefix-arg.
169
170 ;; There's a builtin (this-command-keys), the return value of which could be
171 ;; executed with (command-execute), but there's no (last-command-keys).
172 ;; Using (last-command-keys) if it existed wouldn't be optimal, however,
173 ;; since it would complicate checking membership in vi-dot-too-dangerous.
174
175 ;; It would of course be trivial to implement last-prefix-arg &
176 ;; true-last-command by putting something in post-command-hook, but that
177 ;; entails a performance hit; the approach taken below avoids that.
178
179 ;; First cope with (kill-region). It's straightforward to advise it to save
180 ;; the true value of this-command before clobbering it.
181
182 (require 'advice)
183
184 (defvar vi-dot-last-kill-command nil
185 "True value of `this-command' before (`kill-region') clobbered it.")
186
187 (defadvice kill-region (before vi-dot-save-last-kill-command act)
188 "Remember true value of this-command before (`kill-region') clobbers it."
189 (setq vi-dot-last-kill-command this-command))
190
191 ;; Next cope with the prefix arg. I can advise the various functions that
192 ;; create prefix args to save the arg in a variable ...
193
194 (defvar vi-dot-prefix-arg nil
195 "Prefix arg created as most recent universal argument.")
196
197 ;; ... but alone that's not enough, because if last-command's prefix arg was
198 ;; nil, none of those functions were ever called, so whatever command before
199 ;; last-command did have a prefix arg has left it in vi-dot-prefix-arg, & I
200 ;; need a way to tell whether whatever's in there applies to last-command.
201
202 ;; From Info|ELisp|Command Loop|Reading Input|Key Sequence Input:
203 ;; - Variable: num-input-keys
204 ;; This variable's value is the number of key sequences processed so far
205 ;; in this Emacs session. This includes key sequences read from the
206 ;; terminal and key sequences read from keyboard macros being executed.
207 ;; num-input-keys counts key *sequences*, not key *strokes*; it's only
208 ;; incremented after reading a complete key sequence mapping to a command.
209
210 (defvar vi-dot-num-input-keys-at-prefix -1
211 "# of key sequences read in Emacs session when prefix-arg defined.")
212
213 (mapcar (lambda (f)
214 (eval
215 `(defadvice ,f (after vi-dot-save-universal-arg act)
216 (setq vi-dot-prefix-arg current-prefix-arg
217 vi-dot-num-input-keys-at-prefix num-input-keys))))
218 [universal-argument-more
219 universal-argument-other-key
220 typematic-universal-argument-more-or-less])
221
222 ;; Coping with strings of self-insert commands gets hairy when they interact
223 ;; with auto-filling. Most problems are eliminated by remembering what we're
224 ;; self-inserting, so we only need to get it from the undo information once.
225
226 (defvar vi-dot-last-self-insert nil
227 "If last repeated command was `self-insert-command', it inserted this.")
228
229 ;; That'll require another keystroke count so we know we're in a string of
230 ;; repetitions of self-insert commands:
231
232 (defvar vi-dot-num-input-keys-at-self-insert -1
233 "# key sequences read in Emacs session when `self-insert-command' repeated.")
234
235 ;;;;; *************** ANALOGOUS HACKS TO VI-DOT ITSELF **************** ;;;;;
236
237 ;; That mechanism of checking num-input-keys to figure out what's really
238 ;; going on can be useful to other commands that need to fine-tune their
239 ;; interaction with vi-dot. Instead of requiring them to advise vi-dot, we
240 ;; can just defvar the value they need here, & setq it in the vi-dot command:
241
242 (defvar vi-dot-num-input-keys-at-vi-dot -1
243 "# key sequences read in Emacs session when `vi-dot' last invoked.")
244
245 ;; Also, we can assign a name to the test for which that variable is
246 ;; intended, which thereby documents here how to use it, & makes code that
247 ;; uses it self-documenting:
248
249 (defsubst vi-dot-is-really-this-command ()
250 "Return t if this command is happening because user invoked `vi-dot'.
251 Usually, when a command is executing, the Emacs builtin variable
252 `this-command' identifies the command the user invoked. Some commands modify
253 that variable on the theory they're doing more good than harm; `vi-dot' does
254 that, and usually does do more good than harm. However, like all do-gooders,
255 sometimes `vi-dot' gets surprising results from its altruism. The value of
256 this function is always whether the value of `this-command' would've been
257 'vi-dot if `vi-dot' hadn't modified it."
258 (= vi-dot-num-input-keys-at-vi-dot num-input-keys))
259
260 ;; An example of the use of (vi-dot-is-really-this-command) may still be
261 ;; available in <http://www.eskimo.com/~seldon/dotemacs.el>; search for
262 ;; "defun wm-switch-buffer".
263
264 ;;;;; ******************* THE VI-DOT COMMAND ITSELF ******************* ;;;;;
265
266 ;;;###autoload
267 (defun vi-dot (vi-dot-arg)
268 "Repeat most recently executed command.
269 With prefix arg, apply new prefix arg to that command; otherwise, maintain
270 prefix arg of most recently executed command if it had one.
271 This command is named after the `.' command in the vi editor.
272
273 If this command is invoked by a multi-character key sequence, it can then
274 be repeated by repeating the final character of that sequence. This behavior
275 can be modified by the global variable `vi-dot-repeat-on-final-keystroke'."
276 ;; The most recently executed command could be anything, so surprises could
277 ;; result if it were re-executed in a context where new dynamically
278 ;; localized variables were shadowing global variables in a `let' clause in
279 ;; here. (Remember that GNU Emacs 19 is dynamically localized.)
280 ;; To avoid that, I tried the `lexical-let' of the Common Lisp extensions,
281 ;; but that entails a very noticeable performance hit, so instead I use the
282 ;; "vi-dot-" prefix, reserved by this package, for *local* variables that
283 ;; might be visible to re-executed commands, including this function's arg.
284 (interactive "P")
285 (when (eq last-command 'kill-region)
286 (setq last-command vi-dot-last-kill-command))
287 (setq this-command last-command
288 vi-dot-num-input-keys-at-vi-dot num-input-keys)
289 (when (eq last-command 'mode-exit)
290 (error "last-command is mode-exit & can't be repeated"))
291 (when (memq last-command vi-dot-too-dangerous)
292 (error "Command %S too dangerous to repeat automatically" last-command))
293 (when (and (null vi-dot-arg)
294 (<= (- num-input-keys vi-dot-num-input-keys-at-prefix) 2))
295 (setq vi-dot-arg vi-dot-prefix-arg))
296 ;; Now determine whether to loop on repeated taps of the final character
297 ;; of the key sequence that invoked vi-dot. The Emacs global
298 ;; last-command-char contains the final character now, but may not still
299 ;; contain it after the previous command is repeated, so the character
300 ;; needs to be saved.
301 (let ((vi-dot-repeat-char
302 (if (eq vi-dot-repeat-on-final-keystroke t)
303 ;; allow any final input event that was a character
304 (when (eq last-command-char
305 last-command-event)
306 last-command-char)
307 ;; allow only specified final keystrokes
308 (car (memq last-command-char
309 (listify-key-sequence
310 vi-dot-repeat-on-final-keystroke))))))
311 (if (memq last-command '(exit-minibuffer
312 minibuffer-complete-and-exit
313 self-insert-and-exit))
314 (let ((vi-dot-command (car command-history)))
315 (vi-dot-message "Repeating %S" vi-dot-command)
316 (eval vi-dot-command))
317 (if (null vi-dot-arg)
318 (vi-dot-message "Repeating command %S" last-command)
319 (setq vi-dot-num-input-keys-at-prefix num-input-keys
320 current-prefix-arg vi-dot-arg)
321 (vi-dot-message "Repeating command %S %S" vi-dot-arg last-command))
322 (if (eq last-command 'self-insert-command)
323 (let ((insertion
324 (if (<= (- num-input-keys
325 vi-dot-num-input-keys-at-self-insert)
326 1)
327 vi-dot-last-self-insert
328 (let ((range (nth 1 buffer-undo-list)))
329 (condition-case nil
330 (setq vi-dot-last-self-insert
331 (buffer-substring (car range)
332 (cdr range)))
333 (error (error "%s %s %s" ;Danger, Will Robinson!
334 "vi-dot can't intuit what you"
335 "inserted before auto-fill"
336 "clobbered it, sorry")))))))
337 (setq vi-dot-num-input-keys-at-self-insert num-input-keys)
338 (loop repeat (prefix-numeric-value vi-dot-arg) do
339 (vi-self-insert insertion)))
340 (call-interactively last-command)))
341 (when vi-dot-repeat-char
342 ;; A simple recursion here gets into trouble with max-lisp-eval-depth
343 ;; on long sequences of repetitions of a command like `forward-word'
344 ;; (only 32 repetitions are possible given the default value of 200 for
345 ;; max-lisp-eval-depth), but if I now locally disable the repeat char I
346 ;; can iterate indefinitely here around a single level of recursion.
347 (let (vi-dot-repeat-on-final-keystroke)
348 (while (eq (read-event) vi-dot-repeat-char)
349 (vi-dot vi-dot-arg))
350 (setq unread-command-events (list last-input-event))))))
351
352 (defun vi-self-insert (string)
353 (let ((i 0))
354 (while (< i (length string))
355 (let ((last-command-char (aref string i)))
356 (self-insert-command 1))
357 (setq i (1+ i)))))
358
359 (defun vi-dot-message (format &rest args)
360 "Like `message' but displays with `vi-dot-message-function' if non-nil."
361 (let ((message (apply 'format format args)))
362 (if vi-dot-message-function
363 (funcall vi-dot-message-function message)
364 (message "%s" message))))
365
366 ;; OK, there's one situation left where that doesn't work correctly: when the
367 ;; most recent self-insertion provoked an auto-fill. The problem is that
368 ;; unravelling the undo information after an auto-fill is too hard, since all
369 ;; kinds of stuff can get in there as a result of comment prefixes etc. It'd
370 ;; be possible to advise do-auto-fill to record the most recent
371 ;; self-insertion before it does its thing, but that's a performance hit on
372 ;; auto-fill, which already has performance problems; so it's better to just
373 ;; leave it like this. If text didn't provoke an auto-fill when the user
374 ;; typed it, this'll correctly repeat its self-insertion, even if the
375 ;; repetition does cause auto-fill.
376
377 ;; If you wanted perfection, probably it'd be necessary to hack do-auto-fill
378 ;; into 2 functions, maybe-do-auto-fill & really-do-auto-fill, because only
379 ;; really-do-auto-fill should be advised. As things are, either the undo
380 ;; information would need to be scanned on every do-auto-fill invocation, or
381 ;; the code at the top of do-auto-fill deciding whether filling is necessary
382 ;; would need to be duplicated in the advice, wasting execution time when
383 ;; filling does turn out to be necessary.
384
385 ;; I thought maybe this story had a moral, something about functional
386 ;; decomposition; but now I'm not even sure of that, since a function
387 ;; call per se is a performance hit, & even the code that would
388 ;; correspond to really-do-auto-fill has performance problems that
389 ;; can make it necessary to stop typing while Emacs catches up.
390 ;; Maybe the real moral is that perfection is a chimera.
391
392 ;; Ah, hell, it's all going to fall into a black hole someday anyway.
393
394 ;;;;; ************************* EMACS CONTROL ************************* ;;;;;
395
396 (provide 'vi-dot)
397
398 ;;; vi-dot.el ends here