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