1 ;;; macros.el --- non-primitive commands for keyboard macros -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985-1987, 1992, 1994-1995, 2001-2017 Free Software
6 ;; Maintainer: emacs-devel@gnu.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
27 ;; Extension commands for keyboard macros. These permit you to assign
28 ;; a name to the last-defined keyboard macro, expand and insert the
29 ;; lisp corresponding to a macro, query the user from within a macro,
30 ;; or apply a macro to each line in the reason.
37 (defalias 'name-last-kbd-macro
#'kmacro-name-last-macro
)
40 (defun insert-kbd-macro (macroname &optional keys
)
41 "Insert in buffer the definition of kbd macro MACRONAME, as Lisp code.
42 MACRONAME should be a symbol.
43 Optional second arg KEYS means also record the keys it is on
44 \(this is the prefix argument, when calling interactively).
46 This Lisp code will, when executed, define the kbd macro with the same
47 definition it has now. If you say to record the keys, the Lisp code
48 will also rebind those keys to the macro. Only global key bindings
49 are recorded since executing this Lisp code always makes global
52 To save a kbd macro, visit a file of Lisp code such as your `~/.emacs',
53 use this command, and then save the file."
54 (interactive (list (intern (completing-read "Insert kbd macro (name): "
56 #'kmacro-keyboard-macro-p
60 (if (string= (symbol-name macroname
) "")
62 (setq macroname
'last-kbd-macro definition last-kbd-macro
)
64 (setq definition
(symbol-function macroname
))
66 (prin1 macroname
(current-buffer))
68 (if (stringp definition
)
69 (let ((beg (point)) end
)
70 (prin1 definition
(current-buffer))
71 (setq end
(point-marker))
73 (while (< (point) end
)
74 (let ((char (following-char)))
76 (delete-region (point) (1+ (point)))
79 (delete-region (point) (1+ (point)))
80 (insert "\\C-" (+ 96 char
)))
82 (delete-region (point) (1+ (point)))
85 (delete-region (point) (1+ (point)))
86 (insert "\\C-" (+ 64 char
)))
90 (delete-region (point) (1+ (point)))
93 (delete-region (point) (1+ (point)))
95 ((= char
(aref "\M-\C-\\" 0))
96 (delete-region (point) (1+ (point)))
97 (insert "\\M-\\C-\\\\"))
99 (delete-region (point) (1+ (point)))
100 (insert "\\M-\\C-" (- char
32)))
102 (delete-region (point) (1+ (point)))
103 (insert "\\M-\\C-" (- char
64)))
104 ((= char
(aref "\M-\\" 0))
105 (delete-region (point) (1+ (point)))
108 (delete-region (point) (1+ (point)))
109 (insert "\\M-" (- char
128)))
111 (delete-region (point) (1+ (point)))
112 (insert "\\M-\\C-?"))))))
113 (if (vectorp definition
)
114 (let ((len (length definition
)) (i 0) char
)
116 (insert (if (zerop i
) ?\
[ ?\s
))
117 (setq char
(aref definition i
)
119 (if (not (numberp char
))
120 (prin1 char
(current-buffer))
121 (princ (prin1-char char
) (current-buffer))))
123 ;; FIXME: For kmacros, we shouldn't write the (lambda ...)
124 ;; gunk but instead we should write something more abstract like
125 ;; (kmacro-create [<keys>] 0 "%d").
126 (prin1 definition
(current-buffer))))
129 (let ((keys (or (where-is-internal (symbol-function macroname
)
131 (where-is-internal macroname
'(keymap)))))
133 (insert "(global-set-key ")
134 (prin1 (car keys
) (current-buffer))
136 (prin1 macroname
(current-buffer))
138 (setq keys
(cdr keys
)))))))
141 (defun kbd-macro-query (flag)
142 "Query user during kbd macro execution.
143 With prefix argument, enters recursive edit, reading keyboard
144 commands even within a kbd macro. You can give different commands
145 each time the macro executes.
146 Without prefix argument, asks whether to continue running the macro.
147 Your options are: \\<query-replace-map>
148 \\[act] Finish this iteration normally and continue with the next.
149 \\[skip] Skip the rest of this iteration, and start the next.
150 \\[exit] Stop the macro entirely right now.
151 \\[recenter] Redisplay the screen, then ask again.
152 \\[edit] Enter recursive edit; ask again when you exit from that."
154 (or executing-kbd-macro
156 (user-error "Not defining or executing kbd macro"))
158 (let (executing-kbd-macro defining-kbd-macro
)
160 (if (not executing-kbd-macro
)
163 (msg (substitute-command-keys
164 "Proceed with macro?\\<query-replace-map>\
165 (\\[act], \\[skip], \\[exit], \\[recenter], \\[edit]) ")))
167 (let ((key (let ((executing-kbd-macro nil
)
168 (defining-kbd-macro nil
))
172 (setq key
(vector key
))
173 (setq def
(lookup-key query-replace-map key
))
178 (setq executing-kbd-macro
""))
181 (setq executing-kbd-macro t
))
185 (let (executing-kbd-macro defining-kbd-macro
)
192 (with-output-to-temp-buffer "*Help*"
194 (substitute-command-keys
195 "Specify how to proceed with keyboard macro execution.
196 Possibilities: \\<query-replace-map>
197 \\[act] Finish this iteration normally and continue with the next.
198 \\[skip] Skip the rest of this iteration, and start the next.
199 \\[exit] Stop the macro entirely right now.
200 \\[recenter] Redisplay the screen, then ask again.
201 \\[edit] Enter recursive edit; ask again when you exit from that."))
202 (with-current-buffer standard-output
203 (help-mode)))))))))))
206 (defun apply-macro-to-region-lines (top bottom
&optional macro
)
207 "Apply last keyboard macro to all lines in the region.
208 For each line that begins in the region, move to the beginning of
209 the line, and run the last keyboard macro.
211 When called from lisp, this function takes two arguments TOP and
212 BOTTOM, describing the current region. TOP must be before BOTTOM.
213 The optional third argument MACRO specifies a keyboard macro to
216 This is useful for quoting or unquoting included text, adding and
217 removing comments, or producing tables where the entries are regular.
219 For example, in Usenet articles, sections of text quoted from another
220 author are indented, or have each line start with `>'. To quote a
221 section of text, define a keyboard macro which inserts `>', put point
222 and mark at opposite ends of the quoted section, and use
223 `\\[apply-macro-to-region-lines]' to mark the entire section.
225 Suppose you wanted to build a keyword table in C where each entry
228 { \"foo\", foo_data, foo_function },
229 { \"bar\", bar_data, bar_function },
230 { \"baz\", baz_data, baz_function },
232 You could enter the names in this format:
238 and write a macro to massage a word into a table entry:
241 \\M-d { \"\\C-y\", \\C-y_data, \\C-y_function },
244 and then select the region of un-tablified names and use
245 `\\[apply-macro-to-region-lines]' to build the table from the names."
249 (if (null last-kbd-macro
)
250 (user-error "No keyboard macro has been defined"))
251 (setq macro last-kbd-macro
)))
253 (let ((end-marker (copy-marker bottom
))
258 (setq next-line-marker
(point-marker))
259 (while (< next-line-marker end-marker
)
260 (goto-char next-line-marker
)
263 (set-marker next-line-marker
(point)))
265 (let ((mark-active nil
))
266 (execute-kbd-macro macro
))))
267 (set-marker end-marker nil
)
268 (set-marker next-line-marker nil
))))
270 ;;;###autoload (define-key ctl-x-map "q" 'kbd-macro-query)
274 ;;; macros.el ends here