3 Copyright (C) 1985-1986, 1993, 2000-2015 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
10 (at 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 <http://www.gnu.org/licenses/>. */
26 #include "character.h"
31 /* Number of successful iterations so far
32 for innermost keyboard macro.
33 This is not bound at each level,
34 so after an error, it describes the innermost interrupted macro. */
36 EMACS_INT executing_kbd_macro_iterations
;
38 /* This is the macro that was executing.
39 This is not bound at each level,
40 so after an error, it describes the innermost interrupted macro.
41 We use it only as a kind of flag, so no need to protect it. */
43 Lisp_Object executing_kbd_macro
;
45 DEFUN ("start-kbd-macro", Fstart_kbd_macro
, Sstart_kbd_macro
, 1, 2, "P",
46 doc
: /* Record subsequent keyboard input, defining a keyboard macro.
47 The commands are recorded even as they are executed.
48 Use \\[end-kbd-macro] to finish recording and make the macro available.
49 Use \\[name-last-kbd-macro] to give it a permanent name.
50 Non-nil arg (prefix arg) means append to last macro defined;
51 this begins by re-executing that macro as if you typed it again.
52 If optional second arg, NO-EXEC, is non-nil, do not re-execute last
53 macro before appending to it. */)
54 (Lisp_Object append
, Lisp_Object no_exec
)
56 if (!NILP (KVAR (current_kboard
, defining_kbd_macro
)))
57 error ("Already defining kbd macro");
59 if (!current_kboard
->kbd_macro_buffer
)
61 current_kboard
->kbd_macro_buffer
= xmalloc (30 * word_size
);
62 current_kboard
->kbd_macro_bufsize
= 30;
63 current_kboard
->kbd_macro_ptr
= current_kboard
->kbd_macro_buffer
;
64 current_kboard
->kbd_macro_end
= current_kboard
->kbd_macro_buffer
;
66 update_mode_lines
= 19;
69 if (current_kboard
->kbd_macro_bufsize
> 200)
71 current_kboard
->kbd_macro_buffer
72 = xrealloc (current_kboard
->kbd_macro_buffer
,
74 current_kboard
->kbd_macro_bufsize
= 30;
76 current_kboard
->kbd_macro_ptr
= current_kboard
->kbd_macro_buffer
;
77 current_kboard
->kbd_macro_end
= current_kboard
->kbd_macro_buffer
;
78 message1 ("Defining kbd macro...");
86 /* Check the type of last-kbd-macro in case Lisp code changed it. */
87 len
= CHECK_VECTOR_OR_STRING (KVAR (current_kboard
, Vlast_kbd_macro
));
89 /* Copy last-kbd-macro into the buffer, in case the Lisp code
90 has put another macro there. */
91 if (current_kboard
->kbd_macro_bufsize
- incr
< len
)
92 current_kboard
->kbd_macro_buffer
=
93 xpalloc (current_kboard
->kbd_macro_buffer
,
94 ¤t_kboard
->kbd_macro_bufsize
,
95 len
- current_kboard
->kbd_macro_bufsize
+ incr
, -1,
96 sizeof *current_kboard
->kbd_macro_buffer
);
98 /* Must convert meta modifier when copying string to vector. */
99 cvt
= STRINGP (KVAR (current_kboard
, Vlast_kbd_macro
));
100 for (i
= 0; i
< len
; i
++)
103 c
= Faref (KVAR (current_kboard
, Vlast_kbd_macro
), make_number (i
));
104 if (cvt
&& NATNUMP (c
) && (XFASTINT (c
) & 0x80))
105 XSETFASTINT (c
, CHAR_META
| (XFASTINT (c
) & ~0x80));
106 current_kboard
->kbd_macro_buffer
[i
] = c
;
109 current_kboard
->kbd_macro_ptr
= current_kboard
->kbd_macro_buffer
+ len
;
110 current_kboard
->kbd_macro_end
= current_kboard
->kbd_macro_ptr
;
112 /* Re-execute the macro we are appending to,
113 for consistency of behavior. */
115 Fexecute_kbd_macro (KVAR (current_kboard
, Vlast_kbd_macro
),
116 make_number (1), Qnil
);
118 message1 ("Appending to kbd macro...");
120 kset_defining_kbd_macro (current_kboard
, Qt
);
125 /* Finish defining the current keyboard macro. */
130 kset_defining_kbd_macro (current_kboard
, Qnil
);
131 update_mode_lines
= 20;
134 make_event_array ((current_kboard
->kbd_macro_end
135 - current_kboard
->kbd_macro_buffer
),
136 current_kboard
->kbd_macro_buffer
));
139 DEFUN ("end-kbd-macro", Fend_kbd_macro
, Send_kbd_macro
, 0, 2, "p",
140 doc
: /* Finish defining a keyboard macro.
141 The definition was started by \\[start-kbd-macro].
142 The macro is now available for use via \\[call-last-kbd-macro],
143 or it can be given a name with \\[name-last-kbd-macro] and then invoked
146 With numeric arg, repeat macro now that many times,
147 counting the definition just completed as the first repetition.
148 An argument of zero means repeat until error.
150 In Lisp, optional second arg LOOPFUNC may be a function that is called prior to
151 each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */)
152 (Lisp_Object repeat
, Lisp_Object loopfunc
)
154 if (NILP (KVAR (current_kboard
, defining_kbd_macro
)))
155 error ("Not defining kbd macro");
158 XSETFASTINT (repeat
, 1);
160 CHECK_NUMBER (repeat
);
162 if (!NILP (KVAR (current_kboard
, defining_kbd_macro
)))
165 message1 ("Keyboard macro defined");
168 if (XFASTINT (repeat
) == 0)
169 Fexecute_kbd_macro (KVAR (current_kboard
, Vlast_kbd_macro
), repeat
, loopfunc
);
170 else if (XINT (repeat
) > 1)
172 XSETINT (repeat
, XINT (repeat
) - 1);
173 Fexecute_kbd_macro (KVAR (current_kboard
, Vlast_kbd_macro
),
179 /* Store character c into kbd macro being defined. */
182 store_kbd_macro_char (Lisp_Object c
)
184 struct kboard
*kb
= current_kboard
;
186 if (!NILP (KVAR (kb
, defining_kbd_macro
)))
188 if (kb
->kbd_macro_ptr
- kb
->kbd_macro_buffer
== kb
->kbd_macro_bufsize
)
190 ptrdiff_t ptr_offset
, end_offset
, nbytes
;
192 ptr_offset
= kb
->kbd_macro_ptr
- kb
->kbd_macro_buffer
;
193 end_offset
= kb
->kbd_macro_end
- kb
->kbd_macro_buffer
;
194 if (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof *kb
->kbd_macro_buffer
/ 2
195 < kb
->kbd_macro_bufsize
)
196 memory_full (SIZE_MAX
);
197 nbytes
= kb
->kbd_macro_bufsize
* (2 * sizeof *kb
->kbd_macro_buffer
);
198 kb
->kbd_macro_buffer
= xrealloc (kb
->kbd_macro_buffer
, nbytes
);
199 kb
->kbd_macro_bufsize
*= 2;
200 kb
->kbd_macro_ptr
= kb
->kbd_macro_buffer
+ ptr_offset
;
201 kb
->kbd_macro_end
= kb
->kbd_macro_buffer
+ end_offset
;
204 *kb
->kbd_macro_ptr
++ = c
;
208 /* Declare that all chars stored so far in the kbd macro being defined
209 really belong to it. This is done in between editor commands. */
212 finalize_kbd_macro_chars (void)
214 current_kboard
->kbd_macro_end
= current_kboard
->kbd_macro_ptr
;
217 DEFUN ("cancel-kbd-macro-events", Fcancel_kbd_macro_events
,
218 Scancel_kbd_macro_events
, 0, 0, 0,
219 doc
: /* Cancel the events added to a keyboard macro for this command. */)
222 current_kboard
->kbd_macro_ptr
= current_kboard
->kbd_macro_end
;
226 DEFUN ("store-kbd-macro-event", Fstore_kbd_macro_event
,
227 Sstore_kbd_macro_event
, 1, 1, 0,
228 doc
: /* Store EVENT into the keyboard macro being defined. */)
231 store_kbd_macro_char (event
);
235 DEFUN ("call-last-kbd-macro", Fcall_last_kbd_macro
, Scall_last_kbd_macro
,
237 doc
: /* Call the last keyboard macro that you defined with \\[start-kbd-macro].
239 A prefix argument serves as a repeat count. Zero means repeat until error.
241 To make a macro permanent so you can call it even after
242 defining others, use \\[name-last-kbd-macro].
244 In Lisp, optional second arg LOOPFUNC may be a function that is called prior to
245 each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */)
246 (Lisp_Object prefix
, Lisp_Object loopfunc
)
248 /* Don't interfere with recognition of the previous command
249 from before this macro started. */
250 Vthis_command
= KVAR (current_kboard
, Vlast_command
);
251 /* C-x z after the macro should repeat the macro. */
252 Vreal_this_command
= KVAR (current_kboard
, Vlast_kbd_macro
);
254 if (! NILP (KVAR (current_kboard
, defining_kbd_macro
)))
255 error ("Can't execute anonymous macro while defining one");
256 else if (NILP (KVAR (current_kboard
, Vlast_kbd_macro
)))
257 error ("No kbd macro has been defined");
259 Fexecute_kbd_macro (KVAR (current_kboard
, Vlast_kbd_macro
), prefix
, loopfunc
);
261 /* command_loop_1 sets this to nil before it returns;
262 get back the last command within the macro
263 so that it can be last, again, after we return. */
264 Vthis_command
= KVAR (current_kboard
, Vlast_command
);
269 /* Restore Vexecuting_kbd_macro and executing_kbd_macro_index.
270 Called when the unwind-protect in Fexecute_kbd_macro gets invoked. */
273 pop_kbd_macro (Lisp_Object info
)
276 Vexecuting_kbd_macro
= XCAR (info
);
278 executing_kbd_macro_index
= XINT (XCAR (tem
));
279 Vreal_this_command
= XCDR (tem
);
280 run_hook (Qkbd_macro_termination_hook
);
283 DEFUN ("execute-kbd-macro", Fexecute_kbd_macro
, Sexecute_kbd_macro
, 1, 3, 0,
284 doc
: /* Execute MACRO as string of editor command characters.
285 MACRO can also be a vector of keyboard events. If MACRO is a symbol,
286 its function definition is used.
287 COUNT is a repeat count, or nil for once, or 0 for infinite loop.
289 Optional third arg LOOPFUNC may be a function that is called prior to
290 each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */)
291 (Lisp_Object macro
, Lisp_Object count
, Lisp_Object loopfunc
)
295 ptrdiff_t pdlcount
= SPECPDL_INDEX ();
296 EMACS_INT repeat
= 1;
297 struct gcpro gcpro1
, gcpro2
;
298 EMACS_INT success_count
= 0;
300 executing_kbd_macro_iterations
= 0;
304 count
= Fprefix_numeric_value (count
);
305 repeat
= XINT (count
);
308 final
= indirect_function (macro
);
309 if (!STRINGP (final
) && !VECTORP (final
))
310 error ("Keyboard macros must be strings or vectors");
312 tem
= Fcons (Vexecuting_kbd_macro
,
313 Fcons (make_number (executing_kbd_macro_index
),
314 Vreal_this_command
));
315 record_unwind_protect (pop_kbd_macro
, tem
);
317 GCPRO2 (final
, loopfunc
);
320 Vexecuting_kbd_macro
= final
;
321 executing_kbd_macro
= final
;
322 executing_kbd_macro_index
= 0;
324 kset_prefix_arg (current_kboard
, Qnil
);
326 if (!NILP (loopfunc
))
329 cont
= call0 (loopfunc
);
336 executing_kbd_macro_iterations
= ++success_count
;
341 && (STRINGP (Vexecuting_kbd_macro
) || VECTORP (Vexecuting_kbd_macro
)));
343 executing_kbd_macro
= Qnil
;
345 Vreal_this_command
= Vexecuting_kbd_macro
;
348 return unbind_to (pdlcount
, Qnil
);
354 Vexecuting_kbd_macro
= Qnil
;
355 executing_kbd_macro
= Qnil
;
359 syms_of_macros (void)
361 DEFSYM (Qexecute_kbd_macro
, "execute-kbd-macro");
363 DEFVAR_LISP ("kbd-macro-termination-hook", Vkbd_macro_termination_hook
,
364 doc
: /* Normal hook run whenever a keyboard macro terminates.
365 This is run whether the macro ends normally or prematurely due to an error. */);
366 Vkbd_macro_termination_hook
= Qnil
;
367 DEFSYM (Qkbd_macro_termination_hook
, "kbd-macro-termination-hook");
369 defsubr (&Sstart_kbd_macro
);
370 defsubr (&Send_kbd_macro
);
371 defsubr (&Scall_last_kbd_macro
);
372 defsubr (&Sexecute_kbd_macro
);
373 defsubr (&Scancel_kbd_macro_events
);
374 defsubr (&Sstore_kbd_macro_event
);
376 DEFVAR_KBOARD ("defining-kbd-macro", defining_kbd_macro
,
377 doc
: /* Non-nil while a keyboard macro is being defined. Don't set this!
378 The value is the symbol `append' while appending to the definition of
379 an existing macro. */);
381 DEFVAR_LISP ("executing-kbd-macro", Vexecuting_kbd_macro
,
382 doc
: /* Currently executing keyboard macro (string or vector).
383 This is nil when not executing a keyboard macro. */);
385 DEFVAR_INT ("executing-kbd-macro-index", executing_kbd_macro_index
,
386 doc
: /* Index in currently executing keyboard macro; undefined if none executing. */);
388 DEFVAR_KBOARD ("last-kbd-macro", Vlast_kbd_macro
,
389 doc
: /* Last kbd macro defined, as a string or vector; nil if none defined. */);