Mercurial > emacs
annotate lisp/emulation/viper-macs.el @ 12753:8b24e2806602
(HAVE_SELECT): Defined.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Wed, 02 Aug 1995 18:37:33 +0000 |
parents | 3d339e94aece |
children | 76308c9753ab |
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) | |
398 state macro-body scope) | |
399 vip-custom-file-name)) | |
400 | |
401 (message msg) | |
402 )) | |
403 | |
404 (setq new-elt | |
405 (cons macro-name | |
406 (cond ((eq scope t) (list nil nil (cons t nil))) | |
407 ((symbolp scope) | |
408 (list nil (list (cons scope nil)) (cons t nil))) | |
409 ((stringp scope) | |
410 (list (list (cons scope nil)) nil (cons t nil)))))) | |
411 (setq old-elt (assoc macro-name (eval macro-alist-var))) | |
412 | |
413 (if (null old-elt) | |
414 (progn | |
415 ;; insert new-elt in macro-alist-var and keep the list sorted | |
416 (define-key | |
417 keymap | |
418 (vector (vip-key-to-emacs-key (aref macro-name 0))) | |
419 'vip-exec-mapped-kbd-macro) | |
420 (setq lis (eval macro-alist-var)) | |
421 (while (and lis (string< (vip-array-to-string (car (car lis))) | |
422 (vip-array-to-string macro-name))) | |
423 (setq lis2 (cons (car lis) lis2)) | |
424 (setq lis (cdr lis))) | |
425 | |
426 (setq lis2 (reverse lis2)) | |
427 (set macro-alist-var (append lis2 (cons new-elt lis))) | |
428 (setq old-elt new-elt))) | |
429 (setq old-sub-elt | |
430 (cond ((eq scope t) (vip-kbd-global-pair old-elt)) | |
431 ((symbolp scope) (assoc scope (vip-kbd-mode-alist old-elt))) | |
432 ((stringp scope) (assoc scope (vip-kbd-buf-alist old-elt))))) | |
433 (if old-sub-elt | |
434 (setcdr old-sub-elt macro-body) | |
435 (cond ((symbolp scope) (setcar (cdr (cdr old-elt)) | |
436 (cons (cons scope macro-body) | |
437 (vip-kbd-mode-alist old-elt)))) | |
438 ((stringp scope) (setcar (cdr old-elt) | |
439 (cons (cons scope macro-body) | |
440 (vip-kbd-buf-alist old-elt)))))) | |
441 )) | |
442 | |
443 | |
444 | |
445 ;; macro name must be a vector of vip-style keys | |
446 (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
|
447 "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
|
448 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
|
449 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
|
450 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
|
451 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
|
452 there." |
10789 | 453 (let* (state-name keymap |
454 (macro-alist-var | |
455 (cond ((eq state 'vi-state) | |
456 (setq state-name "Vi state" | |
457 keymap vip-vi-kbd-map) | |
458 'vip-vi-kbd-macro-alist) | |
459 ((memq state '(insert-state replace-state)) | |
460 (setq state-name "Insert state" | |
461 keymap vip-insert-kbd-map) | |
462 'vip-insert-kbd-macro-alist) | |
463 (t | |
464 (setq state-name "Emacs state" | |
465 keymap vip-emacs-kbd-map) | |
466 'vip-emacs-kbd-macro-alist) | |
467 )) | |
468 buf-mapping mode-mapping global-mapping | |
469 macro-pair macro-entry) | |
470 | |
471 ;; Macro-name is usually a vector. However, command history or macros | |
472 ;; recorded in ~/.vip may appear as strings. So, convert to vectors. | |
473 (setq macro-name (vip-fixup-macro macro-name)) | |
474 (if (vip-char-array-p macro-name) | |
475 (setq macro-name (vip-char-array-to-macro macro-name))) | |
476 | |
477 (setq macro-entry (assoc macro-name (eval macro-alist-var))) | |
478 (if (= (length macro-name) 0) | |
479 (error "Can't unmap an empty macro name")) | |
480 (if (null macro-entry) | |
481 (error "%S is not mapped to a macro for %s in `%s'" | |
482 (vip-display-macro macro-name) | |
483 state-name (buffer-name))) | |
484 | |
485 (setq buf-mapping (vip-kbd-buf-pair macro-entry) | |
486 mode-mapping (vip-kbd-mode-pair macro-entry) | |
487 global-mapping (vip-kbd-global-pair macro-entry)) | |
488 | |
489 (cond ((and (cdr buf-mapping) | |
490 (or (and (not (cdr mode-mapping)) (not (cdr global-mapping))) | |
491 (y-or-n-p | |
492 (format "Unmap %S for `%s' only? " | |
493 (vip-display-macro macro-name) | |
494 (buffer-name))))) | |
495 (setq macro-pair buf-mapping) | |
496 (message "%S is unmapped for %s in `%s'" | |
497 (vip-display-macro macro-name) | |
498 state-name (buffer-name))) | |
499 ((and (cdr mode-mapping) | |
500 (or (not (cdr global-mapping)) | |
501 (y-or-n-p | |
502 (format "Unmap %S for the major mode `%S' only? " | |
503 (vip-display-macro macro-name) | |
504 major-mode)))) | |
505 (setq macro-pair mode-mapping) | |
506 (message "%S is unmapped for %s in %S" | |
507 (vip-display-macro macro-name) state-name major-mode)) | |
508 ((cdr (setq macro-pair (vip-kbd-global-pair macro-entry))) | |
509 (message | |
510 "Global mapping of %S for %s is removed" | |
511 (vip-display-macro macro-name) state-name)) | |
512 (t (error "%S is not mapped to a macro for %s in `%s'" | |
513 (vip-display-macro macro-name) | |
514 state-name (buffer-name)))) | |
515 (setcdr macro-pair nil) | |
516 (or (cdr buf-mapping) | |
517 (cdr mode-mapping) | |
518 (cdr global-mapping) | |
519 (progn | |
520 (set macro-alist-var (delq macro-entry (eval macro-alist-var))) | |
521 (if (vip-can-release-key (aref macro-name 0) | |
522 (eval macro-alist-var)) | |
523 (define-key | |
524 keymap | |
525 (vector (vip-key-to-emacs-key (aref macro-name 0))) | |
526 nil)) | |
527 )) | |
528 )) | |
529 | |
12140
75379a19c5d5
Changed vip-*-frame-* to *-frame-*, incorporated overlay strings,
Karl Heuer <kwzh@gnu.org>
parents:
11288
diff
changeset
|
530 ;; Check if MACRO-ALIST has an entry for a macro name starting with |
10789 | 531 ;; CHAR. If not, this indicates that the binding for this char |
532 ;; in vip-vi/insert-kbd-map can be released. | |
533 (defun vip-can-release-key (char macro-alist) | |
534 (let ((lis macro-alist) | |
535 (can-release t) | |
536 macro-name) | |
537 | |
538 (while (and lis can-release) | |
539 (setq macro-name (car (car lis))) | |
540 (if (eq char (aref macro-name 0)) | |
541 (setq can-release nil)) | |
542 (setq lis (cdr lis))) | |
543 can-release)) | |
544 | |
545 | |
546 (defun vip-exec-mapped-kbd-macro (count) | |
547 "Dispatch kbd macro." | |
548 (interactive "P") | |
549 (let* ((macro-alist (cond ((eq vip-current-state 'vi-state) | |
550 vip-vi-kbd-macro-alist) | |
551 ((memq vip-current-state | |
552 '(insert-state replace-state)) | |
553 vip-insert-kbd-macro-alist) | |
554 (t | |
555 vip-emacs-kbd-macro-alist))) | |
556 (unmatched-suffix "") | |
557 ;; Macros and keys are executed with other macros turned off | |
558 ;; For macros, this is done to avoid macro recursion | |
559 vip-vi-kbd-minor-mode vip-insert-kbd-minor-mode | |
560 vip-emacs-kbd-minor-mode | |
561 next-best-match keyseq event-seq | |
562 macro-first-char macro-alist-elt macro-body | |
563 command) | |
564 | |
565 (setq macro-first-char last-command-event | |
566 event-seq (vip-read-fast-keysequence macro-first-char macro-alist) | |
567 keyseq (vip-events-to-macro event-seq) | |
568 macro-alist-elt (assoc keyseq macro-alist) | |
569 next-best-match (vip-find-best-matching-macro macro-alist keyseq)) | |
570 | |
571 (if (null macro-alist-elt) | |
572 (setq macro-alist-elt (car next-best-match) | |
573 unmatched-suffix (subseq event-seq (cdr next-best-match)))) | |
574 | |
575 (cond ((null macro-alist-elt)) | |
576 ((setq macro-body (vip-kbd-buf-definition macro-alist-elt))) | |
577 ((setq macro-body (vip-kbd-mode-definition macro-alist-elt))) | |
578 ((setq macro-body (vip-kbd-global-definition macro-alist-elt)))) | |
579 | |
580 ;; when defining keyboard macro, don't use the macro mappings | |
581 (if (and macro-body (not defining-kbd-macro)) | |
582 ;; block cmd executed as part of a macro from entering command history | |
583 (let ((command-history command-history)) | |
584 (setq vip-this-kbd-macro (car macro-alist-elt)) | |
585 (execute-kbd-macro (vip-macro-to-events macro-body) count) | |
586 (setq vip-this-kbd-macro nil | |
587 vip-last-kbd-macro (car macro-alist-elt)) | |
588 (vip-set-unread-command-events unmatched-suffix)) | |
589 ;; If not a macro, or the macro is suppressed while defining another | |
590 ;; macro, put keyseq back on the event queue | |
591 (vip-set-unread-command-events event-seq) | |
592 ;; if the user typed arg, then use it if prefix arg is not set by | |
593 ;; some other command (setting prefix arg can happen if we do, say, | |
594 ;; 2dw and there is a macro starting with 2. Then control will go to | |
595 ;; this routine | |
596 (or prefix-arg (setq prefix-arg count)) | |
597 (setq command (key-binding (read-key-sequence nil))) | |
598 (if (commandp command) | |
599 (command-execute command) | |
600 (beep 1))) | |
601 )) | |
602 | |
603 | |
604 | |
605 ;;; Displaying and completing macros | |
606 | |
607 (defun vip-describe-kbd-macros () | |
608 "Show currently defined keyboard macros." | |
609 (interactive) | |
610 (with-output-to-temp-buffer " *vip-info*" | |
611 (princ "Macros in Vi state:\n===================\n") | |
612 (mapcar 'vip-describe-one-macro vip-vi-kbd-macro-alist) | |
613 (princ "\n\nMacros in Insert and Replace states:\n====================================\n") | |
614 (mapcar 'vip-describe-one-macro vip-insert-kbd-macro-alist) | |
615 (princ "\n\nMacros in Emacs state:\n======================\n") | |
616 (mapcar 'vip-describe-one-macro vip-emacs-kbd-macro-alist) | |
617 )) | |
618 | |
619 (defun vip-describe-one-macro (macro) | |
620 (princ (format "\n *** Mappings for %S:\n ------------\n" | |
621 (vip-display-macro (car macro)))) | |
622 (princ " ** Buffer-specific:") | |
623 (if (vip-kbd-buf-alist macro) | |
624 (mapcar 'vip-describe-one-macro-elt (vip-kbd-buf-alist macro)) | |
625 (princ " none\n")) | |
626 (princ "\n ** Mode-specific:") | |
627 (if (vip-kbd-mode-alist macro) | |
628 (mapcar 'vip-describe-one-macro-elt (vip-kbd-mode-alist macro)) | |
629 (princ " none\n")) | |
630 (princ "\n ** Global:") | |
631 (if (vip-kbd-global-definition macro) | |
632 (princ | |
633 (format "\n %S" | |
634 (cdr (vip-kbd-global-pair macro)))) | |
635 (princ " none")) | |
636 (princ "\n")) | |
637 | |
638 (defun vip-describe-one-macro-elt (elt) | |
639 (let ((name (car elt)) | |
640 (defn (cdr elt))) | |
641 (princ (format "\n * %S:\n %S\n" name defn)))) | |
642 | |
643 | |
644 | |
645 ;; check if SEQ is a prefix of some car of an element in ALIST | |
646 (defun vip-keyseq-is-a-possible-macro (seq alist) | |
647 (let ((converted-seq (vip-events-to-macro seq))) | |
648 (eval (cons 'or | |
649 (mapcar | |
650 (function (lambda (elt) | |
651 (vip-prefix-subseq-p converted-seq elt))) | |
652 (vip-this-buffer-macros alist)))))) | |
653 | |
654 ;; whether SEQ1 is a prefix of SEQ2 | |
655 (defun vip-prefix-subseq-p (seq1 seq2) | |
656 (let ((len1 (length seq1)) | |
657 (len2 (length seq2))) | |
658 (if (<= len1 len2) | |
659 (equal seq1 (subseq seq2 0 len1))))) | |
660 | |
661 ;; find the longest common prefix | |
662 (defun vip-common-seq-prefix (&rest seqs) | |
663 (let* ((first (car seqs)) | |
664 (rest (cdr seqs)) | |
665 (pref []) | |
666 (idx 0) | |
667 len) | |
668 (if (= (length seqs) 0) | |
669 (setq len 0) | |
670 (setq len (apply 'min (mapcar 'length seqs)))) | |
671 (while (< idx len) | |
672 (if (eval (cons 'and | |
673 (mapcar (function (lambda (s) | |
674 (equal (elt first idx) | |
675 (elt s idx)))) | |
676 rest))) | |
677 (setq pref (vconcat pref (vector (elt first idx))))) | |
678 (setq idx (1+ idx))) | |
679 pref)) | |
680 | |
681 ;; get all sequences that match PREFIX from a given A-LIST | |
682 (defun vip-extract-matching-alist-members (pref alist) | |
683 (delq nil (mapcar (function (lambda (elt) | |
684 (if (vip-prefix-subseq-p pref elt) | |
685 elt))) | |
686 (vip-this-buffer-macros alist)))) | |
687 | |
688 (defun vip-do-sequence-completion (seq alist compl-message) | |
689 (let* ((matches (vip-extract-matching-alist-members seq alist)) | |
690 (new-seq (apply 'vip-common-seq-prefix matches)) | |
691 ) | |
692 (cond ((and (equal seq new-seq) (= (length matches) 1)) | |
693 (message "%s (Sole completion)" compl-message) | |
694 (sit-for 2)) | |
695 ((null matches) | |
696 (message "%s (No match)" compl-message) | |
697 (sit-for 2) | |
698 (setq new-seq seq)) | |
699 ((member seq matches) | |
700 (message "%s (Complete, but not unique)" compl-message) | |
701 (sit-for 2) | |
702 (vip-display-vector-completions matches)) | |
703 ((equal seq new-seq) | |
704 (vip-display-vector-completions matches))) | |
705 new-seq)) | |
706 | |
707 | |
708 (defun vip-display-vector-completions (list) | |
709 (with-output-to-temp-buffer "*Completions*" | |
710 (display-completion-list | |
711 (mapcar 'prin1-to-string | |
712 (mapcar 'vip-display-macro list))))) | |
713 | |
714 | |
715 | |
716 ;; alist is the alist of macros | |
717 ;; str is the fast key sequence entered | |
718 ;; returns: (matching-macro-def . unmatched-suffix-start-index) | |
719 (defun vip-find-best-matching-macro (alist str) | |
720 (let ((lis alist) | |
721 (def-len 0) | |
722 (str-len (length str)) | |
723 match unmatched-start-idx found macro-def) | |
724 (while (and (not found) lis) | |
725 (setq macro-def (car lis) | |
726 def-len (length (car macro-def))) | |
727 (if (and (>= str-len def-len) | |
728 (equal (car macro-def) (subseq str 0 def-len))) | |
729 (if (or (vip-kbd-buf-definition macro-def) | |
730 (vip-kbd-mode-definition macro-def) | |
731 (vip-kbd-global-definition macro-def)) | |
732 (setq found t)) | |
733 ) | |
734 (setq lis (cdr lis))) | |
735 | |
736 (if found | |
737 (setq match macro-def | |
738 unmatched-start-idx def-len) | |
739 (setq match nil | |
740 unmatched-start-idx 0)) | |
741 | |
742 (cons match unmatched-start-idx))) | |
743 | |
744 | |
745 | |
746 ;; returns a list of names of macros defined for the current buffer | |
747 (defun vip-this-buffer-macros (macro-alist) | |
748 (let (candidates) | |
749 (setq candidates | |
750 (mapcar (function | |
751 (lambda (elt) | |
752 (if (or (vip-kbd-buf-definition elt) | |
753 (vip-kbd-mode-definition elt) | |
754 (vip-kbd-global-definition elt)) | |
755 (car elt)))) | |
756 macro-alist)) | |
757 (setq candidates (delq nil candidates)))) | |
758 | |
759 | |
12140
75379a19c5d5
Changed vip-*-frame-* to *-frame-*, incorporated overlay strings,
Karl Heuer <kwzh@gnu.org>
parents:
11288
diff
changeset
|
760 ;; 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
|
761 ;; string--do so. Otherwise, do nothing. |
10789 | 762 (defun vip-display-macro (macro-name) |
763 (cond ((vip-char-symbol-sequence-p macro-name) | |
764 (mapconcat 'symbol-name macro-name "")) | |
765 ((vip-char-array-p macro-name) | |
766 (mapconcat 'char-to-string macro-name "")) | |
767 (t macro-name))) | |
768 | |
769 (defun vip-events-to-macro (event-seq) | |
770 (vconcat (mapcar 'vip-event-key event-seq))) | |
771 | |
12140
75379a19c5d5
Changed vip-*-frame-* to *-frame-*, incorporated overlay strings,
Karl Heuer <kwzh@gnu.org>
parents:
11288
diff
changeset
|
772 ;; convert strings or arrays of characters to Viper macro form |
10789 | 773 (defun vip-char-array-to-macro (array) |
774 (let ((vec (vconcat array)) | |
775 macro) | |
776 (if vip-xemacs-p | |
777 (setq macro (mapcar 'character-to-event vec)) | |
778 (setq macro vec)) | |
779 (vconcat (mapcar 'vip-event-key macro)))) | |
780 | |
781 ;; For macros bodies and names, goes over and checks if all members are | |
782 ;; names of keys (actually, it only checks if they are symbols or lists | |
783 ;; if a digit is found, it is converted into a symbol (0 -> \0, etc). | |
784 ;; If not list or vector, doesn't change its argument | |
785 (defun vip-fixup-macro (macro) | |
786 (let ((len (length macro)) | |
787 (idx 0) | |
788 elt break) | |
789 (if (or (vectorp macro) (listp macro)) | |
790 (while (and (< idx len) (not break)) | |
791 (setq elt (elt macro idx)) | |
792 (cond ((numberp elt) | |
793 ;; fix number | |
794 (if (and (<= 0 elt) (<= elt 9)) | |
795 (cond ((arrayp macro) | |
796 (aset macro | |
797 idx | |
798 (intern (char-to-string (+ ?0 elt))))) | |
799 ((listp macro) | |
800 (setcar (nthcdr idx macro) | |
801 (intern (char-to-string (+ ?0 elt))))) | |
802 ))) | |
803 ;;(setq break t))) | |
804 ((listp elt) | |
805 (vip-fixup-macro elt)) | |
806 ((symbolp elt) nil) | |
807 (t (setq break t))) | |
808 (setq idx (1+ idx)))) | |
809 | |
810 (if break | |
811 (error "Wrong type macro component, symbol-or-listp, %S" elt) | |
812 macro))) | |
813 | |
814 (defun vip-char-array-p (array) | |
815 (eval (cons 'and (mapcar 'numberp array)))) | |
816 | |
817 (defun vip-macro-to-events (macro-body) | |
818 (vconcat (mapcar 'vip-key-to-emacs-key macro-body))) | |
819 | |
820 | |
821 ;; check if vec is a vector of character symbols | |
822 (defun vip-char-symbol-sequence-p (vec) | |
823 (and | |
824 (sequencep vec) | |
825 (eval | |
826 (cons 'and | |
827 (mapcar | |
828 (function (lambda (elt) | |
829 (and (symbolp elt) (= (length (symbol-name elt)) 1)))) | |
830 vec))))) | |
831 | |
832 | |
833 ;; Check if vec is a vector of key-press events representing characters | |
834 ;; XEmacs only | |
835 (defun vip-event-vector-p (vec) | |
836 (and (vectorp vec) | |
837 (eval (cons 'and (mapcar '(lambda (elt) (if (eventp elt) t)) vec))))) | |
838 | |
839 | |
840 ;;; Reading fast key sequences | |
841 | |
842 ;; Assuming that CHAR was the first character in a fast succession of key | |
843 ;; strokes, read the rest. Return the vector of keys that was entered in | |
844 ;; this fast succession of key strokes. | |
845 ;; A fast keysequence is one that is terminated by a pause longer than | |
846 ;; vip-fast-keyseq-timeout. | |
847 (defun vip-read-fast-keysequence (event macro-alist) | |
848 (let ((lis (vector event)) | |
849 next-event) | |
850 (while (and (vip-fast-keysequence-p) | |
851 (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
|
852 (setq next-event (vip-read-key)) |
3d339e94aece
(vip-repeat-from-history-key): new default.
Michael Kifer <kifer@cs.stonybrook.edu>
parents:
12140
diff
changeset
|
853 ;;(setq next-event (vip-read-event)) |
10789 | 854 (or (vip-mouse-event-p next-event) |
855 (setq lis (vconcat lis (vector next-event))))) | |
856 lis)) | |
857 | |
858 | |
859 ;;; Keyboard macros in registers | |
860 | |
861 ;; sets register to last-kbd-macro carefully. | |
862 (defun vip-set-register-macro (reg) | |
863 (if (get-register reg) | |
864 (if (y-or-n-p "Register contains data. Overwrite? ") | |
865 () | |
866 (error | |
867 "Macro not saved in register. Can still be invoked via `C-x e'"))) | |
868 (set-register reg last-kbd-macro)) | |
869 | |
870 (defun vip-register-macro (count) | |
871 "Keyboard macros in registers - a modified \@ command." | |
872 (interactive "P") | |
873 (let ((reg (downcase (read-char)))) | |
874 (cond ((or (and (<= ?a reg) (<= reg ?z))) | |
875 (setq vip-last-macro-reg reg) | |
876 (if defining-kbd-macro | |
877 (progn | |
878 (end-kbd-macro) | |
879 (vip-set-register-macro reg)) | |
880 (execute-kbd-macro (get-register reg) count))) | |
881 ((or (= ?@ reg) (= ?\^j reg) (= ?\^m reg)) | |
882 (if vip-last-macro-reg | |
883 nil | |
884 (error "No previous kbd macro")) | |
885 (execute-kbd-macro (get-register vip-last-macro-reg) count)) | |
886 ((= ?\# reg) | |
887 (start-kbd-macro count)) | |
888 ((= ?! reg) | |
889 (setq reg (downcase (read-char))) | |
890 (if (or (and (<= ?a reg) (<= reg ?z))) | |
891 (progn | |
892 (setq vip-last-macro-reg reg) | |
893 (vip-set-register-macro reg)))) | |
894 (t | |
895 (error (format "`%c': Unknown register" reg)))))) | |
896 | |
897 | |
898 (defun vip-global-execute () | |
899 "Call last keyboad macro for each line in the region." | |
900 (if (> (point) (mark t)) (exchange-point-and-mark)) | |
901 (beginning-of-line) | |
902 (call-last-kbd-macro) | |
903 (while (< (point) (mark t)) | |
904 (forward-line 1) | |
905 (beginning-of-line) | |
906 (call-last-kbd-macro))) | |
907 | |
908 | |
909 (provide 'viper-macs) | |
910 | |
911 ;;; viper-macs.el ends here |