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