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