1 /* macro.c -- keyboard macros for readline. */
3 /* Copyright (C) 1994 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2, or
11 (at your option) any later version.
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
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 SWAP(s, e) do { int t; t = s; s = e; e = t; } while (0)
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 /* Non-zero means to save keys that we dispatch on in a kbd macro. */
65 int _rl_defining_kbd_macro = 0;
67 /* The offset in the above string to the next character to be read. */
68 static int executing_macro_index;
70 /* The current macro string being built. Characters get stuffed
71 in here by add_macro_char (). */
72 static char *current_macro = (char *)NULL;
74 /* The size of the buffer allocated to current_macro. */
75 static int current_macro_size;
77 /* The index at which characters are being added to current_macro. */
78 static int current_macro_index;
80 /* A structure used to save nested macro strings.
81 It is a linked list of string/index for each saved macro. */
83 struct saved_macro *next;
88 /* The list of saved macros. */
89 static struct saved_macro *macro_list = (struct saved_macro *)NULL;
91 /* Set up to read subsequent input from STRING.
92 STRING is free ()'ed when we are done with it. */
94 _rl_with_macro_input (string)
97 _rl_push_executing_macro ();
98 rl_executing_macro = string;
99 executing_macro_index = 0;
100 RL_SETSTATE(RL_STATE_MACROINPUT);
103 /* Return the next character available from a macro, or 0 if
104 there are no macro characters. */
106 _rl_next_macro_key ()
108 if (rl_executing_macro == 0)
111 if (rl_executing_macro[executing_macro_index] == 0)
113 _rl_pop_executing_macro ();
114 return (_rl_next_macro_key ());
117 return (rl_executing_macro[executing_macro_index++]);
120 /* Save the currently executing macro on a stack of saved macros. */
122 _rl_push_executing_macro ()
124 struct saved_macro *saver;
126 saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
127 saver->next = macro_list;
128 saver->sindex = executing_macro_index;
129 saver->string = rl_executing_macro;
134 /* Discard the current macro, replacing it with the one
135 on the top of the stack of saved macros. */
137 _rl_pop_executing_macro ()
139 struct saved_macro *macro;
141 FREE (rl_executing_macro);
142 rl_executing_macro = (char *)NULL;
143 executing_macro_index = 0;
148 rl_executing_macro = macro_list->string;
149 executing_macro_index = macro_list->sindex;
150 macro_list = macro_list->next;
154 if (rl_executing_macro == 0)
155 RL_UNSETSTATE(RL_STATE_MACROINPUT);
158 /* Add a character to the macro being built. */
160 _rl_add_macro_char (c)
163 if (current_macro_index + 1 >= current_macro_size)
165 if (current_macro == 0)
166 current_macro = xmalloc (current_macro_size = 25);
168 current_macro = xrealloc (current_macro, current_macro_size += 25);
171 current_macro[current_macro_index++] = c;
172 current_macro[current_macro_index] = '\0';
176 _rl_kill_kbd_macro ()
180 free (current_macro);
181 current_macro = (char *) NULL;
183 current_macro_size = current_macro_index = 0;
185 FREE (rl_executing_macro);
186 rl_executing_macro = (char *) NULL;
187 executing_macro_index = 0;
189 _rl_defining_kbd_macro = 0;
190 RL_UNSETSTATE(RL_STATE_MACRODEF);
193 /* Begin defining a keyboard macro.
194 Keystrokes are recorded as they are executed.
195 End the definition with rl_end_kbd_macro ().
196 If a numeric argument was explicitly typed, then append this
197 definition to the end of the existing macro, and start by
198 re-executing the existing macro. */
200 rl_start_kbd_macro (ignore1, ignore2)
201 int ignore1, ignore2;
203 if (_rl_defining_kbd_macro)
205 _rl_abort_internal ();
212 _rl_with_macro_input (savestring (current_macro));
215 current_macro_index = 0;
217 _rl_defining_kbd_macro = 1;
218 RL_SETSTATE(RL_STATE_MACRODEF);
222 /* Stop defining a keyboard macro.
223 A numeric argument says to execute the macro right now,
224 that many times, counting the definition as the first time. */
226 rl_end_kbd_macro (count, ignore)
229 if (_rl_defining_kbd_macro == 0)
231 _rl_abort_internal ();
235 current_macro_index -= rl_key_sequence_length - 1;
236 current_macro[current_macro_index] = '\0';
238 _rl_defining_kbd_macro = 0;
239 RL_UNSETSTATE(RL_STATE_MACRODEF);
241 return (rl_call_last_kbd_macro (--count, 0));
244 /* Execute the most recently defined keyboard macro.
245 COUNT says how many times to execute it. */
247 rl_call_last_kbd_macro (count, ignore)
250 if (current_macro == 0)
251 _rl_abort_internal ();
253 if (_rl_defining_kbd_macro)
255 rl_ding (); /* no recursive macros */
256 current_macro[--current_macro_index] = '\0'; /* erase this char */
261 _rl_with_macro_input (savestring (current_macro));
266 rl_push_macro_input (macro)
269 _rl_with_macro_input (macro);