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