annotate lisp/repeat.el @ 22419:3967db186db6

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