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