1 ;;; viper-macs.el --- functions implementing keyboard macros for Viper
3 ;; Copyright (C) 1994, 1995 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
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)
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.
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.
26 ;; Register holding last macro.
27 (defvar vip-last-macro-reg nil)
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)
38 ;; Internal var that passes info between start-kbd-macro and end-kbd-macro
40 (defvar vip-kbd-macro-parameters nil)
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.")
47 (defconst vip-fast-keyseq-timeout 200
48 "*Key sequence separated by no more than this many milliseconds is viewed as a macro, if such a macro is defined.
49 This also controls ESC-keysequences generated by keyboard function keys.")
52 (defvar vip-repeat-from-history-key 'f12
53 "Prefix key for invocation of vip-repeat-from-history function,
54 which repeats previous destructive commands from the history of such
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)'.")
68 macro-name macro-body map-args ins)
69 (save-window-excursion
70 (set-buffer vip-ex-work-buf)
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))
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))
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")))
100 (save-window-excursion
101 (set-buffer vip-ex-work-buf)
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))
116 ;; read arguments for ex-map
117 (defun ex-map-read-args (variant)
118 (let ((cursor-in-echo-area t)
120 temp key event message
121 macro-name macro-body args)
124 (setq args (concat (ex-get-inline-cmd-args ".*map[!]*[ \t]?" "\n\C-m")
126 temp (read-from-string args)
127 macro-name (car temp)
128 macro-body (car (read-from-string args (cdr temp))))
132 '("map: Macro name and body must be a quoted string or a vector"))))
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
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)
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
150 '(?\b ?\d '^? '^H (control h) (control \?) backspace delete))
151 (setq key-seq (subseq key-seq 0 (- (length key-seq) 2))))
154 variant (if (> (length key-seq) 0)
155 (prin1-to-string (vip-display-macro key-seq))
158 (setq event (vip-read-key))
159 ;;(setq event (vip-read-event))
161 (if (vip-mouse-event-p event)
163 (message "%s (No mouse---only keyboard keys, please)"
167 (vip-event-key event)))
169 (setq macro-name key-seq))
171 (if (= (length macro-name) 0)
172 (error "Can't map an empty macro name"))
173 (setq macro-name (vip-fixup-macro macro-name))
174 (if (vip-char-array-p macro-name)
175 (setq macro-name (vip-char-array-to-macro macro-name)))
178 (cond ((vip-char-array-p macro-body)
179 (setq macro-body (vip-char-array-to-macro macro-body)))
180 ((vectorp macro-body) nil)
181 (t (error "map: Invalid syntax in macro definition"))))
182 (setq cursor-in-echo-area nil)(sit-for 0) ; this overcomes xemacs tty bug
183 (cons macro-name macro-body)))
187 ;; read arguments for ex-unmap
188 (defun ex-unmap-read-args (variant)
189 (let ((cursor-in-echo-area t)
190 (macro-alist (if (string= variant "!")
191 vip-insert-kbd-macro-alist
192 vip-vi-kbd-macro-alist))
193 ;; these are disabled just in case, to avoid surprises when doing
195 vip-vi-kbd-minor-mode vip-insert-kbd-minor-mode
196 vip-emacs-kbd-minor-mode
197 vip-vi-intercept-minor-mode vip-insert-intercept-minor-mode
198 vip-emacs-intercept-minor-mode
200 key key-seq macro-name)
201 (setq macro-name (ex-get-inline-cmd-args ".*unma?p?[!]*[ \t]*"))
203 (if (> (length macro-name) 0)
205 (message ":unmap%s <Name>" variant) (sit-for 2)
208 (member key '(?\C-m ?\n (control m) (control j) return linefeed)))
209 (setq key-seq (vconcat key-seq (if key (vector key) [])))
210 ;; the only keys available for editing are these-- no help while there
213 '(?\b ?\d '^? '^H (control h) (control \?) backspace delete))
214 (setq key-seq (subseq key-seq 0 (- (length key-seq) 2))))
215 ((member key '(tab (control i) ?\t))
216 (setq key-seq (subseq key-seq 0 (1- (length key-seq))))
218 (format ":unmap%s %s"
219 variant (if (> (length key-seq) 0)
221 (vip-display-macro key-seq))
224 (vip-do-sequence-completion key-seq macro-alist message))
227 (format ":unmap%s %s"
228 variant (if (> (length key-seq) 0)
230 (vip-display-macro key-seq))
233 (setq event (vip-read-key))
234 ;;(setq event (vip-read-event))
236 (if (vip-mouse-event-p event)
238 (message "%s (No mouse---only keyboard keys, please)"
242 (vip-event-key event)))
244 (setq macro-name key-seq))
246 (if (= (length macro-name) 0)
247 (error "Can't unmap an empty macro name"))
249 ;; convert macro names into vector, if starts with a `['
250 (if (memq (elt macro-name 0) '(?\[ ?\"))
251 (car (read-from-string macro-name))
252 (vconcat macro-name))
256 (defun vip-end-mapping-kbd-macro (&optional ignore)
257 "Terminate kbd macro."
259 (define-key vip-vi-intercept-map "\C-x)" nil)
260 (define-key vip-insert-intercept-map "\C-x)" nil)
261 (define-key vip-emacs-intercept-map "\C-x)" nil)
262 (if (and (not ignore)
263 (or (not vip-kbd-macro-parameters)
264 (not defining-kbd-macro)))
265 (error "Not mapping a kbd-macro"))
266 (let ((mod-char (nth 1 vip-kbd-macro-parameters))
267 (ins (nth 0 vip-kbd-macro-parameters))
268 (macro-name (nth 2 vip-kbd-macro-parameters))
269 (macro-body (nth 3 vip-kbd-macro-parameters)))
270 (setq vip-kbd-macro-parameters nil)
274 (setq macro-body (vip-events-to-macro last-kbd-macro))
275 ;; always go back to Vi, since this is where we started
277 (vip-change-state-to-vi)))
279 (vip-record-kbd-macro macro-name
280 (if ins 'insert-state 'vi-state)
281 (vip-display-macro macro-body))
283 (ex-fixup-history (format "map%s %S %S" mod-char
284 (vip-display-macro macro-name)
285 (vip-display-macro macro-body)))
290 (defadvice start-kbd-macro (after vip-kbd-advice activate)
291 "Remove Viper's intercepting bindings for C-x ).
292 This may be needed if the previous `:map' command terminated abnormally."
293 (define-key vip-vi-intercept-map "\C-x)" nil)
294 (define-key vip-insert-intercept-map "\C-x)" nil)
295 (define-key vip-emacs-intercept-map "\C-x)" nil))
299 ;;; Recording, unrecording, executing
301 ;; accepts as macro names: strings and vectors.
302 ;; strings must be strings of characters; vectors must be vectors of keys
303 ;; in canonic form. the canonic form is essentially the form used in XEmacs
304 (defun vip-record-kbd-macro (macro-name state macro-body &optional scope)
305 "Record a Vi macro. Can be used in `.vip' file to define permanent macros.
306 MACRO-NAME is a string of characters or a vector of keys. STATE is
307 either `vi-state' or `insert-state'. It specifies the Viper state in which to
308 define the macro. MACRO-BODY is a string that represents the keyboard macro.
309 Optional SCOPE says whether the macro should be global \(t\), mode-specific
310 \(a major-mode symbol\), or buffer-specific \(buffer name, a string\).
311 If SCOPE is nil, the user is asked to specify the scope."
312 (let* (state-name keymap
314 (cond ((eq state 'vi-state)
315 (setq state-name "Vi state"
316 keymap vip-vi-kbd-map)
317 'vip-vi-kbd-macro-alist)
318 ((memq state '(insert-state replace-state))
319 (setq state-name "Insert state"
320 keymap vip-insert-kbd-map)
321 'vip-insert-kbd-macro-alist)
323 (setq state-name "Emacs state"
324 keymap vip-emacs-kbd-map)
325 'vip-emacs-kbd-macro-alist)
327 new-elt old-elt old-sub-elt msg
330 (if (= (length macro-name) 0)
331 (error "Can't map an empty macro name"))
333 ;; Macro-name is usually a vector. However, command history or macros
334 ;; recorded in ~/.vip may be recorded as strings. So, convert to vectors.
335 (setq macro-name (vip-fixup-macro macro-name))
336 (if (vip-char-array-p macro-name)
337 (setq macro-name (vip-char-array-to-macro macro-name)))
338 (setq macro-body (vip-fixup-macro macro-body))
339 (if (vip-char-array-p macro-body)
340 (setq macro-body (vip-char-array-to-macro macro-body)))
342 ;; don't ask if scope is given and is of the right type
345 (and scope (symbolp scope))
351 "Map this macro for buffer `%s' only? "
355 "%S is mapped to %s for %s in `%s'"
356 (vip-display-macro macro-name)
357 (vip-abbreviate-string
360 (setq temp (vip-display-macro macro-body)))
362 (if (stringp temp) " ....\"" " ....]"))
363 state-name (buffer-name)))
367 "Map this macro for the major mode `%S' only? "
371 "%S is mapped to %s for %s in `%S'"
372 (vip-display-macro macro-name)
373 (vip-abbreviate-string
376 (setq temp (vip-display-macro macro-body)))
378 (if (stringp macro-body) " ....\"" " ....]"))
379 state-name major-mode))
384 "%S is globally mapped to %s in %s"
385 (vip-display-macro macro-name)
386 (vip-abbreviate-string
389 (setq temp (vip-display-macro macro-body)))
391 (if (stringp macro-body) " ....\"" " ....]"))
394 (if (y-or-n-p (format "Save this macro in %s? "
395 (abbreviate-file-name vip-custom-file-name)))
396 (vip-save-string-in-file
397 (format "\n(vip-record-kbd-macro %S '%S %s '%S)"
398 (vip-display-macro macro-name)
400 ;; if we don't let vector macro-body through %S,
401 ;; the symbols `\.' `\[' etc will be converted into
402 ;; characters, causing invalid read error on recorded
404 ;; I am not sure is macro-body can still be a string at
405 ;; this point, but I am preserving this option anyway.
406 (if (vectorp macro-body)
407 (format "%S" macro-body)
410 vip-custom-file-name))
417 (cond ((eq scope t) (list nil nil (cons t nil)))
419 (list nil (list (cons scope nil)) (cons t nil)))
421 (list (list (cons scope nil)) nil (cons t nil))))))
422 (setq old-elt (assoc macro-name (eval macro-alist-var)))
426 ;; insert new-elt in macro-alist-var and keep the list sorted
429 (vector (vip-key-to-emacs-key (aref macro-name 0)))
430 'vip-exec-mapped-kbd-macro)
431 (setq lis (eval macro-alist-var))
432 (while (and lis (string< (vip-array-to-string (car (car lis)))
433 (vip-array-to-string macro-name)))
434 (setq lis2 (cons (car lis) lis2))
435 (setq lis (cdr lis)))
437 (setq lis2 (reverse lis2))
438 (set macro-alist-var (append lis2 (cons new-elt lis)))
439 (setq old-elt new-elt)))
441 (cond ((eq scope t) (vip-kbd-global-pair old-elt))
442 ((symbolp scope) (assoc scope (vip-kbd-mode-alist old-elt)))
443 ((stringp scope) (assoc scope (vip-kbd-buf-alist old-elt)))))
445 (setcdr old-sub-elt macro-body)
446 (cond ((symbolp scope) (setcar (cdr (cdr old-elt))
447 (cons (cons scope macro-body)
448 (vip-kbd-mode-alist old-elt))))
449 ((stringp scope) (setcar (cdr old-elt)
450 (cons (cons scope macro-body)
451 (vip-kbd-buf-alist old-elt))))))
456 ;; macro name must be a vector of vip-style keys
457 (defun vip-unrecord-kbd-macro (macro-name state)
458 "Delete macro MACRO-NAME from Viper STATE.
459 MACRO-NAME must be a vector of vip-style keys. This command is used by Viper
460 internally, but the user can also use it in ~/.vip to delete pre-defined macros
461 supplied with Viper. The best way to avoid mistakes in macro names to be passed
462 to this function is to use vip-describe-kbd-macros and copy the name from
464 (let* (state-name keymap
466 (cond ((eq state 'vi-state)
467 (setq state-name "Vi state"
468 keymap vip-vi-kbd-map)
469 'vip-vi-kbd-macro-alist)
470 ((memq state '(insert-state replace-state))
471 (setq state-name "Insert state"
472 keymap vip-insert-kbd-map)
473 'vip-insert-kbd-macro-alist)
475 (setq state-name "Emacs state"
476 keymap vip-emacs-kbd-map)
477 'vip-emacs-kbd-macro-alist)
479 buf-mapping mode-mapping global-mapping
480 macro-pair macro-entry)
482 ;; Macro-name is usually a vector. However, command history or macros
483 ;; recorded in ~/.vip may appear as strings. So, convert to vectors.
484 (setq macro-name (vip-fixup-macro macro-name))
485 (if (vip-char-array-p macro-name)
486 (setq macro-name (vip-char-array-to-macro macro-name)))
488 (setq macro-entry (assoc macro-name (eval macro-alist-var)))
489 (if (= (length macro-name) 0)
490 (error "Can't unmap an empty macro name"))
491 (if (null macro-entry)
492 (error "%S is not mapped to a macro for %s in `%s'"
493 (vip-display-macro macro-name)
494 state-name (buffer-name)))
496 (setq buf-mapping (vip-kbd-buf-pair macro-entry)
497 mode-mapping (vip-kbd-mode-pair macro-entry)
498 global-mapping (vip-kbd-global-pair macro-entry))
500 (cond ((and (cdr buf-mapping)
501 (or (and (not (cdr mode-mapping)) (not (cdr global-mapping)))
503 (format "Unmap %S for `%s' only? "
504 (vip-display-macro macro-name)
506 (setq macro-pair buf-mapping)
507 (message "%S is unmapped for %s in `%s'"
508 (vip-display-macro macro-name)
509 state-name (buffer-name)))
510 ((and (cdr mode-mapping)
511 (or (not (cdr global-mapping))
513 (format "Unmap %S for the major mode `%S' only? "
514 (vip-display-macro macro-name)
516 (setq macro-pair mode-mapping)
517 (message "%S is unmapped for %s in %S"
518 (vip-display-macro macro-name) state-name major-mode))
519 ((cdr (setq macro-pair (vip-kbd-global-pair macro-entry)))
521 "Global mapping of %S for %s is removed"
522 (vip-display-macro macro-name) state-name))
523 (t (error "%S is not mapped to a macro for %s in `%s'"
524 (vip-display-macro macro-name)
525 state-name (buffer-name))))
526 (setcdr macro-pair nil)
527 (or (cdr buf-mapping)
531 (set macro-alist-var (delq macro-entry (eval macro-alist-var)))
532 (if (vip-can-release-key (aref macro-name 0)
533 (eval macro-alist-var))
536 (vector (vip-key-to-emacs-key (aref macro-name 0)))
541 ;; Check if MACRO-ALIST has an entry for a macro name starting with
542 ;; CHAR. If not, this indicates that the binding for this char
543 ;; in vip-vi/insert-kbd-map can be released.
544 (defun vip-can-release-key (char macro-alist)
545 (let ((lis macro-alist)
549 (while (and lis can-release)
550 (setq macro-name (car (car lis)))
551 (if (eq char (aref macro-name 0))
552 (setq can-release nil))
553 (setq lis (cdr lis)))
557 (defun vip-exec-mapped-kbd-macro (count)
558 "Dispatch kbd macro."
560 (let* ((macro-alist (cond ((eq vip-current-state 'vi-state)
561 vip-vi-kbd-macro-alist)
562 ((memq vip-current-state
563 '(insert-state replace-state))
564 vip-insert-kbd-macro-alist)
566 vip-emacs-kbd-macro-alist)))
567 (unmatched-suffix "")
568 ;; Macros and keys are executed with other macros turned off
569 ;; For macros, this is done to avoid macro recursion
570 vip-vi-kbd-minor-mode vip-insert-kbd-minor-mode
571 vip-emacs-kbd-minor-mode
572 next-best-match keyseq event-seq
573 macro-first-char macro-alist-elt macro-body
576 (setq macro-first-char last-command-event
577 event-seq (vip-read-fast-keysequence macro-first-char macro-alist)
578 keyseq (vip-events-to-macro event-seq)
579 macro-alist-elt (assoc keyseq macro-alist)
580 next-best-match (vip-find-best-matching-macro macro-alist keyseq))
582 (if (null macro-alist-elt)
583 (setq macro-alist-elt (car next-best-match)
584 unmatched-suffix (subseq event-seq (cdr next-best-match))))
586 (cond ((null macro-alist-elt))
587 ((setq macro-body (vip-kbd-buf-definition macro-alist-elt)))
588 ((setq macro-body (vip-kbd-mode-definition macro-alist-elt)))
589 ((setq macro-body (vip-kbd-global-definition macro-alist-elt))))
591 ;; when defining keyboard macro, don't use the macro mappings
592 (if (and macro-body (not defining-kbd-macro))
593 ;; block cmd executed as part of a macro from entering command history
594 (let ((command-history command-history))
595 (setq vip-this-kbd-macro (car macro-alist-elt))
596 (execute-kbd-macro (vip-macro-to-events macro-body) count)
597 (setq vip-this-kbd-macro nil
598 vip-last-kbd-macro (car macro-alist-elt))
599 (vip-set-unread-command-events unmatched-suffix))
600 ;; If not a macro, or the macro is suppressed while defining another
601 ;; macro, put keyseq back on the event queue
602 (vip-set-unread-command-events event-seq)
603 ;; if the user typed arg, then use it if prefix arg is not set by
604 ;; some other command (setting prefix arg can happen if we do, say,
605 ;; 2dw and there is a macro starting with 2. Then control will go to
607 (or prefix-arg (setq prefix-arg count))
608 (setq command (key-binding (read-key-sequence nil)))
609 (if (commandp command)
610 (command-execute command)
616 ;;; Displaying and completing macros
618 (defun vip-describe-kbd-macros ()
619 "Show currently defined keyboard macros."
621 (with-output-to-temp-buffer " *vip-info*"
622 (princ "Macros in Vi state:\n===================\n")
623 (mapcar 'vip-describe-one-macro vip-vi-kbd-macro-alist)
624 (princ "\n\nMacros in Insert and Replace states:\n====================================\n")
625 (mapcar 'vip-describe-one-macro vip-insert-kbd-macro-alist)
626 (princ "\n\nMacros in Emacs state:\n======================\n")
627 (mapcar 'vip-describe-one-macro vip-emacs-kbd-macro-alist)
630 (defun vip-describe-one-macro (macro)
631 (princ (format "\n *** Mappings for %S:\n ------------\n"
632 (vip-display-macro (car macro))))
633 (princ " ** Buffer-specific:")
634 (if (vip-kbd-buf-alist macro)
635 (mapcar 'vip-describe-one-macro-elt (vip-kbd-buf-alist macro))
637 (princ "\n ** Mode-specific:")
638 (if (vip-kbd-mode-alist macro)
639 (mapcar 'vip-describe-one-macro-elt (vip-kbd-mode-alist macro))
641 (princ "\n ** Global:")
642 (if (vip-kbd-global-definition macro)
645 (cdr (vip-kbd-global-pair macro))))
649 (defun vip-describe-one-macro-elt (elt)
650 (let ((name (car elt))
652 (princ (format "\n * %S:\n %S\n" name defn))))
656 ;; check if SEQ is a prefix of some car of an element in ALIST
657 (defun vip-keyseq-is-a-possible-macro (seq alist)
658 (let ((converted-seq (vip-events-to-macro seq)))
661 (function (lambda (elt)
662 (vip-prefix-subseq-p converted-seq elt)))
663 (vip-this-buffer-macros alist))))))
665 ;; whether SEQ1 is a prefix of SEQ2
666 (defun vip-prefix-subseq-p (seq1 seq2)
667 (let ((len1 (length seq1))
668 (len2 (length seq2)))
670 (equal seq1 (subseq seq2 0 len1)))))
672 ;; find the longest common prefix
673 (defun vip-common-seq-prefix (&rest seqs)
674 (let* ((first (car seqs))
679 (if (= (length seqs) 0)
681 (setq len (apply 'min (mapcar 'length seqs))))
684 (mapcar (function (lambda (s)
685 (equal (elt first idx)
688 (setq pref (vconcat pref (vector (elt first idx)))))
692 ;; get all sequences that match PREFIX from a given A-LIST
693 (defun vip-extract-matching-alist-members (pref alist)
694 (delq nil (mapcar (function (lambda (elt)
695 (if (vip-prefix-subseq-p pref elt)
697 (vip-this-buffer-macros alist))))
699 (defun vip-do-sequence-completion (seq alist compl-message)
700 (let* ((matches (vip-extract-matching-alist-members seq alist))
701 (new-seq (apply 'vip-common-seq-prefix matches))
703 (cond ((and (equal seq new-seq) (= (length matches) 1))
704 (message "%s (Sole completion)" compl-message)
707 (message "%s (No match)" compl-message)
710 ((member seq matches)
711 (message "%s (Complete, but not unique)" compl-message)
713 (vip-display-vector-completions matches))
715 (vip-display-vector-completions matches)))
719 (defun vip-display-vector-completions (list)
720 (with-output-to-temp-buffer "*Completions*"
721 (display-completion-list
722 (mapcar 'prin1-to-string
723 (mapcar 'vip-display-macro list)))))
727 ;; alist is the alist of macros
728 ;; str is the fast key sequence entered
729 ;; returns: (matching-macro-def . unmatched-suffix-start-index)
730 (defun vip-find-best-matching-macro (alist str)
733 (str-len (length str))
734 match unmatched-start-idx found macro-def)
735 (while (and (not found) lis)
736 (setq macro-def (car lis)
737 def-len (length (car macro-def)))
738 (if (and (>= str-len def-len)
739 (equal (car macro-def) (subseq str 0 def-len)))
740 (if (or (vip-kbd-buf-definition macro-def)
741 (vip-kbd-mode-definition macro-def)
742 (vip-kbd-global-definition macro-def))
745 (setq lis (cdr lis)))
748 (setq match macro-def
749 unmatched-start-idx def-len)
751 unmatched-start-idx 0))
753 (cons match unmatched-start-idx)))
757 ;; returns a list of names of macros defined for the current buffer
758 (defun vip-this-buffer-macros (macro-alist)
763 (if (or (vip-kbd-buf-definition elt)
764 (vip-kbd-mode-definition elt)
765 (vip-kbd-global-definition elt))
768 (setq candidates (delq nil candidates))))
771 ;; if seq of Viper key symbols (representing a macro) can be converted to a
772 ;; string--do so. Otherwise, do nothing.
773 (defun vip-display-macro (macro-name)
774 (cond ((vip-char-symbol-sequence-p macro-name)
775 (mapconcat 'symbol-name macro-name ""))
776 ((vip-char-array-p macro-name)
777 (mapconcat 'char-to-string macro-name ""))
780 (defun vip-events-to-macro (event-seq)
781 (vconcat (mapcar 'vip-event-key event-seq)))
783 ;; convert strings or arrays of characters to Viper macro form
784 (defun vip-char-array-to-macro (array)
785 (let ((vec (vconcat array))
788 (setq macro (mapcar 'character-to-event vec))
790 (vconcat (mapcar 'vip-event-key macro))))
792 ;; For macros bodies and names, goes over and checks if all members are
793 ;; names of keys (actually, it only checks if they are symbols or lists
794 ;; if a digit is found, it is converted into a symbol (0 -> \0, etc).
795 ;; If not list or vector, doesn't change its argument
796 (defun vip-fixup-macro (macro)
797 (let ((len (length macro))
800 (if (or (vectorp macro) (listp macro))
801 (while (and (< idx len) (not break))
802 (setq elt (elt macro idx))
805 (if (and (<= 0 elt) (<= elt 9))
806 (cond ((arrayp macro)
809 (intern (char-to-string (+ ?0 elt)))))
811 (setcar (nthcdr idx macro)
812 (intern (char-to-string (+ ?0 elt)))))
816 (vip-fixup-macro elt))
819 (setq idx (1+ idx))))
822 (error "Wrong type macro component, symbol-or-listp, %S" elt)
825 (defun vip-char-array-p (array)
826 (eval (cons 'and (mapcar 'numberp array))))
828 (defun vip-macro-to-events (macro-body)
829 (vconcat (mapcar 'vip-key-to-emacs-key macro-body)))
832 ;; check if vec is a vector of character symbols
833 (defun vip-char-symbol-sequence-p (vec)
839 (function (lambda (elt)
840 (and (symbolp elt) (= (length (symbol-name elt)) 1))))
844 ;; Check if vec is a vector of key-press events representing characters
846 (defun vip-event-vector-p (vec)
848 (eval (cons 'and (mapcar '(lambda (elt) (if (eventp elt) t)) vec)))))
851 ;;; Reading fast key sequences
853 ;; Assuming that CHAR was the first character in a fast succession of key
854 ;; strokes, read the rest. Return the vector of keys that was entered in
855 ;; this fast succession of key strokes.
856 ;; A fast keysequence is one that is terminated by a pause longer than
857 ;; vip-fast-keyseq-timeout.
858 (defun vip-read-fast-keysequence (event macro-alist)
859 (let ((lis (vector event))
861 (while (and (vip-fast-keysequence-p)
862 (vip-keyseq-is-a-possible-macro lis macro-alist))
863 (setq next-event (vip-read-key))
864 ;;(setq next-event (vip-read-event))
865 (or (vip-mouse-event-p next-event)
866 (setq lis (vconcat lis (vector next-event)))))
870 ;;; Keyboard macros in registers
872 ;; sets register to last-kbd-macro carefully.
873 (defun vip-set-register-macro (reg)
874 (if (get-register reg)
875 (if (y-or-n-p "Register contains data. Overwrite? ")
878 "Macro not saved in register. Can still be invoked via `C-x e'")))
879 (set-register reg last-kbd-macro))
881 (defun vip-register-macro (count)
882 "Keyboard macros in registers - a modified \@ command."
884 (let ((reg (downcase (read-char))))
885 (cond ((or (and (<= ?a reg) (<= reg ?z)))
886 (setq vip-last-macro-reg reg)
887 (if defining-kbd-macro
890 (vip-set-register-macro reg))
891 (execute-kbd-macro (get-register reg) count)))
892 ((or (= ?@ reg) (= ?\^j reg) (= ?\^m reg))
893 (if vip-last-macro-reg
895 (error "No previous kbd macro"))
896 (execute-kbd-macro (get-register vip-last-macro-reg) count))
898 (start-kbd-macro count))
900 (setq reg (downcase (read-char)))
901 (if (or (and (<= ?a reg) (<= reg ?z)))
903 (setq vip-last-macro-reg reg)
904 (vip-set-register-macro reg))))
906 (error (format "`%c': Unknown register" reg))))))
909 (defun vip-global-execute ()
910 "Call last keyboad macro for each line in the region."
911 (if (> (point) (mark t)) (exchange-point-and-mark))
913 (call-last-kbd-macro)
914 (while (< (point) (mark t))
917 (call-last-kbd-macro)))
920 (provide 'viper-macs)
922 ;;; viper-macs.el ends here