; doc/emacs/misc.texi (Network Security): Fix typo.
[emacs.git] / lisp / edmacro.el
blob781806279507878f37bb303cdb6e0ff70c2c8e4c
1 ;;; edmacro.el --- keyboard macro editor
3 ;; Copyright (C) 1993-1994, 2001-2018 Free Software Foundation, Inc.
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Dave Gillespie <daveg@synaptics.com>
7 ;; Version: 2.01
8 ;; Keywords: abbrev
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 <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; Usage:
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
50 ;; Lisp functions.
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.
64 ;;; Code:
66 (require 'cl-lib)
67 (require 'kmacro)
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."
74 :type 'boolean
75 :group 'kmacro)
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)
81 map))
83 (defvar edmacro-store-hook)
84 (defvar edmacro-finish-hook)
85 (defvar edmacro-original-buffer)
87 ;;;###autoload
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 `\\[kmacro-end-and-call-macro]' or RET to edit the last
92 keyboard macro, `\\[view-lossage]' to edit the last 300
93 keystrokes as a keyboard macro, or `\\[execute-extended-command]'
94 to edit a macro by its command name.
95 With a prefix argument, format the macro in a more concise way."
96 (interactive
97 (list (read-key-sequence (substitute-command-keys "Keyboard macro to edit \
98 \(\\[kmacro-end-and-call-macro], \\[execute-extended-command], \\[view-lossage],\
99 or keys): "))
100 current-prefix-arg))
101 (when keys
102 (let ((cmd (if (arrayp keys) (key-binding keys) keys))
103 (cmd-noremap (when (arrayp keys) (key-binding keys nil t)))
104 (mac nil) (mac-counter nil) (mac-format nil)
105 kmacro)
106 (cond (store-hook
107 (setq mac keys)
108 (setq cmd nil))
109 ((or (memq cmd '(call-last-kbd-macro kmacro-call-macro kmacro-end-or-call-macro kmacro-end-and-call-macro))
110 (memq cmd-noremap '(call-last-kbd-macro kmacro-call-macro kmacro-end-or-call-macro kmacro-end-and-call-macro))
111 (member keys '("\r" [return])))
112 (or last-kbd-macro
113 (y-or-n-p "No keyboard macro defined. Create one? ")
114 (keyboard-quit))
115 (setq mac (or last-kbd-macro ""))
116 (setq keys nil)
117 (setq cmd 'last-kbd-macro))
118 ((memq 'execute-extended-command (list cmd cmd-noremap))
119 (setq cmd (read-command "Name of keyboard macro to edit: "))
120 (if (string-equal cmd "")
121 (error "No command name given"))
122 (setq keys nil)
123 (setq mac (symbol-function cmd)))
124 ((or (memq cmd '(view-lossage electric-view-lossage))
125 (memq cmd-noremap '(view-lossage electric-view-lossage)))
126 (setq mac (recent-keys))
127 (setq keys nil)
128 (setq cmd 'last-kbd-macro))
129 ((null cmd)
130 (error "Key sequence %s is not defined" (key-description keys)))
131 ((symbolp cmd)
132 (setq mac (symbol-function cmd)))
134 (setq mac cmd)
135 (setq cmd nil)))
136 (when (setq kmacro (kmacro-extract-lambda mac))
137 (setq mac (car kmacro)
138 mac-counter (nth 1 kmacro)
139 mac-format (nth 2 kmacro)))
140 (unless (arrayp mac)
141 (error "Key sequence %s is not a keyboard macro"
142 (key-description keys)))
143 (message "Formatting keyboard macro...")
144 (let* ((oldbuf (current-buffer))
145 (mmac (edmacro-fix-menu-commands mac))
146 (fmt (edmacro-format-keys mmac 1))
147 (fmtv (edmacro-format-keys mmac (not prefix)))
148 (buf (get-buffer-create "*Edit Macro*")))
149 (message "Formatting keyboard macro...done")
150 (switch-to-buffer buf)
151 (kill-all-local-variables)
152 (use-local-map edmacro-mode-map)
153 (setq buffer-read-only nil)
154 (setq major-mode 'edmacro-mode)
155 (setq mode-name "Edit Macro")
156 (set (make-local-variable 'edmacro-original-buffer) oldbuf)
157 (set (make-local-variable 'edmacro-finish-hook) finish-hook)
158 (set (make-local-variable 'edmacro-store-hook) store-hook)
159 (erase-buffer)
160 (insert ";; Keyboard Macro Editor. Press C-c C-c to finish; "
161 "press C-x k RET to cancel.\n")
162 (insert ";; Original keys: " fmt "\n")
163 (unless store-hook
164 (insert "\nCommand: " (if cmd (symbol-name cmd) "none") "\n")
165 (let ((gkeys (where-is-internal (or cmd mac) '(keymap))))
166 (if (and keys (not (member keys gkeys)))
167 (setq gkeys (cons keys gkeys)))
168 (if gkeys
169 (while gkeys
170 (insert "Key: " (edmacro-format-keys (pop gkeys) 1) "\n"))
171 (insert "Key: none\n")))
172 (when (and mac-counter mac-format)
173 (insert (format "Counter: %d\nFormat: \"%s\"\n" mac-counter mac-format))))
174 (insert "\nMacro:\n\n")
175 (save-excursion
176 (insert fmtv "\n"))
177 (recenter '(4))
178 (when (eq mac mmac)
179 (set-buffer-modified-p nil))
180 (run-hooks 'edmacro-format-hook)))))
182 ;;; The next two commands are provided for convenience and backward
183 ;;; compatibility.
185 ;;;###autoload
186 (defun edit-last-kbd-macro (&optional prefix)
187 "Edit the most recently defined keyboard macro."
188 (interactive "P")
189 (edit-kbd-macro 'call-last-kbd-macro prefix))
191 ;;;###autoload
192 (defun edit-named-kbd-macro (&optional prefix)
193 "Edit a keyboard macro which has been given a name by `name-last-kbd-macro'."
194 (interactive "P")
195 (edit-kbd-macro 'execute-extended-command prefix))
197 ;;;###autoload
198 (defun read-kbd-macro (start &optional end)
199 "Read the region as a keyboard macro definition.
200 The region is interpreted as spelled-out keystrokes, e.g., \"M-x abc RET\".
201 See documentation for `edmacro-mode' for details.
202 Leading/trailing \"C-x (\" and \"C-x )\" in the text are allowed and ignored.
203 The resulting macro is installed as the \"current\" keyboard macro.
205 In Lisp, may also be called with a single STRING argument in which case
206 the result is returned rather than being installed as the current macro.
207 The result will be a string if possible, otherwise an event vector.
208 Second argument NEED-VECTOR means to return an event vector always."
209 (interactive "r")
210 (if (stringp start)
211 (edmacro-parse-keys start end)
212 (setq last-kbd-macro (edmacro-parse-keys (buffer-substring start end)))))
214 ;;;###autoload
215 (defun format-kbd-macro (&optional macro verbose)
216 "Return the keyboard macro MACRO as a human-readable string.
217 This string is suitable for passing to `read-kbd-macro'.
218 Second argument VERBOSE means to put one command per line with comments.
219 If VERBOSE is `1', put everything on one line. If VERBOSE is omitted
220 or nil, use a compact 80-column format."
221 (and macro (symbolp macro) (setq macro (symbol-function macro)))
222 (edmacro-format-keys (or macro last-kbd-macro) verbose))
224 ;;; Commands for *Edit Macro* buffer.
226 (defun edmacro-finish-edit ()
227 (interactive)
228 (unless (eq major-mode 'edmacro-mode)
229 (error
230 "This command is valid only in buffers created by `edit-kbd-macro'"))
231 (run-hooks 'edmacro-finish-hook)
232 (let ((cmd nil) (keys nil) (no-keys nil)
233 (mac-counter nil) (mac-format nil)
234 (top (point-min)))
235 (goto-char top)
236 (let ((case-fold-search nil))
237 (while (cond ((looking-at "[ \t]*\\($\\|;;\\|REM[ \t\n]\\)")
239 ((looking-at "Command:[ \t]*\\([^ \t\n]*\\)[ \t]*$")
240 (when edmacro-store-hook
241 (error "\"Command\" line not allowed in this context"))
242 (let ((str (buffer-substring (match-beginning 1)
243 (match-end 1))))
244 (unless (equal str "")
245 (setq cmd (and (not (equal str "none"))
246 (intern str)))
247 (and (fboundp cmd) (not (arrayp (symbol-function cmd)))
248 (not (get cmd 'kmacro))
249 (not (y-or-n-p
250 (format "Command %s is already defined; %s"
251 cmd "proceed? ")))
252 (keyboard-quit))))
254 ((looking-at "Key:\\(.*\\)$")
255 (when edmacro-store-hook
256 (error "\"Key\" line not allowed in this context"))
257 (let ((key (edmacro-parse-keys
258 (buffer-substring (match-beginning 1)
259 (match-end 1)))))
260 (unless (equal key "")
261 (if (equal key "none")
262 (setq no-keys t)
263 (push key keys)
264 (let ((b (key-binding key)))
265 (and b (commandp b) (not (arrayp b))
266 (not (kmacro-extract-lambda b))
267 (or (not (fboundp b))
268 (not (or (arrayp (symbol-function b))
269 (get b 'kmacro))))
270 (not (y-or-n-p
271 (format "Key %s is already defined; %s"
272 (edmacro-format-keys key 1)
273 "proceed? ")))
274 (keyboard-quit))))))
276 ((looking-at "Counter:[ \t]*\\([^ \t\n]*\\)[ \t]*$")
277 (when edmacro-store-hook
278 (error "\"Counter\" line not allowed in this context"))
279 (let ((str (buffer-substring (match-beginning 1)
280 (match-end 1))))
281 (unless (equal str "")
282 (setq mac-counter (string-to-number str))))
284 ((looking-at "Format:[ \t]*\"\\([^\n]*\\)\"[ \t]*$")
285 (when edmacro-store-hook
286 (error "\"Format\" line not allowed in this context"))
287 (let ((str (buffer-substring (match-beginning 1)
288 (match-end 1))))
289 (unless (equal str "")
290 (setq mac-format str)))
292 ((looking-at "Macro:[ \t\n]*")
293 (goto-char (match-end 0))
294 nil)
295 ((eobp) nil)
296 (t (error "Expected a `Macro:' line")))
297 (forward-line 1))
298 (setq top (point)))
299 (let* ((buf (current-buffer))
300 (str (buffer-substring top (point-max)))
301 (modp (buffer-modified-p))
302 (obuf edmacro-original-buffer)
303 (store-hook edmacro-store-hook)
304 (finish-hook edmacro-finish-hook))
305 (unless (or cmd keys store-hook (equal str ""))
306 (error "No command name or keys specified"))
307 (when modp
308 (when (buffer-name obuf)
309 (set-buffer obuf))
310 (message "Compiling keyboard macro...")
311 (let ((mac (edmacro-parse-keys str)))
312 (message "Compiling keyboard macro...done")
313 (if store-hook
314 (funcall store-hook mac)
315 (when (eq cmd 'last-kbd-macro)
316 (setq last-kbd-macro (and (> (length mac) 0) mac))
317 (setq cmd nil))
318 (when cmd
319 (if (= (length mac) 0)
320 (fmakunbound cmd)
321 (fset cmd
322 (if (and mac-counter mac-format)
323 (kmacro-lambda-form mac mac-counter mac-format)
324 mac))))
325 (if no-keys
326 (when cmd
327 (cl-loop for key in (where-is-internal cmd '(keymap)) do
328 (global-unset-key key)))
329 (when keys
330 (if (= (length mac) 0)
331 (cl-loop for key in keys do (global-unset-key key))
332 (cl-loop for key in keys do
333 (global-set-key key
334 (or cmd
335 (if (and mac-counter mac-format)
336 (kmacro-lambda-form
337 mac mac-counter mac-format)
338 mac))))))))))
339 (kill-buffer buf)
340 (when (buffer-name obuf)
341 (switch-to-buffer obuf))
342 (when finish-hook
343 (funcall finish-hook)))))
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) " ")))
352 (defun edmacro-mode ()
353 "\\<edmacro-mode-map>Keyboard Macro Editing mode. Press \
354 \\[edmacro-finish-edit] to save and exit.
355 To abort the edit, just kill this buffer with \\[kill-buffer] RET.
357 Press \\[edmacro-insert-key] to insert the name of any key by typing the key.
359 The editing buffer contains a \"Command:\" line and any number of
360 \"Key:\" lines at the top. These are followed by a \"Macro:\" line
361 and the macro itself as spelled-out keystrokes: `C-x C-f foo RET'.
363 The \"Command:\" line specifies the command name to which the macro
364 is bound, or \"none\" for no command name. Write \"last-kbd-macro\"
365 to refer to the current keyboard macro (as used by \\[call-last-kbd-macro]).
367 The \"Key:\" lines specify key sequences to which the macro is bound,
368 or \"none\" for no key bindings.
370 You can edit these lines to change the places where the new macro
371 is stored.
374 Format of keyboard macros during editing:
376 Text is divided into \"words\" separated by whitespace. Except for
377 the words described below, the characters of each word go directly
378 as characters of the macro. The whitespace that separates words
379 is ignored. Whitespace in the macro must be written explicitly,
380 as in \"foo SPC bar RET\".
382 * The special words RET, SPC, TAB, DEL, LFD, ESC, and NUL represent
383 special control characters. The words must be written in uppercase.
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.
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.
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.
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.
405 * The `^' notation for control characters also works: ^M = C-m.
407 * Double angle brackets enclose command names: <<next-line>> is
408 shorthand for M-x next-line RET.
410 * Finally, REM or ;; causes the rest of the line to be ignored as a
411 comment.
413 Any word may be prefixed by a multiplier in the form of a decimal
414 number and `*': 3*<right> = <right> <right> <right>, and
415 10*foo = foofoofoofoofoofoofoofoofoofoo.
417 Multiple text keys can normally be strung together to form a word,
418 but you may need to add whitespace if the word would look like one
419 of the above notations: `; ; ;' is a keyboard macro with three
420 semicolons, but `;;;' is a comment. Likewise, `\\ 1 2 3' is four
421 keys but `\\123' is a single key written in octal, and `< right >'
422 is seven keys but `<right>' is a single function key. When in
423 doubt, use whitespace."
424 (interactive)
425 (error "This mode can be enabled only by `edit-kbd-macro'"))
426 (put 'edmacro-mode 'mode-class 'special)
428 ;;; Formatting a keyboard macro as human-readable text.
430 (defun edmacro-format-keys (macro &optional verbose)
431 (setq macro (edmacro-fix-menu-commands macro))
432 (let* ((maps (current-active-maps))
433 (pkeys '(end-macro ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?- ?\C-u
434 ?\M-- ?\M-0 ?\M-1 ?\M-2 ?\M-3 ?\M-4 ?\M-5 ?\M-6
435 ?\M-7 ?\M-8 ?\M-9))
436 (mdigs (nthcdr 13 pkeys))
437 (maxkey (if edmacro-eight-bits 255 127))
438 (case-fold-search nil)
439 (res-words '("NUL" "TAB" "LFD" "RET" "ESC" "SPC" "DEL" "REM"))
440 (rest-mac (vconcat macro [end-macro]))
441 (res "")
442 (len 0)
443 (one-line (eq verbose 1)))
444 (if one-line (setq verbose nil))
445 (when (stringp macro)
446 (cl-loop for i below (length macro) do
447 (when (>= (aref rest-mac i) 128)
448 (cl-incf (aref rest-mac i) (- ?\M-\^@ 128)))))
449 (while (not (eq (aref rest-mac 0) 'end-macro))
450 (let* ((prefix
451 (or (and (integerp (aref rest-mac 0))
452 (memq (aref rest-mac 0) mdigs)
453 (memq (key-binding (cl-subseq rest-mac 0 1))
454 '(digit-argument negative-argument))
455 (let ((i 1))
456 (while (memq (aref rest-mac i) (cdr mdigs))
457 (cl-incf i))
458 (and (not (memq (aref rest-mac i) pkeys))
459 (prog1 (vconcat "M-" (cl-subseq rest-mac 0 i) " ")
460 (cl-callf cl-subseq rest-mac i)))))
461 (and (eq (aref rest-mac 0) ?\C-u)
462 (eq (key-binding [?\C-u]) 'universal-argument)
463 (let ((i 1))
464 (while (eq (aref rest-mac i) ?\C-u)
465 (cl-incf i))
466 (and (not (memq (aref rest-mac i) pkeys))
467 (prog1 (cl-loop repeat i concat "C-u ")
468 (cl-callf cl-subseq rest-mac i)))))
469 (and (eq (aref rest-mac 0) ?\C-u)
470 (eq (key-binding [?\C-u]) 'universal-argument)
471 (let ((i 1))
472 (when (eq (aref rest-mac i) ?-)
473 (cl-incf i))
474 (while (memq (aref rest-mac i)
475 '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
476 (cl-incf i))
477 (and (not (memq (aref rest-mac i) pkeys))
478 (prog1 (vconcat "C-u " (cl-subseq rest-mac 1 i) " ")
479 (cl-callf cl-subseq rest-mac i)))))))
480 (bind-len (apply 'max 1
481 (cl-loop for map in maps
482 for b = (lookup-key map rest-mac)
483 when b collect b)))
484 (key (cl-subseq rest-mac 0 bind-len))
485 (fkey nil) tlen tkey
486 (bind (or (cl-loop for map in maps for b = (lookup-key map key)
487 thereis (and (not (integerp b)) b))
488 (and (setq fkey (lookup-key local-function-key-map rest-mac))
489 (setq tlen fkey tkey (cl-subseq rest-mac 0 tlen)
490 fkey (lookup-key local-function-key-map tkey))
491 (cl-loop for map in maps
492 for b = (lookup-key map fkey)
493 when (and (not (integerp b)) b)
494 do (setq bind-len tlen key tkey)
495 and return b
496 finally do (setq fkey nil)))))
497 (first (aref key 0))
498 (text
499 (cl-loop for i from bind-len below (length rest-mac)
500 for ch = (aref rest-mac i)
501 while (and (integerp ch)
502 (> ch 32) (< ch maxkey) (/= ch 92)
503 (eq (key-binding (char-to-string ch))
504 'self-insert-command)
505 (or (> i (- (length rest-mac) 2))
506 (not (eq ch (aref rest-mac (+ i 1))))
507 (not (eq ch (aref rest-mac (+ i 2))))))
508 finally return i))
509 desc)
510 (if (stringp bind) (setq bind nil))
511 (cond ((and (eq bind 'self-insert-command) (not prefix)
512 (> text 1) (integerp first)
513 (> first 32) (<= first maxkey) (/= first 92)
514 (progn
515 (if (> text 30) (setq text 30))
516 (setq desc (concat (cl-subseq rest-mac 0 text)))
517 (when (string-match "^[ACHMsS]-." desc)
518 (setq text 2)
519 (cl-callf substring desc 0 2))
520 (not (string-match
521 "^;;\\|^<.*>$\\|^\\\\[0-9]+$\\|^[0-9]+\\*."
522 desc))))
523 (when (or (string-match "^\\^.$" desc)
524 (member desc res-words))
525 (setq desc (mapconcat 'char-to-string desc " ")))
526 (when verbose
527 (setq bind (format "%s * %d" bind text)))
528 (setq bind-len text))
529 ((and (eq bind 'execute-extended-command)
530 (> text bind-len)
531 (memq (aref rest-mac text) '(return 13))
532 (progn
533 (setq desc (concat (cl-subseq rest-mac bind-len text)))
534 (commandp (intern-soft desc))))
535 (if (commandp (intern-soft desc)) (setq bind desc))
536 (setq desc (format "<<%s>>" desc))
537 (setq bind-len (1+ text)))
539 (setq desc (mapconcat
540 (function
541 (lambda (ch)
542 (cond
543 ((integerp ch)
544 (concat
545 (cl-loop for pf across "ACHMsS"
546 for bit in '(?\A-\^@ ?\C-\^@ ?\H-\^@
547 ?\M-\^@ ?\s-\^@ ?\S-\^@)
548 when (/= (logand ch bit) 0)
549 concat (format "%c-" pf))
550 (let ((ch2 (logand ch (1- (lsh 1 18)))))
551 (cond ((<= ch2 32)
552 (pcase ch2
553 (0 "NUL") (9 "TAB") (10 "LFD")
554 (13 "RET") (27 "ESC") (32 "SPC")
556 (format "C-%c"
557 (+ (if (<= ch2 26) 96 64)
558 ch2)))))
559 ((= ch2 127) "DEL")
560 ((<= ch2 maxkey) (char-to-string ch2))
561 (t (format "\\%o" ch2))))))
562 ((symbolp ch)
563 (format "<%s>" ch))
565 (error "Unrecognized item in macro: %s" ch)))))
566 (or fkey key) " "))))
567 (if prefix
568 (setq desc (concat (edmacro-sanitize-for-string prefix) desc)))
569 (unless (string-match " " desc)
570 (let ((times 1) (pos bind-len))
571 (while (not (cl-mismatch rest-mac rest-mac
572 :start1 0 :end1 bind-len
573 :start2 pos :end2 (+ bind-len pos)))
574 (cl-incf times)
575 (cl-incf pos bind-len))
576 (when (> times 1)
577 (setq desc (format "%d*%s" times desc))
578 (setq bind-len (* bind-len times)))))
579 (setq rest-mac (cl-subseq rest-mac bind-len))
580 (if verbose
581 (progn
582 (unless (equal res "") (cl-callf concat res "\n"))
583 (cl-callf concat res desc)
584 (when (and bind (or (stringp bind) (symbolp bind)))
585 (cl-callf concat res
586 (make-string (max (- 3 (/ (length desc) 8)) 1) 9)
587 ";; " (if (stringp bind) bind (symbol-name bind))))
588 (setq len 0))
589 (if (and (> (+ len (length desc) 2) 72) (not one-line))
590 (progn
591 (cl-callf concat res "\n ")
592 (setq len 1))
593 (unless (equal res "")
594 (cl-callf concat res " ")
595 (cl-incf len)))
596 (cl-callf concat res desc)
597 (cl-incf len (length desc)))))
598 res))
600 (defun edmacro-sanitize-for-string (seq)
601 "Convert a key sequence vector SEQ into a string.
602 The string represents the same events; Meta is indicated by bit 7.
603 This function assumes that the events can be stored in a string."
604 (setq seq (copy-sequence seq))
605 (cl-loop for i below (length seq) do
606 (when (logand (aref seq i) 128)
607 (setf (aref seq i) (logand (aref seq i) 127))))
608 seq)
610 (defun edmacro-fix-menu-commands (macro &optional noerror)
611 (if (vectorp macro)
612 (let (result)
613 ;; Make a list of the elements.
614 (setq macro (append macro nil))
615 (dolist (ev macro)
616 (cond ((atom ev)
617 (push ev result))
618 ((eq (car ev) 'help-echo))
619 ((eq (car ev) 'switch-frame))
620 ((equal ev '(menu-bar))
621 (push 'menu-bar result))
622 ((equal (cl-cadadr ev) '(menu-bar))
623 (push (vector 'menu-bar (car ev)) result))
624 ;; It would be nice to do pop-up menus, too, but not enough
625 ;; info is recorded in macros to make this possible.
626 (noerror
627 ;; Just ignore mouse events.
628 nil)
630 (error "Macros with mouse clicks are not %s"
631 "supported by this command"))))
632 ;; Reverse them again and make them back into a vector.
633 (vconcat (nreverse result)))
634 macro))
636 ;;; Parsing a human-readable keyboard macro.
638 (defun edmacro-parse-keys (string &optional need-vector)
639 (let ((case-fold-search nil)
640 (len (length string)) ; We won't alter string in the loop below.
641 (pos 0)
642 (res []))
643 (while (and (< pos len)
644 (string-match "[^ \t\n\f]+" string pos))
645 (let* ((word-beg (match-beginning 0))
646 (word-end (match-end 0))
647 (word (substring string word-beg len))
648 (times 1)
649 key)
650 ;; Try to catch events of the form "<as df>".
651 (if (string-match "\\`<[^ <>\t\n\f][^>\t\n\f]*>" word)
652 (setq word (match-string 0 word)
653 pos (+ word-beg (match-end 0)))
654 (setq word (substring string word-beg word-end)
655 pos word-end))
656 (when (string-match "\\([0-9]+\\)\\*." word)
657 (setq times (string-to-number (substring word 0 (match-end 1))))
658 (setq word (substring word (1+ (match-end 1)))))
659 (cond ((string-match "^<<.+>>$" word)
660 (setq key (vconcat (if (eq (key-binding [?\M-x])
661 'execute-extended-command)
662 [?\M-x]
663 (or (car (where-is-internal
664 'execute-extended-command))
665 [?\M-x]))
666 (substring word 2 -2) "\r")))
667 ((and (string-match "^\\(\\([ACHMsS]-\\)*\\)<\\(.+\\)>$" word)
668 (progn
669 (setq word (concat (substring word (match-beginning 1)
670 (match-end 1))
671 (substring word (match-beginning 3)
672 (match-end 3))))
673 (not (string-match
674 "\\<\\(NUL\\|RET\\|LFD\\|ESC\\|SPC\\|DEL\\)$"
675 word))))
676 (setq key (list (intern word))))
677 ((or (equal word "REM") (string-match "^;;" word))
678 (setq pos (string-match "$" string pos)))
680 (let ((orig-word word) (prefix 0) (bits 0))
681 (while (string-match "^[ACHMsS]-." word)
682 (cl-incf bits (cdr (assq (aref word 0)
683 '((?A . ?\A-\^@) (?C . ?\C-\^@)
684 (?H . ?\H-\^@) (?M . ?\M-\^@)
685 (?s . ?\s-\^@) (?S . ?\S-\^@)))))
686 (cl-incf prefix 2)
687 (cl-callf substring word 2))
688 (when (string-match "^\\^.$" word)
689 (cl-incf bits ?\C-\^@)
690 (cl-incf prefix)
691 (cl-callf substring word 1))
692 (let ((found (assoc word '(("NUL" . "\0") ("RET" . "\r")
693 ("LFD" . "\n") ("TAB" . "\t")
694 ("ESC" . "\e") ("SPC" . " ")
695 ("DEL" . "\177")))))
696 (when found (setq word (cdr found))))
697 (when (string-match "^\\\\[0-7]+$" word)
698 (cl-loop for ch across word
699 for n = 0 then (+ (* n 8) ch -48)
700 finally do (setq word (vector n))))
701 (cond ((= bits 0)
702 (setq key word))
703 ((and (= bits ?\M-\^@) (stringp word)
704 (string-match "^-?[0-9]+$" word))
705 (setq key (cl-loop for x across word
706 collect (+ x bits))))
707 ((/= (length word) 1)
708 (error "%s must prefix a single character, not %s"
709 (substring orig-word 0 prefix) word))
710 ((and (/= (logand bits ?\C-\^@) 0) (stringp word)
711 ;; We used to accept . and ? here,
712 ;; but . is simply wrong,
713 ;; and C-? is not used (we use DEL instead).
714 (string-match "[@-_a-z]" word))
715 (setq key (list (+ bits (- ?\C-\^@)
716 (logand (aref word 0) 31)))))
718 (setq key (list (+ bits (aref word 0)))))))))
719 (when key
720 (cl-loop repeat times do (cl-callf vconcat res key)))))
721 (when (and (>= (length res) 4)
722 (eq (aref res 0) ?\C-x)
723 (eq (aref res 1) ?\()
724 (eq (aref res (- (length res) 2)) ?\C-x)
725 (eq (aref res (- (length res) 1)) ?\)))
726 (setq res (cl-subseq res 2 -2)))
727 (if (and (not need-vector)
728 (cl-loop for ch across res
729 always (and (characterp ch)
730 (let ((ch2 (logand ch (lognot ?\M-\^@))))
731 (and (>= ch2 0) (<= ch2 127))))))
732 (concat (cl-loop for ch across res
733 collect (if (= (logand ch ?\M-\^@) 0)
734 ch (+ ch 128))))
735 res)))
737 (provide 'edmacro)
739 ;;; edmacro.el ends here