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