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