Add arch taglines
[bpt/emacs.git] / src / macros.c
CommitLineData
5a7f5d07 1/* Keyboard macros.
2136b408 2 Copyright (C) 1985, 1986, 1993, 2000, 2001 Free Software Foundation, Inc.
5a7f5d07
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
502ddf23 8the Free Software Foundation; either version 2, or (at your option)
5a7f5d07
JB
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
5a7f5d07
JB
20
21
18160b98 22#include <config.h>
5a7f5d07
JB
23#include "lisp.h"
24#include "macros.h"
25#include "commands.h"
26#include "buffer.h"
27#include "window.h"
077d751f 28#include "keyboard.h"
5a7f5d07 29
2136b408 30Lisp_Object Qexecute_kbd_macro, Qkbd_macro_termination_hook;
5a7f5d07 31
d4087e06
RS
32/* Kbd macro currently being executed (a string or vector). */
33
5a7f5d07 34Lisp_Object Vexecuting_macro;
d4087e06
RS
35
36/* Index of next character to fetch from that macro. */
37
64799df5 38EMACS_INT executing_macro_index;
5a7f5d07 39
d4087e06
RS
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
45int 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
52Lisp_Object executing_macro;
53
b80d5655
RS
54extern Lisp_Object real_this_command;
55
5a7f5d07
JB
56Lisp_Object Fexecute_kbd_macro ();
57\f
f90d3a6b 58DEFUN ("start-kbd-macro", Fstart_kbd_macro, Sstart_kbd_macro, 1, 2, "P",
70da46c3
PJ
59 doc: /* Record subsequent keyboard input, defining a keyboard macro.
60The commands are recorded even as they are executed.
61Use \\[end-kbd-macro] to finish recording and make the macro available.
62Use \\[name-last-kbd-macro] to give it a permanent name.
63Non-nil arg (prefix arg) means append to last macro defined;
f90d3a6b 64this begins by re-executing that macro as if you typed it again.
177c0ea7 65If optional second arg, NO-EXEC, is non-nil, do not re-execute last
f90d3a6b
KS
66macro before appending to it. */)
67 (append, no_exec)
68 Lisp_Object append, no_exec;
5a7f5d07 69{
cd8b5aa3 70 if (!NILP (current_kboard->defining_kbd_macro))
5a7f5d07
JB
71 error ("Already defining kbd macro");
72
cd8b5aa3 73 if (!current_kboard->kbd_macro_buffer)
8b97da83 74 {
cd8b5aa3
KH
75 current_kboard->kbd_macro_bufsize = 30;
76 current_kboard->kbd_macro_buffer
9e7c370a 77 = (Lisp_Object *)xmalloc (30 * sizeof (Lisp_Object));
8b97da83 78 }
5a7f5d07 79 update_mode_lines++;
265a9e55 80 if (NILP (append))
5a7f5d07 81 {
9e7c370a
KH
82 if (current_kboard->kbd_macro_bufsize > 200)
83 {
84 current_kboard->kbd_macro_bufsize = 30;
85 current_kboard->kbd_macro_buffer
96a60349
KH
86 = (Lisp_Object *)xrealloc (current_kboard->kbd_macro_buffer,
87 30 * sizeof (Lisp_Object));
9e7c370a 88 }
cd8b5aa3
KH
89 current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_buffer;
90 current_kboard->kbd_macro_end = current_kboard->kbd_macro_buffer;
9e7c370a 91 message ("Defining kbd macro...");
5a7f5d07
JB
92 }
93 else
94 {
f84db7d5 95 int i, len;
295fff2c 96 int cvt;
f84db7d5
RS
97
98 /* Check the type of last-kbd-macro in case Lisp code changed it. */
99 if (!STRINGP (current_kboard->Vlast_kbd_macro)
100 && !VECTORP (current_kboard->Vlast_kbd_macro))
101 current_kboard->Vlast_kbd_macro
102 = wrong_type_argument (Qarrayp, current_kboard->Vlast_kbd_macro);
103
104 len = XINT (Flength (current_kboard->Vlast_kbd_macro));
105
106 /* Copy last-kbd-macro into the buffer, in case the Lisp code
107 has put another macro there. */
108 if (current_kboard->kbd_macro_bufsize < len + 30)
109 {
110 current_kboard->kbd_macro_bufsize = len + 30;
111 current_kboard->kbd_macro_buffer
112 = (Lisp_Object *)xrealloc (current_kboard->kbd_macro_buffer,
113 (len + 30) * sizeof (Lisp_Object));
114 }
295fff2c
KS
115
116 /* Must convert meta modifier when copying string to vector. */
117 cvt = STRINGP (current_kboard->Vlast_kbd_macro);
f84db7d5 118 for (i = 0; i < len; i++)
295fff2c
KS
119 {
120 Lisp_Object c;
121 c = Faref (current_kboard->Vlast_kbd_macro, make_number (i));
3499aebf
SM
122 if (cvt && NATNUMP (c) && (XFASTINT (c) & 0x80))
123 XSETFASTINT (c, CHAR_META | (XFASTINT (c) & ~0x80));
295fff2c
KS
124 current_kboard->kbd_macro_buffer[i] = c;
125 }
f84db7d5
RS
126
127 current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_buffer + len;
128 current_kboard->kbd_macro_end = current_kboard->kbd_macro_ptr;
129
130 /* Re-execute the macro we are appending to,
131 for consistency of behavior. */
f90d3a6b
KS
132 if (NILP (no_exec))
133 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro,
134 make_number (1), Qnil);
f84db7d5
RS
135
136 message ("Appending to kbd macro...");
5a7f5d07 137 }
cd8b5aa3 138 current_kboard->defining_kbd_macro = Qt;
177c0ea7 139
5a7f5d07
JB
140 return Qnil;
141}
142
eeb8fc0d
KS
143/* Finish defining the current keyboard macro. */
144
145void
146end_kbd_macro ()
147{
148 current_kboard->defining_kbd_macro = Qnil;
149 update_mode_lines++;
150 current_kboard->Vlast_kbd_macro
151 = make_event_array ((current_kboard->kbd_macro_end
152 - current_kboard->kbd_macro_buffer),
153 current_kboard->kbd_macro_buffer);
154}
155
f90d3a6b 156DEFUN ("end-kbd-macro", Fend_kbd_macro, Send_kbd_macro, 0, 2, "p",
70da46c3
PJ
157 doc: /* Finish defining a keyboard macro.
158The definition was started by \\[start-kbd-macro].
159The macro is now available for use via \\[call-last-kbd-macro],
160or it can be given a name with \\[name-last-kbd-macro] and then invoked
161under that name.
162
163With numeric arg, repeat macro now that many times,
164counting the definition just completed as the first repetition.
f90d3a6b
KS
165An argument of zero means repeat until error.
166
177c0ea7 167In Lisp, optional second arg LOOPFUNC may be a function that is called prior to
f90d3a6b
KS
168each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */)
169 (repeat, loopfunc)
170 Lisp_Object repeat, loopfunc;
5a7f5d07 171{
cd8b5aa3 172 if (NILP (current_kboard->defining_kbd_macro))
d86ad277 173 error ("Not defining kbd macro");
5a7f5d07 174
86a3ca5e
EN
175 if (NILP (repeat))
176 XSETFASTINT (repeat, 1);
5a7f5d07 177 else
b7826503 178 CHECK_NUMBER (repeat);
5a7f5d07 179
cd8b5aa3 180 if (!NILP (current_kboard->defining_kbd_macro))
5a7f5d07 181 {
eeb8fc0d 182 end_kbd_macro ();
9e7c370a 183 message ("Keyboard macro defined");
5a7f5d07
JB
184 }
185
86a3ca5e 186 if (XFASTINT (repeat) == 0)
f90d3a6b 187 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, repeat, loopfunc);
5a7f5d07
JB
188 else
189 {
86a3ca5e
EN
190 XSETINT (repeat, XINT (repeat)-1);
191 if (XINT (repeat) > 0)
f90d3a6b 192 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, repeat, loopfunc);
5a7f5d07
JB
193 }
194 return Qnil;
195}
196
197/* Store character c into kbd macro being defined */
198
c3fd8dd5 199void
5a7f5d07
JB
200store_kbd_macro_char (c)
201 Lisp_Object c;
202{
08e1d6df
GM
203 struct kboard *kb = current_kboard;
204
205 if (!NILP (kb->defining_kbd_macro))
5a7f5d07 206 {
08e1d6df 207 if (kb->kbd_macro_ptr - kb->kbd_macro_buffer == kb->kbd_macro_bufsize)
5a7f5d07 208 {
08e1d6df 209 int ptr_offset, end_offset, nbytes;
177c0ea7 210
08e1d6df
GM
211 ptr_offset = kb->kbd_macro_ptr - kb->kbd_macro_buffer;
212 end_offset = kb->kbd_macro_end - kb->kbd_macro_buffer;
213 kb->kbd_macro_bufsize *= 2;
214 nbytes = kb->kbd_macro_bufsize * sizeof *kb->kbd_macro_buffer;
215 kb->kbd_macro_buffer
216 = (Lisp_Object *) xrealloc (kb->kbd_macro_buffer, nbytes);
217 kb->kbd_macro_ptr = kb->kbd_macro_buffer + ptr_offset;
218 kb->kbd_macro_end = kb->kbd_macro_buffer + end_offset;
5a7f5d07 219 }
177c0ea7 220
08e1d6df 221 *kb->kbd_macro_ptr++ = c;
5a7f5d07
JB
222 }
223}
224
225/* Declare that all chars stored so far in the kbd macro being defined
226 really belong to it. This is done in between editor commands. */
227
c3fd8dd5 228void
5a7f5d07
JB
229finalize_kbd_macro_chars ()
230{
cd8b5aa3 231 current_kboard->kbd_macro_end = current_kboard->kbd_macro_ptr;
5a7f5d07 232}
199afd29
RS
233
234DEFUN ("cancel-kbd-macro-events", Fcancel_kbd_macro_events,
235 Scancel_kbd_macro_events, 0, 0, 0,
70da46c3
PJ
236 doc: /* Cancel the events added to a keyboard macro for this command. */)
237 ()
199afd29
RS
238{
239 current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_end;
6bbd7a29 240 return Qnil;
199afd29 241}
2d5f65a9
KH
242
243DEFUN ("store-kbd-macro-event", Fstore_kbd_macro_event,
244 Sstore_kbd_macro_event, 1, 1, 0,
70da46c3
PJ
245 doc: /* Store EVENT into the keyboard macro being defined. */)
246 (event)
2d5f65a9
KH
247 Lisp_Object event;
248{
249 store_kbd_macro_char (event);
250 return Qnil;
251}
5a7f5d07
JB
252\f
253DEFUN ("call-last-kbd-macro", Fcall_last_kbd_macro, Scall_last_kbd_macro,
f90d3a6b 254 0, 2, "p",
70da46c3
PJ
255 doc: /* Call the last keyboard macro that you defined with \\[start-kbd-macro].
256
257A prefix argument serves as a repeat count. Zero means repeat until error.
258
259To make a macro permanent so you can call it even after
177c0ea7 260defining others, use \\[name-last-kbd-macro].
f90d3a6b 261
177c0ea7 262In Lisp, optional second arg LOOPFUNC may be a function that is called prior to
f90d3a6b
KS
263each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */)
264 (prefix, loopfunc)
265 Lisp_Object prefix, loopfunc;
5a7f5d07 266{
4315204e
RS
267 /* Don't interfere with recognition of the previous command
268 from before this macro started. */
14a18790 269 Vthis_command = current_kboard->Vlast_command;
80184dac
KH
270 /* C-x z after the macro should repeat the macro. */
271 real_this_command = current_kboard->Vlast_kbd_macro;
4315204e 272
cd8b5aa3 273 if (! NILP (current_kboard->defining_kbd_macro))
5a7f5d07 274 error ("Can't execute anonymous macro while defining one");
cd8b5aa3 275 else if (NILP (current_kboard->Vlast_kbd_macro))
5a7f5d07
JB
276 error ("No kbd macro has been defined");
277 else
f90d3a6b 278 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, prefix, loopfunc);
4315204e
RS
279
280 /* command_loop_1 sets this to nil before it returns;
281 get back the last command within the macro
282 so that it can be last, again, after we return. */
14a18790 283 Vthis_command = current_kboard->Vlast_command;
4315204e 284
5a7f5d07
JB
285 return Qnil;
286}
287
288/* Restore Vexecuting_macro and executing_macro_index - called when
289 the unwind-protect in Fexecute_kbd_macro gets invoked. */
d4087e06 290
5a7f5d07
JB
291static Lisp_Object
292pop_kbd_macro (info)
293 Lisp_Object info;
294{
295 Lisp_Object tem;
80184dac
KH
296 Vexecuting_macro = XCAR (info);
297 tem = XCDR (info);
298 executing_macro_index = XINT (XCAR (tem));
299 real_this_command = XCDR (tem);
2136b408 300 Frun_hooks (1, &Qkbd_macro_termination_hook);
5a7f5d07
JB
301 return Qnil;
302}
303
f90d3a6b 304DEFUN ("execute-kbd-macro", Fexecute_kbd_macro, Sexecute_kbd_macro, 1, 3, 0,
70da46c3
PJ
305 doc: /* Execute MACRO as string of editor command characters.
306If MACRO is a symbol, its function definition is used.
177c0ea7 307COUNT is a repeat count, or nil for once, or 0 for infinite loop.
f90d3a6b 308
177c0ea7 309Optional third arg LOOPFUNC may be a function that is called prior to
f90d3a6b
KS
310each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */)
311 (macro, count, loopfunc)
312 Lisp_Object macro, count, loopfunc;
5a7f5d07
JB
313{
314 Lisp_Object final;
315 Lisp_Object tem;
65b21658 316 int pdlcount = SPECPDL_INDEX ();
5a7f5d07 317 int repeat = 1;
f90d3a6b 318 struct gcpro gcpro1, gcpro2;
d4087e06 319 int success_count = 0;
5a7f5d07 320
4b7da890
KH
321 executing_macro_iterations = 0;
322
1c8c5693
EN
323 if (!NILP (count))
324 {
325 count = Fprefix_numeric_value (count);
326 repeat = XINT (count);
327 }
5a7f5d07 328
502ddf23 329 final = indirect_function (macro);
65346ae6 330 if (!STRINGP (final) && !VECTORP (final))
d86ad277 331 error ("Keyboard macros must be strings or vectors");
5a7f5d07 332
80184dac
KH
333 tem = Fcons (Vexecuting_macro,
334 Fcons (make_number (executing_macro_index),
335 real_this_command));
5a7f5d07
JB
336 record_unwind_protect (pop_kbd_macro, tem);
337
f90d3a6b 338 GCPRO2 (final, loopfunc);
5a7f5d07
JB
339 do
340 {
341 Vexecuting_macro = final;
d4087e06 342 executing_macro = final;
5a7f5d07
JB
343 executing_macro_index = 0;
344
04609ce4 345 current_kboard->Vprefix_arg = Qnil;
f90d3a6b
KS
346
347 if (!NILP (loopfunc))
348 {
349 Lisp_Object cont;
350 cont = call0 (loopfunc);
351 if (NILP (cont))
352 break;
353 }
354
5a7f5d07 355 command_loop_1 ();
e86f81cc 356
d4087e06
RS
357 executing_macro_iterations = ++success_count;
358
e86f81cc 359 QUIT;
5a7f5d07 360 }
65346ae6
KH
361 while (--repeat
362 && (STRINGP (Vexecuting_macro) || VECTORP (Vexecuting_macro)));
5a7f5d07 363
d4087e06
RS
364 executing_macro = Qnil;
365
b80d5655
RS
366 real_this_command = Vexecuting_macro;
367
5a7f5d07 368 UNGCPRO;
52d9c145 369 return unbind_to (pdlcount, Qnil);
5a7f5d07
JB
370}
371\f
c3fd8dd5 372void
5a7f5d07
JB
373init_macros ()
374{
5a7f5d07 375 Vexecuting_macro = Qnil;
1ab778be 376 executing_macro = Qnil;
5a7f5d07
JB
377}
378
c3fd8dd5 379void
5a7f5d07
JB
380syms_of_macros ()
381{
5a7f5d07
JB
382 Qexecute_kbd_macro = intern ("execute-kbd-macro");
383 staticpro (&Qexecute_kbd_macro);
2136b408
GM
384 Qkbd_macro_termination_hook = intern ("kbd-macro-termination-hook");
385 staticpro (&Qkbd_macro_termination_hook);
5a7f5d07
JB
386
387 defsubr (&Sstart_kbd_macro);
388 defsubr (&Send_kbd_macro);
389 defsubr (&Scall_last_kbd_macro);
390 defsubr (&Sexecute_kbd_macro);
199afd29 391 defsubr (&Scancel_kbd_macro_events);
2d5f65a9 392 defsubr (&Sstore_kbd_macro_event);
5a7f5d07 393
cd8b5aa3 394 DEFVAR_KBOARD ("defining-kbd-macro", defining_kbd_macro,
70da46c3 395 doc: /* Non-nil while a keyboard macro is being defined. Don't set this! */);
5a7f5d07
JB
396
397 DEFVAR_LISP ("executing-macro", &Vexecuting_macro,
70da46c3 398 doc: /* Currently executing keyboard macro (string or vector); nil if none executing. */);
5a7f5d07 399
64799df5
KS
400 DEFVAR_INT ("executing-macro-index", &executing_macro_index,
401 doc: /* Index in currently executing keyboard macro; undefined if none executing. */);
402
5a7f5d07 403 DEFVAR_LISP_NOPRO ("executing-kbd-macro", &Vexecuting_macro,
70da46c3 404 doc: /* Currently executing keyboard macro (string or vector); nil if none executing. */);
5a7f5d07 405
cd8b5aa3 406 DEFVAR_KBOARD ("last-kbd-macro", Vlast_kbd_macro,
70da46c3 407 doc: /* Last kbd macro defined, as a string or vector; nil if none defined. */);
5a7f5d07 408}
ab5796a9
MB
409
410/* arch-tag: d293fcc9-2266-4163-9198-7fa0de12ec9e
411 (do not change this comment) */