c7017cc6b3dab6303d9278031b557bcfd0862f02
1 /* macro.c -- keyboard macros for readline. */
3 /* Copyright (C) 1994-2009 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
8 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
11 (at your option) any later version.
13 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
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
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 /* **************************************************************** */
54 /* Hacking Keyboard Macros */
56 /* **************************************************************** */
58 /* The currently executing macro string. If this is non-zero,
59 then it is a malloc ()'ed string where input is coming from. */
60 char *rl_executing_macro
= (char *)NULL
;
62 /* The offset in the above string to the next character to be read. */
63 static int executing_macro_index
;
65 /* The current macro string being built. Characters get stuffed
66 in here by add_macro_char (). */
67 static char *current_macro
= (char *)NULL
;
69 /* The size of the buffer allocated to current_macro. */
70 static int current_macro_size
;
72 /* The index at which characters are being added to current_macro. */
73 static int current_macro_index
;
75 /* A structure used to save nested macro strings.
76 It is a linked list of string/index for each saved macro. */
78 struct saved_macro
*next
;
83 /* The list of saved macros. */
84 static struct saved_macro
*macro_list
= (struct saved_macro
*)NULL
;
86 /* Set up to read subsequent input from STRING.
87 STRING is free ()'ed when we are done with it. */
89 _rl_with_macro_input (string
)
92 _rl_push_executing_macro ();
93 rl_executing_macro
= string
;
94 executing_macro_index
= 0;
95 RL_SETSTATE(RL_STATE_MACROINPUT
);
98 /* Return the next character available from a macro, or 0 if
99 there are no macro characters. */
101 _rl_next_macro_key ()
105 if (rl_executing_macro
== 0)
108 if (rl_executing_macro
[executing_macro_index
] == 0)
110 _rl_pop_executing_macro ();
111 return (_rl_next_macro_key ());
114 #if defined (READLINE_CALLBACKS)
115 c
= rl_executing_macro
[executing_macro_index
++];
116 if (RL_ISSTATE (RL_STATE_CALLBACK
) && RL_ISSTATE (RL_STATE_READCMD
|RL_STATE_MOREINPUT
) && rl_executing_macro
[executing_macro_index
] == 0)
117 _rl_pop_executing_macro ();
120 return (rl_executing_macro
[executing_macro_index
++]);
125 _rl_prev_macro_key ()
127 if (rl_executing_macro
== 0)
130 if (executing_macro_index
== 0)
133 executing_macro_index
--;
134 return (rl_executing_macro
[executing_macro_index
]);
137 /* Save the currently executing macro on a stack of saved macros. */
139 _rl_push_executing_macro ()
141 struct saved_macro
*saver
;
143 saver
= (struct saved_macro
*)xmalloc (sizeof (struct saved_macro
));
144 saver
->next
= macro_list
;
145 saver
->sindex
= executing_macro_index
;
146 saver
->string
= rl_executing_macro
;
151 /* Discard the current macro, replacing it with the one
152 on the top of the stack of saved macros. */
154 _rl_pop_executing_macro ()
156 struct saved_macro
*macro
;
158 FREE (rl_executing_macro
);
159 rl_executing_macro
= (char *)NULL
;
160 executing_macro_index
= 0;
165 rl_executing_macro
= macro_list
->string
;
166 executing_macro_index
= macro_list
->sindex
;
167 macro_list
= macro_list
->next
;
171 if (rl_executing_macro
== 0)
172 RL_UNSETSTATE(RL_STATE_MACROINPUT
);
175 /* Add a character to the macro being built. */
177 _rl_add_macro_char (c
)
180 if (current_macro_index
+ 1 >= current_macro_size
)
182 if (current_macro
== 0)
183 current_macro
= (char *)xmalloc (current_macro_size
= 25);
185 current_macro
= (char *)xrealloc (current_macro
, current_macro_size
+= 25);
188 current_macro
[current_macro_index
++] = c
;
189 current_macro
[current_macro_index
] = '\0';
193 _rl_kill_kbd_macro ()
197 xfree (current_macro
);
198 current_macro
= (char *) NULL
;
200 current_macro_size
= current_macro_index
= 0;
202 FREE (rl_executing_macro
);
203 rl_executing_macro
= (char *) NULL
;
204 executing_macro_index
= 0;
206 RL_UNSETSTATE(RL_STATE_MACRODEF
);
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. */
216 rl_start_kbd_macro (ignore1
, ignore2
)
217 int ignore1
, ignore2
;
219 if (RL_ISSTATE (RL_STATE_MACRODEF
))
221 _rl_abort_internal ();
228 _rl_with_macro_input (savestring (current_macro
));
231 current_macro_index
= 0;
233 RL_SETSTATE(RL_STATE_MACRODEF
);
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. */
241 rl_end_kbd_macro (count
, ignore
)
244 if (RL_ISSTATE (RL_STATE_MACRODEF
) == 0)
246 _rl_abort_internal ();
250 current_macro_index
-= rl_key_sequence_length
;
251 current_macro
[current_macro_index
] = '\0';
253 RL_UNSETSTATE(RL_STATE_MACRODEF
);
255 return (rl_call_last_kbd_macro (--count
, 0));
258 /* Execute the most recently defined keyboard macro.
259 COUNT says how many times to execute it. */
261 rl_call_last_kbd_macro (count
, ignore
)
264 if (current_macro
== 0)
265 _rl_abort_internal ();
267 if (RL_ISSTATE (RL_STATE_MACRODEF
))
269 rl_ding (); /* no recursive macros */
270 current_macro
[--current_macro_index
] = '\0'; /* erase this char */
275 _rl_with_macro_input (savestring (current_macro
));
280 rl_print_last_kbd_macro (count
, ignore
)
285 if (current_macro
== 0)
290 m
= _rl_untranslate_macro_value (current_macro
, 1);
296 rl_forced_update_display ();
297 rl_display_fixed
= 1;
303 rl_push_macro_input (macro
)
306 _rl_with_macro_input (macro
);
This page took 0.036721 seconds and 3 git commands to generate.