|
|
1.1 root 1: /* Keyboard macros.
2: Copyright (C) 1985, 1986 Free Software Foundation, Inc.
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
8: the Free Software Foundation; either version 1, or (at your option)
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
18: the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19:
20:
21: #include "config.h"
22: #include "lisp.h"
23: #include "macros.h"
24: #include "commands.h"
25: #include "buffer.h"
26: #include "window.h"
27:
28: int defining_kbd_macro;
29:
30: char *kbd_macro_buffer;
31: char *kbd_macro_ptr;
32: char *kbd_macro_end;
33: int kbd_macro_bufsize;
34: Lisp_Object Vlast_kbd_macro;
35:
36: Lisp_Object Vexecuting_macro;
37: int executing_macro_index;
38:
39: Lisp_Object Fexecute_kbd_macro ();
40:
41: DEFUN ("start-kbd-macro", Fstart_kbd_macro, Sstart_kbd_macro, 1, 1, "P",
42: "Record subsequent keyboard input, defining a keyboard macro.\n\
43: The commands are recorded even as they are executed.\n\
44: Use \\[end-kbd-macro] to finish recording and make the macro available.\n\
45: Use \\[name-last-kbd-macro] to give it a permanent name.\n\
46: Non-nil arg (prefix arg) means append to last macro defined;\n\
47: This begins by re-executing that macro as if you typed it again.")
48: (append)
49: Lisp_Object append;
50: {
51: if (defining_kbd_macro)
52: error ("Already defining kbd macro");
53:
54: update_mode_lines++;
55: if (NULL (append))
56: {
57: kbd_macro_ptr = kbd_macro_buffer;
58: kbd_macro_end = kbd_macro_buffer;
59: message("Defining kbd macro...");
60: }
61: else
62: {
63: message("Appending to kbd macro...");
64: kbd_macro_ptr = kbd_macro_end;
65: Fexecute_kbd_macro (Vlast_kbd_macro, make_number (1));
66: }
67: defining_kbd_macro = 1;
68:
69: return Qnil;
70: }
71:
72: DEFUN ("end-kbd-macro", Fend_kbd_macro, Send_kbd_macro, 0, 1, "p",
73: "Finish defining a keyboard macro.\n\
74: The definition was started by \\[start-kbd-macro].\n\
75: The macro is now available for use via \\[call-last-kbd-macro],\n\
76: or it can be given a name with \\[name-last-kbd-macro] and then invoked\n\
77: under that name.\n\
78: With numeric arg, repeat macro now that many times,\n\
79: counting the definition just completed as the first repetition.")
80: (arg)
81: Lisp_Object arg;
82: {
83: if (!defining_kbd_macro)
84: error ("Not defining kbd macro.");
85:
86: if (NULL (arg))
87: XFASTINT (arg) = 1;
88: else
89: CHECK_NUMBER (arg, 0);
90:
91: if (defining_kbd_macro)
92: {
93: defining_kbd_macro = 0;
94: update_mode_lines++;
95: Vlast_kbd_macro = make_string (kbd_macro_buffer,
96: kbd_macro_end - kbd_macro_buffer);
97: message("Keyboard macro defined");
98: }
99:
100: if (XFASTINT (arg) == 0)
101: Fexecute_kbd_macro (Vlast_kbd_macro, arg);
102: else
103: {
104: XFASTINT (arg)--;
105: if (XFASTINT (arg) > 0)
106: Fexecute_kbd_macro (Vlast_kbd_macro, arg);
107: }
108: return Qnil;
109: }
110:
111: /* Store character c into kbd macro being defined */
112:
113: store_kbd_macro_char (c)
114: unsigned char c;
115: {
116: if (defining_kbd_macro)
117: {
118: if (kbd_macro_ptr - kbd_macro_buffer == kbd_macro_bufsize)
119: {
120: register char *new = (char *) xrealloc (kbd_macro_buffer, kbd_macro_bufsize *= 2);
121: kbd_macro_ptr += new - kbd_macro_buffer;
122: kbd_macro_end = new + kbd_macro_bufsize;
123: kbd_macro_buffer = new;
124: }
125: *kbd_macro_ptr++ = c;
126: }
127: }
128:
129: /* Declare that all chars stored so far in the kbd macro being defined
130: really belong to it. This is done in between editor commands. */
131:
132: finalize_kbd_macro_chars ()
133: {
134: kbd_macro_end = kbd_macro_ptr;
135: }
136:
137: DEFUN ("call-last-kbd-macro", Fcall_last_kbd_macro, Scall_last_kbd_macro,
138: 0, 1, "p",
139: "Call the last keyboard macro that you defined with \\[start-kbd-macro].\n\
140: To make a macro permanent so you can call it even after\n\
141: defining others, use \\[name-last-kbd-macro].")
142: (prefix)
143: Lisp_Object prefix;
144: {
145: if (defining_kbd_macro)
146: error ("Can't execute anonymous macro while defining one");
147: else if (NULL (Vlast_kbd_macro))
148: error ("No kbd macro has been defined");
149: else
150: Fexecute_kbd_macro (Vlast_kbd_macro, prefix);
151: return Qnil;
152: }
153:
154: static Lisp_Object
155: pop_kbd_macro (info)
156: Lisp_Object info;
157: {
158: Lisp_Object tem;
159: Vexecuting_macro = Fcar (info);
160: tem = Fcdr (info);
161: executing_macro_index = XINT (tem);
162: return Qnil;
163: }
164:
165: DEFUN ("execute-kbd-macro", Fexecute_kbd_macro, Sexecute_kbd_macro, 1, 2, 0,
166: "Execute MACRO as string of editor command characters.\n\
167: If MACRO is a symbol, its function definition is used.\n\
168: COUNT is a repeat count, or nil for once, or 0 for infinite loop.")
169: (macro, prefixarg)
170: Lisp_Object macro, prefixarg;
171: {
172: Lisp_Object final;
173: Lisp_Object tem;
174: int count = specpdl_ptr - specpdl;
175: int repeat = 1;
176: struct gcpro gcpro1;
177:
178: if (!NULL (prefixarg))
179: prefixarg = Fprefix_numeric_value (prefixarg),
180: repeat = XINT (prefixarg);
181:
182: final = macro;
183: while (XTYPE (final) == Lisp_Symbol && !EQ (final, Qunbound))
184: final = XSYMBOL (final)->function;
185: CHECK_STRING (final, 0);
186:
187: XFASTINT (tem) = executing_macro_index;
188: tem = Fcons (Vexecuting_macro, tem);
189: record_unwind_protect (pop_kbd_macro, tem);
190:
191: GCPRO1 (final);
192: do
193: {
194: Vexecuting_macro = final;
195: executing_macro_index = 0;
196:
197: command_loop_1 ();
198: }
199: while (--repeat && XTYPE (Vexecuting_macro) == Lisp_String);
200:
201: UNGCPRO;
202: unbind_to (count);
203:
204: return Qnil;
205: }
206:
207: init_macros ()
208: {
209: Vlast_kbd_macro = Qnil;
210: defining_kbd_macro = 0;
211:
212: Vexecuting_macro = Qnil;
213: }
214:
215: syms_of_macros ()
216: {
217: kbd_macro_bufsize = 100;
218: kbd_macro_buffer = (char *) malloc (kbd_macro_bufsize);
219:
220: defsubr (&Sstart_kbd_macro);
221: defsubr (&Send_kbd_macro);
222: defsubr (&Scall_last_kbd_macro);
223: defsubr (&Sexecute_kbd_macro);
224:
225: DEFVAR_BOOL ("defining-kbd-macro", &defining_kbd_macro,
226: "Non-nil means store keyboard input into kbd macro being defined.");
227:
228: DEFVAR_LISP ("executing-macro", &Vexecuting_macro,
229: "Currently executing keyboard macro (a string); nil if none executing.");
230:
231: DEFVAR_LISP_NOPRO ("executing-kbd-macro", &Vexecuting_macro,
232: "Currently executing keyboard macro (a string); nil if none executing.");
233:
234: DEFVAR_LISP ("last-kbd-macro", &Vlast_kbd_macro,
235: "Last kbd macro defined, as a string; nil if none defined.");
236: }
237:
238: keys_of_macros ()
239: {
240: ndefkey (Vctl_x_map, ('e'), "call-last-kbd-macro");
241: ndefkey (Vctl_x_map, ('('), "start-kbd-macro");
242: ndefkey (Vctl_x_map, (')'), "end-kbd-macro");
243: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.