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