Mercurial > emacs
comparison src/macros.c @ 46887:d9b11a5b4ebf
(Fstart_kbd_macro): Added NO-EXEC argument to inhibit
executing macro before appending to it (when used from Lisp).
(Fexecute_kbd_macro): Added LOOPFUNC argument to supply function
which is called prior to each iteration of macro (for kmacro.el).
(Fend_kbd_macro, Fcall_last_kbd_macro): Likewise.
author | Kim F. Storm <storm@cua.dk> |
---|---|
date | Wed, 14 Aug 2002 10:35:31 +0000 |
parents | b77ef572e1c9 |
children | 06c376f5df86 |
comparison
equal
deleted
inserted
replaced
46886:1bc111036f11 | 46887:d9b11a5b4ebf |
---|---|
53 | 53 |
54 extern Lisp_Object real_this_command; | 54 extern Lisp_Object real_this_command; |
55 | 55 |
56 Lisp_Object Fexecute_kbd_macro (); | 56 Lisp_Object Fexecute_kbd_macro (); |
57 | 57 |
58 DEFUN ("start-kbd-macro", Fstart_kbd_macro, Sstart_kbd_macro, 1, 1, "P", | 58 DEFUN ("start-kbd-macro", Fstart_kbd_macro, Sstart_kbd_macro, 1, 2, "P", |
59 doc: /* Record subsequent keyboard input, defining a keyboard macro. | 59 doc: /* Record subsequent keyboard input, defining a keyboard macro. |
60 The commands are recorded even as they are executed. | 60 The commands are recorded even as they are executed. |
61 Use \\[end-kbd-macro] to finish recording and make the macro available. | 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. | 62 Use \\[name-last-kbd-macro] to give it a permanent name. |
63 Non-nil arg (prefix arg) means append to last macro defined; | 63 Non-nil arg (prefix arg) means append to last macro defined; |
64 this begins by re-executing that macro as if you typed it again. */) | 64 this begins by re-executing that macro as if you typed it again. |
65 (append) | 65 If optional second arg, NO-EXEC, is non-nil, do not re-execute last |
66 Lisp_Object append; | 66 macro before appending to it. */) |
67 (append, no_exec) | |
68 Lisp_Object append, no_exec; | |
67 { | 69 { |
68 if (!NILP (current_kboard->defining_kbd_macro)) | 70 if (!NILP (current_kboard->defining_kbd_macro)) |
69 error ("Already defining kbd macro"); | 71 error ("Already defining kbd macro"); |
70 | 72 |
71 if (!current_kboard->kbd_macro_buffer) | 73 if (!current_kboard->kbd_macro_buffer) |
116 current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_buffer + len; | 118 current_kboard->kbd_macro_ptr = current_kboard->kbd_macro_buffer + len; |
117 current_kboard->kbd_macro_end = current_kboard->kbd_macro_ptr; | 119 current_kboard->kbd_macro_end = current_kboard->kbd_macro_ptr; |
118 | 120 |
119 /* Re-execute the macro we are appending to, | 121 /* Re-execute the macro we are appending to, |
120 for consistency of behavior. */ | 122 for consistency of behavior. */ |
121 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, | 123 if (NILP (no_exec)) |
122 make_number (1)); | 124 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, |
125 make_number (1), Qnil); | |
123 | 126 |
124 message ("Appending to kbd macro..."); | 127 message ("Appending to kbd macro..."); |
125 } | 128 } |
126 current_kboard->defining_kbd_macro = Qt; | 129 current_kboard->defining_kbd_macro = Qt; |
127 | 130 |
128 return Qnil; | 131 return Qnil; |
129 } | 132 } |
130 | 133 |
131 DEFUN ("end-kbd-macro", Fend_kbd_macro, Send_kbd_macro, 0, 1, "p", | 134 DEFUN ("end-kbd-macro", Fend_kbd_macro, Send_kbd_macro, 0, 2, "p", |
132 doc: /* Finish defining a keyboard macro. | 135 doc: /* Finish defining a keyboard macro. |
133 The definition was started by \\[start-kbd-macro]. | 136 The definition was started by \\[start-kbd-macro]. |
134 The macro is now available for use via \\[call-last-kbd-macro], | 137 The macro is now available for use via \\[call-last-kbd-macro], |
135 or it can be given a name with \\[name-last-kbd-macro] and then invoked | 138 or it can be given a name with \\[name-last-kbd-macro] and then invoked |
136 under that name. | 139 under that name. |
137 | 140 |
138 With numeric arg, repeat macro now that many times, | 141 With numeric arg, repeat macro now that many times, |
139 counting the definition just completed as the first repetition. | 142 counting the definition just completed as the first repetition. |
140 An argument of zero means repeat until error. */) | 143 An argument of zero means repeat until error. |
141 (repeat) | 144 |
142 Lisp_Object repeat; | 145 In Lisp, optional second arg LOOPFUNC may be a function that is called prior to |
146 each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */) | |
147 (repeat, loopfunc) | |
148 Lisp_Object repeat, loopfunc; | |
143 { | 149 { |
144 if (NILP (current_kboard->defining_kbd_macro)) | 150 if (NILP (current_kboard->defining_kbd_macro)) |
145 error ("Not defining kbd macro"); | 151 error ("Not defining kbd macro"); |
146 | 152 |
147 if (NILP (repeat)) | 153 if (NILP (repeat)) |
159 current_kboard->kbd_macro_buffer); | 165 current_kboard->kbd_macro_buffer); |
160 message ("Keyboard macro defined"); | 166 message ("Keyboard macro defined"); |
161 } | 167 } |
162 | 168 |
163 if (XFASTINT (repeat) == 0) | 169 if (XFASTINT (repeat) == 0) |
164 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, repeat); | 170 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, repeat, loopfunc); |
165 else | 171 else |
166 { | 172 { |
167 XSETINT (repeat, XINT (repeat)-1); | 173 XSETINT (repeat, XINT (repeat)-1); |
168 if (XINT (repeat) > 0) | 174 if (XINT (repeat) > 0) |
169 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, repeat); | 175 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, repeat, loopfunc); |
170 } | 176 } |
171 return Qnil; | 177 return Qnil; |
172 } | 178 } |
173 | 179 |
174 /* Store character c into kbd macro being defined */ | 180 /* Store character c into kbd macro being defined */ |
226 store_kbd_macro_char (event); | 232 store_kbd_macro_char (event); |
227 return Qnil; | 233 return Qnil; |
228 } | 234 } |
229 | 235 |
230 DEFUN ("call-last-kbd-macro", Fcall_last_kbd_macro, Scall_last_kbd_macro, | 236 DEFUN ("call-last-kbd-macro", Fcall_last_kbd_macro, Scall_last_kbd_macro, |
231 0, 1, "p", | 237 0, 2, "p", |
232 doc: /* Call the last keyboard macro that you defined with \\[start-kbd-macro]. | 238 doc: /* Call the last keyboard macro that you defined with \\[start-kbd-macro]. |
233 | 239 |
234 A prefix argument serves as a repeat count. Zero means repeat until error. | 240 A prefix argument serves as a repeat count. Zero means repeat until error. |
235 | 241 |
236 To make a macro permanent so you can call it even after | 242 To make a macro permanent so you can call it even after |
237 defining others, use \\[name-last-kbd-macro]. */) | 243 defining others, use \\[name-last-kbd-macro]. |
238 (prefix) | 244 |
239 Lisp_Object prefix; | 245 In Lisp, optional second arg LOOPFUNC may be a function that is called prior to |
246 each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */) | |
247 (prefix, loopfunc) | |
248 Lisp_Object prefix, loopfunc; | |
240 { | 249 { |
241 /* Don't interfere with recognition of the previous command | 250 /* Don't interfere with recognition of the previous command |
242 from before this macro started. */ | 251 from before this macro started. */ |
243 Vthis_command = current_kboard->Vlast_command; | 252 Vthis_command = current_kboard->Vlast_command; |
244 /* C-x z after the macro should repeat the macro. */ | 253 /* C-x z after the macro should repeat the macro. */ |
247 if (! NILP (current_kboard->defining_kbd_macro)) | 256 if (! NILP (current_kboard->defining_kbd_macro)) |
248 error ("Can't execute anonymous macro while defining one"); | 257 error ("Can't execute anonymous macro while defining one"); |
249 else if (NILP (current_kboard->Vlast_kbd_macro)) | 258 else if (NILP (current_kboard->Vlast_kbd_macro)) |
250 error ("No kbd macro has been defined"); | 259 error ("No kbd macro has been defined"); |
251 else | 260 else |
252 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, prefix); | 261 Fexecute_kbd_macro (current_kboard->Vlast_kbd_macro, prefix, loopfunc); |
253 | 262 |
254 /* command_loop_1 sets this to nil before it returns; | 263 /* command_loop_1 sets this to nil before it returns; |
255 get back the last command within the macro | 264 get back the last command within the macro |
256 so that it can be last, again, after we return. */ | 265 so that it can be last, again, after we return. */ |
257 Vthis_command = current_kboard->Vlast_command; | 266 Vthis_command = current_kboard->Vlast_command; |
273 real_this_command = XCDR (tem); | 282 real_this_command = XCDR (tem); |
274 Frun_hooks (1, &Qkbd_macro_termination_hook); | 283 Frun_hooks (1, &Qkbd_macro_termination_hook); |
275 return Qnil; | 284 return Qnil; |
276 } | 285 } |
277 | 286 |
278 DEFUN ("execute-kbd-macro", Fexecute_kbd_macro, Sexecute_kbd_macro, 1, 2, 0, | 287 DEFUN ("execute-kbd-macro", Fexecute_kbd_macro, Sexecute_kbd_macro, 1, 3, 0, |
279 doc: /* Execute MACRO as string of editor command characters. | 288 doc: /* Execute MACRO as string of editor command characters. |
280 If MACRO is a symbol, its function definition is used. | 289 If MACRO is a symbol, its function definition is used. |
281 COUNT is a repeat count, or nil for once, or 0 for infinite loop. */) | 290 COUNT is a repeat count, or nil for once, or 0 for infinite loop. |
282 (macro, count) | 291 |
283 Lisp_Object macro, count; | 292 Optional third arg LOOPFUNC may be a function that is called prior to |
293 each iteration of the macro. Iteration stops if LOOPFUNC returns nil. */) | |
294 (macro, count, loopfunc) | |
295 Lisp_Object macro, count, loopfunc; | |
284 { | 296 { |
285 Lisp_Object final; | 297 Lisp_Object final; |
286 Lisp_Object tem; | 298 Lisp_Object tem; |
287 int pdlcount = SPECPDL_INDEX (); | 299 int pdlcount = SPECPDL_INDEX (); |
288 int repeat = 1; | 300 int repeat = 1; |
289 struct gcpro gcpro1; | 301 struct gcpro gcpro1, gcpro2; |
290 int success_count = 0; | 302 int success_count = 0; |
291 | 303 |
292 executing_macro_iterations = 0; | 304 executing_macro_iterations = 0; |
293 | 305 |
294 if (!NILP (count)) | 306 if (!NILP (count)) |
304 tem = Fcons (Vexecuting_macro, | 316 tem = Fcons (Vexecuting_macro, |
305 Fcons (make_number (executing_macro_index), | 317 Fcons (make_number (executing_macro_index), |
306 real_this_command)); | 318 real_this_command)); |
307 record_unwind_protect (pop_kbd_macro, tem); | 319 record_unwind_protect (pop_kbd_macro, tem); |
308 | 320 |
309 GCPRO1 (final); | 321 GCPRO2 (final, loopfunc); |
310 do | 322 do |
311 { | 323 { |
312 Vexecuting_macro = final; | 324 Vexecuting_macro = final; |
313 executing_macro = final; | 325 executing_macro = final; |
314 executing_macro_index = 0; | 326 executing_macro_index = 0; |
315 | 327 |
316 current_kboard->Vprefix_arg = Qnil; | 328 current_kboard->Vprefix_arg = Qnil; |
329 | |
330 if (!NILP (loopfunc)) | |
331 { | |
332 Lisp_Object cont; | |
333 cont = call0 (loopfunc); | |
334 if (NILP (cont)) | |
335 break; | |
336 } | |
337 | |
317 command_loop_1 (); | 338 command_loop_1 (); |
318 | 339 |
319 executing_macro_iterations = ++success_count; | 340 executing_macro_iterations = ++success_count; |
320 | 341 |
321 QUIT; | 342 QUIT; |