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