(store_kbd_macro_char): Fix computation of kbd_macro_end.
[bpt/emacs.git] / src / macros.c
1 /* Keyboard macros.
2 Copyright (C) 1985, 1986, 1993, 2000, 2001 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
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.
10
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.
15
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. */
20
21
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"
29
30 Lisp_Object Qexecute_kbd_macro, Qkbd_macro_termination_hook;
31
32 /* Kbd macro currently being executed (a string or vector). */
33
34 Lisp_Object Vexecuting_macro;
35
36 /* Index of next character to fetch from that macro. */
37
38 int executing_macro_index;
39
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. */
44
45 int executing_macro_iterations;
46
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. */
51
52 Lisp_Object executing_macro;
53
54 extern Lisp_Object real_this_command;
55
56 Lisp_Object Fexecute_kbd_macro ();
57 \f
58 DEFUN ("start-kbd-macro", Fstart_kbd_macro, Sstart_kbd_macro, 1, 1, "P",
59 "Record subsequent keyboard input, defining a keyboard macro.\n\
60 The commands are recorded even as they are executed.\n\
61 Use \\[end-kbd-macro] to finish recording and make the macro available.\n\
62 Use \\[name-last-kbd-macro] to give it a permanent name.\n\
63 Non-nil arg (prefix arg) means append to last macro defined;\n\
64 this begins by re-executing that macro as if you typed it again.")
65 (append)
66 Lisp_Object append;
67 {
68 if (!NILP (current_kboard->defining_kbd_macro))
69 error ("Already defining kbd macro");
70
71 if (!current_kboard->kbd_macro_buffer)
72 {
73 current_kboard->kbd_macro_bufsize = 30;
74 current_kboard->kbd_macro_buffer
75 = (Lisp_Object *)xmalloc (30 * sizeof (Lisp_Object));
76 }
77 update_mode_lines++;
78 if (NILP (append))
79 {
80 if (current_kboard->kbd_macro_bufsize > 200)
81 {
82 current_kboard->kbd_macro_bufsize = 30;
83 current_kboard->kbd_macro_buffer
84 = (Lisp_Object *)xrealloc (current_kboard->kbd_macro_buffer,
85 30 * sizeof (Lisp_Object));
86 }
87 current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_buffer;
88 current_kboard->kbd_macro_end = current_kboard->kbd_macro_buffer;
89 message ("Defining kbd macro...");
90 }
91 else
92 {
93 int i, len;
94
95 /* Check the type of last-kbd-macro in case Lisp code changed it. */
96 if (!STRINGP (current_kboard->Vlast_kbd_macro)
97 && !VECTORP (current_kboard->Vlast_kbd_macro))
98 current_kboard->Vlast_kbd_macro
99 = wrong_type_argument (Qarrayp, current_kboard->Vlast_kbd_macro);
100
101 len = XINT (Flength (current_kboard->Vlast_kbd_macro));
102
103 /* Copy last-kbd-macro into the buffer, in case the Lisp code
104 has put another macro there. */
105 if (current_kboard->kbd_macro_bufsize < len + 30)
106 {
107 current_kboard->kbd_macro_bufsize = len + 30;
108 current_kboard->kbd_macro_buffer
109 = (Lisp_Object *)xrealloc (current_kboard->kbd_macro_buffer,
110 (len + 30) * sizeof (Lisp_Object));
111 }
112 for (i = 0; i < len; i++)
113 current_kboard->kbd_macro_buffer[i]
114 = Faref (current_kboard->Vlast_kbd_macro, make_number (i));
115
116 current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_buffer + len;
117 current_kboard->kbd_macro_end = current_kboard->kbd_macro_ptr;
118
119 /* Re-execute the macro we are appending to,
120 for consistency of behavior. */
121 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro,
122 make_number (1));
123
124 message ("Appending to kbd macro...");
125 }
126 current_kboard->defining_kbd_macro = Qt;
127
128 return Qnil;
129 }
130
131 DEFUN ("end-kbd-macro", Fend_kbd_macro, Send_kbd_macro, 0, 1, "p",
132 "Finish defining a keyboard macro.\n\
133 The definition was started by \\[start-kbd-macro].\n\
134 The macro is now available for use via \\[call-last-kbd-macro],\n\
135 or it can be given a name with \\[name-last-kbd-macro] and then invoked\n\
136 under that name.\n\
137 \n\
138 With numeric arg, repeat macro now that many times,\n\
139 counting the definition just completed as the first repetition.\n\
140 An argument of zero means repeat until error.")
141 (repeat)
142 Lisp_Object repeat;
143 {
144 if (NILP (current_kboard->defining_kbd_macro))
145 error ("Not defining kbd macro");
146
147 if (NILP (repeat))
148 XSETFASTINT (repeat, 1);
149 else
150 CHECK_NUMBER (repeat, 0);
151
152 if (!NILP (current_kboard->defining_kbd_macro))
153 {
154 current_kboard->defining_kbd_macro = Qnil;
155 update_mode_lines++;
156 current_kboard->Vlast_kbd_macro
157 = make_event_array ((current_kboard->kbd_macro_end
158 - current_kboard->kbd_macro_buffer),
159 current_kboard->kbd_macro_buffer);
160 message ("Keyboard macro defined");
161 }
162
163 if (XFASTINT (repeat) == 0)
164 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, repeat);
165 else
166 {
167 XSETINT (repeat, XINT (repeat)-1);
168 if (XINT (repeat) > 0)
169 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, repeat);
170 }
171 return Qnil;
172 }
173
174 /* Store character c into kbd macro being defined */
175
176 void
177 store_kbd_macro_char (c)
178 Lisp_Object c;
179 {
180 struct kboard *kb = current_kboard;
181
182 if (!NILP (kb->defining_kbd_macro))
183 {
184 if (kb->kbd_macro_ptr - kb->kbd_macro_buffer == kb->kbd_macro_bufsize)
185 {
186 int ptr_offset, end_offset, nbytes;
187
188 ptr_offset = kb->kbd_macro_ptr - kb->kbd_macro_buffer;
189 end_offset = kb->kbd_macro_end - kb->kbd_macro_buffer;
190 kb->kbd_macro_bufsize *= 2;
191 nbytes = kb->kbd_macro_bufsize * sizeof *kb->kbd_macro_buffer;
192 kb->kbd_macro_buffer
193 = (Lisp_Object *) xrealloc (kb->kbd_macro_buffer, nbytes);
194 kb->kbd_macro_ptr = kb->kbd_macro_buffer + ptr_offset;
195 kb->kbd_macro_end = kb->kbd_macro_buffer + end_offset;
196 }
197
198 *kb->kbd_macro_ptr++ = c;
199 }
200 }
201
202 /* Declare that all chars stored so far in the kbd macro being defined
203 really belong to it. This is done in between editor commands. */
204
205 void
206 finalize_kbd_macro_chars ()
207 {
208 current_kboard->kbd_macro_end = current_kboard->kbd_macro_ptr;
209 }
210
211 DEFUN ("cancel-kbd-macro-events", Fcancel_kbd_macro_events,
212 Scancel_kbd_macro_events, 0, 0, 0,
213 "Cancel the events added to a keyboard macro for this command.")
214 ()
215 {
216 current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_end;
217 return Qnil;
218 }
219
220 DEFUN ("store-kbd-macro-event", Fstore_kbd_macro_event,
221 Sstore_kbd_macro_event, 1, 1, 0,
222 "Store EVENT into the keyboard macro being defined.")
223 (event)
224 Lisp_Object event;
225 {
226 store_kbd_macro_char (event);
227 return Qnil;
228 }
229 \f
230 DEFUN ("call-last-kbd-macro", Fcall_last_kbd_macro, Scall_last_kbd_macro,
231 0, 1, "p",
232 "Call the last keyboard macro that you defined with \\[start-kbd-macro].\n\
233 \n\
234 A prefix argument serves as a repeat count. Zero means repeat until error.\n\
235 \n\
236 To make a macro permanent so you can call it even after\n\
237 defining others, use \\[name-last-kbd-macro].")
238 (prefix)
239 Lisp_Object prefix;
240 {
241 /* Don't interfere with recognition of the previous command
242 from before this macro started. */
243 Vthis_command = current_kboard->Vlast_command;
244 /* C-x z after the macro should repeat the macro. */
245 real_this_command = current_kboard->Vlast_kbd_macro;
246
247 if (! NILP (current_kboard->defining_kbd_macro))
248 error ("Can't execute anonymous macro while defining one");
249 else if (NILP (current_kboard->Vlast_kbd_macro))
250 error ("No kbd macro has been defined");
251 else
252 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, prefix);
253
254 /* command_loop_1 sets this to nil before it returns;
255 get back the last command within the macro
256 so that it can be last, again, after we return. */
257 Vthis_command = current_kboard->Vlast_command;
258
259 return Qnil;
260 }
261
262 /* Restore Vexecuting_macro and executing_macro_index - called when
263 the unwind-protect in Fexecute_kbd_macro gets invoked. */
264
265 static Lisp_Object
266 pop_kbd_macro (info)
267 Lisp_Object info;
268 {
269 Lisp_Object tem;
270 Vexecuting_macro = XCAR (info);
271 tem = XCDR (info);
272 executing_macro_index = XINT (XCAR (tem));
273 real_this_command = XCDR (tem);
274 Frun_hooks (1, &Qkbd_macro_termination_hook);
275 return Qnil;
276 }
277
278 DEFUN ("execute-kbd-macro", Fexecute_kbd_macro, Sexecute_kbd_macro, 1, 2, 0,
279 "Execute MACRO as string of editor command characters.\n\
280 If MACRO is a symbol, its function definition is used.\n\
281 COUNT is a repeat count, or nil for once, or 0 for infinite loop.")
282 (macro, count)
283 Lisp_Object macro, count;
284 {
285 Lisp_Object final;
286 Lisp_Object tem;
287 int pdlcount = specpdl_ptr - specpdl;
288 int repeat = 1;
289 struct gcpro gcpro1;
290 int success_count = 0;
291
292 executing_macro_iterations = 0;
293
294 if (!NILP (count))
295 {
296 count = Fprefix_numeric_value (count);
297 repeat = XINT (count);
298 }
299
300 final = indirect_function (macro);
301 if (!STRINGP (final) && !VECTORP (final))
302 error ("Keyboard macros must be strings or vectors");
303
304 tem = Fcons (Vexecuting_macro,
305 Fcons (make_number (executing_macro_index),
306 real_this_command));
307 record_unwind_protect (pop_kbd_macro, tem);
308
309 GCPRO1 (final);
310 do
311 {
312 Vexecuting_macro = final;
313 executing_macro = final;
314 executing_macro_index = 0;
315
316 current_kboard->Vprefix_arg = Qnil;
317 command_loop_1 ();
318
319 executing_macro_iterations = ++success_count;
320
321 QUIT;
322 }
323 while (--repeat
324 && (STRINGP (Vexecuting_macro) || VECTORP (Vexecuting_macro)));
325
326 executing_macro = Qnil;
327
328 real_this_command = Vexecuting_macro;
329
330 UNGCPRO;
331 return unbind_to (pdlcount, Qnil);
332 }
333 \f
334 void
335 init_macros ()
336 {
337 Vexecuting_macro = Qnil;
338 executing_macro = Qnil;
339 }
340
341 void
342 syms_of_macros ()
343 {
344 Qexecute_kbd_macro = intern ("execute-kbd-macro");
345 staticpro (&Qexecute_kbd_macro);
346 Qkbd_macro_termination_hook = intern ("kbd-macro-termination-hook");
347 staticpro (&Qkbd_macro_termination_hook);
348
349 defsubr (&Sstart_kbd_macro);
350 defsubr (&Send_kbd_macro);
351 defsubr (&Scall_last_kbd_macro);
352 defsubr (&Sexecute_kbd_macro);
353 defsubr (&Scancel_kbd_macro_events);
354 defsubr (&Sstore_kbd_macro_event);
355
356 DEFVAR_KBOARD ("defining-kbd-macro", defining_kbd_macro,
357 "Non-nil while a keyboard macro is being defined. Don't set this!");
358
359 DEFVAR_LISP ("executing-macro", &Vexecuting_macro,
360 "Currently executing keyboard macro (string or vector); nil if none executing.");
361
362 DEFVAR_LISP_NOPRO ("executing-kbd-macro", &Vexecuting_macro,
363 "Currently executing keyboard macro (string or vector); nil if none executing.");
364
365 DEFVAR_KBOARD ("last-kbd-macro", Vlast_kbd_macro,
366 "Last kbd macro defined, as a string or vector; nil if none defined.");
367 }
368
369 void
370 keys_of_macros ()
371 {
372 initial_define_key (control_x_map, ('e'), "call-last-kbd-macro");
373 initial_define_key (control_x_map, ('('), "start-kbd-macro");
374 initial_define_key (control_x_map, (')'), "end-kbd-macro");
375 }