Mercurial > emacs
annotate lisp/edmacro.el @ 548:a7b7b759f694
*** empty log message ***
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Wed, 12 Feb 1992 03:35:14 +0000 |
parents | 1e0bc00dca7a |
children | 8a533acedb77 |
rev | line source |
---|---|
109 | 1 ;; Keyboard macro editor for GNU Emacs. Version 1.02. |
2 ;; Copyright (C) 1990 Free Software Foundation, Inc. | |
3 | |
4 ;; This file is part of GNU Emacs. | |
5 | |
6 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
7 ;; it under the terms of the GNU General Public License as published by | |
8 ;; the Free Software Foundation; either version 1, or (at your option) | |
9 ;; any later version. | |
10 | |
11 ;; GNU Emacs is distributed in the hope that it will be useful, | |
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 ;; GNU General Public License for more details. | |
15 | |
16 ;; You should have received a copy of the GNU General Public License | |
17 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | |
20 ;; Original from: Dave Gillespie, daveg@csvax.caltech.edu. | |
21 | |
22 ;; To use, type `M-x edit-last-kbd-macro' to edit the most recently | |
23 ;; defined keyboard macro. If you have used `M-x name-last-kbd-macro' | |
24 ;; to give a keyboard macro a name, type `M-x edit-kbd-macro' to edit | |
25 ;; the macro by name. When you are done editing, type `C-c C-c' to | |
26 ;; record your changes back into the original keyboard macro. | |
27 | |
28 ;;; The user-level commands for editing macros. | |
29 | |
258 | 30 ;;;###autoload |
109 | 31 (defun edit-last-kbd-macro (&optional prefix buffer hook) |
32 "Edit the most recently defined keyboard macro." | |
33 (interactive "P") | |
34 (edmacro-edit-macro last-kbd-macro | |
35 (function (lambda (x arg) (setq last-kbd-macro x))) | |
36 prefix buffer hook)) | |
37 | |
258 | 38 ;;;###autoload |
109 | 39 (defun edit-kbd-macro (cmd &optional prefix buffer hook in-hook out-hook) |
199
b3710ab30435
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
109
diff
changeset
|
40 "Edit a keyboard macro which has been given a name by `name-last-kbd-macro'. |
b3710ab30435
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
109
diff
changeset
|
41 \(See also `edit-last-kbd-macro'.)" |
109 | 42 (interactive "CCommand name: \nP") |
43 (and cmd | |
44 (edmacro-edit-macro (if in-hook | |
45 (funcall in-hook cmd) | |
46 (symbol-function cmd)) | |
47 (or out-hook | |
48 (list 'lambda '(x arg) | |
49 (list 'fset | |
50 (list 'quote cmd) | |
51 'x))) | |
52 prefix buffer hook cmd))) | |
53 | |
258 | 54 ;;;###autoload |
109 | 55 (defun read-kbd-macro (start end) |
56 "Read the region as a keyboard macro definition. | |
199
b3710ab30435
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
109
diff
changeset
|
57 The region is interpreted as spelled-out keystrokes, e.g., \"M-x abc RET\". |
109 | 58 The resulting macro is installed as the \"current\" keyboard macro. |
59 | |
60 Symbols: RET, SPC, TAB, DEL, LFD, NUL; C-key; M-key. (Must be uppercase.) | |
61 REM marks the rest of a line as a comment. | |
62 Whitespace is ignored; other characters are copied into the macro." | |
63 (interactive "r") | |
64 (setq last-kbd-macro (edmacro-parse-keys (buffer-substring start end))) | |
65 (if (and (string-match "\\`\C-x(" last-kbd-macro) | |
66 (string-match "\C-x)\\'" last-kbd-macro)) | |
67 (setq last-kbd-macro (substring last-kbd-macro 2 -2)))) | |
68 | |
69 ;;; Formatting a keyboard macro as human-readable text. | |
70 | |
71 (defun edmacro-print-macro (macro-str local-map) | |
72 (let ((save-map (current-local-map)) | |
73 (print-escape-newlines t) | |
74 key-symbol key-str key-last prefix-arg this-prefix) | |
75 (unwind-protect | |
76 (progn | |
77 (use-local-map local-map) | |
78 (while (edmacro-peek-char) | |
79 (edmacro-read-key) | |
80 (setq this-prefix prefix-arg) | |
81 (or (memq key-symbol '(digit-argument | |
82 negative-argument | |
83 universal-argument)) | |
84 (null prefix-arg) | |
85 (progn | |
86 (cond ((consp prefix-arg) | |
87 (insert (format "prefix-arg (%d)\n" | |
88 (car prefix-arg)))) | |
89 ((eq prefix-arg '-) | |
90 (insert "prefix-arg -\n")) | |
91 ((numberp prefix-arg) | |
92 (insert (format "prefix-arg %d\n" prefix-arg)))) | |
93 (setq prefix-arg nil))) | |
94 (cond ((null key-symbol) | |
95 (insert "type \"") | |
96 (edmacro-insert-string macro-str) | |
97 (insert "\"\n") | |
98 (setq macro-str "")) | |
99 ((eq key-symbol 'digit-argument) | |
100 (edmacro-prefix-arg key-last nil prefix-arg)) | |
101 ((eq key-symbol 'negative-argument) | |
102 (edmacro-prefix-arg ?- nil prefix-arg)) | |
103 ((eq key-symbol 'universal-argument) | |
104 (let* ((c-u 4) (argstartchar key-last) | |
105 (char (edmacro-read-char))) | |
106 (while (= char argstartchar) | |
107 (setq c-u (* 4 c-u) | |
108 char (edmacro-read-char))) | |
109 (edmacro-prefix-arg char c-u nil))) | |
110 ((eq key-symbol 'self-insert-command) | |
111 (insert "insert ") | |
112 (if (and (>= key-last 32) (<= key-last 126)) | |
113 (let ((str "")) | |
114 (while (or (and (eq key-symbol | |
115 'self-insert-command) | |
116 (< (length str) 60) | |
117 (>= key-last 32) | |
118 (<= key-last 126)) | |
119 (and (memq key-symbol | |
120 '(backward-delete-char | |
121 delete-backward-char | |
122 backward-delete-char-untabify)) | |
123 (> (length str) 0))) | |
124 (if (eq key-symbol 'self-insert-command) | |
125 (setq str (concat str | |
126 (char-to-string key-last))) | |
127 (setq str (substring str 0 -1))) | |
128 (edmacro-read-key)) | |
129 (insert "\"" str "\"\n") | |
130 (edmacro-unread-chars key-str)) | |
131 (insert "\"") | |
132 (edmacro-insert-string (char-to-string key-last)) | |
133 (insert "\"\n"))) | |
134 ((and (eq key-symbol 'quoted-insert) | |
135 (edmacro-peek-char)) | |
136 (insert "quoted-insert\n") | |
137 (let ((ch (edmacro-read-char)) | |
138 ch2) | |
139 (if (and (>= ch ?0) (<= ch ?7)) | |
140 (progn | |
141 (setq ch (- ch ?0) | |
142 ch2 (edmacro-read-char)) | |
143 (if ch2 | |
144 (if (and (>= ch2 ?0) (<= ch2 ?7)) | |
145 (progn | |
146 (setq ch (+ (* ch 8) (- ch2 ?0)) | |
147 ch2 (edmacro-read-char)) | |
148 (if ch2 | |
149 (if (and (>= ch2 ?0) (<= ch2 ?7)) | |
150 (setq ch (+ (* ch 8) (- ch2 ?0))) | |
151 (edmacro-unread-chars ch2)))) | |
152 (edmacro-unread-chars ch2))))) | |
153 (if (or (and (>= ch ?0) (<= ch ?7)) | |
154 (< ch 32) (> ch 126)) | |
155 (insert (format "type \"\\%03o\"\n" ch)) | |
156 (insert "type \"" (char-to-string ch) "\"\n")))) | |
157 ((memq key-symbol '(isearch-forward | |
158 isearch-backward | |
159 isearch-forward-regexp | |
160 isearch-backward-regexp)) | |
161 (insert (symbol-name key-symbol) "\n") | |
162 (edmacro-isearch-argument)) | |
163 ((eq key-symbol 'execute-extended-command) | |
164 (edmacro-read-argument obarray 'commandp)) | |
165 (t | |
166 (let ((cust (get key-symbol 'edmacro-print))) | |
167 (if cust | |
168 (funcall cust) | |
169 (insert (symbol-name key-symbol)) | |
170 (indent-to 30) | |
171 (insert " # ") | |
172 (edmacro-insert-string key-str) | |
173 (insert "\n") | |
174 (let ((int (edmacro-get-interactive key-symbol))) | |
175 (if (string-match "\\`\\*" int) | |
176 (setq int (substring int 1))) | |
177 (while (> (length int) 0) | |
178 (cond ((= (aref int 0) ?a) | |
179 (edmacro-read-argument | |
180 obarray nil)) | |
181 ((memq (aref int 0) '(?b ?B ?D ?f ?F ?n | |
182 ?s ?S ?x ?X)) | |
183 (edmacro-read-argument)) | |
184 ((and (= (aref int 0) ?c) | |
185 (edmacro-peek-char)) | |
186 (insert "type \"") | |
187 (edmacro-insert-string | |
188 (char-to-string | |
189 (edmacro-read-char))) | |
190 (insert "\"\n")) | |
191 ((= (aref int 0) ?C) | |
192 (edmacro-read-argument | |
193 obarray 'commandp)) | |
194 ((= (aref int 0) ?k) | |
195 (edmacro-read-key) | |
196 (if key-symbol | |
197 (progn | |
198 (insert "type \"") | |
199 (edmacro-insert-string key-str) | |
200 (insert "\"\n")) | |
201 (edmacro-unread-chars key-str))) | |
202 ((= (aref int 0) ?N) | |
203 (or this-prefix | |
204 (edmacro-read-argument))) | |
205 ((= (aref int 0) ?v) | |
206 (edmacro-read-argument | |
207 obarray 'user-variable-p))) | |
208 (let ((nl (string-match "\n" int))) | |
209 (setq int (if nl | |
210 (substring int (1+ nl)) | |
211 ""))))))))))) | |
212 (use-local-map save-map)))) | |
213 | |
214 (defun edmacro-prefix-arg (char c-u value) | |
215 (let ((sign 1)) | |
216 (if (and (numberp value) (< value 0)) | |
217 (setq sign -1 value (- value))) | |
218 (if (eq value '-) | |
219 (setq sign -1 value nil)) | |
220 (while (and char (= ?- char)) | |
221 (setq sign (- sign) c-u nil) | |
222 (setq char (edmacro-read-char))) | |
223 (while (and char (>= char ?0) (<= char ?9)) | |
224 (setq value (+ (* (if (numberp value) value 0) 10) (- char ?0)) c-u nil) | |
225 (setq char (edmacro-read-char))) | |
226 (setq prefix-arg | |
227 (cond (c-u (list c-u)) | |
228 ((numberp value) (* value sign)) | |
229 ((= sign -1) '-))) | |
230 (edmacro-unread-chars char))) | |
231 | |
232 (defun edmacro-insert-string (str) | |
233 (let ((i 0) j ch) | |
234 (while (< i (length str)) | |
235 (if (and (> (setq ch (aref str i)) 127) | |
236 (< ch 160)) | |
237 (progn | |
238 (setq ch (- ch 128)) | |
239 (insert "\\M-"))) | |
240 (if (< ch 32) | |
241 (cond ((= ch 8) (insret "\\b")) | |
242 ((= ch 9) (insert "\\t")) | |
243 ((= ch 10) (insert "\\n")) | |
244 ((= ch 13) (insert "\\r")) | |
245 ((= ch 27) (insert "\\e")) | |
246 (t (insert "\\C-" (char-to-string (downcase (+ ch 64)))))) | |
247 (if (< ch 127) | |
248 (if (or (= ch 34) (= ch 92)) | |
249 (insert "\\" (char-to-string ch)) | |
250 (setq j i) | |
251 (while (and (< (setq i (1+ i)) (length str)) | |
252 (>= (setq ch (aref str i)) 32) | |
253 (/= ch 34) (/= ch 92) | |
254 (< ch 127))) | |
255 (insert (substring str j i)) | |
256 (setq i (1- i))) | |
257 (if (memq ch '(127 255)) | |
258 (insert (format "\\%03o" ch)) | |
259 (insert "\\M-" (char-to-string (- ch 128)))))) | |
260 (setq i (1+ i))))) | |
261 | |
262 (defun edmacro-lookup-key (map) | |
263 (let ((loc (and map (lookup-key map macro-str))) | |
264 (glob (lookup-key (current-global-map) macro-str)) | |
265 (loc-str macro-str) | |
266 (glob-str macro-str)) | |
267 (and (integerp loc) | |
268 (setq loc-str (substring macro-str 0 loc) | |
269 loc (lookup-key map loc-str))) | |
270 (and (consp loc) | |
271 (setq loc nil)) | |
272 (or loc | |
273 (setq loc-str "")) | |
274 (and (integerp glob) | |
275 (setq glob-str (substring macro-str 0 glob) | |
276 glob (lookup-key (current-global-map) glob-str))) | |
277 (and (consp glob) | |
278 (setq glob nil)) | |
279 (or glob | |
280 (setq glob-str "")) | |
281 (if (> (length glob-str) (length loc-str)) | |
282 (setq key-symbol glob | |
283 key-str glob-str) | |
284 (setq key-symbol loc | |
285 key-str loc-str)) | |
286 (setq key-last (and (> (length key-str) 0) | |
287 (logand (aref key-str (1- (length key-str))) 127))) | |
288 key-symbol)) | |
289 | |
290 (defun edmacro-read-argument (&optional obarray pred) ;; currently ignored | |
291 (let ((str "") | |
292 (min-bsp 0) | |
293 (exec (eq key-symbol 'execute-extended-command)) | |
294 str-base) | |
295 (while (progn | |
296 (edmacro-lookup-key (current-global-map)) | |
297 (or (and (eq key-symbol 'self-insert-command) | |
298 (< (length str) 60)) | |
299 (memq key-symbol | |
300 '(backward-delete-char | |
301 delete-backward-char | |
302 backward-delete-char-untabify)) | |
303 (eq key-last 9))) | |
304 (setq macro-str (substring macro-str (length key-str))) | |
305 (or (and (eq key-last 9) | |
306 obarray | |
307 (let ((comp (try-completion str obarray pred))) | |
308 (and (stringp comp) | |
309 (> (length comp) (length str)) | |
310 (setq str comp)))) | |
311 (if (or (eq key-symbol 'self-insert-command) | |
312 (and (or (eq key-last 9) | |
313 (<= (length str) min-bsp)) | |
314 (setq min-bsp (+ (length str) (length key-str))))) | |
315 (setq str (concat str key-str)) | |
316 (setq str (substring str 0 -1))))) | |
317 (setq str-base str | |
318 str (concat str key-str) | |
319 macro-str (substring macro-str (length key-str))) | |
320 (if exec | |
321 (let ((comp (try-completion str-base obarray pred))) | |
322 (if (if (stringp comp) | |
323 (and (commandp (intern comp)) | |
324 (setq str-base comp)) | |
325 (commandp (intern str-base))) | |
326 (insert str-base "\n") | |
327 (insert "execute-extended-command\n") | |
328 (insert "type \"") | |
329 (edmacro-insert-string str) | |
330 (insert "\"\n"))) | |
331 (if (> (length str) 0) | |
332 (progn | |
333 (insert "type \"") | |
334 (edmacro-insert-string str) | |
335 (insert "\"\n")))))) | |
336 | |
337 (defun edmacro-isearch-argument () | |
338 (let ((str "") | |
339 (min-bsp 0) | |
340 ch) | |
341 (while (and (setq ch (edmacro-read-char)) | |
342 (or (<= ch 127) (not search-exit-option)) | |
343 (not (eq ch search-exit-char)) | |
344 (or (eq ch search-repeat-char) | |
345 (eq ch search-reverse-char) | |
346 (eq ch search-delete-char) | |
347 (eq ch search-yank-word-char) | |
348 (eq ch search-yank-line-char) | |
349 (eq ch search-quote-char) | |
350 (eq ch ?\r) | |
351 (eq ch ?\t) | |
352 (not search-exit-option) | |
353 (and (/= ch 127) (>= ch 32)))) | |
354 (if (and (eq ch search-quote-char) | |
355 (edmacro-peek-char)) | |
356 (setq str (concat str (char-to-string ch) | |
357 (char-to-string (edmacro-read-char))) | |
358 min-bsp (length str)) | |
359 (if (or (and (< ch 127) (>= ch 32)) | |
360 (eq ch search-yank-word-char) | |
361 (eq ch search-yank-line-char) | |
362 (and (or (not (eq ch search-delete-char)) | |
363 (<= (length str) min-bsp)) | |
364 (setq min-bsp (1+ (length str))))) | |
365 (setq str (concat str (char-to-string ch))) | |
366 (setq str (substring str 0 -1))))) | |
367 (if (eq ch search-exit-char) | |
368 (if (= (length str) 0) ;; non-incremental search | |
369 (progn | |
370 (setq str (concat str (char-to-string ch))) | |
371 (and (eq (edmacro-peek-char) ?\C-w) | |
372 (progn | |
373 (setq str (concat str "\C-w")) | |
374 (edmacro-read-char))) | |
375 (if (> (length str) 0) | |
376 (progn | |
377 (insert "type \"") | |
378 (edmacro-insert-string str) | |
379 (insert "\"\n"))) | |
380 (edmacro-read-argument) | |
381 (setq str ""))) | |
382 (edmacro-unread-chars ch)) | |
383 (if (> (length str) 0) | |
384 (progn | |
385 (insert "type \"") | |
386 (edmacro-insert-string str) | |
387 (insert "\\e\"\n"))))) | |
388 | |
389 ;;; Get the next keystroke-sequence from the input stream. | |
390 ;;; Sets key-symbol, key-str, and key-last as a side effect. | |
391 (defun edmacro-read-key () | |
392 (edmacro-lookup-key (current-local-map)) | |
393 (and key-symbol | |
394 (setq macro-str (substring macro-str (length key-str))))) | |
395 | |
396 (defun edmacro-peek-char () | |
397 (and (> (length macro-str) 0) | |
398 (aref macro-str 0))) | |
399 | |
400 (defun edmacro-read-char () | |
401 (and (> (length macro-str) 0) | |
402 (prog1 | |
403 (aref macro-str 0) | |
404 (setq macro-str (substring macro-str 1))))) | |
405 | |
406 (defun edmacro-unread-chars (chars) | |
407 (and (integerp chars) | |
408 (setq chars (char-to-string chars))) | |
409 (and chars | |
410 (setq macro-str (concat chars macro-str)))) | |
411 | |
412 (defun edmacro-dump (mac) | |
413 (set-mark-command nil) | |
414 (insert "\n\n") | |
415 (edmacro-print-macro mac (current-local-map))) | |
416 | |
417 ;;; Parse a string of spelled-out keystrokes, as produced by key-description. | |
418 | |
419 (defun edmacro-parse-keys (str) | |
420 (let ((pos 0) | |
421 (mac "") | |
422 part) | |
423 (while (and (< pos (length str)) | |
424 (string-match "[^ \t\n]+" str pos)) | |
425 (setq pos (match-end 0) | |
426 part (substring str (match-beginning 0) (match-end 0)) | |
427 mac (concat mac | |
428 (if (and (> (length part) 2) | |
429 (= (aref part 1) ?-) | |
430 (= (aref part 0) ?M)) | |
431 (progn | |
432 (setq part (substring part 2)) | |
433 "\e") | |
434 (if (and (> (length part) 4) | |
435 (= (aref part 0) ?C) | |
436 (= (aref part 1) ?-) | |
437 (= (aref part 2) ?M) | |
438 (= (aref part 3) ?-)) | |
439 (progn | |
440 (setq part (concat "C-" (substring part 4))) | |
441 "\e") | |
442 "")) | |
443 (or (cdr (assoc part '( ( "NUL" . "\0" ) | |
444 ( "RET" . "\r" ) | |
445 ( "LFD" . "\n" ) | |
446 ( "TAB" . "\t" ) | |
447 ( "ESC" . "\e" ) | |
448 ( "SPC" . " " ) | |
449 ( "DEL" . "\177" ) | |
450 ( "C-?" . "\177" ) | |
451 ( "C-2" . "\0" ) | |
452 ( "C-SPC" . "\0") ))) | |
453 (and (equal part "REM") | |
454 (setq pos (or (string-match "\n" str pos) | |
455 (length str))) | |
456 "") | |
457 (and (= (length part) 3) | |
458 (= (aref part 0) ?C) | |
459 (= (aref part 1) ?-) | |
460 (char-to-string (logand (aref part 2) 31))) | |
461 part)))) | |
462 mac)) | |
463 | |
464 ;;; Parse a keyboard macro description in edmacro-print-macro's format. | |
465 | |
466 (defun edmacro-read-macro (&optional map) | |
467 (or map (setq map (current-local-map))) | |
468 (let ((macro-str "")) | |
469 (while (not (progn | |
470 (skip-chars-forward " \t\n") | |
471 (eobp))) | |
472 (cond ((looking-at "#")) ;; comment | |
473 ((looking-at "prefix-arg[ \t]*-[ \t]*\n") | |
474 (edmacro-append-chars "\C-u-")) | |
475 ((looking-at "prefix-arg[ \t]*\\(-?[0-9]+\\)[ \t]*\n") | |
476 (edmacro-append-chars (concat "\C-u" (edmacro-match-string 1)))) | |
477 ((looking-at "prefix-arg[ \t]*(\\([0-9]+\\))[ \t]*\n") | |
478 (let ((val (string-to-int (edmacro-match-string 1)))) | |
479 (while (> val 1) | |
480 (or (= (% val 4) 0) | |
481 (error "Bad prefix argument value")) | |
482 (edmacro-append-chars "\C-u") | |
483 (setq val (/ val 4))))) | |
484 ((looking-at "prefix-arg") | |
485 (error "Bad prefix argument syntax")) | |
486 ((looking-at "insert ") | |
487 (forward-char 7) | |
488 (edmacro-append-chars (read (current-buffer))) | |
489 (if (< (current-column) 7) | |
490 (forward-line -1))) | |
491 ((looking-at "type ") | |
492 (forward-char 5) | |
493 (edmacro-append-chars (read (current-buffer))) | |
494 (if (< (current-column) 5) | |
495 (forward-line -1))) | |
496 ((looking-at "keys \\(.*\\)\n") | |
497 (goto-char (1- (match-end 0))) | |
498 (edmacro-append-chars (edmacro-parse-keys | |
499 (buffer-substring (match-beginning 1) | |
500 (match-end 1))))) | |
501 ((looking-at "\\([-a-zA-z0-9_]+\\)[ \t]*\\(.*\\)\n") | |
502 (let* ((func (intern (edmacro-match-string 1))) | |
503 (arg (edmacro-match-string 2)) | |
504 (cust (get func 'edmacro-read))) | |
505 (if cust | |
506 (funcall cust arg) | |
507 (or (commandp func) | |
508 (error "Not an Emacs command")) | |
509 (or (equal arg "") | |
510 (string-match "\\`#" arg) | |
511 (error "Unexpected argument to command")) | |
512 (let ((keys | |
513 (or (where-is-internal func map t) | |
514 (where-is-internal func (current-global-map) t)))) | |
515 (if keys | |
516 (edmacro-append-chars keys) | |
517 (edmacro-append-chars (concat "\ex" | |
518 (symbol-name func) | |
519 "\n"))))))) | |
520 (t (error "Syntax error"))) | |
521 (forward-line 1)) | |
522 macro-str)) | |
523 | |
524 (defun edmacro-append-chars (chars) | |
525 (setq macro-str (concat macro-str chars))) | |
526 | |
527 (defun edmacro-match-string (n) | |
528 (if (match-beginning n) | |
529 (buffer-substring (match-beginning n) (match-end n)) | |
530 "")) | |
531 | |
532 (defun edmacro-get-interactive (func) | |
533 (if (symbolp func) | |
534 (let ((cust (get func 'edmacro-interactive))) | |
535 (if cust | |
536 cust | |
537 (edmacro-get-interactive (symbol-function func)))) | |
538 (or (and (eq (car-safe func) 'lambda) | |
539 (let ((int (if (consp (nth 2 func)) | |
540 (nth 2 func) | |
541 (nth 3 func)))) | |
542 (and (eq (car-safe int) 'interactive) | |
543 (stringp (nth 1 int)) | |
544 (nth 1 int)))) | |
545 ""))) | |
546 | |
547 (put 'search-forward 'edmacro-interactive "s") | |
548 (put 'search-backward 'edmacro-interactive "s") | |
549 (put 'word-search-forward 'edmacro-interactive "s") | |
550 (put 'word-search-backward 'edmacro-interactive "s") | |
551 (put 're-search-forward 'edmacro-interactive "s") | |
552 (put 're-search-backward 'edmacro-interactive "s") | |
553 (put 'switch-to-buffer 'edmacro-interactive "B") | |
554 (put 'kill-buffer 'edmacro-interactive "B") | |
555 (put 'rename-buffer 'edmacro-interactive "B\nB") | |
556 (put 'goto-char 'edmacro-interactive "N") | |
557 (put 'global-set-key 'edmacro-interactive "k\nC") | |
558 (put 'global-unset-key 'edmacro-interactive "k") | |
559 (put 'local-set-key 'edmacro-interactive "k\nC") | |
560 (put 'local-unset-key 'edmacro-interactive "k") | |
561 | |
562 ;;; Think about kbd-macro-query | |
563 | |
564 ;;; Edit a keyboard macro in another buffer. | |
565 ;;; (Prefix argument is currently ignored.) | |
566 | |
567 (defun edmacro-edit-macro (mac repl &optional prefix buffer hook arg) | |
568 (or (stringp mac) | |
569 (error "Not a keyboard macro")) | |
570 (let ((oldbuf (current-buffer)) | |
571 (local (current-local-map)) | |
572 (buf (get-buffer-create (or buffer "*Edit Macro*")))) | |
573 (set-buffer buf) | |
574 (kill-all-local-variables) | |
575 (use-local-map edmacro-mode-map) | |
576 (setq buffer-read-only nil | |
577 major-mode 'edmacro-mode | |
578 mode-name "Edit Macro") | |
579 (set (make-local-variable 'edmacro-original-buffer) oldbuf) | |
580 (set (make-local-variable 'edmacro-replace-function) repl) | |
581 (set (make-local-variable 'edmacro-replace-argument) arg) | |
582 (set (make-local-variable 'edmacro-finish-hook) hook) | |
583 (erase-buffer) | |
584 (insert "# Keyboard Macro Editor. Press C-c C-c to finish; press C-x k RET to cancel.\n") | |
585 (insert "# Original keys: " (key-description mac) "\n\n") | |
586 (message "Formatting keyboard macro...") | |
587 (edmacro-print-macro mac local) | |
588 (switch-to-buffer buf) | |
589 (goto-char (point-min)) | |
590 (forward-line 3) | |
591 (recenter '(4)) | |
592 (set-buffer-modified-p nil) | |
593 (message "Formatting keyboard macro...done") | |
594 (run-hooks 'edmacro-format-hook))) | |
595 | |
596 (defun edmacro-finish-edit () | |
597 (interactive) | |
598 (or (and (boundp 'edmacro-original-buffer) | |
599 (boundp 'edmacro-replace-function) | |
600 (boundp 'edmacro-replace-argument) | |
601 (boundp 'edmacro-finish-hook) | |
602 (eq major-mode 'edmacro-mode)) | |
199
b3710ab30435
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
109
diff
changeset
|
603 (error "This command is valid only in buffers created by `edit-kbd-macro'.")) |
109 | 604 (let ((buf (current-buffer)) |
605 (str (buffer-string)) | |
606 (func edmacro-replace-function) | |
607 (arg edmacro-replace-argument) | |
608 (hook edmacro-finish-hook)) | |
609 (goto-char (point-min)) | |
610 (run-hooks 'edmacro-compile-hook) | |
611 (and (buffer-modified-p) | |
612 func | |
613 (progn | |
614 (message "Compiling keyboard macro...") | |
615 (let ((mac (edmacro-read-macro | |
616 (and (buffer-name edmacro-original-buffer) | |
617 (save-excursion | |
618 (set-buffer edmacro-original-buffer) | |
619 (current-local-map)))))) | |
620 (and (buffer-name edmacro-original-buffer) | |
621 (switch-to-buffer edmacro-original-buffer)) | |
622 (funcall func mac arg)) | |
623 (message "Compiling keyboard macro...done"))) | |
624 (kill-buffer buf) | |
625 (if hook | |
626 (funcall hook arg)))) | |
627 | |
628 (defun edmacro-mode () | |
199
b3710ab30435
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
109
diff
changeset
|
629 "\\<edmacro-mode-map>Keyboard Macro Editing mode. Press \\[edmacro-finish-edit] to save and exit. |
b3710ab30435
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
109
diff
changeset
|
630 To abort the edit, just kill this buffer with \\[kill-buffer] RET. |
109 | 631 |
632 The keyboard macro is represented as a series of M-x style command names. | |
633 Keystrokes which do not correspond to simple M-x commands are written as | |
199
b3710ab30435
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
109
diff
changeset
|
634 \"type\" commands. When you press \\[edmacro-finish-edit], edmacro converts each command |
109 | 635 back into a suitable keystroke sequence; \"type\" commands are converted |
636 directly back into keystrokes." | |
637 (interactive) | |
199
b3710ab30435
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
109
diff
changeset
|
638 (error "This mode can be enabled only by `edit-kbd-macro' or `edit-last-kbd-macro'.")) |
109 | 639 (put 'edmacro-mode 'mode-class 'special) |
640 | |
641 (if (boundp 'edmacro-mode-map) () | |
642 (setq edmacro-mode-map (make-sparse-keymap)) | |
643 (define-key edmacro-mode-map "\C-c\C-c" 'edmacro-finish-edit)) |