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