246
|
1 /* Keyboard macros.
|
35119
|
2 Copyright (C) 1985, 1986, 1993, 2000, 2001 Free Software Foundation, Inc.
|
246
|
3
|
|
4 This file is part of GNU Emacs.
|
|
5
|
|
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
|
647
|
8 the Free Software Foundation; either version 2, or (at your option)
|
246
|
9 any later version.
|
|
10
|
|
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.
|
|
15
|
|
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
|
14186
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
246
|
20
|
|
21
|
4696
|
22 #include <config.h>
|
246
|
23 #include "lisp.h"
|
|
24 #include "macros.h"
|
|
25 #include "commands.h"
|
|
26 #include "buffer.h"
|
|
27 #include "window.h"
|
11341
|
28 #include "keyboard.h"
|
246
|
29
|
35119
|
30 Lisp_Object Qexecute_kbd_macro, Qkbd_macro_termination_hook;
|
246
|
31
|
15968
|
32 /* Kbd macro currently being executed (a string or vector). */
|
|
33
|
246
|
34 Lisp_Object Vexecuting_macro;
|
15968
|
35
|
|
36 /* Index of next character to fetch from that macro. */
|
|
37
|
47307
|
38 EMACS_INT executing_macro_index;
|
246
|
39
|
15968
|
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
|
|
45 int executing_macro_iterations;
|
|
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
|
|
52 Lisp_Object executing_macro;
|
|
53
|
23434
|
54 extern Lisp_Object real_this_command;
|
|
55
|
246
|
56 Lisp_Object Fexecute_kbd_macro ();
|
|
57
|
46887
|
58 DEFUN ("start-kbd-macro", Fstart_kbd_macro, Sstart_kbd_macro, 1, 2, "P",
|
41940
|
59 doc: /* Record subsequent keyboard input, defining a keyboard macro.
|
|
60 The commands are recorded even as they are executed.
|
|
61 Use \\[end-kbd-macro] to finish recording and make the macro available.
|
|
62 Use \\[name-last-kbd-macro] to give it a permanent name.
|
|
63 Non-nil arg (prefix arg) means append to last macro defined;
|
46887
|
64 this begins by re-executing that macro as if you typed it again.
|
49600
|
65 If optional second arg, NO-EXEC, is non-nil, do not re-execute last
|
46887
|
66 macro before appending to it. */)
|
|
67 (append, no_exec)
|
|
68 Lisp_Object append, no_exec;
|
246
|
69 {
|
11009
|
70 if (!NILP (current_kboard->defining_kbd_macro))
|
246
|
71 error ("Already defining kbd macro");
|
|
72
|
11009
|
73 if (!current_kboard->kbd_macro_buffer)
|
10910
b0edc245c9b7
(defining_kbd_macro): Delete; now part of perdisplay. All uses changed.
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
74 {
|
11009
|
75 current_kboard->kbd_macro_bufsize = 30;
|
|
76 current_kboard->kbd_macro_buffer
|
14301
|
77 = (Lisp_Object *)xmalloc (30 * sizeof (Lisp_Object));
|
10910
b0edc245c9b7
(defining_kbd_macro): Delete; now part of perdisplay. All uses changed.
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
78 }
|
246
|
79 update_mode_lines++;
|
485
|
80 if (NILP (append))
|
246
|
81 {
|
14301
|
82 if (current_kboard->kbd_macro_bufsize > 200)
|
|
83 {
|
|
84 current_kboard->kbd_macro_bufsize = 30;
|
|
85 current_kboard->kbd_macro_buffer
|
15223
|
86 = (Lisp_Object *)xrealloc (current_kboard->kbd_macro_buffer,
|
|
87 30 * sizeof (Lisp_Object));
|
14301
|
88 }
|
11009
|
89 current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_buffer;
|
|
90 current_kboard->kbd_macro_end = current_kboard->kbd_macro_buffer;
|
14301
|
91 message ("Defining kbd macro...");
|
246
|
92 }
|
|
93 else
|
|
94 {
|
26825
|
95 int i, len;
|
49745
|
96 int cvt;
|
26825
|
97
|
|
98 /* Check the type of last-kbd-macro in case Lisp code changed it. */
|
|
99 if (!STRINGP (current_kboard->Vlast_kbd_macro)
|
|
100 && !VECTORP (current_kboard->Vlast_kbd_macro))
|
|
101 current_kboard->Vlast_kbd_macro
|
|
102 = wrong_type_argument (Qarrayp, current_kboard->Vlast_kbd_macro);
|
|
103
|
|
104 len = XINT (Flength (current_kboard->Vlast_kbd_macro));
|
|
105
|
|
106 /* Copy last-kbd-macro into the buffer, in case the Lisp code
|
|
107 has put another macro there. */
|
|
108 if (current_kboard->kbd_macro_bufsize < len + 30)
|
|
109 {
|
|
110 current_kboard->kbd_macro_bufsize = len + 30;
|
|
111 current_kboard->kbd_macro_buffer
|
|
112 = (Lisp_Object *)xrealloc (current_kboard->kbd_macro_buffer,
|
|
113 (len + 30) * sizeof (Lisp_Object));
|
|
114 }
|
49745
|
115
|
|
116 /* Must convert meta modifier when copying string to vector. */
|
|
117 cvt = STRINGP (current_kboard->Vlast_kbd_macro);
|
26825
|
118 for (i = 0; i < len; i++)
|
49745
|
119 {
|
|
120 Lisp_Object c;
|
|
121 c = Faref (current_kboard->Vlast_kbd_macro, make_number (i));
|
49913
|
122 if (cvt && NATNUMP (c) && (XFASTINT (c) & 0x80))
|
|
123 XSETFASTINT (c, CHAR_META | (XFASTINT (c) & ~0x80));
|
49745
|
124 current_kboard->kbd_macro_buffer[i] = c;
|
|
125 }
|
26825
|
126
|
|
127 current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_buffer + len;
|
|
128 current_kboard->kbd_macro_end = current_kboard->kbd_macro_ptr;
|
|
129
|
|
130 /* Re-execute the macro we are appending to,
|
|
131 for consistency of behavior. */
|
46887
|
132 if (NILP (no_exec))
|
|
133 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro,
|
|
134 make_number (1), Qnil);
|
26825
|
135
|
|
136 message ("Appending to kbd macro...");
|
246
|
137 }
|
11009
|
138 current_kboard->defining_kbd_macro = Qt;
|
49600
|
139
|
246
|
140 return Qnil;
|
|
141 }
|
|
142
|
47348
|
143 /* Finish defining the current keyboard macro. */
|
|
144
|
|
145 void
|
|
146 end_kbd_macro ()
|
|
147 {
|
|
148 current_kboard->defining_kbd_macro = Qnil;
|
|
149 update_mode_lines++;
|
|
150 current_kboard->Vlast_kbd_macro
|
|
151 = make_event_array ((current_kboard->kbd_macro_end
|
|
152 - current_kboard->kbd_macro_buffer),
|
|
153 current_kboard->kbd_macro_buffer);
|
|
154 }
|
|
155
|
46887
|
156 DEFUN ("end-kbd-macro", Fend_kbd_macro, Send_kbd_macro, 0, 2, "p",
|
41940
|
157 doc: /* Finish defining a keyboard macro.
|
|
158 The definition was started by \\[start-kbd-macro].
|
|
159 The macro is now available for use via \\[call-last-kbd-macro],
|
|
160 or it can be given a name with \\[name-last-kbd-macro] and then invoked
|
|
161 under that name.
|
|
162
|
|
163 With numeric arg, repeat macro now that many times,
|
|
164 counting the definition just completed as the first repetition.
|
46887
|
165 An argument of zero means repeat until error.
|
|
166
|
49600
|
167 In Lisp, optional second arg LOOPFUNC may be a function that is called prior to
|
46887
|
168 each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */)
|
|
169 (repeat, loopfunc)
|
|
170 Lisp_Object repeat, loopfunc;
|
246
|
171 {
|
11009
|
172 if (NILP (current_kboard->defining_kbd_macro))
|
22915
|
173 error ("Not defining kbd macro");
|
246
|
174
|
14081
|
175 if (NILP (repeat))
|
|
176 XSETFASTINT (repeat, 1);
|
246
|
177 else
|
40656
|
178 CHECK_NUMBER (repeat);
|
246
|
179
|
11009
|
180 if (!NILP (current_kboard->defining_kbd_macro))
|
246
|
181 {
|
47348
|
182 end_kbd_macro ();
|
14301
|
183 message ("Keyboard macro defined");
|
246
|
184 }
|
|
185
|
14081
|
186 if (XFASTINT (repeat) == 0)
|
46887
|
187 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, repeat, loopfunc);
|
246
|
188 else
|
|
189 {
|
14081
|
190 XSETINT (repeat, XINT (repeat)-1);
|
|
191 if (XINT (repeat) > 0)
|
46887
|
192 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, repeat, loopfunc);
|
246
|
193 }
|
|
194 return Qnil;
|
|
195 }
|
|
196
|
|
197 /* Store character c into kbd macro being defined */
|
|
198
|
20299
|
199 void
|
246
|
200 store_kbd_macro_char (c)
|
|
201 Lisp_Object c;
|
|
202 {
|
39135
|
203 struct kboard *kb = current_kboard;
|
|
204
|
|
205 if (!NILP (kb->defining_kbd_macro))
|
246
|
206 {
|
39135
|
207 if (kb->kbd_macro_ptr - kb->kbd_macro_buffer == kb->kbd_macro_bufsize)
|
246
|
208 {
|
39135
|
209 int ptr_offset, end_offset, nbytes;
|
49600
|
210
|
39135
|
211 ptr_offset = kb->kbd_macro_ptr - kb->kbd_macro_buffer;
|
|
212 end_offset = kb->kbd_macro_end - kb->kbd_macro_buffer;
|
|
213 kb->kbd_macro_bufsize *= 2;
|
|
214 nbytes = kb->kbd_macro_bufsize * sizeof *kb->kbd_macro_buffer;
|
|
215 kb->kbd_macro_buffer
|
|
216 = (Lisp_Object *) xrealloc (kb->kbd_macro_buffer, nbytes);
|
|
217 kb->kbd_macro_ptr = kb->kbd_macro_buffer + ptr_offset;
|
|
218 kb->kbd_macro_end = kb->kbd_macro_buffer + end_offset;
|
246
|
219 }
|
49600
|
220
|
39135
|
221 *kb->kbd_macro_ptr++ = c;
|
246
|
222 }
|
|
223 }
|
|
224
|
|
225 /* Declare that all chars stored so far in the kbd macro being defined
|
|
226 really belong to it. This is done in between editor commands. */
|
|
227
|
20299
|
228 void
|
246
|
229 finalize_kbd_macro_chars ()
|
|
230 {
|
11009
|
231 current_kboard->kbd_macro_end = current_kboard->kbd_macro_ptr;
|
246
|
232 }
|
12845
|
233
|
|
234 DEFUN ("cancel-kbd-macro-events", Fcancel_kbd_macro_events,
|
|
235 Scancel_kbd_macro_events, 0, 0, 0,
|
41940
|
236 doc: /* Cancel the events added to a keyboard macro for this command. */)
|
|
237 ()
|
12845
|
238 {
|
|
239 current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_end;
|
31829
|
240 return Qnil;
|
12845
|
241 }
|
13773
|
242
|
|
243 DEFUN ("store-kbd-macro-event", Fstore_kbd_macro_event,
|
|
244 Sstore_kbd_macro_event, 1, 1, 0,
|
41940
|
245 doc: /* Store EVENT into the keyboard macro being defined. */)
|
|
246 (event)
|
13773
|
247 Lisp_Object event;
|
|
248 {
|
|
249 store_kbd_macro_char (event);
|
|
250 return Qnil;
|
|
251 }
|
246
|
252
|
|
253 DEFUN ("call-last-kbd-macro", Fcall_last_kbd_macro, Scall_last_kbd_macro,
|
46887
|
254 0, 2, "p",
|
41940
|
255 doc: /* Call the last keyboard macro that you defined with \\[start-kbd-macro].
|
|
256
|
|
257 A prefix argument serves as a repeat count. Zero means repeat until error.
|
|
258
|
|
259 To make a macro permanent so you can call it even after
|
49600
|
260 defining others, use \\[name-last-kbd-macro].
|
46887
|
261
|
49600
|
262 In Lisp, optional second arg LOOPFUNC may be a function that is called prior to
|
46887
|
263 each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */)
|
|
264 (prefix, loopfunc)
|
|
265 Lisp_Object prefix, loopfunc;
|
246
|
266 {
|
16562
|
267 /* Don't interfere with recognition of the previous command
|
|
268 from before this macro started. */
|
22968
|
269 Vthis_command = current_kboard->Vlast_command;
|
24530
|
270 /* C-x z after the macro should repeat the macro. */
|
|
271 real_this_command = current_kboard->Vlast_kbd_macro;
|
16562
|
272
|
11009
|
273 if (! NILP (current_kboard->defining_kbd_macro))
|
246
|
274 error ("Can't execute anonymous macro while defining one");
|
11009
|
275 else if (NILP (current_kboard->Vlast_kbd_macro))
|
246
|
276 error ("No kbd macro has been defined");
|
|
277 else
|
46887
|
278 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, prefix, loopfunc);
|
16562
|
279
|
|
280 /* command_loop_1 sets this to nil before it returns;
|
|
281 get back the last command within the macro
|
|
282 so that it can be last, again, after we return. */
|
22968
|
283 Vthis_command = current_kboard->Vlast_command;
|
16562
|
284
|
246
|
285 return Qnil;
|
|
286 }
|
|
287
|
|
288 /* Restore Vexecuting_macro and executing_macro_index - called when
|
|
289 the unwind-protect in Fexecute_kbd_macro gets invoked. */
|
15968
|
290
|
246
|
291 static Lisp_Object
|
|
292 pop_kbd_macro (info)
|
|
293 Lisp_Object info;
|
|
294 {
|
|
295 Lisp_Object tem;
|
24530
|
296 Vexecuting_macro = XCAR (info);
|
|
297 tem = XCDR (info);
|
|
298 executing_macro_index = XINT (XCAR (tem));
|
|
299 real_this_command = XCDR (tem);
|
35119
|
300 Frun_hooks (1, &Qkbd_macro_termination_hook);
|
246
|
301 return Qnil;
|
|
302 }
|
|
303
|
46887
|
304 DEFUN ("execute-kbd-macro", Fexecute_kbd_macro, Sexecute_kbd_macro, 1, 3, 0,
|
41940
|
305 doc: /* Execute MACRO as string of editor command characters.
|
|
306 If MACRO is a symbol, its function definition is used.
|
49600
|
307 COUNT is a repeat count, or nil for once, or 0 for infinite loop.
|
46887
|
308
|
49600
|
309 Optional third arg LOOPFUNC may be a function that is called prior to
|
46887
|
310 each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */)
|
|
311 (macro, count, loopfunc)
|
|
312 Lisp_Object macro, count, loopfunc;
|
246
|
313 {
|
|
314 Lisp_Object final;
|
|
315 Lisp_Object tem;
|
46295
|
316 int pdlcount = SPECPDL_INDEX ();
|
246
|
317 int repeat = 1;
|
46887
|
318 struct gcpro gcpro1, gcpro2;
|
15968
|
319 int success_count = 0;
|
246
|
320
|
24482
9b722e922325
(Fexecute_kbd_macro): Initialize executing_macro_iterations at beginning.
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
321 executing_macro_iterations = 0;
|
9b722e922325
(Fexecute_kbd_macro): Initialize executing_macro_iterations at beginning.
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
322
|
14100
|
323 if (!NILP (count))
|
|
324 {
|
|
325 count = Fprefix_numeric_value (count);
|
|
326 repeat = XINT (count);
|
|
327 }
|
246
|
328
|
647
|
329 final = indirect_function (macro);
|
9122
|
330 if (!STRINGP (final) && !VECTORP (final))
|
22915
|
331 error ("Keyboard macros must be strings or vectors");
|
246
|
332
|
24530
|
333 tem = Fcons (Vexecuting_macro,
|
|
334 Fcons (make_number (executing_macro_index),
|
|
335 real_this_command));
|
246
|
336 record_unwind_protect (pop_kbd_macro, tem);
|
|
337
|
46887
|
338 GCPRO2 (final, loopfunc);
|
246
|
339 do
|
|
340 {
|
|
341 Vexecuting_macro = final;
|
15968
|
342 executing_macro = final;
|
246
|
343 executing_macro_index = 0;
|
|
344
|
12119
|
345 current_kboard->Vprefix_arg = Qnil;
|
46887
|
346
|
|
347 if (!NILP (loopfunc))
|
|
348 {
|
|
349 Lisp_Object cont;
|
|
350 cont = call0 (loopfunc);
|
|
351 if (NILP (cont))
|
|
352 break;
|
|
353 }
|
|
354
|
246
|
355 command_loop_1 ();
|
1682
|
356
|
15968
|
357 executing_macro_iterations = ++success_count;
|
|
358
|
1682
|
359 QUIT;
|
246
|
360 }
|
9122
|
361 while (--repeat
|
|
362 && (STRINGP (Vexecuting_macro) || VECTORP (Vexecuting_macro)));
|
246
|
363
|
15968
|
364 executing_macro = Qnil;
|
|
365
|
23434
|
366 real_this_command = Vexecuting_macro;
|
|
367
|
246
|
368 UNGCPRO;
|
14094
|
369 return unbind_to (pdlcount, Qnil);
|
246
|
370 }
|
|
371
|
20299
|
372 void
|
246
|
373 init_macros ()
|
|
374 {
|
|
375 Vexecuting_macro = Qnil;
|
15976
|
376 executing_macro = Qnil;
|
246
|
377 }
|
|
378
|
20299
|
379 void
|
246
|
380 syms_of_macros ()
|
|
381 {
|
|
382 Qexecute_kbd_macro = intern ("execute-kbd-macro");
|
|
383 staticpro (&Qexecute_kbd_macro);
|
35119
|
384 Qkbd_macro_termination_hook = intern ("kbd-macro-termination-hook");
|
|
385 staticpro (&Qkbd_macro_termination_hook);
|
246
|
386
|
|
387 defsubr (&Sstart_kbd_macro);
|
|
388 defsubr (&Send_kbd_macro);
|
|
389 defsubr (&Scall_last_kbd_macro);
|
|
390 defsubr (&Sexecute_kbd_macro);
|
12845
|
391 defsubr (&Scancel_kbd_macro_events);
|
13773
|
392 defsubr (&Sstore_kbd_macro_event);
|
246
|
393
|
11009
|
394 DEFVAR_KBOARD ("defining-kbd-macro", defining_kbd_macro,
|
57962
|
395 doc: /* Non-nil while a keyboard macro is being defined. Don't set this!
|
|
396 The value is the symbol `append' while appending to the definition of
|
|
397 an existing macro. */);
|
246
|
398
|
|
399 DEFVAR_LISP ("executing-macro", &Vexecuting_macro,
|
41940
|
400 doc: /* Currently executing keyboard macro (string or vector); nil if none executing. */);
|
246
|
401
|
47307
|
402 DEFVAR_INT ("executing-macro-index", &executing_macro_index,
|
|
403 doc: /* Index in currently executing keyboard macro; undefined if none executing. */);
|
|
404
|
246
|
405 DEFVAR_LISP_NOPRO ("executing-kbd-macro", &Vexecuting_macro,
|
41940
|
406 doc: /* Currently executing keyboard macro (string or vector); nil if none executing. */);
|
246
|
407
|
11009
|
408 DEFVAR_KBOARD ("last-kbd-macro", Vlast_kbd_macro,
|
41940
|
409 doc: /* Last kbd macro defined, as a string or vector; nil if none defined. */);
|
246
|
410 }
|
52401
|
411
|
|
412 /* arch-tag: d293fcc9-2266-4163-9198-7fa0de12ec9e
|
|
413 (do not change this comment) */
|