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