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 the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, 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
) " ....\"" " ....]"))
395 (format "Save this macro in %s? "
396 (vip-abbreviate-file-name vip-custom-file-name
)))
397 (vip-save-string-in-file
398 (format "\n(vip-record-kbd-macro %S '%S %s '%S)"
399 (vip-display-macro macro-name
)
401 ;; if we don't let vector macro-body through %S,
402 ;; the symbols `\.' `\[' etc will be converted into
403 ;; characters, causing invalid read error on recorded
405 ;; I am not sure is macro-body can still be a string at
406 ;; this point, but I am preserving this option anyway.
407 (if (vectorp macro-body
)
408 (format "%S" macro-body
)
411 vip-custom-file-name
))
418 (cond ((eq scope t
) (list nil nil
(cons t nil
)))
420 (list nil
(list (cons scope nil
)) (cons t nil
)))
422 (list (list (cons scope nil
)) nil
(cons t nil
))))))
423 (setq old-elt
(assoc macro-name
(eval macro-alist-var
)))
427 ;; insert new-elt in macro-alist-var and keep the list sorted
430 (vector (vip-key-to-emacs-key (aref macro-name
0)))
431 'vip-exec-mapped-kbd-macro
)
432 (setq lis
(eval macro-alist-var
))
433 (while (and lis
(string< (vip-array-to-string (car (car lis
)))
434 (vip-array-to-string macro-name
)))
435 (setq lis2
(cons (car lis
) lis2
))
436 (setq lis
(cdr lis
)))
438 (setq lis2
(reverse lis2
))
439 (set macro-alist-var
(append lis2
(cons new-elt lis
)))
440 (setq old-elt new-elt
)))
442 (cond ((eq scope t
) (vip-kbd-global-pair old-elt
))
443 ((symbolp scope
) (assoc scope
(vip-kbd-mode-alist old-elt
)))
444 ((stringp scope
) (assoc scope
(vip-kbd-buf-alist old-elt
)))))
446 (setcdr old-sub-elt macro-body
)
447 (cond ((symbolp scope
) (setcar (cdr (cdr old-elt
))
448 (cons (cons scope macro-body
)
449 (vip-kbd-mode-alist old-elt
))))
450 ((stringp scope
) (setcar (cdr old-elt
)
451 (cons (cons scope macro-body
)
452 (vip-kbd-buf-alist old-elt
))))))
457 ;; macro name must be a vector of vip-style keys
458 (defun vip-unrecord-kbd-macro (macro-name state
)
459 "Delete macro MACRO-NAME from Viper STATE.
460 MACRO-NAME must be a vector of vip-style keys. This command is used by Viper
461 internally, but the user can also use it in ~/.vip to delete pre-defined macros
462 supplied with Viper. The best way to avoid mistakes in macro names to be passed
463 to this function is to use vip-describe-kbd-macros and copy the name from
465 (let* (state-name keymap
467 (cond ((eq state
'vi-state
)
468 (setq state-name
"Vi state"
469 keymap vip-vi-kbd-map
)
470 'vip-vi-kbd-macro-alist
)
471 ((memq state
'(insert-state replace-state
))
472 (setq state-name
"Insert state"
473 keymap vip-insert-kbd-map
)
474 'vip-insert-kbd-macro-alist
)
476 (setq state-name
"Emacs state"
477 keymap vip-emacs-kbd-map
)
478 'vip-emacs-kbd-macro-alist
)
480 buf-mapping mode-mapping global-mapping
481 macro-pair macro-entry
)
483 ;; Macro-name is usually a vector. However, command history or macros
484 ;; recorded in ~/.vip may appear as strings. So, convert to vectors.
485 (setq macro-name
(vip-fixup-macro macro-name
))
486 (if (vip-char-array-p macro-name
)
487 (setq macro-name
(vip-char-array-to-macro macro-name
)))
489 (setq macro-entry
(assoc macro-name
(eval macro-alist-var
)))
490 (if (= (length macro-name
) 0)
491 (error "Can't unmap an empty macro name"))
492 (if (null macro-entry
)
493 (error "%S is not mapped to a macro for %s in `%s'"
494 (vip-display-macro macro-name
)
495 state-name
(buffer-name)))
497 (setq buf-mapping
(vip-kbd-buf-pair macro-entry
)
498 mode-mapping
(vip-kbd-mode-pair macro-entry
)
499 global-mapping
(vip-kbd-global-pair macro-entry
))
501 (cond ((and (cdr buf-mapping
)
502 (or (and (not (cdr mode-mapping
)) (not (cdr global-mapping
)))
504 (format "Unmap %S for `%s' only? "
505 (vip-display-macro macro-name
)
507 (setq macro-pair buf-mapping
)
508 (message "%S is unmapped for %s in `%s'"
509 (vip-display-macro macro-name
)
510 state-name
(buffer-name)))
511 ((and (cdr mode-mapping
)
512 (or (not (cdr global-mapping
))
514 (format "Unmap %S for the major mode `%S' only? "
515 (vip-display-macro macro-name
)
517 (setq macro-pair mode-mapping
)
518 (message "%S is unmapped for %s in %S"
519 (vip-display-macro macro-name
) state-name major-mode
))
520 ((cdr (setq macro-pair
(vip-kbd-global-pair macro-entry
)))
522 "Global mapping of %S for %s is removed"
523 (vip-display-macro macro-name
) state-name
))
524 (t (error "%S is not mapped to a macro for %s in `%s'"
525 (vip-display-macro macro-name
)
526 state-name
(buffer-name))))
527 (setcdr macro-pair nil
)
528 (or (cdr buf-mapping
)
532 (set macro-alist-var
(delq macro-entry
(eval macro-alist-var
)))
533 (if (vip-can-release-key (aref macro-name
0)
534 (eval macro-alist-var
))
537 (vector (vip-key-to-emacs-key (aref macro-name
0)))
542 ;; Check if MACRO-ALIST has an entry for a macro name starting with
543 ;; CHAR. If not, this indicates that the binding for this char
544 ;; in vip-vi/insert-kbd-map can be released.
545 (defun vip-can-release-key (char macro-alist
)
546 (let ((lis macro-alist
)
550 (while (and lis can-release
)
551 (setq macro-name
(car (car lis
)))
552 (if (eq char
(aref macro-name
0))
553 (setq can-release nil
))
554 (setq lis
(cdr lis
)))
558 (defun vip-exec-mapped-kbd-macro (count)
559 "Dispatch kbd macro."
561 (let* ((macro-alist (cond ((eq vip-current-state
'vi-state
)
562 vip-vi-kbd-macro-alist
)
563 ((memq vip-current-state
564 '(insert-state replace-state
))
565 vip-insert-kbd-macro-alist
)
567 vip-emacs-kbd-macro-alist
)))
568 (unmatched-suffix "")
569 ;; Macros and keys are executed with other macros turned off
570 ;; For macros, this is done to avoid macro recursion
571 vip-vi-kbd-minor-mode vip-insert-kbd-minor-mode
572 vip-emacs-kbd-minor-mode
573 next-best-match keyseq event-seq
574 macro-first-char macro-alist-elt macro-body
577 (setq macro-first-char last-command-event
578 event-seq
(vip-read-fast-keysequence macro-first-char macro-alist
)
579 keyseq
(vip-events-to-macro event-seq
)
580 macro-alist-elt
(assoc keyseq macro-alist
)
581 next-best-match
(vip-find-best-matching-macro macro-alist keyseq
))
583 (if (null macro-alist-elt
)
584 (setq macro-alist-elt
(car next-best-match
)
585 unmatched-suffix
(subseq event-seq
(cdr next-best-match
))))
587 (cond ((null macro-alist-elt
))
588 ((setq macro-body
(vip-kbd-buf-definition macro-alist-elt
)))
589 ((setq macro-body
(vip-kbd-mode-definition macro-alist-elt
)))
590 ((setq macro-body
(vip-kbd-global-definition macro-alist-elt
))))
592 ;; when defining keyboard macro, don't use the macro mappings
593 (if (and macro-body
(not defining-kbd-macro
))
594 ;; block cmd executed as part of a macro from entering command history
595 (let ((command-history command-history
))
596 (setq vip-this-kbd-macro
(car macro-alist-elt
))
597 (execute-kbd-macro (vip-macro-to-events macro-body
) count
)
598 (setq vip-this-kbd-macro nil
599 vip-last-kbd-macro
(car macro-alist-elt
))
600 (vip-set-unread-command-events unmatched-suffix
))
601 ;; If not a macro, or the macro is suppressed while defining another
602 ;; macro, put keyseq back on the event queue
603 (vip-set-unread-command-events event-seq
)
604 ;; if the user typed arg, then use it if prefix arg is not set by
605 ;; some other command (setting prefix arg can happen if we do, say,
606 ;; 2dw and there is a macro starting with 2. Then control will go to
608 (or prefix-arg
(setq prefix-arg count
))
609 (setq command
(key-binding (read-key-sequence nil
)))
610 (if (commandp command
)
611 (command-execute command
)
617 ;;; Displaying and completing macros
619 (defun vip-describe-kbd-macros ()
620 "Show currently defined keyboard macros."
622 (with-output-to-temp-buffer " *vip-info*"
623 (princ "Macros in Vi state:\n===================\n")
624 (mapcar 'vip-describe-one-macro vip-vi-kbd-macro-alist
)
625 (princ "\n\nMacros in Insert and Replace states:\n====================================\n")
626 (mapcar 'vip-describe-one-macro vip-insert-kbd-macro-alist
)
627 (princ "\n\nMacros in Emacs state:\n======================\n")
628 (mapcar 'vip-describe-one-macro vip-emacs-kbd-macro-alist
)
631 (defun vip-describe-one-macro (macro)
632 (princ (format "\n *** Mappings for %S:\n ------------\n"
633 (vip-display-macro (car macro
))))
634 (princ " ** Buffer-specific:")
635 (if (vip-kbd-buf-alist macro
)
636 (mapcar 'vip-describe-one-macro-elt
(vip-kbd-buf-alist macro
))
638 (princ "\n ** Mode-specific:")
639 (if (vip-kbd-mode-alist macro
)
640 (mapcar 'vip-describe-one-macro-elt
(vip-kbd-mode-alist macro
))
642 (princ "\n ** Global:")
643 (if (vip-kbd-global-definition macro
)
646 (cdr (vip-kbd-global-pair macro
))))
650 (defun vip-describe-one-macro-elt (elt)
651 (let ((name (car elt
))
653 (princ (format "\n * %S:\n %S\n" name defn
))))
657 ;; check if SEQ is a prefix of some car of an element in ALIST
658 (defun vip-keyseq-is-a-possible-macro (seq alist
)
659 (let ((converted-seq (vip-events-to-macro seq
)))
662 (function (lambda (elt)
663 (vip-prefix-subseq-p converted-seq elt
)))
664 (vip-this-buffer-macros alist
))))))
666 ;; whether SEQ1 is a prefix of SEQ2
667 (defun vip-prefix-subseq-p (seq1 seq2
)
668 (let ((len1 (length seq1
))
669 (len2 (length seq2
)))
671 (equal seq1
(subseq seq2
0 len1
)))))
673 ;; find the longest common prefix
674 (defun vip-common-seq-prefix (&rest seqs
)
675 (let* ((first (car seqs
))
680 (if (= (length seqs
) 0)
682 (setq len
(apply 'min
(mapcar 'length seqs
))))
685 (mapcar (function (lambda (s)
686 (equal (elt first idx
)
689 (setq pref
(vconcat pref
(vector (elt first idx
)))))
693 ;; get all sequences that match PREFIX from a given A-LIST
694 (defun vip-extract-matching-alist-members (pref alist
)
695 (delq nil
(mapcar (function (lambda (elt)
696 (if (vip-prefix-subseq-p pref elt
)
698 (vip-this-buffer-macros alist
))))
700 (defun vip-do-sequence-completion (seq alist compl-message
)
701 (let* ((matches (vip-extract-matching-alist-members seq alist
))
702 (new-seq (apply 'vip-common-seq-prefix matches
))
704 (cond ((and (equal seq new-seq
) (= (length matches
) 1))
705 (message "%s (Sole completion)" compl-message
)
708 (message "%s (No match)" compl-message
)
711 ((member seq matches
)
712 (message "%s (Complete, but not unique)" compl-message
)
714 (vip-display-vector-completions matches
))
716 (vip-display-vector-completions matches
)))
720 (defun vip-display-vector-completions (list)
721 (with-output-to-temp-buffer "*Completions*"
722 (display-completion-list
723 (mapcar 'prin1-to-string
724 (mapcar 'vip-display-macro list
)))))
728 ;; alist is the alist of macros
729 ;; str is the fast key sequence entered
730 ;; returns: (matching-macro-def . unmatched-suffix-start-index)
731 (defun vip-find-best-matching-macro (alist str
)
734 (str-len (length str
))
735 match unmatched-start-idx found macro-def
)
736 (while (and (not found
) lis
)
737 (setq macro-def
(car lis
)
738 def-len
(length (car macro-def
)))
739 (if (and (>= str-len def-len
)
740 (equal (car macro-def
) (subseq str
0 def-len
)))
741 (if (or (vip-kbd-buf-definition macro-def
)
742 (vip-kbd-mode-definition macro-def
)
743 (vip-kbd-global-definition macro-def
))
746 (setq lis
(cdr lis
)))
749 (setq match macro-def
750 unmatched-start-idx def-len
)
752 unmatched-start-idx
0))
754 (cons match unmatched-start-idx
)))
758 ;; returns a list of names of macros defined for the current buffer
759 (defun vip-this-buffer-macros (macro-alist)
764 (if (or (vip-kbd-buf-definition elt
)
765 (vip-kbd-mode-definition elt
)
766 (vip-kbd-global-definition elt
))
769 (setq candidates
(delq nil candidates
))))
772 ;; if seq of Viper key symbols (representing a macro) can be converted to a
773 ;; string--do so. Otherwise, do nothing.
774 (defun vip-display-macro (macro-name)
775 (cond ((vip-char-symbol-sequence-p macro-name
)
776 (mapconcat 'symbol-name macro-name
""))
777 ((vip-char-array-p macro-name
)
778 (mapconcat 'char-to-string macro-name
""))
781 (defun vip-events-to-macro (event-seq)
782 (vconcat (mapcar 'vip-event-key event-seq
)))
784 ;; convert strings or arrays of characters to Viper macro form
785 (defun vip-char-array-to-macro (array)
786 (let ((vec (vconcat array
))
789 (setq macro
(mapcar 'character-to-event vec
))
791 (vconcat (mapcar 'vip-event-key macro
))))
793 ;; For macros bodies and names, goes over and checks if all members are
794 ;; names of keys (actually, it only checks if they are symbols or lists
795 ;; if a digit is found, it is converted into a symbol (0 -> \0, etc).
796 ;; If not list or vector, doesn't change its argument
797 (defun vip-fixup-macro (macro)
798 (let ((len (length macro
))
801 (if (or (vectorp macro
) (listp macro
))
802 (while (and (< idx len
) (not break
))
803 (setq elt
(elt macro idx
))
806 (if (and (<= 0 elt
) (<= elt
9))
807 (cond ((arrayp macro
)
810 (intern (char-to-string (+ ?
0 elt
)))))
812 (setcar (nthcdr idx macro
)
813 (intern (char-to-string (+ ?
0 elt
)))))
817 (vip-fixup-macro elt
))
820 (setq idx
(1+ idx
))))
823 (error "Wrong type macro component, symbol-or-listp, %S" elt
)
826 (defun vip-char-array-p (array)
827 (eval (cons 'and
(mapcar 'numberp array
))))
829 (defun vip-macro-to-events (macro-body)
830 (vconcat (mapcar 'vip-key-to-emacs-key macro-body
)))
833 ;; check if vec is a vector of character symbols
834 (defun vip-char-symbol-sequence-p (vec)
840 (function (lambda (elt)
841 (and (symbolp elt
) (= (length (symbol-name elt
)) 1))))
845 ;; Check if vec is a vector of key-press events representing characters
847 (defun vip-event-vector-p (vec)
849 (eval (cons 'and
(mapcar '(lambda (elt) (if (eventp elt
) t
)) vec
)))))
852 ;;; Reading fast key sequences
854 ;; Assuming that CHAR was the first character in a fast succession of key
855 ;; strokes, read the rest. Return the vector of keys that was entered in
856 ;; this fast succession of key strokes.
857 ;; A fast keysequence is one that is terminated by a pause longer than
858 ;; vip-fast-keyseq-timeout.
859 (defun vip-read-fast-keysequence (event macro-alist
)
860 (let ((lis (vector event
))
862 (while (and (vip-fast-keysequence-p)
863 (vip-keyseq-is-a-possible-macro lis macro-alist
))
864 (setq next-event
(vip-read-key))
865 ;;(setq next-event (vip-read-event))
866 (or (vip-mouse-event-p next-event
)
867 (setq lis
(vconcat lis
(vector next-event
)))))
871 ;;; Keyboard macros in registers
873 ;; sets register to last-kbd-macro carefully.
874 (defun vip-set-register-macro (reg)
875 (if (get-register reg
)
876 (if (y-or-n-p "Register contains data. Overwrite? ")
879 "Macro not saved in register. Can still be invoked via `C-x e'")))
880 (set-register reg last-kbd-macro
))
882 (defun vip-register-macro (count)
883 "Keyboard macros in registers - a modified \@ command."
885 (let ((reg (downcase (read-char))))
886 (cond ((or (and (<= ?a reg
) (<= reg ?z
)))
887 (setq vip-last-macro-reg reg
)
888 (if defining-kbd-macro
891 (vip-set-register-macro reg
))
892 (execute-kbd-macro (get-register reg
) count
)))
893 ((or (= ?
@ reg
) (= ?\^j reg
) (= ?\^m reg
))
894 (if vip-last-macro-reg
896 (error "No previous kbd macro"))
897 (execute-kbd-macro (get-register vip-last-macro-reg
) count
))
899 (start-kbd-macro count
))
901 (setq reg
(downcase (read-char)))
902 (if (or (and (<= ?a reg
) (<= reg ?z
)))
904 (setq vip-last-macro-reg reg
)
905 (vip-set-register-macro reg
))))
907 (error (format "`%c': Unknown register" reg
))))))
910 (defun vip-global-execute ()
911 "Call last keyboad macro for each line in the region."
912 (if (> (point) (mark t
)) (exchange-point-and-mark))
914 (call-last-kbd-macro)
915 (while (< (point) (mark t
))
918 (call-last-kbd-macro)))
921 (provide 'viper-macs
)
923 ;;; viper-macs.el ends here