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