]> git.ipfire.org Git - thirdparty/bash.git/blame - lib/readline/macro.c
Imported from ../bash-2.04.tar.gz.
[thirdparty/bash.git] / lib / readline / macro.c
CommitLineData
ccc6cda3
JA
1/* macro.c -- keyboard macros for readline. */
2
3/* Copyright (C) 1994 Free Software Foundation, Inc.
4
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
7
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
bb70624e 10 as published by the Free Software Foundation; either version 2, or
ccc6cda3
JA
11 (at your option) any later version.
12
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.
17
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,
bb70624e 21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
ccc6cda3
JA
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
bb70624e
JA
49#include "rlprivate.h"
50#include "xmalloc.h"
ccc6cda3 51
bb70624e 52#define SWAP(s, e) do { int t; t = s; s = e; e = t; } while (0)
ccc6cda3
JA
53
54/* **************************************************************** */
55/* */
56/* Hacking Keyboard Macros */
57/* */
58/* **************************************************************** */
59
60/* Non-zero means to save keys that we dispatch on in a kbd macro. */
61int _rl_defining_kbd_macro = 0;
62
63/* The currently executing macro string. If this is non-zero,
64 then it is a malloc ()'ed string where input is coming from. */
65char *_rl_executing_macro = (char *)NULL;
66
67/* The offset in the above string to the next character to be read. */
68static int executing_macro_index;
69
70/* The current macro string being built. Characters get stuffed
71 in here by add_macro_char (). */
72static char *current_macro = (char *)NULL;
73
74/* The size of the buffer allocated to current_macro. */
75static int current_macro_size;
76
77/* The index at which characters are being added to current_macro. */
78static int current_macro_index;
79
80/* A structure used to save nested macro strings.
81 It is a linked list of string/index for each saved macro. */
82struct saved_macro {
83 struct saved_macro *next;
84 char *string;
85 int sindex;
86};
87
88/* The list of saved macros. */
89static struct saved_macro *macro_list = (struct saved_macro *)NULL;
90
91/* Set up to read subsequent input from STRING.
92 STRING is free ()'ed when we are done with it. */
93void
94_rl_with_macro_input (string)
95 char *string;
96{
97 _rl_push_executing_macro ();
98 _rl_executing_macro = string;
99 executing_macro_index = 0;
100}
101
102/* Return the next character available from a macro, or 0 if
103 there are no macro characters. */
104int
105_rl_next_macro_key ()
106{
107 if (_rl_executing_macro == 0)
108 return (0);
109
110 if (_rl_executing_macro[executing_macro_index] == 0)
111 {
112 _rl_pop_executing_macro ();
113 return (_rl_next_macro_key ());
114 }
115
116 return (_rl_executing_macro[executing_macro_index++]);
117}
118
119/* Save the currently executing macro on a stack of saved macros. */
120void
121_rl_push_executing_macro ()
122{
123 struct saved_macro *saver;
124
125 saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
126 saver->next = macro_list;
127 saver->sindex = executing_macro_index;
128 saver->string = _rl_executing_macro;
129
130 macro_list = saver;
131}
132
133/* Discard the current macro, replacing it with the one
134 on the top of the stack of saved macros. */
135void
136_rl_pop_executing_macro ()
137{
138 struct saved_macro *macro;
139
140 if (_rl_executing_macro)
141 free (_rl_executing_macro);
142
143 _rl_executing_macro = (char *)NULL;
144 executing_macro_index = 0;
145
146 if (macro_list)
147 {
148 macro = macro_list;
149 _rl_executing_macro = macro_list->string;
150 executing_macro_index = macro_list->sindex;
151 macro_list = macro_list->next;
152 free (macro);
153 }
154}
155
156/* Add a character to the macro being built. */
157void
158_rl_add_macro_char (c)
159 int c;
160{
161 if (current_macro_index + 1 >= current_macro_size)
162 {
163 if (current_macro == 0)
164 current_macro = xmalloc (current_macro_size = 25);
165 else
166 current_macro = xrealloc (current_macro, current_macro_size += 25);
167 }
168
169 current_macro[current_macro_index++] = c;
170 current_macro[current_macro_index] = '\0';
171}
172
173void
174_rl_kill_kbd_macro ()
175{
176 if (current_macro)
177 {
178 free (current_macro);
179 current_macro = (char *) NULL;
180 }
181 current_macro_size = current_macro_index = 0;
182
183 if (_rl_executing_macro)
184 {
185 free (_rl_executing_macro);
186 _rl_executing_macro = (char *) NULL;
187 }
188 executing_macro_index = 0;
189
190 _rl_defining_kbd_macro = 0;
191}
192
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. */
199int
200rl_start_kbd_macro (ignore1, ignore2)
201 int ignore1, ignore2;
202{
203 if (_rl_defining_kbd_macro)
204 {
205 _rl_abort_internal ();
206 return -1;
207 }
208
209 if (rl_explicit_arg)
210 {
211 if (current_macro)
212 _rl_with_macro_input (savestring (current_macro));
213 }
214 else
215 current_macro_index = 0;
216
217 _rl_defining_kbd_macro = 1;
218 return 0;
219}
220
221/* Stop defining a keyboard macro.
222 A numeric argument says to execute the macro right now,
223 that many times, counting the definition as the first time. */
224int
225rl_end_kbd_macro (count, ignore)
226 int count, ignore;
227{
228 if (_rl_defining_kbd_macro == 0)
229 {
230 _rl_abort_internal ();
231 return -1;
232 }
233
234 current_macro_index -= rl_key_sequence_length - 1;
235 current_macro[current_macro_index] = '\0';
236
237 _rl_defining_kbd_macro = 0;
238
239 return (rl_call_last_kbd_macro (--count, 0));
240}
241
242/* Execute the most recently defined keyboard macro.
243 COUNT says how many times to execute it. */
244int
245rl_call_last_kbd_macro (count, ignore)
246 int count, ignore;
247{
248 if (current_macro == 0)
249 _rl_abort_internal ();
250
251 if (_rl_defining_kbd_macro)
252 {
253 ding (); /* no recursive macros */
254 current_macro[--current_macro_index] = '\0'; /* erase this char */
255 return 0;
256 }
257
258 while (count--)
259 _rl_with_macro_input (savestring (current_macro));
260 return 0;
261}
262
263void
264rl_push_macro_input (macro)
265 char *macro;
266{
267 _rl_with_macro_input (macro);
268}