Initial revision
[bpt/emacs.git] / lisp / macros.el
CommitLineData
a2535589
JA
1;; Non-primitive commands for keyboard macros.
2;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
3
4;; This file is part of GNU Emacs.
5
6;; GNU Emacs is free software; you can redistribute it and/or modify
7;; it under the terms of the GNU General Public License as published by
8;; the Free Software Foundation; either version 1, or (at your option)
9;; any later version.
10
11;; GNU Emacs is distributed in the hope that it will be useful,
12;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;; GNU General Public License for more details.
15
16;; You should have received a copy of the GNU General Public License
17;; along with GNU Emacs; see the file COPYING. If not, write to
18;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19
20
21(defun name-last-kbd-macro (symbol)
22 "Assign a name to the last keyboard macro defined.
23One arg, a symbol, which is the name to define.
24The symbol's function definition becomes the keyboard macro string.
25Such a \"function\" cannot be called from Lisp, but it is a valid command
26definition for the editor command loop."
27 (interactive "SName for last kbd macro: ")
28 (or last-kbd-macro
29 (error "No keyboard macro defined"))
30 (and (fboundp symbol)
31 (not (stringp (symbol-function symbol)))
32 (error "Function %s is already defined and not a keyboard macro."
33 symbol))
34 (fset symbol last-kbd-macro))
35
36(defun insert-kbd-macro (macroname &optional keys)
37 "Insert in buffer the definition of kbd macro NAME, as Lisp code.
38Second argument KEYS non-nil means also record the keys it is on.
39 (This is the prefix argument, when calling interactively.)
40
41This Lisp code will, when executed, define the kbd macro with the
42same definition it has now. If you say to record the keys,
43the Lisp code will also rebind those keys to the macro.
44Only global key bindings are recorded since executing this Lisp code
45always makes global bindings.
46
47To save a kbd macro, visit a file of Lisp code such as your ~/.emacs,
48use this command, and then save the file."
49 (interactive "CInsert kbd macro (name): \nP")
50 (insert "(fset '")
51 (prin1 macroname (current-buffer))
52 (insert "\n ")
53 (prin1 (symbol-function macroname) (current-buffer))
54 (insert ")\n")
55 (if keys
56 (let ((keys (where-is-internal macroname nil)))
57 (while keys
58 (insert "(global-set-key ")
59 (prin1 (car keys) (current-buffer))
60 (insert " '")
61 (prin1 macroname (current-buffer))
62 (insert ")\n")
63 (setq keys (cdr keys))))))
64
65(defun kbd-macro-query (flag)
66 "Query user during kbd macro execution.
67With prefix argument, enters recursive edit,
68 reading keyboard commands even within a kbd macro.
69 You can give different commands each time the macro executes.
70Without prefix argument, reads a character. Your options are:
71 Space -- execute the rest of the macro.
72 DEL -- skip the rest of the macro; start next repetition.
73 C-d -- skip rest of the macro and don't repeat it any more.
74 C-r -- enter a recursive edit, then on exit ask again for a character
75 C-l -- redisplay screen and ask again."
76 (interactive "P")
77 (or executing-macro
78 defining-kbd-macro
79 (error "Not defining or executing kbd macro"))
80 (if flag
81 (let (executing-macro defining-kbd-macro)
82 (recursive-edit))
83 (if (not executing-macro)
84 nil
85 (let ((loop t))
86 (while loop
87 (let ((char (let ((executing-macro nil)
88 (defining-kbd-macro nil))
89 (message "Proceed with macro? (Space, DEL, C-d, C-r or C-l) ")
90 (read-char))))
91 (cond ((= char ? )
92 (setq loop nil))
93 ((= char ?\177)
94 (setq loop nil)
95 (setq executing-macro ""))
96 ((= char ?\C-d)
97 (setq loop nil)
98 (setq executing-macro t))
99 ((= char ?\C-l)
100 (recenter nil))
101 ((= char ?\C-r)
102 (let (executing-macro defining-kbd-macro)
103 (recursive-edit))))))))))