*** empty log message ***
[emacs.git] / lisp / emulation / viper-macs.el
blob1ef27a3389ae481dfdf1c9a4318e9107cf87913b
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)
10 ;; any later version.
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.
22 (require 'viper-util)
24 ;;; Variables
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
39 ;; in :map and :map!
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
55 commands.
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)'.")
63 ;;; Code
65 ;; Ex map command
66 (defun ex-map ()
67 (let ((mod-char "")
68 macro-name macro-body map-args ins)
69 (save-window-excursion
70 (set-buffer vip-ex-work-buf)
71 (if (looking-at "!")
72 (progn
73 (setq ins t
74 mod-char "!")
75 (forward-char 1))))
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))
80 (if 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))
86 (start-kbd-macro nil)
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")))
96 ;; Ex unmap
97 (defun ex-unmap ()
98 (let ((mod-char "")
99 temp macro-name ins)
100 (save-window-excursion
101 (set-buffer vip-ex-work-buf)
102 (if (looking-at "!")
103 (progn
104 (setq ins t
105 mod-char "!")
106 (forward-char 1))))
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)
119 (key-seq [])
120 temp key event message
121 macro-name macro-body args)
123 (condition-case nil
124 (setq args (concat (ex-get-inline-cmd-args ".*map[!]*[ \t]?" "\n\C-m")
125 " nil nil ")
126 temp (read-from-string args)
127 macro-name (car temp)
128 macro-body (car (read-from-string args (cdr temp))))
129 (error
130 (signal
131 'error
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
137 (if macro-name
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)
143 (while
144 (not (member key
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
148 (if (member
150 '(?\b ?\d '^? '^H (control h) (control \?) backspace delete))
151 (setq key-seq (subseq key-seq 0 (- (length key-seq) 2))))
152 (setq message
153 (format ":map%s %s"
154 variant (if (> (length key-seq) 0)
155 (prin1-to-string (vip-display-macro key-seq))
156 "")))
157 (message message)
158 (setq event (vip-read-key))
159 ;;(setq event (vip-read-event))
160 (setq key
161 (if (vip-mouse-event-p event)
162 (progn
163 (message "%s (No mouse---only keyboard keys, please)"
164 message)
165 (sit-for 2)
166 nil)
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)))
177 (if macro-body
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
194 ;; completing-read
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
199 event message
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)
206 (while
207 (not
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
211 (cond ((member
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))))
217 (setq message
218 (format ":unmap%s %s"
219 variant (if (> (length key-seq) 0)
220 (prin1-to-string
221 (vip-display-macro key-seq))
222 "")))
223 (setq key-seq
224 (vip-do-sequence-completion key-seq macro-alist message))
226 (setq message
227 (format ":unmap%s %s"
228 variant (if (> (length key-seq) 0)
229 (prin1-to-string
230 (vip-display-macro key-seq))
231 "")))
232 (message message)
233 (setq event (vip-read-key))
234 ;;(setq event (vip-read-event))
235 (setq key
236 (if (vip-mouse-event-p event)
237 (progn
238 (message "%s (No mouse---only keyboard keys, please)"
239 message)
240 (sit-for 2)
241 nil)
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."
258 (interactive)
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)
271 (or ignore
272 (progn
273 (end-kbd-macro nil)
274 (setq macro-body (vip-events-to-macro last-kbd-macro))
275 ;; always go back to Vi, since this is where we started
276 ;; defining macro
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
313 (macro-alist-var
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
328 temp lis lis2)
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
343 (or (eq scope t)
344 (stringp scope)
345 (and scope (symbolp scope))
346 (progn
347 (setq scope
348 (cond
349 ((y-or-n-p
350 (format
351 "Map this macro for buffer `%s' only? "
352 (buffer-name)))
353 (setq msg
354 (format
355 "%S is mapped to %s for %s in `%s'"
356 (vip-display-macro macro-name)
357 (vip-abbreviate-string
358 (format
359 "%S"
360 (setq temp (vip-display-macro macro-body)))
361 14 "" ""
362 (if (stringp temp) " ....\"" " ....]"))
363 state-name (buffer-name)))
364 (buffer-name))
365 ((y-or-n-p
366 (format
367 "Map this macro for the major mode `%S' only? "
368 major-mode))
369 (setq msg
370 (format
371 "%S is mapped to %s for %s in `%S'"
372 (vip-display-macro macro-name)
373 (vip-abbreviate-string
374 (format
375 "%S"
376 (setq temp (vip-display-macro macro-body)))
377 14 "" ""
378 (if (stringp macro-body) " ....\"" " ....]"))
379 state-name major-mode))
380 major-mode)
382 (setq msg
383 (format
384 "%S is globally mapped to %s in %s"
385 (vip-display-macro macro-name)
386 (vip-abbreviate-string
387 (format
388 "%S"
389 (setq temp (vip-display-macro macro-body)))
390 14 "" ""
391 (if (stringp macro-body) " ....\"" " ....]"))
392 state-name))
393 t)))
394 (if (y-or-n-p
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)
400 state
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
404 ;; macros in .vip.
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)
409 macro-body)
410 scope)
411 vip-custom-file-name))
413 (message msg)
416 (setq new-elt
417 (cons macro-name
418 (cond ((eq scope t) (list nil nil (cons t nil)))
419 ((symbolp scope)
420 (list nil (list (cons scope nil)) (cons t nil)))
421 ((stringp scope)
422 (list (list (cons scope nil)) nil (cons t nil))))))
423 (setq old-elt (assoc macro-name (eval macro-alist-var)))
425 (if (null old-elt)
426 (progn
427 ;; insert new-elt in macro-alist-var and keep the list sorted
428 (define-key
429 keymap
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)))
441 (setq old-sub-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)))))
445 (if old-sub-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
464 there."
465 (let* (state-name keymap
466 (macro-alist-var
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)))
503 (y-or-n-p
504 (format "Unmap %S for `%s' only? "
505 (vip-display-macro macro-name)
506 (buffer-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))
513 (y-or-n-p
514 (format "Unmap %S for the major mode `%S' only? "
515 (vip-display-macro macro-name)
516 major-mode))))
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)))
521 (message
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)
529 (cdr mode-mapping)
530 (cdr global-mapping)
531 (progn
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))
535 (define-key
536 keymap
537 (vector (vip-key-to-emacs-key (aref macro-name 0)))
538 nil))
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)
547 (can-release t)
548 macro-name)
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)))
555 can-release))
558 (defun vip-exec-mapped-kbd-macro (count)
559 "Dispatch kbd macro."
560 (interactive "P")
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
575 command)
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
607 ;; this routine
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)
612 (beep 1)))
617 ;;; Displaying and completing macros
619 (defun vip-describe-kbd-macros ()
620 "Show currently defined keyboard macros."
621 (interactive)
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))
637 (princ " none\n"))
638 (princ "\n ** Mode-specific:")
639 (if (vip-kbd-mode-alist macro)
640 (mapcar 'vip-describe-one-macro-elt (vip-kbd-mode-alist macro))
641 (princ " none\n"))
642 (princ "\n ** Global:")
643 (if (vip-kbd-global-definition macro)
644 (princ
645 (format "\n %S"
646 (cdr (vip-kbd-global-pair macro))))
647 (princ " none"))
648 (princ "\n"))
650 (defun vip-describe-one-macro-elt (elt)
651 (let ((name (car elt))
652 (defn (cdr 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)))
660 (eval (cons 'or
661 (mapcar
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)))
670 (if (<= len1 len2)
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))
676 (rest (cdr seqs))
677 (pref [])
678 (idx 0)
679 len)
680 (if (= (length seqs) 0)
681 (setq len 0)
682 (setq len (apply 'min (mapcar 'length seqs))))
683 (while (< idx len)
684 (if (eval (cons 'and
685 (mapcar (function (lambda (s)
686 (equal (elt first idx)
687 (elt s idx))))
688 rest)))
689 (setq pref (vconcat pref (vector (elt first idx)))))
690 (setq idx (1+ idx)))
691 pref))
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)
697 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)
706 (sit-for 2))
707 ((null matches)
708 (message "%s (No match)" compl-message)
709 (sit-for 2)
710 (setq new-seq seq))
711 ((member seq matches)
712 (message "%s (Complete, but not unique)" compl-message)
713 (sit-for 2)
714 (vip-display-vector-completions matches))
715 ((equal seq new-seq)
716 (vip-display-vector-completions matches)))
717 new-seq))
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)
732 (let ((lis alist)
733 (def-len 0)
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))
744 (setq found t))
746 (setq lis (cdr lis)))
748 (if found
749 (setq match macro-def
750 unmatched-start-idx def-len)
751 (setq match nil
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)
760 (let (candidates)
761 (setq candidates
762 (mapcar (function
763 (lambda (elt)
764 (if (or (vip-kbd-buf-definition elt)
765 (vip-kbd-mode-definition elt)
766 (vip-kbd-global-definition elt))
767 (car elt))))
768 macro-alist))
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 ""))
779 (t 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))
787 macro)
788 (if vip-xemacs-p
789 (setq macro (mapcar 'character-to-event vec))
790 (setq macro 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))
799 (idx 0)
800 elt break)
801 (if (or (vectorp macro) (listp macro))
802 (while (and (< idx len) (not break))
803 (setq elt (elt macro idx))
804 (cond ((numberp elt)
805 ;; fix number
806 (if (and (<= 0 elt) (<= elt 9))
807 (cond ((arrayp macro)
808 (aset macro
810 (intern (char-to-string (+ ?0 elt)))))
811 ((listp macro)
812 (setcar (nthcdr idx macro)
813 (intern (char-to-string (+ ?0 elt)))))
815 ;;(setq break t)))
816 ((listp elt)
817 (vip-fixup-macro elt))
818 ((symbolp elt) nil)
819 (t (setq break t)))
820 (setq idx (1+ idx))))
822 (if break
823 (error "Wrong type macro component, symbol-or-listp, %S" elt)
824 macro)))
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)
835 (and
836 (sequencep vec)
837 (eval
838 (cons 'and
839 (mapcar
840 (function (lambda (elt)
841 (and (symbolp elt) (= (length (symbol-name elt)) 1))))
842 vec)))))
845 ;; Check if vec is a vector of key-press events representing characters
846 ;; XEmacs only
847 (defun vip-event-vector-p (vec)
848 (and (vectorp 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))
861 next-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)))))
868 lis))
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? ")
878 (error
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."
884 (interactive "P")
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
889 (progn
890 (end-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))
898 ((= ?\# reg)
899 (start-kbd-macro count))
900 ((= ?! reg)
901 (setq reg (downcase (read-char)))
902 (if (or (and (<= ?a reg) (<= reg ?z)))
903 (progn
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))
913 (beginning-of-line)
914 (call-last-kbd-macro)
915 (while (< (point) (mark t))
916 (forward-line 1)
917 (beginning-of-line)
918 (call-last-kbd-macro)))
921 (provide 'viper-macs)
923 ;;; viper-macs.el ends here