(edit-kbd-macro): Reject empty cmd name.
[bpt/emacs.git] / lisp / macros.el
CommitLineData
6594deb0
ER
1;;; macros.el --- non-primitive commands for keyboard macros.
2
f8c25f1b 3;; Copyright (C) 1985, 86, 87, 92, 94, 95 Free Software Foundation, Inc.
a2535589 4
9750e079 5;; Maintainer: FSF
e9571d2a 6;; Keywords: abbrev
9750e079 7
a2535589
JA
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
e5167999 12;; the Free Software Foundation; either version 2, or (at your option)
a2535589
JA
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
b578f267
EN
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
a2535589 24
e41b2db1
ER
25;;; Commentary:
26
27;; Extension commands for keyboard macros. These permit you to assign
28;; a name to the last-defined keyboard macro, expand and insert the
29;; lisp corresponding to a macro, query the user from within a macro,
30;; or apply a macro to each line in the reason.
31
e5167999 32;;; Code:
a2535589 33
7229064d 34;;;###autoload
a2535589
JA
35(defun name-last-kbd-macro (symbol)
36 "Assign a name to the last keyboard macro defined.
ac5b56bc 37Argument SYMBOL is the name to define.
a2535589 38The symbol's function definition becomes the keyboard macro string.
540671f3 39Such a \"function\" cannot be called from Lisp, but it is a valid editor command."
a2535589
JA
40 (interactive "SName for last kbd macro: ")
41 (or last-kbd-macro
42 (error "No keyboard macro defined"))
43 (and (fboundp symbol)
44 (not (stringp (symbol-function symbol)))
4dc7458e 45 (not (vectorp (symbol-function symbol)))
a2535589
JA
46 (error "Function %s is already defined and not a keyboard macro."
47 symbol))
48 (fset symbol last-kbd-macro))
49
7229064d 50;;;###autoload
a2535589
JA
51(defun insert-kbd-macro (macroname &optional keys)
52 "Insert in buffer the definition of kbd macro NAME, as Lisp code.
540671f3 53Optional second arg KEYS means also record the keys it is on
b4b3b736 54\(this is the prefix argument, when calling interactively).
a2535589 55
540671f3
RS
56This Lisp code will, when executed, define the kbd macro with the same
57definition it has now. If you say to record the keys, the Lisp code
58will also rebind those keys to the macro. Only global key bindings
59are recorded since executing this Lisp code always makes global
60bindings.
a2535589 61
b4b3b736 62To save a kbd macro, visit a file of Lisp code such as your `~/.emacs',
a2535589
JA
63use this command, and then save the file."
64 (interactive "CInsert kbd macro (name): \nP")
45ca32a6
RS
65 (let (definition)
66 (if (string= (symbol-name macroname) "")
67 (progn
68 (setq macroname 'last-kbd-macro definition last-kbd-macro)
69 (insert "(setq "))
70 (setq definition (symbol-function macroname))
71 (insert "(fset '"))
72 (prin1 macroname (current-buffer))
73 (insert "\n ")
157d78ec
KH
74 (if (stringp definition)
75 (let ((beg (point)) end)
76 (prin1 definition (current-buffer))
77 (setq end (point-marker))
78 (goto-char beg)
f0653de7
RS
79 (while (< (point) end)
80 (let ((char (following-char)))
81 (cond ((= char 0)
82 (delete-region (point) (1+ (point)))
83 (insert "\\C-@"))
84 ((< char 27)
85 (delete-region (point) (1+ (point)))
86 (insert "\\C-" (+ 96 char)))
87 ((= char ?\C-\\)
88 (delete-region (point) (1+ (point)))
89 (insert "\\C-\\\\"))
90 ((< char 32)
91 (delete-region (point) (1+ (point)))
92 (insert "\\C-" (+ 64 char)))
93 ((< char 127)
94 (forward-char 1))
95 ((= char 127)
96 (delete-region (point) (1+ (point)))
97 (insert "\\C-?"))
98 ((= char 128)
99 (delete-region (point) (1+ (point)))
100 (insert "\\M-\\C-@"))
101 ((= char (aref "\M-\C-\\" 0))
102 (delete-region (point) (1+ (point)))
103 (insert "\\M-\\C-\\\\"))
104 ((< char 155)
105 (delete-region (point) (1+ (point)))
106 (insert "\\M-\\C-" (- char 32)))
107 ((< char 160)
108 (delete-region (point) (1+ (point)))
109 (insert "\\M-\\C-" (- char 64)))
110 ((= char (aref "\M-\\" 0))
111 (delete-region (point) (1+ (point)))
112 (insert "\\M-\\\\"))
113 ((< char 255)
114 (delete-region (point) (1+ (point)))
115 (insert "\\M-" (- char 128)))
116 ((= char 255)
117 (delete-region (point) (1+ (point)))
157d78ec
KH
118 (insert "\\M-\\C-?"))))))
119 (if (vectorp definition)
07d1e73a 120 (let ((len (length definition)) (i 0) char mods)
157d78ec
KH
121 (while (< i len)
122 (insert (if (zerop i) ?\[ ?\ ))
123 (setq char (aref definition i)
124 i (1+ i))
07d1e73a 125 (cond ((not (numberp char))
157d78ec 126 (prin1 char (current-buffer)))
07d1e73a
RS
127 (t
128 (insert "?")
129 (setq mods (event-modifiers char)
130 char (event-basic-type char))
131 (while mods
132 (cond ((eq (car mods) 'control)
133 (insert "\\C-"))
134 ((eq (car mods) 'meta)
135 (insert "\\M-"))
136 ((eq (car mods) 'hyper)
137 (insert "\\H-"))
138 ((eq (car mods) 'super)
139 (insert "\\s-"))
140 ((eq (car mods) 'alt)
141 (insert "\\A-"))
142 ((and (eq (car mods) 'shift)
143 (>= char ?a)
144 (<= char ?z))
145 (setq char (upcase char)))
146 ((eq (car mods) 'shift)
147 (insert "\\S-")))
148 (setq mods (cdr mods)))
149 (cond ((= char ?\\)
150 (insert "\\\\"))
151 ((= char 127)
152 (insert "\\C-?"))
153 ((< char 127)
154 (insert char))
155 (t (insert "\\" (format "%o" char)))))))
157d78ec
KH
156 (insert ?\]))
157 (prin1 definition (current-buffer))))
45ca32a6
RS
158 (insert ")\n")
159 (if keys
16f63554 160 (let ((keys (where-is-internal macroname '(keymap))))
45ca32a6
RS
161 (while keys
162 (insert "(global-set-key ")
163 (prin1 (car keys) (current-buffer))
164 (insert " '")
165 (prin1 macroname (current-buffer))
166 (insert ")\n")
167 (setq keys (cdr keys)))))))
a2535589 168
7229064d 169;;;###autoload
a2535589
JA
170(defun kbd-macro-query (flag)
171 "Query user during kbd macro execution.
540671f3
RS
172 With prefix argument, enters recursive edit, reading keyboard
173commands even within a kbd macro. You can give different commands
174each time the macro executes.
ba790870
RS
175 Without prefix argument, asks whether to continue running the macro.
176Your options are: \\<query-replace-map>
177\\[act] Finish this iteration normally and continue with the next.
178\\[skip] Skip the rest of this iteration, and start the next.
179\\[exit] Stop the macro entirely right now.
180\\[recenter] Redisplay the screen, then ask again.
181\\[edit] Enter recursive edit; ask again when you exit from that."
a2535589
JA
182 (interactive "P")
183 (or executing-macro
184 defining-kbd-macro
185 (error "Not defining or executing kbd macro"))
186 (if flag
187 (let (executing-macro defining-kbd-macro)
188 (recursive-edit))
189 (if (not executing-macro)
190 nil
af400c00
RS
191 (let ((loop t)
192 (msg (substitute-command-keys
193 "Proceed with macro?\\<query-replace-map>\
8693344e 194 (\\[act], \\[skip], \\[exit], \\[recenter], \\[edit]) ")))
a2535589 195 (while loop
af400c00
RS
196 (let ((key (let ((executing-macro nil)
197 (defining-kbd-macro nil))
8700ec61 198 (message "%s" msg)
af400c00
RS
199 (read-event)))
200 def)
201 (setq key (vector key))
202 (setq def (lookup-key query-replace-map key))
203 (cond ((eq def 'act)
a2535589 204 (setq loop nil))
af400c00 205 ((eq def 'skip)
a2535589
JA
206 (setq loop nil)
207 (setq executing-macro ""))
af400c00 208 ((eq def 'exit)
a2535589
JA
209 (setq loop nil)
210 (setq executing-macro t))
af400c00 211 ((eq def 'recenter)
a2535589 212 (recenter nil))
af400c00 213 ((eq def 'edit)
a2535589 214 (let (executing-macro defining-kbd-macro)
af400c00
RS
215 (recursive-edit)))
216 ((eq def 'quit)
217 (setq quit-flag t))
218 (t
219 (or (eq def 'help)
220 (ding))
221 (with-output-to-temp-buffer "*Help*"
222 (princ
223 (substitute-command-keys
eb8c3be9 224 "Specify how to proceed with keyboard macro execution.
af400c00
RS
225Possibilities: \\<query-replace-map>
226\\[act] Finish this iteration normally and continue with the next.
227\\[skip] Skip the rest of this iteration, and start the next.
228\\[exit] Stop the macro entirely right now.
229\\[recenter] Redisplay the screen, then ask again.
6ec1c571
KH
230\\[edit] Enter recursive edit; ask again when you exit from that."))
231 (save-excursion
232 (set-buffer standard-output)
233 (help-mode)))))))))))
7229064d 234
63c86e17
JB
235;;;###autoload
236(defun apply-macro-to-region-lines (top bottom &optional macro)
bcc78bc0
JB
237 "For each complete line between point and mark, move to the beginning
238of the line, and run the last keyboard macro.
63c86e17
JB
239
240When called from lisp, this function takes two arguments TOP and
241BOTTOM, describing the current region. TOP must be before BOTTOM.
242The optional third argument MACRO specifies a keyboard macro to
243execute.
244
245This is useful for quoting or unquoting included text, adding and
246removing comments, or producing tables where the entries are regular.
247
248For example, in Usenet articles, sections of text quoted from another
249author are indented, or have each line start with `>'. To quote a
250section of text, define a keyboard macro which inserts `>', put point
251and mark at opposite ends of the quoted section, and use
252`\\[apply-macro-to-region-lines]' to mark the entire section.
253
254Suppose you wanted to build a keyword table in C where each entry
255looked like this:
256
257 { \"foo\", foo_data, foo_function },
258 { \"bar\", bar_data, bar_function },
259 { \"baz\", baz_data, baz_function },
260
261You could enter the names in this format:
262
263 foo
264 bar
265 baz
266
267and write a macro to massage a word into a table entry:
268
269 \\C-x (
270 \\M-d { \"\\C-y\", \\C-y_data, \\C-y_function },
271 \\C-x )
272
273and then select the region of un-tablified names and use
274`\\[apply-macro-to-region-lines]' to build the table from the names.
275"
276 (interactive "r")
bcc78bc0
JB
277 (or macro
278 (progn
279 (if (null last-kbd-macro)
280 (error "No keyboard macro has been defined."))
281 (setq macro last-kbd-macro)))
63c86e17
JB
282 (save-excursion
283 (let ((end-marker (progn
284 (goto-char bottom)
285 (beginning-of-line)
5277487d
JB
286 (point-marker)))
287 next-line-marker)
63c86e17
JB
288 (goto-char top)
289 (if (not (bolp))
290 (forward-line 1))
5277487d
JB
291 (setq next-line-marker (point-marker))
292 (while (< next-line-marker end-marker)
293 (goto-char next-line-marker)
3d64136f 294 (save-excursion
5277487d
JB
295 (forward-line 1)
296 (set-marker next-line-marker (point)))
297 (save-excursion
298 (execute-kbd-macro (or macro last-kbd-macro))))
299 (set-marker end-marker nil)
300 (set-marker next-line-marker nil))))
63c86e17 301
5f5d794a 302;;;###autoload (define-key ctl-x-map "q" 'kbd-macro-query)
6594deb0
ER
303
304;;; macros.el ends here