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