3 Copyright (C) 1985-1986, 1993, 2000-2011 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/>. */
30 Lisp_Object Qexecute_kbd_macro
, Qkbd_macro_termination_hook
;
32 /* Number of successful iterations so far
33 for innermost keyboard macro.
34 This is not bound at each level,
35 so after an error, it describes the innermost interrupted macro. */
37 int executing_kbd_macro_iterations
;
39 /* This is the macro that was executing.
40 This is not bound at each level,
41 so after an error, it describes the innermost interrupted macro.
42 We use it only as a kind of flag, so no need to protect it. */
44 Lisp_Object executing_kbd_macro
;
46 Lisp_Object
Fexecute_kbd_macro (Lisp_Object macro
, Lisp_Object count
, Lisp_Object loopfunc
);
48 DEFUN ("start-kbd-macro", Fstart_kbd_macro
, Sstart_kbd_macro
, 1, 2, "P",
49 doc
: /* Record subsequent keyboard input, defining a keyboard macro.
50 The commands are recorded even as they are executed.
51 Use \\[end-kbd-macro] to finish recording and make the macro available.
52 Use \\[name-last-kbd-macro] to give it a permanent name.
53 Non-nil arg (prefix arg) means append to last macro defined;
54 this begins by re-executing that macro as if you typed it again.
55 If optional second arg, NO-EXEC, is non-nil, do not re-execute last
56 macro before appending to it. */)
57 (Lisp_Object append
, Lisp_Object no_exec
)
59 if (!NILP (KVAR (current_kboard
, defining_kbd_macro
)))
60 error ("Already defining kbd macro");
62 if (!current_kboard
->kbd_macro_buffer
)
64 current_kboard
->kbd_macro_bufsize
= 30;
65 current_kboard
->kbd_macro_buffer
66 = (Lisp_Object
*)xmalloc (30 * sizeof (Lisp_Object
));
71 if (current_kboard
->kbd_macro_bufsize
> 200)
73 current_kboard
->kbd_macro_bufsize
= 30;
74 current_kboard
->kbd_macro_buffer
75 = (Lisp_Object
*)xrealloc (current_kboard
->kbd_macro_buffer
,
76 30 * sizeof (Lisp_Object
));
78 current_kboard
->kbd_macro_ptr
= current_kboard
->kbd_macro_buffer
;
79 current_kboard
->kbd_macro_end
= current_kboard
->kbd_macro_buffer
;
80 message ("Defining kbd macro...");
87 /* Check the type of last-kbd-macro in case Lisp code changed it. */
88 CHECK_VECTOR_OR_STRING (KVAR (current_kboard
, Vlast_kbd_macro
));
90 len
= XINT (Flength (KVAR (current_kboard
, Vlast_kbd_macro
)));
92 /* Copy last-kbd-macro into the buffer, in case the Lisp code
93 has put another macro there. */
94 if (current_kboard
->kbd_macro_bufsize
< len
+ 30)
96 current_kboard
->kbd_macro_bufsize
= len
+ 30;
97 current_kboard
->kbd_macro_buffer
98 = (Lisp_Object
*)xrealloc (current_kboard
->kbd_macro_buffer
,
99 (len
+ 30) * sizeof (Lisp_Object
));
102 /* Must convert meta modifier when copying string to vector. */
103 cvt
= STRINGP (KVAR (current_kboard
, Vlast_kbd_macro
));
104 for (i
= 0; i
< len
; i
++)
107 c
= Faref (KVAR (current_kboard
, Vlast_kbd_macro
), make_number (i
));
108 if (cvt
&& NATNUMP (c
) && (XFASTINT (c
) & 0x80))
109 XSETFASTINT (c
, CHAR_META
| (XFASTINT (c
) & ~0x80));
110 current_kboard
->kbd_macro_buffer
[i
] = c
;
113 current_kboard
->kbd_macro_ptr
= current_kboard
->kbd_macro_buffer
+ len
;
114 current_kboard
->kbd_macro_end
= current_kboard
->kbd_macro_ptr
;
116 /* Re-execute the macro we are appending to,
117 for consistency of behavior. */
119 Fexecute_kbd_macro (KVAR (current_kboard
, Vlast_kbd_macro
),
120 make_number (1), Qnil
);
122 message ("Appending to kbd macro...");
124 KVAR (current_kboard
, defining_kbd_macro
) = Qt
;
129 /* Finish defining the current keyboard macro. */
134 KVAR (current_kboard
, defining_kbd_macro
) = Qnil
;
136 KVAR (current_kboard
, Vlast_kbd_macro
)
137 = make_event_array ((current_kboard
->kbd_macro_end
138 - current_kboard
->kbd_macro_buffer
),
139 current_kboard
->kbd_macro_buffer
);
142 DEFUN ("end-kbd-macro", Fend_kbd_macro
, Send_kbd_macro
, 0, 2, "p",
143 doc
: /* Finish defining a keyboard macro.
144 The definition was started by \\[start-kbd-macro].
145 The macro is now available for use via \\[call-last-kbd-macro],
146 or it can be given a name with \\[name-last-kbd-macro] and then invoked
149 With numeric arg, repeat macro now that many times,
150 counting the definition just completed as the first repetition.
151 An argument of zero means repeat until error.
153 In Lisp, optional second arg LOOPFUNC may be a function that is called prior to
154 each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */)
155 (Lisp_Object repeat
, Lisp_Object loopfunc
)
157 if (NILP (KVAR (current_kboard
, defining_kbd_macro
)))
158 error ("Not defining kbd macro");
161 XSETFASTINT (repeat
, 1);
163 CHECK_NUMBER (repeat
);
165 if (!NILP (KVAR (current_kboard
, defining_kbd_macro
)))
168 message ("Keyboard macro defined");
171 if (XFASTINT (repeat
) == 0)
172 Fexecute_kbd_macro (KVAR (current_kboard
, Vlast_kbd_macro
), repeat
, loopfunc
);
175 XSETINT (repeat
, XINT (repeat
)-1);
176 if (XINT (repeat
) > 0)
177 Fexecute_kbd_macro (KVAR (current_kboard
, Vlast_kbd_macro
), repeat
, loopfunc
);
182 /* Store character c into kbd macro being defined */
185 store_kbd_macro_char (Lisp_Object c
)
187 struct kboard
*kb
= current_kboard
;
189 if (!NILP (KVAR (kb
, defining_kbd_macro
)))
191 if (kb
->kbd_macro_ptr
- kb
->kbd_macro_buffer
== kb
->kbd_macro_bufsize
)
193 int ptr_offset
, end_offset
, nbytes
;
195 ptr_offset
= kb
->kbd_macro_ptr
- kb
->kbd_macro_buffer
;
196 end_offset
= kb
->kbd_macro_end
- kb
->kbd_macro_buffer
;
197 kb
->kbd_macro_bufsize
*= 2;
198 nbytes
= kb
->kbd_macro_bufsize
* sizeof *kb
->kbd_macro_buffer
;
200 = (Lisp_Object
*) xrealloc (kb
->kbd_macro_buffer
, nbytes
);
201 kb
->kbd_macro_ptr
= kb
->kbd_macro_buffer
+ ptr_offset
;
202 kb
->kbd_macro_end
= kb
->kbd_macro_buffer
+ end_offset
;
205 *kb
->kbd_macro_ptr
++ = c
;
209 /* Declare that all chars stored so far in the kbd macro being defined
210 really belong to it. This is done in between editor commands. */
213 finalize_kbd_macro_chars (void)
215 current_kboard
->kbd_macro_end
= current_kboard
->kbd_macro_ptr
;
218 DEFUN ("cancel-kbd-macro-events", Fcancel_kbd_macro_events
,
219 Scancel_kbd_macro_events
, 0, 0, 0,
220 doc
: /* Cancel the events added to a keyboard macro for this command. */)
223 current_kboard
->kbd_macro_ptr
= current_kboard
->kbd_macro_end
;
227 DEFUN ("store-kbd-macro-event", Fstore_kbd_macro_event
,
228 Sstore_kbd_macro_event
, 1, 1, 0,
229 doc
: /* Store EVENT into the keyboard macro being defined. */)
232 store_kbd_macro_char (event
);
236 DEFUN ("call-last-kbd-macro", Fcall_last_kbd_macro
, Scall_last_kbd_macro
,
238 doc
: /* Call the last keyboard macro that you defined with \\[start-kbd-macro].
240 A prefix argument serves as a repeat count. Zero means repeat until error.
242 To make a macro permanent so you can call it even after
243 defining others, use \\[name-last-kbd-macro].
245 In Lisp, optional second arg LOOPFUNC may be a function that is called prior to
246 each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */)
247 (Lisp_Object prefix
, Lisp_Object loopfunc
)
249 /* Don't interfere with recognition of the previous command
250 from before this macro started. */
251 Vthis_command
= KVAR (current_kboard
, Vlast_command
);
252 /* C-x z after the macro should repeat the macro. */
253 real_this_command
= KVAR (current_kboard
, Vlast_kbd_macro
);
255 if (! NILP (KVAR (current_kboard
, defining_kbd_macro
)))
256 error ("Can't execute anonymous macro while defining one");
257 else if (NILP (KVAR (current_kboard
, Vlast_kbd_macro
)))
258 error ("No kbd macro has been defined");
260 Fexecute_kbd_macro (KVAR (current_kboard
, Vlast_kbd_macro
), prefix
, loopfunc
);
262 /* command_loop_1 sets this to nil before it returns;
263 get back the last command within the macro
264 so that it can be last, again, after we return. */
265 Vthis_command
= KVAR (current_kboard
, Vlast_command
);
270 /* Restore Vexecuting_kbd_macro and executing_kbd_macro_index.
271 Called when the unwind-protect in Fexecute_kbd_macro gets invoked. */
274 pop_kbd_macro (Lisp_Object info
)
277 Vexecuting_kbd_macro
= XCAR (info
);
279 executing_kbd_macro_index
= XINT (XCAR (tem
));
280 real_this_command
= XCDR (tem
);
281 Frun_hooks (1, &Qkbd_macro_termination_hook
);
285 DEFUN ("execute-kbd-macro", Fexecute_kbd_macro
, Sexecute_kbd_macro
, 1, 3, 0,
286 doc
: /* Execute MACRO as string of editor command characters.
287 If MACRO is a symbol, its function definition is used.
288 COUNT is a repeat count, or nil for once, or 0 for infinite loop.
290 Optional third arg LOOPFUNC may be a function that is called prior to
291 each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */)
292 (Lisp_Object macro
, Lisp_Object count
, Lisp_Object loopfunc
)
296 int pdlcount
= SPECPDL_INDEX ();
298 struct gcpro gcpro1
, gcpro2
;
299 int success_count
= 0;
301 executing_kbd_macro_iterations
= 0;
305 count
= Fprefix_numeric_value (count
);
306 repeat
= XINT (count
);
309 final
= indirect_function (macro
);
310 if (!STRINGP (final
) && !VECTORP (final
))
311 error ("Keyboard macros must be strings or vectors");
313 tem
= Fcons (Vexecuting_kbd_macro
,
314 Fcons (make_number (executing_kbd_macro_index
),
316 record_unwind_protect (pop_kbd_macro
, tem
);
318 GCPRO2 (final
, loopfunc
);
321 Vexecuting_kbd_macro
= final
;
322 executing_kbd_macro
= final
;
323 executing_kbd_macro_index
= 0;
325 KVAR (current_kboard
, Vprefix_arg
) = Qnil
;
327 if (!NILP (loopfunc
))
330 cont
= call0 (loopfunc
);
337 executing_kbd_macro_iterations
= ++success_count
;
342 && (STRINGP (Vexecuting_kbd_macro
) || VECTORP (Vexecuting_kbd_macro
)));
344 executing_kbd_macro
= Qnil
;
346 real_this_command
= Vexecuting_kbd_macro
;
349 return unbind_to (pdlcount
, Qnil
);
355 Vexecuting_kbd_macro
= Qnil
;
356 executing_kbd_macro
= Qnil
;
360 syms_of_macros (void)
362 Qexecute_kbd_macro
= intern_c_string ("execute-kbd-macro");
363 staticpro (&Qexecute_kbd_macro
);
365 DEFVAR_LISP ("kbd-macro-termination-hook", Vkbd_macro_termination_hook
,
366 doc
: /* Normal hook run whenever a keyboard macro terminates.
367 This is run whether the macro ends normally or prematurely due to an error. */);
368 Vkbd_macro_termination_hook
= Qnil
;
369 Qkbd_macro_termination_hook
= intern_c_string ("kbd-macro-termination-hook");
370 staticpro (&Qkbd_macro_termination_hook
);
372 defsubr (&Sstart_kbd_macro
);
373 defsubr (&Send_kbd_macro
);
374 defsubr (&Scall_last_kbd_macro
);
375 defsubr (&Sexecute_kbd_macro
);
376 defsubr (&Scancel_kbd_macro_events
);
377 defsubr (&Sstore_kbd_macro_event
);
379 DEFVAR_KBOARD ("defining-kbd-macro", defining_kbd_macro
,
380 doc
: /* Non-nil while a keyboard macro is being defined. Don't set this!
381 The value is the symbol `append' while appending to the definition of
382 an existing macro. */);
384 DEFVAR_LISP ("executing-kbd-macro", Vexecuting_kbd_macro
,
385 doc
: /* Currently executing keyboard macro (string or vector).
386 This is nil when not executing a keyboard macro. */);
388 DEFVAR_INT ("executing-kbd-macro-index", executing_kbd_macro_index
,
389 doc
: /* Index in currently executing keyboard macro; undefined if none executing. */);
391 DEFVAR_KBOARD ("last-kbd-macro", Vlast_kbd_macro
,
392 doc
: /* Last kbd macro defined, as a string or vector; nil if none defined. */);