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