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