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