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