macro.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* macro.c -- keyboard macros for readline. */
  2. /* Copyright (C) 1994-2009 Free Software Foundation, Inc.
  3. This file is part of the GNU Readline Library (Readline), a library
  4. for reading lines of text with interactive input and history editing.
  5. Readline is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Readline is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Readline. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #define READLINE_LIBRARY
  17. #if defined (HAVE_CONFIG_H)
  18. # include <config.h>
  19. #endif
  20. #include <sys/types.h>
  21. #if defined (HAVE_UNISTD_H)
  22. # include <unistd.h> /* for _POSIX_VERSION */
  23. #endif /* HAVE_UNISTD_H */
  24. #if defined (HAVE_STDLIB_H)
  25. # include <stdlib.h>
  26. #else
  27. # include "ansi_stdlib.h"
  28. #endif /* HAVE_STDLIB_H */
  29. #include <stdio.h>
  30. /* System-specific feature definitions and include files. */
  31. #include "rldefs.h"
  32. /* Some standard library routines. */
  33. #include "readline.h"
  34. #include "history.h"
  35. #include "rlprivate.h"
  36. #include "xmalloc.h"
  37. /* **************************************************************** */
  38. /* */
  39. /* Hacking Keyboard Macros */
  40. /* */
  41. /* **************************************************************** */
  42. /* The currently executing macro string. If this is non-zero,
  43. then it is a malloc ()'ed string where input is coming from. */
  44. char *rl_executing_macro = (char *)NULL;
  45. /* The offset in the above string to the next character to be read. */
  46. static int executing_macro_index;
  47. /* The current macro string being built. Characters get stuffed
  48. in here by add_macro_char (). */
  49. static char *current_macro = (char *)NULL;
  50. /* The size of the buffer allocated to current_macro. */
  51. static int current_macro_size;
  52. /* The index at which characters are being added to current_macro. */
  53. static int current_macro_index;
  54. /* A structure used to save nested macro strings.
  55. It is a linked list of string/index for each saved macro. */
  56. struct saved_macro {
  57. struct saved_macro *next;
  58. char *string;
  59. int sindex;
  60. };
  61. /* The list of saved macros. */
  62. static struct saved_macro *macro_list = (struct saved_macro *)NULL;
  63. /* Set up to read subsequent input from STRING.
  64. STRING is free ()'ed when we are done with it. */
  65. void
  66. _rl_with_macro_input (string)
  67. char *string;
  68. {
  69. _rl_push_executing_macro ();
  70. rl_executing_macro = string;
  71. executing_macro_index = 0;
  72. RL_SETSTATE(RL_STATE_MACROINPUT);
  73. }
  74. /* Return the next character available from a macro, or 0 if
  75. there are no macro characters. */
  76. int
  77. _rl_next_macro_key ()
  78. {
  79. int c;
  80. if (rl_executing_macro == 0)
  81. return (0);
  82. if (rl_executing_macro[executing_macro_index] == 0)
  83. {
  84. _rl_pop_executing_macro ();
  85. return (_rl_next_macro_key ());
  86. }
  87. #if defined (READLINE_CALLBACKS)
  88. c = rl_executing_macro[executing_macro_index++];
  89. if (RL_ISSTATE (RL_STATE_CALLBACK) && RL_ISSTATE (RL_STATE_READCMD|RL_STATE_MOREINPUT) && rl_executing_macro[executing_macro_index] == 0)
  90. _rl_pop_executing_macro ();
  91. return c;
  92. #else
  93. return (rl_executing_macro[executing_macro_index++]);
  94. #endif
  95. }
  96. /* Save the currently executing macro on a stack of saved macros. */
  97. void
  98. _rl_push_executing_macro ()
  99. {
  100. struct saved_macro *saver;
  101. saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
  102. saver->next = macro_list;
  103. saver->sindex = executing_macro_index;
  104. saver->string = rl_executing_macro;
  105. macro_list = saver;
  106. }
  107. /* Discard the current macro, replacing it with the one
  108. on the top of the stack of saved macros. */
  109. void
  110. _rl_pop_executing_macro ()
  111. {
  112. struct saved_macro *macro;
  113. FREE (rl_executing_macro);
  114. rl_executing_macro = (char *)NULL;
  115. executing_macro_index = 0;
  116. if (macro_list)
  117. {
  118. macro = macro_list;
  119. rl_executing_macro = macro_list->string;
  120. executing_macro_index = macro_list->sindex;
  121. macro_list = macro_list->next;
  122. xfree (macro);
  123. }
  124. if (rl_executing_macro == 0)
  125. RL_UNSETSTATE(RL_STATE_MACROINPUT);
  126. }
  127. /* Add a character to the macro being built. */
  128. void
  129. _rl_add_macro_char (c)
  130. int c;
  131. {
  132. if (current_macro_index + 1 >= current_macro_size)
  133. {
  134. if (current_macro == 0)
  135. current_macro = (char *)xmalloc (current_macro_size = 25);
  136. else
  137. current_macro = (char *)xrealloc (current_macro, current_macro_size += 25);
  138. }
  139. current_macro[current_macro_index++] = c;
  140. current_macro[current_macro_index] = '\0';
  141. }
  142. void
  143. _rl_kill_kbd_macro ()
  144. {
  145. if (current_macro)
  146. {
  147. xfree (current_macro);
  148. current_macro = (char *) NULL;
  149. }
  150. current_macro_size = current_macro_index = 0;
  151. FREE (rl_executing_macro);
  152. rl_executing_macro = (char *) NULL;
  153. executing_macro_index = 0;
  154. RL_UNSETSTATE(RL_STATE_MACRODEF);
  155. }
  156. /* Begin defining a keyboard macro.
  157. Keystrokes are recorded as they are executed.
  158. End the definition with rl_end_kbd_macro ().
  159. If a numeric argument was explicitly typed, then append this
  160. definition to the end of the existing macro, and start by
  161. re-executing the existing macro. */
  162. int
  163. rl_start_kbd_macro (ignore1, ignore2)
  164. int ignore1, ignore2;
  165. {
  166. if (RL_ISSTATE (RL_STATE_MACRODEF))
  167. {
  168. _rl_abort_internal ();
  169. return -1;
  170. }
  171. if (rl_explicit_arg)
  172. {
  173. if (current_macro)
  174. _rl_with_macro_input (savestring (current_macro));
  175. }
  176. else
  177. current_macro_index = 0;
  178. RL_SETSTATE(RL_STATE_MACRODEF);
  179. return 0;
  180. }
  181. /* Stop defining a keyboard macro.
  182. A numeric argument says to execute the macro right now,
  183. that many times, counting the definition as the first time. */
  184. int
  185. rl_end_kbd_macro (count, ignore)
  186. int count, ignore;
  187. {
  188. if (RL_ISSTATE (RL_STATE_MACRODEF) == 0)
  189. {
  190. _rl_abort_internal ();
  191. return -1;
  192. }
  193. current_macro_index -= rl_key_sequence_length - 1;
  194. current_macro[current_macro_index] = '\0';
  195. RL_UNSETSTATE(RL_STATE_MACRODEF);
  196. return (rl_call_last_kbd_macro (--count, 0));
  197. }
  198. /* Execute the most recently defined keyboard macro.
  199. COUNT says how many times to execute it. */
  200. int
  201. rl_call_last_kbd_macro (count, ignore)
  202. int count, ignore;
  203. {
  204. if (current_macro == 0)
  205. _rl_abort_internal ();
  206. if (RL_ISSTATE (RL_STATE_MACRODEF))
  207. {
  208. rl_ding (); /* no recursive macros */
  209. current_macro[--current_macro_index] = '\0'; /* erase this char */
  210. return 0;
  211. }
  212. while (count--)
  213. _rl_with_macro_input (savestring (current_macro));
  214. return 0;
  215. }
  216. void
  217. rl_push_macro_input (macro)
  218. char *macro;
  219. {
  220. _rl_with_macro_input (macro);
  221. }