1 ;;; macros.el --- non-primitive commands for keyboard macros.
3 ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
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
12 ;; the Free Software Foundation; either version 2, or (at your option)
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.
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.
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.
34 (defun name-last-kbd-macro (symbol)
35 "Assign a name to the last keyboard macro defined.
36 Argument SYMBOL is the name to define.
37 The symbol's function definition becomes the keyboard macro string.
38 Such a \"function\" cannot be called from Lisp, but it is a valid editor command."
39 (interactive "SName for last kbd macro: ")
41 (error "No keyboard macro defined"))
43 (not (stringp (symbol-function symbol
)))
44 (not (vectorp (symbol-function symbol
)))
45 (error "Function %s is already defined and not a keyboard macro."
47 (fset symbol last-kbd-macro
))
50 (defun insert-kbd-macro (macroname &optional keys
)
51 "Insert in buffer the definition of kbd macro NAME, as Lisp code.
52 Optional second arg KEYS means also record the keys it is on
53 \(this is the prefix argument, when calling interactively).
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
61 To save a kbd macro, visit a file of Lisp code such as your `~/.emacs',
62 use this command, and then save the file."
63 (interactive "CInsert kbd macro (name): \nP")
65 (if (string= (symbol-name macroname
) "")
67 (setq macroname
'last-kbd-macro definition last-kbd-macro
)
69 (setq definition
(symbol-function macroname
))
71 (prin1 macroname
(current-buffer))
73 (let ((beg (point)) end
)
74 (prin1 definition
(current-buffer))
75 (setq end
(point-marker))
77 (while (< (point) end
)
78 (let ((char (following-char)))
80 (delete-region (point) (1+ (point)))
81 (insert "\\C-" (+ 96 char
)))
85 (delete-region (point) (1+ (point)))
88 (delete-region (point) (1+ (point)))
89 (insert "\\M-C-" (- char
32)))
91 (delete-region (point) (1+ (point)))
92 (insert "\\M-" (- char
128)))
94 (delete-region (point) (1+ (point)))
95 (insert "\\M-C-?"))))))
98 (let ((keys (where-is-internal macroname nil
)))
100 (insert "(global-set-key ")
101 (prin1 (car keys
) (current-buffer))
103 (prin1 macroname
(current-buffer))
105 (setq keys
(cdr keys
)))))))
108 (defun kbd-macro-query (flag)
109 "Query user during kbd macro execution.
110 With prefix argument, enters recursive edit, reading keyboard
111 commands even within a kbd macro. You can give different commands
112 each time the macro executes.
113 Without prefix argument, asks whether to continue running the macro.
114 Your options are: \\<query-replace-map>
115 \\[act] Finish this iteration normally and continue with the next.
116 \\[skip] Skip the rest of this iteration, and start the next.
117 \\[exit] Stop the macro entirely right now.
118 \\[recenter] Redisplay the screen, then ask again.
119 \\[edit] Enter recursive edit; ask again when you exit from that."
123 (error "Not defining or executing kbd macro"))
125 (let (executing-macro defining-kbd-macro
)
127 (if (not executing-macro
)
130 (msg (substitute-command-keys
131 "Proceed with macro?\\<query-replace-map>\
132 (\\[act], \\[skip], \\[exit], \\[recenter], \\[edit]) ")))
134 (let ((key (let ((executing-macro nil
)
135 (defining-kbd-macro nil
))
139 (setq key
(vector key
))
140 (setq def
(lookup-key query-replace-map key
))
145 (setq executing-macro
""))
148 (setq executing-macro t
))
152 (let (executing-macro defining-kbd-macro
)
159 (with-output-to-temp-buffer "*Help*"
161 (substitute-command-keys
162 "Specify how to proceed with keyboard macro execution.
163 Possibilities: \\<query-replace-map>
164 \\[act] Finish this iteration normally and continue with the next.
165 \\[skip] Skip the rest of this iteration, and start the next.
166 \\[exit] Stop the macro entirely right now.
167 \\[recenter] Redisplay the screen, then ask again.
168 \\[edit] Enter recursive edit; ask again when you exit from that."))))
172 (defun apply-macro-to-region-lines (top bottom
&optional macro
)
173 "For each complete line between point and mark, move to the beginning
174 of the line, and run the last keyboard macro.
176 When called from lisp, this function takes two arguments TOP and
177 BOTTOM, describing the current region. TOP must be before BOTTOM.
178 The optional third argument MACRO specifies a keyboard macro to
181 This is useful for quoting or unquoting included text, adding and
182 removing comments, or producing tables where the entries are regular.
184 For example, in Usenet articles, sections of text quoted from another
185 author are indented, or have each line start with `>'. To quote a
186 section of text, define a keyboard macro which inserts `>', put point
187 and mark at opposite ends of the quoted section, and use
188 `\\[apply-macro-to-region-lines]' to mark the entire section.
190 Suppose you wanted to build a keyword table in C where each entry
193 { \"foo\", foo_data, foo_function },
194 { \"bar\", bar_data, bar_function },
195 { \"baz\", baz_data, baz_function },
197 You could enter the names in this format:
203 and write a macro to massage a word into a table entry:
206 \\M-d { \"\\C-y\", \\C-y_data, \\C-y_function },
209 and then select the region of un-tablified names and use
210 `\\[apply-macro-to-region-lines]' to build the table from the names.
215 (if (null last-kbd-macro
)
216 (error "No keyboard macro has been defined."))
217 (setq macro last-kbd-macro
)))
219 (let ((end-marker (progn
227 (setq next-line-marker
(point-marker))
228 (while (< next-line-marker end-marker
)
229 (goto-char next-line-marker
)
232 (set-marker next-line-marker
(point)))
234 (execute-kbd-macro (or macro last-kbd-macro
))))
235 (set-marker end-marker nil
)
236 (set-marker next-line-marker nil
))))
239 (define-key ctl-x-map
"q" 'kbd-macro-query
)
241 ;;; macros.el ends here