(Fdefalias): Doc fix.
[emacs.git] / src / macros.c
blob2b413d6d7d4d86943fa56e5695706a54b9737e62
1 /* Keyboard macros.
2 Copyright (C) 1985, 1986, 1993 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include <config.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"
30 Lisp_Object Qexecute_kbd_macro;
32 Lisp_Object Vexecuting_macro;
33 int executing_macro_index;
35 Lisp_Object Fexecute_kbd_macro ();
37 DEFUN ("start-kbd-macro", Fstart_kbd_macro, Sstart_kbd_macro, 1, 1, "P",
38 "Record subsequent keyboard input, defining a keyboard macro.\n\
39 The commands are recorded even as they are executed.\n\
40 Use \\[end-kbd-macro] to finish recording and make the macro available.\n\
41 Use \\[name-last-kbd-macro] to give it a permanent name.\n\
42 Non-nil arg (prefix arg) means append to last macro defined;\n\
43 This begins by re-executing that macro as if you typed it again.")
44 (append)
45 Lisp_Object append;
47 if (!NILP (current_kboard->defining_kbd_macro))
48 error ("Already defining kbd macro");
50 if (!current_kboard->kbd_macro_buffer)
52 current_kboard->kbd_macro_bufsize = 30;
53 current_kboard->kbd_macro_buffer
54 = (Lisp_Object *)xmalloc (30 * sizeof (Lisp_Object));
56 update_mode_lines++;
57 if (NILP (append))
59 if (current_kboard->kbd_macro_bufsize > 200)
61 current_kboard->kbd_macro_bufsize = 30;
62 current_kboard->kbd_macro_buffer
63 = (Lisp_Object *)xrealloc (current_kboard->kbd_macro_buffer,
64 30 * sizeof (Lisp_Object));
66 current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_buffer;
67 current_kboard->kbd_macro_end = current_kboard->kbd_macro_buffer;
68 message ("Defining kbd macro...");
70 else
72 message ("Appending to kbd macro...");
73 current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_end;
74 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro,
75 make_number (1));
77 current_kboard->defining_kbd_macro = Qt;
79 return Qnil;
82 DEFUN ("end-kbd-macro", Fend_kbd_macro, Send_kbd_macro, 0, 1, "p",
83 "Finish defining a keyboard macro.\n\
84 The definition was started by \\[start-kbd-macro].\n\
85 The macro is now available for use via \\[call-last-kbd-macro],\n\
86 or it can be given a name with \\[name-last-kbd-macro] and then invoked\n\
87 under that name.\n\
88 \n\
89 With numeric arg, repeat macro now that many times,\n\
90 counting the definition just completed as the first repetition.\n\
91 An argument of zero means repeat until error.")
92 (repeat)
93 Lisp_Object repeat;
95 if (NILP (current_kboard->defining_kbd_macro))
96 error ("Not defining kbd macro.");
98 if (NILP (repeat))
99 XSETFASTINT (repeat, 1);
100 else
101 CHECK_NUMBER (repeat, 0);
103 if (!NILP (current_kboard->defining_kbd_macro))
105 current_kboard->defining_kbd_macro = Qnil;
106 update_mode_lines++;
107 current_kboard->Vlast_kbd_macro
108 = make_event_array ((current_kboard->kbd_macro_end
109 - current_kboard->kbd_macro_buffer),
110 current_kboard->kbd_macro_buffer);
111 message ("Keyboard macro defined");
114 if (XFASTINT (repeat) == 0)
115 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, repeat);
116 else
118 XSETINT (repeat, XINT (repeat)-1);
119 if (XINT (repeat) > 0)
120 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, repeat);
122 return Qnil;
125 /* Store character c into kbd macro being defined */
127 store_kbd_macro_char (c)
128 Lisp_Object c;
130 if (!NILP (current_kboard->defining_kbd_macro))
132 if ((current_kboard->kbd_macro_ptr
133 - current_kboard->kbd_macro_buffer)
134 == current_kboard->kbd_macro_bufsize)
136 register Lisp_Object *new;
137 current_kboard->kbd_macro_bufsize *= 2;
138 new = (Lisp_Object *)xrealloc (current_kboard->kbd_macro_buffer,
139 (current_kboard->kbd_macro_bufsize
140 * sizeof (Lisp_Object)));
141 current_kboard->kbd_macro_ptr
142 += new - current_kboard->kbd_macro_buffer;
143 current_kboard->kbd_macro_end
144 += new - current_kboard->kbd_macro_buffer;
145 current_kboard->kbd_macro_buffer = new;
147 *current_kboard->kbd_macro_ptr++ = c;
151 /* Declare that all chars stored so far in the kbd macro being defined
152 really belong to it. This is done in between editor commands. */
154 finalize_kbd_macro_chars ()
156 current_kboard->kbd_macro_end = current_kboard->kbd_macro_ptr;
159 DEFUN ("cancel-kbd-macro-events", Fcancel_kbd_macro_events,
160 Scancel_kbd_macro_events, 0, 0, 0,
161 "Cancel the events added to a keyboard macro for this command.")
164 current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_end;
167 DEFUN ("store-kbd-macro-event", Fstore_kbd_macro_event,
168 Sstore_kbd_macro_event, 1, 1, 0,
169 "Store EVENT into the keyboard macro being defined.")
170 (event)
171 Lisp_Object event;
173 store_kbd_macro_char (event);
174 return Qnil;
177 DEFUN ("call-last-kbd-macro", Fcall_last_kbd_macro, Scall_last_kbd_macro,
178 0, 1, "p",
179 "Call the last keyboard macro that you defined with \\[start-kbd-macro].\n\
181 A prefix argument serves as a repeat count. Zero means repeat until error.\n\
183 To make a macro permanent so you can call it even after\n\
184 defining others, use \\[name-last-kbd-macro].")
185 (prefix)
186 Lisp_Object prefix;
188 if (! NILP (current_kboard->defining_kbd_macro))
189 error ("Can't execute anonymous macro while defining one");
190 else if (NILP (current_kboard->Vlast_kbd_macro))
191 error ("No kbd macro has been defined");
192 else
193 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, prefix);
194 return Qnil;
197 /* Restore Vexecuting_macro and executing_macro_index - called when
198 the unwind-protect in Fexecute_kbd_macro gets invoked. */
199 static Lisp_Object
200 pop_kbd_macro (info)
201 Lisp_Object info;
203 Lisp_Object tem;
204 Vexecuting_macro = Fcar (info);
205 tem = Fcdr (info);
206 executing_macro_index = XINT (tem);
207 return Qnil;
210 DEFUN ("execute-kbd-macro", Fexecute_kbd_macro, Sexecute_kbd_macro, 1, 2, 0,
211 "Execute MACRO as string of editor command characters.\n\
212 If MACRO is a symbol, its function definition is used.\n\
213 COUNT is a repeat count, or nil for once, or 0 for infinite loop.")
214 (macro, count)
215 Lisp_Object macro, count;
217 Lisp_Object final;
218 Lisp_Object tem;
219 int pdlcount = specpdl_ptr - specpdl;
220 int repeat = 1;
221 struct gcpro gcpro1;
223 if (!NILP (count))
225 count = Fprefix_numeric_value (count);
226 repeat = XINT (count);
229 final = indirect_function (macro);
230 if (!STRINGP (final) && !VECTORP (final))
231 error ("Keyboard macros must be strings or vectors.");
233 XSETFASTINT (tem, executing_macro_index);
234 tem = Fcons (Vexecuting_macro, tem);
235 record_unwind_protect (pop_kbd_macro, tem);
237 GCPRO1 (final);
240 Vexecuting_macro = final;
241 executing_macro_index = 0;
243 current_kboard->Vprefix_arg = Qnil;
244 command_loop_1 ();
246 QUIT;
248 while (--repeat
249 && (STRINGP (Vexecuting_macro) || VECTORP (Vexecuting_macro)));
251 UNGCPRO;
252 return unbind_to (pdlcount, Qnil);
255 init_macros ()
257 Vexecuting_macro = Qnil;
260 syms_of_macros ()
262 Qexecute_kbd_macro = intern ("execute-kbd-macro");
263 staticpro (&Qexecute_kbd_macro);
265 defsubr (&Sstart_kbd_macro);
266 defsubr (&Send_kbd_macro);
267 defsubr (&Scall_last_kbd_macro);
268 defsubr (&Sexecute_kbd_macro);
269 defsubr (&Scancel_kbd_macro_events);
270 defsubr (&Sstore_kbd_macro_event);
272 DEFVAR_KBOARD ("defining-kbd-macro", defining_kbd_macro,
273 "Non-nil while a keyboard macro is being defined. Don't set this!");
275 DEFVAR_LISP ("executing-macro", &Vexecuting_macro,
276 "Currently executing keyboard macro (string or vector); nil if none executing.");
278 DEFVAR_LISP_NOPRO ("executing-kbd-macro", &Vexecuting_macro,
279 "Currently executing keyboard macro (string or vector); nil if none executing.");
281 DEFVAR_KBOARD ("last-kbd-macro", Vlast_kbd_macro,
282 "Last kbd macro defined, as a string or vector; nil if none defined.");
285 keys_of_macros ()
287 initial_define_key (control_x_map, ('e'), "call-last-kbd-macro");
288 initial_define_key (control_x_map, ('('), "start-kbd-macro");
289 initial_define_key (control_x_map, (')'), "end-kbd-macro");