(iso-cvt-define-menu): menu-bar-files-menu renamed to menu-bar-file-menu.
[emacs.git] / lisp / macros.el
blobbb9fda41a4552fa14399af1d8668526ac92cb8fe
1 ;;; macros.el --- non-primitive commands for keyboard macros
3 ;; Copyright (C) 1985, 86, 87, 92, 94, 95, 04 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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
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.
32 ;;; Code:
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 (if (string-equal symbol "")
49 (error "No command name given"))
50 (fset symbol last-kbd-macro))
52 ;;;###autoload
53 (defun insert-kbd-macro (macroname &optional keys)
54 "Insert in buffer the definition of kbd macro NAME, as Lisp code.
55 Optional second arg KEYS means also record the keys it is on
56 \(this is the prefix argument, when calling interactively).
58 This Lisp code will, when executed, define the kbd macro with the same
59 definition it has now. If you say to record the keys, the Lisp code
60 will also rebind those keys to the macro. Only global key bindings
61 are recorded since executing this Lisp code always makes global
62 bindings.
64 To save a kbd macro, visit a file of Lisp code such as your `~/.emacs',
65 use this command, and then save the file."
66 (interactive (list (intern (completing-read "Insert kbd macro (name): "
67 obarray
68 (lambda (elt)
69 (and (fboundp elt)
70 (or (stringp (symbol-function elt))
71 (vectorp (symbol-function elt)))))
72 t))
73 current-prefix-arg))
74 (let (definition)
75 (if (string= (symbol-name macroname) "")
76 (progn
77 (setq macroname 'last-kbd-macro definition last-kbd-macro)
78 (insert "(setq "))
79 (setq definition (symbol-function macroname))
80 (insert "(fset '"))
81 (prin1 macroname (current-buffer))
82 (insert "\n ")
83 (if (stringp definition)
84 (let ((beg (point)) end)
85 (prin1 definition (current-buffer))
86 (setq end (point-marker))
87 (goto-char beg)
88 (while (< (point) end)
89 (let ((char (following-char)))
90 (cond ((= char 0)
91 (delete-region (point) (1+ (point)))
92 (insert "\\C-@"))
93 ((< char 27)
94 (delete-region (point) (1+ (point)))
95 (insert "\\C-" (+ 96 char)))
96 ((= char ?\C-\\)
97 (delete-region (point) (1+ (point)))
98 (insert "\\C-\\\\"))
99 ((< char 32)
100 (delete-region (point) (1+ (point)))
101 (insert "\\C-" (+ 64 char)))
102 ((< char 127)
103 (forward-char 1))
104 ((= char 127)
105 (delete-region (point) (1+ (point)))
106 (insert "\\C-?"))
107 ((= char 128)
108 (delete-region (point) (1+ (point)))
109 (insert "\\M-\\C-@"))
110 ((= char (aref "\M-\C-\\" 0))
111 (delete-region (point) (1+ (point)))
112 (insert "\\M-\\C-\\\\"))
113 ((< char 155)
114 (delete-region (point) (1+ (point)))
115 (insert "\\M-\\C-" (- char 32)))
116 ((< char 160)
117 (delete-region (point) (1+ (point)))
118 (insert "\\M-\\C-" (- char 64)))
119 ((= char (aref "\M-\\" 0))
120 (delete-region (point) (1+ (point)))
121 (insert "\\M-\\\\"))
122 ((< char 255)
123 (delete-region (point) (1+ (point)))
124 (insert "\\M-" (- char 128)))
125 ((= char 255)
126 (delete-region (point) (1+ (point)))
127 (insert "\\M-\\C-?"))))))
128 (if (vectorp definition)
129 (let ((len (length definition)) (i 0) char mods)
130 (while (< i len)
131 (insert (if (zerop i) ?\[ ?\ ))
132 (setq char (aref definition i)
133 i (1+ i))
134 (cond ((not (numberp char))
135 (prin1 char (current-buffer)))
137 (insert "?")
138 (setq mods (event-modifiers char)
139 char (event-basic-type char))
140 (while mods
141 (cond ((eq (car mods) 'control)
142 (insert "\\C-"))
143 ((eq (car mods) 'meta)
144 (insert "\\M-"))
145 ((eq (car mods) 'hyper)
146 (insert "\\H-"))
147 ((eq (car mods) 'super)
148 (insert "\\s-"))
149 ((eq (car mods) 'alt)
150 (insert "\\A-"))
151 ((and (eq (car mods) 'shift)
152 (>= char ?a)
153 (<= char ?z))
154 (setq char (upcase char)))
155 ((eq (car mods) 'shift)
156 (insert "\\S-")))
157 (setq mods (cdr mods)))
158 (cond ((= char ?\\)
159 (insert "\\\\"))
160 ((= char ?\")
161 (insert "\\\""))
162 ((= char ?\;)
163 (insert "\\;"))
164 ((= char 127)
165 (insert "\\C-?"))
166 ((< char 127)
167 (insert char))
168 (t (insert "\\" (format "%o" char)))))))
169 (insert ?\]))
170 (prin1 definition (current-buffer))))
171 (insert ")\n")
172 (if keys
173 (let ((keys (where-is-internal macroname '(keymap))))
174 (while keys
175 (insert "(global-set-key ")
176 (prin1 (car keys) (current-buffer))
177 (insert " '")
178 (prin1 macroname (current-buffer))
179 (insert ")\n")
180 (setq keys (cdr keys)))))))
182 ;;;###autoload
183 (defun kbd-macro-query (flag)
184 "Query user during kbd macro execution.
185 With prefix argument, enters recursive edit, reading keyboard
186 commands even within a kbd macro. You can give different commands
187 each time the macro executes.
188 Without prefix argument, asks whether to continue running the macro.
189 Your options are: \\<query-replace-map>
190 \\[act] Finish this iteration normally and continue with the next.
191 \\[skip] Skip the rest of this iteration, and start the next.
192 \\[exit] Stop the macro entirely right now.
193 \\[recenter] Redisplay the screen, then ask again.
194 \\[edit] Enter recursive edit; ask again when you exit from that."
195 (interactive "P")
196 (or executing-kbd-macro
197 defining-kbd-macro
198 (error "Not defining or executing kbd macro"))
199 (if flag
200 (let (executing-kbd-macro defining-kbd-macro)
201 (recursive-edit))
202 (if (not executing-kbd-macro)
204 (let ((loop t)
205 (msg (substitute-command-keys
206 "Proceed with macro?\\<query-replace-map>\
207 (\\[act], \\[skip], \\[exit], \\[recenter], \\[edit]) ")))
208 (while loop
209 (let ((key (let ((executing-kbd-macro nil)
210 (defining-kbd-macro nil))
211 (message "%s" msg)
212 (read-event)))
213 def)
214 (setq key (vector key))
215 (setq def (lookup-key query-replace-map key))
216 (cond ((eq def 'act)
217 (setq loop nil))
218 ((eq def 'skip)
219 (setq loop nil)
220 (setq executing-kbd-macro ""))
221 ((eq def 'exit)
222 (setq loop nil)
223 (setq executing-kbd-macro t))
224 ((eq def 'recenter)
225 (recenter nil))
226 ((eq def 'edit)
227 (let (executing-kbd-macro defining-kbd-macro)
228 (recursive-edit)))
229 ((eq def 'quit)
230 (setq quit-flag t))
232 (or (eq def 'help)
233 (ding))
234 (with-output-to-temp-buffer "*Help*"
235 (princ
236 (substitute-command-keys
237 "Specify how to proceed with keyboard macro execution.
238 Possibilities: \\<query-replace-map>
239 \\[act] Finish this iteration normally and continue with the next.
240 \\[skip] Skip the rest of this iteration, and start the next.
241 \\[exit] Stop the macro entirely right now.
242 \\[recenter] Redisplay the screen, then ask again.
243 \\[edit] Enter recursive edit; ask again when you exit from that."))
244 (save-excursion
245 (set-buffer standard-output)
246 (help-mode)))))))))))
248 ;;;###autoload
249 (defun apply-macro-to-region-lines (top bottom &optional macro)
250 "Apply last keyboard macro to all lines in the region.
251 For each line that begins in the region, move to the beginning of
252 the line, and run the last keyboard macro.
254 When called from lisp, this function takes two arguments TOP and
255 BOTTOM, describing the current region. TOP must be before BOTTOM.
256 The optional third argument MACRO specifies a keyboard macro to
257 execute.
259 This is useful for quoting or unquoting included text, adding and
260 removing comments, or producing tables where the entries are regular.
262 For example, in Usenet articles, sections of text quoted from another
263 author are indented, or have each line start with `>'. To quote a
264 section of text, define a keyboard macro which inserts `>', put point
265 and mark at opposite ends of the quoted section, and use
266 `\\[apply-macro-to-region-lines]' to mark the entire section.
268 Suppose you wanted to build a keyword table in C where each entry
269 looked like this:
271 { \"foo\", foo_data, foo_function },
272 { \"bar\", bar_data, bar_function },
273 { \"baz\", baz_data, baz_function },
275 You could enter the names in this format:
281 and write a macro to massage a word into a table entry:
283 \\C-x (
284 \\M-d { \"\\C-y\", \\C-y_data, \\C-y_function },
285 \\C-x )
287 and then select the region of un-tablified names and use
288 `\\[apply-macro-to-region-lines]' to build the table from the names."
289 (interactive "r")
290 (or macro
291 (progn
292 (if (null last-kbd-macro)
293 (error "No keyboard macro has been defined"))
294 (setq macro last-kbd-macro)))
295 (save-excursion
296 (let ((end-marker (copy-marker bottom))
297 next-line-marker)
298 (goto-char top)
299 (if (not (bolp))
300 (forward-line 1))
301 (setq next-line-marker (point-marker))
302 (while (< next-line-marker end-marker)
303 (goto-char next-line-marker)
304 (save-excursion
305 (forward-line 1)
306 (set-marker next-line-marker (point)))
307 (save-excursion
308 (let ((mark-active nil))
309 (execute-kbd-macro (or macro last-kbd-macro)))))
310 (set-marker end-marker nil)
311 (set-marker next-line-marker nil))))
313 ;;;###autoload (define-key ctl-x-map "q" 'kbd-macro-query)
315 (provide 'macros)
317 ;;; arch-tag: 346ed1a5-1220-4bc8-b533-961ee704361f
318 ;;; macros.el ends here