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 /* **************************************************************** */
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_executing_macro
[executing_macro_index
] == 0)
117 _rl_pop_executing_macro ();
120 return (rl_executing_macro
[executing_macro_index
++]);
124 /* Save the currently executing macro on a stack of saved macros. */
126 _rl_push_executing_macro ()
128 struct saved_macro
*saver
;
130 saver
= (struct saved_macro
*)xmalloc (sizeof (struct saved_macro
));
131 saver
->next
= macro_list
;
132 saver
->sindex
= executing_macro_index
;
133 saver
->string
= rl_executing_macro
;
138 /* Discard the current macro, replacing it with the one
139 on the top of the stack of saved macros. */
141 _rl_pop_executing_macro ()
143 struct saved_macro
*macro
;
145 FREE (rl_executing_macro
);
146 rl_executing_macro
= (char *)NULL
;
147 executing_macro_index
= 0;
152 rl_executing_macro
= macro_list
->string
;
153 executing_macro_index
= macro_list
->sindex
;
154 macro_list
= macro_list
->next
;
158 if (rl_executing_macro
== 0)
159 RL_UNSETSTATE(RL_STATE_MACROINPUT
);
162 /* Add a character to the macro being built. */
164 _rl_add_macro_char (c
)
167 if (current_macro_index
+ 1 >= current_macro_size
)
169 if (current_macro
== 0)
170 current_macro
= (char *)xmalloc (current_macro_size
= 25);
172 current_macro
= (char *)xrealloc (current_macro
, current_macro_size
+= 25);
175 current_macro
[current_macro_index
++] = c
;
176 current_macro
[current_macro_index
] = '\0';
180 _rl_kill_kbd_macro ()
184 free (current_macro
);
185 current_macro
= (char *) NULL
;
187 current_macro_size
= current_macro_index
= 0;
189 FREE (rl_executing_macro
);
190 rl_executing_macro
= (char *) NULL
;
191 executing_macro_index
= 0;
193 RL_UNSETSTATE(RL_STATE_MACRODEF
);
196 /* Begin defining a keyboard macro.
197 Keystrokes are recorded as they are executed.
198 End the definition with rl_end_kbd_macro ().
199 If a numeric argument was explicitly typed, then append this
200 definition to the end of the existing macro, and start by
201 re-executing the existing macro. */
203 rl_start_kbd_macro (ignore1
, ignore2
)
204 int ignore1
, ignore2
;
206 if (RL_ISSTATE (RL_STATE_MACRODEF
))
208 _rl_abort_internal ();
215 _rl_with_macro_input (savestring (current_macro
));
218 current_macro_index
= 0;
220 RL_SETSTATE(RL_STATE_MACRODEF
);
224 /* Stop defining a keyboard macro.
225 A numeric argument says to execute the macro right now,
226 that many times, counting the definition as the first time. */
228 rl_end_kbd_macro (count
, ignore
)
231 if (RL_ISSTATE (RL_STATE_MACRODEF
) == 0)
233 _rl_abort_internal ();
237 current_macro_index
-= rl_key_sequence_length
- 1;
238 current_macro
[current_macro_index
] = '\0';
240 RL_UNSETSTATE(RL_STATE_MACRODEF
);
242 return (rl_call_last_kbd_macro (--count
, 0));
245 /* Execute the most recently defined keyboard macro.
246 COUNT says how many times to execute it. */
248 rl_call_last_kbd_macro (count
, ignore
)
251 if (current_macro
== 0)
252 _rl_abort_internal ();
254 if (RL_ISSTATE (RL_STATE_MACRODEF
))
256 rl_ding (); /* no recursive macros */
257 current_macro
[--current_macro_index
] = '\0'; /* erase this char */
262 _rl_with_macro_input (savestring (current_macro
));
267 rl_push_macro_input (macro
)
270 _rl_with_macro_input (macro
);
This page took 0.034055 seconds and 4 git commands to generate.