(using-unix-filesystems):
[emacs.git] / lisp / macros.el
blobd834dca8806f5fad135fb4f697d7cf201f7f6851
1 ;;; macros.el --- non-primitive commands for keyboard macros.
3 ;; Copyright (C) 1985, 86, 87, 92, 94, 95 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: abbrev
8 ;; This file is part of GNU Emacs.
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.
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.
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.
24 ;;; Commentary:
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.
31 ;;; Code:
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))
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).
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.
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 mods)
120 (while (< i len)
121 (insert (if (zerop i) ?\[ ?\ ))
122 (setq char (aref definition i)
123 i (1+ i))
124 (cond ((not (numberp char))
125 (prin1 char (current-buffer)))
127 (insert "?")
128 (setq mods (event-modifiers char)
129 char (event-basic-type char))
130 (while mods
131 (cond ((eq (car mods) 'control)
132 (insert "\\C-"))
133 ((eq (car mods) 'meta)
134 (insert "\\M-"))
135 ((eq (car mods) 'hyper)
136 (insert "\\H-"))
137 ((eq (car mods) 'super)
138 (insert "\\s-"))
139 ((eq (car mods) 'alt)
140 (insert "\\A-"))
141 ((and (eq (car mods) 'shift)
142 (>= char ?a)
143 (<= char ?z))
144 (setq char (upcase char)))
145 ((eq (car mods) 'shift)
146 (insert "\\S-")))
147 (setq mods (cdr mods)))
148 (cond ((= char ?\\)
149 (insert "\\\\"))
150 ((= char 127)
151 (insert "\\C-?"))
152 ((< char 127)
153 (insert char))
154 (t (insert "\\" (format "%o" char)))))))
155 (insert ?\]))
156 (prin1 definition (current-buffer))))
157 (insert ")\n")
158 (if keys
159 (let ((keys (where-is-internal macroname '(keymap))))
160 (while keys
161 (insert "(global-set-key ")
162 (prin1 (car keys) (current-buffer))
163 (insert " '")
164 (prin1 macroname (current-buffer))
165 (insert ")\n")
166 (setq keys (cdr keys)))))))
168 ;;;###autoload
169 (defun kbd-macro-query (flag)
170 "Query user during kbd macro execution.
171 With prefix argument, enters recursive edit, reading keyboard
172 commands even within a kbd macro. You can give different commands
173 each time the macro executes.
174 Without prefix argument, asks whether to continue running the macro.
175 Your options are: \\<query-replace-map>
176 \\[act] Finish this iteration normally and continue with the next.
177 \\[skip] Skip the rest of this iteration, and start the next.
178 \\[exit] Stop the macro entirely right now.
179 \\[recenter] Redisplay the screen, then ask again.
180 \\[edit] Enter recursive edit; ask again when you exit from that."
181 (interactive "P")
182 (or executing-macro
183 defining-kbd-macro
184 (error "Not defining or executing kbd macro"))
185 (if flag
186 (let (executing-macro defining-kbd-macro)
187 (recursive-edit))
188 (if (not executing-macro)
190 (let ((loop t)
191 (msg (substitute-command-keys
192 "Proceed with macro?\\<query-replace-map>\
193 (\\[act], \\[skip], \\[exit], \\[recenter], \\[edit]) ")))
194 (while loop
195 (let ((key (let ((executing-macro nil)
196 (defining-kbd-macro nil))
197 (message msg)
198 (read-event)))
199 def)
200 (setq key (vector key))
201 (setq def (lookup-key query-replace-map key))
202 (cond ((eq def 'act)
203 (setq loop nil))
204 ((eq def 'skip)
205 (setq loop nil)
206 (setq executing-macro ""))
207 ((eq def 'exit)
208 (setq loop nil)
209 (setq executing-macro t))
210 ((eq def 'recenter)
211 (recenter nil))
212 ((eq def 'edit)
213 (let (executing-macro defining-kbd-macro)
214 (recursive-edit)))
215 ((eq def 'quit)
216 (setq quit-flag t))
218 (or (eq def 'help)
219 (ding))
220 (with-output-to-temp-buffer "*Help*"
221 (princ
222 (substitute-command-keys
223 "Specify how to proceed with keyboard macro execution.
224 Possibilities: \\<query-replace-map>
225 \\[act] Finish this iteration normally and continue with the next.
226 \\[skip] Skip the rest of this iteration, and start the next.
227 \\[exit] Stop the macro entirely right now.
228 \\[recenter] Redisplay the screen, then ask again.
229 \\[edit] Enter recursive edit; ask again when you exit from that."))
230 (save-excursion
231 (set-buffer standard-output)
232 (help-mode)))))))))))
234 ;;;###autoload
235 (defun apply-macro-to-region-lines (top bottom &optional macro)
236 "For each complete line between point and mark, move to the beginning
237 of the line, and run the last keyboard macro.
239 When called from lisp, this function takes two arguments TOP and
240 BOTTOM, describing the current region. TOP must be before BOTTOM.
241 The optional third argument MACRO specifies a keyboard macro to
242 execute.
244 This is useful for quoting or unquoting included text, adding and
245 removing comments, or producing tables where the entries are regular.
247 For example, in Usenet articles, sections of text quoted from another
248 author are indented, or have each line start with `>'. To quote a
249 section of text, define a keyboard macro which inserts `>', put point
250 and mark at opposite ends of the quoted section, and use
251 `\\[apply-macro-to-region-lines]' to mark the entire section.
253 Suppose you wanted to build a keyword table in C where each entry
254 looked like this:
256 { \"foo\", foo_data, foo_function },
257 { \"bar\", bar_data, bar_function },
258 { \"baz\", baz_data, baz_function },
260 You could enter the names in this format:
266 and write a macro to massage a word into a table entry:
268 \\C-x (
269 \\M-d { \"\\C-y\", \\C-y_data, \\C-y_function },
270 \\C-x )
272 and then select the region of un-tablified names and use
273 `\\[apply-macro-to-region-lines]' to build the table from the names.
275 (interactive "r")
276 (or macro
277 (progn
278 (if (null last-kbd-macro)
279 (error "No keyboard macro has been defined."))
280 (setq macro last-kbd-macro)))
281 (save-excursion
282 (let ((end-marker (progn
283 (goto-char bottom)
284 (beginning-of-line)
285 (point-marker)))
286 next-line-marker)
287 (goto-char top)
288 (if (not (bolp))
289 (forward-line 1))
290 (setq next-line-marker (point-marker))
291 (while (< next-line-marker end-marker)
292 (goto-char next-line-marker)
293 (save-excursion
294 (forward-line 1)
295 (set-marker next-line-marker (point)))
296 (save-excursion
297 (execute-kbd-macro (or macro last-kbd-macro))))
298 (set-marker end-marker nil)
299 (set-marker next-line-marker nil))))
301 ;;;###autoload (define-key ctl-x-map "q" 'kbd-macro-query)
303 ;;; macros.el ends here