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