1 /* macro.c -- keyboard macros for readline. */
3 /* Copyright (C) 1994-2009,2017 Free Software Foundation, Inc.
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.
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
11 (at your option) any later version.
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
16 GNU General Public License for more details.
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/>.
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
28 #include <sys/types.h>
30 #if defined (HAVE_UNISTD_H)
31 # include <unistd.h> /* for _POSIX_VERSION */
32 #endif /* HAVE_UNISTD_H */
34 #if defined (HAVE_STDLIB_H)
37 # include "ansi_stdlib.h"
38 #endif /* HAVE_STDLIB_H */
42 /* System-specific feature definitions and include files. */
45 /* Some standard library routines. */
49 #include "rlprivate.h"
52 #define MAX_MACRO_LEVEL 16
54 /* **************************************************************** */
56 /* Hacking Keyboard Macros */
58 /* **************************************************************** */
60 /* The currently executing macro string. If this is non-zero,
61 then it is a malloc ()'ed string where input is coming from. */
62 char *rl_executing_macro = (char *)NULL;
64 /* The offset in the above string to the next character to be read. */
65 static int executing_macro_index;
67 /* The current macro string being built. Characters get stuffed
68 in here by add_macro_char (). */
69 static char *current_macro = (char *)NULL;
71 /* The size of the buffer allocated to current_macro. */
72 static int current_macro_size;
74 /* The index at which characters are being added to current_macro. */
75 static int current_macro_index;
77 /* A structure used to save nested macro strings.
78 It is a linked list of string/index for each saved macro. */
80 struct saved_macro *next;
85 /* The list of saved macros. */
86 static struct saved_macro *macro_list = (struct saved_macro *)NULL;
88 static int macro_level = 0;
90 /* Set up to read subsequent input from STRING.
91 STRING is free ()'ed when we are done with it. */
93 _rl_with_macro_input (char *string)
95 if (macro_level > MAX_MACRO_LEVEL)
97 _rl_errmsg ("maximum macro execution nesting level exceeded");
98 _rl_abort_internal ();
103 if (rl_executing_macro) /* XXX - later */
105 _rl_push_executing_macro ();
106 rl_executing_macro = string;
107 executing_macro_index = 0;
108 RL_SETSTATE(RL_STATE_MACROINPUT);
111 /* Return the next character available from a macro, or 0 if
112 there are no macro characters. */
114 _rl_next_macro_key (void)
118 if (rl_executing_macro == 0)
121 if (rl_executing_macro[executing_macro_index] == 0)
123 _rl_pop_executing_macro ();
124 return (_rl_next_macro_key ());
127 #if defined (READLINE_CALLBACKS)
128 c = rl_executing_macro[executing_macro_index++];
129 if (RL_ISSTATE (RL_STATE_CALLBACK) && RL_ISSTATE (RL_STATE_READCMD|RL_STATE_MOREINPUT) && rl_executing_macro[executing_macro_index] == 0)
130 _rl_pop_executing_macro ();
133 /* XXX - consider doing the same as the callback code, just not testing
134 whether we're running in callback mode */
135 return (rl_executing_macro[executing_macro_index++]);
140 _rl_peek_macro_key (void)
142 if (rl_executing_macro == 0)
144 if (rl_executing_macro[executing_macro_index] == 0 && (macro_list == 0 || macro_list->string == 0))
146 if (rl_executing_macro[executing_macro_index] == 0 && macro_list && macro_list->string)
147 return (macro_list->string[0]);
148 return (rl_executing_macro[executing_macro_index]);
152 _rl_prev_macro_key (void)
154 if (rl_executing_macro == 0)
157 if (executing_macro_index == 0)
160 executing_macro_index--;
161 return (rl_executing_macro[executing_macro_index]);
164 /* Save the currently executing macro on a stack of saved macros. */
166 _rl_push_executing_macro (void)
168 struct saved_macro *saver;
170 saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
171 saver->next = macro_list;
172 saver->sindex = executing_macro_index;
173 saver->string = rl_executing_macro;
180 /* Discard the current macro, replacing it with the one
181 on the top of the stack of saved macros. */
183 _rl_pop_executing_macro (void)
185 struct saved_macro *macro;
187 FREE (rl_executing_macro);
188 rl_executing_macro = (char *)NULL;
189 executing_macro_index = 0;
194 rl_executing_macro = macro_list->string;
195 executing_macro_index = macro_list->sindex;
196 macro_list = macro_list->next;
202 if (rl_executing_macro == 0)
203 RL_UNSETSTATE(RL_STATE_MACROINPUT);
206 /* Add a character to the macro being built. */
208 _rl_add_macro_char (int c)
210 if (current_macro_index + 1 >= current_macro_size)
212 if (current_macro == 0)
213 current_macro = (char *)xmalloc (current_macro_size = 25);
215 current_macro = (char *)xrealloc (current_macro, current_macro_size += 25);
218 current_macro[current_macro_index++] = c;
219 current_macro[current_macro_index] = '\0';
223 _rl_kill_kbd_macro (void)
227 xfree (current_macro);
228 current_macro = (char *) NULL;
230 current_macro_size = current_macro_index = 0;
232 FREE (rl_executing_macro);
233 rl_executing_macro = (char *) NULL;
234 executing_macro_index = 0;
236 RL_UNSETSTATE(RL_STATE_MACRODEF);
239 /* Begin defining a keyboard macro.
240 Keystrokes are recorded as they are executed.
241 End the definition with rl_end_kbd_macro ().
242 If a numeric argument was explicitly typed, then append this
243 definition to the end of the existing macro, and start by
244 re-executing the existing macro. */
246 rl_start_kbd_macro (int ignore1, int ignore2)
248 if (RL_ISSTATE (RL_STATE_MACRODEF))
250 _rl_abort_internal ();
257 _rl_with_macro_input (savestring (current_macro));
260 current_macro_index = 0;
262 RL_SETSTATE(RL_STATE_MACRODEF);
266 /* Stop defining a keyboard macro.
267 A numeric argument says to execute the macro right now,
268 that many times, counting the definition as the first time. */
270 rl_end_kbd_macro (int count, int ignore)
272 if (RL_ISSTATE (RL_STATE_MACRODEF) == 0)
274 _rl_abort_internal ();
278 current_macro_index -= rl_key_sequence_length;
279 current_macro[current_macro_index] = '\0';
281 RL_UNSETSTATE(RL_STATE_MACRODEF);
283 return (rl_call_last_kbd_macro (--count, 0));
286 /* Execute the most recently defined keyboard macro.
287 COUNT says how many times to execute it. */
289 rl_call_last_kbd_macro (int count, int ignore)
291 if (current_macro == 0)
292 _rl_abort_internal ();
294 if (RL_ISSTATE (RL_STATE_MACRODEF))
296 rl_ding (); /* no recursive macros */
297 current_macro[--current_macro_index] = '\0'; /* erase this char */
302 _rl_with_macro_input (savestring (current_macro));
307 rl_print_last_kbd_macro (int count, int ignore)
311 if (current_macro == 0)
316 m = _rl_untranslate_macro_value (current_macro, 1);
322 rl_forced_update_display ();
323 rl_display_fixed = 1;
329 rl_push_macro_input (char *macro)
331 _rl_with_macro_input (macro);