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