Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; macros.el --- non-primitive commands for keyboard macros |
6594deb0 | 2 | |
c90f2757 | 3 | ;; Copyright (C) 1985, 1986, 1987, 1992, 1994, 1995, 2001, 2002, 2003, |
114f9c96 | 4 | ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
a2535589 | 5 | |
9750e079 | 6 | ;; Maintainer: FSF |
e9571d2a | 7 | ;; Keywords: abbrev |
9750e079 | 8 | |
a2535589 JA |
9 | ;; This file is part of GNU Emacs. |
10 | ||
eb3fa2cf | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
a2535589 | 12 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
a2535589 JA |
15 | |
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
a2535589 | 23 | |
e41b2db1 ER |
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 | ||
e5167999 | 31 | ;;; Code: |
a2535589 | 32 | |
7229064d | 33 | ;;;###autoload |
a2535589 JA |
34 | (defun name-last-kbd-macro (symbol) |
35 | "Assign a name to the last keyboard macro defined. | |
ac5b56bc | 36 | Argument SYMBOL is the name to define. |
a2535589 | 37 | The symbol's function definition becomes the keyboard macro string. |
540671f3 | 38 | Such a \"function\" cannot be called from Lisp, but it is a valid editor command." |
a2535589 JA |
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))) | |
4dc7458e | 44 | (not (vectorp (symbol-function symbol))) |
55535639 | 45 | (error "Function %s is already defined and not a keyboard macro" |
a2535589 | 46 | symbol)) |
a3fc4dee RS |
47 | (if (string-equal symbol "") |
48 | (error "No command name given")) | |
a2535589 JA |
49 | (fset symbol last-kbd-macro)) |
50 | ||
7229064d | 51 | ;;;###autoload |
a2535589 JA |
52 | (defun insert-kbd-macro (macroname &optional keys) |
53 | "Insert in buffer the definition of kbd macro NAME, as Lisp code. | |
540671f3 | 54 | Optional second arg KEYS means also record the keys it is on |
b4b3b736 | 55 | \(this is the prefix argument, when calling interactively). |
a2535589 | 56 | |
540671f3 RS |
57 | This Lisp code will, when executed, define the kbd macro with the same |
58 | definition it has now. If you say to record the keys, the Lisp code | |
59 | will also rebind those keys to the macro. Only global key bindings | |
60 | are recorded since executing this Lisp code always makes global | |
61 | bindings. | |
a2535589 | 62 | |
b4b3b736 | 63 | To save a kbd macro, visit a file of Lisp code such as your `~/.emacs', |
a2535589 | 64 | use this command, and then save the file." |
5edd7c33 KS |
65 | (interactive (list (intern (completing-read "Insert kbd macro (name): " |
66 | obarray | |
4d79b874 EZ |
67 | (lambda (elt) |
68 | (and (fboundp elt) | |
69 | (or (stringp (symbol-function elt)) | |
5edd7c33 KS |
70 | (vectorp (symbol-function elt)) |
71 | (get elt 'kmacro)))) | |
4d79b874 EZ |
72 | t)) |
73 | current-prefix-arg)) | |
45ca32a6 RS |
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 ") | |
157d78ec KH |
83 | (if (stringp definition) |
84 | (let ((beg (point)) end) | |
85 | (prin1 definition (current-buffer)) | |
86 | (setq end (point-marker)) | |
87 | (goto-char beg) | |
f0653de7 RS |
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))) | |
157d78ec KH |
127 | (insert "\\M-\\C-?")))))) |
128 | (if (vectorp definition) | |
07d1e73a | 129 | (let ((len (length definition)) (i 0) char mods) |
157d78ec | 130 | (while (< i len) |
0c655b90 | 131 | (insert (if (zerop i) ?\[ ?\s)) |
157d78ec KH |
132 | (setq char (aref definition i) |
133 | i (1+ i)) | |
59f36859 SM |
134 | (if (not (numberp char)) |
135 | (prin1 char (current-buffer)) | |
136 | (princ (prin1-char char) (current-buffer)))) | |
157d78ec KH |
137 | (insert ?\])) |
138 | (prin1 definition (current-buffer)))) | |
45ca32a6 RS |
139 | (insert ")\n") |
140 | (if keys | |
233f0c9f CY |
141 | (let ((keys (where-is-internal (symbol-function macroname) |
142 | '(keymap)))) | |
45ca32a6 RS |
143 | (while keys |
144 | (insert "(global-set-key ") | |
145 | (prin1 (car keys) (current-buffer)) | |
146 | (insert " '") | |
147 | (prin1 macroname (current-buffer)) | |
148 | (insert ")\n") | |
149 | (setq keys (cdr keys))))))) | |
a2535589 | 150 | |
7229064d | 151 | ;;;###autoload |
a2535589 JA |
152 | (defun kbd-macro-query (flag) |
153 | "Query user during kbd macro execution. | |
540671f3 RS |
154 | With prefix argument, enters recursive edit, reading keyboard |
155 | commands even within a kbd macro. You can give different commands | |
156 | each time the macro executes. | |
ba790870 RS |
157 | Without prefix argument, asks whether to continue running the macro. |
158 | Your options are: \\<query-replace-map> | |
159 | \\[act] Finish this iteration normally and continue with the next. | |
160 | \\[skip] Skip the rest of this iteration, and start the next. | |
161 | \\[exit] Stop the macro entirely right now. | |
162 | \\[recenter] Redisplay the screen, then ask again. | |
163 | \\[edit] Enter recursive edit; ask again when you exit from that." | |
a2535589 | 164 | (interactive "P") |
efcf38c7 | 165 | (or executing-kbd-macro |
a2535589 JA |
166 | defining-kbd-macro |
167 | (error "Not defining or executing kbd macro")) | |
168 | (if flag | |
efcf38c7 | 169 | (let (executing-kbd-macro defining-kbd-macro) |
a2535589 | 170 | (recursive-edit)) |
efcf38c7 | 171 | (if (not executing-kbd-macro) |
a2535589 | 172 | nil |
af400c00 RS |
173 | (let ((loop t) |
174 | (msg (substitute-command-keys | |
175 | "Proceed with macro?\\<query-replace-map>\ | |
8693344e | 176 | (\\[act], \\[skip], \\[exit], \\[recenter], \\[edit]) "))) |
a2535589 | 177 | (while loop |
efcf38c7 | 178 | (let ((key (let ((executing-kbd-macro nil) |
af400c00 | 179 | (defining-kbd-macro nil)) |
8700ec61 | 180 | (message "%s" msg) |
af400c00 RS |
181 | (read-event))) |
182 | def) | |
183 | (setq key (vector key)) | |
184 | (setq def (lookup-key query-replace-map key)) | |
185 | (cond ((eq def 'act) | |
a2535589 | 186 | (setq loop nil)) |
af400c00 | 187 | ((eq def 'skip) |
a2535589 | 188 | (setq loop nil) |
efcf38c7 | 189 | (setq executing-kbd-macro "")) |
af400c00 | 190 | ((eq def 'exit) |
a2535589 | 191 | (setq loop nil) |
efcf38c7 | 192 | (setq executing-kbd-macro t)) |
af400c00 | 193 | ((eq def 'recenter) |
a2535589 | 194 | (recenter nil)) |
af400c00 | 195 | ((eq def 'edit) |
efcf38c7 | 196 | (let (executing-kbd-macro defining-kbd-macro) |
af400c00 RS |
197 | (recursive-edit))) |
198 | ((eq def 'quit) | |
199 | (setq quit-flag t)) | |
200 | (t | |
201 | (or (eq def 'help) | |
202 | (ding)) | |
203 | (with-output-to-temp-buffer "*Help*" | |
204 | (princ | |
205 | (substitute-command-keys | |
eb8c3be9 | 206 | "Specify how to proceed with keyboard macro execution. |
af400c00 RS |
207 | Possibilities: \\<query-replace-map> |
208 | \\[act] Finish this iteration normally and continue with the next. | |
209 | \\[skip] Skip the rest of this iteration, and start the next. | |
210 | \\[exit] Stop the macro entirely right now. | |
211 | \\[recenter] Redisplay the screen, then ask again. | |
6ec1c571 | 212 | \\[edit] Enter recursive edit; ask again when you exit from that.")) |
7fdbcd83 | 213 | (with-current-buffer standard-output |
6ec1c571 | 214 | (help-mode))))))))))) |
7229064d | 215 | |
63c86e17 JB |
216 | ;;;###autoload |
217 | (defun apply-macro-to-region-lines (top bottom &optional macro) | |
2b1c8da0 LT |
218 | "Apply last keyboard macro to all lines in the region. |
219 | For each line that begins in the region, move to the beginning of | |
220 | the line, and run the last keyboard macro. | |
63c86e17 JB |
221 | |
222 | When called from lisp, this function takes two arguments TOP and | |
223 | BOTTOM, describing the current region. TOP must be before BOTTOM. | |
224 | The optional third argument MACRO specifies a keyboard macro to | |
225 | execute. | |
226 | ||
227 | This is useful for quoting or unquoting included text, adding and | |
228 | removing comments, or producing tables where the entries are regular. | |
229 | ||
230 | For example, in Usenet articles, sections of text quoted from another | |
231 | author are indented, or have each line start with `>'. To quote a | |
232 | section of text, define a keyboard macro which inserts `>', put point | |
233 | and mark at opposite ends of the quoted section, and use | |
234 | `\\[apply-macro-to-region-lines]' to mark the entire section. | |
235 | ||
236 | Suppose you wanted to build a keyword table in C where each entry | |
237 | looked like this: | |
238 | ||
f1180544 | 239 | { \"foo\", foo_data, foo_function }, |
63c86e17 JB |
240 | { \"bar\", bar_data, bar_function }, |
241 | { \"baz\", baz_data, baz_function }, | |
242 | ||
243 | You could enter the names in this format: | |
244 | ||
245 | foo | |
246 | bar | |
247 | baz | |
248 | ||
249 | and write a macro to massage a word into a table entry: | |
250 | ||
251 | \\C-x ( | |
252 | \\M-d { \"\\C-y\", \\C-y_data, \\C-y_function }, | |
253 | \\C-x ) | |
254 | ||
255 | and then select the region of un-tablified names and use | |
2b1c8da0 | 256 | `\\[apply-macro-to-region-lines]' to build the table from the names." |
63c86e17 | 257 | (interactive "r") |
bcc78bc0 JB |
258 | (or macro |
259 | (progn | |
260 | (if (null last-kbd-macro) | |
55535639 | 261 | (error "No keyboard macro has been defined")) |
bcc78bc0 | 262 | (setq macro last-kbd-macro))) |
63c86e17 | 263 | (save-excursion |
2b1c8da0 | 264 | (let ((end-marker (copy-marker bottom)) |
5277487d | 265 | next-line-marker) |
63c86e17 JB |
266 | (goto-char top) |
267 | (if (not (bolp)) | |
268 | (forward-line 1)) | |
5277487d JB |
269 | (setq next-line-marker (point-marker)) |
270 | (while (< next-line-marker end-marker) | |
271 | (goto-char next-line-marker) | |
3d64136f | 272 | (save-excursion |
5277487d JB |
273 | (forward-line 1) |
274 | (set-marker next-line-marker (point))) | |
275 | (save-excursion | |
f732f824 | 276 | (let ((mark-active nil)) |
b003beb1 | 277 | (execute-kbd-macro macro)))) |
5277487d JB |
278 | (set-marker end-marker nil) |
279 | (set-marker next-line-marker nil)))) | |
63c86e17 | 280 | |
5f5d794a | 281 | ;;;###autoload (define-key ctl-x-map "q" 'kbd-macro-query) |
6594deb0 | 282 | |
896546cd RS |
283 | (provide 'macros) |
284 | ||
cbee283d | 285 | ;; arch-tag: 346ed1a5-1220-4bc8-b533-961ee704361f |
6594deb0 | 286 | ;;; macros.el ends here |