00001 /* macro.c -- keyboard macros for readline. */ 00002 00003 /* Copyright (C) 1994 Free Software Foundation, Inc. 00004 00005 This file is part of the GNU Readline Library, a library for 00006 reading lines of text with interactive input and history editing. 00007 00008 The GNU Readline Library is free software; you can redistribute it 00009 and/or modify it under the terms of the GNU General Public License 00010 as published by the Free Software Foundation; either version 2, or 00011 (at your option) any later version. 00012 00013 The GNU Readline Library is distributed in the hope that it will be 00014 useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00015 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 The GNU General Public License is often shipped with GNU software, and 00019 is generally kept in a file called COPYING or LICENSE. If you do not 00020 have a copy of the license, write to the Free Software Foundation, 00021 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ 00022 #define READLINE_LIBRARY 00023 00024 #include "config_readline.h" 00025 00026 #include <sys/types.h> 00027 00028 #if defined (HAVE_UNISTD_H) 00029 # include <unistd.h> /* for _POSIX_VERSION */ 00030 #endif /* HAVE_UNISTD_H */ 00031 00032 #if defined (HAVE_STDLIB_H) 00033 # include <stdlib.h> 00034 #else 00035 # include "ansi_stdlib.h" 00036 #endif /* HAVE_STDLIB_H */ 00037 00038 #include <stdio.h> 00039 00040 /* System-specific feature definitions and include files. */ 00041 #include "rldefs.h" 00042 00043 /* Some standard library routines. */ 00044 #include "readline.h" 00045 #include "history.h" 00046 00047 #include "rlprivate.h" 00048 #include "xmalloc.h" 00049 00050 /* **************************************************************** */ 00051 /* */ 00052 /* Hacking Keyboard Macros */ 00053 /* */ 00054 /* **************************************************************** */ 00055 00056 /* The currently executing macro string. If this is non-zero, 00057 then it is a malloc ()'ed string where input is coming from. */ 00058 char *rl_executing_macro = (char *)NULL; 00059 00060 /* The offset in the above string to the next character to be read. */ 00061 static int executing_macro_index; 00062 00063 /* The current macro string being built. Characters get stuffed 00064 in here by add_macro_char (). */ 00065 static char *current_macro = (char *)NULL; 00066 00067 /* The size of the buffer allocated to current_macro. */ 00068 static int current_macro_size; 00069 00070 /* The index at which characters are being added to current_macro. */ 00071 static int current_macro_index; 00072 00073 /* A structure used to save nested macro strings. 00074 It is a linked list of string/index for each saved macro. */ 00075 struct saved_macro { 00076 struct saved_macro *next; 00077 char *string; 00078 int sindex; 00079 }; 00080 00081 /* The list of saved macros. */ 00082 static struct saved_macro *macro_list = (struct saved_macro *)NULL; 00083 00084 /* Set up to read subsequent input from STRING. 00085 STRING is free ()'ed when we are done with it. */ 00086 void 00087 _rl_with_macro_input (string) 00088 char *string; 00089 { 00090 _rl_push_executing_macro (); 00091 rl_executing_macro = string; 00092 executing_macro_index = 0; 00093 RL_SETSTATE(RL_STATE_MACROINPUT); 00094 } 00095 00096 /* Return the next character available from a macro, or 0 if 00097 there are no macro characters. */ 00098 int 00099 _rl_next_macro_key () 00100 { 00101 if (rl_executing_macro == 0) 00102 return (0); 00103 00104 if (rl_executing_macro[executing_macro_index] == 0) 00105 { 00106 _rl_pop_executing_macro (); 00107 return (_rl_next_macro_key ()); 00108 } 00109 00110 return (rl_executing_macro[executing_macro_index++]); 00111 } 00112 00113 /* Save the currently executing macro on a stack of saved macros. */ 00114 void 00115 _rl_push_executing_macro () 00116 { 00117 struct saved_macro *saver; 00118 00119 saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro)); 00120 saver->next = macro_list; 00121 saver->sindex = executing_macro_index; 00122 saver->string = rl_executing_macro; 00123 00124 macro_list = saver; 00125 } 00126 00127 /* Discard the current macro, replacing it with the one 00128 on the top of the stack of saved macros. */ 00129 void 00130 _rl_pop_executing_macro () 00131 { 00132 struct saved_macro *macro; 00133 00134 FREE (rl_executing_macro); 00135 rl_executing_macro = (char *)NULL; 00136 executing_macro_index = 0; 00137 00138 if (macro_list) 00139 { 00140 macro = macro_list; 00141 rl_executing_macro = macro_list->string; 00142 executing_macro_index = macro_list->sindex; 00143 macro_list = macro_list->next; 00144 free (macro); 00145 } 00146 00147 if (rl_executing_macro == 0) 00148 RL_UNSETSTATE(RL_STATE_MACROINPUT); 00149 } 00150 00151 /* Add a character to the macro being built. */ 00152 void 00153 _rl_add_macro_char (c) 00154 int c; 00155 { 00156 if (current_macro_index + 1 >= current_macro_size) 00157 { 00158 if (current_macro == 0) 00159 current_macro = (char *)xmalloc (current_macro_size = 25); 00160 else 00161 current_macro = (char *)xrealloc (current_macro, current_macro_size += 25); 00162 } 00163 00164 current_macro[current_macro_index++] = c; 00165 current_macro[current_macro_index] = '\0'; 00166 } 00167 00168 void 00169 _rl_kill_kbd_macro () 00170 { 00171 if (current_macro) 00172 { 00173 free (current_macro); 00174 current_macro = (char *) NULL; 00175 } 00176 current_macro_size = current_macro_index = 0; 00177 00178 FREE (rl_executing_macro); 00179 rl_executing_macro = (char *) NULL; 00180 executing_macro_index = 0; 00181 00182 RL_UNSETSTATE(RL_STATE_MACRODEF); 00183 } 00184 00185 /* Begin defining a keyboard macro. 00186 Keystrokes are recorded as they are executed. 00187 End the definition with rl_end_kbd_macro (). 00188 If a numeric argument was explicitly typed, then append this 00189 definition to the end of the existing macro, and start by 00190 re-executing the existing macro. */ 00191 int 00192 rl_start_kbd_macro (ignore1, ignore2) 00193 int ignore1, ignore2; 00194 { 00195 if (RL_ISSTATE (RL_STATE_MACRODEF)) 00196 { 00197 _rl_abort_internal (); 00198 return -1; 00199 } 00200 00201 if (rl_explicit_arg) 00202 { 00203 if (current_macro) 00204 _rl_with_macro_input (savestring (current_macro)); 00205 } 00206 else 00207 current_macro_index = 0; 00208 00209 RL_SETSTATE(RL_STATE_MACRODEF); 00210 return 0; 00211 } 00212 00213 /* Stop defining a keyboard macro. 00214 A numeric argument says to execute the macro right now, 00215 that many times, counting the definition as the first time. */ 00216 int 00217 rl_end_kbd_macro (count, ignore) 00218 int count, ignore; 00219 { 00220 if (RL_ISSTATE (RL_STATE_MACRODEF) == 0) 00221 { 00222 _rl_abort_internal (); 00223 return -1; 00224 } 00225 00226 current_macro_index -= rl_key_sequence_length - 1; 00227 current_macro[current_macro_index] = '\0'; 00228 00229 RL_UNSETSTATE(RL_STATE_MACRODEF); 00230 00231 return (rl_call_last_kbd_macro (--count, 0)); 00232 } 00233 00234 /* Execute the most recently defined keyboard macro. 00235 COUNT says how many times to execute it. */ 00236 int 00237 rl_call_last_kbd_macro (count, ignore) 00238 int count, ignore; 00239 { 00240 if (current_macro == 0) 00241 _rl_abort_internal (); 00242 00243 if (RL_ISSTATE (RL_STATE_MACRODEF)) 00244 { 00245 rl_ding (); /* no recursive macros */ 00246 current_macro[--current_macro_index] = '\0'; /* erase this char */ 00247 return 0; 00248 } 00249 00250 while (count--) 00251 _rl_with_macro_input (savestring (current_macro)); 00252 return 0; 00253 } 00254 00255 void 00256 rl_push_macro_input (macro) 00257 char *macro; 00258 { 00259 _rl_with_macro_input (macro); 00260 }
1.4.7

