1 ;; Keyboard macro editor for GNU Emacs. Version 1.02.
2 ;; Copyright (C) 1990 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
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)
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.
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.
20 ;; Original from: Dave Gillespie, daveg@csvax.caltech.edu.
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.
28 ;;; The user-level commands for editing macros.
31 (defun edit-last-kbd-macro (&optional prefix buffer hook
)
32 "Edit the most recently defined keyboard macro."
34 (edmacro-edit-macro last-kbd-macro
35 (function (lambda (x arg
) (setq last-kbd-macro x
)))
39 (defun edit-kbd-macro (cmd &optional prefix buffer hook in-hook out-hook
)
40 "Edit a keyboard macro which has been given a name by `name-last-kbd-macro'.
41 \(See also `edit-last-kbd-macro'.)"
42 (interactive "CCommand name: \nP")
44 (edmacro-edit-macro (if in-hook
46 (symbol-function cmd
))
48 (list 'lambda
'(x arg
)
52 prefix buffer hook cmd
)))
55 (defun read-kbd-macro (start end
)
56 "Read the region as a keyboard macro definition.
57 The region is interpreted as spelled-out keystrokes, e.g., \"M-x abc RET\".
58 The resulting macro is installed as the \"current\" keyboard macro.
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."
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))))
69 ;;; Formatting a keyboard macro as human-readable text.
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
)
77 (use-local-map local-map
)
78 (while (edmacro-peek-char)
80 (setq this-prefix prefix-arg
)
81 (or (memq key-symbol
'(digit-argument
86 (cond ((consp prefix-arg
)
87 (insert (format "prefix-arg (%d)\n"
90 (insert "prefix-arg -\n"))
92 (insert (format "prefix-arg %d\n" prefix-arg
))))
93 (setq prefix-arg nil
)))
94 (cond ((null key-symbol
)
96 (edmacro-insert-string 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
)
108 char
(edmacro-read-char)))
109 (edmacro-prefix-arg char c-u nil
)))
110 ((eq key-symbol
'self-insert-command
)
112 (if (and (>= key-last
32) (<= key-last
126))
114 (while (or (and (eq key-symbol
115 'self-insert-command
)
119 (and (memq key-symbol
120 '(backward-delete-char
122 backward-delete-char-untabify
))
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)))
129 (insert "\"" str
"\"\n")
130 (edmacro-unread-chars key-str
))
132 (edmacro-insert-string (char-to-string key-last
))
134 ((and (eq key-symbol
'quoted-insert
)
136 (insert "quoted-insert\n")
137 (let ((ch (edmacro-read-char))
139 (if (and (>= ch ?
0) (<= ch ?
7))
142 ch2
(edmacro-read-char))
144 (if (and (>= ch2 ?
0) (<= ch2 ?
7))
146 (setq ch
(+ (* ch
8) (- ch2 ?
0))
147 ch2
(edmacro-read-char))
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
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
))
166 (let ((cust (get key-symbol
'edmacro-print
)))
169 (insert (symbol-name key-symbol
))
172 (edmacro-insert-string key-str
)
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
181 ((memq (aref int
0) '(?b ?B ?D ?f ?F ?n
183 (edmacro-read-argument))
184 ((and (= (aref int
0) ?c
)
187 (edmacro-insert-string
189 (edmacro-read-char)))
192 (edmacro-read-argument
199 (edmacro-insert-string key-str
)
201 (edmacro-unread-chars key-str
)))
204 (edmacro-read-argument)))
206 (edmacro-read-argument
207 obarray
'user-variable-p
)))
208 (let ((nl (string-match "\n" int
)))
210 (substring int
(1+ nl
))
212 (use-local-map save-map
))))
214 (defun edmacro-prefix-arg (char c-u value
)
216 (if (and (numberp value
) (< value
0))
217 (setq sign -
1 value
(- 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)))
227 (cond (c-u (list c-u
))
228 ((numberp value
) (* value sign
))
230 (edmacro-unread-chars char
)))
232 (defun edmacro-insert-string (str)
234 (while (< i
(length str
))
235 (if (and (> (setq ch
(aref str i
)) 127)
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))))))
248 (if (or (= ch
34) (= ch
92))
249 (insert "\\" (char-to-string ch
))
251 (while (and (< (setq i
(1+ i
)) (length str
))
252 (>= (setq ch
(aref str i
)) 32)
253 (/= ch
34) (/= ch
92)
255 (insert (substring str j i
))
257 (if (memq ch
'(127 255))
258 (insert (format "\\%03o" ch
))
259 (insert "\\M-" (char-to-string (- ch
128))))))
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
))
266 (glob-str macro-str
))
268 (setq loc-str
(substring macro-str
0 loc
)
269 loc
(lookup-key map loc-str
)))
275 (setq glob-str
(substring macro-str
0 glob
)
276 glob
(lookup-key (current-global-map) glob-str
)))
281 (if (> (length glob-str
) (length loc-str
))
282 (setq key-symbol glob
286 (setq key-last
(and (> (length key-str
) 0)
287 (logand (aref key-str
(1- (length key-str
))) 127)))
290 (defun edmacro-read-argument (&optional obarray pred
) ;; currently ignored
293 (exec (eq key-symbol
'execute-extended-command
))
296 (edmacro-lookup-key (current-global-map))
297 (or (and (eq key-symbol
'self-insert-command
)
300 '(backward-delete-char
302 backward-delete-char-untabify
))
304 (setq macro-str
(substring macro-str
(length key-str
)))
305 (or (and (eq key-last
9)
307 (let ((comp (try-completion str obarray pred
)))
309 (> (length comp
) (length str
))
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)))))
318 str
(concat str key-str
)
319 macro-str
(substring macro-str
(length key-str
)))
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")
329 (edmacro-insert-string str
)
331 (if (> (length str
) 0)
334 (edmacro-insert-string str
)
337 (defun edmacro-isearch-argument ()
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
)
352 (not search-exit-option
)
353 (and (/= ch
127) (>= ch
32))))
354 (if (and (eq ch search-quote-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
370 (setq str
(concat str
(char-to-string ch
)))
371 (and (eq (edmacro-peek-char) ?\C-w
)
373 (setq str
(concat str
"\C-w"))
374 (edmacro-read-char)))
375 (if (> (length str
) 0)
378 (edmacro-insert-string str
)
380 (edmacro-read-argument)
382 (edmacro-unread-chars ch
))
383 (if (> (length str
) 0)
386 (edmacro-insert-string str
)
387 (insert "\\e\"\n")))))
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))
394 (setq macro-str
(substring macro-str
(length key-str
)))))
396 (defun edmacro-peek-char ()
397 (and (> (length macro-str
) 0)
400 (defun edmacro-read-char ()
401 (and (> (length macro-str
) 0)
404 (setq macro-str
(substring macro-str
1)))))
406 (defun edmacro-unread-chars (chars)
407 (and (integerp chars
)
408 (setq chars
(char-to-string chars
)))
410 (setq macro-str
(concat chars macro-str
))))
412 (defun edmacro-dump (mac)
413 (set-mark-command nil
)
415 (edmacro-print-macro mac
(current-local-map)))
417 ;;; Parse a string of spelled-out keystrokes, as produced by key-description.
419 (defun edmacro-parse-keys (str)
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))
428 (if (and (> (length part
) 2)
430 (= (aref part
0) ?M
))
432 (setq part
(substring part
2))
434 (if (and (> (length part
) 4)
438 (= (aref part
3) ?-
))
440 (setq part
(concat "C-" (substring part
4)))
443 (or (cdr (assoc part
'( ( "NUL" .
"\0" )
452 ( "C-SPC" .
"\0") )))
453 (and (equal part
"REM")
454 (setq pos
(or (string-match "\n" str pos
)
457 (and (= (length part
) 3)
460 (char-to-string (logand (aref part
2) 31)))
464 ;;; Parse a keyboard macro description in edmacro-print-macro's format.
466 (defun edmacro-read-macro (&optional map
)
467 (or map
(setq map
(current-local-map)))
468 (let ((macro-str ""))
470 (skip-chars-forward " \t\n")
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))))
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 ")
488 (edmacro-append-chars (read (current-buffer)))
489 (if (< (current-column) 7)
491 ((looking-at "type ")
493 (edmacro-append-chars (read (current-buffer)))
494 (if (< (current-column) 5)
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)
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
)))
508 (error "Not an Emacs command"))
510 (string-match "\\`#" arg
)
511 (error "Unexpected argument to command"))
513 (or (where-is-internal func map t
)
514 (where-is-internal func
(current-global-map) t
))))
516 (edmacro-append-chars keys
)
517 (edmacro-append-chars (concat "\ex"
520 (t (error "Syntax error")))
524 (defun edmacro-append-chars (chars)
525 (setq macro-str
(concat macro-str chars
)))
527 (defun edmacro-match-string (n)
528 (if (match-beginning n
)
529 (buffer-substring (match-beginning n
) (match-end n
))
532 (defun edmacro-get-interactive (func)
534 (let ((cust (get func
'edmacro-interactive
)))
537 (edmacro-get-interactive (symbol-function func
))))
538 (or (and (eq (car-safe func
) 'lambda
)
539 (let ((int (if (consp (nth 2 func
))
542 (and (eq (car-safe int
) 'interactive
)
543 (stringp (nth 1 int
))
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")
562 ;;; Think about kbd-macro-query
564 ;;; Edit a keyboard macro in another buffer.
565 ;;; (Prefix argument is currently ignored.)
567 (defun edmacro-edit-macro (mac repl
&optional prefix buffer hook arg
)
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*"))))
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
)
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))
592 (set-buffer-modified-p nil
)
593 (message "Formatting keyboard macro...done")
594 (run-hooks 'edmacro-format-hook
)))
596 (defun edmacro-finish-edit ()
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
))
603 (error "This command is valid only in buffers created by `edit-kbd-macro'."))
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)
614 (message "Compiling keyboard macro...")
615 (let ((mac (edmacro-read-macro
616 (and (buffer-name edmacro-original-buffer
)
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")))
626 (funcall hook arg
))))
628 (defun edmacro-mode ()
629 "\\<edmacro-mode-map>Keyboard Macro Editing mode. Press \\[edmacro-finish-edit] to save and exit.
630 To abort the edit, just kill this buffer with \\[kill-buffer] RET.
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
634 \"type\" commands. When you press \\[edmacro-finish-edit], edmacro converts each command
635 back into a suitable keystroke sequence; \"type\" commands are converted
636 directly back into keystrokes."
638 (error "This mode can be enabled only by `edit-kbd-macro' or `edit-last-kbd-macro'."))
639 (put 'edmacro-mode
'mode-class
'special
)
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
))