xref: /openbsd-src/gnu/lib/libreadline/macro.c (revision 9704b281e65e1189747652d0ba55eee892cff5f7)
11acd27e7Smillert /* macro.c -- keyboard macros for readline. */
21acd27e7Smillert 
31acd27e7Smillert /* Copyright (C) 1994 Free Software Foundation, Inc.
41acd27e7Smillert 
51acd27e7Smillert    This file is part of the GNU Readline Library, a library for
61acd27e7Smillert    reading lines of text with interactive input and history editing.
71acd27e7Smillert 
81acd27e7Smillert    The GNU Readline Library is free software; you can redistribute it
91acd27e7Smillert    and/or modify it under the terms of the GNU General Public License
101acd27e7Smillert    as published by the Free Software Foundation; either version 2, or
111acd27e7Smillert    (at your option) any later version.
121acd27e7Smillert 
131acd27e7Smillert    The GNU Readline Library is distributed in the hope that it will be
141acd27e7Smillert    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
151acd27e7Smillert    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
161acd27e7Smillert    GNU General Public License for more details.
171acd27e7Smillert 
181acd27e7Smillert    The GNU General Public License is often shipped with GNU software, and
191acd27e7Smillert    is generally kept in a file called COPYING or LICENSE.  If you do not
201acd27e7Smillert    have a copy of the license, write to the Free Software Foundation,
211acd27e7Smillert    59 Temple Place, Suite 330, Boston, MA 02111 USA. */
221acd27e7Smillert #define READLINE_LIBRARY
231acd27e7Smillert 
241acd27e7Smillert #if defined (HAVE_CONFIG_H)
251acd27e7Smillert #  include <config.h>
261acd27e7Smillert #endif
271acd27e7Smillert 
281acd27e7Smillert #include <sys/types.h>
291acd27e7Smillert 
301acd27e7Smillert #if defined (HAVE_UNISTD_H)
311acd27e7Smillert #  include <unistd.h>           /* for _POSIX_VERSION */
321acd27e7Smillert #endif /* HAVE_UNISTD_H */
331acd27e7Smillert 
341acd27e7Smillert #if defined (HAVE_STDLIB_H)
351acd27e7Smillert #  include <stdlib.h>
361acd27e7Smillert #else
371acd27e7Smillert #  include "ansi_stdlib.h"
381acd27e7Smillert #endif /* HAVE_STDLIB_H */
391acd27e7Smillert 
401acd27e7Smillert #include <stdio.h>
411acd27e7Smillert 
421acd27e7Smillert /* System-specific feature definitions and include files. */
431acd27e7Smillert #include "rldefs.h"
441acd27e7Smillert 
451acd27e7Smillert /* Some standard library routines. */
461acd27e7Smillert #include "readline.h"
471acd27e7Smillert #include "history.h"
481acd27e7Smillert 
491acd27e7Smillert #include "rlprivate.h"
501acd27e7Smillert #include "xmalloc.h"
511acd27e7Smillert 
521acd27e7Smillert /* **************************************************************** */
531acd27e7Smillert /*								    */
541acd27e7Smillert /*			Hacking Keyboard Macros			    */
551acd27e7Smillert /*								    */
561acd27e7Smillert /* **************************************************************** */
571acd27e7Smillert 
581acd27e7Smillert /* The currently executing macro string.  If this is non-zero,
591acd27e7Smillert    then it is a malloc ()'ed string where input is coming from. */
60*15b117eaSkettenis char *rl_executing_macro = (char *)NULL;
611acd27e7Smillert 
621acd27e7Smillert /* The offset in the above string to the next character to be read. */
631acd27e7Smillert static int executing_macro_index;
641acd27e7Smillert 
651acd27e7Smillert /* The current macro string being built.  Characters get stuffed
661acd27e7Smillert    in here by add_macro_char (). */
671acd27e7Smillert static char *current_macro = (char *)NULL;
681acd27e7Smillert 
691acd27e7Smillert /* The size of the buffer allocated to current_macro. */
701acd27e7Smillert static int current_macro_size;
711acd27e7Smillert 
721acd27e7Smillert /* The index at which characters are being added to current_macro. */
731acd27e7Smillert static int current_macro_index;
741acd27e7Smillert 
751acd27e7Smillert /* A structure used to save nested macro strings.
761acd27e7Smillert    It is a linked list of string/index for each saved macro. */
771acd27e7Smillert struct saved_macro {
781acd27e7Smillert   struct saved_macro *next;
791acd27e7Smillert   char *string;
801acd27e7Smillert   int sindex;
811acd27e7Smillert };
821acd27e7Smillert 
831acd27e7Smillert /* The list of saved macros. */
841acd27e7Smillert static struct saved_macro *macro_list = (struct saved_macro *)NULL;
851acd27e7Smillert 
861acd27e7Smillert /* Set up to read subsequent input from STRING.
871acd27e7Smillert    STRING is free ()'ed when we are done with it. */
881acd27e7Smillert void
_rl_with_macro_input(string)891acd27e7Smillert _rl_with_macro_input (string)
901acd27e7Smillert      char *string;
911acd27e7Smillert {
921acd27e7Smillert   _rl_push_executing_macro ();
93*15b117eaSkettenis   rl_executing_macro = string;
941acd27e7Smillert   executing_macro_index = 0;
95*15b117eaSkettenis   RL_SETSTATE(RL_STATE_MACROINPUT);
961acd27e7Smillert }
971acd27e7Smillert 
981acd27e7Smillert /* Return the next character available from a macro, or 0 if
991acd27e7Smillert    there are no macro characters. */
1001acd27e7Smillert int
_rl_next_macro_key()1011acd27e7Smillert _rl_next_macro_key ()
1021acd27e7Smillert {
103*15b117eaSkettenis   if (rl_executing_macro == 0)
1041acd27e7Smillert     return (0);
1051acd27e7Smillert 
106*15b117eaSkettenis   if (rl_executing_macro[executing_macro_index] == 0)
1071acd27e7Smillert     {
1081acd27e7Smillert       _rl_pop_executing_macro ();
1091acd27e7Smillert       return (_rl_next_macro_key ());
1101acd27e7Smillert     }
1111acd27e7Smillert 
112*15b117eaSkettenis   return (rl_executing_macro[executing_macro_index++]);
1131acd27e7Smillert }
1141acd27e7Smillert 
1151acd27e7Smillert /* Save the currently executing macro on a stack of saved macros. */
1161acd27e7Smillert void
_rl_push_executing_macro()1171acd27e7Smillert _rl_push_executing_macro ()
1181acd27e7Smillert {
1191acd27e7Smillert   struct saved_macro *saver;
1201acd27e7Smillert 
1211acd27e7Smillert   saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
1221acd27e7Smillert   saver->next = macro_list;
1231acd27e7Smillert   saver->sindex = executing_macro_index;
124*15b117eaSkettenis   saver->string = rl_executing_macro;
1251acd27e7Smillert 
1261acd27e7Smillert   macro_list = saver;
1271acd27e7Smillert }
1281acd27e7Smillert 
1291acd27e7Smillert /* Discard the current macro, replacing it with the one
1301acd27e7Smillert    on the top of the stack of saved macros. */
1311acd27e7Smillert void
_rl_pop_executing_macro()1321acd27e7Smillert _rl_pop_executing_macro ()
1331acd27e7Smillert {
1341acd27e7Smillert   struct saved_macro *macro;
1351acd27e7Smillert 
136*15b117eaSkettenis   FREE (rl_executing_macro);
137*15b117eaSkettenis   rl_executing_macro = (char *)NULL;
1381acd27e7Smillert   executing_macro_index = 0;
1391acd27e7Smillert 
1401acd27e7Smillert   if (macro_list)
1411acd27e7Smillert     {
1421acd27e7Smillert       macro = macro_list;
143*15b117eaSkettenis       rl_executing_macro = macro_list->string;
1441acd27e7Smillert       executing_macro_index = macro_list->sindex;
1451acd27e7Smillert       macro_list = macro_list->next;
1461acd27e7Smillert       free (macro);
1471acd27e7Smillert     }
148*15b117eaSkettenis 
149*15b117eaSkettenis   if (rl_executing_macro == 0)
150*15b117eaSkettenis     RL_UNSETSTATE(RL_STATE_MACROINPUT);
1511acd27e7Smillert }
1521acd27e7Smillert 
1531acd27e7Smillert /* Add a character to the macro being built. */
1541acd27e7Smillert void
_rl_add_macro_char(c)1551acd27e7Smillert _rl_add_macro_char (c)
1561acd27e7Smillert      int c;
1571acd27e7Smillert {
1581acd27e7Smillert   if (current_macro_index + 1 >= current_macro_size)
1591acd27e7Smillert     {
1601acd27e7Smillert       if (current_macro == 0)
161*15b117eaSkettenis 	current_macro = (char *)xmalloc (current_macro_size = 25);
1621acd27e7Smillert       else
163*15b117eaSkettenis 	current_macro = (char *)xrealloc (current_macro, current_macro_size += 25);
1641acd27e7Smillert     }
1651acd27e7Smillert 
1661acd27e7Smillert   current_macro[current_macro_index++] = c;
1671acd27e7Smillert   current_macro[current_macro_index] = '\0';
1681acd27e7Smillert }
1691acd27e7Smillert 
1701acd27e7Smillert void
_rl_kill_kbd_macro()1711acd27e7Smillert _rl_kill_kbd_macro ()
1721acd27e7Smillert {
1731acd27e7Smillert   if (current_macro)
1741acd27e7Smillert     {
1751acd27e7Smillert       free (current_macro);
1761acd27e7Smillert       current_macro = (char *) NULL;
1771acd27e7Smillert     }
1781acd27e7Smillert   current_macro_size = current_macro_index = 0;
1791acd27e7Smillert 
180*15b117eaSkettenis   FREE (rl_executing_macro);
181*15b117eaSkettenis   rl_executing_macro = (char *) NULL;
1821acd27e7Smillert   executing_macro_index = 0;
1831acd27e7Smillert 
184*15b117eaSkettenis   RL_UNSETSTATE(RL_STATE_MACRODEF);
1851acd27e7Smillert }
1861acd27e7Smillert 
1871acd27e7Smillert /* Begin defining a keyboard macro.
1881acd27e7Smillert    Keystrokes are recorded as they are executed.
1891acd27e7Smillert    End the definition with rl_end_kbd_macro ().
1901acd27e7Smillert    If a numeric argument was explicitly typed, then append this
1911acd27e7Smillert    definition to the end of the existing macro, and start by
1921acd27e7Smillert    re-executing the existing macro. */
1931acd27e7Smillert int
rl_start_kbd_macro(ignore1,ignore2)1941acd27e7Smillert rl_start_kbd_macro (ignore1, ignore2)
1951acd27e7Smillert      int ignore1, ignore2;
1961acd27e7Smillert {
197*15b117eaSkettenis   if (RL_ISSTATE (RL_STATE_MACRODEF))
1981acd27e7Smillert     {
1991acd27e7Smillert       _rl_abort_internal ();
2001acd27e7Smillert       return -1;
2011acd27e7Smillert     }
2021acd27e7Smillert 
2031acd27e7Smillert   if (rl_explicit_arg)
2041acd27e7Smillert     {
2051acd27e7Smillert       if (current_macro)
2061acd27e7Smillert 	_rl_with_macro_input (savestring (current_macro));
2071acd27e7Smillert     }
2081acd27e7Smillert   else
2091acd27e7Smillert     current_macro_index = 0;
2101acd27e7Smillert 
211*15b117eaSkettenis   RL_SETSTATE(RL_STATE_MACRODEF);
2121acd27e7Smillert   return 0;
2131acd27e7Smillert }
2141acd27e7Smillert 
2151acd27e7Smillert /* Stop defining a keyboard macro.
2161acd27e7Smillert    A numeric argument says to execute the macro right now,
2171acd27e7Smillert    that many times, counting the definition as the first time. */
2181acd27e7Smillert int
rl_end_kbd_macro(count,ignore)2191acd27e7Smillert rl_end_kbd_macro (count, ignore)
2201acd27e7Smillert      int count, ignore;
2211acd27e7Smillert {
222*15b117eaSkettenis   if (RL_ISSTATE (RL_STATE_MACRODEF) == 0)
2231acd27e7Smillert     {
2241acd27e7Smillert       _rl_abort_internal ();
2251acd27e7Smillert       return -1;
2261acd27e7Smillert     }
2271acd27e7Smillert 
2281acd27e7Smillert   current_macro_index -= rl_key_sequence_length - 1;
2291acd27e7Smillert   current_macro[current_macro_index] = '\0';
2301acd27e7Smillert 
231*15b117eaSkettenis   RL_UNSETSTATE(RL_STATE_MACRODEF);
2321acd27e7Smillert 
2331acd27e7Smillert   return (rl_call_last_kbd_macro (--count, 0));
2341acd27e7Smillert }
2351acd27e7Smillert 
2361acd27e7Smillert /* Execute the most recently defined keyboard macro.
2371acd27e7Smillert    COUNT says how many times to execute it. */
2381acd27e7Smillert int
rl_call_last_kbd_macro(count,ignore)2391acd27e7Smillert rl_call_last_kbd_macro (count, ignore)
2401acd27e7Smillert      int count, ignore;
2411acd27e7Smillert {
2421acd27e7Smillert   if (current_macro == 0)
2431acd27e7Smillert     _rl_abort_internal ();
2441acd27e7Smillert 
245*15b117eaSkettenis   if (RL_ISSTATE (RL_STATE_MACRODEF))
2461acd27e7Smillert     {
247*15b117eaSkettenis       rl_ding ();		/* no recursive macros */
2481acd27e7Smillert       current_macro[--current_macro_index] = '\0';	/* erase this char */
2491acd27e7Smillert       return 0;
2501acd27e7Smillert     }
2511acd27e7Smillert 
2521acd27e7Smillert   while (count--)
2531acd27e7Smillert     _rl_with_macro_input (savestring (current_macro));
2541acd27e7Smillert   return 0;
2551acd27e7Smillert }
2561acd27e7Smillert 
2571acd27e7Smillert void
rl_push_macro_input(macro)2581acd27e7Smillert rl_push_macro_input (macro)
2591acd27e7Smillert      char *macro;
2601acd27e7Smillert {
2611acd27e7Smillert   _rl_with_macro_input (macro);
2621acd27e7Smillert }
263