Improve responsiveness while in 'replace-buffer-contents'
[emacs.git] / src / macros.c
blobb1fc7a037f45793aa422552efef2bc42a411941c
1 /* Keyboard macros.
3 Copyright (C) 1985-1986, 1993, 2000-2018 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #include "lisp.h"
24 #include "macros.h"
25 #include "window.h"
26 #include "keyboard.h"
28 /* Number of successful iterations so far
29 for innermost keyboard macro.
30 This is not bound at each level,
31 so after an error, it describes the innermost interrupted macro. */
33 EMACS_INT executing_kbd_macro_iterations;
35 /* This is the macro that was executing.
36 This is not bound at each level,
37 so after an error, it describes the innermost interrupted macro.
38 We use it only as a kind of flag, so no need to protect it. */
40 Lisp_Object executing_kbd_macro;
42 DEFUN ("start-kbd-macro", Fstart_kbd_macro, Sstart_kbd_macro, 1, 2, "P",
43 doc: /* Record subsequent keyboard input, defining a keyboard macro.
44 The commands are recorded even as they are executed.
45 Use \\[end-kbd-macro] to finish recording and make the macro available.
46 Use \\[name-last-kbd-macro] to give it a permanent name.
47 Non-nil arg (prefix arg) means append to last macro defined;
48 this begins by re-executing that macro as if you typed it again.
49 If optional second arg, NO-EXEC, is non-nil, do not re-execute last
50 macro before appending to it. */)
51 (Lisp_Object append, Lisp_Object no_exec)
53 if (!NILP (KVAR (current_kboard, defining_kbd_macro)))
54 error ("Already defining kbd macro");
56 if (!current_kboard->kbd_macro_buffer)
58 current_kboard->kbd_macro_buffer = xmalloc (30 * word_size);
59 current_kboard->kbd_macro_bufsize = 30;
60 current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_buffer;
61 current_kboard->kbd_macro_end = current_kboard->kbd_macro_buffer;
63 update_mode_lines = 19;
64 if (NILP (append))
66 if (current_kboard->kbd_macro_bufsize > 200)
68 current_kboard->kbd_macro_buffer
69 = xrealloc (current_kboard->kbd_macro_buffer,
70 30 * word_size);
71 current_kboard->kbd_macro_bufsize = 30;
73 current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_buffer;
74 current_kboard->kbd_macro_end = current_kboard->kbd_macro_buffer;
75 message1 ("Defining kbd macro...");
77 else
79 int incr = 30;
80 ptrdiff_t i, len;
81 bool cvt;
83 /* Check the type of last-kbd-macro in case Lisp code changed it. */
84 len = CHECK_VECTOR_OR_STRING (KVAR (current_kboard, Vlast_kbd_macro));
86 /* Copy last-kbd-macro into the buffer, in case the Lisp code
87 has put another macro there. */
88 if (current_kboard->kbd_macro_bufsize - incr < len)
89 current_kboard->kbd_macro_buffer =
90 xpalloc (current_kboard->kbd_macro_buffer,
91 &current_kboard->kbd_macro_bufsize,
92 len - current_kboard->kbd_macro_bufsize + incr, -1,
93 sizeof *current_kboard->kbd_macro_buffer);
95 /* Must convert meta modifier when copying string to vector. */
96 cvt = STRINGP (KVAR (current_kboard, Vlast_kbd_macro));
97 for (i = 0; i < len; i++)
99 Lisp_Object c;
100 c = Faref (KVAR (current_kboard, Vlast_kbd_macro), make_number (i));
101 if (cvt && NATNUMP (c) && (XFASTINT (c) & 0x80))
102 XSETFASTINT (c, CHAR_META | (XFASTINT (c) & ~0x80));
103 current_kboard->kbd_macro_buffer[i] = c;
106 current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_buffer + len;
107 current_kboard->kbd_macro_end = current_kboard->kbd_macro_ptr;
109 /* Re-execute the macro we are appending to,
110 for consistency of behavior. */
111 if (NILP (no_exec))
112 Fexecute_kbd_macro (KVAR (current_kboard, Vlast_kbd_macro),
113 make_number (1), Qnil);
115 message1 ("Appending to kbd macro...");
117 kset_defining_kbd_macro (current_kboard, Qt);
119 return Qnil;
122 /* Finish defining the current keyboard macro. */
124 void
125 end_kbd_macro (void)
127 kset_defining_kbd_macro (current_kboard, Qnil);
128 update_mode_lines = 20;
129 kset_last_kbd_macro
130 (current_kboard,
131 make_event_array ((current_kboard->kbd_macro_end
132 - current_kboard->kbd_macro_buffer),
133 current_kboard->kbd_macro_buffer));
136 DEFUN ("end-kbd-macro", Fend_kbd_macro, Send_kbd_macro, 0, 2, "p",
137 doc: /* Finish defining a keyboard macro.
138 The definition was started by \\[start-kbd-macro].
139 The macro is now available for use via \\[call-last-kbd-macro],
140 or it can be given a name with \\[name-last-kbd-macro] and then invoked
141 under that name.
143 With numeric arg, repeat macro now that many times,
144 counting the definition just completed as the first repetition.
145 An argument of zero means repeat until error.
147 In Lisp, optional second arg LOOPFUNC may be a function that is called prior to
148 each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */)
149 (Lisp_Object repeat, Lisp_Object loopfunc)
151 if (NILP (KVAR (current_kboard, defining_kbd_macro)))
152 error ("Not defining kbd macro");
154 if (NILP (repeat))
155 XSETFASTINT (repeat, 1);
156 else
157 CHECK_NUMBER (repeat);
159 if (!NILP (KVAR (current_kboard, defining_kbd_macro)))
161 end_kbd_macro ();
162 message1 ("Keyboard macro defined");
165 if (XFASTINT (repeat) == 0)
166 Fexecute_kbd_macro (KVAR (current_kboard, Vlast_kbd_macro), repeat, loopfunc);
167 else if (XINT (repeat) > 1)
169 XSETINT (repeat, XINT (repeat) - 1);
170 Fexecute_kbd_macro (KVAR (current_kboard, Vlast_kbd_macro),
171 repeat, loopfunc);
173 return Qnil;
176 /* Store character c into kbd macro being defined. */
178 void
179 store_kbd_macro_char (Lisp_Object c)
181 struct kboard *kb = current_kboard;
183 if (!NILP (KVAR (kb, defining_kbd_macro)))
185 if (kb->kbd_macro_ptr - kb->kbd_macro_buffer == kb->kbd_macro_bufsize)
187 ptrdiff_t ptr_offset = kb->kbd_macro_ptr - kb->kbd_macro_buffer;
188 ptrdiff_t end_offset = kb->kbd_macro_end - kb->kbd_macro_buffer;
189 kb->kbd_macro_buffer = xpalloc (kb->kbd_macro_buffer,
190 &kb->kbd_macro_bufsize,
191 1, -1, sizeof *kb->kbd_macro_buffer);
192 kb->kbd_macro_ptr = kb->kbd_macro_buffer + ptr_offset;
193 kb->kbd_macro_end = kb->kbd_macro_buffer + end_offset;
196 *kb->kbd_macro_ptr++ = c;
200 /* Declare that all chars stored so far in the kbd macro being defined
201 really belong to it. This is done in between editor commands. */
203 void
204 finalize_kbd_macro_chars (void)
206 current_kboard->kbd_macro_end = current_kboard->kbd_macro_ptr;
209 DEFUN ("cancel-kbd-macro-events", Fcancel_kbd_macro_events,
210 Scancel_kbd_macro_events, 0, 0, 0,
211 doc: /* Cancel the events added to a keyboard macro for this command. */)
212 (void)
214 current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_end;
215 return Qnil;
218 DEFUN ("store-kbd-macro-event", Fstore_kbd_macro_event,
219 Sstore_kbd_macro_event, 1, 1, 0,
220 doc: /* Store EVENT into the keyboard macro being defined. */)
221 (Lisp_Object event)
223 store_kbd_macro_char (event);
224 return Qnil;
227 DEFUN ("call-last-kbd-macro", Fcall_last_kbd_macro, Scall_last_kbd_macro,
228 0, 2, "p",
229 doc: /* Call the last keyboard macro that you defined with \\[start-kbd-macro].
231 A prefix argument serves as a repeat count. Zero means repeat until error.
233 To make a macro permanent so you can call it even after
234 defining others, use \\[name-last-kbd-macro].
236 In Lisp, optional second arg LOOPFUNC may be a function that is called prior to
237 each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */)
238 (Lisp_Object prefix, Lisp_Object loopfunc)
240 /* Don't interfere with recognition of the previous command
241 from before this macro started. */
242 Vthis_command = KVAR (current_kboard, Vlast_command);
243 /* C-x z after the macro should repeat the macro. */
244 Vreal_this_command = KVAR (current_kboard, Vlast_kbd_macro);
246 if (! NILP (KVAR (current_kboard, defining_kbd_macro)))
247 error ("Can't execute anonymous macro while defining one");
248 else if (NILP (KVAR (current_kboard, Vlast_kbd_macro)))
249 error ("No kbd macro has been defined");
250 else
251 Fexecute_kbd_macro (KVAR (current_kboard, Vlast_kbd_macro), prefix, loopfunc);
253 /* command_loop_1 sets this to nil before it returns;
254 get back the last command within the macro
255 so that it can be last, again, after we return. */
256 Vthis_command = KVAR (current_kboard, Vlast_command);
258 return Qnil;
261 /* Restore Vexecuting_kbd_macro and executing_kbd_macro_index.
262 Called when the unwind-protect in Fexecute_kbd_macro gets invoked. */
264 static void
265 pop_kbd_macro (Lisp_Object info)
267 Lisp_Object tem;
268 Vexecuting_kbd_macro = XCAR (info);
269 tem = XCDR (info);
270 executing_kbd_macro_index = XINT (XCAR (tem));
271 Vreal_this_command = XCDR (tem);
272 run_hook (Qkbd_macro_termination_hook);
275 DEFUN ("execute-kbd-macro", Fexecute_kbd_macro, Sexecute_kbd_macro, 1, 3, 0,
276 doc: /* Execute MACRO as string of editor command characters.
277 MACRO can also be a vector of keyboard events. If MACRO is a symbol,
278 its function definition is used.
279 COUNT is a repeat count, or nil for once, or 0 for infinite loop.
281 Optional third arg LOOPFUNC may be a function that is called prior to
282 each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */)
283 (Lisp_Object macro, Lisp_Object count, Lisp_Object loopfunc)
285 Lisp_Object final;
286 Lisp_Object tem;
287 ptrdiff_t pdlcount = SPECPDL_INDEX ();
288 EMACS_INT repeat = 1;
289 EMACS_INT success_count = 0;
291 executing_kbd_macro_iterations = 0;
293 if (!NILP (count))
295 count = Fprefix_numeric_value (count);
296 repeat = XINT (count);
299 final = indirect_function (macro);
300 if (!STRINGP (final) && !VECTORP (final))
301 error ("Keyboard macros must be strings or vectors");
303 tem = Fcons (Vexecuting_kbd_macro,
304 Fcons (make_number (executing_kbd_macro_index),
305 Vreal_this_command));
306 record_unwind_protect (pop_kbd_macro, tem);
310 Vexecuting_kbd_macro = final;
311 executing_kbd_macro = final;
312 executing_kbd_macro_index = 0;
314 kset_prefix_arg (current_kboard, Qnil);
316 if (!NILP (loopfunc))
318 Lisp_Object cont;
319 cont = call0 (loopfunc);
320 if (NILP (cont))
321 break;
324 command_loop_1 ();
326 executing_kbd_macro_iterations = ++success_count;
328 maybe_quit ();
330 while (--repeat
331 && (STRINGP (Vexecuting_kbd_macro) || VECTORP (Vexecuting_kbd_macro)));
333 executing_kbd_macro = Qnil;
335 Vreal_this_command = Vexecuting_kbd_macro;
337 return unbind_to (pdlcount, Qnil);
340 void
341 init_macros (void)
343 Vexecuting_kbd_macro = Qnil;
344 executing_kbd_macro = Qnil;
347 void
348 syms_of_macros (void)
350 DEFVAR_LISP ("kbd-macro-termination-hook", Vkbd_macro_termination_hook,
351 doc: /* Normal hook run whenever a keyboard macro terminates.
352 This is run whether the macro ends normally or prematurely due to an error. */);
353 Vkbd_macro_termination_hook = Qnil;
354 DEFSYM (Qkbd_macro_termination_hook, "kbd-macro-termination-hook");
356 defsubr (&Sstart_kbd_macro);
357 defsubr (&Send_kbd_macro);
358 defsubr (&Scall_last_kbd_macro);
359 defsubr (&Sexecute_kbd_macro);
360 defsubr (&Scancel_kbd_macro_events);
361 defsubr (&Sstore_kbd_macro_event);
363 DEFVAR_KBOARD ("defining-kbd-macro", defining_kbd_macro,
364 doc: /* Non-nil while a keyboard macro is being defined. Don't set this!
365 The value is the symbol `append' while appending to the definition of
366 an existing macro. */);
368 DEFVAR_LISP ("executing-kbd-macro", Vexecuting_kbd_macro,
369 doc: /* Currently executing keyboard macro (string or vector).
370 This is nil when not executing a keyboard macro. */);
372 DEFVAR_INT ("executing-kbd-macro-index", executing_kbd_macro_index,
373 doc: /* Index in currently executing keyboard macro; undefined if none executing. */);
375 DEFVAR_KBOARD ("last-kbd-macro", Vlast_kbd_macro,
376 doc: /* Last kbd macro defined, as a string or vector; nil if none defined. */);