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