newcomment.el (comment-line): Fix missing paren.
[emacs.git] / lisp / macros.el
blobbf1aa30d607f57e254bed2e383923a63f03a9f31
1 ;;; macros.el --- non-primitive commands for keyboard macros
3 ;; Copyright (C) 1985-1987, 1992, 1994-1995, 2001-2015 Free Software
4 ;; Foundation, Inc.
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: abbrev
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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 MACRONAME, as Lisp code.
55 MACRONAME should be a symbol.
56 Optional second arg KEYS means also record the keys it is on
57 \(this is the prefix argument, when calling interactively).
59 This Lisp code will, when executed, define the kbd macro with the same
60 definition it has now. If you say to record the keys, the Lisp code
61 will also rebind those keys to the macro. Only global key bindings
62 are recorded since executing this Lisp code always makes global
63 bindings.
65 To save a kbd macro, visit a file of Lisp code such as your `~/.emacs',
66 use this command, and then save the file."
67 (interactive (list (intern (completing-read "Insert kbd macro (name): "
68 obarray
69 (lambda (elt)
70 (and (fboundp elt)
71 (or (stringp (symbol-function elt))
72 (vectorp (symbol-function elt))
73 (get elt 'kmacro))))
74 t))
75 current-prefix-arg))
76 (let (definition)
77 (if (string= (symbol-name macroname) "")
78 (progn
79 (setq macroname 'last-kbd-macro definition last-kbd-macro)
80 (insert "(setq "))
81 (setq definition (symbol-function macroname))
82 (insert "(fset '"))
83 (prin1 macroname (current-buffer))
84 (insert "\n ")
85 (if (stringp definition)
86 (let ((beg (point)) end)
87 (prin1 definition (current-buffer))
88 (setq end (point-marker))
89 (goto-char beg)
90 (while (< (point) end)
91 (let ((char (following-char)))
92 (cond ((= char 0)
93 (delete-region (point) (1+ (point)))
94 (insert "\\C-@"))
95 ((< char 27)
96 (delete-region (point) (1+ (point)))
97 (insert "\\C-" (+ 96 char)))
98 ((= char ?\C-\\)
99 (delete-region (point) (1+ (point)))
100 (insert "\\C-\\\\"))
101 ((< char 32)
102 (delete-region (point) (1+ (point)))
103 (insert "\\C-" (+ 64 char)))
104 ((< char 127)
105 (forward-char 1))
106 ((= char 127)
107 (delete-region (point) (1+ (point)))
108 (insert "\\C-?"))
109 ((= char 128)
110 (delete-region (point) (1+ (point)))
111 (insert "\\M-\\C-@"))
112 ((= char (aref "\M-\C-\\" 0))
113 (delete-region (point) (1+ (point)))
114 (insert "\\M-\\C-\\\\"))
115 ((< char 155)
116 (delete-region (point) (1+ (point)))
117 (insert "\\M-\\C-" (- char 32)))
118 ((< char 160)
119 (delete-region (point) (1+ (point)))
120 (insert "\\M-\\C-" (- char 64)))
121 ((= char (aref "\M-\\" 0))
122 (delete-region (point) (1+ (point)))
123 (insert "\\M-\\\\"))
124 ((< char 255)
125 (delete-region (point) (1+ (point)))
126 (insert "\\M-" (- char 128)))
127 ((= char 255)
128 (delete-region (point) (1+ (point)))
129 (insert "\\M-\\C-?"))))))
130 (if (vectorp definition)
131 (let ((len (length definition)) (i 0) char)
132 (while (< i len)
133 (insert (if (zerop i) ?\[ ?\s))
134 (setq char (aref definition i)
135 i (1+ i))
136 (if (not (numberp char))
137 (prin1 char (current-buffer))
138 (princ (prin1-char char) (current-buffer))))
139 (insert ?\]))
140 (prin1 definition (current-buffer))))
141 (insert ")\n")
142 (if keys
143 (let ((keys (where-is-internal (symbol-function macroname)
144 '(keymap))))
145 (while keys
146 (insert "(global-set-key ")
147 (prin1 (car keys) (current-buffer))
148 (insert " '")
149 (prin1 macroname (current-buffer))
150 (insert ")\n")
151 (setq keys (cdr keys)))))))
153 ;;;###autoload
154 (defun kbd-macro-query (flag)
155 "Query user during kbd macro execution.
156 With prefix argument, enters recursive edit, reading keyboard
157 commands even within a kbd macro. You can give different commands
158 each time the macro executes.
159 Without prefix argument, asks whether to continue running the macro.
160 Your options are: \\<query-replace-map>
161 \\[act] Finish this iteration normally and continue with the next.
162 \\[skip] Skip the rest of this iteration, and start the next.
163 \\[exit] Stop the macro entirely right now.
164 \\[recenter] Redisplay the screen, then ask again.
165 \\[edit] Enter recursive edit; ask again when you exit from that."
166 (interactive "P")
167 (or executing-kbd-macro
168 defining-kbd-macro
169 (error "Not defining or executing kbd macro"))
170 (if flag
171 (let (executing-kbd-macro defining-kbd-macro)
172 (recursive-edit))
173 (if (not executing-kbd-macro)
175 (let ((loop t)
176 (msg (substitute-command-keys
177 "Proceed with macro?\\<query-replace-map>\
178 (\\[act], \\[skip], \\[exit], \\[recenter], \\[edit]) ")))
179 (while loop
180 (let ((key (let ((executing-kbd-macro nil)
181 (defining-kbd-macro nil))
182 (message "%s" msg)
183 (read-event)))
184 def)
185 (setq key (vector key))
186 (setq def (lookup-key query-replace-map key))
187 (cond ((eq def 'act)
188 (setq loop nil))
189 ((eq def 'skip)
190 (setq loop nil)
191 (setq executing-kbd-macro ""))
192 ((eq def 'exit)
193 (setq loop nil)
194 (setq executing-kbd-macro t))
195 ((eq def 'recenter)
196 (recenter nil))
197 ((eq def 'edit)
198 (let (executing-kbd-macro defining-kbd-macro)
199 (recursive-edit)))
200 ((eq def 'quit)
201 (setq quit-flag t))
203 (or (eq def 'help)
204 (ding))
205 (with-output-to-temp-buffer "*Help*"
206 (princ
207 (substitute-command-keys
208 "Specify how to proceed with keyboard macro execution.
209 Possibilities: \\<query-replace-map>
210 \\[act] Finish this iteration normally and continue with the next.
211 \\[skip] Skip the rest of this iteration, and start the next.
212 \\[exit] Stop the macro entirely right now.
213 \\[recenter] Redisplay the screen, then ask again.
214 \\[edit] Enter recursive edit; ask again when you exit from that."))
215 (with-current-buffer standard-output
216 (help-mode)))))))))))
218 ;;;###autoload
219 (defun apply-macro-to-region-lines (top bottom &optional macro)
220 "Apply last keyboard macro to all lines in the region.
221 For each line that begins in the region, move to the beginning of
222 the line, and run the last keyboard macro.
224 When called from lisp, this function takes two arguments TOP and
225 BOTTOM, describing the current region. TOP must be before BOTTOM.
226 The optional third argument MACRO specifies a keyboard macro to
227 execute.
229 This is useful for quoting or unquoting included text, adding and
230 removing comments, or producing tables where the entries are regular.
232 For example, in Usenet articles, sections of text quoted from another
233 author are indented, or have each line start with `>'. To quote a
234 section of text, define a keyboard macro which inserts `>', put point
235 and mark at opposite ends of the quoted section, and use
236 `\\[apply-macro-to-region-lines]' to mark the entire section.
238 Suppose you wanted to build a keyword table in C where each entry
239 looked like this:
241 { \"foo\", foo_data, foo_function },
242 { \"bar\", bar_data, bar_function },
243 { \"baz\", baz_data, baz_function },
245 You could enter the names in this format:
251 and write a macro to massage a word into a table entry:
253 \\C-x (
254 \\M-d { \"\\C-y\", \\C-y_data, \\C-y_function },
255 \\C-x )
257 and then select the region of un-tablified names and use
258 `\\[apply-macro-to-region-lines]' to build the table from the names."
259 (interactive "r")
260 (or macro
261 (progn
262 (if (null last-kbd-macro)
263 (error "No keyboard macro has been defined"))
264 (setq macro last-kbd-macro)))
265 (save-excursion
266 (let ((end-marker (copy-marker bottom))
267 next-line-marker)
268 (goto-char top)
269 (if (not (bolp))
270 (forward-line 1))
271 (setq next-line-marker (point-marker))
272 (while (< next-line-marker end-marker)
273 (goto-char next-line-marker)
274 (save-excursion
275 (forward-line 1)
276 (set-marker next-line-marker (point)))
277 (save-excursion
278 (let ((mark-active nil))
279 (execute-kbd-macro macro))))
280 (set-marker end-marker nil)
281 (set-marker next-line-marker nil))))
283 ;;;###autoload (define-key ctl-x-map "q" 'kbd-macro-query)
285 (provide 'macros)
287 ;;; macros.el ends here