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