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)
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. */
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.")
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
));
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 (30 * sizeof (Lisp_Object
));
65 current_kboard
->kbd_macro_ptr
= current_kboard
->kbd_macro_buffer
;
66 current_kboard
->kbd_macro_end
= current_kboard
->kbd_macro_buffer
;
67 message ("Defining kbd macro...");
71 message ("Appending to kbd macro...");
72 current_kboard
->kbd_macro_ptr
= current_kboard
->kbd_macro_end
;
73 Fexecute_kbd_macro (current_kboard
->Vlast_kbd_macro
,
76 current_kboard
->defining_kbd_macro
= Qt
;
81 DEFUN ("end-kbd-macro", Fend_kbd_macro
, Send_kbd_macro
, 0, 1, "p",
82 "Finish defining a keyboard macro.\n\
83 The definition was started by \\[start-kbd-macro].\n\
84 The macro is now available for use via \\[call-last-kbd-macro],\n\
85 or it can be given a name with \\[name-last-kbd-macro] and then invoked\n\
88 With numeric arg, repeat macro now that many times,\n\
89 counting the definition just completed as the first repetition.\n\
90 An argument of zero means repeat until error.")
94 if (NILP (current_kboard
->defining_kbd_macro
))
95 error ("Not defining kbd macro.");
98 XSETFASTINT (repeat
, 1);
100 CHECK_NUMBER (repeat
, 0);
102 if (!NILP (current_kboard
->defining_kbd_macro
))
104 current_kboard
->defining_kbd_macro
= Qnil
;
106 current_kboard
->Vlast_kbd_macro
107 = make_event_array ((current_kboard
->kbd_macro_end
108 - current_kboard
->kbd_macro_buffer
),
109 current_kboard
->kbd_macro_buffer
);
110 message ("Keyboard macro defined");
113 if (XFASTINT (repeat
) == 0)
114 Fexecute_kbd_macro (current_kboard
->Vlast_kbd_macro
, repeat
);
117 XSETINT (repeat
, XINT (repeat
)-1);
118 if (XINT (repeat
) > 0)
119 Fexecute_kbd_macro (current_kboard
->Vlast_kbd_macro
, repeat
);
124 /* Store character c into kbd macro being defined */
126 store_kbd_macro_char (c
)
129 if (!NILP (current_kboard
->defining_kbd_macro
))
131 if ((current_kboard
->kbd_macro_ptr
132 - current_kboard
->kbd_macro_buffer
)
133 == current_kboard
->kbd_macro_bufsize
)
135 register Lisp_Object
*new;
136 current_kboard
->kbd_macro_bufsize
*= 2;
137 new = (Lisp_Object
*)xrealloc (current_kboard
->kbd_macro_buffer
,
138 (current_kboard
->kbd_macro_bufsize
139 * sizeof (Lisp_Object
)));
140 current_kboard
->kbd_macro_ptr
141 += new - current_kboard
->kbd_macro_buffer
;
142 current_kboard
->kbd_macro_end
143 += new - current_kboard
->kbd_macro_buffer
;
144 current_kboard
->kbd_macro_buffer
= new;
146 *current_kboard
->kbd_macro_ptr
++ = c
;
150 /* Declare that all chars stored so far in the kbd macro being defined
151 really belong to it. This is done in between editor commands. */
153 finalize_kbd_macro_chars ()
155 current_kboard
->kbd_macro_end
= current_kboard
->kbd_macro_ptr
;
158 DEFUN ("cancel-kbd-macro-events", Fcancel_kbd_macro_events
,
159 Scancel_kbd_macro_events
, 0, 0, 0,
160 "Cancel the events added to a keyboard macro for this command.")
163 current_kboard
->kbd_macro_ptr
= current_kboard
->kbd_macro_end
;
166 DEFUN ("store-kbd-macro-event", Fstore_kbd_macro_event
,
167 Sstore_kbd_macro_event
, 1, 1, 0,
168 "Store EVENT into the keyboard macro being defined.")
172 store_kbd_macro_char (event
);
176 DEFUN ("call-last-kbd-macro", Fcall_last_kbd_macro
, Scall_last_kbd_macro
,
178 "Call the last keyboard macro that you defined with \\[start-kbd-macro].\n\
180 A prefix argument serves as a repeat count. Zero means repeat until error.\n\
182 To make a macro permanent so you can call it even after\n\
183 defining others, use \\[name-last-kbd-macro].")
187 if (! NILP (current_kboard
->defining_kbd_macro
))
188 error ("Can't execute anonymous macro while defining one");
189 else if (NILP (current_kboard
->Vlast_kbd_macro
))
190 error ("No kbd macro has been defined");
192 Fexecute_kbd_macro (current_kboard
->Vlast_kbd_macro
, prefix
);
196 /* Restore Vexecuting_macro and executing_macro_index - called when
197 the unwind-protect in Fexecute_kbd_macro gets invoked. */
203 Vexecuting_macro
= Fcar (info
);
205 executing_macro_index
= XINT (tem
);
209 DEFUN ("execute-kbd-macro", Fexecute_kbd_macro
, Sexecute_kbd_macro
, 1, 2, 0,
210 "Execute MACRO as string of editor command characters.\n\
211 If MACRO is a symbol, its function definition is used.\n\
212 COUNT is a repeat count, or nil for once, or 0 for infinite loop.")
214 Lisp_Object macro
, count
;
218 int pdlcount
= specpdl_ptr
- specpdl
;
224 count
= Fprefix_numeric_value (count
);
225 repeat
= XINT (count
);
228 final
= indirect_function (macro
);
229 if (!STRINGP (final
) && !VECTORP (final
))
230 error ("Keyboard macros must be strings or vectors.");
232 XSETFASTINT (tem
, executing_macro_index
);
233 tem
= Fcons (Vexecuting_macro
, tem
);
234 record_unwind_protect (pop_kbd_macro
, tem
);
239 Vexecuting_macro
= final
;
240 executing_macro_index
= 0;
242 current_kboard
->Vprefix_arg
= Qnil
;
248 && (STRINGP (Vexecuting_macro
) || VECTORP (Vexecuting_macro
)));
251 return unbind_to (pdlcount
, Qnil
);
256 Vexecuting_macro
= Qnil
;
261 Qexecute_kbd_macro
= intern ("execute-kbd-macro");
262 staticpro (&Qexecute_kbd_macro
);
264 defsubr (&Sstart_kbd_macro
);
265 defsubr (&Send_kbd_macro
);
266 defsubr (&Scall_last_kbd_macro
);
267 defsubr (&Sexecute_kbd_macro
);
268 defsubr (&Scancel_kbd_macro_events
);
269 defsubr (&Sstore_kbd_macro_event
);
271 DEFVAR_KBOARD ("defining-kbd-macro", defining_kbd_macro
,
272 "Non-nil while a keyboard macro is being defined. Don't set this!");
274 DEFVAR_LISP ("executing-macro", &Vexecuting_macro
,
275 "Currently executing keyboard macro (string or vector); nil if none executing.");
277 DEFVAR_LISP_NOPRO ("executing-kbd-macro", &Vexecuting_macro
,
278 "Currently executing keyboard macro (string or vector); nil if none executing.");
280 DEFVAR_KBOARD ("last-kbd-macro", Vlast_kbd_macro
,
281 "Last kbd macro defined, as a string or vector; nil if none defined.");
286 initial_define_key (control_x_map
, ('e'), "call-last-kbd-macro");
287 initial_define_key (control_x_map
, ('('), "start-kbd-macro");
288 initial_define_key (control_x_map
, (')'), "end-kbd-macro");