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>
|
173
|
8 ;; Version: 3.17
|
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))))
|
163
|
541 ((string-match "^M--?[0-9]+$" word)
|
|
542 ;; Special case: M- followed by an optional hyphen and
|
|
543 ;; one or more digits
|
|
544 (mapcar (lambda (digit)
|
|
545 (list 'meta digit))
|
|
546 (substring word 2)))
|
151
|
547 ((string-match "^\\([MCSsAH]\\|Sh\\)-" word)
|
|
548 ;; Parse C-* and stuff
|
|
549 (list
|
|
550 (let ((pos1 0)
|
|
551 (r1 nil)
|
|
552 follow curpart prefix)
|
|
553 (while (progn (setq curpart (substring word pos1))
|
|
554 (string-match "^\\([MCSsAH]\\|Sh\\)-"
|
|
555 curpart))
|
|
556 (setq prefix (assoc (match-string 1 curpart)
|
|
557 edmacro-modifiers))
|
|
558 (push (cdr prefix) r1)
|
|
559 (incf pos1 (1+ (length (car prefix)))))
|
|
560 (setq follow (substring word pos1))
|
|
561 (if (equal follow "")
|
|
562 (error "%s must precede a string"
|
|
563 (substring word 0 pos1)))
|
|
564 (nconc (nreverse r1) (list (funcall conv follow))))))
|
|
565 (force-sym
|
|
566 ;; This must be a symbol
|
|
567 (list (intern word)))
|
|
568 (t
|
|
569 ;; Characters
|
|
570 (mapcar conv-chars word))))
|
|
571 (new nil))
|
|
572 (loop repeat times do (setq new (append add new)))
|
|
573 new)))
|
|
574
|
|
575 ;; Convert the keypress events in vector x to keys, and return a
|
|
576 ;; vector of keys. If a list element is not a keypress event, ignore
|
|
577 ;; it.
|
|
578 (defun edmacro-events-to-keys (x &optional list)
|
|
579 (let (new)
|
|
580 (mapc (lambda (el)
|
|
581 (cond ((key-press-event-p el)
|
|
582 (push (let ((mods (event-modifiers el)))
|
|
583 (if mods
|
|
584 (append mods (list (event-key el)))
|
|
585 (event-key el)))
|
|
586 new))
|
|
587 ((or (characterp el)
|
|
588 (symbolp el)
|
|
589 (listp el))
|
|
590 (push el new))))
|
|
591 x)
|
|
592 (setq new (nreverse new))
|
|
593 (if list
|
|
594 new
|
|
595 (mapvector 'identity new))))
|
|
596
|
|
597 ;; Collapse a list of keys into a list of function keys, where
|
|
598 ;; applicable.
|
|
599 (defun edmacro-fkeys (keys)
|
|
600 (let (new k lookup)
|
|
601 (while keys
|
|
602 (setq k (nconc k (list (car keys))))
|
|
603 (setq lookup (lookup-key function-key-map (mapvector 'identity k)))
|
|
604 (cond ((vectorp lookup)
|
|
605 (push (mapcar 'identity lookup) new)
|
|
606 (setq k nil))
|
|
607 ((keymapp lookup)
|
|
608 nil)
|
|
609 ((null lookup)
|
|
610 (push k new)
|
|
611 (setq k nil))
|
|
612 (t
|
|
613 (setq k nil)))
|
|
614 (pop keys))
|
|
615 (when (keymapp lookup)
|
|
616 (push k new))
|
|
617 (apply 'nconc (nreverse new))))
|
|
618
|
|
619 ;; Convert a character or symbol to string
|
22
|
620 (defun edmacro-conv (char-or-sym add-<>)
|
|
621 (let ((char-to-word '((?\0 . "NUL")
|
24
|
622 (?\r . "RET")
|
|
623 (?\n . "LFD")
|
|
624 (?\t . "TAB")
|
|
625 (?\e . "ESC")
|
|
626 (?\ . "SPC")
|
|
627 (?\C-? . "DEL")))
|
22
|
628 (symbol-to-char '((return . ?\r)
|
24
|
629 (linefeed . ?\n)
|
22
|
630 (space . ?\ )
|
|
631 (delete . ?\C-?)
|
|
632 (tab . ?\t)
|
|
633 (escape . ?\e))))
|
|
634 (if (symbolp char-or-sym)
|
|
635 (if (= (length (symbol-name char-or-sym)) 1)
|
|
636 (setq char-or-sym (aref (symbol-name char-or-sym) 0))
|
|
637 (let ((found (assq char-or-sym symbol-to-char)))
|
|
638 (if found
|
|
639 (setq char-or-sym (cdr found))))))
|
|
640 ;; Return:
|
|
641 (cons (symbolp char-or-sym)
|
|
642 (if (symbolp char-or-sym)
|
|
643 (if add-<>
|
|
644 (concat "<" (symbol-name char-or-sym) ">")
|
|
645 (symbol-name char-or-sym))
|
|
646 (let ((found (assq char-or-sym char-to-word)))
|
24
|
647 (cond (found
|
|
648 (cdr found))
|
|
649 ((< char-or-sym 128)
|
|
650 (single-key-description char-or-sym))
|
134
|
651 ((and edmacro-eight-bits
|
|
652 (>= char-or-sym 128))
|
|
653 (char-to-string char-or-sym))
|
24
|
654 (t
|
|
655 (format "\\%o" (edmacro-int-char char-or-sym)))))))))
|
22
|
656
|
|
657 (defun edmacro-format-1 (keys command times togetherp)
|
|
658 (let ((res "")
|
|
659 (start keys)
|
|
660 el)
|
|
661 (while keys
|
151
|
662 (when (or (eq (car keys) ?-)
|
|
663 (eq (car keys) '-)
|
155
|
664 (eq (car keys) ?>)
|
151
|
665 (not (or togetherp (eq start keys))))
|
22
|
666 (callf concat res " "))
|
|
667 (if (> times 1)
|
|
668 (setq res (concat (format "%d*" times) res)))
|
|
669 (setq el (car keys))
|
|
670 (callf concat res
|
|
671 (cond ((listp el)
|
|
672 (let ((my ""))
|
|
673 (if (or
|
|
674 (let (cnv)
|
|
675 (while el
|
151
|
676 (let ((found (find (car el) edmacro-modifiers
|
|
677 :key 'cdr)))
|
22
|
678 (callf concat my
|
|
679 (if found
|
151
|
680 (concat (car found) "-")
|
22
|
681 (setq cnv (edmacro-conv (car el) nil))
|
|
682 (cdr cnv))))
|
151
|
683 (pop el))
|
22
|
684 (car cnv))
|
|
685 (> times 1))
|
|
686 (concat "<" my ">")
|
|
687 my)))
|
|
688 (t
|
|
689 (cdr (edmacro-conv el t)))))
|
151
|
690 (pop keys))
|
22
|
691 (if command
|
|
692 (callf concat res
|
151
|
693 (make-string (max (- 3 (/ (length res) tab-width)) 1) ?\t)
|
|
694 ";; "
|
|
695 (symbol-name command)
|
|
696 (if togetherp (format " * %d" (length start)))))
|
22
|
697 res))
|
|
698
|
151
|
699 (defsubst edmacro-seq-equal (seq1 seq2)
|
|
700 (while (and seq1 seq2
|
|
701 (equal (car seq1) (car seq2)))
|
|
702 (pop seq1)
|
|
703 (pop seq2))
|
|
704 (not seq1))
|
22
|
705
|
136
|
706 ;;; Formatting a keyboard macro as human-readable text.
|
|
707
|
24
|
708 (defun edmacro-format-keys (macro &optional verbose)
|
|
709 ;; XEmacs:
|
151
|
710 ;; If we're dealing with events, convert them to symbols first;
|
|
711 ;; also, deal with Fkeys.
|
|
712 (setq macro (edmacro-fkeys (edmacro-events-to-keys macro t)))
|
|
713 (let ((res ""))
|
|
714 (while macro
|
|
715 (let (key lookup (times 1) self-insert-p)
|
|
716 (loop
|
|
717 do (setq key (nconc key (list (car macro)))
|
|
718 macro (cdr macro)
|
|
719 lookup (lookup-key global-map (mapvector
|
|
720 'identity key)))
|
|
721 while (and macro lookup (not (commandp lookup))))
|
|
722 ;; keyboard macro
|
|
723 (if (vectorp lookup)
|
|
724 (setq lookup nil))
|
|
725 (if (and (eq lookup 'self-insert-command)
|
|
726 (= (length key) 1)
|
|
727 (not (memq (car key)
|
|
728 '(?\ ?\r ?\n space return linefeed tab))))
|
|
729 (while (and (< (length key) 23)
|
|
730 (eq (lookup-key global-map (car macro))
|
|
731 'self-insert-command)
|
|
732 (not (memq
|
|
733 (car macro)
|
24
|
734 '(?\ ?\r ?\n space return linefeed tab))))
|
151
|
735 (setq key (nconc key (list (car macro)))
|
|
736 macro (cdr macro)
|
|
737 self-insert-p t))
|
|
738 (let ((keysize (length key)))
|
24
|
739 (while (edmacro-seq-equal key macro)
|
151
|
740 (setq macro (nthcdr keysize macro))
|
|
741 (incf times))))
|
|
742 (if (or self-insert-p
|
|
743 (null (cdr key))
|
|
744 (= times 1))
|
|
745 (callf concat res
|
|
746 (edmacro-format-1 key (if verbose lookup
|
|
747 nil)
|
|
748 times self-insert-p)
|
|
749 (and macro (if verbose "\n" " ")))
|
|
750 (loop
|
|
751 repeat times
|
|
752 do
|
|
753 (callf concat res
|
|
754 (edmacro-format-1 key (if verbose lookup
|
|
755 nil)
|
|
756 1 self-insert-p)
|
|
757 (and macro (if verbose "\n" " ")))))))
|
|
758 res))
|
22
|
759
|
|
760 (provide 'edmacro)
|
|
761
|
|
762 ;;; edmacro.el ends here
|