(scan_keyword_or_put_char, write_c_args): Use `fn'
[emacs.git] / src / macros.c
blobe9601b1aa7ff60b8b24e2a66af0c4f1413dd3cb6
1 /* Keyboard macros.
2 Copyright (C) 1985, 1986, 1993, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include <config.h>
23 #include "lisp.h"
24 #include "macros.h"
25 #include "commands.h"
26 #include "buffer.h"
27 #include "window.h"
28 #include "keyboard.h"
30 Lisp_Object Qexecute_kbd_macro, Qkbd_macro_termination_hook;
32 /* Kbd macro currently being executed (a string or vector). */
34 Lisp_Object Vexecuting_macro;
36 /* Index of next character to fetch from that macro. */
38 int executing_macro_index;
40 /* Number of successful iterations so far
41 for innermost keyboard macro.
42 This is not bound at each level,
43 so after an error, it describes the innermost interrupted macro. */
45 int executing_macro_iterations;
47 /* This is the macro that was executing.
48 This is not bound at each level,
49 so after an error, it describes the innermost interrupted macro.
50 We use it only as a kind of flag, so no need to protect it. */
52 Lisp_Object executing_macro;
54 extern Lisp_Object real_this_command;
56 Lisp_Object Fexecute_kbd_macro ();
58 DEFUN ("start-kbd-macro", Fstart_kbd_macro, Sstart_kbd_macro, 1, 2, "P",
59 doc: /* Record subsequent keyboard input, defining a keyboard macro.
60 The commands are recorded even as they are executed.
61 Use \\[end-kbd-macro] to finish recording and make the macro available.
62 Use \\[name-last-kbd-macro] to give it a permanent name.
63 Non-nil arg (prefix arg) means append to last macro defined;
64 this begins by re-executing that macro as if you typed it again.
65 If optional second arg, NO-EXEC, is non-nil, do not re-execute last
66 macro before appending to it. */)
67 (append, no_exec)
68 Lisp_Object append, no_exec;
70 if (!NILP (current_kboard->defining_kbd_macro))
71 error ("Already defining kbd macro");
73 if (!current_kboard->kbd_macro_buffer)
75 current_kboard->kbd_macro_bufsize = 30;
76 current_kboard->kbd_macro_buffer
77 = (Lisp_Object *)xmalloc (30 * sizeof (Lisp_Object));
79 update_mode_lines++;
80 if (NILP (append))
82 if (current_kboard->kbd_macro_bufsize > 200)
84 current_kboard->kbd_macro_bufsize = 30;
85 current_kboard->kbd_macro_buffer
86 = (Lisp_Object *)xrealloc (current_kboard->kbd_macro_buffer,
87 30 * sizeof (Lisp_Object));
89 current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_buffer;
90 current_kboard->kbd_macro_end = current_kboard->kbd_macro_buffer;
91 message ("Defining kbd macro...");
93 else
95 int i, len;
97 /* Check the type of last-kbd-macro in case Lisp code changed it. */
98 if (!STRINGP (current_kboard->Vlast_kbd_macro)
99 && !VECTORP (current_kboard->Vlast_kbd_macro))
100 current_kboard->Vlast_kbd_macro
101 = wrong_type_argument (Qarrayp, current_kboard->Vlast_kbd_macro);
103 len = XINT (Flength (current_kboard->Vlast_kbd_macro));
105 /* Copy last-kbd-macro into the buffer, in case the Lisp code
106 has put another macro there. */
107 if (current_kboard->kbd_macro_bufsize < len + 30)
109 current_kboard->kbd_macro_bufsize = len + 30;
110 current_kboard->kbd_macro_buffer
111 = (Lisp_Object *)xrealloc (current_kboard->kbd_macro_buffer,
112 (len + 30) * sizeof (Lisp_Object));
114 for (i = 0; i < len; i++)
115 current_kboard->kbd_macro_buffer[i]
116 = Faref (current_kboard->Vlast_kbd_macro, make_number (i));
118 current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_buffer + len;
119 current_kboard->kbd_macro_end = current_kboard->kbd_macro_ptr;
121 /* Re-execute the macro we are appending to,
122 for consistency of behavior. */
123 if (NILP (no_exec))
124 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro,
125 make_number (1), Qnil);
127 message ("Appending to kbd macro...");
129 current_kboard->defining_kbd_macro = Qt;
131 return Qnil;
134 DEFUN ("end-kbd-macro", Fend_kbd_macro, Send_kbd_macro, 0, 2, "p",
135 doc: /* Finish defining a keyboard macro.
136 The definition was started by \\[start-kbd-macro].
137 The macro is now available for use via \\[call-last-kbd-macro],
138 or it can be given a name with \\[name-last-kbd-macro] and then invoked
139 under that name.
141 With numeric arg, repeat macro now that many times,
142 counting the definition just completed as the first repetition.
143 An argument of zero means repeat until error.
145 In Lisp, optional second arg LOOPFUNC may be a function that is called prior to
146 each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */)
147 (repeat, loopfunc)
148 Lisp_Object repeat, loopfunc;
150 if (NILP (current_kboard->defining_kbd_macro))
151 error ("Not defining kbd macro");
153 if (NILP (repeat))
154 XSETFASTINT (repeat, 1);
155 else
156 CHECK_NUMBER (repeat);
158 if (!NILP (current_kboard->defining_kbd_macro))
160 current_kboard->defining_kbd_macro = Qnil;
161 update_mode_lines++;
162 current_kboard->Vlast_kbd_macro
163 = make_event_array ((current_kboard->kbd_macro_end
164 - current_kboard->kbd_macro_buffer),
165 current_kboard->kbd_macro_buffer);
166 message ("Keyboard macro defined");
169 if (XFASTINT (repeat) == 0)
170 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, repeat, loopfunc);
171 else
173 XSETINT (repeat, XINT (repeat)-1);
174 if (XINT (repeat) > 0)
175 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, repeat, loopfunc);
177 return Qnil;
180 /* Store character c into kbd macro being defined */
182 void
183 store_kbd_macro_char (c)
184 Lisp_Object c;
186 struct kboard *kb = current_kboard;
188 if (!NILP (kb->defining_kbd_macro))
190 if (kb->kbd_macro_ptr - kb->kbd_macro_buffer == kb->kbd_macro_bufsize)
192 int ptr_offset, end_offset, nbytes;
194 ptr_offset = kb->kbd_macro_ptr - kb->kbd_macro_buffer;
195 end_offset = kb->kbd_macro_end - kb->kbd_macro_buffer;
196 kb->kbd_macro_bufsize *= 2;
197 nbytes = kb->kbd_macro_bufsize * sizeof *kb->kbd_macro_buffer;
198 kb->kbd_macro_buffer
199 = (Lisp_Object *) xrealloc (kb->kbd_macro_buffer, nbytes);
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. */
211 void
212 finalize_kbd_macro_chars ()
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;
223 return Qnil;
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. */)
229 (event)
230 Lisp_Object event;
232 store_kbd_macro_char (event);
233 return Qnil;
236 DEFUN ("call-last-kbd-macro", Fcall_last_kbd_macro, Scall_last_kbd_macro,
237 0, 2, "p",
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 (prefix, loopfunc)
248 Lisp_Object prefix, loopfunc;
250 /* Don't interfere with recognition of the previous command
251 from before this macro started. */
252 Vthis_command = current_kboard->Vlast_command;
253 /* C-x z after the macro should repeat the macro. */
254 real_this_command = current_kboard->Vlast_kbd_macro;
256 if (! NILP (current_kboard->defining_kbd_macro))
257 error ("Can't execute anonymous macro while defining one");
258 else if (NILP (current_kboard->Vlast_kbd_macro))
259 error ("No kbd macro has been defined");
260 else
261 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, prefix, loopfunc);
263 /* command_loop_1 sets this to nil before it returns;
264 get back the last command within the macro
265 so that it can be last, again, after we return. */
266 Vthis_command = current_kboard->Vlast_command;
268 return Qnil;
271 /* Restore Vexecuting_macro and executing_macro_index - called when
272 the unwind-protect in Fexecute_kbd_macro gets invoked. */
274 static Lisp_Object
275 pop_kbd_macro (info)
276 Lisp_Object info;
278 Lisp_Object tem;
279 Vexecuting_macro = XCAR (info);
280 tem = XCDR (info);
281 executing_macro_index = XINT (XCAR (tem));
282 real_this_command = XCDR (tem);
283 Frun_hooks (1, &Qkbd_macro_termination_hook);
284 return Qnil;
287 DEFUN ("execute-kbd-macro", Fexecute_kbd_macro, Sexecute_kbd_macro, 1, 3, 0,
288 doc: /* Execute MACRO as string of editor command characters.
289 If MACRO is a symbol, its function definition is used.
290 COUNT is a repeat count, or nil for once, or 0 for infinite loop.
292 Optional third arg LOOPFUNC may be a function that is called prior to
293 each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */)
294 (macro, count, loopfunc)
295 Lisp_Object macro, count, loopfunc;
297 Lisp_Object final;
298 Lisp_Object tem;
299 int pdlcount = SPECPDL_INDEX ();
300 int repeat = 1;
301 struct gcpro gcpro1, gcpro2;
302 int success_count = 0;
304 executing_macro_iterations = 0;
306 if (!NILP (count))
308 count = Fprefix_numeric_value (count);
309 repeat = XINT (count);
312 final = indirect_function (macro);
313 if (!STRINGP (final) && !VECTORP (final))
314 error ("Keyboard macros must be strings or vectors");
316 tem = Fcons (Vexecuting_macro,
317 Fcons (make_number (executing_macro_index),
318 real_this_command));
319 record_unwind_protect (pop_kbd_macro, tem);
321 GCPRO2 (final, loopfunc);
324 Vexecuting_macro = final;
325 executing_macro = final;
326 executing_macro_index = 0;
328 current_kboard->Vprefix_arg = Qnil;
330 if (!NILP (loopfunc))
332 Lisp_Object cont;
333 cont = call0 (loopfunc);
334 if (NILP (cont))
335 break;
338 command_loop_1 ();
340 executing_macro_iterations = ++success_count;
342 QUIT;
344 while (--repeat
345 && (STRINGP (Vexecuting_macro) || VECTORP (Vexecuting_macro)));
347 executing_macro = Qnil;
349 real_this_command = Vexecuting_macro;
351 UNGCPRO;
352 return unbind_to (pdlcount, Qnil);
355 void
356 init_macros ()
358 Vexecuting_macro = Qnil;
359 executing_macro = Qnil;
362 void
363 syms_of_macros ()
365 Qexecute_kbd_macro = intern ("execute-kbd-macro");
366 staticpro (&Qexecute_kbd_macro);
367 Qkbd_macro_termination_hook = intern ("kbd-macro-termination-hook");
368 staticpro (&Qkbd_macro_termination_hook);
370 defsubr (&Sstart_kbd_macro);
371 defsubr (&Send_kbd_macro);
372 defsubr (&Scall_last_kbd_macro);
373 defsubr (&Sexecute_kbd_macro);
374 defsubr (&Scancel_kbd_macro_events);
375 defsubr (&Sstore_kbd_macro_event);
377 DEFVAR_KBOARD ("defining-kbd-macro", defining_kbd_macro,
378 doc: /* Non-nil while a keyboard macro is being defined. Don't set this! */);
380 DEFVAR_LISP ("executing-macro", &Vexecuting_macro,
381 doc: /* Currently executing keyboard macro (string or vector); nil if none executing. */);
383 DEFVAR_LISP_NOPRO ("executing-kbd-macro", &Vexecuting_macro,
384 doc: /* Currently executing keyboard macro (string or vector); nil if none executing. */);
386 DEFVAR_KBOARD ("last-kbd-macro", Vlast_kbd_macro,
387 doc: /* Last kbd macro defined, as a string or vector; nil if none defined. */);