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