Commit | Line | Data |
---|---|---|
d60d9f65 SS |
1 | /* macro.c -- keyboard macros for readline. */ |
2 | ||
cc88a640 | 3 | /* Copyright (C) 1994-2009 Free Software Foundation, Inc. |
d60d9f65 | 4 | |
cc88a640 JK |
5 | This file is part of the GNU Readline Library (Readline), a library |
6 | for reading lines of text with interactive input and history editing. | |
d60d9f65 | 7 | |
cc88a640 JK |
8 | Readline is free software: you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation, either version 3 of the License, or | |
d60d9f65 SS |
11 | (at your option) any later version. |
12 | ||
cc88a640 JK |
13 | Readline is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
d60d9f65 SS |
16 | GNU General Public License for more details. |
17 | ||
cc88a640 JK |
18 | You should have received a copy of the GNU General Public License |
19 | along with Readline. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
d60d9f65 SS |
22 | #define READLINE_LIBRARY |
23 | ||
24 | #if defined (HAVE_CONFIG_H) | |
25 | # include <config.h> | |
26 | #endif | |
27 | ||
28 | #include <sys/types.h> | |
29 | ||
30 | #if defined (HAVE_UNISTD_H) | |
31 | # include <unistd.h> /* for _POSIX_VERSION */ | |
32 | #endif /* HAVE_UNISTD_H */ | |
33 | ||
34 | #if defined (HAVE_STDLIB_H) | |
35 | # include <stdlib.h> | |
36 | #else | |
37 | # include "ansi_stdlib.h" | |
38 | #endif /* HAVE_STDLIB_H */ | |
39 | ||
40 | #include <stdio.h> | |
41 | ||
42 | /* System-specific feature definitions and include files. */ | |
43 | #include "rldefs.h" | |
44 | ||
45 | /* Some standard library routines. */ | |
46 | #include "readline.h" | |
47 | #include "history.h" | |
48 | ||
1b17e766 EZ |
49 | #include "rlprivate.h" |
50 | #include "xmalloc.h" | |
d60d9f65 | 51 | |
d60d9f65 SS |
52 | /* **************************************************************** */ |
53 | /* */ | |
54 | /* Hacking Keyboard Macros */ | |
55 | /* */ | |
56 | /* **************************************************************** */ | |
57 | ||
d60d9f65 SS |
58 | /* The currently executing macro string. If this is non-zero, |
59 | then it is a malloc ()'ed string where input is coming from. */ | |
9255ee31 | 60 | char *rl_executing_macro = (char *)NULL; |
d60d9f65 SS |
61 | |
62 | /* The offset in the above string to the next character to be read. */ | |
63 | static int executing_macro_index; | |
64 | ||
65 | /* The current macro string being built. Characters get stuffed | |
66 | in here by add_macro_char (). */ | |
67 | static char *current_macro = (char *)NULL; | |
68 | ||
69 | /* The size of the buffer allocated to current_macro. */ | |
70 | static int current_macro_size; | |
71 | ||
72 | /* The index at which characters are being added to current_macro. */ | |
73 | static int current_macro_index; | |
74 | ||
75 | /* A structure used to save nested macro strings. | |
76 | It is a linked list of string/index for each saved macro. */ | |
77 | struct saved_macro { | |
78 | struct saved_macro *next; | |
79 | char *string; | |
80 | int sindex; | |
81 | }; | |
82 | ||
83 | /* The list of saved macros. */ | |
84 | static struct saved_macro *macro_list = (struct saved_macro *)NULL; | |
85 | ||
86 | /* Set up to read subsequent input from STRING. | |
87 | STRING is free ()'ed when we are done with it. */ | |
88 | void | |
89 | _rl_with_macro_input (string) | |
90 | char *string; | |
91 | { | |
92 | _rl_push_executing_macro (); | |
9255ee31 | 93 | rl_executing_macro = string; |
d60d9f65 | 94 | executing_macro_index = 0; |
9255ee31 | 95 | RL_SETSTATE(RL_STATE_MACROINPUT); |
d60d9f65 SS |
96 | } |
97 | ||
98 | /* Return the next character available from a macro, or 0 if | |
99 | there are no macro characters. */ | |
100 | int | |
101 | _rl_next_macro_key () | |
102 | { | |
5bdf8622 DJ |
103 | int c; |
104 | ||
9255ee31 | 105 | if (rl_executing_macro == 0) |
d60d9f65 SS |
106 | return (0); |
107 | ||
9255ee31 | 108 | if (rl_executing_macro[executing_macro_index] == 0) |
d60d9f65 SS |
109 | { |
110 | _rl_pop_executing_macro (); | |
111 | return (_rl_next_macro_key ()); | |
112 | } | |
113 | ||
5bdf8622 DJ |
114 | #if defined (READLINE_CALLBACKS) |
115 | c = rl_executing_macro[executing_macro_index++]; | |
cc88a640 | 116 | if (RL_ISSTATE (RL_STATE_CALLBACK) && RL_ISSTATE (RL_STATE_READCMD|RL_STATE_MOREINPUT) && rl_executing_macro[executing_macro_index] == 0) |
5bdf8622 DJ |
117 | _rl_pop_executing_macro (); |
118 | return c; | |
119 | #else | |
9255ee31 | 120 | return (rl_executing_macro[executing_macro_index++]); |
5bdf8622 | 121 | #endif |
d60d9f65 SS |
122 | } |
123 | ||
124 | /* Save the currently executing macro on a stack of saved macros. */ | |
125 | void | |
126 | _rl_push_executing_macro () | |
127 | { | |
128 | struct saved_macro *saver; | |
129 | ||
130 | saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro)); | |
131 | saver->next = macro_list; | |
132 | saver->sindex = executing_macro_index; | |
9255ee31 | 133 | saver->string = rl_executing_macro; |
d60d9f65 SS |
134 | |
135 | macro_list = saver; | |
136 | } | |
137 | ||
138 | /* Discard the current macro, replacing it with the one | |
139 | on the top of the stack of saved macros. */ | |
140 | void | |
141 | _rl_pop_executing_macro () | |
142 | { | |
143 | struct saved_macro *macro; | |
144 | ||
9255ee31 EZ |
145 | FREE (rl_executing_macro); |
146 | rl_executing_macro = (char *)NULL; | |
d60d9f65 SS |
147 | executing_macro_index = 0; |
148 | ||
149 | if (macro_list) | |
150 | { | |
151 | macro = macro_list; | |
9255ee31 | 152 | rl_executing_macro = macro_list->string; |
d60d9f65 SS |
153 | executing_macro_index = macro_list->sindex; |
154 | macro_list = macro_list->next; | |
cc88a640 | 155 | xfree (macro); |
d60d9f65 | 156 | } |
9255ee31 EZ |
157 | |
158 | if (rl_executing_macro == 0) | |
159 | RL_UNSETSTATE(RL_STATE_MACROINPUT); | |
d60d9f65 SS |
160 | } |
161 | ||
162 | /* Add a character to the macro being built. */ | |
163 | void | |
164 | _rl_add_macro_char (c) | |
165 | int c; | |
166 | { | |
167 | if (current_macro_index + 1 >= current_macro_size) | |
168 | { | |
169 | if (current_macro == 0) | |
9255ee31 | 170 | current_macro = (char *)xmalloc (current_macro_size = 25); |
d60d9f65 | 171 | else |
9255ee31 | 172 | current_macro = (char *)xrealloc (current_macro, current_macro_size += 25); |
d60d9f65 SS |
173 | } |
174 | ||
175 | current_macro[current_macro_index++] = c; | |
176 | current_macro[current_macro_index] = '\0'; | |
177 | } | |
178 | ||
179 | void | |
180 | _rl_kill_kbd_macro () | |
181 | { | |
182 | if (current_macro) | |
183 | { | |
cc88a640 | 184 | xfree (current_macro); |
d60d9f65 SS |
185 | current_macro = (char *) NULL; |
186 | } | |
187 | current_macro_size = current_macro_index = 0; | |
188 | ||
9255ee31 EZ |
189 | FREE (rl_executing_macro); |
190 | rl_executing_macro = (char *) NULL; | |
d60d9f65 SS |
191 | executing_macro_index = 0; |
192 | ||
9255ee31 | 193 | RL_UNSETSTATE(RL_STATE_MACRODEF); |
d60d9f65 SS |
194 | } |
195 | ||
196 | /* Begin defining a keyboard macro. | |
197 | Keystrokes are recorded as they are executed. | |
198 | End the definition with rl_end_kbd_macro (). | |
199 | If a numeric argument was explicitly typed, then append this | |
200 | definition to the end of the existing macro, and start by | |
201 | re-executing the existing macro. */ | |
202 | int | |
203 | rl_start_kbd_macro (ignore1, ignore2) | |
204 | int ignore1, ignore2; | |
205 | { | |
9255ee31 | 206 | if (RL_ISSTATE (RL_STATE_MACRODEF)) |
d60d9f65 SS |
207 | { |
208 | _rl_abort_internal (); | |
5836a818 | 209 | return -1; |
d60d9f65 SS |
210 | } |
211 | ||
212 | if (rl_explicit_arg) | |
213 | { | |
214 | if (current_macro) | |
215 | _rl_with_macro_input (savestring (current_macro)); | |
216 | } | |
217 | else | |
218 | current_macro_index = 0; | |
219 | ||
9255ee31 | 220 | RL_SETSTATE(RL_STATE_MACRODEF); |
d60d9f65 SS |
221 | return 0; |
222 | } | |
223 | ||
224 | /* Stop defining a keyboard macro. | |
225 | A numeric argument says to execute the macro right now, | |
226 | that many times, counting the definition as the first time. */ | |
227 | int | |
228 | rl_end_kbd_macro (count, ignore) | |
229 | int count, ignore; | |
230 | { | |
9255ee31 | 231 | if (RL_ISSTATE (RL_STATE_MACRODEF) == 0) |
d60d9f65 SS |
232 | { |
233 | _rl_abort_internal (); | |
5836a818 | 234 | return -1; |
d60d9f65 SS |
235 | } |
236 | ||
5836a818 | 237 | current_macro_index -= rl_key_sequence_length - 1; |
d60d9f65 SS |
238 | current_macro[current_macro_index] = '\0'; |
239 | ||
9255ee31 | 240 | RL_UNSETSTATE(RL_STATE_MACRODEF); |
d60d9f65 SS |
241 | |
242 | return (rl_call_last_kbd_macro (--count, 0)); | |
243 | } | |
244 | ||
245 | /* Execute the most recently defined keyboard macro. | |
246 | COUNT says how many times to execute it. */ | |
247 | int | |
248 | rl_call_last_kbd_macro (count, ignore) | |
249 | int count, ignore; | |
250 | { | |
251 | if (current_macro == 0) | |
252 | _rl_abort_internal (); | |
253 | ||
9255ee31 | 254 | if (RL_ISSTATE (RL_STATE_MACRODEF)) |
d60d9f65 | 255 | { |
9255ee31 | 256 | rl_ding (); /* no recursive macros */ |
d60d9f65 SS |
257 | current_macro[--current_macro_index] = '\0'; /* erase this char */ |
258 | return 0; | |
259 | } | |
260 | ||
261 | while (count--) | |
262 | _rl_with_macro_input (savestring (current_macro)); | |
263 | return 0; | |
264 | } | |
265 | ||
266 | void | |
267 | rl_push_macro_input (macro) | |
268 | char *macro; | |
269 | { | |
270 | _rl_with_macro_input (macro); | |
271 | } |