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