1 ;;; edmacro.el --- keyboard macro editor
3 ;; Copyright (C) 1993-1994, 2001-2013 Free Software Foundation, Inc.
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Dave Gillespie <daveg@synaptics.com>
10 ;; This file is part of GNU Emacs.
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
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; The `C-x C-k e' (`edit-kbd-macro') command edits a keyboard macro
30 ;; in a special buffer. It prompts you to type a key sequence,
31 ;; which should be one of:
33 ;; * RET or `C-x e' (call-last-kbd-macro), to edit the most
34 ;; recently defined keyboard macro.
36 ;; * `M-x' followed by a command name, to edit a named command
37 ;; whose definition is a keyboard macro.
39 ;; * `C-h l' (view-lossage), to edit the 300 most recent keystrokes
40 ;; and install them as the "current" macro.
42 ;; * any key sequence whose definition is a keyboard macro.
44 ;; This file includes a version of `insert-kbd-macro' that uses the
45 ;; more readable format defined by these routines.
47 ;; Also, the `read-kbd-macro' command parses the region as
48 ;; a keyboard macro, and installs it as the "current" macro.
49 ;; This and `format-kbd-macro' can also be called directly as
52 ;; Type `C-h m', or see the documentation for `edmacro-mode' below,
53 ;; for information about the format of written keyboard macros.
55 ;; `edit-kbd-macro' formats the macro with one command per line,
56 ;; including the command names as comments on the right. If the
57 ;; formatter gets confused about which keymap was used for the
58 ;; characters, the command-name comments will be wrong but that
59 ;; won't hurt anything.
61 ;; With a prefix argument, `edit-kbd-macro' will format the
62 ;; macro in a more concise way that omits the comments.
69 ;;; The user-level commands for editing macros.
71 (defcustom edmacro-eight-bits nil
72 "Non-nil if `edit-kbd-macro' should leave 8-bit characters intact.
73 Default nil means to write characters above \\177 in octal notation."
77 (defvar edmacro-mode-map
78 (let ((map (make-sparse-keymap)))
79 (define-key map
"\C-c\C-c" 'edmacro-finish-edit
)
80 (define-key map
"\C-c\C-q" 'edmacro-insert-key
)
83 (defvar edmacro-store-hook
)
84 (defvar edmacro-finish-hook
)
85 (defvar edmacro-original-buffer
)
88 (defun edit-kbd-macro (keys &optional prefix finish-hook store-hook
)
89 "Edit a keyboard macro.
90 At the prompt, type any key sequence which is bound to a keyboard macro.
91 Or, type `C-x e' or RET to edit the last keyboard macro, `C-h l' to edit
92 the last 300 keystrokes as a keyboard macro, or `M-x' to edit a macro by
94 With a prefix argument, format the macro in a more concise way."
95 (interactive "kKeyboard macro to edit (C-x e, M-x, C-h l, or keys): \nP")
97 (let ((cmd (if (arrayp keys
) (key-binding keys
) keys
))
98 (mac nil
) (mac-counter nil
) (mac-format nil
)
103 ((or (memq cmd
'(call-last-kbd-macro kmacro-call-macro
104 kmacro-end-or-call-macro kmacro-end-and-call-macro
))
105 (member keys
'("\r" [return])))
107 (y-or-n-p "No keyboard macro defined. Create one? ")
109 (setq mac (or last-kbd-macro ""))
111 (setq cmd 'last-kbd-macro))
112 ((eq cmd 'execute-extended-command)
113 (setq cmd (read-command "Name of keyboard macro to edit: "))
114 (if (string-equal cmd "")
115 (error "No command name given"))
117 (setq mac (symbol-function cmd)))
118 ((memq cmd '(view-lossage electric-view-lossage))
119 (setq mac (recent-keys))
121 (setq cmd 'last-kbd-macro))
123 (error "Key sequence %s is not defined" (key-description keys)))
125 (setq mac (symbol-function cmd)))
129 (when (setq kmacro (kmacro-extract-lambda mac))
130 (setq mac (car kmacro)
131 mac-counter (nth 1 kmacro)
132 mac-format (nth 2 kmacro)))
134 (error "Key sequence %s is not a keyboard macro"
135 (key-description keys)))
136 (message "Formatting keyboard macro...")
137 (let* ((oldbuf (current-buffer))
138 (mmac (edmacro-fix-menu-commands mac))
139 (fmt (edmacro-format-keys mmac 1))
140 (fmtv (edmacro-format-keys mmac (not prefix)))
141 (buf (get-buffer-create "*Edit Macro*")))
142 (message "Formatting keyboard macro...done")
143 (switch-to-buffer buf)
144 (kill-all-local-variables)
145 (use-local-map edmacro-mode-map)
146 (setq buffer-read-only nil)
147 (setq major-mode 'edmacro-mode)
148 (setq mode-name "Edit Macro")
149 (set (make-local-variable 'edmacro-original-buffer) oldbuf)
150 (set (make-local-variable 'edmacro-finish-hook) finish-hook)
151 (set (make-local-variable 'edmacro-store-hook) store-hook)
153 (insert ";; Keyboard Macro Editor. Press C-c C-c to finish; "
154 "press C-x k RET to cancel.\n")
155 (insert ";; Original keys: " fmt "\n")
157 (insert "\nCommand: " (if cmd (symbol-name cmd) "none") "\n")
158 (let ((gkeys (where-is-internal (or cmd mac) '(keymap))))
159 (if (and keys (not (member keys gkeys)))
160 (setq gkeys (cons keys gkeys)))
163 (insert "Key: " (edmacro-format-keys (pop gkeys) 1) "\n"))
164 (insert "Key: none\n")))
165 (when (and mac-counter mac-format)
166 (insert (format "Counter: %d\nFormat: \"%s\"\n" mac-counter mac-format))))
167 (insert "\nMacro:\n\n")
172 (set-buffer-modified-p nil))
173 (run-hooks 'edmacro-format-hook)))))
175 ;;; The next two commands are provided for convenience and backward
179 (defun edit-last-kbd-macro (&optional prefix)
180 "Edit the most recently defined keyboard macro."
182 (edit-kbd-macro 'call-last-kbd-macro prefix))
185 (defun edit-named-kbd-macro (&optional prefix)
186 "Edit a keyboard macro which has been given a name by `name-last-kbd-macro'."
188 (edit-kbd-macro 'execute-extended-command prefix))
191 (defun read-kbd-macro (start &optional end)
192 "Read the region as a keyboard macro definition.
193 The region is interpreted as spelled-out keystrokes, e.g., \"M-x abc RET\".
194 See documentation for `edmacro-mode' for details.
195 Leading/trailing \"C-x (\" and \"C-x )\" in the text are allowed and ignored.
196 The resulting macro is installed as the \"current\" keyboard macro.
198 In Lisp, may also be called with a single STRING argument in which case
199 the result is returned rather than being installed as the current macro.
200 The result will be a string if possible, otherwise an event vector.
201 Second argument NEED-VECTOR means to return an event vector always."
204 (edmacro-parse-keys start end)
205 (setq last-kbd-macro (edmacro-parse-keys (buffer-substring start end)))))
208 (defun format-kbd-macro (&optional macro verbose)
209 "Return the keyboard macro MACRO as a human-readable string.
210 This string is suitable for passing to `read-kbd-macro'.
211 Second argument VERBOSE means to put one command per line with comments.
212 If VERBOSE is `1', put everything on one line. If VERBOSE is omitted
213 or nil, use a compact 80-column format."
214 (and macro (symbolp macro) (setq macro (symbol-function macro)))
215 (edmacro-format-keys (or macro last-kbd-macro) verbose))
217 ;;; Commands for *Edit Macro* buffer.
219 (defun edmacro-finish-edit ()
221 (unless (eq major-mode 'edmacro-mode)
223 "This command is valid only in buffers created by `edit-kbd-macro'"))
224 (run-hooks 'edmacro-finish-hook)
225 (let ((cmd nil) (keys nil) (no-keys nil)
226 (mac-counter nil) (mac-format nil)
229 (let ((case-fold-search nil))
230 (while (cond ((looking-at "[ \t]*\\($\\|;;\\|REM[ \t\n]\\)")
232 ((looking-at "Command:[ \t]*\\([^ \t\n]*\\)[ \t]*$")
233 (when edmacro-store-hook
234 (error "\"Command\" line not allowed in this context"))
235 (let ((str (buffer-substring (match-beginning 1)
237 (unless (equal str "")
238 (setq cmd (and (not (equal str "none"))
240 (and (fboundp cmd) (not (arrayp (symbol-function cmd)))
241 (not (get cmd 'kmacro))
243 (format "Command %s is already defined; %s"
247 ((looking-at "Key:\\(.*\\)$")
248 (when edmacro-store-hook
249 (error "\"Key\" line not allowed in this context"))
250 (let ((key (edmacro-parse-keys
251 (buffer-substring (match-beginning 1)
253 (unless (equal key "")
254 (if (equal key "none")
257 (let ((b (key-binding key)))
258 (and b (commandp b) (not (arrayp b))
259 (not (kmacro-extract-lambda b))
260 (or (not (fboundp b))
261 (not (or (arrayp (symbol-function b))
264 (format "Key %s is already defined; %s"
265 (edmacro-format-keys key 1)
269 ((looking-at "Counter:[ \t]*\\([^ \t\n]*\\)[ \t]*$")
270 (when edmacro-store-hook
271 (error "\"Counter\" line not allowed in this context"))
272 (let ((str (buffer-substring (match-beginning 1)
274 (unless (equal str "")
275 (setq mac-counter (string-to-number str))))
277 ((looking-at "Format:[ \t]*\"\\([^\n]*\\)\"[ \t]*$")
278 (when edmacro-store-hook
279 (error "\"Format\" line not allowed in this context"))
280 (let ((str (buffer-substring (match-beginning 1)
282 (unless (equal str "")
283 (setq mac-format str)))
285 ((looking-at "Macro:[ \t\n]*")
286 (goto-char (match-end 0))
289 (t (error "Expected a `Macro:' line")))
292 (let* ((buf (current-buffer))
293 (str (buffer-substring top (point-max)))
294 (modp (buffer-modified-p))
295 (obuf edmacro-original-buffer)
296 (store-hook edmacro-store-hook)
297 (finish-hook edmacro-finish-hook))
298 (unless (or cmd keys store-hook (equal str ""))
299 (error "No command name or keys specified"))
301 (when (buffer-name obuf)
303 (message "Compiling keyboard macro...")
304 (let ((mac (edmacro-parse-keys str)))
305 (message "Compiling keyboard macro...done")
307 (funcall store-hook mac)
308 (when (eq cmd 'last-kbd-macro)
309 (setq last-kbd-macro (and (> (length mac) 0) mac))
312 (if (= (length mac) 0)
315 (if (and mac-counter mac-format)
316 (kmacro-lambda-form mac mac-counter mac-format)
320 (cl-loop for key in (where-is-internal cmd '(keymap)) do
321 (global-unset-key key)))
323 (if (= (length mac) 0)
324 (cl-loop for key in keys do (global-unset-key key))
325 (cl-loop for key in keys do
328 (if (and mac-counter mac-format)
330 mac mac-counter mac-format)
333 (when (buffer-name obuf)
334 (switch-to-buffer obuf))
336 (funcall finish-hook)))))
338 (defun edmacro-insert-key (key)
339 "Insert the written name of a key in the buffer."
340 (interactive "kKey to insert: ")
342 (insert (edmacro-format-keys key t) "\n")
343 (insert (edmacro-format-keys key) " ")))
345 (defun edmacro-mode ()
346 "\\<edmacro-mode-map>Keyboard Macro Editing mode. Press \
347 \\[edmacro-finish-edit] to save and exit.
348 To abort the edit, just kill this buffer with \\[kill-buffer] RET.
350 Press \\[edmacro-insert-key] to insert the name of any key by typing the key.
352 The editing buffer contains a \"Command:\" line and any number of
353 \"Key:\" lines at the top. These are followed by a \"Macro:\" line
354 and the macro itself as spelled-out keystrokes: `C-x C-f foo RET'.
356 The \"Command:\" line specifies the command name to which the macro
357 is bound, or \"none\" for no command name. Write \"last-kbd-macro\"
358 to refer to the current keyboard macro (as used by \\[call-last-kbd-macro]).
360 The \"Key:\" lines specify key sequences to which the macro is bound,
361 or \"none\" for no key bindings.
363 You can edit these lines to change the places where the new macro
367 Format of keyboard macros during editing:
369 Text is divided into \"words\" separated by whitespace. Except for
370 the words described below, the characters of each word go directly
371 as characters of the macro. The whitespace that separates words
372 is ignored. Whitespace in the macro must be written explicitly,
373 as in \"foo SPC bar RET\".
375 * The special words RET, SPC, TAB, DEL, LFD, ESC, and NUL represent
376 special control characters. The words must be written in uppercase.
378 * A word in angle brackets, e.g., <return>, <down>, or <f1>, represents
379 a function key. (Note that in the standard configuration, the
380 function key <return> and the control key RET are synonymous.)
381 You can use angle brackets on the words RET, SPC, etc., but they
382 are not required there.
384 * Keys can be written by their ASCII code, using a backslash followed
385 by up to six octal digits. This is the only way to represent keys
386 with codes above \\377.
388 * One or more prefixes M- (meta), C- (control), S- (shift), A- (alt),
389 H- (hyper), and s- (super) may precede a character or key notation.
390 For function keys, the prefixes may go inside or outside of the
391 brackets: C-<down> = <C-down>. The prefixes may be written in
392 any order: M-C-x = C-M-x.
394 Prefixes are not allowed on multi-key words, e.g., C-abc, except
395 that the Meta prefix is allowed on a sequence of digits and optional
396 minus sign: M--123 = M-- M-1 M-2 M-3.
398 * The `^' notation for control characters also works: ^M = C-m.
400 * Double angle brackets enclose command names: <<next-line>> is
401 shorthand for M-x next-line RET.
403 * Finally, REM or ;; causes the rest of the line to be ignored as a
406 Any word may be prefixed by a multiplier in the form of a decimal
407 number and `*': 3*<right> = <right> <right> <right>, and
408 10*foo = foofoofoofoofoofoofoofoofoofoo.
410 Multiple text keys can normally be strung together to form a word,
411 but you may need to add whitespace if the word would look like one
412 of the above notations: `; ; ;' is a keyboard macro with three
413 semicolons, but `;;;' is a comment. Likewise, `\\ 1 2 3' is four
414 keys but `\\123' is a single key written in octal, and `< right >'
415 is seven keys but `<right>' is a single function key. When in
416 doubt, use whitespace."
418 (error "This mode can be enabled only by `edit-kbd-macro'"))
419 (put 'edmacro-mode 'mode-class 'special)
421 ;;; Formatting a keyboard macro as human-readable text.
423 (defun edmacro-format-keys (macro &optional verbose)
424 (setq macro (edmacro-fix-menu-commands macro))
425 (let* ((maps (current-active-maps))
426 (pkeys '(end-macro ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?- ?\C-u
427 ?\M-- ?\M-0 ?\M-1 ?\M-2 ?\M-3 ?\M-4 ?\M-5 ?\M-6
429 (mdigs (nthcdr 13 pkeys))
430 (maxkey (if edmacro-eight-bits 255 127))
431 (case-fold-search nil)
432 (res-words '("NUL" "TAB" "LFD" "RET" "ESC" "SPC" "DEL" "REM"))
433 (rest-mac (vconcat macro [end-macro]))
436 (one-line (eq verbose 1)))
437 (if one-line (setq verbose nil))
438 (when (stringp macro)
439 (cl-loop for i below (length macro) do
440 (when (>= (aref rest-mac i) 128)
441 (cl-incf (aref rest-mac i) (- ?\M-\^@ 128)))))
442 (while (not (eq (aref rest-mac 0) 'end-macro))
444 (or (and (integerp (aref rest-mac 0))
445 (memq (aref rest-mac 0) mdigs)
446 (memq (key-binding (cl-subseq rest-mac 0 1))
447 '(digit-argument negative-argument))
449 (while (memq (aref rest-mac i) (cdr mdigs))
451 (and (not (memq (aref rest-mac i) pkeys))
452 (prog1 (vconcat "M-" (cl-subseq rest-mac 0 i) " ")
453 (cl-callf cl-subseq rest-mac i)))))
454 (and (eq (aref rest-mac 0) ?\C-u)
455 (eq (key-binding [?\C-u]) 'universal-argument)
457 (while (eq (aref rest-mac i) ?\C-u)
459 (and (not (memq (aref rest-mac i) pkeys))
460 (prog1 (cl-loop repeat i concat "C-u ")
461 (cl-callf cl-subseq rest-mac i)))))
462 (and (eq (aref rest-mac 0) ?\C-u)
463 (eq (key-binding [?\C-u]) 'universal-argument)
465 (when (eq (aref rest-mac i) ?-)
467 (while (memq (aref rest-mac i)
468 '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
470 (and (not (memq (aref rest-mac i) pkeys))
471 (prog1 (vconcat "C-u " (cl-subseq rest-mac 1 i) " ")
472 (cl-callf cl-subseq rest-mac i)))))))
473 (bind-len (apply 'max 1
474 (cl-loop for map in maps
475 for b = (lookup-key map rest-mac)
477 (key (cl-subseq rest-mac 0 bind-len))
479 (bind (or (cl-loop for map in maps for b = (lookup-key map key)
480 thereis (and (not (integerp b)) b))
481 (and (setq fkey (lookup-key local-function-key-map rest-mac))
482 (setq tlen fkey tkey (cl-subseq rest-mac 0 tlen)
483 fkey (lookup-key local-function-key-map tkey))
484 (cl-loop for map in maps
485 for b = (lookup-key map fkey)
486 when (and (not (integerp b)) b)
487 do (setq bind-len tlen key tkey)
489 finally do (setq fkey nil)))))
492 (cl-loop for i from bind-len below (length rest-mac)
493 for ch = (aref rest-mac i)
494 while (and (integerp ch)
495 (> ch 32) (< ch maxkey) (/= ch 92)
496 (eq (key-binding (char-to-string ch))
497 'self-insert-command)
498 (or (> i (- (length rest-mac) 2))
499 (not (eq ch (aref rest-mac (+ i 1))))
500 (not (eq ch (aref rest-mac (+ i 2))))))
503 (if (stringp bind) (setq bind nil))
504 (cond ((and (eq bind 'self-insert-command) (not prefix)
505 (> text 1) (integerp first)
506 (> first 32) (<= first maxkey) (/= first 92)
508 (if (> text 30) (setq text 30))
509 (setq desc (concat (cl-subseq rest-mac 0 text)))
510 (when (string-match "^[ACHMsS]-." desc)
512 (cl-callf substring desc 0 2))
514 "^;;\\|^<.*>$\\|^\\\\[0-9]+$\\|^[0-9]+\\*."
516 (when (or (string-match "^\\^.$" desc)
517 (member desc res-words))
518 (setq desc (mapconcat 'char-to-string desc " ")))
520 (setq bind (format "%s * %d" bind text)))
521 (setq bind-len text))
522 ((and (eq bind 'execute-extended-command)
524 (memq (aref rest-mac text) '(return 13))
526 (setq desc (concat (cl-subseq rest-mac bind-len text)))
527 (commandp (intern-soft desc))))
528 (if (commandp (intern-soft desc)) (setq bind desc))
529 (setq desc (format "<<%s>>" desc))
530 (setq bind-len (1+ text)))
532 (setq desc (mapconcat
538 (cl-loop for pf across "ACHMsS"
539 for bit in '(?\A-\^@ ?\C-\^@ ?\H-\^@
540 ?\M-\^@ ?\s-\^@ ?\S-\^@)
541 when (/= (logand ch bit) 0)
542 concat (format "%c-" pf))
543 (let ((ch2 (logand ch (1- (lsh 1 18)))))
546 (0 "NUL") (9 "TAB") (10 "LFD")
547 (13 "RET") (27 "ESC") (32 "SPC")
550 (+ (if (<= ch2 26) 96 64)
553 ((<= ch2 maxkey) (char-to-string ch2))
554 (t (format "\\%o" ch2))))))
558 (error "Unrecognized item in macro: %s" ch)))))
559 (or fkey key) " "))))
561 (setq desc (concat (edmacro-sanitize-for-string prefix) desc)))
562 (unless (string-match " " desc)
563 (let ((times 1) (pos bind-len))
564 (while (not (cl-mismatch rest-mac rest-mac
565 :start1 0 :end1 bind-len
566 :start2 pos :end2 (+ bind-len pos)))
568 (cl-incf pos bind-len))
570 (setq desc (format "%d*%s" times desc))
571 (setq bind-len (* bind-len times)))))
572 (setq rest-mac (cl-subseq rest-mac bind-len))
575 (unless (equal res "") (cl-callf concat res "\n"))
576 (cl-callf concat res desc)
577 (when (and bind (or (stringp bind) (symbolp bind)))
579 (make-string (max (- 3 (/ (length desc) 8)) 1) 9)
580 ";; " (if (stringp bind) bind (symbol-name bind))))
582 (if (and (> (+ len (length desc) 2) 72) (not one-line))
584 (cl-callf concat res "\n ")
586 (unless (equal res "")
587 (cl-callf concat res " ")
589 (cl-callf concat res desc)
590 (cl-incf len (length desc)))))
593 (defun edmacro-sanitize-for-string (seq)
594 "Convert a key sequence vector SEQ into a string.
595 The string represents the same events; Meta is indicated by bit 7.
596 This function assumes that the events can be stored in a string."
597 (setq seq (copy-sequence seq))
598 (cl-loop for i below (length seq) do
599 (when (logand (aref seq i) 128)
600 (setf (aref seq i) (logand (aref seq i) 127))))
603 (defun edmacro-fix-menu-commands (macro &optional noerror)
606 ;; Make a list of the elements.
607 (setq macro (append macro nil))
611 ((eq (car ev) 'help-echo))
612 ((eq (car ev) 'switch-frame))
613 ((equal ev '(menu-bar))
614 (push 'menu-bar result))
615 ((equal (cl-cadadr ev) '(menu-bar))
616 (push (vector 'menu-bar (car ev)) result))
617 ;; It would be nice to do pop-up menus, too, but not enough
618 ;; info is recorded in macros to make this possible.
620 ;; Just ignore mouse events.
623 (error "Macros with mouse clicks are not %s"
624 "supported by this command"))))
625 ;; Reverse them again and make them back into a vector.
626 (vconcat (nreverse result)))
629 ;;; Parsing a human-readable keyboard macro.
631 (defun edmacro-parse-keys (string &optional need-vector)
632 (let ((case-fold-search nil)
633 (len (length string)) ; We won't alter string in the loop below.
636 (while (and (< pos len)
637 (string-match "[^ \t\n\f]+" string pos))
638 (let* ((word-beg (match-beginning 0))
639 (word-end (match-end 0))
640 (word (substring string word-beg len))
643 ;; Try to catch events of the form "<as df>".
644 (if (string-match "\\`<[^ <>\t\n\f][^>\t\n\f]*>" word)
645 (setq word (match-string 0 word)
646 pos (+ word-beg (match-end 0)))
647 (setq word (substring string word-beg word-end)
649 (when (string-match "\\([0-9]+\\)\\*." word)
650 (setq times (string-to-number (substring word 0 (match-end 1))))
651 (setq word (substring word (1+ (match-end 1)))))
652 (cond ((string-match "^<<.+>>$" word)
653 (setq key (vconcat (if (eq (key-binding [?\M-x])
654 'execute-extended-command)
656 (or (car (where-is-internal
657 'execute-extended-command))
659 (substring word 2 -2) "\r")))
660 ((and (string-match "^\\(\\([ACHMsS]-\\)*\\)<\\(.+\\)>$" word)
662 (setq word (concat (substring word (match-beginning 1)
664 (substring word (match-beginning 3)
667 "\\<\\(NUL\\|RET\\|LFD\\|ESC\\|SPC\\|DEL\\)$"
669 (setq key (list (intern word))))
670 ((or (equal word "REM") (string-match "^;;" word))
671 (setq pos (string-match "$" string pos)))
673 (let ((orig-word word) (prefix 0) (bits 0))
674 (while (string-match "^[ACHMsS]-." word)
675 (cl-incf bits (cdr (assq (aref word 0)
676 '((?A . ?\A-\^@) (?C . ?\C-\^@)
677 (?H . ?\H-\^@) (?M . ?\M-\^@)
678 (?s . ?\s-\^@) (?S . ?\S-\^@)))))
680 (cl-callf substring word 2))
681 (when (string-match "^\\^.$" word)
682 (cl-incf bits ?\C-\^@)
684 (cl-callf substring word 1))
685 (let ((found (assoc word '(("NUL" . "\0") ("RET" . "\r")
686 ("LFD" . "\n") ("TAB" . "\t")
687 ("ESC" . "\e") ("SPC" . " ")
689 (when found (setq word (cdr found))))
690 (when (string-match "^\\\\[0-7]+$" word)
691 (cl-loop for ch across word
692 for n = 0 then (+ (* n 8) ch -48)
693 finally do (setq word (vector n))))
696 ((and (= bits ?\M-\^@) (stringp word)
697 (string-match "^-?[0-9]+$" word))
698 (setq key (cl-loop for x across word
699 collect (+ x bits))))
700 ((/= (length word) 1)
701 (error "%s must prefix a single character, not %s"
702 (substring orig-word 0 prefix) word))
703 ((and (/= (logand bits ?\C-\^@) 0) (stringp word)
704 ;; We used to accept . and ? here,
705 ;; but . is simply wrong,
706 ;; and C-? is not used (we use DEL instead).
707 (string-match "[@-_a-z]" word))
708 (setq key (list (+ bits (- ?\C-\^@)
709 (logand (aref word 0) 31)))))
711 (setq key (list (+ bits (aref word 0)))))))))
713 (cl-loop repeat times do (cl-callf vconcat res key)))))
714 (when (and (>= (length res) 4)
715 (eq (aref res 0) ?\C-x)
716 (eq (aref res 1) ?\()
717 (eq (aref res (- (length res) 2)) ?\C-x)
718 (eq (aref res (- (length res) 1)) ?\)))
719 (setq res (cl-subseq res 2 -2)))
720 (if (and (not need-vector)
721 (cl-loop for ch across res
722 always (and (characterp ch)
723 (let ((ch2 (logand ch (lognot ?\M-\^@))))
724 (and (>= ch2 0) (<= ch2 127))))))
725 (concat (cl-loop for ch across res
726 collect (if (= (logand ch ?\M-\^@) 0)
732 ;;; edmacro.el ends here