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