1 ;;; viper-macs.el --- functions implementing keyboard macros for Viper
3 ;; Copyright (C) 1994, 1995, 1996, 1997, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
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, or (at your option)
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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
32 (defvar viper-ex-work-buf
)
33 (defvar viper-custom-file-name
)
34 (defvar viper-current-state
)
35 (defvar viper-fast-keyseq-timeout
)
37 ;; loading happens only in non-interactive compilation
38 ;; in order to spare non-viperized emacs from being viperized
51 ;; Register holding last macro.
52 (defvar viper-last-macro-reg nil
)
54 ;; format of the elements of kbd alists:
55 ;; (name ((buf . macr)...(buf . macr)) ((maj-mode . macr)...) (t . macr))
56 ;; kbd macro alist for Vi state
57 (defvar viper-vi-kbd-macro-alist nil
)
58 ;; same for insert/replace state
59 (defvar viper-insert-kbd-macro-alist nil
)
60 ;; same for emacs state
61 (defvar viper-emacs-kbd-macro-alist nil
)
63 ;; Internal var that passes info between start-kbd-macro and end-kbd-macro
65 (defvar viper-kbd-macro-parameters nil
)
67 (defvar viper-this-kbd-macro nil
68 "Vector of keys representing the name of currently running Viper kbd macro.")
69 (defvar viper-last-kbd-macro nil
70 "Vector of keys representing the name of last Viper keyboard macro.")
72 (defcustom viper-repeat-from-history-key
'f12
73 "Prefix key for accessing previously typed Vi commands.
75 The previous command is accessible, as usual, via `.'. The command before this
76 can be invoked as `<this key> 1', and the command before that, and the command
77 before that one is accessible as `<this key> 2'.
78 The notation for these keys is borrowed from XEmacs. Basically,
79 a key is a symbol, e.g., `a', `\\1', `f2', etc., or a list, e.g.,
91 macro-name macro-body map-args ins
)
92 (save-window-excursion
93 (set-buffer viper-ex-work-buf
)
99 (setq map-args
(ex-map-read-args mod-char
)
100 macro-name
(car map-args
)
101 macro-body
(cdr map-args
))
102 (setq viper-kbd-macro-parameters
(list ins mod-char macro-name macro-body
))
104 (viper-end-mapping-kbd-macro 'ignore
)
105 (ex-fixup-history (format "map%s %S" mod-char
106 (viper-display-macro macro-name
)))
107 ;; if defining macro for insert, switch there for authentic WYSIWYG
108 (if ins
(viper-change-state-to-insert))
109 (start-kbd-macro nil
)
110 (define-key viper-vi-intercept-map
"\C-x)" 'viper-end-mapping-kbd-macro
)
111 (define-key viper-insert-intercept-map
"\C-x)" 'viper-end-mapping-kbd-macro
)
112 (define-key viper-emacs-intercept-map
"\C-x)" 'viper-end-mapping-kbd-macro
)
113 (message "Mapping %S in %s state. Type macro definition followed by `C-x )'"
114 (viper-display-macro macro-name
)
115 (if ins
"Insert" "Vi")))
123 (save-window-excursion
124 (set-buffer viper-ex-work-buf
)
131 (setq macro-name
(ex-unmap-read-args mod-char
))
132 (setq temp
(viper-fixup-macro (vconcat macro-name
))) ;; copy and fixup
133 (ex-fixup-history (format "unmap%s %S" mod-char
134 (viper-display-macro temp
)))
135 (viper-unrecord-kbd-macro macro-name
(if ins
'insert-state
'vi-state
))
139 ;; read arguments for ex-map
140 (defun ex-map-read-args (variant)
141 (let ((cursor-in-echo-area t
)
143 temp key event message
144 macro-name macro-body args
)
147 (setq args
(concat (ex-get-inline-cmd-args ".*map[!]*[ \t]?" "\n\C-m")
149 temp
(read-from-string args
)
150 macro-name
(car temp
)
151 macro-body
(car (read-from-string args
(cdr temp
))))
155 '("map: Macro name and body must be a quoted string or a vector"))))
157 ;; We expect macro-name to be a vector, a string, or a quoted string.
158 ;; In the second case, it will emerge as a symbol when read from
159 ;; the above read-from-string. So we need to convert it into a string
161 (cond ((vectorp macro-name
) nil
)
162 ((stringp macro-name
)
163 (setq macro-name
(vconcat macro-name
)))
164 (t (setq macro-name
(vconcat (prin1-to-string macro-name
)))))
165 (message ":map%s <Macro Name>" variant
)(sit-for 2)
168 '(?\C-m ?
\n (control m
) (control j
) return linefeed
)))
169 (setq key-seq
(vconcat key-seq
(if key
(vector key
) [])))
170 ;; the only keys available for editing are these-- no help while there
173 '(?
\b ?\d
'^?
'^H
(control h
) (control \?) backspace delete
))
174 (setq key-seq
(viper-subseq key-seq
0 (- (length key-seq
) 2))))
178 variant
(if (> (length key-seq
) 0)
179 (prin1-to-string (viper-display-macro key-seq
))
181 (message "%s" message
)
182 (setq event
(viper-read-key))
183 ;;(setq event (viper-read-event))
185 (if (viper-mouse-event-p event
)
187 (message "%s (No mouse---only keyboard keys, please)"
191 (viper-event-key event
)))
193 (setq macro-name key-seq
))
195 (if (= (length macro-name
) 0)
196 (error "Can't map an empty macro name"))
197 (setq macro-name
(viper-fixup-macro macro-name
))
198 (if (viper-char-array-p macro-name
)
199 (setq macro-name
(viper-char-array-to-macro macro-name
)))
202 (cond ((viper-char-array-p macro-body
)
203 (setq macro-body
(viper-char-array-to-macro macro-body
)))
204 ((vectorp macro-body
) nil
)
205 (t (error "map: Invalid syntax in macro definition"))))
206 (setq cursor-in-echo-area nil
)(sit-for 0) ; this overcomes xemacs tty bug
207 (cons macro-name macro-body
)))
211 ;; read arguments for ex-unmap
212 (defun ex-unmap-read-args (variant)
213 (let ((cursor-in-echo-area t
)
214 (macro-alist (if (string= variant
"!")
215 viper-insert-kbd-macro-alist
216 viper-vi-kbd-macro-alist
))
217 ;; these are disabled just in case, to avoid surprises when doing
219 viper-vi-kbd-minor-mode viper-insert-kbd-minor-mode
220 viper-emacs-kbd-minor-mode
221 viper-vi-intercept-minor-mode viper-insert-intercept-minor-mode
222 viper-emacs-intercept-minor-mode
224 key key-seq macro-name
)
225 (setq macro-name
(ex-get-inline-cmd-args ".*unma?p?[!]*[ \t]*"))
227 (if (> (length macro-name
) 0)
229 (message ":unmap%s <Name>" variant
) (sit-for 2)
232 (member key
'(?\C-m ?
\n (control m
) (control j
) return linefeed
)))
233 (setq key-seq
(vconcat key-seq
(if key
(vector key
) [])))
234 ;; the only keys available for editing are these-- no help while there
237 '(?
\b ?\d
'^?
'^H
(control h
) (control \?) backspace delete
))
238 (setq key-seq
(viper-subseq key-seq
0 (- (length key-seq
) 2))))
239 ((member key
'(tab (control i
) ?
\t))
240 (setq key-seq
(viper-subseq key-seq
0 (1- (length key-seq
))))
244 variant
(if (> (length key-seq
) 0)
246 (viper-display-macro key-seq
))
249 (viper-do-sequence-completion key-seq macro-alist message
))
254 variant
(if (> (length key-seq
) 0)
256 (viper-display-macro key-seq
))
258 (message "%s" message
)
259 (setq event
(viper-read-key))
260 ;;(setq event (viper-read-event))
262 (if (viper-mouse-event-p event
)
264 (message "%s (No mouse---only keyboard keys, please)"
268 (viper-event-key event
)))
270 (setq macro-name key-seq
))
272 (if (= (length macro-name
) 0)
273 (error "Can't unmap an empty macro name"))
275 ;; convert macro names into vector, if starts with a `['
276 (if (memq (elt macro-name
0) '(?\
[ ?
\"))
277 (car (read-from-string macro-name
))
278 (vconcat macro-name
))
282 ;; Terminate a Vi kbd macro.
283 ;; optional argument IGNORE, if t, indicates that we are dealing with an
284 ;; existing macro that needs to be registered, but there is no need to
285 ;; terminate a kbd macro.
286 (defun viper-end-mapping-kbd-macro (&optional ignore
)
288 (define-key viper-vi-intercept-map
"\C-x)" nil
)
289 (define-key viper-insert-intercept-map
"\C-x)" nil
)
290 (define-key viper-emacs-intercept-map
"\C-x)" nil
)
291 (if (and (not ignore
)
292 (or (not viper-kbd-macro-parameters
)
293 (not defining-kbd-macro
)))
294 (error "Not mapping a kbd-macro"))
295 (let ((mod-char (nth 1 viper-kbd-macro-parameters
))
296 (ins (nth 0 viper-kbd-macro-parameters
))
297 (macro-name (nth 2 viper-kbd-macro-parameters
))
298 (macro-body (nth 3 viper-kbd-macro-parameters
)))
299 (setq viper-kbd-macro-parameters nil
)
303 (setq macro-body
(viper-events-to-macro last-kbd-macro
))
304 ;; always go back to Vi, since this is where we started
306 (viper-change-state-to-vi)))
308 (viper-record-kbd-macro macro-name
309 (if ins
'insert-state
'vi-state
)
310 (viper-display-macro macro-body
))
312 (ex-fixup-history (format "map%s %S %S" mod-char
313 (viper-display-macro macro-name
)
314 (viper-display-macro macro-body
)))
320 ;;; Recording, unrecording, executing
322 ;; Accepts as macro names: strings and vectors.
323 ;; strings must be strings of characters; vectors must be vectors of keys
324 ;; in canonic form. The canonic form is essentially the form used in XEmacs
325 ;; More general definitions are inherited by more specific scopes:
326 ;; global->major mode->buffer. More specific definitions override more general
327 (defun viper-record-kbd-macro (macro-name state macro-body
&optional scope
)
328 "Record a Vi macro. Can be used in `.viper' file to define permanent macros.
329 MACRO-NAME is a string of characters or a vector of keys. STATE is
330 either `vi-state' or `insert-state'. It specifies the Viper state in which to
331 define the macro. MACRO-BODY is a string that represents the keyboard macro.
332 Optional SCOPE says whether the macro should be global \(t\), mode-specific
333 \(a major-mode symbol\), or buffer-specific \(buffer name, a string\).
334 If SCOPE is nil, the user is asked to specify the scope."
335 (let* (state-name keymap
337 (cond ((eq state
'vi-state
)
338 (setq state-name
"Vi state"
339 keymap viper-vi-kbd-map
)
340 'viper-vi-kbd-macro-alist
)
341 ((memq state
'(insert-state replace-state
))
342 (setq state-name
"Insert state"
343 keymap viper-insert-kbd-map
)
344 'viper-insert-kbd-macro-alist
)
346 (setq state-name
"Emacs state"
347 keymap viper-emacs-kbd-map
)
348 'viper-emacs-kbd-macro-alist
)
350 new-elt old-elt old-sub-elt msg
353 (if (= (length macro-name
) 0)
354 (error "Can't map an empty macro name"))
356 ;; Macro-name is usually a vector. However, command history or macros
357 ;; recorded in ~/.viper may be recorded as strings. So, convert to
359 (setq macro-name
(viper-fixup-macro macro-name
))
360 (if (viper-char-array-p macro-name
)
361 (setq macro-name
(viper-char-array-to-macro macro-name
)))
362 (setq macro-body
(viper-fixup-macro macro-body
))
363 (if (viper-char-array-p macro-body
)
364 (setq macro-body
(viper-char-array-to-macro macro-body
)))
366 ;; don't ask if scope is given and is of the right type
369 (and scope
(symbolp scope
))
375 "Map this macro for buffer `%s' only? "
379 "%S is mapped to %s for %s in `%s'"
380 (viper-display-macro macro-name
)
381 (viper-abbreviate-string
384 (setq temp
(viper-display-macro macro-body
)))
386 (if (stringp temp
) " ....\"" " ....]"))
387 state-name
(buffer-name)))
391 "Map this macro for the major mode `%S' only? "
395 "%S is mapped to %s for %s in `%S'"
396 (viper-display-macro macro-name
)
397 (viper-abbreviate-string
400 (setq temp
(viper-display-macro macro-body
)))
402 (if (stringp macro-body
) " ....\"" " ....]"))
403 state-name major-mode
))
408 "%S is globally mapped to %s in %s"
409 (viper-display-macro macro-name
)
410 (viper-abbreviate-string
413 (setq temp
(viper-display-macro macro-body
)))
415 (if (stringp macro-body
) " ....\"" " ....]"))
419 (format "Save this macro in %s? "
420 (viper-abbreviate-file-name viper-custom-file-name
)))
421 (viper-save-string-in-file
422 (format "\n(viper-record-kbd-macro %S '%S %s '%S)"
423 (viper-display-macro macro-name
)
425 ;; if we don't let vector macro-body through %S,
426 ;; the symbols `\.' `\[' etc will be converted into
427 ;; characters, causing invalid read error on recorded
429 ;; I am not sure is macro-body can still be a string at
430 ;; this point, but I am preserving this option anyway.
431 (if (vectorp macro-body
)
432 (format "%S" macro-body
)
435 viper-custom-file-name
))
442 (cond ((eq scope t
) (list nil nil
(cons t nil
)))
444 (list nil
(list (cons scope nil
)) (cons t nil
)))
446 (list (list (cons scope nil
)) nil
(cons t nil
))))))
447 (setq old-elt
(assoc macro-name
(eval macro-alist-var
)))
451 ;; insert new-elt in macro-alist-var and keep the list sorted
454 (vector (viper-key-to-emacs-key (aref macro-name
0)))
455 'viper-exec-mapped-kbd-macro
)
456 (setq lis
(eval macro-alist-var
))
457 (while (and lis
(string< (viper-array-to-string (car (car lis
)))
458 (viper-array-to-string macro-name
)))
459 (setq lis2
(cons (car lis
) lis2
))
460 (setq lis
(cdr lis
)))
462 (setq lis2
(reverse lis2
))
463 (set macro-alist-var
(append lis2
(cons new-elt lis
)))
464 (setq old-elt new-elt
)))
466 (cond ((eq scope t
) (viper-kbd-global-pair old-elt
))
467 ((symbolp scope
) (assoc scope
(viper-kbd-mode-alist old-elt
)))
468 ((stringp scope
) (assoc scope
(viper-kbd-buf-alist old-elt
)))))
470 (setcdr old-sub-elt macro-body
)
471 (cond ((symbolp scope
) (setcar (cdr (cdr old-elt
))
472 (cons (cons scope macro-body
)
473 (viper-kbd-mode-alist old-elt
))))
474 ((stringp scope
) (setcar (cdr old-elt
)
475 (cons (cons scope macro-body
)
476 (viper-kbd-buf-alist old-elt
))))))
481 ;; macro name must be a vector of viper-style keys
482 ;; viper-unrecord-kbd-macro doesn't have scope. Macro definitions are inherited
483 ;; from global -> major mode -> buffer
484 ;; More specific definition overrides more general
485 ;; Can't unrecord definition for more specific, if a more general definition is
487 (defun viper-unrecord-kbd-macro (macro-name state
)
488 "Delete macro MACRO-NAME from Viper STATE.
489 MACRO-NAME must be a vector of viper-style keys. This command is used by Viper
490 internally, but the user can also use it in ~/.viper to delete pre-defined
491 macros supplied with Viper. The best way to avoid mistakes in macro names to
492 be passed to this function is to use viper-describe-kbd-macros and copy the
494 (let* (state-name keymap
496 (cond ((eq state
'vi-state
)
497 (setq state-name
"Vi state"
498 keymap viper-vi-kbd-map
)
499 'viper-vi-kbd-macro-alist
)
500 ((memq state
'(insert-state replace-state
))
501 (setq state-name
"Insert state"
502 keymap viper-insert-kbd-map
)
503 'viper-insert-kbd-macro-alist
)
505 (setq state-name
"Emacs state"
506 keymap viper-emacs-kbd-map
)
507 'viper-emacs-kbd-macro-alist
)
509 buf-mapping mode-mapping global-mapping
510 macro-pair macro-entry
)
512 ;; Macro-name is usually a vector. However, command history or macros
513 ;; recorded in ~/.viper may appear as strings. So, convert to vectors.
514 (setq macro-name
(viper-fixup-macro macro-name
))
515 (if (viper-char-array-p macro-name
)
516 (setq macro-name
(viper-char-array-to-macro macro-name
)))
518 (setq macro-entry
(assoc macro-name
(eval macro-alist-var
)))
519 (if (= (length macro-name
) 0)
520 (error "Can't unmap an empty macro name"))
521 (if (null macro-entry
)
522 (error "%S is not mapped to a macro for %s in `%s'"
523 (viper-display-macro macro-name
)
524 state-name
(buffer-name)))
526 (setq buf-mapping
(viper-kbd-buf-pair macro-entry
)
527 mode-mapping
(viper-kbd-mode-pair macro-entry
)
528 global-mapping
(viper-kbd-global-pair macro-entry
))
530 (cond ((and (cdr buf-mapping
)
531 (or (and (not (cdr mode-mapping
)) (not (cdr global-mapping
)))
533 (format "Unmap %S for `%s' only? "
534 (viper-display-macro macro-name
)
536 (setq macro-pair buf-mapping
)
537 (message "%S is unmapped for %s in `%s'"
538 (viper-display-macro macro-name
)
539 state-name
(buffer-name)))
540 ((and (cdr mode-mapping
)
541 (or (not (cdr global-mapping
))
543 (format "Unmap %S for the major mode `%S' only? "
544 (viper-display-macro macro-name
)
546 (setq macro-pair mode-mapping
)
547 (message "%S is unmapped for %s in %S"
548 (viper-display-macro macro-name
) state-name major-mode
))
549 ((cdr (setq macro-pair global-mapping
))
551 "Global mapping for %S in %s is removed"
552 (viper-display-macro macro-name
) state-name
))
553 (t (error "%S is not mapped to a macro for %s in `%s'"
554 (viper-display-macro macro-name
)
555 state-name
(buffer-name))))
556 (setcdr macro-pair nil
)
557 (or (cdr buf-mapping
)
561 (set macro-alist-var
(delq macro-entry
(eval macro-alist-var
)))
562 (if (viper-can-release-key (aref macro-name
0)
563 (eval macro-alist-var
))
566 (vector (viper-key-to-emacs-key (aref macro-name
0)))
571 ;; Check if MACRO-ALIST has an entry for a macro name starting with
572 ;; CHAR. If not, this indicates that the binding for this char
573 ;; in viper-vi/insert-kbd-map can be released.
574 (defun viper-can-release-key (char macro-alist
)
575 (let ((lis macro-alist
)
579 (while (and lis can-release
)
580 (setq macro-name
(car (car lis
)))
581 (if (eq char
(aref macro-name
0))
582 (setq can-release nil
))
583 (setq lis
(cdr lis
)))
587 (defun viper-exec-mapped-kbd-macro (count)
588 "Dispatch kbd macro."
590 (let* ((macro-alist (cond ((eq viper-current-state
'vi-state
)
591 viper-vi-kbd-macro-alist
)
592 ((memq viper-current-state
593 '(insert-state replace-state
))
594 viper-insert-kbd-macro-alist
)
596 viper-emacs-kbd-macro-alist
)))
597 (unmatched-suffix "")
598 ;; Macros and keys are executed with other macros turned off
599 ;; For macros, this is done to avoid macro recursion
600 viper-vi-kbd-minor-mode viper-insert-kbd-minor-mode
601 viper-emacs-kbd-minor-mode
602 next-best-match keyseq event-seq
603 macro-first-char macro-alist-elt macro-body
606 (setq macro-first-char last-command-event
607 event-seq
(viper-read-fast-keysequence macro-first-char macro-alist
)
608 keyseq
(viper-events-to-macro event-seq
)
609 macro-alist-elt
(assoc keyseq macro-alist
)
610 next-best-match
(viper-find-best-matching-macro macro-alist keyseq
))
612 (if (null macro-alist-elt
)
613 (setq macro-alist-elt
(car next-best-match
)
614 unmatched-suffix
(viper-subseq event-seq
(cdr next-best-match
))))
616 (cond ((null macro-alist-elt
))
617 ((setq macro-body
(viper-kbd-buf-definition macro-alist-elt
)))
618 ((setq macro-body
(viper-kbd-mode-definition macro-alist-elt
)))
619 ((setq macro-body
(viper-kbd-global-definition macro-alist-elt
))))
621 ;; when defining keyboard macro, don't use the macro mappings
622 (if (and macro-body
(not defining-kbd-macro
))
623 ;; block cmd executed as part of a macro from entering command history
624 (let ((command-history command-history
))
625 (setq viper-this-kbd-macro
(car macro-alist-elt
))
626 (execute-kbd-macro (viper-macro-to-events macro-body
) count
)
627 (setq viper-this-kbd-macro nil
628 viper-last-kbd-macro
(car macro-alist-elt
))
629 (viper-set-unread-command-events unmatched-suffix
))
630 ;; If not a macro, or the macro is suppressed while defining another
631 ;; macro, put keyseq back on the event queue
632 (viper-set-unread-command-events event-seq
)
633 ;; if the user typed arg, then use it if prefix arg is not set by
634 ;; some other command (setting prefix arg can happen if we do, say,
635 ;; 2dw and there is a macro starting with 2. Then control will go to
637 (or prefix-arg
(setq prefix-arg count
))
638 (setq command
(key-binding (read-key-sequence nil
)))
639 (if (commandp command
)
640 (command-execute command
)
646 ;;; Displaying and completing macros
648 (defun viper-describe-kbd-macros ()
649 "Show currently defined keyboard macros."
651 (with-output-to-temp-buffer " *viper-info*"
652 (princ "Macros in Vi state:\n===================\n")
653 (mapc 'viper-describe-one-macro viper-vi-kbd-macro-alist
)
654 (princ "\n\nMacros in Insert and Replace states:\n====================================\n")
655 (mapc 'viper-describe-one-macro viper-insert-kbd-macro-alist
)
656 (princ "\n\nMacros in Emacs state:\n======================\n")
657 (mapcar 'viper-describe-one-macro viper-emacs-kbd-macro-alist
)
660 (defun viper-describe-one-macro (macro)
661 (princ (format "\n *** Mappings for %S:\n ------------\n"
662 (viper-display-macro (car macro
))))
663 (princ " ** Buffer-specific:")
664 (if (viper-kbd-buf-alist macro
)
665 (mapc 'viper-describe-one-macro-elt
(viper-kbd-buf-alist macro
))
667 (princ "\n ** Mode-specific:")
668 (if (viper-kbd-mode-alist macro
)
669 (mapc 'viper-describe-one-macro-elt
(viper-kbd-mode-alist macro
))
671 (princ "\n ** Global:")
672 (if (viper-kbd-global-definition macro
)
673 (princ (format "\n %S" (cdr (viper-kbd-global-pair macro
))))
677 (defun viper-describe-one-macro-elt (elt)
678 (let ((name (car elt
))
680 (princ (format "\n * %S:\n %S\n" name defn
))))
684 ;; check if SEQ is a prefix of some car of an element in ALIST
685 (defun viper-keyseq-is-a-possible-macro (seq alist
)
686 (let ((converted-seq (viper-events-to-macro seq
)))
689 (lambda (elt) (viper-prefix-subseq-p converted-seq elt
))
690 (viper-this-buffer-macros alist
))))))
692 ;; whether SEQ1 is a prefix of SEQ2
693 (defun viper-prefix-subseq-p (seq1 seq2
)
694 (let ((len1 (length seq1
))
695 (len2 (length seq2
)))
697 (equal seq1
(viper-subseq seq2
0 len1
)))))
699 ;; find the longest common prefix
700 (defun viper-common-seq-prefix (&rest seqs
)
701 (let* ((first (car seqs
))
706 (if (= (length seqs
) 0)
708 (setq len
(apply 'min
(mapcar 'length seqs
))))
711 (mapcar (lambda (s) (equal (elt first idx
) (elt s idx
)))
713 (setq pref
(vconcat pref
(vector (elt first idx
)))))
717 ;; get all sequences that match PREFIX from a given A-LIST
718 (defun viper-extract-matching-alist-members (pref alist
)
719 (delq nil
(mapcar (lambda (elt) (if (viper-prefix-subseq-p pref elt
) elt
))
720 (viper-this-buffer-macros alist
))))
722 (defun viper-do-sequence-completion (seq alist compl-message
)
723 (let* ((matches (viper-extract-matching-alist-members seq alist
))
724 (new-seq (apply 'viper-common-seq-prefix matches
))
726 (cond ((and (equal seq new-seq
) (= (length matches
) 1))
727 (message "%s (Sole completion)" compl-message
)
730 (message "%s (No match)" compl-message
)
733 ((member seq matches
)
734 (message "%s (Complete, but not unique)" compl-message
)
736 (viper-display-vector-completions matches
))
738 (viper-display-vector-completions matches
)))
742 (defun viper-display-vector-completions (list)
743 (with-output-to-temp-buffer "*Completions*"
744 (display-completion-list
745 (mapcar 'prin1-to-string
746 (mapcar 'viper-display-macro list
)))))
750 ;; alist is the alist of macros
751 ;; str is the fast key sequence entered
752 ;; returns: (matching-macro-def . unmatched-suffix-start-index)
753 (defun viper-find-best-matching-macro (alist str
)
756 (str-len (length str
))
757 match unmatched-start-idx found macro-def
)
758 (while (and (not found
) lis
)
759 (setq macro-def
(car lis
)
760 def-len
(length (car macro-def
)))
761 (if (and (>= str-len def-len
)
762 (equal (car macro-def
) (viper-subseq str
0 def-len
)))
763 (if (or (viper-kbd-buf-definition macro-def
)
764 (viper-kbd-mode-definition macro-def
)
765 (viper-kbd-global-definition macro-def
))
768 (setq lis
(cdr lis
)))
771 (setq match macro-def
772 unmatched-start-idx def-len
)
774 unmatched-start-idx
0))
776 (cons match unmatched-start-idx
)))
780 ;; returns a list of names of macros defined for the current buffer
781 (defun viper-this-buffer-macros (macro-alist)
784 (mapcar (lambda (elt)
785 (if (or (viper-kbd-buf-definition elt
)
786 (viper-kbd-mode-definition elt
)
787 (viper-kbd-global-definition elt
))
790 (setq candidates
(delq nil candidates
))))
793 ;; if seq of Viper key symbols (representing a macro) can be converted to a
794 ;; string--do so. Otherwise, do nothing.
795 (defun viper-display-macro (macro-name-or-body)
796 (cond ((viper-char-symbol-sequence-p macro-name-or-body
)
797 (mapconcat 'symbol-name macro-name-or-body
""))
798 ((viper-char-array-p macro-name-or-body
)
799 (mapconcat 'char-to-string macro-name-or-body
""))
800 (t macro-name-or-body
)))
802 ;; convert sequence of events (that came presumably from emacs kbd macro) into
803 ;; Viper's macro, which is a vector of the form
805 ;; Each desc is either a symbol of (meta symb), (shift symb), etc.
806 ;; Here we purge events that happen to be lists. In most cases, these events
807 ;; got into a macro definition unintentionally; say, when the user moves mouse
808 ;; during a macro definition, then something like (switch-frame ...) might get
809 ;; in. Another reason for purging lists-events is that we can't store them in
810 ;; textual form (say, in .emacs) and then read them back.
811 (defun viper-events-to-macro (event-seq)
812 (vconcat (delq nil
(mapcar (lambda (elt) (if (consp elt
)
814 (viper-event-key elt
)))
817 ;; convert strings or arrays of characters to Viper macro form
818 (defun viper-char-array-to-macro (array)
819 (let ((vec (vconcat array
))
821 (if (featurep 'xemacs
)
822 (setq macro
(mapcar 'character-to-event vec
))
824 (vconcat (mapcar 'viper-event-key macro
))))
826 ;; For macros bodies and names, goes over MACRO and checks if all members are
827 ;; names of keys (actually, it only checks if they are symbols or lists
828 ;; if a digit is found, it is converted into a symbol (e.g., 0 -> \0, etc).
829 ;; If MACRO is not a list or vector -- doesn't change MACRO.
830 (defun viper-fixup-macro (macro)
831 (let ((len (length macro
))
834 (if (or (vectorp macro
) (listp macro
))
835 (while (and (< idx len
) (not break
))
836 (setq elt
(elt macro idx
))
839 (if (and (<= 0 elt
) (<= elt
9))
840 (cond ((arrayp macro
)
843 (intern (char-to-string (+ ?
0 elt
)))))
845 (setcar (nthcdr idx macro
)
846 (intern (char-to-string (+ ?
0 elt
)))))
849 (viper-fixup-macro elt
))
852 (setq idx
(1+ idx
))))
855 (error "Wrong type macro component, symbol-or-listp, %S" elt
)
858 (defun viper-macro-to-events (macro-body)
859 (vconcat (mapcar 'viper-key-to-emacs-key macro-body
)))
863 ;;; Reading fast key sequences
865 ;; Assuming that CHAR was the first character in a fast succession of key
866 ;; strokes, read the rest. Return the vector of keys that was entered in
867 ;; this fast succession of key strokes.
868 ;; A fast keysequence is one that is terminated by a pause longer than
869 ;; viper-fast-keyseq-timeout.
870 (defun viper-read-fast-keysequence (event macro-alist
)
871 (let ((lis (vector event
))
873 (while (and (viper-fast-keysequence-p)
874 (viper-keyseq-is-a-possible-macro lis macro-alist
))
875 ;; Seems that viper-read-event is more robust here. We need to be able to
876 ;; place these events on unread-command-events list. If we use
877 ;; viper-read-key then events will be converted to keys, and sometimes
878 ;; (e.g., (control \[)) those keys differ from the corresponding events.
879 ;; So, do not use (setq next-event (viper-read-key))
880 (setq next-event
(viper-read-event))
881 (or (viper-mouse-event-p next-event
)
882 (setq lis
(vconcat lis
(vector next-event
)))))
886 ;;; Keyboard macros in registers
888 ;; sets register to last-kbd-macro carefully.
889 (defun viper-set-register-macro (reg)
890 (if (get-register reg
)
891 (if (y-or-n-p "Register contains data. Overwrite? ")
894 "Macro not saved in register. Can still be invoked via `C-x e'")))
895 (set-register reg last-kbd-macro
))
897 (defun viper-register-macro (count)
898 "Keyboard macros in registers - a modified \@ command."
900 (let ((reg (downcase (read-char))))
901 (cond ((or (and (<= ?a reg
) (<= reg ?z
)))
902 (setq viper-last-macro-reg reg
)
903 (if defining-kbd-macro
906 (viper-set-register-macro reg
))
907 (execute-kbd-macro (get-register reg
) count
)))
908 ((or (= ?
@ reg
) (= ?\^j reg
) (= ?\^m reg
))
909 (if viper-last-macro-reg
911 (error "No previous kbd macro"))
912 (execute-kbd-macro (get-register viper-last-macro-reg
) count
))
914 (start-kbd-macro count
))
916 (setq reg
(downcase (read-char)))
917 (if (or (and (<= ?a reg
) (<= reg ?z
)))
919 (setq viper-last-macro-reg reg
)
920 (viper-set-register-macro reg
))))
922 (error "`%c': Unknown register" reg
)))))
925 (defun viper-global-execute ()
926 "Call last keyboad macro for each line in the region."
927 (if (> (point) (mark t
)) (exchange-point-and-mark))
929 (call-last-kbd-macro)
930 (while (< (point) (mark t
))
933 (call-last-kbd-macro)))
936 ;; arch-tag: ecd3cc5c-8cd0-4bbe-b2ec-7e75a4b7d0aa
937 ;;; viper-macs.el ends here