annotate lisp/viper/viper-macs.el @ 124:9b50b4588a93 r20-1b15

Import from CVS: tag r20-1b15
author cvs
date Mon, 13 Aug 2007 09:26:39 +0200
parents 6a378aca36af
children 0132846995bd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; viper-macs.el --- functions implementing keyboard macros for Viper
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
82
6a378aca36af Import from CVS: tag r20-0b91
cvs
parents: 80
diff changeset
3 ;; Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; This file is part of GNU Emacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;; it under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;; GNU Emacs is distributed in the hope that it will be useful,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;; GNU General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;; You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 ;; Boston, MA 02111-1307, USA.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 ;; Code
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23
80
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
24 (provide 'viper-macs)
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
25
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
26 ;; compiler pacifier
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
27 (defvar vip-ex-work-buf)
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
28 (defvar vip-custom-file-name)
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
29 (defvar vip-current-state)
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
30
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
31 (eval-when-compile
82
6a378aca36af Import from CVS: tag r20-0b91
cvs
parents: 80
diff changeset
32 (let ((load-path (cons (expand-file-name ".") load-path)))
80
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
33 (or (featurep 'viper-util)
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
34 (load "viper-util.el" nil nil 'nosuffix))
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
35 (or (featurep 'viper-keym)
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
36 (load "viper-keym.el" nil nil 'nosuffix))
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
37 (or (featurep 'viper-mous)
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
38 (load "viper-mous.el" nil nil 'nosuffix))
82
6a378aca36af Import from CVS: tag r20-0b91
cvs
parents: 80
diff changeset
39 (or (featurep 'viper)
6a378aca36af Import from CVS: tag r20-0b91
cvs
parents: 80
diff changeset
40 (load "viper.el" nil nil 'nosuffix))
80
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
41 ))
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
42 ;; end pacifier
1ce6082ce73f Import from CVS: tag r20-0b90
cvs
parents: 70
diff changeset
43
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 (require 'viper-util)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 (require 'viper-keym)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 ;;; Variables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 ;; Register holding last macro.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 (defvar vip-last-macro-reg nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 ;; format of the elements of kbd alists:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 ;; (name ((buf . macr)...(buf . macr)) ((maj-mode . macr)...) (t . macr))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 ;; kbd macro alist for Vi state
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 (defvar vip-vi-kbd-macro-alist nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 ;; same for insert/replace state
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 (defvar vip-insert-kbd-macro-alist nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 ;; same for emacs state
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 (defvar vip-emacs-kbd-macro-alist nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 ;; Internal var that passes info between start-kbd-macro and end-kbd-macro
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 ;; in :map and :map!
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 (defvar vip-kbd-macro-parameters nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 (defvar vip-this-kbd-macro nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 "Vector of keys representing the name of currently running Viper kbd macro.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (defvar vip-last-kbd-macro nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 "Vector of keys representing the name of last Viper keyboard macro.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 (defconst vip-fast-keyseq-timeout 200
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 "*Key sequence separated by no more than this many milliseconds is viewed as a macro, if such a macro is defined.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 This also controls ESC-keysequences generated by keyboard function keys.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 (defvar vip-repeat-from-history-key 'f12
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 "Prefix key for invocation of vip-repeat-from-history function,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 which repeats previous destructive commands from the history of such
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 commands.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 This function can then be invoked as <this-key> 1 or <this-key> 2.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 The notation for these keys is borrowed from XEmacs. Basically,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 a key is a symbol, e.g., `a', `\\1', `f2', etc., or a list, e.g.,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 `(meta control f1)'.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 ;;; Code
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 ;; Ex map command
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 (defun ex-map ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 (let ((mod-char "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 macro-name macro-body map-args ins)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (save-window-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (set-buffer vip-ex-work-buf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 (if (looking-at "!")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 (setq ins t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 mod-char "!")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 (forward-char 1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 (setq map-args (ex-map-read-args mod-char)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 macro-name (car map-args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 macro-body (cdr map-args))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 (setq vip-kbd-macro-parameters (list ins mod-char macro-name macro-body))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (if macro-body
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (vip-end-mapping-kbd-macro 'ignore)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (ex-fixup-history (format "map%s %S" mod-char
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (vip-display-macro macro-name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 ;; if defining macro for insert, switch there for authentic WYSIWYG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (if ins (vip-change-state-to-insert))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 (start-kbd-macro nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (define-key vip-vi-intercept-map "\C-x)" 'vip-end-mapping-kbd-macro)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (define-key vip-insert-intercept-map "\C-x)" 'vip-end-mapping-kbd-macro)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (define-key vip-emacs-intercept-map "\C-x)" 'vip-end-mapping-kbd-macro)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (message "Mapping %S in %s state. Hit `C-x )' to complete the mapping"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (vip-display-macro macro-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (if ins "Insert" "Vi")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 ;; Ex unmap
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 (defun ex-unmap ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 (let ((mod-char "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 temp macro-name ins)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 (save-window-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 (set-buffer vip-ex-work-buf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (if (looking-at "!")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 (setq ins t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 mod-char "!")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 (forward-char 1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 (setq macro-name (ex-unmap-read-args mod-char))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 (setq temp (vip-fixup-macro (vconcat macro-name))) ;; copy and fixup
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 (ex-fixup-history (format "unmap%s %S" mod-char
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (vip-display-macro temp)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 (vip-unrecord-kbd-macro macro-name (if ins 'insert-state 'vi-state))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 ;; read arguments for ex-map
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 (defun ex-map-read-args (variant)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (let ((cursor-in-echo-area t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 (key-seq [])
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 temp key event message
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 macro-name macro-body args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 (condition-case nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (setq args (concat (ex-get-inline-cmd-args ".*map[!]*[ \t]?" "\n\C-m")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 " nil nil ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 temp (read-from-string args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 macro-name (car temp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 macro-body (car (read-from-string args (cdr temp))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 (error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (signal
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 'error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 '("map: Macro name and body must be a quoted string or a vector"))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 ;; We expect macro-name to be a vector, a string, or a quoted string.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 ;; In the second case, it will emerge as a symbol when read from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 ;; the above read-from-string. So we need to convert it into a string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 (if macro-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (cond ((vectorp macro-name) nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 ((stringp macro-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 (setq macro-name (vconcat macro-name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 (t (setq macro-name (vconcat (prin1-to-string macro-name)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 (message ":map%s <Name>" variant)(sit-for 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 (while
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 (not (member key
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 '(?\C-m ?\n (control m) (control j) return linefeed)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 (setq key-seq (vconcat key-seq (if key (vector key) [])))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 ;; the only keys available for editing are these-- no help while there
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 (if (member
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 key
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 '(?\b ?\d '^? '^H (control h) (control \?) backspace delete))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 (setq key-seq (subseq key-seq 0 (- (length key-seq) 2))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 (setq message
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 (format
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 ":map%s %s"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 variant (if (> (length key-seq) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 (prin1-to-string (vip-display-macro key-seq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 "")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 (message message)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 (setq event (vip-read-key))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 ;;(setq event (vip-read-event))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 (setq key
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 (if (vip-mouse-event-p event)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 (message "%s (No mouse---only keyboard keys, please)"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 message)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 (sit-for 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 (vip-event-key event)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 (setq macro-name key-seq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 (if (= (length macro-name) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 (error "Can't map an empty macro name"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 (setq macro-name (vip-fixup-macro macro-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 (if (vip-char-array-p macro-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 (setq macro-name (vip-char-array-to-macro macro-name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 (if macro-body
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 (cond ((vip-char-array-p macro-body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 (setq macro-body (vip-char-array-to-macro macro-body)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 ((vectorp macro-body) nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 (t (error "map: Invalid syntax in macro definition"))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 (setq cursor-in-echo-area nil)(sit-for 0) ; this overcomes xemacs tty bug
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 (cons macro-name macro-body)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 ;; read arguments for ex-unmap
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 (defun ex-unmap-read-args (variant)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 (let ((cursor-in-echo-area t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 (macro-alist (if (string= variant "!")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 vip-insert-kbd-macro-alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 vip-vi-kbd-macro-alist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 ;; these are disabled just in case, to avoid surprises when doing
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 ;; completing-read
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 vip-vi-kbd-minor-mode vip-insert-kbd-minor-mode
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 vip-emacs-kbd-minor-mode
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 vip-vi-intercept-minor-mode vip-insert-intercept-minor-mode
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 vip-emacs-intercept-minor-mode
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 event message
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 key key-seq macro-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 (setq macro-name (ex-get-inline-cmd-args ".*unma?p?[!]*[ \t]*"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 (if (> (length macro-name) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 (message ":unmap%s <Name>" variant) (sit-for 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 (while
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 (not
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 (member key '(?\C-m ?\n (control m) (control j) return linefeed)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 (setq key-seq (vconcat key-seq (if key (vector key) [])))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 ;; the only keys available for editing are these-- no help while there
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 (cond ((member
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 key
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 '(?\b ?\d '^? '^H (control h) (control \?) backspace delete))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 (setq key-seq (subseq key-seq 0 (- (length key-seq) 2))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 ((member key '(tab (control i) ?\t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 (setq key-seq (subseq key-seq 0 (1- (length key-seq))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 (setq message
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 (format
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 ":unmap%s %s"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 variant (if (> (length key-seq) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 (prin1-to-string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 (vip-display-macro key-seq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 "")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 (setq key-seq
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 (vip-do-sequence-completion key-seq macro-alist message))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 (setq message
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 (format
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 ":unmap%s %s"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 variant (if (> (length key-seq) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 (prin1-to-string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 (vip-display-macro key-seq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 "")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 (message message)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 (setq event (vip-read-key))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 ;;(setq event (vip-read-event))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 (setq key
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 (if (vip-mouse-event-p event)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 (message "%s (No mouse---only keyboard keys, please)"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 message)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 (sit-for 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 (vip-event-key event)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 (setq macro-name key-seq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 (if (= (length macro-name) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 (error "Can't unmap an empty macro name"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 ;; convert macro names into vector, if starts with a `['
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 (if (memq (elt macro-name 0) '(?\[ ?\"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 (car (read-from-string macro-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 (vconcat macro-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 ;; Terminate a Vi kbd macro.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 ;; optional argument IGNORE, if t, indicates that we are dealing with an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 ;; existing macro that needs to be registered, but there is no need to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 ;; terminate a kbd macro.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 (defun vip-end-mapping-kbd-macro (&optional ignore)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 (define-key vip-vi-intercept-map "\C-x)" nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 (define-key vip-insert-intercept-map "\C-x)" nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 (define-key vip-emacs-intercept-map "\C-x)" nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 (if (and (not ignore)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 (or (not vip-kbd-macro-parameters)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 (not defining-kbd-macro)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 (error "Not mapping a kbd-macro"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 (let ((mod-char (nth 1 vip-kbd-macro-parameters))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 (ins (nth 0 vip-kbd-macro-parameters))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 (macro-name (nth 2 vip-kbd-macro-parameters))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 (macro-body (nth 3 vip-kbd-macro-parameters)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 (setq vip-kbd-macro-parameters nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 (or ignore
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 (end-kbd-macro nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 (setq macro-body (vip-events-to-macro last-kbd-macro))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 ;; always go back to Vi, since this is where we started
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 ;; defining macro
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 (vip-change-state-to-vi)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 (vip-record-kbd-macro macro-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 (if ins 'insert-state 'vi-state)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 (vip-display-macro macro-body))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 (ex-fixup-history (format "map%s %S %S" mod-char
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 (vip-display-macro macro-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 (vip-display-macro macro-body)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 (defadvice start-kbd-macro (after vip-kbd-advice activate)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 "Remove Viper's intercepting bindings for C-x ).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 This may be needed if the previous `:map' command terminated abnormally."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 (define-key vip-vi-intercept-map "\C-x)" nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 (define-key vip-insert-intercept-map "\C-x)" nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 (define-key vip-emacs-intercept-map "\C-x)" nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 ;;; Recording, unrecording, executing
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 ;; accepts as macro names: strings and vectors.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 ;; strings must be strings of characters; vectors must be vectors of keys
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 ;; in canonic form. the canonic form is essentially the form used in XEmacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 (defun vip-record-kbd-macro (macro-name state macro-body &optional scope)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 "Record a Vi macro. Can be used in `.vip' file to define permanent macros.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 MACRO-NAME is a string of characters or a vector of keys. STATE is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 either `vi-state' or `insert-state'. It specifies the Viper state in which to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 define the macro. MACRO-BODY is a string that represents the keyboard macro.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 Optional SCOPE says whether the macro should be global \(t\), mode-specific
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 \(a major-mode symbol\), or buffer-specific \(buffer name, a string\).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 If SCOPE is nil, the user is asked to specify the scope."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 (let* (state-name keymap
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 (macro-alist-var
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 (cond ((eq state 'vi-state)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 (setq state-name "Vi state"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 keymap vip-vi-kbd-map)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 'vip-vi-kbd-macro-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 ((memq state '(insert-state replace-state))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 (setq state-name "Insert state"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 keymap vip-insert-kbd-map)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 'vip-insert-kbd-macro-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 (setq state-name "Emacs state"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 keymap vip-emacs-kbd-map)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 'vip-emacs-kbd-macro-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 new-elt old-elt old-sub-elt msg
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 temp lis lis2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 (if (= (length macro-name) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 (error "Can't map an empty macro name"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 ;; Macro-name is usually a vector. However, command history or macros
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 ;; recorded in ~/.vip may be recorded as strings. So, convert to vectors.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 (setq macro-name (vip-fixup-macro macro-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 (if (vip-char-array-p macro-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 (setq macro-name (vip-char-array-to-macro macro-name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 (setq macro-body (vip-fixup-macro macro-body))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 (if (vip-char-array-p macro-body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 (setq macro-body (vip-char-array-to-macro macro-body)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 ;; don't ask if scope is given and is of the right type
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 (or (eq scope t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 (stringp scope)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 (and scope (symbolp scope))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 (setq scope
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 ((y-or-n-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 (format
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 "Map this macro for buffer `%s' only? "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 (buffer-name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 (setq msg
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 (format
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 "%S is mapped to %s for %s in `%s'"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 (vip-display-macro macro-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 (vip-abbreviate-string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 (format
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 "%S"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 (setq temp (vip-display-macro macro-body)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 14 "" ""
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 (if (stringp temp) " ....\"" " ....]"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 state-name (buffer-name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 (buffer-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 ((y-or-n-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 (format
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 "Map this macro for the major mode `%S' only? "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 major-mode))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 (setq msg
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 (format
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 "%S is mapped to %s for %s in `%S'"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 (vip-display-macro macro-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 (vip-abbreviate-string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 (format
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 "%S"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 (setq temp (vip-display-macro macro-body)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 14 "" ""
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 (if (stringp macro-body) " ....\"" " ....]"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 state-name major-mode))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 major-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 (setq msg
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 (format
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 "%S is globally mapped to %s in %s"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 (vip-display-macro macro-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 (vip-abbreviate-string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 (format
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 "%S"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 (setq temp (vip-display-macro macro-body)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 14 "" ""
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 (if (stringp macro-body) " ....\"" " ....]"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 state-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 (if (y-or-n-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 (format "Save this macro in %s? "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 (vip-abbreviate-file-name vip-custom-file-name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 (vip-save-string-in-file
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 (format "\n(vip-record-kbd-macro %S '%S %s '%S)"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 (vip-display-macro macro-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 state
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 ;; if we don't let vector macro-body through %S,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 ;; the symbols `\.' `\[' etc will be converted into
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 ;; characters, causing invalid read error on recorded
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 ;; macros in .vip.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 ;; I am not sure is macro-body can still be a string at
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 ;; this point, but I am preserving this option anyway.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437 (if (vectorp macro-body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 (format "%S" macro-body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 macro-body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 scope)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 vip-custom-file-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 (message msg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 (setq new-elt
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 (cons macro-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 (cond ((eq scope t) (list nil nil (cons t nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 ((symbolp scope)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 (list nil (list (cons scope nil)) (cons t nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 ((stringp scope)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 (list (list (cons scope nil)) nil (cons t nil))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 (setq old-elt (assoc macro-name (eval macro-alist-var)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 (if (null old-elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 ;; insert new-elt in macro-alist-var and keep the list sorted
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 (define-key
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 keymap
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 (vector (vip-key-to-emacs-key (aref macro-name 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 'vip-exec-mapped-kbd-macro)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 (setq lis (eval macro-alist-var))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 (while (and lis (string< (vip-array-to-string (car (car lis)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 (vip-array-to-string macro-name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 (setq lis2 (cons (car lis) lis2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 (setq lis (cdr lis)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 (setq lis2 (reverse lis2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 (set macro-alist-var (append lis2 (cons new-elt lis)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 (setq old-elt new-elt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 (setq old-sub-elt
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 (cond ((eq scope t) (vip-kbd-global-pair old-elt))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 ((symbolp scope) (assoc scope (vip-kbd-mode-alist old-elt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 ((stringp scope) (assoc scope (vip-kbd-buf-alist old-elt)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 (if old-sub-elt
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 (setcdr old-sub-elt macro-body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 (cond ((symbolp scope) (setcar (cdr (cdr old-elt))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 (cons (cons scope macro-body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 (vip-kbd-mode-alist old-elt))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 ((stringp scope) (setcar (cdr old-elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 (cons (cons scope macro-body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 (vip-kbd-buf-alist old-elt))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 ;; macro name must be a vector of vip-style keys
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 (defun vip-unrecord-kbd-macro (macro-name state)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 "Delete macro MACRO-NAME from Viper STATE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 MACRO-NAME must be a vector of vip-style keys. This command is used by Viper
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 internally, but the user can also use it in ~/.vip to delete pre-defined macros
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 supplied with Viper. The best way to avoid mistakes in macro names to be passed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 to this function is to use vip-describe-kbd-macros and copy the name from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 there."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 (let* (state-name keymap
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 (macro-alist-var
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 (cond ((eq state 'vi-state)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 (setq state-name "Vi state"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 keymap vip-vi-kbd-map)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 'vip-vi-kbd-macro-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 ((memq state '(insert-state replace-state))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 (setq state-name "Insert state"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 keymap vip-insert-kbd-map)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 'vip-insert-kbd-macro-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506 (setq state-name "Emacs state"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 keymap vip-emacs-kbd-map)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 'vip-emacs-kbd-macro-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 buf-mapping mode-mapping global-mapping
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 macro-pair macro-entry)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 ;; Macro-name is usually a vector. However, command history or macros
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 ;; recorded in ~/.vip may appear as strings. So, convert to vectors.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 (setq macro-name (vip-fixup-macro macro-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 (if (vip-char-array-p macro-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 (setq macro-name (vip-char-array-to-macro macro-name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 (setq macro-entry (assoc macro-name (eval macro-alist-var)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 (if (= (length macro-name) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 (error "Can't unmap an empty macro name"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522 (if (null macro-entry)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 (error "%S is not mapped to a macro for %s in `%s'"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 (vip-display-macro macro-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 state-name (buffer-name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 (setq buf-mapping (vip-kbd-buf-pair macro-entry)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 mode-mapping (vip-kbd-mode-pair macro-entry)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 global-mapping (vip-kbd-global-pair macro-entry))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 (cond ((and (cdr buf-mapping)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532 (or (and (not (cdr mode-mapping)) (not (cdr global-mapping)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533 (y-or-n-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 (format "Unmap %S for `%s' only? "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535 (vip-display-macro macro-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 (buffer-name)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 (setq macro-pair buf-mapping)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
538 (message "%S is unmapped for %s in `%s'"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539 (vip-display-macro macro-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 state-name (buffer-name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 ((and (cdr mode-mapping)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 (or (not (cdr global-mapping))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543 (y-or-n-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 (format "Unmap %S for the major mode `%S' only? "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 (vip-display-macro macro-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546 major-mode))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 (setq macro-pair mode-mapping)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 (message "%S is unmapped for %s in %S"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549 (vip-display-macro macro-name) state-name major-mode))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550 ((cdr (setq macro-pair (vip-kbd-global-pair macro-entry)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551 (message
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552 "Global mapping of %S for %s is removed"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
553 (vip-display-macro macro-name) state-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554 (t (error "%S is not mapped to a macro for %s in `%s'"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555 (vip-display-macro macro-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556 state-name (buffer-name))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557 (setcdr macro-pair nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558 (or (cdr buf-mapping)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
559 (cdr mode-mapping)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 (cdr global-mapping)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
561 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
562 (set macro-alist-var (delq macro-entry (eval macro-alist-var)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
563 (if (vip-can-release-key (aref macro-name 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
564 (eval macro-alist-var))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565 (define-key
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
566 keymap
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
567 (vector (vip-key-to-emacs-key (aref macro-name 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
568 nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
569 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
572 ;; Check if MACRO-ALIST has an entry for a macro name starting with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
573 ;; CHAR. If not, this indicates that the binding for this char
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
574 ;; in vip-vi/insert-kbd-map can be released.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
575 (defun vip-can-release-key (char macro-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
576 (let ((lis macro-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
577 (can-release t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
578 macro-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
579
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
580 (while (and lis can-release)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
581 (setq macro-name (car (car lis)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
582 (if (eq char (aref macro-name 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
583 (setq can-release nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
584 (setq lis (cdr lis)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
585 can-release))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
587
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
588 (defun vip-exec-mapped-kbd-macro (count)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
589 "Dispatch kbd macro."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
590 (interactive "P")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
591 (let* ((macro-alist (cond ((eq vip-current-state 'vi-state)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592 vip-vi-kbd-macro-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593 ((memq vip-current-state
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594 '(insert-state replace-state))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 vip-insert-kbd-macro-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
596 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
597 vip-emacs-kbd-macro-alist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
598 (unmatched-suffix "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
599 ;; Macros and keys are executed with other macros turned off
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
600 ;; For macros, this is done to avoid macro recursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
601 vip-vi-kbd-minor-mode vip-insert-kbd-minor-mode
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
602 vip-emacs-kbd-minor-mode
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
603 next-best-match keyseq event-seq
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
604 macro-first-char macro-alist-elt macro-body
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
605 command)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
606
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
607 (setq macro-first-char last-command-event
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
608 event-seq (vip-read-fast-keysequence macro-first-char macro-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
609 keyseq (vip-events-to-macro event-seq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
610 macro-alist-elt (assoc keyseq macro-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
611 next-best-match (vip-find-best-matching-macro macro-alist keyseq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
612
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
613 (if (null macro-alist-elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
614 (setq macro-alist-elt (car next-best-match)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
615 unmatched-suffix (subseq event-seq (cdr next-best-match))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
616
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
617 (cond ((null macro-alist-elt))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
618 ((setq macro-body (vip-kbd-buf-definition macro-alist-elt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
619 ((setq macro-body (vip-kbd-mode-definition macro-alist-elt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
620 ((setq macro-body (vip-kbd-global-definition macro-alist-elt))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
621
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
622 ;; when defining keyboard macro, don't use the macro mappings
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
623 (if (and macro-body (not defining-kbd-macro))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
624 ;; block cmd executed as part of a macro from entering command history
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
625 (let ((command-history command-history))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
626 (setq vip-this-kbd-macro (car macro-alist-elt))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
627 (execute-kbd-macro (vip-macro-to-events macro-body) count)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
628 (setq vip-this-kbd-macro nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
629 vip-last-kbd-macro (car macro-alist-elt))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
630 (vip-set-unread-command-events unmatched-suffix))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
631 ;; If not a macro, or the macro is suppressed while defining another
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
632 ;; macro, put keyseq back on the event queue
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
633 (vip-set-unread-command-events event-seq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
634 ;; if the user typed arg, then use it if prefix arg is not set by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
635 ;; some other command (setting prefix arg can happen if we do, say,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
636 ;; 2dw and there is a macro starting with 2. Then control will go to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
637 ;; this routine
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
638 (or prefix-arg (setq prefix-arg count))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
639 (setq command (key-binding (read-key-sequence nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
640 (if (commandp command)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
641 (command-execute command)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
642 (beep 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
643 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
644
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
645
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
646
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
647 ;;; Displaying and completing macros
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
648
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
649 (defun vip-describe-kbd-macros ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
650 "Show currently defined keyboard macros."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
651 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
652 (with-output-to-temp-buffer " *vip-info*"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
653 (princ "Macros in Vi state:\n===================\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
654 (mapcar 'vip-describe-one-macro vip-vi-kbd-macro-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
655 (princ "\n\nMacros in Insert and Replace states:\n====================================\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
656 (mapcar 'vip-describe-one-macro vip-insert-kbd-macro-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
657 (princ "\n\nMacros in Emacs state:\n======================\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
658 (mapcar 'vip-describe-one-macro vip-emacs-kbd-macro-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
659 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
660
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
661 (defun vip-describe-one-macro (macro)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
662 (princ (format "\n *** Mappings for %S:\n ------------\n"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
663 (vip-display-macro (car macro))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
664 (princ " ** Buffer-specific:")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
665 (if (vip-kbd-buf-alist macro)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
666 (mapcar 'vip-describe-one-macro-elt (vip-kbd-buf-alist macro))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
667 (princ " none\n"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
668 (princ "\n ** Mode-specific:")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
669 (if (vip-kbd-mode-alist macro)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
670 (mapcar 'vip-describe-one-macro-elt (vip-kbd-mode-alist macro))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
671 (princ " none\n"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
672 (princ "\n ** Global:")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
673 (if (vip-kbd-global-definition macro)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
674 (princ (format "\n %S" (cdr (vip-kbd-global-pair macro))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
675 (princ " none"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
676 (princ "\n"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
677
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
678 (defun vip-describe-one-macro-elt (elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
679 (let ((name (car elt))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
680 (defn (cdr elt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
681 (princ (format "\n * %S:\n %S\n" name defn))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
682
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
683
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
684
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
685 ;; check if SEQ is a prefix of some car of an element in ALIST
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
686 (defun vip-keyseq-is-a-possible-macro (seq alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
687 (let ((converted-seq (vip-events-to-macro seq)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
688 (eval (cons 'or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
689 (mapcar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
690 (function (lambda (elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
691 (vip-prefix-subseq-p converted-seq elt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
692 (vip-this-buffer-macros alist))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
693
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
694 ;; whether SEQ1 is a prefix of SEQ2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
695 (defun vip-prefix-subseq-p (seq1 seq2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
696 (let ((len1 (length seq1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
697 (len2 (length seq2)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
698 (if (<= len1 len2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
699 (equal seq1 (subseq seq2 0 len1)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
700
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
701 ;; find the longest common prefix
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
702 (defun vip-common-seq-prefix (&rest seqs)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
703 (let* ((first (car seqs))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
704 (rest (cdr seqs))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
705 (pref [])
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
706 (idx 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
707 len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
708 (if (= (length seqs) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
709 (setq len 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
710 (setq len (apply 'min (mapcar 'length seqs))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
711 (while (< idx len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
712 (if (eval (cons 'and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
713 (mapcar (function (lambda (s)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
714 (equal (elt first idx)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
715 (elt s idx))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
716 rest)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
717 (setq pref (vconcat pref (vector (elt first idx)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
718 (setq idx (1+ idx)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
719 pref))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
720
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
721 ;; get all sequences that match PREFIX from a given A-LIST
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
722 (defun vip-extract-matching-alist-members (pref alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
723 (delq nil (mapcar (function (lambda (elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
724 (if (vip-prefix-subseq-p pref elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
725 elt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
726 (vip-this-buffer-macros alist))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
727
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
728 (defun vip-do-sequence-completion (seq alist compl-message)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
729 (let* ((matches (vip-extract-matching-alist-members seq alist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
730 (new-seq (apply 'vip-common-seq-prefix matches))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
731 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
732 (cond ((and (equal seq new-seq) (= (length matches) 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
733 (message "%s (Sole completion)" compl-message)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
734 (sit-for 2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
735 ((null matches)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
736 (message "%s (No match)" compl-message)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
737 (sit-for 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
738 (setq new-seq seq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
739 ((member seq matches)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
740 (message "%s (Complete, but not unique)" compl-message)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
741 (sit-for 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
742 (vip-display-vector-completions matches))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
743 ((equal seq new-seq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
744 (vip-display-vector-completions matches)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
745 new-seq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
746
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
747
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
748 (defun vip-display-vector-completions (list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
749 (with-output-to-temp-buffer "*Completions*"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
750 (display-completion-list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
751 (mapcar 'prin1-to-string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
752 (mapcar 'vip-display-macro list)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
753
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
754
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
755
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
756 ;; alist is the alist of macros
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
757 ;; str is the fast key sequence entered
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
758 ;; returns: (matching-macro-def . unmatched-suffix-start-index)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
759 (defun vip-find-best-matching-macro (alist str)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
760 (let ((lis alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
761 (def-len 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
762 (str-len (length str))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
763 match unmatched-start-idx found macro-def)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
764 (while (and (not found) lis)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
765 (setq macro-def (car lis)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
766 def-len (length (car macro-def)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
767 (if (and (>= str-len def-len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
768 (equal (car macro-def) (subseq str 0 def-len)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
769 (if (or (vip-kbd-buf-definition macro-def)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
770 (vip-kbd-mode-definition macro-def)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
771 (vip-kbd-global-definition macro-def))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
772 (setq found t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
773 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
774 (setq lis (cdr lis)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
775
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
776 (if found
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
777 (setq match macro-def
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
778 unmatched-start-idx def-len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
779 (setq match nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
780 unmatched-start-idx 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
781
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
782 (cons match unmatched-start-idx)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
783
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
784
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
785
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
786 ;; returns a list of names of macros defined for the current buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
787 (defun vip-this-buffer-macros (macro-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
788 (let (candidates)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
789 (setq candidates
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
790 (mapcar (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
791 (lambda (elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
792 (if (or (vip-kbd-buf-definition elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
793 (vip-kbd-mode-definition elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
794 (vip-kbd-global-definition elt))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
795 (car elt))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
796 macro-alist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
797 (setq candidates (delq nil candidates))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
798
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
799
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
800 ;; if seq of Viper key symbols (representing a macro) can be converted to a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
801 ;; string--do so. Otherwise, do nothing.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
802 (defun vip-display-macro (macro-name-or-body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
803 (cond ((vip-char-symbol-sequence-p macro-name-or-body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
804 (mapconcat 'symbol-name macro-name-or-body ""))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
805 ((vip-char-array-p macro-name-or-body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
806 (mapconcat 'char-to-string macro-name-or-body ""))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
807 (t macro-name-or-body)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
808
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
809 ;; convert sequence of events (that came presumably from emacs kbd macro) into
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
810 ;; Viper's macro, which is a vector of the form
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
811 ;; [ desc desc ... ]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
812 ;; Each desc is either a symbol of (meta symb), (shift symb), etc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
813 ;; Here we purge events that happen to be lists. In most cases, these events
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
814 ;; got into a macro definition unintentionally; say, when the user moves mouse
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
815 ;; during a macro definition, then something like (switch-frame ...) might get
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
816 ;; in. Another reason for purging lists-events is that we can't store them in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
817 ;; textual form (say, in .emacs) and then read them back.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
818 (defun vip-events-to-macro (event-seq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
819 (vconcat (delq nil (mapcar (function (lambda (elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
820 (if (consp elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
821 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
822 (vip-event-key elt))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
823 event-seq))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
824
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
825 ;; convert strings or arrays of characters to Viper macro form
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
826 (defun vip-char-array-to-macro (array)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
827 (let ((vec (vconcat array))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
828 macro)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
829 (if vip-xemacs-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
830 (setq macro (mapcar 'character-to-event vec))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
831 (setq macro vec))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
832 (vconcat (mapcar 'vip-event-key macro))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
833
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
834 ;; For macros bodies and names, goes over MACRO and checks if all members are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
835 ;; names of keys (actually, it only checks if they are symbols or lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
836 ;; if a digit is found, it is converted into a symbol (e.g., 0 -> \0, etc).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
837 ;; If MACRO is not a list or vector -- doesn't change MACRO.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
838 (defun vip-fixup-macro (macro)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
839 (let ((len (length macro))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
840 (idx 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
841 elt break)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
842 (if (or (vectorp macro) (listp macro))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
843 (while (and (< idx len) (not break))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
844 (setq elt (elt macro idx))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
845 (cond ((numberp elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
846 ;; fix number
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
847 (if (and (<= 0 elt) (<= elt 9))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
848 (cond ((arrayp macro)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
849 (aset macro
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
850 idx
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
851 (intern (char-to-string (+ ?0 elt)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
852 ((listp macro)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
853 (setcar (nthcdr idx macro)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
854 (intern (char-to-string (+ ?0 elt)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
855 )))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
856 ((listp elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
857 (vip-fixup-macro elt))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
858 ((symbolp elt) nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
859 (t (setq break t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
860 (setq idx (1+ idx))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
861
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
862 (if break
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
863 (error "Wrong type macro component, symbol-or-listp, %S" elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
864 macro)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
865
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
866 (defun vip-char-array-p (array)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
867 (eval (cons 'and (mapcar 'vip-characterp array))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
868
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
869 (defun vip-macro-to-events (macro-body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
870 (vconcat (mapcar 'vip-key-to-emacs-key macro-body)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
871
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
872
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
873 ;; check if vec is a vector of character symbols
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
874 (defun vip-char-symbol-sequence-p (vec)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
875 (and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
876 (sequencep vec)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
877 (eval
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
878 (cons 'and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
879 (mapcar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
880 (function (lambda (elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
881 (and (symbolp elt) (= (length (symbol-name elt)) 1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
882 vec)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
883
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
884
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
885 ;; Check if vec is a vector of key-press events representing characters
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
886 ;; XEmacs only
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
887 (defun vip-event-vector-p (vec)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
888 (and (vectorp vec)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
889 (eval (cons 'and (mapcar '(lambda (elt) (if (eventp elt) t)) vec)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
890
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
891
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
892 ;;; Reading fast key sequences
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
893
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
894 ;; Assuming that CHAR was the first character in a fast succession of key
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
895 ;; strokes, read the rest. Return the vector of keys that was entered in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
896 ;; this fast succession of key strokes.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
897 ;; A fast keysequence is one that is terminated by a pause longer than
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
898 ;; vip-fast-keyseq-timeout.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
899 (defun vip-read-fast-keysequence (event macro-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
900 (let ((lis (vector event))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
901 next-event)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
902 (while (and (vip-fast-keysequence-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
903 (vip-keyseq-is-a-possible-macro lis macro-alist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
904 (setq next-event (vip-read-key))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
905 ;;(setq next-event (vip-read-event))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
906 (or (vip-mouse-event-p next-event)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
907 (setq lis (vconcat lis (vector next-event)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
908 lis))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
909
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
910
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
911 ;;; Keyboard macros in registers
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
912
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
913 ;; sets register to last-kbd-macro carefully.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
914 (defun vip-set-register-macro (reg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
915 (if (get-register reg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
916 (if (y-or-n-p "Register contains data. Overwrite? ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
917 ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
918 (error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
919 "Macro not saved in register. Can still be invoked via `C-x e'")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
920 (set-register reg last-kbd-macro))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
921
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
922 (defun vip-register-macro (count)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
923 "Keyboard macros in registers - a modified \@ command."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
924 (interactive "P")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
925 (let ((reg (downcase (read-char))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
926 (cond ((or (and (<= ?a reg) (<= reg ?z)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
927 (setq vip-last-macro-reg reg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
928 (if defining-kbd-macro
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
929 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
930 (end-kbd-macro)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
931 (vip-set-register-macro reg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
932 (execute-kbd-macro (get-register reg) count)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
933 ((or (= ?@ reg) (= ?\^j reg) (= ?\^m reg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
934 (if vip-last-macro-reg
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
935 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
936 (error "No previous kbd macro"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
937 (execute-kbd-macro (get-register vip-last-macro-reg) count))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
938 ((= ?\# reg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
939 (start-kbd-macro count))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
940 ((= ?! reg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
941 (setq reg (downcase (read-char)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
942 (if (or (and (<= ?a reg) (<= reg ?z)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
943 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
944 (setq vip-last-macro-reg reg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
945 (vip-set-register-macro reg))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
946 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
947 (error "`%c': Unknown register" reg)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
948
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
949
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
950 (defun vip-global-execute ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
951 "Call last keyboad macro for each line in the region."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
952 (if (> (point) (mark t)) (exchange-point-and-mark))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
953 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
954 (call-last-kbd-macro)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
955 (while (< (point) (mark t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
956 (forward-line 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
957 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
958 (call-last-kbd-macro)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
959
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
960
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
961 ;;; viper-macs.el ends here