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 /* Non-zero means to save keys that we dispatch on in a kbd macro. */
61 int _rl_defining_kbd_macro
= 0;
63 /* The currently executing macro string. If this is non-zero,
64 then it is a malloc ()'ed string where input is coming from. */
65 char *_rl_executing_macro
= (char *)NULL
;
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;
102 /* Return the next character available from a macro, or 0 if
103 there are no macro characters. */
105 _rl_next_macro_key ()
107 if (_rl_executing_macro
== 0)
110 if (_rl_executing_macro
[executing_macro_index
] == 0)
112 _rl_pop_executing_macro ();
113 return (_rl_next_macro_key ());
116 return (_rl_executing_macro
[executing_macro_index
++]);
119 /* Save the currently executing macro on a stack of saved macros. */
121 _rl_push_executing_macro ()
123 struct saved_macro
*saver
;
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
;
133 /* Discard the current macro, replacing it with the one
134 on the top of the stack of saved macros. */
136 _rl_pop_executing_macro ()
138 struct saved_macro
*macro
;
140 if (_rl_executing_macro
)
141 free (_rl_executing_macro
);
143 _rl_executing_macro
= (char *)NULL
;
144 executing_macro_index
= 0;
149 _rl_executing_macro
= macro_list
->string
;
150 executing_macro_index
= macro_list
->sindex
;
151 macro_list
= macro_list
->next
;
156 /* Add a character to the macro being built. */
158 _rl_add_macro_char (c
)
161 if (current_macro_index
+ 1 >= current_macro_size
)
163 if (current_macro
== 0)
164 current_macro
= xmalloc (current_macro_size
= 25);
166 current_macro
= xrealloc (current_macro
, current_macro_size
+= 25);
169 current_macro
[current_macro_index
++] = c
;
170 current_macro
[current_macro_index
] = '\0';
174 _rl_kill_kbd_macro ()
178 free (current_macro
);
179 current_macro
= (char *) NULL
;
181 current_macro_size
= current_macro_index
= 0;
183 if (_rl_executing_macro
)
185 free (_rl_executing_macro
);
186 _rl_executing_macro
= (char *) NULL
;
188 executing_macro_index
= 0;
190 _rl_defining_kbd_macro
= 0;
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;
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. */
225 rl_end_kbd_macro (count
, ignore
)
228 if (_rl_defining_kbd_macro
== 0)
230 _rl_abort_internal ();
234 current_macro_index
-= rl_key_sequence_length
- 1;
235 current_macro
[current_macro_index
] = '\0';
237 _rl_defining_kbd_macro
= 0;
239 return (rl_call_last_kbd_macro (--count
, 0));
242 /* Execute the most recently defined keyboard macro.
243 COUNT says how many times to execute it. */
245 rl_call_last_kbd_macro (count
, ignore
)
248 if (current_macro
== 0)
249 _rl_abort_internal ();
251 if (_rl_defining_kbd_macro
)
253 ding (); /* no recursive macros */
254 current_macro
[--current_macro_index
] = '\0'; /* erase this char */
259 _rl_with_macro_input (savestring (current_macro
));
264 rl_push_macro_input (macro
)
267 _rl_with_macro_input (macro
);
This page took 0.034276 seconds and 4 git commands to generate.