* admin/gitmerge.el (gitmerge-missing):
[emacs.git] / lisp / macros.el
blob5583c02f68b30cb6a9a862e530ebe4eb5676f093
1 ;;; macros.el --- non-primitive commands for keyboard macros -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985-1987, 1992, 1994-1995, 2001-2017 Free Software
4 ;; Foundation, Inc.
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: abbrev
8 ;; Package: emacs
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/>.
25 ;;; Commentary:
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.
32 ;;; Code:
34 (require 'kmacro)
36 ;;;###autoload
37 (defalias 'name-last-kbd-macro #'kmacro-name-last-macro)
39 ;;;###autoload
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
50 bindings.
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): "
55 obarray
56 #'kmacro-keyboard-macro-p
57 t))
58 current-prefix-arg))
59 (let (definition)
60 (if (string= (symbol-name macroname) "")
61 (progn
62 (setq macroname 'last-kbd-macro definition last-kbd-macro)
63 (insert "(setq "))
64 (setq definition (symbol-function macroname))
65 (insert "(fset '"))
66 (prin1 macroname (current-buffer))
67 (insert "\n ")
68 (if (stringp definition)
69 (let ((beg (point)) end)
70 (prin1 definition (current-buffer))
71 (setq end (point-marker))
72 (goto-char beg)
73 (while (< (point) end)
74 (let ((char (following-char)))
75 (cond ((= char 0)
76 (delete-region (point) (1+ (point)))
77 (insert "\\C-@"))
78 ((< char 27)
79 (delete-region (point) (1+ (point)))
80 (insert "\\C-" (+ 96 char)))
81 ((= char ?\C-\\)
82 (delete-region (point) (1+ (point)))
83 (insert "\\C-\\\\"))
84 ((< char 32)
85 (delete-region (point) (1+ (point)))
86 (insert "\\C-" (+ 64 char)))
87 ((< char 127)
88 (forward-char 1))
89 ((= char 127)
90 (delete-region (point) (1+ (point)))
91 (insert "\\C-?"))
92 ((= char 128)
93 (delete-region (point) (1+ (point)))
94 (insert "\\M-\\C-@"))
95 ((= char (aref "\M-\C-\\" 0))
96 (delete-region (point) (1+ (point)))
97 (insert "\\M-\\C-\\\\"))
98 ((< char 155)
99 (delete-region (point) (1+ (point)))
100 (insert "\\M-\\C-" (- char 32)))
101 ((< char 160)
102 (delete-region (point) (1+ (point)))
103 (insert "\\M-\\C-" (- char 64)))
104 ((= char (aref "\M-\\" 0))
105 (delete-region (point) (1+ (point)))
106 (insert "\\M-\\\\"))
107 ((< char 255)
108 (delete-region (point) (1+ (point)))
109 (insert "\\M-" (- char 128)))
110 ((= char 255)
111 (delete-region (point) (1+ (point)))
112 (insert "\\M-\\C-?"))))))
113 (if (vectorp definition)
114 (let ((len (length definition)) (i 0) char)
115 (while (< i len)
116 (insert (if (zerop i) ?\[ ?\s))
117 (setq char (aref definition i)
118 i (1+ i))
119 (if (not (numberp char))
120 (prin1 char (current-buffer))
121 (princ (prin1-char char) (current-buffer))))
122 (insert ?\]))
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))))
127 (insert ")\n")
128 (if keys
129 (let ((keys (or (where-is-internal (symbol-function macroname)
130 '(keymap))
131 (where-is-internal macroname '(keymap)))))
132 (while keys
133 (insert "(global-set-key ")
134 (prin1 (car keys) (current-buffer))
135 (insert " '")
136 (prin1 macroname (current-buffer))
137 (insert ")\n")
138 (setq keys (cdr keys)))))))
140 ;;;###autoload
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."
153 (interactive "P")
154 (or executing-kbd-macro
155 defining-kbd-macro
156 (user-error "Not defining or executing kbd macro"))
157 (if flag
158 (let (executing-kbd-macro defining-kbd-macro)
159 (recursive-edit))
160 (if (not executing-kbd-macro)
162 (let ((loop t)
163 (msg (substitute-command-keys
164 "Proceed with macro?\\<query-replace-map>\
165 (\\[act], \\[skip], \\[exit], \\[recenter], \\[edit]) ")))
166 (while loop
167 (let ((key (let ((executing-kbd-macro nil)
168 (defining-kbd-macro nil))
169 (message "%s" msg)
170 (read-event)))
171 def)
172 (setq key (vector key))
173 (setq def (lookup-key query-replace-map key))
174 (cond ((eq def 'act)
175 (setq loop nil))
176 ((eq def 'skip)
177 (setq loop nil)
178 (setq executing-kbd-macro ""))
179 ((eq def 'exit)
180 (setq loop nil)
181 (setq executing-kbd-macro t))
182 ((eq def 'recenter)
183 (recenter nil))
184 ((eq def 'edit)
185 (let (executing-kbd-macro defining-kbd-macro)
186 (recursive-edit)))
187 ((eq def 'quit)
188 (setq quit-flag t))
190 (or (eq def 'help)
191 (ding))
192 (with-output-to-temp-buffer "*Help*"
193 (princ
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)))))))))))
205 ;;;###autoload
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
214 execute.
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
226 looked like this:
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:
240 \\C-x (
241 \\M-d { \"\\C-y\", \\C-y_data, \\C-y_function },
242 \\C-x )
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."
246 (interactive "r")
247 (or macro
248 (progn
249 (if (null last-kbd-macro)
250 (user-error "No keyboard macro has been defined"))
251 (setq macro last-kbd-macro)))
252 (save-excursion
253 (let ((end-marker (copy-marker bottom))
254 next-line-marker)
255 (goto-char top)
256 (if (not (bolp))
257 (forward-line 1))
258 (setq next-line-marker (point-marker))
259 (while (< next-line-marker end-marker)
260 (goto-char next-line-marker)
261 (save-excursion
262 (forward-line 1)
263 (set-marker next-line-marker (point)))
264 (save-excursion
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)
272 (provide 'macros)
274 ;;; macros.el ends here