Commit | Line | Data |
---|---|---|
5a7f5d07 | 1 | /* Keyboard macros. |
bb1c6663 | 2 | |
acaf905b | 3 | Copyright (C) 1985-1986, 1993, 2000-2012 Free Software Foundation, Inc. |
5a7f5d07 JB |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
9ec0b715 | 7 | GNU Emacs is free software: you can redistribute it and/or modify |
5a7f5d07 | 8 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
5a7f5d07 JB |
11 | |
12 | GNU Emacs is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
5a7f5d07 JB |
19 | |
20 | ||
18160b98 | 21 | #include <config.h> |
d7306fe6 | 22 | #include <setjmp.h> |
5a7f5d07 JB |
23 | #include "lisp.h" |
24 | #include "macros.h" | |
25 | #include "commands.h" | |
e5560ff7 | 26 | #include "character.h" |
5a7f5d07 JB |
27 | #include "buffer.h" |
28 | #include "window.h" | |
077d751f | 29 | #include "keyboard.h" |
5a7f5d07 | 30 | |
955cbe7b PE |
31 | Lisp_Object Qexecute_kbd_macro; |
32 | static Lisp_Object Qkbd_macro_termination_hook; | |
5a7f5d07 | 33 | |
d4087e06 RS |
34 | /* Number of successful iterations so far |
35 | for innermost keyboard macro. | |
36 | This is not bound at each level, | |
37 | so after an error, it describes the innermost interrupted macro. */ | |
38 | ||
48e30793 | 39 | EMACS_INT executing_kbd_macro_iterations; |
d4087e06 RS |
40 | |
41 | /* This is the macro that was executing. | |
42 | This is not bound at each level, | |
43 | so after an error, it describes the innermost interrupted macro. | |
44 | We use it only as a kind of flag, so no need to protect it. */ | |
45 | ||
649d952d | 46 | Lisp_Object executing_kbd_macro; |
d4087e06 | 47 | |
971de7fb | 48 | Lisp_Object Fexecute_kbd_macro (Lisp_Object macro, Lisp_Object count, Lisp_Object loopfunc); |
5a7f5d07 | 49 | \f |
f90d3a6b | 50 | DEFUN ("start-kbd-macro", Fstart_kbd_macro, Sstart_kbd_macro, 1, 2, "P", |
70da46c3 PJ |
51 | doc: /* Record subsequent keyboard input, defining a keyboard macro. |
52 | The commands are recorded even as they are executed. | |
53 | Use \\[end-kbd-macro] to finish recording and make the macro available. | |
54 | Use \\[name-last-kbd-macro] to give it a permanent name. | |
55 | Non-nil arg (prefix arg) means append to last macro defined; | |
f90d3a6b | 56 | this begins by re-executing that macro as if you typed it again. |
177c0ea7 | 57 | If optional second arg, NO-EXEC, is non-nil, do not re-execute last |
f90d3a6b | 58 | macro before appending to it. */) |
5842a27b | 59 | (Lisp_Object append, Lisp_Object no_exec) |
5a7f5d07 | 60 | { |
1344aad4 | 61 | if (!NILP (KVAR (current_kboard, defining_kbd_macro))) |
5a7f5d07 JB |
62 | error ("Already defining kbd macro"); |
63 | ||
cd8b5aa3 | 64 | if (!current_kboard->kbd_macro_buffer) |
8b97da83 | 65 | { |
cd8b5aa3 | 66 | current_kboard->kbd_macro_buffer |
9e7c370a | 67 | = (Lisp_Object *)xmalloc (30 * sizeof (Lisp_Object)); |
c86960f0 | 68 | current_kboard->kbd_macro_bufsize = 30; |
8b97da83 | 69 | } |
5a7f5d07 | 70 | update_mode_lines++; |
265a9e55 | 71 | if (NILP (append)) |
5a7f5d07 | 72 | { |
9e7c370a KH |
73 | if (current_kboard->kbd_macro_bufsize > 200) |
74 | { | |
9e7c370a | 75 | current_kboard->kbd_macro_buffer |
96a60349 KH |
76 | = (Lisp_Object *)xrealloc (current_kboard->kbd_macro_buffer, |
77 | 30 * sizeof (Lisp_Object)); | |
6d84508d | 78 | current_kboard->kbd_macro_bufsize = 30; |
9e7c370a | 79 | } |
cd8b5aa3 KH |
80 | current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_buffer; |
81 | current_kboard->kbd_macro_end = current_kboard->kbd_macro_buffer; | |
9e7c370a | 82 | message ("Defining kbd macro..."); |
5a7f5d07 JB |
83 | } |
84 | else | |
85 | { | |
6d84508d PE |
86 | ptrdiff_t i; |
87 | EMACS_INT len; | |
295fff2c | 88 | int cvt; |
f84db7d5 RS |
89 | |
90 | /* Check the type of last-kbd-macro in case Lisp code changed it. */ | |
1344aad4 | 91 | CHECK_VECTOR_OR_STRING (KVAR (current_kboard, Vlast_kbd_macro)); |
f84db7d5 | 92 | |
1344aad4 | 93 | len = XINT (Flength (KVAR (current_kboard, Vlast_kbd_macro))); |
f84db7d5 RS |
94 | |
95 | /* Copy last-kbd-macro into the buffer, in case the Lisp code | |
96 | has put another macro there. */ | |
97 | if (current_kboard->kbd_macro_bufsize < len + 30) | |
98 | { | |
d311d28c PE |
99 | if (PTRDIFF_MAX < MOST_POSITIVE_FIXNUM + 30 |
100 | && PTRDIFF_MAX < len + 30) | |
6d84508d | 101 | memory_full (SIZE_MAX); |
d311d28c PE |
102 | current_kboard->kbd_macro_buffer = |
103 | xpalloc (current_kboard->kbd_macro_buffer, | |
104 | ¤t_kboard->kbd_macro_bufsize, | |
105 | len + 30 - current_kboard->kbd_macro_bufsize, -1, | |
106 | sizeof *current_kboard->kbd_macro_buffer); | |
f84db7d5 | 107 | } |
295fff2c KS |
108 | |
109 | /* Must convert meta modifier when copying string to vector. */ | |
1344aad4 | 110 | cvt = STRINGP (KVAR (current_kboard, Vlast_kbd_macro)); |
f84db7d5 | 111 | for (i = 0; i < len; i++) |
295fff2c KS |
112 | { |
113 | Lisp_Object c; | |
1344aad4 | 114 | c = Faref (KVAR (current_kboard, Vlast_kbd_macro), make_number (i)); |
3499aebf SM |
115 | if (cvt && NATNUMP (c) && (XFASTINT (c) & 0x80)) |
116 | XSETFASTINT (c, CHAR_META | (XFASTINT (c) & ~0x80)); | |
295fff2c KS |
117 | current_kboard->kbd_macro_buffer[i] = c; |
118 | } | |
f84db7d5 RS |
119 | |
120 | current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_buffer + len; | |
121 | current_kboard->kbd_macro_end = current_kboard->kbd_macro_ptr; | |
122 | ||
123 | /* Re-execute the macro we are appending to, | |
124 | for consistency of behavior. */ | |
f90d3a6b | 125 | if (NILP (no_exec)) |
1344aad4 | 126 | Fexecute_kbd_macro (KVAR (current_kboard, Vlast_kbd_macro), |
f90d3a6b | 127 | make_number (1), Qnil); |
f84db7d5 RS |
128 | |
129 | message ("Appending to kbd macro..."); | |
5a7f5d07 | 130 | } |
1344aad4 | 131 | KVAR (current_kboard, defining_kbd_macro) = Qt; |
177c0ea7 | 132 | |
5a7f5d07 JB |
133 | return Qnil; |
134 | } | |
135 | ||
eeb8fc0d KS |
136 | /* Finish defining the current keyboard macro. */ |
137 | ||
138 | void | |
971de7fb | 139 | end_kbd_macro (void) |
eeb8fc0d | 140 | { |
1344aad4 | 141 | KVAR (current_kboard, defining_kbd_macro) = Qnil; |
eeb8fc0d | 142 | update_mode_lines++; |
1344aad4 | 143 | KVAR (current_kboard, Vlast_kbd_macro) |
eeb8fc0d KS |
144 | = make_event_array ((current_kboard->kbd_macro_end |
145 | - current_kboard->kbd_macro_buffer), | |
146 | current_kboard->kbd_macro_buffer); | |
147 | } | |
148 | ||
f90d3a6b | 149 | DEFUN ("end-kbd-macro", Fend_kbd_macro, Send_kbd_macro, 0, 2, "p", |
70da46c3 PJ |
150 | doc: /* Finish defining a keyboard macro. |
151 | The definition was started by \\[start-kbd-macro]. | |
152 | The macro is now available for use via \\[call-last-kbd-macro], | |
153 | or it can be given a name with \\[name-last-kbd-macro] and then invoked | |
154 | under that name. | |
155 | ||
156 | With numeric arg, repeat macro now that many times, | |
157 | counting the definition just completed as the first repetition. | |
f90d3a6b KS |
158 | An argument of zero means repeat until error. |
159 | ||
177c0ea7 | 160 | In Lisp, optional second arg LOOPFUNC may be a function that is called prior to |
f90d3a6b | 161 | each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */) |
5842a27b | 162 | (Lisp_Object repeat, Lisp_Object loopfunc) |
5a7f5d07 | 163 | { |
1344aad4 | 164 | if (NILP (KVAR (current_kboard, defining_kbd_macro))) |
d86ad277 | 165 | error ("Not defining kbd macro"); |
5a7f5d07 | 166 | |
86a3ca5e EN |
167 | if (NILP (repeat)) |
168 | XSETFASTINT (repeat, 1); | |
5a7f5d07 | 169 | else |
b7826503 | 170 | CHECK_NUMBER (repeat); |
5a7f5d07 | 171 | |
1344aad4 | 172 | if (!NILP (KVAR (current_kboard, defining_kbd_macro))) |
5a7f5d07 | 173 | { |
eeb8fc0d | 174 | end_kbd_macro (); |
9e7c370a | 175 | message ("Keyboard macro defined"); |
5a7f5d07 JB |
176 | } |
177 | ||
86a3ca5e | 178 | if (XFASTINT (repeat) == 0) |
1344aad4 | 179 | Fexecute_kbd_macro (KVAR (current_kboard, Vlast_kbd_macro), repeat, loopfunc); |
48e30793 | 180 | else if (XINT (repeat) > 1) |
5a7f5d07 | 181 | { |
86a3ca5e | 182 | XSETINT (repeat, XINT (repeat)-1); |
48e30793 PE |
183 | Fexecute_kbd_macro (KVAR (current_kboard, Vlast_kbd_macro), |
184 | repeat, loopfunc); | |
5a7f5d07 JB |
185 | } |
186 | return Qnil; | |
187 | } | |
188 | ||
189 | /* Store character c into kbd macro being defined */ | |
190 | ||
c3fd8dd5 | 191 | void |
971de7fb | 192 | store_kbd_macro_char (Lisp_Object c) |
5a7f5d07 | 193 | { |
08e1d6df GM |
194 | struct kboard *kb = current_kboard; |
195 | ||
1344aad4 | 196 | if (!NILP (KVAR (kb, defining_kbd_macro))) |
5a7f5d07 | 197 | { |
08e1d6df | 198 | if (kb->kbd_macro_ptr - kb->kbd_macro_buffer == kb->kbd_macro_bufsize) |
5a7f5d07 | 199 | { |
6d84508d | 200 | ptrdiff_t ptr_offset, end_offset, nbytes; |
177c0ea7 | 201 | |
08e1d6df GM |
202 | ptr_offset = kb->kbd_macro_ptr - kb->kbd_macro_buffer; |
203 | end_offset = kb->kbd_macro_end - kb->kbd_macro_buffer; | |
6d84508d PE |
204 | if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *kb->kbd_macro_buffer / 2 |
205 | < kb->kbd_macro_bufsize) | |
206 | memory_full (SIZE_MAX); | |
c86960f0 | 207 | nbytes = kb->kbd_macro_bufsize * (2 * sizeof *kb->kbd_macro_buffer); |
08e1d6df GM |
208 | kb->kbd_macro_buffer |
209 | = (Lisp_Object *) xrealloc (kb->kbd_macro_buffer, nbytes); | |
6d84508d | 210 | kb->kbd_macro_bufsize *= 2; |
08e1d6df GM |
211 | kb->kbd_macro_ptr = kb->kbd_macro_buffer + ptr_offset; |
212 | kb->kbd_macro_end = kb->kbd_macro_buffer + end_offset; | |
5a7f5d07 | 213 | } |
177c0ea7 | 214 | |
08e1d6df | 215 | *kb->kbd_macro_ptr++ = c; |
5a7f5d07 JB |
216 | } |
217 | } | |
218 | ||
219 | /* Declare that all chars stored so far in the kbd macro being defined | |
220 | really belong to it. This is done in between editor commands. */ | |
221 | ||
c3fd8dd5 | 222 | void |
971de7fb | 223 | finalize_kbd_macro_chars (void) |
5a7f5d07 | 224 | { |
cd8b5aa3 | 225 | current_kboard->kbd_macro_end = current_kboard->kbd_macro_ptr; |
5a7f5d07 | 226 | } |
199afd29 | 227 | |
a7ca3326 | 228 | DEFUN ("cancel-kbd-macro-events", Fcancel_kbd_macro_events, |
199afd29 | 229 | Scancel_kbd_macro_events, 0, 0, 0, |
70da46c3 | 230 | doc: /* Cancel the events added to a keyboard macro for this command. */) |
5842a27b | 231 | (void) |
199afd29 RS |
232 | { |
233 | current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_end; | |
6bbd7a29 | 234 | return Qnil; |
199afd29 | 235 | } |
2d5f65a9 KH |
236 | |
237 | DEFUN ("store-kbd-macro-event", Fstore_kbd_macro_event, | |
238 | Sstore_kbd_macro_event, 1, 1, 0, | |
70da46c3 | 239 | doc: /* Store EVENT into the keyboard macro being defined. */) |
5842a27b | 240 | (Lisp_Object event) |
2d5f65a9 KH |
241 | { |
242 | store_kbd_macro_char (event); | |
243 | return Qnil; | |
244 | } | |
5a7f5d07 JB |
245 | \f |
246 | DEFUN ("call-last-kbd-macro", Fcall_last_kbd_macro, Scall_last_kbd_macro, | |
f90d3a6b | 247 | 0, 2, "p", |
70da46c3 PJ |
248 | doc: /* Call the last keyboard macro that you defined with \\[start-kbd-macro]. |
249 | ||
250 | A prefix argument serves as a repeat count. Zero means repeat until error. | |
251 | ||
252 | To make a macro permanent so you can call it even after | |
177c0ea7 | 253 | defining others, use \\[name-last-kbd-macro]. |
f90d3a6b | 254 | |
177c0ea7 | 255 | In Lisp, optional second arg LOOPFUNC may be a function that is called prior to |
f90d3a6b | 256 | each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */) |
5842a27b | 257 | (Lisp_Object prefix, Lisp_Object loopfunc) |
5a7f5d07 | 258 | { |
4315204e RS |
259 | /* Don't interfere with recognition of the previous command |
260 | from before this macro started. */ | |
1344aad4 | 261 | Vthis_command = KVAR (current_kboard, Vlast_command); |
80184dac | 262 | /* C-x z after the macro should repeat the macro. */ |
f2d6a3df | 263 | Vreal_this_command = KVAR (current_kboard, Vlast_kbd_macro); |
4315204e | 264 | |
1344aad4 | 265 | if (! NILP (KVAR (current_kboard, defining_kbd_macro))) |
5a7f5d07 | 266 | error ("Can't execute anonymous macro while defining one"); |
1344aad4 | 267 | else if (NILP (KVAR (current_kboard, Vlast_kbd_macro))) |
5a7f5d07 JB |
268 | error ("No kbd macro has been defined"); |
269 | else | |
1344aad4 | 270 | Fexecute_kbd_macro (KVAR (current_kboard, Vlast_kbd_macro), prefix, loopfunc); |
4315204e RS |
271 | |
272 | /* command_loop_1 sets this to nil before it returns; | |
273 | get back the last command within the macro | |
274 | so that it can be last, again, after we return. */ | |
1344aad4 | 275 | Vthis_command = KVAR (current_kboard, Vlast_command); |
4315204e | 276 | |
5a7f5d07 JB |
277 | return Qnil; |
278 | } | |
279 | ||
649d952d KS |
280 | /* Restore Vexecuting_kbd_macro and executing_kbd_macro_index. |
281 | Called when the unwind-protect in Fexecute_kbd_macro gets invoked. */ | |
d4087e06 | 282 | |
5a7f5d07 | 283 | static Lisp_Object |
971de7fb | 284 | pop_kbd_macro (Lisp_Object info) |
5a7f5d07 JB |
285 | { |
286 | Lisp_Object tem; | |
524ed533 | 287 | Vexecuting_kbd_macro = XCAR (info); |
80184dac | 288 | tem = XCDR (info); |
649d952d | 289 | executing_kbd_macro_index = XINT (XCAR (tem)); |
f2d6a3df | 290 | Vreal_this_command = XCDR (tem); |
2136b408 | 291 | Frun_hooks (1, &Qkbd_macro_termination_hook); |
5a7f5d07 JB |
292 | return Qnil; |
293 | } | |
294 | ||
a7ca3326 | 295 | DEFUN ("execute-kbd-macro", Fexecute_kbd_macro, Sexecute_kbd_macro, 1, 3, 0, |
70da46c3 PJ |
296 | doc: /* Execute MACRO as string of editor command characters. |
297 | If MACRO is a symbol, its function definition is used. | |
177c0ea7 | 298 | COUNT is a repeat count, or nil for once, or 0 for infinite loop. |
f90d3a6b | 299 | |
177c0ea7 | 300 | Optional third arg LOOPFUNC may be a function that is called prior to |
f90d3a6b | 301 | each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */) |
5842a27b | 302 | (Lisp_Object macro, Lisp_Object count, Lisp_Object loopfunc) |
5a7f5d07 JB |
303 | { |
304 | Lisp_Object final; | |
305 | Lisp_Object tem; | |
d311d28c | 306 | ptrdiff_t pdlcount = SPECPDL_INDEX (); |
48e30793 | 307 | EMACS_INT repeat = 1; |
f90d3a6b | 308 | struct gcpro gcpro1, gcpro2; |
48e30793 | 309 | EMACS_INT success_count = 0; |
5a7f5d07 | 310 | |
649d952d | 311 | executing_kbd_macro_iterations = 0; |
4b7da890 | 312 | |
1c8c5693 EN |
313 | if (!NILP (count)) |
314 | { | |
315 | count = Fprefix_numeric_value (count); | |
316 | repeat = XINT (count); | |
317 | } | |
5a7f5d07 | 318 | |
502ddf23 | 319 | final = indirect_function (macro); |
65346ae6 | 320 | if (!STRINGP (final) && !VECTORP (final)) |
d86ad277 | 321 | error ("Keyboard macros must be strings or vectors"); |
5a7f5d07 | 322 | |
524ed533 | 323 | tem = Fcons (Vexecuting_kbd_macro, |
649d952d | 324 | Fcons (make_number (executing_kbd_macro_index), |
f2d6a3df | 325 | Vreal_this_command)); |
5a7f5d07 JB |
326 | record_unwind_protect (pop_kbd_macro, tem); |
327 | ||
f90d3a6b | 328 | GCPRO2 (final, loopfunc); |
5a7f5d07 JB |
329 | do |
330 | { | |
524ed533 | 331 | Vexecuting_kbd_macro = final; |
649d952d KS |
332 | executing_kbd_macro = final; |
333 | executing_kbd_macro_index = 0; | |
5a7f5d07 | 334 | |
1344aad4 | 335 | KVAR (current_kboard, Vprefix_arg) = Qnil; |
f90d3a6b KS |
336 | |
337 | if (!NILP (loopfunc)) | |
338 | { | |
339 | Lisp_Object cont; | |
340 | cont = call0 (loopfunc); | |
341 | if (NILP (cont)) | |
342 | break; | |
343 | } | |
344 | ||
5a7f5d07 | 345 | command_loop_1 (); |
e86f81cc | 346 | |
649d952d | 347 | executing_kbd_macro_iterations = ++success_count; |
d4087e06 | 348 | |
e86f81cc | 349 | QUIT; |
5a7f5d07 | 350 | } |
65346ae6 | 351 | while (--repeat |
524ed533 | 352 | && (STRINGP (Vexecuting_kbd_macro) || VECTORP (Vexecuting_kbd_macro))); |
5a7f5d07 | 353 | |
649d952d | 354 | executing_kbd_macro = Qnil; |
d4087e06 | 355 | |
f2d6a3df | 356 | Vreal_this_command = Vexecuting_kbd_macro; |
b80d5655 | 357 | |
5a7f5d07 | 358 | UNGCPRO; |
52d9c145 | 359 | return unbind_to (pdlcount, Qnil); |
5a7f5d07 JB |
360 | } |
361 | \f | |
c3fd8dd5 | 362 | void |
971de7fb | 363 | init_macros (void) |
5a7f5d07 | 364 | { |
524ed533 | 365 | Vexecuting_kbd_macro = Qnil; |
649d952d | 366 | executing_kbd_macro = Qnil; |
5a7f5d07 JB |
367 | } |
368 | ||
c3fd8dd5 | 369 | void |
971de7fb | 370 | syms_of_macros (void) |
5a7f5d07 | 371 | { |
cd3520a4 | 372 | DEFSYM (Qexecute_kbd_macro, "execute-kbd-macro"); |
bb1c6663 GM |
373 | |
374 | DEFVAR_LISP ("kbd-macro-termination-hook", Vkbd_macro_termination_hook, | |
375 | doc: /* Normal hook run whenever a keyboard macro terminates. | |
376 | This is run whether the macro ends normally or prematurely due to an error. */); | |
377 | Vkbd_macro_termination_hook = Qnil; | |
cd3520a4 | 378 | DEFSYM (Qkbd_macro_termination_hook, "kbd-macro-termination-hook"); |
5a7f5d07 JB |
379 | |
380 | defsubr (&Sstart_kbd_macro); | |
381 | defsubr (&Send_kbd_macro); | |
382 | defsubr (&Scall_last_kbd_macro); | |
383 | defsubr (&Sexecute_kbd_macro); | |
199afd29 | 384 | defsubr (&Scancel_kbd_macro_events); |
2d5f65a9 | 385 | defsubr (&Sstore_kbd_macro_event); |
5a7f5d07 | 386 | |
cd8b5aa3 | 387 | DEFVAR_KBOARD ("defining-kbd-macro", defining_kbd_macro, |
6cf374b1 LT |
388 | doc: /* Non-nil while a keyboard macro is being defined. Don't set this! |
389 | The value is the symbol `append' while appending to the definition of | |
390 | an existing macro. */); | |
5a7f5d07 | 391 | |
29208e82 | 392 | DEFVAR_LISP ("executing-kbd-macro", Vexecuting_kbd_macro, |
524ed533 RS |
393 | doc: /* Currently executing keyboard macro (string or vector). |
394 | This is nil when not executing a keyboard macro. */); | |
5a7f5d07 | 395 | |
29208e82 | 396 | DEFVAR_INT ("executing-kbd-macro-index", executing_kbd_macro_index, |
64799df5 KS |
397 | doc: /* Index in currently executing keyboard macro; undefined if none executing. */); |
398 | ||
cd8b5aa3 | 399 | DEFVAR_KBOARD ("last-kbd-macro", Vlast_kbd_macro, |
70da46c3 | 400 | doc: /* Last kbd macro defined, as a string or vector; nil if none defined. */); |
5a7f5d07 | 401 | } |