(vc-diff): Make NOT-URGENT default to t.
[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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
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
32 ;;; Code:
33
34 ;;;###autoload
35 (defun name-last-kbd-macro (symbol)
36 "Assign a name to the last keyboard macro defined.
37 Argument SYMBOL is the name to define.
38 The symbol's function definition becomes the keyboard macro string.
39 Such a \"function\" cannot be called from Lisp, but it is a valid editor command."
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)))
45 (not (vectorp (symbol-function symbol)))
46 (error "Function %s is already defined and not a keyboard macro."
47 symbol))
48 (fset symbol last-kbd-macro))
49
50 ;;;###autoload
51 (defun insert-kbd-macro (macroname &optional keys)
52 "Insert in buffer the definition of kbd macro NAME, as Lisp code.
53 Optional second arg KEYS means also record the keys it is on
54 \(this is the prefix argument, when calling interactively).
55
56 This Lisp code will, when executed, define the kbd macro with the same
57 definition it has now. If you say to record the keys, the Lisp code
58 will also rebind those keys to the macro. Only global key bindings
59 are recorded since executing this Lisp code always makes global
60 bindings.
61
62 To save a kbd macro, visit a file of Lisp code such as your `~/.emacs',
63 use this command, and then save the file."
64 (interactive "CInsert kbd macro (name): \nP")
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 ")
74 (if (stringp definition)
75 (let ((beg (point)) end)
76 (prin1 definition (current-buffer))
77 (setq end (point-marker))
78 (goto-char beg)
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)))
118 (insert "\\M-\\C-?"))))))
119 (if (vectorp definition)
120 (let ((len (length definition)) (i 0) char mods)
121 (while (< i len)
122 (insert (if (zerop i) ?\[ ?\ ))
123 (setq char (aref definition i)
124 i (1+ i))
125 (cond ((not (numberp char))
126 (prin1 char (current-buffer)))
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)))))))
156 (insert ?\]))
157 (prin1 definition (current-buffer))))
158 (insert ")\n")
159 (if keys
160 (let ((keys (where-is-internal macroname '(keymap))))
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)))))))
168
169 ;;;###autoload
170 (defun kbd-macro-query (flag)
171 "Query user during kbd macro execution.
172 With prefix argument, enters recursive edit, reading keyboard
173 commands even within a kbd macro. You can give different commands
174 each time the macro executes.
175 Without prefix argument, asks whether to continue running the macro.
176 Your 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."
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
191 (let ((loop t)
192 (msg (substitute-command-keys
193 "Proceed with macro?\\<query-replace-map>\
194 (\\[act], \\[skip], \\[exit], \\[recenter], \\[edit]) ")))
195 (while loop
196 (let ((key (let ((executing-macro nil)
197 (defining-kbd-macro nil))
198 (message "%s" msg)
199 (read-event)))
200 def)
201 (setq key (vector key))
202 (setq def (lookup-key query-replace-map key))
203 (cond ((eq def 'act)
204 (setq loop nil))
205 ((eq def 'skip)
206 (setq loop nil)
207 (setq executing-macro ""))
208 ((eq def 'exit)
209 (setq loop nil)
210 (setq executing-macro t))
211 ((eq def 'recenter)
212 (recenter nil))
213 ((eq def 'edit)
214 (let (executing-macro defining-kbd-macro)
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
224 "Specify how to proceed with keyboard macro execution.
225 Possibilities: \\<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.
230 \\[edit] Enter recursive edit; ask again when you exit from that."))
231 (save-excursion
232 (set-buffer standard-output)
233 (help-mode)))))))))))
234
235 ;;;###autoload
236 (defun apply-macro-to-region-lines (top bottom &optional macro)
237 "For each complete line between point and mark, move to the beginning
238 of the line, and run the last keyboard macro.
239
240 When called from lisp, this function takes two arguments TOP and
241 BOTTOM, describing the current region. TOP must be before BOTTOM.
242 The optional third argument MACRO specifies a keyboard macro to
243 execute.
244
245 This is useful for quoting or unquoting included text, adding and
246 removing comments, or producing tables where the entries are regular.
247
248 For example, in Usenet articles, sections of text quoted from another
249 author are indented, or have each line start with `>'. To quote a
250 section of text, define a keyboard macro which inserts `>', put point
251 and mark at opposite ends of the quoted section, and use
252 `\\[apply-macro-to-region-lines]' to mark the entire section.
253
254 Suppose you wanted to build a keyword table in C where each entry
255 looked like this:
256
257 { \"foo\", foo_data, foo_function },
258 { \"bar\", bar_data, bar_function },
259 { \"baz\", baz_data, baz_function },
260
261 You could enter the names in this format:
262
263 foo
264 bar
265 baz
266
267 and 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
273 and 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")
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)))
282 (save-excursion
283 (let ((end-marker (progn
284 (goto-char bottom)
285 (beginning-of-line)
286 (point-marker)))
287 next-line-marker)
288 (goto-char top)
289 (if (not (bolp))
290 (forward-line 1))
291 (setq next-line-marker (point-marker))
292 (while (< next-line-marker end-marker)
293 (goto-char next-line-marker)
294 (save-excursion
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))))
301
302 ;;;###autoload (define-key ctl-x-map "q" 'kbd-macro-query)
303
304 ;;; macros.el ends here