22
|
1 ;;; edmacro.el --- keyboard macro editor
|
|
2
|
|
3 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
|
24
|
6 ;; Hrvoje Niksic <hniksic@srce.hr> -- XEmacs port
|
|
7 ;; Maintainer: Hrvoje Niksic <hniksic@srce.hr>
|
151
|
8 ;; Version: 3.14
|
22
|
9 ;; Keywords: abbrev
|
|
10
|
24
|
11 ;; This file is part of XEmacs.
|
22
|
12
|
24
|
13 ;; XEmacs is free software; you can redistribute it and/or modify
|
22
|
14 ;; it under the terms of the GNU General Public License as published by
|
|
15 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
16 ;; any later version.
|
|
17
|
24
|
18 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
21 ;; General Public License for more details.
|
22
|
22
|
|
23 ;; You should have received a copy of the GNU General Public License
|
24
|
24 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
25 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
26 ;; 02111-1307, USA.
|
|
27
|
|
28 ;;; Synched up with: FSF 19.34.
|
136
|
29 ;;; The important parts of this file have been rewritten for XEmacs,
|
|
30 ;;; so it's completely different from the FSF version. The original
|
|
31 ;;; could not be used because it worked with the Emacs key
|
|
32 ;;; representation, and it mixed characters and integers too freely.
|
22
|
33
|
|
34 ;;; Commentary:
|
|
35
|
|
36 ;;; Usage:
|
|
37 ;;
|
|
38 ;; The `C-x C-k' (`edit-kbd-macro') command edits a keyboard macro
|
|
39 ;; in a special buffer. It prompts you to type a key sequence,
|
|
40 ;; which should be one of:
|
|
41 ;;
|
|
42 ;; * RET or `C-x e' (call-last-kbd-macro), to edit the most
|
|
43 ;; recently defined keyboard macro.
|
|
44 ;;
|
|
45 ;; * `M-x' followed by a command name, to edit a named command
|
|
46 ;; whose definition is a keyboard macro.
|
|
47 ;;
|
|
48 ;; * `C-h l' (view-lossage), to edit the 100 most recent keystrokes
|
|
49 ;; and install them as the "current" macro.
|
|
50 ;;
|
|
51 ;; * any key sequence whose definition is a keyboard macro.
|
|
52 ;;
|
|
53 ;; This file includes a version of `insert-kbd-macro' that uses the
|
|
54 ;; more readable format defined by these routines.
|
|
55 ;;
|
|
56 ;; Also, the `read-kbd-macro' command parses the region as
|
|
57 ;; a keyboard macro, and installs it as the "current" macro.
|
|
58 ;; This and `format-kbd-macro' can also be called directly as
|
|
59 ;; Lisp functions.
|
|
60
|
136
|
61 ;; The `kbd' macro is a shorter-named and more efficient form of
|
|
62 ;; `read-kbd-macro'. Unlike `read-kbd-macro', it is evaluated at
|
|
63 ;; read-time, and doesn't bring any overhead to compiled programs. It
|
|
64 ;; is recommended to use in your programs and initializations, as you
|
|
65 ;; needn't know the internal keysym representation. For example:
|
118
|
66 ;;
|
|
67 ;; (define-key foo-mode-map (kbd "C-c <up>") 'foo-up)
|
134
|
68 ;;
|
136
|
69 ;; is the exact equivalent of
|
134
|
70 ;;
|
118
|
71 ;; (define-key foo-mode-map [(control ?c) up] 'foo-up)
|
134
|
72 ;;
|
118
|
73
|
22
|
74 ;; Type `C-h m', or see the documentation for `edmacro-mode' below,
|
|
75 ;; for information about the format of written keyboard macros.
|
|
76
|
|
77 ;; `edit-kbd-macro' formats the macro with one command per line,
|
|
78 ;; including the command names as comments on the right. If the
|
|
79 ;; formatter gets confused about which keymap was used for the
|
|
80 ;; characters, the command-name comments will be wrong but that
|
|
81 ;; won't hurt anything.
|
|
82
|
|
83 ;; With a prefix argument, `edit-kbd-macro' will format the
|
|
84 ;; macro in a more concise way that omits the comments.
|
|
85
|
|
86 ;; This package requires GNU Emacs 19 or later, and daveg's CL
|
|
87 ;; package 2.02 or later. (CL 2.02 comes standard starting with
|
|
88 ;; Emacs 19.18.) This package does not work with Emacs 18 or
|
|
89 ;; Lucid Emacs.
|
|
90
|
151
|
91 ;; Ported to XEmacs. This code will not run on GNU Emacs 19. -hniksic
|
22
|
92
|
|
93 ;;; Code:
|
|
94
|
|
95 (eval-when-compile
|
24
|
96 (require 'cl))
|
22
|
97
|
151
|
98 (defgroup edmacro nil
|
|
99 "Keyboard macro editor."
|
|
100 :group 'keyboard)
|
|
101
|
|
102 (defcustom edmacro-eight-bits nil
|
|
103 "*Non-nil if edit-kbd-macro should leave 8-bit characters intact.
|
|
104 Default nil means to write characters above \\177 in octal notation."
|
|
105 :type 'boolean
|
|
106 :group 'edmacro)
|
|
107
|
|
108 (defcustom edmacro-format-hook nil
|
|
109 "*Hook run after formatting the keyboard macro."
|
|
110 :type 'hook
|
|
111 :group 'edmacro)
|
|
112
|
|
113 (defvar edmacro-finish-hook nil)
|
|
114 (defvar edmacro-store-hook nil)
|
|
115 (defvar edmacro-original-buffer nil)
|
|
116
|
22
|
117 ;;; The user-level commands for editing macros.
|
|
118
|
|
119 ;;;###autoload (define-key ctl-x-map "\C-k" 'edit-kbd-macro)
|
|
120
|
|
121 (defvar edmacro-mode-map nil)
|
|
122 (unless edmacro-mode-map
|
|
123 (setq edmacro-mode-map (make-sparse-keymap))
|
|
124 (define-key edmacro-mode-map "\C-c\C-c" 'edmacro-finish-edit)
|
|
125 (define-key edmacro-mode-map "\C-c\C-q" 'edmacro-insert-key))
|
|
126
|
|
127 ;;;###autoload
|
|
128 (defun edit-kbd-macro (keys &optional prefix finish-hook store-hook)
|
|
129 "Edit a keyboard macro.
|
|
130 At the prompt, type any key sequence which is bound to a keyboard macro.
|
|
131 Or, type `C-x e' or RET to edit the last keyboard macro, `C-h l' to edit
|
|
132 the last 100 keystrokes as a keyboard macro, or `M-x' to edit a macro by
|
|
133 its command name.
|
|
134 With a prefix argument, format the macro in a more concise way."
|
|
135 (interactive "kKeyboard macro to edit (C-x e, M-x, C-h l, or keys): \nP")
|
151
|
136 (when (vectorp keys)
|
|
137 (setq keys (edmacro-events-to-keys keys)))
|
|
138 (let ((cmd (if (symbolp keys) keys (key-binding keys)))
|
|
139 (mac nil))
|
|
140 (cond (store-hook
|
|
141 (setq mac keys)
|
|
142 (setq cmd nil))
|
|
143 ((or (eq cmd 'call-last-kbd-macro)
|
|
144 (and (arrayp keys)
|
|
145 (= 1 (length keys))
|
|
146 (or (eq 'return (aref keys 0))
|
|
147 (eq ?\r (aref keys 0))
|
|
148 (equal '(control ?m) (aref keys 0)))))
|
|
149 (or last-kbd-macro
|
|
150 (y-or-n-p "No keyboard macro defined. Create one? ")
|
|
151 (keyboard-quit))
|
|
152 (setq mac (or last-kbd-macro []))
|
|
153 (setq cmd 'last-kbd-macro))
|
|
154 ((eq cmd 'execute-extended-command)
|
|
155 (setq cmd (edmacro-minibuf-read "Name of keyboard macro to edit: "))
|
|
156 (if (string-equal cmd "")
|
|
157 (error "No command name given"))
|
|
158 (setq mac (symbol-function cmd)))
|
|
159 ((eq cmd 'view-lossage)
|
|
160 (setq mac (recent-keys))
|
|
161 (setq cmd 'last-kbd-macro))
|
|
162 ((null cmd)
|
|
163 (error "Key sequence `%s' is not defined" (key-description keys)))
|
|
164 ((symbolp cmd)
|
|
165 (setq mac (symbol-function cmd)))
|
|
166 (t
|
|
167 (setq mac cmd)
|
|
168 (setq cmd nil)))
|
|
169 (unless (arrayp mac)
|
|
170 (error "Key sequence `%s' is not a keyboard macro"
|
|
171 (key-description keys)))
|
|
172 (message "Formatting keyboard macro...")
|
|
173 (let ((oldbuf (current-buffer))
|
|
174 (fmt (edmacro-format-keys mac))
|
|
175 (fmtv (edmacro-format-keys mac (not prefix)))
|
|
176 (buf (get-buffer-create "*Edit Macro*")))
|
|
177 (message "Formatting keyboard macro...done")
|
|
178 (switch-to-buffer buf)
|
|
179 (kill-all-local-variables)
|
|
180 (use-local-map edmacro-mode-map)
|
|
181 (setq buffer-read-only nil)
|
|
182 (setq major-mode 'edmacro-mode)
|
|
183 (setq mode-name "Edit Macro")
|
|
184 (set (make-local-variable 'edmacro-original-buffer) oldbuf)
|
|
185 (set (make-local-variable 'edmacro-finish-hook) finish-hook)
|
|
186 (set (make-local-variable 'edmacro-store-hook) store-hook)
|
|
187 (erase-buffer)
|
|
188 (insert ";; Keyboard Macro Editor. Press C-c C-c to finish; "
|
|
189 "press C-x k RET to cancel.\n")
|
|
190 (insert ";; Original keys: " fmt "\n")
|
|
191 (unless store-hook
|
|
192 (insert "\nCommand: " (if cmd (symbol-name cmd) "none") "\n")
|
|
193 (let ((keys (where-is-internal (or cmd mac))))
|
|
194 (if keys
|
|
195 (insert "Key: " (edmacro-format-keys (car keys)) "\n")
|
|
196 (insert "Key: none\n"))))
|
|
197 (insert "\nMacro:\n\n")
|
|
198 (save-excursion
|
|
199 (insert fmtv "\n"))
|
|
200 (recenter '(4))
|
|
201 (run-hooks 'edmacro-format-hook))))
|
22
|
202
|
|
203 ;;; The next two commands are provided for convenience and backward
|
|
204 ;;; compatibility.
|
|
205
|
|
206 ;;;###autoload
|
|
207 (defun edit-last-kbd-macro (&optional prefix)
|
|
208 "Edit the most recently defined keyboard macro."
|
|
209 (interactive "P")
|
|
210 (edit-kbd-macro 'call-last-kbd-macro prefix))
|
|
211
|
|
212 ;;;###autoload
|
|
213 (defun edit-named-kbd-macro (&optional prefix)
|
|
214 "Edit a keyboard macro which has been given a name by `name-last-kbd-macro'."
|
|
215 (interactive "P")
|
|
216 (edit-kbd-macro 'execute-extended-command prefix))
|
|
217
|
|
218 ;;;###autoload
|
|
219 (defun read-kbd-macro (start &optional end)
|
|
220 "Read the region as a keyboard macro definition.
|
|
221 The region is interpreted as spelled-out keystrokes, e.g., \"M-x abc RET\".
|
|
222 See documentation for `edmacro-mode' for details.
|
|
223 Leading/trailing \"C-x (\" and \"C-x )\" in the text are allowed and ignored.
|
|
224 The resulting macro is installed as the \"current\" keyboard macro.
|
|
225
|
|
226 In Lisp, may also be called with a single STRING argument in which case
|
|
227 the result is returned rather than being installed as the current macro.
|
|
228 The result will be a string if possible, otherwise an event vector.
|
|
229 Second argument NEED-VECTOR means to return an event vector always."
|
|
230 (interactive "r")
|
|
231 (if (stringp start)
|
151
|
232 (edmacro-parse-keys start)
|
22
|
233 (setq last-kbd-macro (edmacro-parse-keys (buffer-substring start end)))))
|
|
234
|
|
235 ;;;###autoload
|
136
|
236 (defmacro kbd (keys)
|
118
|
237 "Convert KEYS to the internal Emacs key representation."
|
134
|
238 (read-kbd-macro keys))
|
118
|
239
|
|
240 ;;;###autoload
|
22
|
241 (defun format-kbd-macro (&optional macro verbose)
|
|
242 "Return the keyboard macro MACRO as a human-readable string.
|
|
243 This string is suitable for passing to `read-kbd-macro'.
|
|
244 Second argument VERBOSE means to put one command per line with comments.
|
|
245 If VERBOSE is `1', put everything on one line. If VERBOSE is omitted
|
|
246 or nil, use a compact 80-column format."
|
|
247 (and macro (symbolp macro) (setq macro (symbol-function macro)))
|
|
248 (edmacro-format-keys (or macro last-kbd-macro) verbose))
|
151
|
249
|
22
|
250
|
|
251 ;;; Commands for *Edit Macro* buffer.
|
|
252
|
|
253 (defun edmacro-finish-edit ()
|
|
254 (interactive)
|
|
255 (unless (eq major-mode 'edmacro-mode)
|
|
256 (error
|
|
257 "This command is valid only in buffers created by `edit-kbd-macro'"))
|
|
258 (run-hooks 'edmacro-finish-hook)
|
|
259 (let ((cmd nil) (keys nil) (no-keys nil)
|
|
260 (top (point-min)))
|
|
261 (goto-char top)
|
|
262 (let ((case-fold-search nil))
|
|
263 (while (cond ((looking-at "[ \t]*\\($\\|;;\\|REM[ \t\n]\\)")
|
|
264 t)
|
|
265 ((looking-at "Command:[ \t]*\\([^ \t\n]*\\)[ \t]*$")
|
|
266 (when edmacro-store-hook
|
|
267 (error "\"Command\" line not allowed in this context"))
|
|
268 (let ((str (buffer-substring (match-beginning 1)
|
|
269 (match-end 1))))
|
|
270 (unless (equal str "")
|
|
271 (setq cmd (and (not (equal str "none"))
|
|
272 (intern str)))
|
|
273 (and (fboundp cmd) (not (arrayp (symbol-function cmd)))
|
|
274 (not (y-or-n-p
|
|
275 (format "Command %s is already defined; %s"
|
|
276 cmd "proceed? ")))
|
|
277 (keyboard-quit))))
|
|
278 t)
|
|
279 ((looking-at "Key:\\(.*\\)$")
|
|
280 (when edmacro-store-hook
|
|
281 (error "\"Key\" line not allowed in this context"))
|
|
282 (let ((key (edmacro-parse-keys
|
|
283 (buffer-substring (match-beginning 1)
|
|
284 (match-end 1)))))
|
24
|
285 (unless (equal key [])
|
|
286 (if (equal key [?n ?o ?n ?e])
|
22
|
287 (setq no-keys t)
|
|
288 (push key keys)
|
|
289 (let ((b (key-binding key)))
|
|
290 (and b (commandp b) (not (arrayp b))
|
|
291 (or (not (fboundp b))
|
|
292 (not (arrayp (symbol-function b))))
|
|
293 (not (y-or-n-p
|
151
|
294 (format
|
|
295 "Key `%s' is already defined; %s"
|
|
296 (edmacro-format-keys key)
|
|
297 "proceed? ")))
|
22
|
298 (keyboard-quit))))))
|
|
299 t)
|
|
300 ((looking-at "Macro:[ \t\n]*")
|
|
301 (goto-char (match-end 0))
|
|
302 nil)
|
|
303 ((eobp) nil)
|
|
304 (t (error "Expected a `Macro:' line")))
|
|
305 (forward-line 1))
|
|
306 (setq top (point)))
|
|
307 (let* ((buf (current-buffer))
|
|
308 (str (buffer-substring top (point-max)))
|
|
309 (modp (buffer-modified-p))
|
|
310 (obuf edmacro-original-buffer)
|
151
|
311 (store-hook edmacro-store-hook))
|
22
|
312 (unless (or cmd keys store-hook (equal str ""))
|
|
313 (error "No command name or keys specified"))
|
|
314 (when modp
|
|
315 (when (buffer-name obuf)
|
|
316 (set-buffer obuf))
|
|
317 (message "Compiling keyboard macro...")
|
|
318 (let ((mac (edmacro-parse-keys str)))
|
|
319 (message "Compiling keyboard macro...done")
|
|
320 (if store-hook
|
|
321 (funcall store-hook mac)
|
|
322 (when (eq cmd 'last-kbd-macro)
|
|
323 (setq last-kbd-macro (and (> (length mac) 0) mac))
|
|
324 (setq cmd nil))
|
|
325 (when cmd
|
|
326 (if (= (length mac) 0)
|
|
327 (fmakunbound cmd)
|
|
328 (fset cmd mac)))
|
|
329 (if no-keys
|
|
330 (when cmd
|
24
|
331 (loop for key in (where-is-internal cmd) do
|
22
|
332 (global-unset-key key)))
|
|
333 (when keys
|
|
334 (if (= (length mac) 0)
|
|
335 (loop for key in keys do (global-unset-key key))
|
|
336 (loop for key in keys do
|
|
337 (global-set-key key (or cmd mac)))))))))
|
|
338 (kill-buffer buf)
|
|
339 (when (buffer-name obuf)
|
151
|
340 (switch-to-buffer obuf)))))
|
22
|
341
|
|
342 (defun edmacro-insert-key (key)
|
|
343 "Insert the written name of a key in the buffer."
|
|
344 (interactive "kKey to insert: ")
|
|
345 (if (bolp)
|
|
346 (insert (edmacro-format-keys key t) "\n")
|
|
347 (insert (edmacro-format-keys key) " ")))
|
|
348
|
|
349 (defun edmacro-mode ()
|
|
350 "\\<edmacro-mode-map>Keyboard Macro Editing mode. Press
|
|
351 \\[edmacro-finish-edit] to save and exit.
|
|
352 To abort the edit, just kill this buffer with \\[kill-buffer] RET.
|
|
353
|
|
354 Press \\[edmacro-insert-key] to insert the name of any key by typing the key.
|
|
355
|
|
356 The editing buffer contains a \"Command:\" line and any number of
|
|
357 \"Key:\" lines at the top. These are followed by a \"Macro:\" line
|
|
358 and the macro itself as spelled-out keystrokes: `C-x C-f foo RET'.
|
|
359
|
|
360 The \"Command:\" line specifies the command name to which the macro
|
|
361 is bound, or \"none\" for no command name. Write \"last-kbd-macro\"
|
|
362 to refer to the current keyboard macro (as used by \\[call-last-kbd-macro]).
|
|
363
|
|
364 The \"Key:\" lines specify key sequences to which the macro is bound,
|
|
365 or \"none\" for no key bindings.
|
|
366
|
|
367 You can edit these lines to change the places where the new macro
|
|
368 is stored.
|
|
369
|
|
370
|
|
371 Format of keyboard macros during editing:
|
|
372
|
|
373 Text is divided into \"words\" separated by whitespace. Except for
|
|
374 the words described below, the characters of each word go directly
|
|
375 as characters of the macro. The whitespace that separates words
|
|
376 is ignored. Whitespace in the macro must be written explicitly,
|
|
377 as in \"foo SPC bar RET\".
|
|
378
|
136
|
379 * The special words RET, SPC, TAB, DEL, BS, LFD, ESC, and NUL represent
|
22
|
380 special control characters. The words must be written in uppercase.
|
|
381
|
|
382 * A word in angle brackets, e.g., <return>, <down>, or <f1>, represents
|
|
383 a function key. (Note that in the standard configuration, the
|
|
384 function key <return> and the control key RET are synonymous.)
|
|
385 You can use angle brackets on the words RET, SPC, etc., but they
|
|
386 are not required there.
|
|
387
|
|
388 * Keys can be written by their ASCII code, using a backslash followed
|
|
389 by up to six octal digits. This is the only way to represent keys
|
|
390 with codes above \\377.
|
|
391
|
|
392 * One or more prefixes M- (meta), C- (control), S- (shift), A- (alt),
|
|
393 H- (hyper), and s- (super) may precede a character or key notation.
|
|
394 For function keys, the prefixes may go inside or outside of the
|
|
395 brackets: C-<down> = <C-down>. The prefixes may be written in
|
|
396 any order: M-C-x = C-M-x.
|
|
397
|
|
398 Prefixes are not allowed on multi-key words, e.g., C-abc, except
|
|
399 that the Meta prefix is allowed on a sequence of digits and optional
|
|
400 minus sign: M--123 = M-- M-1 M-2 M-3.
|
|
401
|
|
402 * The `^' notation for control characters also works: ^M = C-m.
|
|
403
|
|
404 * Double angle brackets enclose command names: <<next-line>> is
|
|
405 shorthand for M-x next-line RET.
|
|
406
|
|
407 * Finally, REM or ;; causes the rest of the line to be ignored as a
|
|
408 comment.
|
|
409
|
|
410 Any word may be prefixed by a multiplier in the form of a decimal
|
|
411 number and `*': 3*<right> = <right> <right> <right>, and
|
|
412 10*foo = foofoofoofoofoofoofoofoofoofoo.
|
|
413
|
|
414 Multiple text keys can normally be strung together to form a word,
|
|
415 but you may need to add whitespace if the word would look like one
|
|
416 of the above notations: `; ; ;' is a keyboard macro with three
|
|
417 semicolons, but `;;;' is a comment. Likewise, `\\ 1 2 3' is four
|
|
418 keys but `\\123' is a single key written in octal, and `< right >'
|
|
419 is seven keys but `<right>' is a single function key. When in
|
|
420 doubt, use whitespace."
|
|
421 (interactive)
|
|
422 (error "This mode can be enabled only by `edit-kbd-macro'"))
|
|
423 (put 'edmacro-mode 'mode-class 'special)
|
151
|
424
|
22
|
425
|
|
426 (defun edmacro-int-char (int)
|
151
|
427 (if (fboundp 'int-char)
|
|
428 (int-char int)
|
22
|
429 int))
|
|
430
|
151
|
431 (defvar edmacro-read-history nil)
|
|
432
|
|
433 ;; Completing read on named keyboard macros only.
|
|
434 (defun edmacro-minibuf-read (prompt)
|
|
435 (intern (completing-read
|
|
436 prompt obarray
|
|
437 (lambda (arg)
|
|
438 (and (commandp arg)
|
|
439 (vectorp (symbol-function arg))))
|
|
440 t nil 'edmacro-read-history)))
|
|
441
|
136
|
442
|
151
|
443 (defvar edmacro-char-to-word
|
|
444 '((?\0 . "NUL")
|
|
445 (?\r . "RET")
|
|
446 (?\n . "LFD")
|
|
447 (?\t . "TAB")
|
|
448 (?\e . "ESC")
|
|
449 (?\ . "SPC")
|
|
450 (?\C-? . "DEL")))
|
|
451
|
|
452 (defvar edmacro-modifiers
|
|
453 '(("C" . control)
|
|
454 ("M" . meta)
|
|
455 ("S" . shift)
|
|
456 ("Sh" . shift)
|
|
457 ("A" . alt)
|
|
458 ("H" . hyper)
|
|
459 ("s" . super)))
|
|
460
|
136
|
461 ;;; Parsing a human-readable keyboard macro.
|
22
|
462
|
|
463 ;; Changes for XEmacs -- these two functions re-written from scratch.
|
|
464 ;; edmacro-parse-keys always returns a vector. edmacro-format-keys
|
|
465 ;; accepts a vector (but works with a string too). Vector may contain
|
|
466 ;; keypress events. -hniksic
|
151
|
467 (defun edmacro-parse-keys (string)
|
134
|
468 (let* ((pos 0)
|
|
469 (case-fold-search nil)
|
|
470 res)
|
22
|
471 (while (and (< pos (length string))
|
151
|
472 (string-match "[^ \t\r\n\f]+" string pos))
|
|
473 (let ((word (substring string (match-beginning 0) (match-end 0))))
|
22
|
474 (setq pos (match-end 0))
|
151
|
475 (if (or (equal word "REM") (string-match "^;;" word))
|
|
476 ;; Comment (discard to EOL) .
|
|
477 (setq pos (string-match "$" string pos))
|
|
478 (push (edmacro-parse-word word) res))))
|
|
479 (mapvector 'identity (apply 'nconc (nreverse res)))))
|
22
|
480
|
151
|
481 ;; Parse a word.
|
|
482 (defun edmacro-parse-word (word)
|
|
483 (let ((force-sym nil)
|
|
484 (times 1)
|
|
485 abbr)
|
|
486 (when (string-match "\\([0-9]+\\)\\*." word)
|
|
487 (setq times (string-to-int (substring word 0 (match-end 1))))
|
|
488 (setq word (substring word (1+ (match-end 1)))))
|
|
489 (when (string-match "^<\\([^<>]+\\)>$" word)
|
|
490 (setq word (match-string 1 word))
|
|
491 (setq force-sym t))
|
|
492 (let* ((word-to-sym '(("NUL" . ?\0)
|
|
493 ("RET" . return)
|
|
494 ("LFD" . linefeed)
|
|
495 ("TAB" . tab)
|
|
496 ("ESC" . escape)
|
|
497 ("SPC" . space)
|
|
498 ("BS" . backspace)
|
|
499 ("DEL" . delete)))
|
|
500 (conv (lambda (arg)
|
|
501 ;; string-to-symbol-or-char converter
|
|
502 (if (= (length arg) 1)
|
|
503 (aref arg 0)
|
|
504 (if (string-match "^<\\([^>]+\\)>$" arg)
|
|
505 (setq arg (match-string 1 arg)))
|
|
506 (let ((match (assoc arg word-to-sym)))
|
|
507 (if match
|
|
508 (cdr match)
|
|
509 (intern arg))))))
|
|
510 (conv-chars (lambda (arg)
|
|
511 (let ((match (assoc arg edmacro-char-to-word)))
|
|
512 (if match
|
|
513 (cdr (assoc (cdr match) word-to-sym))
|
|
514 arg))))
|
|
515 (add
|
|
516 (cond
|
|
517 ((string-match "^\\\\[0-7]+" word)
|
|
518 ;; Octal value of character.
|
|
519 (list
|
|
520 (edmacro-int-char
|
|
521 (hexl-octal-string-to-integer (substring word 1)))))
|
|
522 ((string-match "^<<.+>>$" word)
|
|
523 ;; Extended command.
|
|
524 (nconc
|
|
525 (list
|
|
526 (if (eq (key-binding [(meta x)])
|
|
527 'execute-extended-command)
|
|
528 '(meta x)
|
|
529 (or (car (where-is-internal
|
|
530 'execute-extended-command))
|
|
531 '(meta x))))
|
|
532 (mapcar conv-chars (concat (substring word 2 -2) "\r"))))
|
|
533 ((setq abbr (assoc word word-to-sym))
|
|
534 ;; Convert to symbol.
|
|
535 (list (cdr abbr)))
|
|
536 ((string-match "^\\^" word)
|
|
537 ;; ^X == C-x
|
|
538 (if (/= (length word) 2)
|
|
539 (error "^ must be followed by one character"))
|
|
540 `((control ,(aref word 1))))
|
|
541 ((string-match "^\\([MCSsAH]\\|Sh\\)-" word)
|
|
542 ;; Parse C-* and stuff
|
|
543 (list
|
|
544 (let ((pos1 0)
|
|
545 (r1 nil)
|
|
546 follow curpart prefix)
|
|
547 (while (progn (setq curpart (substring word pos1))
|
|
548 (string-match "^\\([MCSsAH]\\|Sh\\)-"
|
|
549 curpart))
|
|
550 (setq prefix (assoc (match-string 1 curpart)
|
|
551 edmacro-modifiers))
|
|
552 (push (cdr prefix) r1)
|
|
553 (incf pos1 (1+ (length (car prefix)))))
|
|
554 (setq follow (substring word pos1))
|
|
555 (if (equal follow "")
|
|
556 (error "%s must precede a string"
|
|
557 (substring word 0 pos1)))
|
|
558 (nconc (nreverse r1) (list (funcall conv follow))))))
|
|
559 (force-sym
|
|
560 ;; This must be a symbol
|
|
561 (list (intern word)))
|
|
562 (t
|
|
563 ;; Characters
|
|
564 (mapcar conv-chars word))))
|
|
565 (new nil))
|
|
566 (loop repeat times do (setq new (append add new)))
|
|
567 new)))
|
|
568
|
|
569 ;; Convert the keypress events in vector x to keys, and return a
|
|
570 ;; vector of keys. If a list element is not a keypress event, ignore
|
|
571 ;; it.
|
|
572 (defun edmacro-events-to-keys (x &optional list)
|
|
573 (let (new)
|
|
574 (mapc (lambda (el)
|
|
575 (cond ((key-press-event-p el)
|
|
576 (push (let ((mods (event-modifiers el)))
|
|
577 (if mods
|
|
578 (append mods (list (event-key el)))
|
|
579 (event-key el)))
|
|
580 new))
|
|
581 ((or (characterp el)
|
|
582 (symbolp el)
|
|
583 (listp el))
|
|
584 (push el new))))
|
|
585 x)
|
|
586 (setq new (nreverse new))
|
|
587 (if list
|
|
588 new
|
|
589 (mapvector 'identity new))))
|
|
590
|
|
591 ;; Collapse a list of keys into a list of function keys, where
|
|
592 ;; applicable.
|
|
593 (defun edmacro-fkeys (keys)
|
|
594 (let (new k lookup)
|
|
595 (while keys
|
|
596 (setq k (nconc k (list (car keys))))
|
|
597 (setq lookup (lookup-key function-key-map (mapvector 'identity k)))
|
|
598 (cond ((vectorp lookup)
|
|
599 (push (mapcar 'identity lookup) new)
|
|
600 (setq k nil))
|
|
601 ((keymapp lookup)
|
|
602 nil)
|
|
603 ((null lookup)
|
|
604 (push k new)
|
|
605 (setq k nil))
|
|
606 (t
|
|
607 (setq k nil)))
|
|
608 (pop keys))
|
|
609 (when (keymapp lookup)
|
|
610 (push k new))
|
|
611 (apply 'nconc (nreverse new))))
|
|
612
|
|
613 ;; Convert a character or symbol to string
|
22
|
614 (defun edmacro-conv (char-or-sym add-<>)
|
|
615 (let ((char-to-word '((?\0 . "NUL")
|
24
|
616 (?\r . "RET")
|
|
617 (?\n . "LFD")
|
|
618 (?\t . "TAB")
|
|
619 (?\e . "ESC")
|
|
620 (?\ . "SPC")
|
|
621 (?\C-? . "DEL")))
|
22
|
622 (symbol-to-char '((return . ?\r)
|
24
|
623 (linefeed . ?\n)
|
22
|
624 (space . ?\ )
|
|
625 (delete . ?\C-?)
|
|
626 (tab . ?\t)
|
|
627 (escape . ?\e))))
|
|
628 (if (symbolp char-or-sym)
|
|
629 (if (= (length (symbol-name char-or-sym)) 1)
|
|
630 (setq char-or-sym (aref (symbol-name char-or-sym) 0))
|
|
631 (let ((found (assq char-or-sym symbol-to-char)))
|
|
632 (if found
|
|
633 (setq char-or-sym (cdr found))))))
|
|
634 ;; Return:
|
|
635 (cons (symbolp char-or-sym)
|
|
636 (if (symbolp char-or-sym)
|
|
637 (if add-<>
|
|
638 (concat "<" (symbol-name char-or-sym) ">")
|
|
639 (symbol-name char-or-sym))
|
|
640 (let ((found (assq char-or-sym char-to-word)))
|
24
|
641 (cond (found
|
|
642 (cdr found))
|
|
643 ((< char-or-sym 128)
|
|
644 (single-key-description char-or-sym))
|
134
|
645 ((and edmacro-eight-bits
|
|
646 (>= char-or-sym 128))
|
|
647 (char-to-string char-or-sym))
|
24
|
648 (t
|
|
649 (format "\\%o" (edmacro-int-char char-or-sym)))))))))
|
22
|
650
|
|
651 (defun edmacro-format-1 (keys command times togetherp)
|
|
652 (let ((res "")
|
|
653 (start keys)
|
|
654 el)
|
|
655 (while keys
|
151
|
656 (when (or (eq (car keys) ?-)
|
|
657 (eq (car keys) '-)
|
|
658 (not (or togetherp (eq start keys))))
|
22
|
659 (callf concat res " "))
|
|
660 (if (> times 1)
|
|
661 (setq res (concat (format "%d*" times) res)))
|
|
662 (setq el (car keys))
|
|
663 (callf concat res
|
|
664 (cond ((listp el)
|
|
665 (let ((my ""))
|
|
666 (if (or
|
|
667 (let (cnv)
|
|
668 (while el
|
151
|
669 (let ((found (find (car el) edmacro-modifiers
|
|
670 :key 'cdr)))
|
22
|
671 (callf concat my
|
|
672 (if found
|
151
|
673 (concat (car found) "-")
|
22
|
674 (setq cnv (edmacro-conv (car el) nil))
|
|
675 (cdr cnv))))
|
151
|
676 (pop el))
|
22
|
677 (car cnv))
|
|
678 (> times 1))
|
|
679 (concat "<" my ">")
|
|
680 my)))
|
|
681 (t
|
|
682 (cdr (edmacro-conv el t)))))
|
151
|
683 (pop keys))
|
22
|
684 (if command
|
|
685 (callf concat res
|
151
|
686 (make-string (max (- 3 (/ (length res) tab-width)) 1) ?\t)
|
|
687 ";; "
|
|
688 (symbol-name command)
|
|
689 (if togetherp (format " * %d" (length start)))))
|
22
|
690 res))
|
|
691
|
151
|
692 (defsubst edmacro-seq-equal (seq1 seq2)
|
|
693 (while (and seq1 seq2
|
|
694 (equal (car seq1) (car seq2)))
|
|
695 (pop seq1)
|
|
696 (pop seq2))
|
|
697 (not seq1))
|
22
|
698
|
136
|
699 ;;; Formatting a keyboard macro as human-readable text.
|
|
700
|
24
|
701 (defun edmacro-format-keys (macro &optional verbose)
|
|
702 ;; XEmacs:
|
151
|
703 ;; If we're dealing with events, convert them to symbols first;
|
|
704 ;; also, deal with Fkeys.
|
|
705 (setq macro (edmacro-fkeys (edmacro-events-to-keys macro t)))
|
|
706 (let ((res ""))
|
|
707 (while macro
|
|
708 (let (key lookup (times 1) self-insert-p)
|
|
709 (loop
|
|
710 do (setq key (nconc key (list (car macro)))
|
|
711 macro (cdr macro)
|
|
712 lookup (lookup-key global-map (mapvector
|
|
713 'identity key)))
|
|
714 while (and macro lookup (not (commandp lookup))))
|
|
715 ;; keyboard macro
|
|
716 (if (vectorp lookup)
|
|
717 (setq lookup nil))
|
|
718 (if (and (eq lookup 'self-insert-command)
|
|
719 (= (length key) 1)
|
|
720 (not (memq (car key)
|
|
721 '(?\ ?\r ?\n space return linefeed tab))))
|
|
722 (while (and (< (length key) 23)
|
|
723 (eq (lookup-key global-map (car macro))
|
|
724 'self-insert-command)
|
|
725 (not (memq
|
|
726 (car macro)
|
24
|
727 '(?\ ?\r ?\n space return linefeed tab))))
|
151
|
728 (setq key (nconc key (list (car macro)))
|
|
729 macro (cdr macro)
|
|
730 self-insert-p t))
|
|
731 (let ((keysize (length key)))
|
24
|
732 (while (edmacro-seq-equal key macro)
|
151
|
733 (setq macro (nthcdr keysize macro))
|
|
734 (incf times))))
|
|
735 (if (or self-insert-p
|
|
736 (null (cdr key))
|
|
737 (= times 1))
|
|
738 (callf concat res
|
|
739 (edmacro-format-1 key (if verbose lookup
|
|
740 nil)
|
|
741 times self-insert-p)
|
|
742 (and macro (if verbose "\n" " ")))
|
|
743 (loop
|
|
744 repeat times
|
|
745 do
|
|
746 (callf concat res
|
|
747 (edmacro-format-1 key (if verbose lookup
|
|
748 nil)
|
|
749 1 self-insert-p)
|
|
750 (and macro (if verbose "\n" " ")))))))
|
|
751 res))
|
22
|
752
|
|
753
|
|
754 ;;; The following probably ought to go in macros.el:
|
|
755
|
|
756 ;;;###autoload
|
|
757 (defun insert-kbd-macro (macroname &optional keys)
|
|
758 "Insert in buffer the definition of kbd macro NAME, as Lisp code.
|
|
759 Optional second arg KEYS means also record the keys it is on
|
|
760 \(this is the prefix argument, when calling interactively).
|
|
761
|
|
762 This Lisp code will, when executed, define the kbd macro with the same
|
|
763 definition it has now. If you say to record the keys, the Lisp code
|
|
764 will also rebind those keys to the macro. Only global key bindings
|
|
765 are recorded since executing this Lisp code always makes global
|
|
766 bindings.
|
|
767
|
|
768 To save a kbd macro, visit a file of Lisp code such as your `~/.emacs',
|
|
769 use this command, and then save the file."
|
|
770 (interactive "CInsert kbd macro (name): \nP")
|
|
771 (let (definition)
|
|
772 (if (string= (symbol-name macroname) "")
|
|
773 (progn
|
|
774 (setq definition (format-kbd-macro))
|
|
775 (insert "(setq last-kbd-macro"))
|
|
776 (setq definition (format-kbd-macro macroname))
|
|
777 (insert (format "(defalias '%s" macroname)))
|
|
778 (if (> (length definition) 50)
|
|
779 (insert " (read-kbd-macro\n")
|
|
780 (insert "\n (read-kbd-macro "))
|
|
781 (prin1 definition (current-buffer))
|
|
782 (insert "))\n")
|
|
783 (if keys
|
24
|
784 (let ((keys (where-is-internal macroname)))
|
22
|
785 (while keys
|
|
786 (insert (format "(global-set-key %S '%s)\n" (car keys) macroname))
|
151
|
787 (pop keys))))))
|
22
|
788
|
|
789 (provide 'edmacro)
|
|
790
|
|
791 ;;; edmacro.el ends here
|