0
|
1 ;;; macros.el --- non-primitive commands for keyboard macros.
|
|
2
|
|
3 ;; Copyright (C) 1985, 86, 87, 92, 94, 95 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Maintainer: FSF
|
|
6 ;; Keywords: abbrev
|
|
7
|
|
8 ;; This file is part of XEmacs.
|
|
9
|
|
10 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
11 ;; 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.
|
|
14
|
|
15 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
18 ;; General Public License for more details.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
16
|
21 ;; along with XEmacs; 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.
|
0
|
24
|
|
25 ;;; Synched up with: FSF 19.30.
|
|
26
|
|
27 ;;; Commentary:
|
|
28
|
|
29 ;; Extension commands for keyboard macros. These permit you to assign
|
|
30 ;; a name to the last-defined keyboard macro, expand and insert the
|
|
31 ;; lisp corresponding to a macro, query the user from within a macro,
|
|
32 ;; or apply a macro to each line in the reason.
|
|
33
|
|
34 ;;; Code:
|
|
35
|
|
36 ;;;###autoload
|
|
37 (defun name-last-kbd-macro (symbol)
|
|
38 "Assign a name to the last keyboard macro defined.
|
|
39 Argument SYMBOL is the name to define.
|
|
40 The symbol's function definition becomes the keyboard macro string.
|
|
41 Such a \"function\" cannot be called from Lisp, but it is a valid
|
|
42 editor command."
|
|
43 (interactive "SName for last kbd macro: ")
|
|
44 (or last-kbd-macro
|
|
45 (error "No keyboard macro defined"))
|
|
46 (and (fboundp symbol)
|
|
47 (not (stringp (symbol-function symbol)))
|
|
48 (not (vectorp (symbol-function symbol)))
|
|
49 (error "Function %s is already defined and not a keyboard macro."
|
|
50 symbol))
|
|
51 (fset symbol last-kbd-macro))
|
|
52
|
|
53 (defun insert-kbd-macro-pretty-string (string)
|
|
54 ;; Convert control characters to the traditional readable representation:
|
|
55 ;; put the four characters \M-x in the buffer instead of the one char \370,
|
|
56 ;; which would deceptively print as `oslash' with the default settings.
|
|
57 (save-restriction
|
|
58 (narrow-to-region (point) (point))
|
|
59 (prin1 string (current-buffer))
|
|
60 (goto-char (1+ (point-min)))
|
|
61 (while (not (eobp))
|
|
62 (cond ((= (following-char) 0) (insert "\\C-@") (delete-char 1))
|
|
63 ((= (following-char) ?\n) (insert "\\n") (delete-char 1))
|
|
64 ((= (following-char) ?\r) (insert "\\r") (delete-char 1))
|
|
65 ((= (following-char) ?\t) (insert "\\t") (delete-char 1))
|
|
66 ((= (following-char) ?\e) (insert "\\e") (delete-char 1))
|
|
67 ((= (following-char) 127) (insert "\\C-?") (delete-char 1))
|
|
68 ((= (following-char) 128) (insert "\\M-\\C-@") (delete-char 1))
|
|
69 ((= (following-char) 255) (insert "\\M-\\C-?") (delete-char 1))
|
|
70 ((and (> (following-char) 127) (< (following-char) 155))
|
|
71 (insert "\\M-\\C-")
|
|
72 (insert (- (following-char) 32))
|
|
73 (delete-char 1)
|
|
74 (forward-char -1))
|
|
75 ((and (>= (following-char) 155) (< (following-char) 160))
|
|
76 (insert "\\M-\\C-")
|
|
77 (insert (- (following-char) 64))
|
|
78 (delete-char 1)
|
|
79 (forward-char -1))
|
|
80 ((>= (following-char) 160)
|
|
81 (insert "\\M-")
|
|
82 (insert (- (following-char) 128))
|
|
83 (delete-char 1)
|
|
84 (forward-char -1))
|
|
85 ((< (following-char) 27)
|
|
86 ;;(insert "\\^") (insert (+ (following-char) 64))
|
|
87 (insert "\\C-") (insert (+ (following-char) 96))
|
|
88 (delete-char 1)
|
|
89 (forward-char -1))
|
|
90 ((< (following-char) 32)
|
|
91 ;;(insert "\\^") (insert (+ (following-char) 64))
|
|
92 (insert "\\C-") (insert (+ (following-char) 64))
|
|
93 (delete-char 1)
|
|
94 (forward-char -1))
|
|
95 (t
|
|
96 (forward-char 1))))))
|
|
97
|
|
98 ;;;###autoload
|
|
99 (defun insert-kbd-macro (macroname &optional keys)
|
|
100 "Insert in buffer the definition of kbd macro NAME, as Lisp code.
|
|
101 Optional second argument KEYS means also record the keys it is on
|
|
102 \(this is the prefix argument, when calling interactively).
|
|
103
|
|
104 This Lisp code will, when executed, define the kbd macro with the
|
|
105 same definition it has now. If you say to record the keys,
|
|
106 the Lisp code will also rebind those keys to the macro.
|
|
107 Only global key bindings are recorded since executing this Lisp code
|
|
108 always makes global bindings.
|
|
109
|
|
110 To save a kbd macro, visit a file of Lisp code such as your `~/.emacs',
|
|
111 use this command, and then save the file."
|
|
112 (interactive "CInsert kbd macro (name): \nP")
|
|
113 (let (definition)
|
|
114 (if (string= (symbol-name macroname) "")
|
|
115 (progn
|
|
116 (setq macroname 'last-kbd-macro
|
|
117 definition last-kbd-macro)
|
|
118 (insert "(setq "))
|
|
119 (progn
|
|
120 (setq definition (symbol-function macroname))
|
|
121 (insert "(fset '")))
|
|
122 (prin1 macroname (current-buffer))
|
|
123 (insert "\n ")
|
|
124 (let ((string (events-to-keys definition t)))
|
|
125 (if (stringp string)
|
|
126 (insert-kbd-macro-pretty-string string)
|
|
127 (prin1 string (current-buffer))))
|
|
128 (insert ")\n")
|
|
129 (if keys
|
|
130 (let ((keys (where-is-internal macroname)))
|
|
131 (while keys
|
|
132 (insert "(global-set-key ")
|
|
133 (prin1 (car keys) (current-buffer))
|
|
134 (insert " '")
|
|
135 (prin1 macroname (current-buffer))
|
|
136 (insert ")\n")
|
|
137 (setq keys (cdr keys)))))))
|
|
138
|
|
139 ;;;###autoload
|
|
140 (defun kbd-macro-query (flag)
|
|
141 "Query user during kbd macro execution.
|
|
142 With prefix argument, enters recursive edit,
|
|
143 reading keyboard commands even within a kbd macro.
|
|
144 You can give different commands each time the macro executes.
|
|
145 Without prefix argument, asks whether to continue running the macro.
|
|
146 Your options are: \\<query-replace-map>
|
|
147 \\[act] Finish this iteration normally and continue with the next.
|
|
148 \\[skip] Skip the rest of this iteration, and start the next.
|
|
149 \\[exit] Stop the macro entirely right now.
|
|
150 \\[recenter] Redisplay the frame, then ask again.
|
|
151 \\[edit] Enter recursive edit; ask again when you exit from that."
|
|
152 (interactive "P")
|
|
153 (or executing-macro
|
|
154 defining-kbd-macro
|
|
155 (error "Not defining or executing kbd macro"))
|
|
156 (if flag
|
|
157 (let (executing-macro defining-kbd-macro)
|
|
158 (recursive-edit))
|
|
159 (if (not executing-macro)
|
|
160 nil
|
|
161 (let ((loop t)
|
|
162 (msg (substitute-command-keys
|
|
163 "Proceed with macro?\\<query-replace-map>\
|
|
164 (\\[act], \\[skip], \\[exit], \\[recenter], \\[edit]) ")))
|
|
165 (while loop
|
|
166 (let ((key (let ((executing-macro nil)
|
|
167 (defining-kbd-macro nil))
|
|
168 (message msg)
|
|
169 (read-char)))
|
|
170 def)
|
|
171 (setq key (vector key))
|
|
172 (setq def (lookup-key query-replace-map key))
|
|
173 (cond ((eq def 'act)
|
|
174 (setq loop nil))
|
|
175 ((eq def 'skip)
|
|
176 (setq loop nil)
|
|
177 (setq executing-macro ""))
|
|
178 ((eq def 'exit)
|
|
179 (setq loop nil)
|
|
180 (setq executing-macro t))
|
|
181 ((eq def 'recenter)
|
|
182 (recenter nil))
|
|
183 ((eq def 'edit)
|
|
184 (let (executing-macro defining-kbd-macro)
|
|
185 (recursive-edit)))
|
|
186 ((eq def 'quit)
|
|
187 (setq quit-flag t))
|
|
188 (t
|
|
189 (or (eq def 'help)
|
|
190 (ding))
|
|
191 (with-output-to-temp-buffer "*Help*"
|
|
192 (princ
|
|
193 (substitute-command-keys
|
|
194 "Specify how to proceed with keyboard macro execution.
|
|
195 Possibilities: \\<query-replace-map>
|
|
196 \\[act] Finish this iteration normally and continue with the next.
|
|
197 \\[skip] Skip the rest of this iteration, and start the next.
|
|
198 \\[exit] Stop the macro entirely right now.
|
|
199 \\[recenter] Redisplay the frame, then ask again.
|
|
200 \\[edit] Enter recursive edit; ask again when you exit from that."))
|
|
201 (save-excursion
|
|
202 (set-buffer standard-output)
|
|
203 (help-mode)))))))))))
|
|
204
|
|
205 ;;;###autoload
|
|
206 (defun apply-macro-to-region-lines (top bottom &optional macro)
|
|
207 "For each complete line between point and mark, move to the beginning
|
|
208 of the line, and run the last keyboard macro.
|
|
209
|
|
210 When called from lisp, this function takes two arguments TOP and
|
|
211 BOTTOM, describing the current region. TOP must be before BOTTOM.
|
|
212 The optional third argument MACRO specifies a keyboard macro to
|
|
213 execute.
|
|
214
|
|
215 This is useful for quoting or unquoting included text, adding and
|
|
216 removing comments, or producing tables where the entries are regular.
|
|
217
|
|
218 For example, in Usenet articles, sections of text quoted from another
|
|
219 author are indented, or have each line start with `>'. To quote a
|
|
220 section of text, define a keyboard macro which inserts `>', put point
|
|
221 and mark at opposite ends of the quoted section, and use
|
|
222 `\\[apply-macro-to-region-lines]' to mark the entire section.
|
|
223
|
|
224 Suppose you wanted to build a keyword table in C where each entry
|
|
225 looked like this:
|
|
226
|
|
227 { \"foo\", foo_data, foo_function },
|
|
228 { \"bar\", bar_data, bar_function },
|
|
229 { \"baz\", baz_data, baz_function },
|
|
230
|
|
231 You could enter the names in this format:
|
|
232
|
|
233 foo
|
|
234 bar
|
|
235 baz
|
|
236
|
|
237 and write a macro to massage a word into a table entry:
|
|
238
|
|
239 \\C-x (
|
|
240 \\M-d { \"\\C-y\", \\C-y_data, \\C-y_function },
|
|
241 \\C-x )
|
|
242
|
|
243 and then select the region of un-tablified names and use
|
|
244 `\\[apply-macro-to-region-lines]' to build the table from the names.
|
|
245 "
|
|
246 (interactive "r")
|
|
247 (or macro
|
|
248 (progn
|
|
249 (if (null last-kbd-macro)
|
|
250 (error "No keyboard macro has been defined."))
|
|
251 (setq macro last-kbd-macro)))
|
|
252 (save-excursion
|
|
253 (let ((end-marker (progn
|
|
254 (goto-char bottom)
|
|
255 (beginning-of-line)
|
|
256 (point-marker)))
|
|
257 next-line-marker)
|
|
258 (goto-char top)
|
|
259 (if (not (bolp))
|
|
260 (forward-line 1))
|
|
261 (setq next-line-marker (point-marker))
|
|
262 (while (< next-line-marker end-marker)
|
|
263 (goto-char next-line-marker)
|
|
264 (save-excursion
|
|
265 (forward-line 1)
|
|
266 (set-marker next-line-marker (point)))
|
|
267 (save-excursion
|
|
268 (execute-kbd-macro (or macro last-kbd-macro))))
|
|
269 (set-marker end-marker nil)
|
|
270 (set-marker next-line-marker nil))))
|
|
271
|
|
272 ;;; macros.el ends here
|