Commit | Line | Data |
---|---|---|
6594deb0 ER |
1 | ;;; macros.el --- non-primitive commands for keyboard macros. |
2 | ||
8f1204db | 3 | ;; Copyright (C) 1985, 1986, 1987, 1992, 1994 Free Software Foundation, Inc. |
a2535589 | 4 | |
9750e079 | 5 | ;; Maintainer: FSF |
e9571d2a | 6 | ;; Keywords: abbrev |
9750e079 | 7 | |
a2535589 JA |
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 | |
e5167999 | 12 | ;; the Free Software Foundation; either version 2, or (at your option) |
a2535589 JA |
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 | |
22 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
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))) |
a2535589 JA |
45 | (error "Function %s is already defined and not a keyboard macro." |
46 | symbol)) | |
47 | (fset symbol last-kbd-macro)) | |
48 | ||
7229064d | 49 | ;;;###autoload |
a2535589 JA |
50 | (defun insert-kbd-macro (macroname &optional keys) |
51 | "Insert in buffer the definition of kbd macro NAME, as Lisp code. | |
540671f3 | 52 | Optional second arg KEYS means also record the keys it is on |
b4b3b736 | 53 | \(this is the prefix argument, when calling interactively). |
a2535589 | 54 | |
540671f3 RS |
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. | |
a2535589 | 60 | |
b4b3b736 | 61 | To save a kbd macro, visit a file of Lisp code such as your `~/.emacs', |
a2535589 JA |
62 | use this command, and then save the file." |
63 | (interactive "CInsert kbd macro (name): \nP") | |
45ca32a6 RS |
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 | (let ((beg (point)) end) | |
74 | (prin1 definition (current-buffer)) | |
75 | (setq end (point-marker)) | |
76 | (goto-char beg) | |
77 | (while (< (point) end) | |
78 | (let ((char (following-char))) | |
41d1fb49 RS |
79 | (cond ((= char 0) |
80 | (delete-region (point) (1+ (point))) | |
81 | (insert "\\C-@")) | |
82 | ((< char 27) | |
45ca32a6 RS |
83 | (delete-region (point) (1+ (point))) |
84 | (insert "\\C-" (+ 96 char))) | |
41d1fb49 RS |
85 | ((< char 32) |
86 | (delete-region (point) (1+ (point))) | |
87 | (insert "\\C-" (+ 64 char))) | |
45ca32a6 RS |
88 | ((< char 127) |
89 | (forward-char 1)) | |
90 | ((= char 127) | |
91 | (delete-region (point) (1+ (point))) | |
92 | (insert "\\C-?")) | |
41d1fb49 RS |
93 | ((= char 128) |
94 | (delete-region (point) (1+ (point))) | |
95 | (insert "\\M-\\C-@")) | |
96 | ((< char 155) | |
97 | (delete-region (point) (1+ (point))) | |
98 | (insert "\\M-\\C-" (- char 32))) | |
45ca32a6 RS |
99 | ((< char 160) |
100 | (delete-region (point) (1+ (point))) | |
41d1fb49 | 101 | (insert "\\M-\\C-" (- char 64))) |
45ca32a6 RS |
102 | ((< char 255) |
103 | (delete-region (point) (1+ (point))) | |
104 | (insert "\\M-" (- char 128))) | |
105 | ((= char 255) | |
106 | (delete-region (point) (1+ (point))) | |
41d1fb49 | 107 | (insert "\\M-\\C-?")))))) |
45ca32a6 RS |
108 | (insert ")\n") |
109 | (if keys | |
16f63554 | 110 | (let ((keys (where-is-internal macroname '(keymap)))) |
45ca32a6 RS |
111 | (while keys |
112 | (insert "(global-set-key ") | |
113 | (prin1 (car keys) (current-buffer)) | |
114 | (insert " '") | |
115 | (prin1 macroname (current-buffer)) | |
116 | (insert ")\n") | |
117 | (setq keys (cdr keys))))))) | |
a2535589 | 118 | |
7229064d | 119 | ;;;###autoload |
a2535589 JA |
120 | (defun kbd-macro-query (flag) |
121 | "Query user during kbd macro execution. | |
540671f3 RS |
122 | With prefix argument, enters recursive edit, reading keyboard |
123 | commands even within a kbd macro. You can give different commands | |
124 | each time the macro executes. | |
ba790870 RS |
125 | Without prefix argument, asks whether to continue running the macro. |
126 | Your options are: \\<query-replace-map> | |
127 | \\[act] Finish this iteration normally and continue with the next. | |
128 | \\[skip] Skip the rest of this iteration, and start the next. | |
129 | \\[exit] Stop the macro entirely right now. | |
130 | \\[recenter] Redisplay the screen, then ask again. | |
131 | \\[edit] Enter recursive edit; ask again when you exit from that." | |
a2535589 JA |
132 | (interactive "P") |
133 | (or executing-macro | |
134 | defining-kbd-macro | |
135 | (error "Not defining or executing kbd macro")) | |
136 | (if flag | |
137 | (let (executing-macro defining-kbd-macro) | |
138 | (recursive-edit)) | |
139 | (if (not executing-macro) | |
140 | nil | |
af400c00 RS |
141 | (let ((loop t) |
142 | (msg (substitute-command-keys | |
143 | "Proceed with macro?\\<query-replace-map>\ | |
8693344e | 144 | (\\[act], \\[skip], \\[exit], \\[recenter], \\[edit]) "))) |
a2535589 | 145 | (while loop |
af400c00 RS |
146 | (let ((key (let ((executing-macro nil) |
147 | (defining-kbd-macro nil)) | |
148 | (message msg) | |
149 | (read-event))) | |
150 | def) | |
151 | (setq key (vector key)) | |
152 | (setq def (lookup-key query-replace-map key)) | |
153 | (cond ((eq def 'act) | |
a2535589 | 154 | (setq loop nil)) |
af400c00 | 155 | ((eq def 'skip) |
a2535589 JA |
156 | (setq loop nil) |
157 | (setq executing-macro "")) | |
af400c00 | 158 | ((eq def 'exit) |
a2535589 JA |
159 | (setq loop nil) |
160 | (setq executing-macro t)) | |
af400c00 | 161 | ((eq def 'recenter) |
a2535589 | 162 | (recenter nil)) |
af400c00 | 163 | ((eq def 'edit) |
a2535589 | 164 | (let (executing-macro defining-kbd-macro) |
af400c00 RS |
165 | (recursive-edit))) |
166 | ((eq def 'quit) | |
167 | (setq quit-flag t)) | |
168 | (t | |
169 | (or (eq def 'help) | |
170 | (ding)) | |
171 | (with-output-to-temp-buffer "*Help*" | |
172 | (princ | |
173 | (substitute-command-keys | |
eb8c3be9 | 174 | "Specify how to proceed with keyboard macro execution. |
af400c00 RS |
175 | Possibilities: \\<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 | ))))))) | |
7229064d | 182 | |
63c86e17 JB |
183 | ;;;###autoload |
184 | (defun apply-macro-to-region-lines (top bottom &optional macro) | |
bcc78bc0 JB |
185 | "For each complete line between point and mark, move to the beginning |
186 | of the line, and run the last keyboard macro. | |
63c86e17 JB |
187 | |
188 | When called from lisp, this function takes two arguments TOP and | |
189 | BOTTOM, describing the current region. TOP must be before BOTTOM. | |
190 | The optional third argument MACRO specifies a keyboard macro to | |
191 | execute. | |
192 | ||
193 | This is useful for quoting or unquoting included text, adding and | |
194 | removing comments, or producing tables where the entries are regular. | |
195 | ||
196 | For example, in Usenet articles, sections of text quoted from another | |
197 | author are indented, or have each line start with `>'. To quote a | |
198 | section of text, define a keyboard macro which inserts `>', put point | |
199 | and mark at opposite ends of the quoted section, and use | |
200 | `\\[apply-macro-to-region-lines]' to mark the entire section. | |
201 | ||
202 | Suppose you wanted to build a keyword table in C where each entry | |
203 | looked like this: | |
204 | ||
205 | { \"foo\", foo_data, foo_function }, | |
206 | { \"bar\", bar_data, bar_function }, | |
207 | { \"baz\", baz_data, baz_function }, | |
208 | ||
209 | You could enter the names in this format: | |
210 | ||
211 | foo | |
212 | bar | |
213 | baz | |
214 | ||
215 | and write a macro to massage a word into a table entry: | |
216 | ||
217 | \\C-x ( | |
218 | \\M-d { \"\\C-y\", \\C-y_data, \\C-y_function }, | |
219 | \\C-x ) | |
220 | ||
221 | and then select the region of un-tablified names and use | |
222 | `\\[apply-macro-to-region-lines]' to build the table from the names. | |
223 | " | |
224 | (interactive "r") | |
bcc78bc0 JB |
225 | (or macro |
226 | (progn | |
227 | (if (null last-kbd-macro) | |
228 | (error "No keyboard macro has been defined.")) | |
229 | (setq macro last-kbd-macro))) | |
63c86e17 JB |
230 | (save-excursion |
231 | (let ((end-marker (progn | |
232 | (goto-char bottom) | |
233 | (beginning-of-line) | |
5277487d JB |
234 | (point-marker))) |
235 | next-line-marker) | |
63c86e17 JB |
236 | (goto-char top) |
237 | (if (not (bolp)) | |
238 | (forward-line 1)) | |
5277487d JB |
239 | (setq next-line-marker (point-marker)) |
240 | (while (< next-line-marker end-marker) | |
241 | (goto-char next-line-marker) | |
3d64136f | 242 | (save-excursion |
5277487d JB |
243 | (forward-line 1) |
244 | (set-marker next-line-marker (point))) | |
245 | (save-excursion | |
246 | (execute-kbd-macro (or macro last-kbd-macro)))) | |
247 | (set-marker end-marker nil) | |
248 | (set-marker next-line-marker nil)))) | |
63c86e17 | 249 | |
5f5d794a | 250 | ;;;###autoload (define-key ctl-x-map "q" 'kbd-macro-query) |
6594deb0 ER |
251 | |
252 | ;;; macros.el ends here |