Mercurial > emacs
annotate lisp/repeat.el @ 92676:6984f0acb7d8
*** empty log message ***
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Sun, 09 Mar 2008 03:42:20 +0000 |
parents | b151c81bfd6e |
children | c70e45a7acfd 1e3a407766b9 |
rev | line source |
---|---|
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
1 ;;; repeat.el --- convenient way to repeat the previous command |
22131 | 2 |
74442 | 3 ;; Copyright (C) 1998, 2001, 2002, 2003, 2004, 2005, |
79721 | 4 ;; 2006, 2007, 2008 Free Software Foundation, Inc. |
22131 | 5 |
6 ;; Author: Will Mengarini <seldon@eskimo.com> | |
7 ;; Created: Mo 02 Mar 98 | |
8 ;; Version: 0.51, We 13 May 98 | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
9 ;; Keywords: convenience, vi, repeat |
22131 | 10 |
11 ;; This file is part of GNU Emacs. | |
12 | |
38401 | 13 ;; GNU Emacs is free software; you can redistribute it and/or modify |
22131 | 14 ;; it under the terms of the GNU General Public License as published by |
78236
9355f9b7bbff
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
75347
diff
changeset
|
15 ;; the Free Software Foundation; either version 3, or (at your option) |
22131 | 16 ;; any later version. |
17 | |
38401 | 18 ;; GNU Emacs is distributed in the hope that it will be useful, |
22131 | 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. | |
22 | |
23 ;; You should have received a copy of the GNU General Public License | |
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
64091 | 25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
26 ;; Boston, MA 02110-1301, USA. | |
22131 | 27 |
28 ;;; Commentary: | |
29 | |
30 ;; Sometimes the fastest way to get something done is just to lean on a key; | |
31 ;; moving forward through a series of words by leaning on M-f is an example. | |
32 ;; But 'forward-page is orthodoxily bound to C-x ], so moving forward through | |
33 ;; several pages requires | |
34 ;; Loop until desired page is reached: | |
35 ;; Hold down control key with left pinkie. | |
36 ;; Tap <x>. | |
37 ;; Lift left pinkie off control key. | |
38 ;; Tap <]>. | |
39 ;; This is a pain in the ass. | |
40 | |
41 ;; This package defines a command that repeats the preceding command, | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
42 ;; whatever that was, including its arguments, whatever they were. |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
43 ;; This command is connected to the key C-x z. |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
44 ;; To repeat the previous command once, type C-x z. |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
45 ;; To repeat it a second time immediately after, type just z. |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
46 ;; By typing z again and again, you can repeat the command over and over. |
22131 | 47 |
48 ;; This works correctly inside a keyboard macro as far as recording and | |
49 ;; playback go, but `edit-kbd-macro' gets it wrong. That shouldn't really | |
50 ;; matter; if you need to edit something like | |
51 ;; C-x ] ;; forward-page | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
52 ;; C-x z ;; repeat |
22131 | 53 ;; zz ;; self-insert-command * 2 |
54 ;; C-x ;; Control-X-prefix | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
55 ;; you can just kill the bogus final 2 lines, then duplicate the repeat line |
22131 | 56 ;; as many times as it's really needed. Also, `edit-kbd-macro' works |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
57 ;; correctly if `repeat' is invoked through a rebinding to a single keystroke |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
58 ;; and the global variable repeat-on-final-keystroke is set to a value |
22131 | 59 ;; that doesn't include that keystroke. For example, the lines |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
60 ;; (global-set-key "\C-z" 'repeat) |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
61 ;; (setq repeat-on-final-keystroke "z") |
22131 | 62 ;; in your .emacs would allow `edit-kbd-macro' to work correctly when C-z was |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
63 ;; used in a keyboard macro to invoke `repeat', but would still allow C-x z |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
64 ;; to be used for `repeat' elsewhere. The real reason for documenting this |
22131 | 65 ;; isn't that anybody would need it for the `edit-kbd-macro' problem, but |
66 ;; that there might be other unexpected ramifications of re-executing on | |
67 ;; repetitions of the final keystroke, and this shows how to do workarounds. | |
68 | |
69 ;; If the preceding command had a prefix argument, that argument is applied | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
70 ;; to the repeat command, unless the repeat command is given a new prefix |
22131 | 71 ;; argument, in which case it applies that new prefix argument to the |
72 ;; preceding command. This means a key sequence like C-u - C-x C-t can be | |
73 ;; repeated. (It shoves the preceding line upward in the buffer.) | |
74 | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
75 ;; Here are some other key sequences with which repeat might be useful: |
22131 | 76 ;; C-u - C-t [shove preceding character backward in line] |
77 ;; C-u - M-t [shove preceding word backward in sentence] | |
78 ;; C-x ^ enlarge-window [one line] (assuming frame has > 1 window) | |
79 ;; C-u - C-x ^ [shrink window one line] | |
80 ;; C-x ` next-error | |
81 ;; C-u - C-x ` [previous error] | |
82 ;; C-x DEL backward-kill-sentence | |
83 ;; C-x e call-last-kbd-macro | |
84 ;; C-x r i insert-register | |
85 ;; C-x r t string-rectangle | |
86 ;; C-x TAB indent-rigidly [one character] | |
87 ;; C-u - C-x TAB [outdent rigidly one character] | |
88 ;; C-x { shrink-window-horizontally | |
89 ;; C-x } enlarge-window-horizontally | |
90 | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
91 ;; This command was first called `vi-dot', because |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
92 ;; it was inspired by the `.' command in the vi editor, |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
93 ;; but it was renamed to make its name more meaningful. |
22131 | 94 |
95 ;;; Code: | |
96 | |
97 ;;;;; ************************* USER OPTIONS ************************** ;;;;; | |
98 | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
99 (defcustom repeat-too-dangerous '(kill-this-buffer) |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
100 "Commands too dangerous to repeat with \\[repeat]." |
22132 | 101 :group 'convenience |
102 :type '(repeat function)) | |
22131 | 103 |
104 ;; If the last command was self-insert-command, the char to be inserted was | |
105 ;; obtained by that command from last-command-char, which has now been | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
106 ;; clobbered by the command sequence that invoked `repeat'. We could get it |
22131 | 107 ;; from (recent-keys) & set last-command-char to that, "unclobbering" it, but |
108 ;; this has the disadvantage that if the user types a sequence of different | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
109 ;; chars then invokes repeat, only the final char will be inserted. In vi, |
22131 | 110 ;; the dot command can reinsert the entire most-recently-inserted sequence. |
111 | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
112 (defvar repeat-message-function nil |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
113 "If non-nil, function used by `repeat' command to say what it's doing. |
22131 | 114 Message is something like \"Repeating command glorp\". |
22132 | 115 To disable such messages, set this variable to `ignore'. To customize |
22131 | 116 display, assign a function that takes one string as an arg and displays |
117 it however you want.") | |
118 | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
119 (defcustom repeat-on-final-keystroke t |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
120 "Allow `repeat' to re-execute for repeating lastchar of a key sequence. |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
121 If this variable is t, `repeat' determines what key sequence |
22131 | 122 it was invoked by, extracts the final character of that sequence, and |
123 re-executes as many times as that final character is hit; so for example | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
124 if `repeat' is bound to C-x z, typing C-x z z z repeats the previous command |
22131 | 125 3 times. If this variable is a sequence of characters, then re-execution |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
126 only occurs if the final character by which `repeat' was invoked is a |
22132 | 127 member of that sequence. If this variable is nil, no re-execution occurs." |
128 :group 'convenience | |
129 :type 'boolean) | |
49597
e88404e8f2cf
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
38401
diff
changeset
|
130 |
22131 | 131 ;;;;; ****************** HACKS TO THE REST OF EMACS ******************* ;;;;; |
132 | |
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; | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
136 ;; this is done to make it easy for `yank-pop' to know that it's being invoked |
22131 | 137 ;; after a kill command. The second is that the meaning of the command is |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
138 ;; often altered by the prefix arg, but although Emacs (19.34) has a |
22131 | 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. | |
141 | |
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, | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
145 ;; since it would complicate checking membership in repeat-too-dangerous. |
22131 | 146 |
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. | |
150 | |
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. | |
154 | |
78698
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
155 ;; With Emacs 22.2 the variable `last-repeatable-command' stores the |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
156 ;; most recently executed command that was not bound to an input event. |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
157 ;; `repeat' now repeats that command instead of `real-last-command' to |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
158 ;; avoid a "... must be bound to an event with parameters" error. |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
159 |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
160 (defvar repeat-last-self-insert nil |
22131 | 161 "If last repeated command was `self-insert-command', it inserted this.") |
162 | |
163 ;; That'll require another keystroke count so we know we're in a string of | |
164 ;; repetitions of self-insert commands: | |
165 | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
166 (defvar repeat-num-input-keys-at-self-insert -1 |
22131 | 167 "# key sequences read in Emacs session when `self-insert-command' repeated.") |
168 | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
169 ;;;;; *************** ANALOGOUS HACKS TO `repeat' ITSELF **************** ;;;;; |
22131 | 170 |
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 | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
173 ;; interaction with repeat. Instead of requiring them to advise repeat, we |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
174 ;; can just defvar the value they need here, & setq it in the repeat command: |
22131 | 175 |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
176 (defvar repeat-num-input-keys-at-repeat -1 |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
177 "# key sequences read in Emacs session when `repeat' last invoked.") |
22131 | 178 |
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: | |
182 | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
183 (defsubst repeat-is-really-this-command () |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
184 "Return t if this command is happening because user invoked `repeat'. |
22131 | 185 Usually, when a command is executing, the Emacs builtin variable |
186 `this-command' identifies the command the user invoked. Some commands modify | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
187 that variable on the theory they're doing more good than harm; `repeat' does |
22131 | 188 that, and usually does do more good than harm. However, like all do-gooders, |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
189 sometimes `repeat' gets surprising results from its altruism. The value of |
22131 | 190 this function is always whether the value of `this-command' would've been |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
191 'repeat if `repeat' hadn't modified it." |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
192 (= repeat-num-input-keys-at-repeat num-input-keys)) |
22131 | 193 |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
194 ;; An example of the use of (repeat-is-really-this-command) may still be |
22131 | 195 ;; available in <http://www.eskimo.com/~seldon/dotemacs.el>; search for |
196 ;; "defun wm-switch-buffer". | |
197 | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
198 ;;;;; ******************* THE REPEAT COMMAND ITSELF ******************* ;;;;; |
22131 | 199 |
23098 | 200 (defvar repeat-previous-repeated-command nil |
201 "The previous repeated command.") | |
202 | |
87862
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
203 ;; The following variable counts repeated self-insertions. The idea is |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
204 ;; that repeating a self-insertion command and subsequently undoing it |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
205 ;; should have almost the same effect as if the characters were inserted |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
206 ;; manually. The basic difference is that we leave in one undo-boundary |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
207 ;; between the original insertion and its first repetition. |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
208 (defvar repeat-undo-count nil |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
209 "Number of self-insertions since last `undo-boundary'.") |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
210 |
22131 | 211 ;;;###autoload |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
212 (defun repeat (repeat-arg) |
22131 | 213 "Repeat most recently executed command. |
78698
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
214 With prefix arg, apply new prefix arg to that command; otherwise, |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
215 use the prefix arg that was used before (if any). |
23098 | 216 This command is like the `.' command in the vi editor. |
22131 | 217 |
78698
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
218 If this command is invoked by a multi-character key sequence, it |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
219 can then be repeated by repeating the final character of that |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
220 sequence. This behavior can be modified by the global variable |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
221 `repeat-on-final-keystroke'. |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
222 |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
223 `repeat' ignores commands bound to input events. Hence the term |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
224 \"most recently executed command\" shall be read as \"most |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
225 recently executed command not bound to an input event\"." |
22131 | 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 | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
232 ;; "repeat-" prefix, reserved by this package, for *local* variables that |
22131 | 233 ;; might be visible to re-executed commands, including this function's arg. |
234 (interactive "P") | |
78698
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
235 (when (eq last-repeatable-command 'repeat) |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
236 (setq last-repeatable-command repeat-previous-repeated-command)) |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
237 (cond |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
238 ((null last-repeatable-command) |
23098 | 239 (error "There is nothing to repeat")) |
78698
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
240 ((eq last-repeatable-command 'mode-exit) |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
241 (error "last-repeatable-command is mode-exit & can't be repeated")) |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
242 ((memq last-repeatable-command repeat-too-dangerous) |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
243 (error "Command %S too dangerous to repeat automatically" |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
244 last-repeatable-command))) |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
245 (setq this-command last-repeatable-command |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
246 repeat-previous-repeated-command last-repeatable-command |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
247 repeat-num-input-keys-at-repeat num-input-keys) |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
248 (when (null repeat-arg) |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
249 (setq repeat-arg last-prefix-arg)) |
22131 | 250 ;; Now determine whether to loop on repeated taps of the final character |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
251 ;; of the key sequence that invoked repeat. The Emacs global |
22131 | 252 ;; last-command-char 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. | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
255 (let ((repeat-repeat-char |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
256 (if (eq repeat-on-final-keystroke t) |
78698
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
257 last-command-char |
22131 | 258 ;; allow only specified final keystrokes |
259 (car (memq last-command-char | |
260 (listify-key-sequence | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
261 repeat-on-final-keystroke)))))) |
78698
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
262 (if (memq last-repeatable-command '(exit-minibuffer |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
263 minibuffer-complete-and-exit |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
264 self-insert-and-exit)) |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
265 (let ((repeat-command (car command-history))) |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
266 (repeat-message "Repeating %S" repeat-command) |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
267 (eval repeat-command)) |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
268 (if (null repeat-arg) |
78698
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
269 (repeat-message "Repeating command %S" last-repeatable-command) |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
270 (setq current-prefix-arg repeat-arg) |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
271 (repeat-message |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
272 "Repeating command %S %S" repeat-arg last-repeatable-command)) |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
273 (if (eq last-repeatable-command 'self-insert-command) |
22131 | 274 (let ((insertion |
275 (if (<= (- num-input-keys | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
276 repeat-num-input-keys-at-self-insert) |
22131 | 277 1) |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
278 repeat-last-self-insert |
22131 | 279 (let ((range (nth 1 buffer-undo-list))) |
280 (condition-case nil | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
281 (setq repeat-last-self-insert |
22131 | 282 (buffer-substring (car range) |
283 (cdr range))) | |
49597
e88404e8f2cf
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
38401
diff
changeset
|
284 (error (error "%s %s %s" ;Danger, Will Robinson! |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
285 "repeat can't intuit what you" |
22131 | 286 "inserted before auto-fill" |
287 "clobbered it, sorry"))))))) | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
288 (setq repeat-num-input-keys-at-self-insert num-input-keys) |
23474
242836a572c4
(repeat): Handle keyboard macros properly.
Richard M. Stallman <rms@gnu.org>
parents:
23109
diff
changeset
|
289 ;; If the self-insert had a repeat count, INSERTION |
242836a572c4
(repeat): Handle keyboard macros properly.
Richard M. Stallman <rms@gnu.org>
parents:
23109
diff
changeset
|
290 ;; includes that many copies of the same character. |
242836a572c4
(repeat): Handle keyboard macros properly.
Richard M. Stallman <rms@gnu.org>
parents:
23109
diff
changeset
|
291 ;; So use just the first character |
242836a572c4
(repeat): Handle keyboard macros properly.
Richard M. Stallman <rms@gnu.org>
parents:
23109
diff
changeset
|
292 ;; and repeat it the right number of times. |
24531 | 293 (setq insertion (substring insertion -1)) |
23474
242836a572c4
(repeat): Handle keyboard macros properly.
Richard M. Stallman <rms@gnu.org>
parents:
23109
diff
changeset
|
294 (let ((count (prefix-numeric-value repeat-arg)) |
242836a572c4
(repeat): Handle keyboard macros properly.
Richard M. Stallman <rms@gnu.org>
parents:
23109
diff
changeset
|
295 (i 0)) |
78698
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
296 ;; Run pre- and post-command hooks for self-insertion too. |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
297 (run-hooks 'pre-command-hook) |
87862
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
298 (cond |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
299 ((not repeat-undo-count)) |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
300 ((< repeat-undo-count 20) |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
301 ;; Don't make an undo-boundary here. |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
302 (setq repeat-undo-count (1+ repeat-undo-count))) |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
303 (t |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
304 ;; Make an undo-boundary after 20 repetitions only. |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
305 (undo-boundary) |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
306 (setq repeat-undo-count 1))) |
23474
242836a572c4
(repeat): Handle keyboard macros properly.
Richard M. Stallman <rms@gnu.org>
parents:
23109
diff
changeset
|
307 (while (< i count) |
242836a572c4
(repeat): Handle keyboard macros properly.
Richard M. Stallman <rms@gnu.org>
parents:
23109
diff
changeset
|
308 (repeat-self-insert insertion) |
78698
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
309 (setq i (1+ i))) |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
310 (run-hooks 'post-command-hook))) |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
311 (let ((indirect (indirect-function last-repeatable-command))) |
87862
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
312 ;; Make each repetition undo separately. |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
313 (undo-boundary) |
24531 | 314 (if (or (stringp indirect) |
315 (vectorp indirect)) | |
78698
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
316 ;; Bind real-last-command so that executing the macro does |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
317 ;; not alter it. Do the same for last-repeatable-command. |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
318 (let ((real-last-command real-last-command) |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
319 (last-repeatable-command last-repeatable-command)) |
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
320 (execute-kbd-macro last-repeatable-command)) |
61585
d5d321d5c34f
(repeat): Invoke pre-command-hook and post-command-hook.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
321 (run-hooks 'pre-command-hook) |
78698
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
322 (call-interactively last-repeatable-command) |
61585
d5d321d5c34f
(repeat): Invoke pre-command-hook and post-command-hook.
Eli Zaretskii <eliz@gnu.org>
parents:
52401
diff
changeset
|
323 (run-hooks 'post-command-hook))))) |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
324 (when repeat-repeat-char |
22131 | 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. | |
87862
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
330 (let (repeat-on-final-keystroke |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
331 ;; Bind `undo-inhibit-record-point' to t in order to avoid |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
332 ;; recording point in `buffer-undo-list' here. We have to |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
333 ;; do this since the command loop does not set the last |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
334 ;; position of point thus confusing the point recording |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
335 ;; mechanism when inserting or deleting text. |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
336 (undo-inhibit-record-point t)) |
78698
3260c437ca82
(repeat): Use last-repeatable-command instead of
Martin Rudalics <rudalics@gmx.at>
parents:
78236
diff
changeset
|
337 (setq real-last-command 'repeat) |
87862
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
338 (setq repeat-undo-count 1) |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
339 (unwind-protect |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
340 (while (eq (read-event) repeat-repeat-char) |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
341 (repeat repeat-arg)) |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
342 ;; Make sure `repeat-undo-count' is reset. |
b151c81bfd6e
(repeat-undo-count): New variable.
Martin Rudalics <rudalics@gmx.at>
parents:
79721
diff
changeset
|
343 (setq repeat-undo-count nil)) |
22131 | 344 (setq unread-command-events (list last-input-event)))))) |
345 | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
346 (defun repeat-self-insert (string) |
22132 | 347 (let ((i 0)) |
348 (while (< i (length string)) | |
349 (let ((last-command-char (aref string i))) | |
350 (self-insert-command 1)) | |
351 (setq i (1+ i))))) | |
352 | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
353 (defun repeat-message (format &rest args) |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
354 "Like `message' but displays with `repeat-message-function' if non-nil." |
22131 | 355 (let ((message (apply 'format format args))) |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
356 (if repeat-message-function |
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
357 (funcall repeat-message-function message) |
22131 | 358 (message "%s" message)))) |
359 | |
360 ;; OK, there's one situation left where that doesn't work correctly: when the | |
361 ;; most recent self-insertion provoked an auto-fill. The problem is that | |
362 ;; unravelling the undo information after an auto-fill is too hard, since all | |
363 ;; kinds of stuff can get in there as a result of comment prefixes etc. It'd | |
364 ;; be possible to advise do-auto-fill to record the most recent | |
365 ;; self-insertion before it does its thing, but that's a performance hit on | |
366 ;; auto-fill, which already has performance problems; so it's better to just | |
367 ;; leave it like this. If text didn't provoke an auto-fill when the user | |
368 ;; typed it, this'll correctly repeat its self-insertion, even if the | |
369 ;; repetition does cause auto-fill. | |
370 | |
371 ;; If you wanted perfection, probably it'd be necessary to hack do-auto-fill | |
372 ;; into 2 functions, maybe-do-auto-fill & really-do-auto-fill, because only | |
373 ;; really-do-auto-fill should be advised. As things are, either the undo | |
374 ;; information would need to be scanned on every do-auto-fill invocation, or | |
375 ;; the code at the top of do-auto-fill deciding whether filling is necessary | |
376 ;; would need to be duplicated in the advice, wasting execution time when | |
377 ;; filling does turn out to be necessary. | |
378 | |
379 ;; I thought maybe this story had a moral, something about functional | |
380 ;; decomposition; but now I'm not even sure of that, since a function | |
381 ;; call per se is a performance hit, & even the code that would | |
382 ;; correspond to really-do-auto-fill has performance problems that | |
383 ;; can make it necessary to stop typing while Emacs catches up. | |
384 ;; Maybe the real moral is that perfection is a chimera. | |
385 | |
386 ;; Ah, hell, it's all going to fall into a black hole someday anyway. | |
387 | |
388 ;;;;; ************************* EMACS CONTROL ************************* ;;;;; | |
389 | |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
390 (provide 'repeat) |
22131 | 391 |
52401 | 392 ;;; arch-tag: cd569600-a1ad-4fa7-9062-bb91dfeaf1db |
22439
3dc0b9f57ff6
Renamed from vi-dot.el.
Richard M. Stallman <rms@gnu.org>
parents:
22132
diff
changeset
|
393 ;;; repeat.el ends here |