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