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 1, 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
675 Mass Ave, Cambridge, MA 02139, 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
#define SWAP(s, e) do { int t; t = s; s = e; e = t; } while (0)
51
/* Forward definitions. */
52
void _rl_push_executing_macro (), _rl_pop_executing_macro ();
53
void _rl_add_macro_char ();
55
/* Extern declarations. */
56
extern int rl_explicit_arg;
57
extern int rl_key_sequence_length;
59
extern void _rl_abort_internal ();
61
extern char *xmalloc (), *xrealloc ();
63
/* **************************************************************** */
65
/* Hacking Keyboard Macros */
67
/* **************************************************************** */
69
/* Non-zero means to save keys that we dispatch on in a kbd macro. */
70
int _rl_defining_kbd_macro = 0;
72
/* The currently executing macro string. If this is non-zero,
73
then it is a malloc ()'ed string where input is coming from. */
74
char *_rl_executing_macro = (char *)NULL;
76
/* The offset in the above string to the next character to be read. */
77
static int executing_macro_index;
79
/* The current macro string being built. Characters get stuffed
80
in here by add_macro_char (). */
81
static char *current_macro = (char *)NULL;
83
/* The size of the buffer allocated to current_macro. */
84
static int current_macro_size;
86
/* The index at which characters are being added to current_macro. */
87
static int current_macro_index;
89
/* A structure used to save nested macro strings.
90
It is a linked list of string/index for each saved macro. */
92
struct saved_macro *next;
97
/* The list of saved macros. */
98
static struct saved_macro *macro_list = (struct saved_macro *)NULL;
100
/* Set up to read subsequent input from STRING.
101
STRING is free ()'ed when we are done with it. */
103
_rl_with_macro_input (string)
106
_rl_push_executing_macro ();
107
_rl_executing_macro = string;
108
executing_macro_index = 0;
111
/* Return the next character available from a macro, or 0 if
112
there are no macro characters. */
114
_rl_next_macro_key ()
116
if (_rl_executing_macro == 0)
119
if (_rl_executing_macro[executing_macro_index] == 0)
121
_rl_pop_executing_macro ();
122
return (_rl_next_macro_key ());
125
return (_rl_executing_macro[executing_macro_index++]);
128
/* Save the currently executing macro on a stack of saved macros. */
130
_rl_push_executing_macro ()
132
struct saved_macro *saver;
134
saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
135
saver->next = macro_list;
136
saver->sindex = executing_macro_index;
137
saver->string = _rl_executing_macro;
142
/* Discard the current macro, replacing it with the one
143
on the top of the stack of saved macros. */
145
_rl_pop_executing_macro ()
147
struct saved_macro *macro;
149
if (_rl_executing_macro)
150
free (_rl_executing_macro);
152
_rl_executing_macro = (char *)NULL;
153
executing_macro_index = 0;
158
_rl_executing_macro = macro_list->string;
159
executing_macro_index = macro_list->sindex;
160
macro_list = macro_list->next;
165
/* Add a character to the macro being built. */
167
_rl_add_macro_char (c)
170
if (current_macro_index + 1 >= current_macro_size)
172
if (current_macro == 0)
173
current_macro = xmalloc (current_macro_size = 25);
175
current_macro = xrealloc (current_macro, current_macro_size += 25);
178
current_macro[current_macro_index++] = c;
179
current_macro[current_macro_index] = '\0';
183
_rl_kill_kbd_macro ()
187
free (current_macro);
188
current_macro = (char *) NULL;
190
current_macro_size = current_macro_index = 0;
192
if (_rl_executing_macro)
194
free (_rl_executing_macro);
195
_rl_executing_macro = (char *) NULL;
197
executing_macro_index = 0;
199
_rl_defining_kbd_macro = 0;
202
/* Begin defining a keyboard macro.
203
Keystrokes are recorded as they are executed.
204
End the definition with rl_end_kbd_macro ().
205
If a numeric argument was explicitly typed, then append this
206
definition to the end of the existing macro, and start by
207
re-executing the existing macro. */
209
rl_start_kbd_macro (ignore1, ignore2)
210
int ignore1, ignore2;
212
if (_rl_defining_kbd_macro)
214
_rl_abort_internal ();
221
_rl_with_macro_input (savestring (current_macro));
224
current_macro_index = 0;
226
_rl_defining_kbd_macro = 1;
230
/* Stop defining a keyboard macro.
231
A numeric argument says to execute the macro right now,
232
that many times, counting the definition as the first time. */
234
rl_end_kbd_macro (count, ignore)
237
if (_rl_defining_kbd_macro == 0)
239
_rl_abort_internal ();
243
current_macro_index -= rl_key_sequence_length - 1;
244
current_macro[current_macro_index] = '\0';
246
_rl_defining_kbd_macro = 0;
248
return (rl_call_last_kbd_macro (--count, 0));
251
/* Execute the most recently defined keyboard macro.
252
COUNT says how many times to execute it. */
254
rl_call_last_kbd_macro (count, ignore)
257
if (current_macro == 0)
258
_rl_abort_internal ();
260
if (_rl_defining_kbd_macro)
262
ding (); /* no recursive macros */
263
current_macro[--current_macro_index] = '\0'; /* erase this char */
268
_rl_with_macro_input (savestring (current_macro));
273
rl_push_macro_input (macro)
276
_rl_with_macro_input (macro);