blob: b99a1a69fe6920c712f2631b69dfef24f487b6e7 [file] [log] [blame]
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001/* macro.c -- keyboard macros for readline. */
2
Jari Aalto31859422009-01-12 13:36:28 +00003/* Copyright (C) 1994-2009 Free Software Foundation, Inc.
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004
Jari Aalto31859422009-01-12 13:36:28 +00005 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
Jari Aaltoccc6cda1996-12-23 17:02:34 +00007
Jari Aalto31859422009-01-12 13:36:28 +00008 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
Jari Aaltoccc6cda1996-12-23 17:02:34 +000011 (at your option) any later version.
12
Jari Aalto31859422009-01-12 13:36:28 +000013 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
Jari Aaltoccc6cda1996-12-23 17:02:34 +000016 GNU General Public License for more details.
17
Jari Aalto31859422009-01-12 13:36:28 +000018 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
20*/
21
Jari Aaltoccc6cda1996-12-23 17:02:34 +000022#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
Jari Aaltobb706242000-03-17 21:46:59 +000049#include "rlprivate.h"
50#include "xmalloc.h"
51
Jari Aaltoccc6cda1996-12-23 17:02:34 +000052/* **************************************************************** */
53/* */
54/* Hacking Keyboard Macros */
55/* */
56/* **************************************************************** */
57
Jari Aaltoccc6cda1996-12-23 17:02:34 +000058/* The currently executing macro string. If this is non-zero,
59 then it is a malloc ()'ed string where input is coming from. */
Jari Aalto28ef6c32001-04-06 19:14:31 +000060char *rl_executing_macro = (char *)NULL;
61
Jari Aaltoccc6cda1996-12-23 17:02:34 +000062/* The offset in the above string to the next character to be read. */
63static int executing_macro_index;
64
65/* The current macro string being built. Characters get stuffed
66 in here by add_macro_char (). */
67static char *current_macro = (char *)NULL;
68
69/* The size of the buffer allocated to current_macro. */
70static int current_macro_size;
71
72/* The index at which characters are being added to current_macro. */
73static int current_macro_index;
74
75/* A structure used to save nested macro strings.
76 It is a linked list of string/index for each saved macro. */
77struct saved_macro {
78 struct saved_macro *next;
79 char *string;
80 int sindex;
81};
82
83/* The list of saved macros. */
84static struct saved_macro *macro_list = (struct saved_macro *)NULL;
85
86/* Set up to read subsequent input from STRING.
87 STRING is free ()'ed when we are done with it. */
88void
89_rl_with_macro_input (string)
90 char *string;
91{
92 _rl_push_executing_macro ();
Jari Aalto28ef6c32001-04-06 19:14:31 +000093 rl_executing_macro = string;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000094 executing_macro_index = 0;
Jari Aalto28ef6c32001-04-06 19:14:31 +000095 RL_SETSTATE(RL_STATE_MACROINPUT);
Jari Aaltoccc6cda1996-12-23 17:02:34 +000096}
97
98/* Return the next character available from a macro, or 0 if
99 there are no macro characters. */
100int
101_rl_next_macro_key ()
102{
Jari Aalto95732b42005-12-07 14:08:12 +0000103 int c;
104
Jari Aalto28ef6c32001-04-06 19:14:31 +0000105 if (rl_executing_macro == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000106 return (0);
107
Jari Aalto28ef6c32001-04-06 19:14:31 +0000108 if (rl_executing_macro[executing_macro_index] == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000109 {
110 _rl_pop_executing_macro ();
111 return (_rl_next_macro_key ());
112 }
113
Jari Aalto95732b42005-12-07 14:08:12 +0000114#if defined (READLINE_CALLBACKS)
115 c = rl_executing_macro[executing_macro_index++];
Jari Aalto06285672006-10-10 14:15:34 +0000116 if (RL_ISSTATE (RL_STATE_CALLBACK) && RL_ISSTATE (RL_STATE_READCMD|RL_STATE_MOREINPUT) && rl_executing_macro[executing_macro_index] == 0)
Jari Aalto95732b42005-12-07 14:08:12 +0000117 _rl_pop_executing_macro ();
118 return c;
119#else
Jari Aalto28ef6c32001-04-06 19:14:31 +0000120 return (rl_executing_macro[executing_macro_index++]);
Jari Aalto95732b42005-12-07 14:08:12 +0000121#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000122}
123
Chet Rameyac50fba2014-02-26 09:36:43 -0500124int
125_rl_prev_macro_key ()
126{
127 if (rl_executing_macro == 0)
128 return (0);
129
130 if (executing_macro_index == 0)
131 return (0);
132
133 executing_macro_index--;
134 return (rl_executing_macro[executing_macro_index]);
135}
136
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000137/* Save the currently executing macro on a stack of saved macros. */
138void
139_rl_push_executing_macro ()
140{
141 struct saved_macro *saver;
142
143 saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
144 saver->next = macro_list;
145 saver->sindex = executing_macro_index;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000146 saver->string = rl_executing_macro;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000147
148 macro_list = saver;
149}
150
151/* Discard the current macro, replacing it with the one
152 on the top of the stack of saved macros. */
153void
154_rl_pop_executing_macro ()
155{
156 struct saved_macro *macro;
157
Jari Aalto28ef6c32001-04-06 19:14:31 +0000158 FREE (rl_executing_macro);
159 rl_executing_macro = (char *)NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000160 executing_macro_index = 0;
161
162 if (macro_list)
163 {
164 macro = macro_list;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000165 rl_executing_macro = macro_list->string;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000166 executing_macro_index = macro_list->sindex;
167 macro_list = macro_list->next;
Jari Aalto31859422009-01-12 13:36:28 +0000168 xfree (macro);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000169 }
Jari Aalto28ef6c32001-04-06 19:14:31 +0000170
171 if (rl_executing_macro == 0)
172 RL_UNSETSTATE(RL_STATE_MACROINPUT);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000173}
174
175/* Add a character to the macro being built. */
176void
177_rl_add_macro_char (c)
178 int c;
179{
180 if (current_macro_index + 1 >= current_macro_size)
181 {
182 if (current_macro == 0)
Jari Aaltof73dda02001-11-13 17:56:06 +0000183 current_macro = (char *)xmalloc (current_macro_size = 25);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000184 else
Jari Aaltof73dda02001-11-13 17:56:06 +0000185 current_macro = (char *)xrealloc (current_macro, current_macro_size += 25);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000186 }
187
188 current_macro[current_macro_index++] = c;
189 current_macro[current_macro_index] = '\0';
190}
191
192void
193_rl_kill_kbd_macro ()
194{
195 if (current_macro)
196 {
Jari Aalto31859422009-01-12 13:36:28 +0000197 xfree (current_macro);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000198 current_macro = (char *) NULL;
199 }
200 current_macro_size = current_macro_index = 0;
201
Jari Aalto28ef6c32001-04-06 19:14:31 +0000202 FREE (rl_executing_macro);
203 rl_executing_macro = (char *) NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000204 executing_macro_index = 0;
205
Jari Aalto28ef6c32001-04-06 19:14:31 +0000206 RL_UNSETSTATE(RL_STATE_MACRODEF);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000207}
208
209/* Begin defining a keyboard macro.
210 Keystrokes are recorded as they are executed.
211 End the definition with rl_end_kbd_macro ().
212 If a numeric argument was explicitly typed, then append this
213 definition to the end of the existing macro, and start by
214 re-executing the existing macro. */
215int
216rl_start_kbd_macro (ignore1, ignore2)
217 int ignore1, ignore2;
218{
Jari Aalto7117c2d2002-07-17 14:10:11 +0000219 if (RL_ISSTATE (RL_STATE_MACRODEF))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000220 {
221 _rl_abort_internal ();
222 return -1;
223 }
224
225 if (rl_explicit_arg)
226 {
227 if (current_macro)
228 _rl_with_macro_input (savestring (current_macro));
229 }
230 else
231 current_macro_index = 0;
232
Jari Aalto28ef6c32001-04-06 19:14:31 +0000233 RL_SETSTATE(RL_STATE_MACRODEF);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000234 return 0;
235}
236
237/* Stop defining a keyboard macro.
238 A numeric argument says to execute the macro right now,
239 that many times, counting the definition as the first time. */
240int
241rl_end_kbd_macro (count, ignore)
242 int count, ignore;
243{
Jari Aalto7117c2d2002-07-17 14:10:11 +0000244 if (RL_ISSTATE (RL_STATE_MACRODEF) == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000245 {
246 _rl_abort_internal ();
247 return -1;
248 }
249
Chet Rameyac50fba2014-02-26 09:36:43 -0500250 current_macro_index -= rl_key_sequence_length;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000251 current_macro[current_macro_index] = '\0';
252
Jari Aalto28ef6c32001-04-06 19:14:31 +0000253 RL_UNSETSTATE(RL_STATE_MACRODEF);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000254
255 return (rl_call_last_kbd_macro (--count, 0));
256}
257
258/* Execute the most recently defined keyboard macro.
259 COUNT says how many times to execute it. */
260int
261rl_call_last_kbd_macro (count, ignore)
262 int count, ignore;
263{
264 if (current_macro == 0)
265 _rl_abort_internal ();
266
Jari Aalto7117c2d2002-07-17 14:10:11 +0000267 if (RL_ISSTATE (RL_STATE_MACRODEF))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000268 {
Jari Aalto28ef6c32001-04-06 19:14:31 +0000269 rl_ding (); /* no recursive macros */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000270 current_macro[--current_macro_index] = '\0'; /* erase this char */
271 return 0;
272 }
273
274 while (count--)
275 _rl_with_macro_input (savestring (current_macro));
276 return 0;
277}
278
Chet Rameyac50fba2014-02-26 09:36:43 -0500279int
280rl_print_last_kbd_macro (count, ignore)
281 int count, ignore;
282{
283 char *m;
284
285 if (current_macro == 0)
286 {
287 rl_ding ();
288 return 0;
289 }
290 m = _rl_untranslate_macro_value (current_macro, 1);
291 rl_crlf ();
292 printf ("%s", m);
293 fflush (stdout);
294 rl_crlf ();
295 FREE (m);
296 rl_forced_update_display ();
297 rl_display_fixed = 1;
298
299 return 0;
300}
301
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000302void
303rl_push_macro_input (macro)
304 char *macro;
305{
306 _rl_with_macro_input (macro);
307}