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