Mercurial > emacs
annotate lisp/emulation/viper-macs.el @ 30167:83e9db7aeddb
Add optional version as 4th element of specs and use it for several
things new in v21. Remove load-path. Fix type of
line-number-display-limit.
author | Dave Love <fx@gnu.org> |
---|---|
date | Wed, 12 Jul 2000 17:25:28 +0000 |
parents | 4f315ca65976 |
children | 67b464da13ec |
rev | line source |
---|---|
13337 | 1 ;;; viper-macs.el --- functions implementing keyboard macros for Viper |
2 | |
18047 | 3 ;; Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation, Inc. |
11288 | 4 |
10789 | 5 ;; This file is part of GNU Emacs. |
6 | |
7 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
8 ;; it under the terms of the GNU General Public License as published by | |
9 ;; the Free Software Foundation; either version 2, or (at your option) | |
10 ;; any later version. | |
11 | |
12 ;; GNU Emacs is distributed in the hope that it will be useful, | |
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 ;; GNU General Public License for more details. | |
16 | |
17 ;; You should have received a copy of the GNU General Public License | |
14169 | 18 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 ;; Boston, MA 02111-1307, USA. | |
10789 | 21 |
14909
7ff1df13b124
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14587
diff
changeset
|
22 ;; Code |
7ff1df13b124
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14587
diff
changeset
|
23 |
18047 | 24 (provide 'viper-macs) |
25 | |
26 ;; compiler pacifier | |
19079 | 27 (defvar viper-ex-work-buf) |
28 (defvar viper-custom-file-name) | |
29 (defvar viper-current-state) | |
30 (defvar viper-fast-keyseq-timeout) | |
18047 | 31 |
18172 | 32 ;; loading happens only in non-interactive compilation |
33 ;; in order to spare non-viperized emacs from being viperized | |
34 (if noninteractive | |
35 (eval-when-compile | |
36 (let ((load-path (cons (expand-file-name ".") load-path))) | |
37 (or (featurep 'viper-util) | |
38 (load "viper-util.el" nil nil 'nosuffix)) | |
39 (or (featurep 'viper-keym) | |
40 (load "viper-keym.el" nil nil 'nosuffix)) | |
41 (or (featurep 'viper-mous) | |
42 (load "viper-mous.el" nil nil 'nosuffix)) | |
43 (or (featurep 'viper-cmd) | |
44 (load "viper-cmd.el" nil nil 'nosuffix)) | |
45 ))) | |
18047 | 46 ;; end pacifier |
47 | |
10789 | 48 (require 'viper-util) |
14909
7ff1df13b124
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14587
diff
changeset
|
49 (require 'viper-keym) |
7ff1df13b124
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14587
diff
changeset
|
50 |
10789 | 51 |
52 ;;; Variables | |
53 | |
54 ;; Register holding last macro. | |
19079 | 55 (defvar viper-last-macro-reg nil) |
10789 | 56 |
57 ;; format of the elements of kbd alists: | |
58 ;; (name ((buf . macr)...(buf . macr)) ((maj-mode . macr)...) (t . macr)) | |
59 ;; kbd macro alist for Vi state | |
19079 | 60 (defvar viper-vi-kbd-macro-alist nil) |
10789 | 61 ;; same for insert/replace state |
19079 | 62 (defvar viper-insert-kbd-macro-alist nil) |
10789 | 63 ;; same for emacs state |
19079 | 64 (defvar viper-emacs-kbd-macro-alist nil) |
10789 | 65 |
66 ;; Internal var that passes info between start-kbd-macro and end-kbd-macro | |
67 ;; in :map and :map! | |
19079 | 68 (defvar viper-kbd-macro-parameters nil) |
10789 | 69 |
19079 | 70 (defvar viper-this-kbd-macro nil |
10789 | 71 "Vector of keys representing the name of currently running Viper kbd macro.") |
19079 | 72 (defvar viper-last-kbd-macro nil |
10789 | 73 "Vector of keys representing the name of last Viper keyboard macro.") |
74 | |
19079 | 75 (defcustom viper-repeat-from-history-key 'f12 |
18839 | 76 "Prefix key for accessing previously typed Vi commands. |
10789 | 77 |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
78 The previous command is accessible, as usual, via `.'. The command before this |
18839 | 79 can be invoked as `<this key> 1', and the command before that, and the command |
80 before that one is accessible as `<this key> 2'. | |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
81 The notation for these keys is borrowed from XEmacs. Basically, |
10789 | 82 a key is a symbol, e.g., `a', `\\1', `f2', etc., or a list, e.g., |
18839 | 83 `(meta control f1)'." |
19907
819a01a83872
(viper-repeat-from-history-key): Fix customize type.
Richard M. Stallman <rms@gnu.org>
parents:
19079
diff
changeset
|
84 :type 'sexp |
18839 | 85 :group 'viper) |
10789 | 86 |
87 | |
88 | |
89 ;;; Code | |
90 | |
12140
75379a19c5d5
Changed vip-*-frame-* to *-frame-*, incorporated overlay strings,
Karl Heuer <kwzh@gnu.org>
parents:
11288
diff
changeset
|
91 ;; Ex map command |
10789 | 92 (defun ex-map () |
93 (let ((mod-char "") | |
94 macro-name macro-body map-args ins) | |
95 (save-window-excursion | |
19079 | 96 (set-buffer viper-ex-work-buf) |
10789 | 97 (if (looking-at "!") |
98 (progn | |
99 (setq ins t | |
100 mod-char "!") | |
101 (forward-char 1)))) | |
102 (setq map-args (ex-map-read-args mod-char) | |
103 macro-name (car map-args) | |
104 macro-body (cdr map-args)) | |
19079 | 105 (setq viper-kbd-macro-parameters (list ins mod-char macro-name macro-body)) |
10789 | 106 (if macro-body |
19079 | 107 (viper-end-mapping-kbd-macro 'ignore) |
10789 | 108 (ex-fixup-history (format "map%s %S" mod-char |
19079 | 109 (viper-display-macro macro-name))) |
10789 | 110 ;; if defining macro for insert, switch there for authentic WYSIWYG |
19079 | 111 (if ins (viper-change-state-to-insert)) |
10789 | 112 (start-kbd-macro nil) |
19079 | 113 (define-key viper-vi-intercept-map "\C-x)" 'viper-end-mapping-kbd-macro) |
114 (define-key viper-insert-intercept-map "\C-x)" 'viper-end-mapping-kbd-macro) | |
115 (define-key viper-emacs-intercept-map "\C-x)" 'viper-end-mapping-kbd-macro) | |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
116 (message "Mapping %S in %s state. Hit `C-x )' to complete the mapping" |
19079 | 117 (viper-display-macro macro-name) |
10789 | 118 (if ins "Insert" "Vi"))) |
119 )) | |
120 | |
121 | |
12140
75379a19c5d5
Changed vip-*-frame-* to *-frame-*, incorporated overlay strings,
Karl Heuer <kwzh@gnu.org>
parents:
11288
diff
changeset
|
122 ;; Ex unmap |
10789 | 123 (defun ex-unmap () |
124 (let ((mod-char "") | |
125 temp macro-name ins) | |
126 (save-window-excursion | |
19079 | 127 (set-buffer viper-ex-work-buf) |
10789 | 128 (if (looking-at "!") |
129 (progn | |
130 (setq ins t | |
131 mod-char "!") | |
132 (forward-char 1)))) | |
133 | |
134 (setq macro-name (ex-unmap-read-args mod-char)) | |
19079 | 135 (setq temp (viper-fixup-macro (vconcat macro-name))) ;; copy and fixup |
10789 | 136 (ex-fixup-history (format "unmap%s %S" mod-char |
19079 | 137 (viper-display-macro temp))) |
138 (viper-unrecord-kbd-macro macro-name (if ins 'insert-state 'vi-state)) | |
10789 | 139 )) |
140 | |
141 | |
142 ;; read arguments for ex-map | |
143 (defun ex-map-read-args (variant) | |
144 (let ((cursor-in-echo-area t) | |
145 (key-seq []) | |
146 temp key event message | |
147 macro-name macro-body args) | |
148 | |
149 (condition-case nil | |
150 (setq args (concat (ex-get-inline-cmd-args ".*map[!]*[ \t]?" "\n\C-m") | |
151 " nil nil ") | |
152 temp (read-from-string args) | |
153 macro-name (car temp) | |
154 macro-body (car (read-from-string args (cdr temp)))) | |
155 (error | |
156 (signal | |
157 'error | |
158 '("map: Macro name and body must be a quoted string or a vector")))) | |
159 | |
160 ;; We expect macro-name to be a vector, a string, or a quoted string. | |
161 ;; In the second case, it will emerge as a symbol when read from | |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
162 ;; the above read-from-string. So we need to convert it into a string |
10789 | 163 (if macro-name |
164 (cond ((vectorp macro-name) nil) | |
165 ((stringp macro-name) | |
166 (setq macro-name (vconcat macro-name))) | |
167 (t (setq macro-name (vconcat (prin1-to-string macro-name))))) | |
168 (message ":map%s <Name>" variant)(sit-for 2) | |
169 (while | |
170 (not (member key | |
171 '(?\C-m ?\n (control m) (control j) return linefeed))) | |
172 (setq key-seq (vconcat key-seq (if key (vector key) []))) | |
173 ;; the only keys available for editing are these-- no help while there | |
174 (if (member | |
175 key | |
176 '(?\b ?\d '^? '^H (control h) (control \?) backspace delete)) | |
177 (setq key-seq (subseq key-seq 0 (- (length key-seq) 2)))) | |
178 (setq message | |
14585
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
179 (format |
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
180 ":map%s %s" |
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
181 variant (if (> (length key-seq) 0) |
19079 | 182 (prin1-to-string (viper-display-macro key-seq)) |
14585
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
183 ""))) |
14384
854325337547
Moved code around to minimize compiler warnings.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14335
diff
changeset
|
184 (message message) |
19079 | 185 (setq event (viper-read-key)) |
186 ;;(setq event (viper-read-event)) | |
10789 | 187 (setq key |
19079 | 188 (if (viper-mouse-event-p event) |
10789 | 189 (progn |
190 (message "%s (No mouse---only keyboard keys, please)" | |
191 message) | |
192 (sit-for 2) | |
193 nil) | |
19079 | 194 (viper-event-key event))) |
10789 | 195 ) |
196 (setq macro-name key-seq)) | |
197 | |
198 (if (= (length macro-name) 0) | |
199 (error "Can't map an empty macro name")) | |
19079 | 200 (setq macro-name (viper-fixup-macro macro-name)) |
201 (if (viper-char-array-p macro-name) | |
202 (setq macro-name (viper-char-array-to-macro macro-name))) | |
10789 | 203 |
204 (if macro-body | |
19079 | 205 (cond ((viper-char-array-p macro-body) |
206 (setq macro-body (viper-char-array-to-macro macro-body))) | |
10789 | 207 ((vectorp macro-body) nil) |
208 (t (error "map: Invalid syntax in macro definition")))) | |
12140
75379a19c5d5
Changed vip-*-frame-* to *-frame-*, incorporated overlay strings,
Karl Heuer <kwzh@gnu.org>
parents:
11288
diff
changeset
|
209 (setq cursor-in-echo-area nil)(sit-for 0) ; this overcomes xemacs tty bug |
75379a19c5d5
Changed vip-*-frame-* to *-frame-*, incorporated overlay strings,
Karl Heuer <kwzh@gnu.org>
parents:
11288
diff
changeset
|
210 (cons macro-name macro-body))) |
10789 | 211 |
212 | |
213 | |
214 ;; read arguments for ex-unmap | |
215 (defun ex-unmap-read-args (variant) | |
216 (let ((cursor-in-echo-area t) | |
217 (macro-alist (if (string= variant "!") | |
19079 | 218 viper-insert-kbd-macro-alist |
219 viper-vi-kbd-macro-alist)) | |
10789 | 220 ;; these are disabled just in case, to avoid surprises when doing |
221 ;; completing-read | |
19079 | 222 viper-vi-kbd-minor-mode viper-insert-kbd-minor-mode |
223 viper-emacs-kbd-minor-mode | |
224 viper-vi-intercept-minor-mode viper-insert-intercept-minor-mode | |
225 viper-emacs-intercept-minor-mode | |
10789 | 226 event message |
227 key key-seq macro-name) | |
228 (setq macro-name (ex-get-inline-cmd-args ".*unma?p?[!]*[ \t]*")) | |
229 | |
230 (if (> (length macro-name) 0) | |
231 () | |
232 (message ":unmap%s <Name>" variant) (sit-for 2) | |
233 (while | |
234 (not | |
235 (member key '(?\C-m ?\n (control m) (control j) return linefeed))) | |
236 (setq key-seq (vconcat key-seq (if key (vector key) []))) | |
237 ;; the only keys available for editing are these-- no help while there | |
238 (cond ((member | |
239 key | |
240 '(?\b ?\d '^? '^H (control h) (control \?) backspace delete)) | |
241 (setq key-seq (subseq key-seq 0 (- (length key-seq) 2)))) | |
242 ((member key '(tab (control i) ?\t)) | |
243 (setq key-seq (subseq key-seq 0 (1- (length key-seq)))) | |
244 (setq message | |
14585
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
245 (format |
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
246 ":unmap%s %s" |
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
247 variant (if (> (length key-seq) 0) |
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
248 (prin1-to-string |
19079 | 249 (viper-display-macro key-seq)) |
14585
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
250 ""))) |
10789 | 251 (setq key-seq |
19079 | 252 (viper-do-sequence-completion key-seq macro-alist message)) |
10789 | 253 )) |
254 (setq message | |
14585
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
255 (format |
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
256 ":unmap%s %s" |
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
257 variant (if (> (length key-seq) 0) |
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
258 (prin1-to-string |
19079 | 259 (viper-display-macro key-seq)) |
14585
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
260 ""))) |
14384
854325337547
Moved code around to minimize compiler warnings.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14335
diff
changeset
|
261 (message message) |
19079 | 262 (setq event (viper-read-key)) |
263 ;;(setq event (viper-read-event)) | |
10789 | 264 (setq key |
19079 | 265 (if (viper-mouse-event-p event) |
10789 | 266 (progn |
267 (message "%s (No mouse---only keyboard keys, please)" | |
268 message) | |
269 (sit-for 2) | |
270 nil) | |
19079 | 271 (viper-event-key event))) |
10789 | 272 ) |
273 (setq macro-name key-seq)) | |
274 | |
275 (if (= (length macro-name) 0) | |
276 (error "Can't unmap an empty macro name")) | |
277 | |
278 ;; convert macro names into vector, if starts with a `[' | |
279 (if (memq (elt macro-name 0) '(?\[ ?\")) | |
280 (car (read-from-string macro-name)) | |
281 (vconcat macro-name)) | |
282 )) | |
283 | |
284 | |
14585
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
285 ;; Terminate a Vi kbd macro. |
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
286 ;; optional argument IGNORE, if t, indicates that we are dealing with an |
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
287 ;; existing macro that needs to be registered, but there is no need to |
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
288 ;; terminate a kbd macro. |
19079 | 289 (defun viper-end-mapping-kbd-macro (&optional ignore) |
10789 | 290 (interactive) |
19079 | 291 (define-key viper-vi-intercept-map "\C-x)" nil) |
292 (define-key viper-insert-intercept-map "\C-x)" nil) | |
293 (define-key viper-emacs-intercept-map "\C-x)" nil) | |
10789 | 294 (if (and (not ignore) |
19079 | 295 (or (not viper-kbd-macro-parameters) |
10789 | 296 (not defining-kbd-macro))) |
297 (error "Not mapping a kbd-macro")) | |
19079 | 298 (let ((mod-char (nth 1 viper-kbd-macro-parameters)) |
299 (ins (nth 0 viper-kbd-macro-parameters)) | |
300 (macro-name (nth 2 viper-kbd-macro-parameters)) | |
301 (macro-body (nth 3 viper-kbd-macro-parameters))) | |
302 (setq viper-kbd-macro-parameters nil) | |
10789 | 303 (or ignore |
304 (progn | |
305 (end-kbd-macro nil) | |
19079 | 306 (setq macro-body (viper-events-to-macro last-kbd-macro)) |
10789 | 307 ;; always go back to Vi, since this is where we started |
308 ;; defining macro | |
19079 | 309 (viper-change-state-to-vi))) |
10789 | 310 |
19079 | 311 (viper-record-kbd-macro macro-name |
10789 | 312 (if ins 'insert-state 'vi-state) |
19079 | 313 (viper-display-macro macro-body)) |
10789 | 314 |
315 (ex-fixup-history (format "map%s %S %S" mod-char | |
19079 | 316 (viper-display-macro macro-name) |
317 (viper-display-macro macro-body))) | |
10789 | 318 )) |
319 | |
320 | |
321 | |
322 | |
323 ;;; Recording, unrecording, executing | |
324 | |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
325 ;; Accepts as macro names: strings and vectors. |
10789 | 326 ;; strings must be strings of characters; vectors must be vectors of keys |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
327 ;; in canonic form. The canonic form is essentially the form used in XEmacs |
19079 | 328 (defun viper-record-kbd-macro (macro-name state macro-body &optional scope) |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
329 "Record a Vi macro. Can be used in `.viper' file to define permanent macros. |
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
330 MACRO-NAME is a string of characters or a vector of keys. STATE is |
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
331 either `vi-state' or `insert-state'. It specifies the Viper state in which to |
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
332 define the macro. MACRO-BODY is a string that represents the keyboard macro. |
10789 | 333 Optional SCOPE says whether the macro should be global \(t\), mode-specific |
334 \(a major-mode symbol\), or buffer-specific \(buffer name, a string\). | |
335 If SCOPE is nil, the user is asked to specify the scope." | |
336 (let* (state-name keymap | |
337 (macro-alist-var | |
338 (cond ((eq state 'vi-state) | |
339 (setq state-name "Vi state" | |
19079 | 340 keymap viper-vi-kbd-map) |
341 'viper-vi-kbd-macro-alist) | |
10789 | 342 ((memq state '(insert-state replace-state)) |
343 (setq state-name "Insert state" | |
19079 | 344 keymap viper-insert-kbd-map) |
345 'viper-insert-kbd-macro-alist) | |
10789 | 346 (t |
347 (setq state-name "Emacs state" | |
19079 | 348 keymap viper-emacs-kbd-map) |
349 'viper-emacs-kbd-macro-alist) | |
10789 | 350 )) |
351 new-elt old-elt old-sub-elt msg | |
352 temp lis lis2) | |
353 | |
354 (if (= (length macro-name) 0) | |
355 (error "Can't map an empty macro name")) | |
356 | |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
357 ;; Macro-name is usually a vector. However, command history or macros |
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
358 ;; recorded in ~/.viper may be recorded as strings. So, convert to |
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
359 ;; vectors. |
19079 | 360 (setq macro-name (viper-fixup-macro macro-name)) |
361 (if (viper-char-array-p macro-name) | |
362 (setq macro-name (viper-char-array-to-macro macro-name))) | |
363 (setq macro-body (viper-fixup-macro macro-body)) | |
364 (if (viper-char-array-p macro-body) | |
365 (setq macro-body (viper-char-array-to-macro macro-body))) | |
10789 | 366 |
367 ;; don't ask if scope is given and is of the right type | |
368 (or (eq scope t) | |
369 (stringp scope) | |
370 (and scope (symbolp scope)) | |
371 (progn | |
372 (setq scope | |
373 (cond | |
374 ((y-or-n-p | |
375 (format | |
376 "Map this macro for buffer `%s' only? " | |
377 (buffer-name))) | |
378 (setq msg | |
379 (format | |
380 "%S is mapped to %s for %s in `%s'" | |
19079 | 381 (viper-display-macro macro-name) |
382 (viper-abbreviate-string | |
10789 | 383 (format |
384 "%S" | |
19079 | 385 (setq temp (viper-display-macro macro-body))) |
10789 | 386 14 "" "" |
387 (if (stringp temp) " ....\"" " ....]")) | |
388 state-name (buffer-name))) | |
389 (buffer-name)) | |
390 ((y-or-n-p | |
391 (format | |
392 "Map this macro for the major mode `%S' only? " | |
393 major-mode)) | |
394 (setq msg | |
395 (format | |
396 "%S is mapped to %s for %s in `%S'" | |
19079 | 397 (viper-display-macro macro-name) |
398 (viper-abbreviate-string | |
10789 | 399 (format |
400 "%S" | |
19079 | 401 (setq temp (viper-display-macro macro-body))) |
10789 | 402 14 "" "" |
403 (if (stringp macro-body) " ....\"" " ....]")) | |
404 state-name major-mode)) | |
405 major-mode) | |
406 (t | |
407 (setq msg | |
408 (format | |
409 "%S is globally mapped to %s in %s" | |
19079 | 410 (viper-display-macro macro-name) |
411 (viper-abbreviate-string | |
10789 | 412 (format |
413 "%S" | |
19079 | 414 (setq temp (viper-display-macro macro-body))) |
10789 | 415 14 "" "" |
416 (if (stringp macro-body) " ....\"" " ....]")) | |
417 state-name)) | |
418 t))) | |
14580
1883960762e0
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14433
diff
changeset
|
419 (if (y-or-n-p |
1883960762e0
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14433
diff
changeset
|
420 (format "Save this macro in %s? " |
19079 | 421 (viper-abbreviate-file-name viper-custom-file-name))) |
422 (viper-save-string-in-file | |
423 (format "\n(viper-record-kbd-macro %S '%S %s '%S)" | |
424 (viper-display-macro macro-name) | |
13211
76308c9753ab
(vip-record-kbd-macro): correctly escapes `.' and `[' now.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
12695
diff
changeset
|
425 state |
76308c9753ab
(vip-record-kbd-macro): correctly escapes `.' and `[' now.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
12695
diff
changeset
|
426 ;; if we don't let vector macro-body through %S, |
76308c9753ab
(vip-record-kbd-macro): correctly escapes `.' and `[' now.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
12695
diff
changeset
|
427 ;; the symbols `\.' `\[' etc will be converted into |
76308c9753ab
(vip-record-kbd-macro): correctly escapes `.' and `[' now.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
12695
diff
changeset
|
428 ;; characters, causing invalid read error on recorded |
19079 | 429 ;; macros in .viper. |
13211
76308c9753ab
(vip-record-kbd-macro): correctly escapes `.' and `[' now.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
12695
diff
changeset
|
430 ;; I am not sure is macro-body can still be a string at |
76308c9753ab
(vip-record-kbd-macro): correctly escapes `.' and `[' now.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
12695
diff
changeset
|
431 ;; this point, but I am preserving this option anyway. |
76308c9753ab
(vip-record-kbd-macro): correctly escapes `.' and `[' now.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
12695
diff
changeset
|
432 (if (vectorp macro-body) |
76308c9753ab
(vip-record-kbd-macro): correctly escapes `.' and `[' now.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
12695
diff
changeset
|
433 (format "%S" macro-body) |
76308c9753ab
(vip-record-kbd-macro): correctly escapes `.' and `[' now.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
12695
diff
changeset
|
434 macro-body) |
76308c9753ab
(vip-record-kbd-macro): correctly escapes `.' and `[' now.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
12695
diff
changeset
|
435 scope) |
19079 | 436 viper-custom-file-name)) |
10789 | 437 |
14384
854325337547
Moved code around to minimize compiler warnings.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14335
diff
changeset
|
438 (message msg) |
10789 | 439 )) |
440 | |
441 (setq new-elt | |
442 (cons macro-name | |
443 (cond ((eq scope t) (list nil nil (cons t nil))) | |
444 ((symbolp scope) | |
445 (list nil (list (cons scope nil)) (cons t nil))) | |
446 ((stringp scope) | |
447 (list (list (cons scope nil)) nil (cons t nil)))))) | |
448 (setq old-elt (assoc macro-name (eval macro-alist-var))) | |
449 | |
450 (if (null old-elt) | |
451 (progn | |
452 ;; insert new-elt in macro-alist-var and keep the list sorted | |
453 (define-key | |
454 keymap | |
19079 | 455 (vector (viper-key-to-emacs-key (aref macro-name 0))) |
456 'viper-exec-mapped-kbd-macro) | |
10789 | 457 (setq lis (eval macro-alist-var)) |
19079 | 458 (while (and lis (string< (viper-array-to-string (car (car lis))) |
459 (viper-array-to-string macro-name))) | |
10789 | 460 (setq lis2 (cons (car lis) lis2)) |
461 (setq lis (cdr lis))) | |
462 | |
463 (setq lis2 (reverse lis2)) | |
464 (set macro-alist-var (append lis2 (cons new-elt lis))) | |
465 (setq old-elt new-elt))) | |
466 (setq old-sub-elt | |
19079 | 467 (cond ((eq scope t) (viper-kbd-global-pair old-elt)) |
468 ((symbolp scope) (assoc scope (viper-kbd-mode-alist old-elt))) | |
469 ((stringp scope) (assoc scope (viper-kbd-buf-alist old-elt))))) | |
10789 | 470 (if old-sub-elt |
471 (setcdr old-sub-elt macro-body) | |
472 (cond ((symbolp scope) (setcar (cdr (cdr old-elt)) | |
473 (cons (cons scope macro-body) | |
19079 | 474 (viper-kbd-mode-alist old-elt)))) |
10789 | 475 ((stringp scope) (setcar (cdr old-elt) |
476 (cons (cons scope macro-body) | |
19079 | 477 (viper-kbd-buf-alist old-elt)))))) |
10789 | 478 )) |
479 | |
480 | |
481 | |
19079 | 482 ;; macro name must be a vector of viper-style keys |
483 (defun viper-unrecord-kbd-macro (macro-name state) | |
12140
75379a19c5d5
Changed vip-*-frame-* to *-frame-*, incorporated overlay strings,
Karl Heuer <kwzh@gnu.org>
parents:
11288
diff
changeset
|
484 "Delete macro MACRO-NAME from Viper STATE. |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
485 MACRO-NAME must be a vector of viper-style keys. This command is used by Viper |
19079 | 486 internally, but the user can also use it in ~/.viper to delete pre-defined |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
487 macros supplied with Viper. The best way to avoid mistakes in macro names to |
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
488 be passed to this function is to use viper-describe-kbd-macros and copy the |
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
489 name from there." |
10789 | 490 (let* (state-name keymap |
491 (macro-alist-var | |
492 (cond ((eq state 'vi-state) | |
493 (setq state-name "Vi state" | |
19079 | 494 keymap viper-vi-kbd-map) |
495 'viper-vi-kbd-macro-alist) | |
10789 | 496 ((memq state '(insert-state replace-state)) |
497 (setq state-name "Insert state" | |
19079 | 498 keymap viper-insert-kbd-map) |
499 'viper-insert-kbd-macro-alist) | |
10789 | 500 (t |
501 (setq state-name "Emacs state" | |
19079 | 502 keymap viper-emacs-kbd-map) |
503 'viper-emacs-kbd-macro-alist) | |
10789 | 504 )) |
505 buf-mapping mode-mapping global-mapping | |
506 macro-pair macro-entry) | |
507 | |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
508 ;; Macro-name is usually a vector. However, command history or macros |
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
509 ;; recorded in ~/.viper may appear as strings. So, convert to vectors. |
19079 | 510 (setq macro-name (viper-fixup-macro macro-name)) |
511 (if (viper-char-array-p macro-name) | |
512 (setq macro-name (viper-char-array-to-macro macro-name))) | |
10789 | 513 |
514 (setq macro-entry (assoc macro-name (eval macro-alist-var))) | |
515 (if (= (length macro-name) 0) | |
516 (error "Can't unmap an empty macro name")) | |
517 (if (null macro-entry) | |
518 (error "%S is not mapped to a macro for %s in `%s'" | |
19079 | 519 (viper-display-macro macro-name) |
10789 | 520 state-name (buffer-name))) |
521 | |
19079 | 522 (setq buf-mapping (viper-kbd-buf-pair macro-entry) |
523 mode-mapping (viper-kbd-mode-pair macro-entry) | |
524 global-mapping (viper-kbd-global-pair macro-entry)) | |
10789 | 525 |
526 (cond ((and (cdr buf-mapping) | |
527 (or (and (not (cdr mode-mapping)) (not (cdr global-mapping))) | |
528 (y-or-n-p | |
529 (format "Unmap %S for `%s' only? " | |
19079 | 530 (viper-display-macro macro-name) |
10789 | 531 (buffer-name))))) |
532 (setq macro-pair buf-mapping) | |
533 (message "%S is unmapped for %s in `%s'" | |
19079 | 534 (viper-display-macro macro-name) |
10789 | 535 state-name (buffer-name))) |
536 ((and (cdr mode-mapping) | |
537 (or (not (cdr global-mapping)) | |
538 (y-or-n-p | |
539 (format "Unmap %S for the major mode `%S' only? " | |
19079 | 540 (viper-display-macro macro-name) |
10789 | 541 major-mode)))) |
542 (setq macro-pair mode-mapping) | |
543 (message "%S is unmapped for %s in %S" | |
19079 | 544 (viper-display-macro macro-name) state-name major-mode)) |
545 ((cdr (setq macro-pair (viper-kbd-global-pair macro-entry))) | |
10789 | 546 (message |
18839 | 547 "Global mapping for %S in %s is removed" |
19079 | 548 (viper-display-macro macro-name) state-name)) |
10789 | 549 (t (error "%S is not mapped to a macro for %s in `%s'" |
19079 | 550 (viper-display-macro macro-name) |
10789 | 551 state-name (buffer-name)))) |
552 (setcdr macro-pair nil) | |
553 (or (cdr buf-mapping) | |
554 (cdr mode-mapping) | |
555 (cdr global-mapping) | |
556 (progn | |
557 (set macro-alist-var (delq macro-entry (eval macro-alist-var))) | |
19079 | 558 (if (viper-can-release-key (aref macro-name 0) |
10789 | 559 (eval macro-alist-var)) |
560 (define-key | |
561 keymap | |
19079 | 562 (vector (viper-key-to-emacs-key (aref macro-name 0))) |
10789 | 563 nil)) |
564 )) | |
565 )) | |
566 | |
12140
75379a19c5d5
Changed vip-*-frame-* to *-frame-*, incorporated overlay strings,
Karl Heuer <kwzh@gnu.org>
parents:
11288
diff
changeset
|
567 ;; Check if MACRO-ALIST has an entry for a macro name starting with |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
568 ;; CHAR. If not, this indicates that the binding for this char |
19079 | 569 ;; in viper-vi/insert-kbd-map can be released. |
570 (defun viper-can-release-key (char macro-alist) | |
10789 | 571 (let ((lis macro-alist) |
572 (can-release t) | |
573 macro-name) | |
574 | |
575 (while (and lis can-release) | |
576 (setq macro-name (car (car lis))) | |
577 (if (eq char (aref macro-name 0)) | |
578 (setq can-release nil)) | |
579 (setq lis (cdr lis))) | |
580 can-release)) | |
581 | |
582 | |
19079 | 583 (defun viper-exec-mapped-kbd-macro (count) |
10789 | 584 "Dispatch kbd macro." |
585 (interactive "P") | |
19079 | 586 (let* ((macro-alist (cond ((eq viper-current-state 'vi-state) |
587 viper-vi-kbd-macro-alist) | |
588 ((memq viper-current-state | |
10789 | 589 '(insert-state replace-state)) |
19079 | 590 viper-insert-kbd-macro-alist) |
10789 | 591 (t |
19079 | 592 viper-emacs-kbd-macro-alist))) |
10789 | 593 (unmatched-suffix "") |
594 ;; Macros and keys are executed with other macros turned off | |
595 ;; For macros, this is done to avoid macro recursion | |
19079 | 596 viper-vi-kbd-minor-mode viper-insert-kbd-minor-mode |
597 viper-emacs-kbd-minor-mode | |
10789 | 598 next-best-match keyseq event-seq |
599 macro-first-char macro-alist-elt macro-body | |
600 command) | |
601 | |
602 (setq macro-first-char last-command-event | |
19079 | 603 event-seq (viper-read-fast-keysequence macro-first-char macro-alist) |
604 keyseq (viper-events-to-macro event-seq) | |
10789 | 605 macro-alist-elt (assoc keyseq macro-alist) |
19079 | 606 next-best-match (viper-find-best-matching-macro macro-alist keyseq)) |
10789 | 607 |
608 (if (null macro-alist-elt) | |
609 (setq macro-alist-elt (car next-best-match) | |
610 unmatched-suffix (subseq event-seq (cdr next-best-match)))) | |
611 | |
612 (cond ((null macro-alist-elt)) | |
19079 | 613 ((setq macro-body (viper-kbd-buf-definition macro-alist-elt))) |
614 ((setq macro-body (viper-kbd-mode-definition macro-alist-elt))) | |
615 ((setq macro-body (viper-kbd-global-definition macro-alist-elt)))) | |
10789 | 616 |
617 ;; when defining keyboard macro, don't use the macro mappings | |
618 (if (and macro-body (not defining-kbd-macro)) | |
619 ;; block cmd executed as part of a macro from entering command history | |
620 (let ((command-history command-history)) | |
19079 | 621 (setq viper-this-kbd-macro (car macro-alist-elt)) |
622 (execute-kbd-macro (viper-macro-to-events macro-body) count) | |
623 (setq viper-this-kbd-macro nil | |
624 viper-last-kbd-macro (car macro-alist-elt)) | |
625 (viper-set-unread-command-events unmatched-suffix)) | |
10789 | 626 ;; If not a macro, or the macro is suppressed while defining another |
627 ;; macro, put keyseq back on the event queue | |
19079 | 628 (viper-set-unread-command-events event-seq) |
10789 | 629 ;; if the user typed arg, then use it if prefix arg is not set by |
630 ;; some other command (setting prefix arg can happen if we do, say, | |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
631 ;; 2dw and there is a macro starting with 2. Then control will go to |
10789 | 632 ;; this routine |
633 (or prefix-arg (setq prefix-arg count)) | |
634 (setq command (key-binding (read-key-sequence nil))) | |
635 (if (commandp command) | |
636 (command-execute command) | |
637 (beep 1))) | |
638 )) | |
639 | |
640 | |
641 | |
642 ;;; Displaying and completing macros | |
643 | |
19079 | 644 (defun viper-describe-kbd-macros () |
10789 | 645 "Show currently defined keyboard macros." |
646 (interactive) | |
19079 | 647 (with-output-to-temp-buffer " *viper-info*" |
10789 | 648 (princ "Macros in Vi state:\n===================\n") |
19079 | 649 (mapcar 'viper-describe-one-macro viper-vi-kbd-macro-alist) |
10789 | 650 (princ "\n\nMacros in Insert and Replace states:\n====================================\n") |
19079 | 651 (mapcar 'viper-describe-one-macro viper-insert-kbd-macro-alist) |
10789 | 652 (princ "\n\nMacros in Emacs state:\n======================\n") |
19079 | 653 (mapcar 'viper-describe-one-macro viper-emacs-kbd-macro-alist) |
10789 | 654 )) |
655 | |
19079 | 656 (defun viper-describe-one-macro (macro) |
10789 | 657 (princ (format "\n *** Mappings for %S:\n ------------\n" |
19079 | 658 (viper-display-macro (car macro)))) |
10789 | 659 (princ " ** Buffer-specific:") |
19079 | 660 (if (viper-kbd-buf-alist macro) |
661 (mapcar 'viper-describe-one-macro-elt (viper-kbd-buf-alist macro)) | |
10789 | 662 (princ " none\n")) |
663 (princ "\n ** Mode-specific:") | |
19079 | 664 (if (viper-kbd-mode-alist macro) |
665 (mapcar 'viper-describe-one-macro-elt (viper-kbd-mode-alist macro)) | |
10789 | 666 (princ " none\n")) |
667 (princ "\n ** Global:") | |
19079 | 668 (if (viper-kbd-global-definition macro) |
669 (princ (format "\n %S" (cdr (viper-kbd-global-pair macro)))) | |
10789 | 670 (princ " none")) |
671 (princ "\n")) | |
672 | |
19079 | 673 (defun viper-describe-one-macro-elt (elt) |
10789 | 674 (let ((name (car elt)) |
675 (defn (cdr elt))) | |
676 (princ (format "\n * %S:\n %S\n" name defn)))) | |
677 | |
678 | |
679 | |
680 ;; check if SEQ is a prefix of some car of an element in ALIST | |
19079 | 681 (defun viper-keyseq-is-a-possible-macro (seq alist) |
682 (let ((converted-seq (viper-events-to-macro seq))) | |
10789 | 683 (eval (cons 'or |
684 (mapcar | |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
685 (lambda (elt) (viper-prefix-subseq-p converted-seq elt)) |
19079 | 686 (viper-this-buffer-macros alist)))))) |
10789 | 687 |
688 ;; whether SEQ1 is a prefix of SEQ2 | |
19079 | 689 (defun viper-prefix-subseq-p (seq1 seq2) |
10789 | 690 (let ((len1 (length seq1)) |
691 (len2 (length seq2))) | |
692 (if (<= len1 len2) | |
693 (equal seq1 (subseq seq2 0 len1))))) | |
694 | |
695 ;; find the longest common prefix | |
19079 | 696 (defun viper-common-seq-prefix (&rest seqs) |
10789 | 697 (let* ((first (car seqs)) |
698 (rest (cdr seqs)) | |
699 (pref []) | |
700 (idx 0) | |
701 len) | |
702 (if (= (length seqs) 0) | |
703 (setq len 0) | |
704 (setq len (apply 'min (mapcar 'length seqs)))) | |
705 (while (< idx len) | |
706 (if (eval (cons 'and | |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
707 (mapcar (lambda (s) (equal (elt first idx) (elt s idx))) |
10789 | 708 rest))) |
709 (setq pref (vconcat pref (vector (elt first idx))))) | |
710 (setq idx (1+ idx))) | |
711 pref)) | |
712 | |
713 ;; get all sequences that match PREFIX from a given A-LIST | |
19079 | 714 (defun viper-extract-matching-alist-members (pref alist) |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
715 (delq nil (mapcar (lambda (elt) (if (viper-prefix-subseq-p pref elt) elt)) |
19079 | 716 (viper-this-buffer-macros alist)))) |
10789 | 717 |
19079 | 718 (defun viper-do-sequence-completion (seq alist compl-message) |
719 (let* ((matches (viper-extract-matching-alist-members seq alist)) | |
720 (new-seq (apply 'viper-common-seq-prefix matches)) | |
10789 | 721 ) |
722 (cond ((and (equal seq new-seq) (= (length matches) 1)) | |
723 (message "%s (Sole completion)" compl-message) | |
724 (sit-for 2)) | |
725 ((null matches) | |
726 (message "%s (No match)" compl-message) | |
727 (sit-for 2) | |
728 (setq new-seq seq)) | |
729 ((member seq matches) | |
730 (message "%s (Complete, but not unique)" compl-message) | |
731 (sit-for 2) | |
19079 | 732 (viper-display-vector-completions matches)) |
10789 | 733 ((equal seq new-seq) |
19079 | 734 (viper-display-vector-completions matches))) |
10789 | 735 new-seq)) |
736 | |
737 | |
19079 | 738 (defun viper-display-vector-completions (list) |
10789 | 739 (with-output-to-temp-buffer "*Completions*" |
740 (display-completion-list | |
741 (mapcar 'prin1-to-string | |
19079 | 742 (mapcar 'viper-display-macro list))))) |
10789 | 743 |
744 | |
745 | |
746 ;; alist is the alist of macros | |
747 ;; str is the fast key sequence entered | |
748 ;; returns: (matching-macro-def . unmatched-suffix-start-index) | |
19079 | 749 (defun viper-find-best-matching-macro (alist str) |
10789 | 750 (let ((lis alist) |
751 (def-len 0) | |
752 (str-len (length str)) | |
753 match unmatched-start-idx found macro-def) | |
754 (while (and (not found) lis) | |
755 (setq macro-def (car lis) | |
756 def-len (length (car macro-def))) | |
757 (if (and (>= str-len def-len) | |
758 (equal (car macro-def) (subseq str 0 def-len))) | |
19079 | 759 (if (or (viper-kbd-buf-definition macro-def) |
760 (viper-kbd-mode-definition macro-def) | |
761 (viper-kbd-global-definition macro-def)) | |
10789 | 762 (setq found t)) |
763 ) | |
764 (setq lis (cdr lis))) | |
765 | |
766 (if found | |
767 (setq match macro-def | |
768 unmatched-start-idx def-len) | |
769 (setq match nil | |
770 unmatched-start-idx 0)) | |
771 | |
772 (cons match unmatched-start-idx))) | |
773 | |
774 | |
775 | |
776 ;; returns a list of names of macros defined for the current buffer | |
19079 | 777 (defun viper-this-buffer-macros (macro-alist) |
10789 | 778 (let (candidates) |
779 (setq candidates | |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
780 (mapcar (lambda (elt) |
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
781 (if (or (viper-kbd-buf-definition elt) |
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
782 (viper-kbd-mode-definition elt) |
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
783 (viper-kbd-global-definition elt)) |
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
784 (car elt))) |
10789 | 785 macro-alist)) |
786 (setq candidates (delq nil candidates)))) | |
787 | |
788 | |
12140
75379a19c5d5
Changed vip-*-frame-* to *-frame-*, incorporated overlay strings,
Karl Heuer <kwzh@gnu.org>
parents:
11288
diff
changeset
|
789 ;; if seq of Viper key symbols (representing a macro) can be converted to a |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
790 ;; string--do so. Otherwise, do nothing. |
19079 | 791 (defun viper-display-macro (macro-name-or-body) |
792 (cond ((viper-char-symbol-sequence-p macro-name-or-body) | |
14585
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
793 (mapconcat 'symbol-name macro-name-or-body "")) |
19079 | 794 ((viper-char-array-p macro-name-or-body) |
14585
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
795 (mapconcat 'char-to-string macro-name-or-body "")) |
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
796 (t macro-name-or-body))) |
10789 | 797 |
14587
542db2bd7e38
(vip-events-to-macro): discard events represented as lists in macro
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14585
diff
changeset
|
798 ;; convert sequence of events (that came presumably from emacs kbd macro) into |
542db2bd7e38
(vip-events-to-macro): discard events represented as lists in macro
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14585
diff
changeset
|
799 ;; Viper's macro, which is a vector of the form |
542db2bd7e38
(vip-events-to-macro): discard events represented as lists in macro
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14585
diff
changeset
|
800 ;; [ desc desc ... ] |
542db2bd7e38
(vip-events-to-macro): discard events represented as lists in macro
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14585
diff
changeset
|
801 ;; Each desc is either a symbol of (meta symb), (shift symb), etc. |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
802 ;; Here we purge events that happen to be lists. In most cases, these events |
14587
542db2bd7e38
(vip-events-to-macro): discard events represented as lists in macro
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14585
diff
changeset
|
803 ;; got into a macro definition unintentionally; say, when the user moves mouse |
542db2bd7e38
(vip-events-to-macro): discard events represented as lists in macro
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14585
diff
changeset
|
804 ;; during a macro definition, then something like (switch-frame ...) might get |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
805 ;; in. Another reason for purging lists-events is that we can't store them in |
14587
542db2bd7e38
(vip-events-to-macro): discard events represented as lists in macro
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14585
diff
changeset
|
806 ;; textual form (say, in .emacs) and then read them back. |
19079 | 807 (defun viper-events-to-macro (event-seq) |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
808 (vconcat (delq nil (mapcar (lambda (elt) (if (consp elt) |
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
809 nil |
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
810 (viper-event-key elt))) |
14587
542db2bd7e38
(vip-events-to-macro): discard events represented as lists in macro
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14585
diff
changeset
|
811 event-seq)))) |
10789 | 812 |
12140
75379a19c5d5
Changed vip-*-frame-* to *-frame-*, incorporated overlay strings,
Karl Heuer <kwzh@gnu.org>
parents:
11288
diff
changeset
|
813 ;; convert strings or arrays of characters to Viper macro form |
19079 | 814 (defun viper-char-array-to-macro (array) |
10789 | 815 (let ((vec (vconcat array)) |
816 macro) | |
19079 | 817 (if viper-xemacs-p |
10789 | 818 (setq macro (mapcar 'character-to-event vec)) |
819 (setq macro vec)) | |
19079 | 820 (vconcat (mapcar 'viper-event-key macro)))) |
10789 | 821 |
14585
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
822 ;; For macros bodies and names, goes over MACRO and checks if all members are |
10789 | 823 ;; names of keys (actually, it only checks if they are symbols or lists |
14585
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
824 ;; if a digit is found, it is converted into a symbol (e.g., 0 -> \0, etc). |
b6228a159e75
(ex-map-read-args,ex-unmap-read-args): fixed messages.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14581
diff
changeset
|
825 ;; If MACRO is not a list or vector -- doesn't change MACRO. |
19079 | 826 (defun viper-fixup-macro (macro) |
10789 | 827 (let ((len (length macro)) |
828 (idx 0) | |
829 elt break) | |
830 (if (or (vectorp macro) (listp macro)) | |
831 (while (and (< idx len) (not break)) | |
832 (setq elt (elt macro idx)) | |
833 (cond ((numberp elt) | |
834 ;; fix number | |
835 (if (and (<= 0 elt) (<= elt 9)) | |
836 (cond ((arrayp macro) | |
837 (aset macro | |
838 idx | |
839 (intern (char-to-string (+ ?0 elt))))) | |
840 ((listp macro) | |
841 (setcar (nthcdr idx macro) | |
842 (intern (char-to-string (+ ?0 elt))))) | |
843 ))) | |
844 ((listp elt) | |
19079 | 845 (viper-fixup-macro elt)) |
10789 | 846 ((symbolp elt) nil) |
847 (t (setq break t))) | |
848 (setq idx (1+ idx)))) | |
849 | |
850 (if break | |
851 (error "Wrong type macro component, symbol-or-listp, %S" elt) | |
852 macro))) | |
853 | |
19079 | 854 (defun viper-char-array-p (array) |
855 (eval (cons 'and (mapcar 'viper-characterp array)))) | |
10789 | 856 |
19079 | 857 (defun viper-macro-to-events (macro-body) |
858 (vconcat (mapcar 'viper-key-to-emacs-key macro-body))) | |
10789 | 859 |
860 | |
861 ;; check if vec is a vector of character symbols | |
19079 | 862 (defun viper-char-symbol-sequence-p (vec) |
10789 | 863 (and |
864 (sequencep vec) | |
865 (eval | |
866 (cons 'and | |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
867 (mapcar (lambda (elt) |
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
868 (and (symbolp elt) (= (length (symbol-name elt)) 1))) |
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
869 vec))))) |
10789 | 870 |
871 | |
872 ;; Check if vec is a vector of key-press events representing characters | |
873 ;; XEmacs only | |
19079 | 874 (defun viper-event-vector-p (vec) |
10789 | 875 (and (vectorp vec) |
876 (eval (cons 'and (mapcar '(lambda (elt) (if (eventp elt) t)) vec))))) | |
877 | |
878 | |
879 ;;; Reading fast key sequences | |
880 | |
881 ;; Assuming that CHAR was the first character in a fast succession of key | |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
882 ;; strokes, read the rest. Return the vector of keys that was entered in |
10789 | 883 ;; this fast succession of key strokes. |
884 ;; A fast keysequence is one that is terminated by a pause longer than | |
19079 | 885 ;; viper-fast-keyseq-timeout. |
886 (defun viper-read-fast-keysequence (event macro-alist) | |
10789 | 887 (let ((lis (vector event)) |
888 next-event) | |
19079 | 889 (while (and (viper-fast-keysequence-p) |
890 (viper-keyseq-is-a-possible-macro lis macro-alist)) | |
891 (setq next-event (viper-read-key)) | |
892 ;;(setq next-event (viper-read-event)) | |
893 (or (viper-mouse-event-p next-event) | |
10789 | 894 (setq lis (vconcat lis (vector next-event))))) |
895 lis)) | |
896 | |
897 | |
898 ;;; Keyboard macros in registers | |
899 | |
900 ;; sets register to last-kbd-macro carefully. | |
19079 | 901 (defun viper-set-register-macro (reg) |
10789 | 902 (if (get-register reg) |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
903 (if (y-or-n-p "Register contains data. Overwrite? ") |
10789 | 904 () |
905 (error | |
26263
4f315ca65976
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
19907
diff
changeset
|
906 "Macro not saved in register. Can still be invoked via `C-x e'"))) |
10789 | 907 (set-register reg last-kbd-macro)) |
908 | |
19079 | 909 (defun viper-register-macro (count) |
10789 | 910 "Keyboard macros in registers - a modified \@ command." |
911 (interactive "P") | |
912 (let ((reg (downcase (read-char)))) | |
913 (cond ((or (and (<= ?a reg) (<= reg ?z))) | |
19079 | 914 (setq viper-last-macro-reg reg) |
10789 | 915 (if defining-kbd-macro |
916 (progn | |
917 (end-kbd-macro) | |
19079 | 918 (viper-set-register-macro reg)) |
10789 | 919 (execute-kbd-macro (get-register reg) count))) |
920 ((or (= ?@ reg) (= ?\^j reg) (= ?\^m reg)) | |
19079 | 921 (if viper-last-macro-reg |
10789 | 922 nil |
923 (error "No previous kbd macro")) | |
19079 | 924 (execute-kbd-macro (get-register viper-last-macro-reg) count)) |
10789 | 925 ((= ?\# reg) |
926 (start-kbd-macro count)) | |
927 ((= ?! reg) | |
928 (setq reg (downcase (read-char))) | |
929 (if (or (and (<= ?a reg) (<= reg ?z))) | |
930 (progn | |
19079 | 931 (setq viper-last-macro-reg reg) |
932 (viper-set-register-macro reg)))) | |
10789 | 933 (t |
14581
4951b11970a1
*** empty log message ***
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
14580
diff
changeset
|
934 (error "`%c': Unknown register" reg))))) |
10789 | 935 |
936 | |
19079 | 937 (defun viper-global-execute () |
10789 | 938 "Call last keyboad macro for each line in the region." |
939 (if (> (point) (mark t)) (exchange-point-and-mark)) | |
940 (beginning-of-line) | |
941 (call-last-kbd-macro) | |
942 (while (< (point) (mark t)) | |
943 (forward-line 1) | |
944 (beginning-of-line) | |
945 (call-last-kbd-macro))) | |
946 | |
947 | |
948 ;;; viper-macs.el ends here |