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