1 ;;; viper-macs.el -- functions implementing keyboard macros for Viper
3 ;; This file is part of GNU Emacs.
5 ;; GNU Emacs is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with GNU Emacs; see the file COPYING. If not, write to
17 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;; Register holding last macro.
25 (defvar vip-last-macro-reg nil)
27 ;; format of the elements of kbd alists:
28 ;; (name ((buf . macr)...(buf . macr)) ((maj-mode . macr)...) (t . macr))
29 ;; kbd macro alist for Vi state
30 (defvar vip-vi-kbd-macro-alist nil)
31 ;; same for insert/replace state
32 (defvar vip-insert-kbd-macro-alist nil)
33 ;; same for emacs state
34 (defvar vip-emacs-kbd-macro-alist nil)
36 ;; Internal var that passes info between start-kbd-macro and end-kbd-macro
38 (defvar vip-kbd-macro-parameters nil)
40 (defvar vip-this-kbd-macro nil
41 "Vector of keys representing the name of currently running Viper kbd macro.")
42 (defvar vip-last-kbd-macro nil
43 "Vector of keys representing the name of last Viper keyboard macro.")
45 (defconst vip-fast-keyseq-timeout 200
46 "*Key sequences separated by this many miliseconds are interpreted as a macro, if such a macro is defined.
47 This also controls ESC-keysequences generated by keyboard function keys.")
50 (defvar vip-repeat-from-history-key 'f1
51 "Prefix key for invocation of vip-repeat-from-history function,
52 which repeats previous destructive commands from the history of such
54 This function can then be invoked as <this-key> 1 or <this-key> 2.
55 The notation for these keys is borrowed from XEmacs. Basically,
56 a key is a symbol, e.g., `a', `\\1', `f2', etc., or a list, e.g.,
57 `(meta control f1)'.")
66 macro-name macro-body map-args ins)
67 (save-window-excursion
68 (set-buffer vip-ex-work-buf)
74 (setq map-args (ex-map-read-args mod-char)
75 macro-name (car map-args)
76 macro-body (cdr map-args))
77 (setq vip-kbd-macro-parameters (list ins mod-char macro-name macro-body))
79 (vip-end-mapping-kbd-macro 'ignore)
80 (ex-fixup-history (format "map%s %S" mod-char
81 (vip-display-macro macro-name)))
82 ;; if defining macro for insert, switch there for authentic WYSIWYG
83 (if ins (vip-change-state-to-insert))
85 (define-key vip-vi-intercept-map "\C-x)" 'vip-end-mapping-kbd-macro)
86 (define-key vip-insert-intercept-map "\C-x)" 'vip-end-mapping-kbd-macro)
87 (define-key vip-emacs-intercept-map "\C-x)" 'vip-end-mapping-kbd-macro)
88 (message "Mapping %S in %s state. Hit `C-x )' to complete the mapping"
89 (vip-display-macro macro-name)
90 (if ins "Insert" "Vi")))
98 (save-window-excursion
99 (set-buffer vip-ex-work-buf)
106 (setq macro-name (ex-unmap-read-args mod-char))
107 (setq temp (vip-fixup-macro (vconcat macro-name))) ;; copy and fixup
108 (ex-fixup-history (format "unmap%s %S" mod-char
109 (vip-display-macro temp)))
110 (vip-unrecord-kbd-macro macro-name (if ins 'insert-state 'vi-state))
114 ;; read arguments for ex-map
115 (defun ex-map-read-args (variant)
116 (let ((cursor-in-echo-area t)
118 temp key event message
119 macro-name macro-body args)
122 (setq args (concat (ex-get-inline-cmd-args ".*map[!]*[ \t]?" "\n\C-m")
124 temp (read-from-string args)
125 macro-name (car temp)
126 macro-body (car (read-from-string args (cdr temp))))
130 '("map: Macro name and body must be a quoted string or a vector"))))
132 ;; We expect macro-name to be a vector, a string, or a quoted string.
133 ;; In the second case, it will emerge as a symbol when read from
134 ;; the above read-from-string. So we need to convert it into a string
136 (cond ((vectorp macro-name) nil)
137 ((stringp macro-name)
138 (setq macro-name (vconcat macro-name)))
139 (t (setq macro-name (vconcat (prin1-to-string macro-name)))))
140 (message ":map%s <Name>" variant)(sit-for 2)
143 '(?\C-m ?\n (control m) (control j) return linefeed)))
144 (setq key-seq (vconcat key-seq (if key (vector key) [])))
145 ;; the only keys available for editing are these-- no help while there
148 '(?\b ?\d '^? '^H (control h) (control \?) backspace delete))
149 (setq key-seq (subseq key-seq 0 (- (length key-seq) 2))))
152 variant (if (> (length key-seq) 0)
153 (prin1-to-string (vip-display-macro key-seq))
156 (setq event (vip-read-event))
158 (if (vip-mouse-event-p event)
160 (message "%s (No mouse---only keyboard keys, please)"
164 (vip-event-key event)))
166 (setq macro-name key-seq))
168 (if (= (length macro-name) 0)
169 (error "Can't map an empty macro name"))
170 (setq macro-name (vip-fixup-macro macro-name))
171 (if (vip-char-array-p macro-name)
172 (setq macro-name (vip-char-array-to-macro macro-name)))
175 (cond ((vip-char-array-p macro-body)
176 (setq macro-body (vip-char-array-to-macro macro-body)))
177 ((vectorp macro-body) nil)
178 (t (error "map: Invalid syntax in macro definition"))))
179 (cons macro-name macro-body)
184 ;; read arguments for ex-unmap
185 (defun ex-unmap-read-args (variant)
186 (let ((cursor-in-echo-area t)
187 (macro-alist (if (string= variant "!")
188 vip-insert-kbd-macro-alist
189 vip-vi-kbd-macro-alist))
190 ;; these are disabled just in case, to avoid surprises when doing
192 vip-vi-kbd-minor-mode vip-insert-kbd-minor-mode
193 vip-emacs-kbd-minor-mode
194 vip-vi-intercept-minor-mode vip-insert-intercept-minor-mode
195 vip-emacs-intercept-minor-mode
197 key key-seq macro-name)
198 (setq macro-name (ex-get-inline-cmd-args ".*unma?p?[!]*[ \t]*"))
200 (if (> (length macro-name) 0)
202 (message ":unmap%s <Name>" variant) (sit-for 2)
205 (member key '(?\C-m ?\n (control m) (control j) return linefeed)))
206 (setq key-seq (vconcat key-seq (if key (vector key) [])))
207 ;; the only keys available for editing are these-- no help while there
210 '(?\b ?\d '^? '^H (control h) (control \?) backspace delete))
211 (setq key-seq (subseq key-seq 0 (- (length key-seq) 2))))
212 ((member key '(tab (control i) ?\t))
213 (setq key-seq (subseq key-seq 0 (1- (length key-seq))))
215 (format ":unmap%s %s"
216 variant (if (> (length key-seq) 0)
218 (vip-display-macro key-seq))
221 (vip-do-sequence-completion key-seq macro-alist message))
224 (format ":unmap%s %s"
225 variant (if (> (length key-seq) 0)
227 (vip-display-macro key-seq))
230 (setq event (vip-read-event))
232 (if (vip-mouse-event-p event)
234 (message "%s (No mouse---only keyboard keys, please)"
238 (vip-event-key event)))
240 (setq macro-name key-seq))
242 (if (= (length macro-name) 0)
243 (error "Can't unmap an empty macro name"))
245 ;; convert macro names into vector, if starts with a `['
246 (if (memq (elt macro-name 0) '(?\[ ?\"))
247 (car (read-from-string macro-name))
248 (vconcat macro-name))
253 (defun vip-end-mapping-kbd-macro (&optional ignore)
254 "Terminate kbd macro."
256 (define-key vip-vi-intercept-map "\C-x)" nil)
257 (define-key vip-insert-intercept-map "\C-x)" nil)
258 (define-key vip-emacs-intercept-map "\C-x)" nil)
259 (if (and (not ignore)
260 (or (not vip-kbd-macro-parameters)
261 (not defining-kbd-macro)))
262 (error "Not mapping a kbd-macro"))
263 (let ((mod-char (nth 1 vip-kbd-macro-parameters))
264 (ins (nth 0 vip-kbd-macro-parameters))
265 (macro-name (nth 2 vip-kbd-macro-parameters))
266 (macro-body (nth 3 vip-kbd-macro-parameters)))
267 (setq vip-kbd-macro-parameters nil)
271 (setq macro-body (vip-events-to-macro last-kbd-macro))
272 ;; always go back to Vi, since this is where we started
274 (vip-change-state-to-vi)))
276 (vip-record-kbd-macro macro-name
277 (if ins 'insert-state 'vi-state)
278 (vip-display-macro macro-body))
280 (ex-fixup-history (format "map%s %S %S" mod-char
281 (vip-display-macro macro-name)
282 (vip-display-macro macro-body)))
287 (defadvice start-kbd-macro (after vip-kbd-advice activate)
288 "Remove Viper's intercepting bindings for C-x ).
289 This may be needed if the previous `:map' command terminated abnormally."
290 (define-key vip-vi-intercept-map "\C-x)" nil)
291 (define-key vip-insert-intercept-map "\C-x)" nil)
292 (define-key vip-emacs-intercept-map "\C-x)" nil))
296 ;;; Recording, unrecording, executing
298 ;; accepts as macro names: strings and vectors.
299 ;; strings must be strings of characters; vectors must be vectors of keys
300 ;; in canonic form. the canonic form is essentially the form used in XEmacs
301 (defun vip-record-kbd-macro (macro-name state macro-body &optional scope)
302 "Record a Vi macro. Can be used in `.vip' file to define permanent macros.
303 MACRO-NAME is a string of characters or a vector of keys. STATE is
304 either `vi-state' or `insert-state'. It specifies the Viper state in which to
305 define the macro. MACRO-BODY is a string that represents the keyboard macro.
306 Optional SCOPE says whether the macro should be global \(t\), mode-specific
307 \(a major-mode symbol\), or buffer-specific \(buffer name, a string\).
308 If SCOPE is nil, the user is asked to specify the scope."
309 (let* (state-name keymap
311 (cond ((eq state 'vi-state)
312 (setq state-name "Vi state"
313 keymap vip-vi-kbd-map)
314 'vip-vi-kbd-macro-alist)
315 ((memq state '(insert-state replace-state))
316 (setq state-name "Insert state"
317 keymap vip-insert-kbd-map)
318 'vip-insert-kbd-macro-alist)
320 (setq state-name "Emacs state"
321 keymap vip-emacs-kbd-map)
322 'vip-emacs-kbd-macro-alist)
324 new-elt old-elt old-sub-elt msg
327 (if (= (length macro-name) 0)
328 (error "Can't map an empty macro name"))
330 ;; Macro-name is usually a vector. However, command history or macros
331 ;; recorded in ~/.vip may be recorded as strings. So, convert to vectors.
332 (setq macro-name (vip-fixup-macro macro-name))
333 (if (vip-char-array-p macro-name)
334 (setq macro-name (vip-char-array-to-macro macro-name)))
335 (setq macro-body (vip-fixup-macro macro-body))
336 (if (vip-char-array-p macro-body)
337 (setq macro-body (vip-char-array-to-macro macro-body)))
339 ;; don't ask if scope is given and is of the right type
342 (and scope (symbolp scope))
348 "Map this macro for buffer `%s' only? "
352 "%S is mapped to %s for %s in `%s'"
353 (vip-display-macro macro-name)
354 (vip-abbreviate-string
357 (setq temp (vip-display-macro macro-body)))
359 (if (stringp temp) " ....\"" " ....]"))
360 state-name (buffer-name)))
364 "Map this macro for the major mode `%S' only? "
368 "%S is mapped to %s for %s in `%S'"
369 (vip-display-macro macro-name)
370 (vip-abbreviate-string
373 (setq temp (vip-display-macro macro-body)))
375 (if (stringp macro-body) " ....\"" " ....]"))
376 state-name major-mode))
381 "%S is globally mapped to %s in %s"
382 (vip-display-macro macro-name)
383 (vip-abbreviate-string
386 (setq temp (vip-display-macro macro-body)))
388 (if (stringp macro-body) " ....\"" " ....]"))
391 (if (y-or-n-p (format "Save this macro in %s? "
392 (abbreviate-file-name vip-custom-file-name)))
393 (vip-save-string-in-file
394 (format "\n(vip-record-kbd-macro %S '%S %s '%S)"
395 (vip-display-macro macro-name)
396 state macro-body scope)
397 vip-custom-file-name))
404 (cond ((eq scope t) (list nil nil (cons t nil)))
406 (list nil (list (cons scope nil)) (cons t nil)))
408 (list (list (cons scope nil)) nil (cons t nil))))))
409 (setq old-elt (assoc macro-name (eval macro-alist-var)))
413 ;; insert new-elt in macro-alist-var and keep the list sorted
416 (vector (vip-key-to-emacs-key (aref macro-name 0)))
417 'vip-exec-mapped-kbd-macro)
418 (setq lis (eval macro-alist-var))
419 (while (and lis (string< (vip-array-to-string (car (car lis)))
420 (vip-array-to-string macro-name)))
421 (setq lis2 (cons (car lis) lis2))
422 (setq lis (cdr lis)))
424 (setq lis2 (reverse lis2))
425 (set macro-alist-var (append lis2 (cons new-elt lis)))
426 (setq old-elt new-elt)))
428 (cond ((eq scope t) (vip-kbd-global-pair old-elt))
429 ((symbolp scope) (assoc scope (vip-kbd-mode-alist old-elt)))
430 ((stringp scope) (assoc scope (vip-kbd-buf-alist old-elt)))))
432 (setcdr old-sub-elt macro-body)
433 (cond ((symbolp scope) (setcar (cdr (cdr old-elt))
434 (cons (cons scope macro-body)
435 (vip-kbd-mode-alist old-elt))))
436 ((stringp scope) (setcar (cdr old-elt)
437 (cons (cons scope macro-body)
438 (vip-kbd-buf-alist old-elt))))))
443 ;; macro name must be a vector of vip-style keys
444 (defun vip-unrecord-kbd-macro (macro-name state)
445 (let* (state-name keymap
447 (cond ((eq state 'vi-state)
448 (setq state-name "Vi state"
449 keymap vip-vi-kbd-map)
450 'vip-vi-kbd-macro-alist)
451 ((memq state '(insert-state replace-state))
452 (setq state-name "Insert state"
453 keymap vip-insert-kbd-map)
454 'vip-insert-kbd-macro-alist)
456 (setq state-name "Emacs state"
457 keymap vip-emacs-kbd-map)
458 'vip-emacs-kbd-macro-alist)
460 buf-mapping mode-mapping global-mapping
461 macro-pair macro-entry)
463 ;; Macro-name is usually a vector. However, command history or macros
464 ;; recorded in ~/.vip may appear as strings. So, convert to vectors.
465 (setq macro-name (vip-fixup-macro macro-name))
466 (if (vip-char-array-p macro-name)
467 (setq macro-name (vip-char-array-to-macro macro-name)))
469 (setq macro-entry (assoc macro-name (eval macro-alist-var)))
470 (if (= (length macro-name) 0)
471 (error "Can't unmap an empty macro name"))
472 (if (null macro-entry)
473 (error "%S is not mapped to a macro for %s in `%s'"
474 (vip-display-macro macro-name)
475 state-name (buffer-name)))
477 (setq buf-mapping (vip-kbd-buf-pair macro-entry)
478 mode-mapping (vip-kbd-mode-pair macro-entry)
479 global-mapping (vip-kbd-global-pair macro-entry))
481 (cond ((and (cdr buf-mapping)
482 (or (and (not (cdr mode-mapping)) (not (cdr global-mapping)))
484 (format "Unmap %S for `%s' only? "
485 (vip-display-macro macro-name)
487 (setq macro-pair buf-mapping)
488 (message "%S is unmapped for %s in `%s'"
489 (vip-display-macro macro-name)
490 state-name (buffer-name)))
491 ((and (cdr mode-mapping)
492 (or (not (cdr global-mapping))
494 (format "Unmap %S for the major mode `%S' only? "
495 (vip-display-macro macro-name)
497 (setq macro-pair mode-mapping)
498 (message "%S is unmapped for %s in %S"
499 (vip-display-macro macro-name) state-name major-mode))
500 ((cdr (setq macro-pair (vip-kbd-global-pair macro-entry)))
502 "Global mapping of %S for %s is removed"
503 (vip-display-macro macro-name) state-name))
504 (t (error "%S is not mapped to a macro for %s in `%s'"
505 (vip-display-macro macro-name)
506 state-name (buffer-name))))
507 (setcdr macro-pair nil)
508 (or (cdr buf-mapping)
512 (set macro-alist-var (delq macro-entry (eval macro-alist-var)))
513 (if (vip-can-release-key (aref macro-name 0)
514 (eval macro-alist-var))
517 (vector (vip-key-to-emacs-key (aref macro-name 0)))
522 ;; Checks if MACRO-ALIST has an entry for a macro name starting with
523 ;; CHAR. If not, this indicates that the binding for this char
524 ;; in vip-vi/insert-kbd-map can be released.
525 (defun vip-can-release-key (char macro-alist)
526 (let ((lis macro-alist)
530 (while (and lis can-release)
531 (setq macro-name (car (car lis)))
532 (if (eq char (aref macro-name 0))
533 (setq can-release nil))
534 (setq lis (cdr lis)))
538 (defun vip-exec-mapped-kbd-macro (count)
539 "Dispatch kbd macro."
541 (let* ((macro-alist (cond ((eq vip-current-state 'vi-state)
542 vip-vi-kbd-macro-alist)
543 ((memq vip-current-state
544 '(insert-state replace-state))
545 vip-insert-kbd-macro-alist)
547 vip-emacs-kbd-macro-alist)))
548 (unmatched-suffix "")
549 ;; Macros and keys are executed with other macros turned off
550 ;; For macros, this is done to avoid macro recursion
551 vip-vi-kbd-minor-mode vip-insert-kbd-minor-mode
552 vip-emacs-kbd-minor-mode
553 next-best-match keyseq event-seq
554 macro-first-char macro-alist-elt macro-body
557 (setq macro-first-char last-command-event
558 event-seq (vip-read-fast-keysequence macro-first-char macro-alist)
559 keyseq (vip-events-to-macro event-seq)
560 macro-alist-elt (assoc keyseq macro-alist)
561 next-best-match (vip-find-best-matching-macro macro-alist keyseq))
563 (if (null macro-alist-elt)
564 (setq macro-alist-elt (car next-best-match)
565 unmatched-suffix (subseq event-seq (cdr next-best-match))))
567 (cond ((null macro-alist-elt))
568 ((setq macro-body (vip-kbd-buf-definition macro-alist-elt)))
569 ((setq macro-body (vip-kbd-mode-definition macro-alist-elt)))
570 ((setq macro-body (vip-kbd-global-definition macro-alist-elt))))
572 ;; when defining keyboard macro, don't use the macro mappings
573 (if (and macro-body (not defining-kbd-macro))
574 ;; block cmd executed as part of a macro from entering command history
575 (let ((command-history command-history))
576 (setq vip-this-kbd-macro (car macro-alist-elt))
577 (execute-kbd-macro (vip-macro-to-events macro-body) count)
578 (setq vip-this-kbd-macro nil
579 vip-last-kbd-macro (car macro-alist-elt))
580 (vip-set-unread-command-events unmatched-suffix))
581 ;; If not a macro, or the macro is suppressed while defining another
582 ;; macro, put keyseq back on the event queue
583 (vip-set-unread-command-events event-seq)
584 ;; if the user typed arg, then use it if prefix arg is not set by
585 ;; some other command (setting prefix arg can happen if we do, say,
586 ;; 2dw and there is a macro starting with 2. Then control will go to
588 (or prefix-arg (setq prefix-arg count))
589 (setq command (key-binding (read-key-sequence nil)))
590 (if (commandp command)
591 (command-execute command)
597 ;;; Displaying and completing macros
599 (defun vip-describe-kbd-macros ()
600 "Show currently defined keyboard macros."
602 (with-output-to-temp-buffer " *vip-info*"
603 (princ "Macros in Vi state:\n===================\n")
604 (mapcar 'vip-describe-one-macro vip-vi-kbd-macro-alist)
605 (princ "\n\nMacros in Insert and Replace states:\n====================================\n")
606 (mapcar 'vip-describe-one-macro vip-insert-kbd-macro-alist)
607 (princ "\n\nMacros in Emacs state:\n======================\n")
608 (mapcar 'vip-describe-one-macro vip-emacs-kbd-macro-alist)
611 (defun vip-describe-one-macro (macro)
612 (princ (format "\n *** Mappings for %S:\n ------------\n"
613 (vip-display-macro (car macro))))
614 (princ " ** Buffer-specific:")
615 (if (vip-kbd-buf-alist macro)
616 (mapcar 'vip-describe-one-macro-elt (vip-kbd-buf-alist macro))
618 (princ "\n ** Mode-specific:")
619 (if (vip-kbd-mode-alist macro)
620 (mapcar 'vip-describe-one-macro-elt (vip-kbd-mode-alist macro))
622 (princ "\n ** Global:")
623 (if (vip-kbd-global-definition macro)
626 (cdr (vip-kbd-global-pair macro))))
630 (defun vip-describe-one-macro-elt (elt)
631 (let ((name (car elt))
633 (princ (format "\n * %S:\n %S\n" name defn))))
637 ;; check if SEQ is a prefix of some car of an element in ALIST
638 (defun vip-keyseq-is-a-possible-macro (seq alist)
639 (let ((converted-seq (vip-events-to-macro seq)))
642 (function (lambda (elt)
643 (vip-prefix-subseq-p converted-seq elt)))
644 (vip-this-buffer-macros alist))))))
646 ;; whether SEQ1 is a prefix of SEQ2
647 (defun vip-prefix-subseq-p (seq1 seq2)
648 (let ((len1 (length seq1))
649 (len2 (length seq2)))
651 (equal seq1 (subseq seq2 0 len1)))))
653 ;; find the longest common prefix
654 (defun vip-common-seq-prefix (&rest seqs)
655 (let* ((first (car seqs))
660 (if (= (length seqs) 0)
662 (setq len (apply 'min (mapcar 'length seqs))))
665 (mapcar (function (lambda (s)
666 (equal (elt first idx)
669 (setq pref (vconcat pref (vector (elt first idx)))))
673 ;; get all sequences that match PREFIX from a given A-LIST
674 (defun vip-extract-matching-alist-members (pref alist)
675 (delq nil (mapcar (function (lambda (elt)
676 (if (vip-prefix-subseq-p pref elt)
678 (vip-this-buffer-macros alist))))
680 (defun vip-do-sequence-completion (seq alist compl-message)
681 (let* ((matches (vip-extract-matching-alist-members seq alist))
682 (new-seq (apply 'vip-common-seq-prefix matches))
684 (cond ((and (equal seq new-seq) (= (length matches) 1))
685 (message "%s (Sole completion)" compl-message)
688 (message "%s (No match)" compl-message)
691 ((member seq matches)
692 (message "%s (Complete, but not unique)" compl-message)
694 (vip-display-vector-completions matches))
696 (vip-display-vector-completions matches)))
700 (defun vip-display-vector-completions (list)
701 (with-output-to-temp-buffer "*Completions*"
702 (display-completion-list
703 (mapcar 'prin1-to-string
704 (mapcar 'vip-display-macro list)))))
708 ;; alist is the alist of macros
709 ;; str is the fast key sequence entered
710 ;; returns: (matching-macro-def . unmatched-suffix-start-index)
711 (defun vip-find-best-matching-macro (alist str)
714 (str-len (length str))
715 match unmatched-start-idx found macro-def)
716 (while (and (not found) lis)
717 (setq macro-def (car lis)
718 def-len (length (car macro-def)))
719 (if (and (>= str-len def-len)
720 (equal (car macro-def) (subseq str 0 def-len)))
721 (if (or (vip-kbd-buf-definition macro-def)
722 (vip-kbd-mode-definition macro-def)
723 (vip-kbd-global-definition macro-def))
726 (setq lis (cdr lis)))
729 (setq match macro-def
730 unmatched-start-idx def-len)
732 unmatched-start-idx 0))
734 (cons match unmatched-start-idx)))
738 ;; returns a list of names of macros defined for the current buffer
739 (defun vip-this-buffer-macros (macro-alist)
744 (if (or (vip-kbd-buf-definition elt)
745 (vip-kbd-mode-definition elt)
746 (vip-kbd-global-definition elt))
749 (setq candidates (delq nil candidates))))
752 ;; if seq of key symbols can be converted to a string--do so. Otherwise, do
754 (defun vip-display-macro (macro-name)
755 (cond ((vip-char-symbol-sequence-p macro-name)
756 (mapconcat 'symbol-name macro-name ""))
757 ((vip-char-array-p macro-name)
758 (mapconcat 'char-to-string macro-name ""))
761 (defun vip-events-to-macro (event-seq)
762 (vconcat (mapcar 'vip-event-key event-seq)))
764 ;; convert strings of characters or arrays of characters to Viper macro form
765 (defun vip-char-array-to-macro (array)
766 (let ((vec (vconcat array))
769 (setq macro (mapcar 'character-to-event vec))
771 (vconcat (mapcar 'vip-event-key macro))))
773 ;; For macros bodies and names, goes over and checks if all members are
774 ;; names of keys (actually, it only checks if they are symbols or lists
775 ;; if a digit is found, it is converted into a symbol (0 -> \0, etc).
776 ;; If not list or vector, doesn't change its argument
777 (defun vip-fixup-macro (macro)
778 (let ((len (length macro))
781 (if (or (vectorp macro) (listp macro))
782 (while (and (< idx len) (not break))
783 (setq elt (elt macro idx))
786 (if (and (<= 0 elt) (<= elt 9))
787 (cond ((arrayp macro)
790 (intern (char-to-string (+ ?0 elt)))))
792 (setcar (nthcdr idx macro)
793 (intern (char-to-string (+ ?0 elt)))))
797 (vip-fixup-macro elt))
800 (setq idx (1+ idx))))
803 (error "Wrong type macro component, symbol-or-listp, %S" elt)
806 (defun vip-char-array-p (array)
807 (eval (cons 'and (mapcar 'numberp array))))
809 (defun vip-macro-to-events (macro-body)
810 (vconcat (mapcar 'vip-key-to-emacs-key macro-body)))
813 ;; check if vec is a vector of character symbols
814 (defun vip-char-symbol-sequence-p (vec)
820 (function (lambda (elt)
821 (and (symbolp elt) (= (length (symbol-name elt)) 1))))
825 ;; Check if vec is a vector of key-press events representing characters
827 (defun vip-event-vector-p (vec)
829 (eval (cons 'and (mapcar '(lambda (elt) (if (eventp elt) t)) vec)))))
832 ;;; Reading fast key sequences
834 ;; Assuming that CHAR was the first character in a fast succession of key
835 ;; strokes, read the rest. Return the vector of keys that was entered in
836 ;; this fast succession of key strokes.
837 ;; A fast keysequence is one that is terminated by a pause longer than
838 ;; vip-fast-keyseq-timeout.
839 (defun vip-read-fast-keysequence (event macro-alist)
840 (let ((lis (vector event))
842 (while (and (vip-fast-keysequence-p)
843 (vip-keyseq-is-a-possible-macro lis macro-alist))
844 (setq next-event (vip-read-event))
845 (or (vip-mouse-event-p next-event)
846 (setq lis (vconcat lis (vector next-event)))))
850 ;;; Keyboard macros in registers
852 ;; sets register to last-kbd-macro carefully.
853 (defun vip-set-register-macro (reg)
854 (if (get-register reg)
855 (if (y-or-n-p "Register contains data. Overwrite? ")
858 "Macro not saved in register. Can still be invoked via `C-x e'")))
859 (set-register reg last-kbd-macro))
861 (defun vip-register-macro (count)
862 "Keyboard macros in registers - a modified \@ command."
864 (let ((reg (downcase (read-char))))
865 (cond ((or (and (<= ?a reg) (<= reg ?z)))
866 (setq vip-last-macro-reg reg)
867 (if defining-kbd-macro
870 (vip-set-register-macro reg))
871 (execute-kbd-macro (get-register reg) count)))
872 ((or (= ?@ reg) (= ?\^j reg) (= ?\^m reg))
873 (if vip-last-macro-reg
875 (error "No previous kbd macro"))
876 (execute-kbd-macro (get-register vip-last-macro-reg) count))
878 (start-kbd-macro count))
880 (setq reg (downcase (read-char)))
881 (if (or (and (<= ?a reg) (<= reg ?z)))
883 (setq vip-last-macro-reg reg)
884 (vip-set-register-macro reg))))
886 (error (format "`%c': Unknown register" reg))))))
889 (defun vip-global-execute ()
890 "Call last keyboad macro for each line in the region."
891 (if (> (point) (mark t)) (exchange-point-and-mark))
893 (call-last-kbd-macro)
894 (while (< (point) (mark t))
897 (call-last-kbd-macro)))
900 (provide 'viper-macs)
902 ;;; viper-macs.el ends here