Whitespace change.
[bpt/emacs.git] / lisp / edmacro.el
CommitLineData
e5167999
ER
1;;; edmacro.el --- keyboard macro editor
2
8f1204db 3;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
9750e079 4
629d4dcd
RS
5;; Author: Dave Gillespie <daveg@synaptics.com>
6;; Maintainer: Dave Gillespie <daveg@synaptics.com>
7;; Version: 2.01
e9571d2a 8;; Keywords: abbrev
66b3ecce
DL
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
e5167999 14;; the Free Software Foundation; either version 2, or (at your option)
66b3ecce
DL
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b578f267
EN
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
66b3ecce 26
e5167999 27;;; Commentary:
66b3ecce 28
629d4dcd
RS
29;;; Usage:
30;;
31;; The `C-x C-k' (`edit-kbd-macro') command edits a keyboard macro
32;; in a special buffer. It prompts you to type a key sequence,
33;; which should be one of:
34;;
35;; * RET or `C-x e' (call-last-kbd-macro), to edit the most
36;; recently defined keyboard macro.
37;;
38;; * `M-x' followed by a command name, to edit a named command
39;; whose definition is a keyboard macro.
40;;
41;; * `C-h l' (view-lossage), to edit the 100 most recent keystrokes
42;; and install them as the "current" macro.
43;;
44;; * any key sequence whose definition is a keyboard macro.
45;;
46;; This file includes a version of `insert-kbd-macro' that uses the
47;; more readable format defined by these routines.
48;;
49;; Also, the `read-kbd-macro' command parses the region as
50;; a keyboard macro, and installs it as the "current" macro.
51;; This and `format-kbd-macro' can also be called directly as
52;; Lisp functions.
53
54;; Type `C-h m', or see the documentation for `edmacro-mode' below,
55;; for information about the format of written keyboard macros.
56
57;; `edit-kbd-macro' formats the macro with one command per line,
58;; including the command names as comments on the right. If the
59;; formatter gets confused about which keymap was used for the
60;; characters, the command-name comments will be wrong but that
61;; won't hurt anything.
62
63;; With a prefix argument, `edit-kbd-macro' will format the
64;; macro in a more concise way that omits the comments.
65
66;; This package requires GNU Emacs 19 or later, and daveg's CL
67;; package 2.02 or later. (CL 2.02 comes standard starting with
68;; Emacs 19.18.) This package does not work with Emacs 18 or
69;; Lucid Emacs.
e5167999
ER
70
71;;; Code:
66b3ecce 72\f
c31afdbd
RS
73(eval-when-compile
74 (require 'cl))
629d4dcd 75
66b3ecce
DL
76;;; The user-level commands for editing macros.
77
629d4dcd 78;;;###autoload (define-key ctl-x-map "\C-k" 'edit-kbd-macro)
629d4dcd
RS
79
80;;;###autoload
81(defvar edmacro-eight-bits nil
82 "*Non-nil if edit-kbd-macro should leave 8-bit characters intact.
83Default nil means to write characters above \\177 in octal notation.")
84
85(defvar edmacro-mode-map nil)
86(unless edmacro-mode-map
87 (setq edmacro-mode-map (make-sparse-keymap))
88 (define-key edmacro-mode-map "\C-c\C-c" 'edmacro-finish-edit)
89 (define-key edmacro-mode-map "\C-c\C-q" 'edmacro-insert-key))
90
f9f9507e 91;;;###autoload
629d4dcd
RS
92(defun edit-kbd-macro (keys &optional prefix finish-hook store-hook)
93 "Edit a keyboard macro.
94At the prompt, type any key sequence which is bound to a keyboard macro.
95Or, type `C-x e' or RET to edit the last keyboard macro, `C-h l' to edit
96the last 100 keystrokes as a keyboard macro, or `M-x' to edit a macro by
97its command name.
98With a prefix argument, format the macro in a more concise way."
99 (interactive "kKeyboard macro to edit (C-x e, M-x, C-h l, or keys): \nP")
100 (when keys
101 (let ((cmd (if (arrayp keys) (key-binding keys) keys))
102 (mac nil))
103 (cond (store-hook
104 (setq mac keys)
105 (setq cmd nil))
106 ((or (eq cmd 'call-last-kbd-macro)
107 (member keys '("\r" [return])))
108 (or last-kbd-macro
109 (y-or-n-p "No keyboard macro defined. Create one? ")
110 (keyboard-quit))
111 (setq mac (or last-kbd-macro ""))
112 (setq cmd 'last-kbd-macro))
113 ((eq cmd 'execute-extended-command)
114 (setq cmd (read-command "Name of keyboard macro to edit: "))
115 (setq mac (symbol-function cmd)))
116 ((eq cmd 'view-lossage)
117 (setq mac (recent-keys))
118 (setq cmd 'last-kbd-macro))
a8e1fefa
KH
119 ((null cmd)
120 (error "Key sequence %s is not defined" (key-description keys)))
629d4dcd
RS
121 ((symbolp cmd)
122 (setq mac (symbol-function cmd)))
123 (t
124 (setq mac cmd)
125 (setq cmd nil)))
126 (unless (arrayp mac)
a8e1fefa
KH
127 (error "Key sequence %s is not a keyboard macro"
128 (key-description keys)))
629d4dcd
RS
129 (message "Formatting keyboard macro...")
130 (let* ((oldbuf (current-buffer))
131 (mmac (edmacro-fix-menu-commands mac))
132 (fmt (edmacro-format-keys mmac 1))
133 (fmtv (edmacro-format-keys mmac (not prefix)))
134 (buf (get-buffer-create "*Edit Macro*")))
135 (message "Formatting keyboard macro...done")
136 (switch-to-buffer buf)
137 (kill-all-local-variables)
138 (use-local-map edmacro-mode-map)
139 (setq buffer-read-only nil)
140 (setq major-mode 'edmacro-mode)
141 (setq mode-name "Edit Macro")
142 (set (make-local-variable 'edmacro-original-buffer) oldbuf)
143 (set (make-local-variable 'edmacro-finish-hook) finish-hook)
144 (set (make-local-variable 'edmacro-store-hook) store-hook)
145 (erase-buffer)
146 (insert ";; Keyboard Macro Editor. Press C-c C-c to finish; "
147 "press C-x k RET to cancel.\n")
148 (insert ";; Original keys: " fmt "\n")
149 (unless store-hook
150 (insert "\nCommand: " (if cmd (symbol-name cmd) "none") "\n")
3f5e6d79 151 (let ((keys (where-is-internal (or cmd mac) '(keymap))))
629d4dcd
RS
152 (if keys
153 (while keys
154 (insert "Key: " (edmacro-format-keys (pop keys) 1) "\n"))
155 (insert "Key: none\n"))))
156 (insert "\nMacro:\n\n")
157 (save-excursion
158 (insert fmtv "\n"))
159 (recenter '(4))
160 (when (eq mac mmac)
161 (set-buffer-modified-p nil))
162 (run-hooks 'edmacro-format-hook)))))
163
164;;; The next two commands are provided for convenience and backward
165;;; compatibility.
166
167;;;###autoload
168(defun edit-last-kbd-macro (&optional prefix)
66b3ecce
DL
169 "Edit the most recently defined keyboard macro."
170 (interactive "P")
629d4dcd 171 (edit-kbd-macro 'call-last-kbd-macro prefix))
66b3ecce 172
f9f9507e 173;;;###autoload
629d4dcd
RS
174(defun edit-named-kbd-macro (&optional prefix)
175 "Edit a keyboard macro which has been given a name by `name-last-kbd-macro'."
176 (interactive "P")
177 (edit-kbd-macro 'execute-extended-command prefix))
66b3ecce 178
f9f9507e 179;;;###autoload
629d4dcd 180(defun read-kbd-macro (start &optional end)
66b3ecce 181 "Read the region as a keyboard macro definition.
052bdd09 182The region is interpreted as spelled-out keystrokes, e.g., \"M-x abc RET\".
629d4dcd
RS
183See documentation for `edmacro-mode' for details.
184Leading/trailing \"C-x (\" and \"C-x )\" in the text are allowed and ignored.
66b3ecce
DL
185The resulting macro is installed as the \"current\" keyboard macro.
186
629d4dcd
RS
187In Lisp, may also be called with a single STRING argument in which case
188the result is returned rather than being installed as the current macro.
189The result will be a string if possible, otherwise an event vector.
190Second argument NEED-VECTOR means to return an event vector always."
66b3ecce 191 (interactive "r")
629d4dcd
RS
192 (if (stringp start)
193 (edmacro-parse-keys start end)
194 (setq last-kbd-macro (edmacro-parse-keys (buffer-substring start end)))))
66b3ecce 195
629d4dcd
RS
196;;;###autoload
197(defun format-kbd-macro (&optional macro verbose)
198 "Return the keyboard macro MACRO as a human-readable string.
199This string is suitable for passing to `read-kbd-macro'.
200Second argument VERBOSE means to put one command per line with comments.
201If VERBOSE is `1', put everything on one line. If VERBOSE is omitted
202or nil, use a compact 80-column format."
203 (and macro (symbolp macro) (setq macro (symbol-function macro)))
204 (edmacro-format-keys (or macro last-kbd-macro) verbose))
66b3ecce 205\f
629d4dcd 206;;; Commands for *Edit Macro* buffer.
66b3ecce
DL
207
208(defun edmacro-finish-edit ()
209 (interactive)
629d4dcd
RS
210 (unless (eq major-mode 'edmacro-mode)
211 (error
212 "This command is valid only in buffers created by `edit-kbd-macro'"))
213 (run-hooks 'edmacro-finish-hook)
214 (let ((cmd nil) (keys nil) (no-keys nil)
215 (top (point-min)))
216 (goto-char top)
217 (let ((case-fold-search nil))
218 (while (cond ((looking-at "[ \t]*\\($\\|;;\\|REM[ \t\n]\\)")
219 t)
220 ((looking-at "Command:[ \t]*\\([^ \t\n]*\\)[ \t]*$")
221 (when edmacro-store-hook
222 (error "\"Command\" line not allowed in this context"))
223 (let ((str (buffer-substring (match-beginning 1)
224 (match-end 1))))
225 (unless (equal str "")
c31afdbd 226 (setq cmd (and (not (equal str "none"))
629d4dcd
RS
227 (intern str)))
228 (and (fboundp cmd) (not (arrayp (symbol-function cmd)))
229 (not (y-or-n-p
230 (format "Command %s is already defined; %s"
231 cmd "proceed? ")))
232 (keyboard-quit))))
233 t)
234 ((looking-at "Key:\\(.*\\)$")
235 (when edmacro-store-hook
236 (error "\"Key\" line not allowed in this context"))
237 (let ((key (edmacro-parse-keys
238 (buffer-substring (match-beginning 1)
239 (match-end 1)))))
240 (unless (equal key "")
c31afdbd 241 (if (equal key "none")
629d4dcd
RS
242 (setq no-keys t)
243 (push key keys)
244 (let ((b (key-binding key)))
245 (and b (commandp b) (not (arrayp b))
246 (or (not (fboundp b))
247 (not (arrayp (symbol-function b))))
248 (not (y-or-n-p
249 (format "Key %s is already defined; %s"
250 (edmacro-format-keys key 1)
251 "proceed? ")))
252 (keyboard-quit))))))
253 t)
254 ((looking-at "Macro:[ \t\n]*")
255 (goto-char (match-end 0))
256 nil)
257 ((eobp) nil)
258 (t (error "Expected a `Macro:' line")))
259 (forward-line 1))
260 (setq top (point)))
261 (let* ((buf (current-buffer))
262 (str (buffer-substring top (point-max)))
263 (modp (buffer-modified-p))
264 (obuf edmacro-original-buffer)
265 (store-hook edmacro-store-hook)
266 (finish-hook edmacro-finish-hook))
267 (unless (or cmd keys store-hook (equal str ""))
268 (error "No command name or keys specified"))
269 (when modp
270 (when (buffer-name obuf)
271 (set-buffer obuf))
272 (message "Compiling keyboard macro...")
273 (let ((mac (edmacro-parse-keys str)))
274 (message "Compiling keyboard macro...done")
275 (if store-hook
276 (funcall store-hook mac)
277 (when (eq cmd 'last-kbd-macro)
278 (setq last-kbd-macro (and (> (length mac) 0) mac))
279 (setq cmd nil))
280 (when cmd
281 (if (= (length mac) 0)
282 (fmakunbound cmd)
283 (fset cmd mac)))
284 (if no-keys
285 (when cmd
3f5e6d79 286 (loop for key in (where-is-internal cmd '(keymap)) do
629d4dcd
RS
287 (global-unset-key key)))
288 (when keys
289 (if (= (length mac) 0)
290 (loop for key in keys do (global-unset-key key))
291 (loop for key in keys do
292 (global-set-key key (or cmd mac)))))))))
293 (kill-buffer buf)
294 (when (buffer-name obuf)
295 (switch-to-buffer obuf))
296 (when finish-hook
297 (funcall finish-hook)))))
298
299(defun edmacro-insert-key (key)
300 "Insert the written name of a key in the buffer."
301 (interactive "kKey to insert: ")
302 (if (bolp)
303 (insert (edmacro-format-keys key t) "\n")
304 (insert (edmacro-format-keys key) " ")))
66b3ecce
DL
305
306(defun edmacro-mode ()
629d4dcd
RS
307 "\\<edmacro-mode-map>Keyboard Macro Editing mode. Press
308\\[edmacro-finish-edit] to save and exit.
052bdd09 309To abort the edit, just kill this buffer with \\[kill-buffer] RET.
66b3ecce 310
629d4dcd
RS
311Press \\[edmacro-insert-key] to insert the name of any key by typing the key.
312
313The editing buffer contains a \"Command:\" line and any number of
314\"Key:\" lines at the top. These are followed by a \"Macro:\" line
315and the macro itself as spelled-out keystrokes: `C-x C-f foo RET'.
316
317The \"Command:\" line specifies the command name to which the macro
318is bound, or \"none\" for no command name. Write \"last-kbd-macro\"
319to refer to the current keyboard macro (as used by \\[call-last-kbd-macro]).
320
321The \"Key:\" lines specify key sequences to which the macro is bound,
322or \"none\" for no key bindings.
323
324You can edit these lines to change the places where the new macro
325is stored.
326
327
328Format of keyboard macros during editing:
329
330Text is divided into \"words\" separated by whitespace. Except for
331the words described below, the characters of each word go directly
332as characters of the macro. The whitespace that separates words
333is ignored. Whitespace in the macro must be written explicitly,
334as in \"foo SPC bar RET\".
335
336 * The special words RET, SPC, TAB, DEL, LFD, ESC, and NUL represent
337 special control characters. The words must be written in uppercase.
338
339 * A word in angle brackets, e.g., <return>, <down>, or <f1>, represents
340 a function key. (Note that in the standard configuration, the
341 function key <return> and the control key RET are synonymous.)
342 You can use angle brackets on the words RET, SPC, etc., but they
343 are not required there.
344
345 * Keys can be written by their ASCII code, using a backslash followed
346 by up to six octal digits. This is the only way to represent keys
347 with codes above \\377.
348
349 * One or more prefixes M- (meta), C- (control), S- (shift), A- (alt),
350 H- (hyper), and s- (super) may precede a character or key notation.
351 For function keys, the prefixes may go inside or outside of the
352 brackets: C-<down> = <C-down>. The prefixes may be written in
353 any order: M-C-x = C-M-x.
354
355 Prefixes are not allowed on multi-key words, e.g., C-abc, except
356 that the Meta prefix is allowed on a sequence of digits and optional
357 minus sign: M--123 = M-- M-1 M-2 M-3.
358
359 * The `^' notation for control characters also works: ^M = C-m.
360
361 * Double angle brackets enclose command names: <<next-line>> is
362 shorthand for M-x next-line RET.
363
364 * Finally, REM or ;; causes the rest of the line to be ignored as a
365 comment.
366
367Any word may be prefixed by a multiplier in the form of a decimal
368number and `*': 3*<right> = <right> <right> <right>, and
36910*foo = foofoofoofoofoofoofoofoofoofoo.
370
371Multiple text keys can normally be strung together to form a word,
372but you may need to add whitespace if the word would look like one
373of the above notations: `; ; ;' is a keyboard macro with three
374semicolons, but `;;;' is a comment. Likewise, `\\ 1 2 3' is four
375keys but `\\123' is a single key written in octal, and `< right >'
376is seven keys but `<right>' is a single function key. When in
377doubt, use whitespace."
66b3ecce 378 (interactive)
629d4dcd 379 (error "This mode can be enabled only by `edit-kbd-macro'"))
66b3ecce 380(put 'edmacro-mode 'mode-class 'special)
629d4dcd
RS
381\f
382;;; Formatting a keyboard macro as human-readable text.
66b3ecce 383
629d4dcd
RS
384(defun edmacro-format-keys (macro &optional verbose)
385 (setq macro (edmacro-fix-menu-commands macro))
386 (let* ((maps (append (current-minor-mode-maps)
7470b9b6
RS
387 (if (current-local-map)
388 (list (current-local-map)))
389 (list (current-global-map))))
629d4dcd
RS
390 (pkeys '(end-macro ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?- ?\C-u
391 ?\M-- ?\M-0 ?\M-1 ?\M-2 ?\M-3 ?\M-4 ?\M-5 ?\M-6
392 ?\M-7 ?\M-8 ?\M-9))
393 (mdigs (nthcdr 13 pkeys))
394 (maxkey (if edmacro-eight-bits 255 127))
395 (case-fold-search nil)
396 (res-words '("NUL" "TAB" "LFD" "RET" "ESC" "SPC" "DEL" "REM"))
397 (rest-mac (vconcat macro [end-macro]))
398 (res "")
399 (len 0)
400 (one-line (eq verbose 1)))
401 (if one-line (setq verbose nil))
402 (when (stringp macro)
403 (loop for i below (length macro) do
404 (when (>= (aref rest-mac i) 128)
a9b8cb16 405 (incf (aref rest-mac i) (- ?\M-\^@ 128)))))
629d4dcd
RS
406 (while (not (eq (aref rest-mac 0) 'end-macro))
407 (let* ((prefix
408 (or (and (integerp (aref rest-mac 0))
409 (memq (aref rest-mac 0) mdigs)
c31afdbd 410 (memq (key-binding (edmacro-subseq rest-mac 0 1))
629d4dcd
RS
411 '(digit-argument negative-argument))
412 (let ((i 1))
413 (while (memq (aref rest-mac i) (cdr mdigs))
414 (incf i))
415 (and (not (memq (aref rest-mac i) pkeys))
c31afdbd
RS
416 (prog1 (concat "M-" (edmacro-subseq rest-mac 0 i) " ")
417 (callf edmacro-subseq rest-mac i)))))
629d4dcd
RS
418 (and (eq (aref rest-mac 0) ?\C-u)
419 (eq (key-binding [?\C-u]) 'universal-argument)
420 (let ((i 1))
421 (while (eq (aref rest-mac i) ?\C-u)
422 (incf i))
423 (and (not (memq (aref rest-mac i) pkeys))
424 (prog1 (loop repeat i concat "C-u ")
c31afdbd 425 (callf edmacro-subseq rest-mac i)))))
629d4dcd
RS
426 (and (eq (aref rest-mac 0) ?\C-u)
427 (eq (key-binding [?\C-u]) 'universal-argument)
428 (let ((i 1))
429 (when (eq (aref rest-mac i) ?-)
430 (incf i))
431 (while (memq (aref rest-mac i)
432 '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
433 (incf i))
434 (and (not (memq (aref rest-mac i) pkeys))
c31afdbd
RS
435 (prog1 (concat "C-u " (edmacro-subseq rest-mac 1 i) " ")
436 (callf edmacro-subseq rest-mac i)))))))
629d4dcd
RS
437 (bind-len (apply 'max 1
438 (loop for map in maps
439 for b = (lookup-key map rest-mac)
440 when b collect b)))
c31afdbd 441 (key (edmacro-subseq rest-mac 0 bind-len))
629d4dcd
RS
442 (fkey nil) tlen tkey
443 (bind (or (loop for map in maps for b = (lookup-key map key)
444 thereis (and (not (integerp b)) b))
445 (and (setq fkey (lookup-key function-key-map rest-mac))
c31afdbd 446 (setq tlen fkey tkey (edmacro-subseq rest-mac 0 tlen)
629d4dcd
RS
447 fkey (lookup-key function-key-map tkey))
448 (loop for map in maps
449 for b = (lookup-key map fkey)
450 when (and (not (integerp b)) b)
451 do (setq bind-len tlen key tkey)
452 and return b
453 finally do (setq fkey nil)))))
454 (first (aref key 0))
455 (text (loop for i from bind-len below (length rest-mac)
456 for ch = (aref rest-mac i)
457 while (and (integerp ch)
458 (> ch 32) (< ch maxkey) (/= ch 92)
459 (eq (key-binding (char-to-string ch))
460 'self-insert-command)
461 (or (> i (- (length rest-mac) 2))
462 (not (eq ch (aref rest-mac (+ i 1))))
463 (not (eq ch (aref rest-mac (+ i 2))))))
464 finally return i))
465 desc)
466 (if (stringp bind) (setq bind nil))
467 (cond ((and (eq bind 'self-insert-command) (not prefix)
468 (> text 1) (integerp first)
469 (> first 32) (<= first maxkey) (/= first 92)
470 (progn
471 (if (> text 30) (setq text 30))
c31afdbd 472 (setq desc (concat (edmacro-subseq rest-mac 0 text)))
629d4dcd
RS
473 (when (string-match "^[ACHMsS]-." desc)
474 (setq text 2)
475 (callf substring desc 0 2))
476 (not (string-match
477 "^;;\\|^<.*>$\\|^\\\\[0-9]+$\\|^[0-9]+\\*."
478 desc))))
479 (when (or (string-match "^\\^.$" desc)
480 (member desc res-words))
481 (setq desc (mapconcat 'char-to-string desc " ")))
482 (when verbose
483 (setq bind (format "%s * %d" bind text)))
484 (setq bind-len text))
485 ((and (eq bind 'execute-extended-command)
486 (> text bind-len)
487 (memq (aref rest-mac text) '(return 13))
488 (progn
c31afdbd 489 (setq desc (concat (edmacro-subseq rest-mac bind-len text)))
629d4dcd
RS
490 (commandp (intern-soft desc))))
491 (if (commandp (intern-soft desc)) (setq bind desc))
492 (setq desc (format "<<%s>>" desc))
493 (setq bind-len (1+ text)))
494 (t
495 (setq desc (mapconcat
496 (function
497 (lambda (ch)
498 (cond
499 ((integerp ch)
500 (concat
501 (loop for pf across "ACHMsS"
a9b8cb16
KH
502 for bit in '(?\A-\^@ ?\C-\^@ ?\H-\^@
503 ?\M-\^@ ?\s-\^@ ?\S-\^@)
504 when (/= (logand ch bit) 0)
629d4dcd
RS
505 concat (format "%c-" pf))
506 (let ((ch2 (logand ch (1- (lsh 1 18)))))
507 (cond ((<= ch2 32)
508 (case ch2
509 (0 "NUL") (9 "TAB") (10 "LFD")
510 (13 "RET") (27 "ESC") (32 "SPC")
511 (t
512 (format "C-%c"
513 (+ (if (<= ch2 26) 96 64)
514 ch2)))))
515 ((= ch2 127) "DEL")
516 ((<= ch2 maxkey) (char-to-string ch2))
517 (t (format "\\%o" ch2))))))
518 ((symbolp ch)
519 (format "<%s>" ch))
520 (t
521 (error "Unrecognized item in macro: %s" ch)))))
522 (or fkey key) " "))))
523 (if prefix (setq desc (concat prefix desc)))
524 (unless (string-match " " desc)
525 (let ((times 1) (pos bind-len))
c31afdbd
RS
526 (while (not (edmacro-mismatch rest-mac rest-mac
527 0 bind-len pos (+ bind-len pos)))
629d4dcd
RS
528 (incf times)
529 (incf pos bind-len))
530 (when (> times 1)
531 (setq desc (format "%d*%s" times desc))
532 (setq bind-len (* bind-len times)))))
c31afdbd 533 (setq rest-mac (edmacro-subseq rest-mac bind-len))
629d4dcd
RS
534 (if verbose
535 (progn
536 (unless (equal res "") (callf concat res "\n"))
537 (callf concat res desc)
538 (when (and bind (or (stringp bind) (symbolp bind)))
539 (callf concat res
540 (make-string (max (- 3 (/ (length desc) 8)) 1) 9)
541 ";; " (if (stringp bind) bind (symbol-name bind))))
542 (setq len 0))
543 (if (and (> (+ len (length desc) 2) 72) (not one-line))
544 (progn
545 (callf concat res "\n ")
546 (setq len 1))
547 (unless (equal res "")
548 (callf concat res " ")
549 (incf len)))
550 (callf concat res desc)
551 (incf len (length desc)))))
552 res))
553
c31afdbd
RS
554(defun edmacro-mismatch (cl-seq1 cl-seq2 cl-start1 cl-end1 cl-start2 cl-end2)
555 "Compare SEQ1 with SEQ2, return index of first mismatching element.
556Return nil if the sequences match. If one sequence is a prefix of the
557other, the return value indicates the end of the shorted sequence."
558 (let (cl-test cl-test-not cl-key cl-from-end)
559 (or cl-end1 (setq cl-end1 (length cl-seq1)))
560 (or cl-end2 (setq cl-end2 (length cl-seq2)))
561 (if cl-from-end
562 (progn
563 (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2)
564 (cl-check-match (elt cl-seq1 (1- cl-end1))
565 (elt cl-seq2 (1- cl-end2))))
566 (setq cl-end1 (1- cl-end1) cl-end2 (1- cl-end2)))
567 (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2))
568 (1- cl-end1)))
569 (let ((cl-p1 (and (listp cl-seq1) (nthcdr cl-start1 cl-seq1)))
570 (cl-p2 (and (listp cl-seq2) (nthcdr cl-start2 cl-seq2))))
571 (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2)
572 (cl-check-match (if cl-p1 (car cl-p1)
573 (aref cl-seq1 cl-start1))
574 (if cl-p2 (car cl-p2)
575 (aref cl-seq2 cl-start2))))
576 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2)
577 cl-start1 (1+ cl-start1) cl-start2 (1+ cl-start2)))
578 (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2))
579 cl-start1)))))
580
581(defun edmacro-subseq (seq start &optional end)
582 "Return the subsequence of SEQ from START to END.
583If END is omitted, it defaults to the length of the sequence.
584If START or END is negative, it counts from the end."
585 (if (stringp seq) (substring seq start end)
586 (let (len)
587 (and end (< end 0) (setq end (+ end (setq len (length seq)))))
588 (if (< start 0) (setq start (+ start (or len (setq len (length seq))))))
589 (cond ((listp seq)
590 (if (> start 0) (setq seq (nthcdr start seq)))
591 (if end
592 (let ((res nil))
593 (while (>= (setq end (1- end)) start)
594 (cl-push (cl-pop seq) res))
595 (nreverse res))
596 (copy-sequence seq)))
597 (t
598 (or end (setq end (or len (length seq))))
599 (let ((res (make-vector (max (- end start) 0) nil))
600 (i 0))
601 (while (< start end)
602 (aset res i (aref seq start))
603 (setq i (1+ i) start (1+ start)))
604 res))))))
605
629d4dcd
RS
606(defun edmacro-fix-menu-commands (macro)
607 (when (vectorp macro)
608 (let ((i 0) ev)
609 (while (< i (length macro))
610 (when (consp (setq ev (aref macro i)))
611 (cond ((equal (cadadr ev) '(menu-bar))
c31afdbd 612 (setq macro (vconcat (edmacro-subseq macro 0 i)
629d4dcd 613 (vector 'menu-bar (car ev))
c31afdbd 614 (edmacro-subseq macro (1+ i))))
629d4dcd
RS
615 (incf i))
616 ;; It would be nice to do pop-up menus, too, but not enough
617 ;; info is recorded in macros to make this possible.
618 (t
619 (error "Macros with mouse clicks are not %s"
620 "supported by this command"))))
621 (incf i))))
622 macro)
623\f
624;;; Parsing a human-readable keyboard macro.
625
626(defun edmacro-parse-keys (string &optional need-vector)
627 (let ((case-fold-search nil)
628 (pos 0)
629 (res []))
630 (while (and (< pos (length string))
631 (string-match "[^ \t\n\f]+" string pos))
632 (let ((word (substring string (match-beginning 0) (match-end 0)))
633 (key nil)
634 (times 1))
635 (setq pos (match-end 0))
636 (when (string-match "\\([0-9]+\\)\\*." word)
637 (setq times (string-to-int (substring word 0 (match-end 1))))
638 (setq word (substring word (1+ (match-end 1)))))
639 (cond ((string-match "^<<.+>>$" word)
640 (setq key (vconcat (if (eq (key-binding [?\M-x])
641 'execute-extended-command)
642 [?\M-x]
643 (or (car (where-is-internal
644 'execute-extended-command))
645 [?\M-x]))
646 (substring word 2 -2) "\r")))
647 ((and (string-match "^\\(\\([ACHMsS]-\\)*\\)<\\(.+\\)>$" word)
648 (progn
649 (setq word (concat (substring word (match-beginning 1)
650 (match-end 1))
651 (substring word (match-beginning 3)
652 (match-end 3))))
653 (not (string-match
654 "\\<\\(NUL\\|RET\\|LFD\\|ESC\\|SPC\\|DEL\\)$"
655 word))))
656 (setq key (list (intern word))))
657 ((or (equal word "REM") (string-match "^;;" word))
658 (setq pos (string-match "$" string pos)))
659 (t
660 (let ((orig-word word) (prefix 0) (bits 0))
661 (while (string-match "^[ACHMsS]-." word)
a9b8cb16
KH
662 (incf bits (cdr (assq (aref word 0)
663 '((?A . ?\A-\^@) (?C . ?\C-\^@)
664 (?H . ?\H-\^@) (?M . ?\M-\^@)
665 (?s . ?\s-\^@) (?S . ?\S-\^@)))))
629d4dcd
RS
666 (incf prefix 2)
667 (callf substring word 2))
668 (when (string-match "^\\^.$" word)
a9b8cb16 669 (incf bits ?\C-\^@)
629d4dcd
RS
670 (incf prefix)
671 (callf substring word 1))
672 (let ((found (assoc word '(("NUL" . "\0") ("RET" . "\r")
673 ("LFD" . "\n") ("TAB" . "\t")
674 ("ESC" . "\e") ("SPC" . " ")
675 ("DEL" . "\177")))))
676 (when found (setq word (cdr found))))
677 (when (string-match "^\\\\[0-7]+$" word)
678 (loop for ch across word
679 for n = 0 then (+ (* n 8) ch -48)
680 finally do (setq word (vector n))))
681 (cond ((= bits 0)
682 (setq key word))
a9b8cb16 683 ((and (= bits ?\M-\^@) (stringp word)
629d4dcd
RS
684 (string-match "^-?[0-9]+$" word))
685 (setq key (loop for x across word collect (+ x bits))))
686 ((/= (length word) 1)
687 (error "%s must prefix a single character, not %s"
688 (substring orig-word 0 prefix) word))
a9b8cb16 689 ((and (/= (logand bits ?\C-\^@) 0) (stringp word)
629d4dcd 690 (string-match "[@-_.a-z?]" word))
a9b8cb16 691 (setq key (list (+ bits (- ?\C-\^@)
629d4dcd
RS
692 (if (equal word "?") 127
693 (logand (aref word 0) 31))))))
694 (t
695 (setq key (list (+ bits (aref word 0)))))))))
696 (when key
697 (loop repeat times do (callf vconcat res key)))))
698 (when (and (>= (length res) 4)
699 (eq (aref res 0) ?\C-x)
700 (eq (aref res 1) ?\()
701 (eq (aref res (- (length res) 2)) ?\C-x)
702 (eq (aref res (- (length res) 1)) ?\)))
c31afdbd 703 (setq res (edmacro-subseq res 2 -2)))
629d4dcd
RS
704 (if (and (not need-vector)
705 (loop for ch across res
706 always (and (integerp ch)
a9b8cb16 707 (let ((ch2 (logand ch (lognot ?\M-\^@))))
629d4dcd
RS
708 (and (>= ch2 0) (<= ch2 127))))))
709 (concat (loop for ch across res
a9b8cb16 710 collect (if (= (logand ch ?\M-\^@) 0)
629d4dcd
RS
711 ch (+ ch 128))))
712 res)))
713\f
714;;; The following probably ought to go in macros.el:
715
716;;;###autoload
717(defun insert-kbd-macro (macroname &optional keys)
718 "Insert in buffer the definition of kbd macro NAME, as Lisp code.
719Optional second arg KEYS means also record the keys it is on
720\(this is the prefix argument, when calling interactively).
721
722This Lisp code will, when executed, define the kbd macro with the same
723definition it has now. If you say to record the keys, the Lisp code
724will also rebind those keys to the macro. Only global key bindings
725are recorded since executing this Lisp code always makes global
726bindings.
727
728To save a kbd macro, visit a file of Lisp code such as your `~/.emacs',
729use this command, and then save the file."
730 (interactive "CInsert kbd macro (name): \nP")
731 (let (definition)
732 (if (string= (symbol-name macroname) "")
733 (progn
734 (setq definition (format-kbd-macro))
735 (insert "(setq last-kbd-macro"))
736 (setq definition (format-kbd-macro macroname))
737 (insert (format "(defalias '%s" macroname)))
738 (if (> (length definition) 50)
739 (insert " (read-kbd-macro\n")
740 (insert "\n (read-kbd-macro "))
741 (prin1 definition (current-buffer))
742 (insert "))\n")
743 (if keys
3f5e6d79 744 (let ((keys (where-is-internal macroname '(keymap))))
629d4dcd
RS
745 (while keys
746 (insert (format "(global-set-key %S '%s)\n" (car keys) macroname))
747 (setq keys (cdr keys)))))))
748
749(provide 'edmacro)
c0274f38
ER
750
751;;; edmacro.el ends here
629d4dcd 752