121308Sache/* macro.c -- keyboard macros for readline. */
221308Sache
321308Sache/* Copyright (C) 1994 Free Software Foundation, Inc.
421308Sache
521308Sache   This file is part of the GNU Readline Library, a library for
621308Sache   reading lines of text with interactive input and history editing.
721308Sache
821308Sache   The GNU Readline Library is free software; you can redistribute it
921308Sache   and/or modify it under the terms of the GNU General Public License
1058310Sache   as published by the Free Software Foundation; either version 2, or
1121308Sache   (at your option) any later version.
1221308Sache
1321308Sache   The GNU Readline Library is distributed in the hope that it will be
1421308Sache   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
1521308Sache   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1621308Sache   GNU General Public License for more details.
1721308Sache
1821308Sache   The GNU General Public License is often shipped with GNU software, and
1921308Sache   is generally kept in a file called COPYING or LICENSE.  If you do not
2021308Sache   have a copy of the license, write to the Free Software Foundation,
2158310Sache   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
2221308Sache#define READLINE_LIBRARY
2321308Sache
2421308Sache#if defined (HAVE_CONFIG_H)
2521308Sache#  include <config.h>
2621308Sache#endif
2721308Sache
2821308Sache#include <sys/types.h>
2921308Sache
3021308Sache#if defined (HAVE_UNISTD_H)
3121308Sache#  include <unistd.h>           /* for _POSIX_VERSION */
3221308Sache#endif /* HAVE_UNISTD_H */
3321308Sache
3421308Sache#if defined (HAVE_STDLIB_H)
3521308Sache#  include <stdlib.h>
3621308Sache#else
3721308Sache#  include "ansi_stdlib.h"
3821308Sache#endif /* HAVE_STDLIB_H */
3921308Sache
4021308Sache#include <stdio.h>
4121308Sache
4221308Sache/* System-specific feature definitions and include files. */
4321308Sache#include "rldefs.h"
4421308Sache
4521308Sache/* Some standard library routines. */
4621308Sache#include "readline.h"
4721308Sache#include "history.h"
4821308Sache
4958310Sache#include "rlprivate.h"
5058310Sache#include "xmalloc.h"
5158310Sache
5221308Sache/* **************************************************************** */
5321308Sache/*								    */
5421308Sache/*			Hacking Keyboard Macros 		    */
5521308Sache/*								    */
5621308Sache/* **************************************************************** */
5721308Sache
5875406Sache/* The currently executing macro string.  If this is non-zero,
5975406Sache   then it is a malloc ()'ed string where input is coming from. */
6075406Sachechar *rl_executing_macro = (char *)NULL;
6175406Sache
6221308Sache/* The offset in the above string to the next character to be read. */
6321308Sachestatic int executing_macro_index;
6421308Sache
6521308Sache/* The current macro string being built.  Characters get stuffed
6621308Sache   in here by add_macro_char (). */
6721308Sachestatic char *current_macro = (char *)NULL;
6821308Sache
6921308Sache/* The size of the buffer allocated to current_macro. */
7021308Sachestatic int current_macro_size;
7121308Sache
7221308Sache/* The index at which characters are being added to current_macro. */
7321308Sachestatic int current_macro_index;
7421308Sache
7521308Sache/* A structure used to save nested macro strings.
7621308Sache   It is a linked list of string/index for each saved macro. */
7721308Sachestruct saved_macro {
7821308Sache  struct saved_macro *next;
7921308Sache  char *string;
8021308Sache  int sindex;
8121308Sache};
8221308Sache
8321308Sache/* The list of saved macros. */
8421308Sachestatic struct saved_macro *macro_list = (struct saved_macro *)NULL;
8521308Sache
8621308Sache/* Set up to read subsequent input from STRING.
8721308Sache   STRING is free ()'ed when we are done with it. */
8821308Sachevoid
8921308Sache_rl_with_macro_input (string)
9021308Sache     char *string;
9121308Sache{
9221308Sache  _rl_push_executing_macro ();
9375406Sache  rl_executing_macro = string;
9421308Sache  executing_macro_index = 0;
9575406Sache  RL_SETSTATE(RL_STATE_MACROINPUT);
9621308Sache}
9721308Sache
9821308Sache/* Return the next character available from a macro, or 0 if
9921308Sache   there are no macro characters. */
10021308Sacheint
10121308Sache_rl_next_macro_key ()
10221308Sache{
103157184Sache  int c;
104157184Sache
10575406Sache  if (rl_executing_macro == 0)
10621308Sache    return (0);
10721308Sache
10875406Sache  if (rl_executing_macro[executing_macro_index] == 0)
10921308Sache    {
11021308Sache      _rl_pop_executing_macro ();
11121308Sache      return (_rl_next_macro_key ());
11221308Sache    }
11321308Sache
114157184Sache#if defined (READLINE_CALLBACKS)
115157184Sache  c = rl_executing_macro[executing_macro_index++];
116165670Sache  if (RL_ISSTATE (RL_STATE_CALLBACK) && RL_ISSTATE (RL_STATE_READCMD|RL_STATE_MOREINPUT) && rl_executing_macro[executing_macro_index] == 0)
117157184Sache      _rl_pop_executing_macro ();
118157184Sache  return c;
119157184Sache#else
12075406Sache  return (rl_executing_macro[executing_macro_index++]);
121157184Sache#endif
12221308Sache}
12321308Sache
12421308Sache/* Save the currently executing macro on a stack of saved macros. */
12521308Sachevoid
12621308Sache_rl_push_executing_macro ()
12721308Sache{
12821308Sache  struct saved_macro *saver;
12921308Sache
13021308Sache  saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
13121308Sache  saver->next = macro_list;
13221308Sache  saver->sindex = executing_macro_index;
13375406Sache  saver->string = rl_executing_macro;
13421308Sache
13521308Sache  macro_list = saver;
13621308Sache}
13721308Sache
13821308Sache/* Discard the current macro, replacing it with the one
13921308Sache   on the top of the stack of saved macros. */
14021308Sachevoid
14121308Sache_rl_pop_executing_macro ()
14221308Sache{
14321308Sache  struct saved_macro *macro;
14421308Sache
14575406Sache  FREE (rl_executing_macro);
14675406Sache  rl_executing_macro = (char *)NULL;
14721308Sache  executing_macro_index = 0;
14821308Sache
14921308Sache  if (macro_list)
15021308Sache    {
15121308Sache      macro = macro_list;
15275406Sache      rl_executing_macro = macro_list->string;
15321308Sache      executing_macro_index = macro_list->sindex;
15421308Sache      macro_list = macro_list->next;
15521308Sache      free (macro);
15621308Sache    }
15775406Sache
15875406Sache  if (rl_executing_macro == 0)
15975406Sache    RL_UNSETSTATE(RL_STATE_MACROINPUT);
16021308Sache}
16121308Sache
16221308Sache/* Add a character to the macro being built. */
16321308Sachevoid
16421308Sache_rl_add_macro_char (c)
16521308Sache     int c;
16621308Sache{
16721308Sache  if (current_macro_index + 1 >= current_macro_size)
16821308Sache    {
16921308Sache      if (current_macro == 0)
170119610Sache	current_macro = (char *)xmalloc (current_macro_size = 25);
17121308Sache      else
172119610Sache	current_macro = (char *)xrealloc (current_macro, current_macro_size += 25);
17321308Sache    }
17421308Sache
17521308Sache  current_macro[current_macro_index++] = c;
17621308Sache  current_macro[current_macro_index] = '\0';
17721308Sache}
17821308Sache
17921308Sachevoid
18021308Sache_rl_kill_kbd_macro ()
18121308Sache{
18221308Sache  if (current_macro)
18321308Sache    {
18421308Sache      free (current_macro);
18521308Sache      current_macro = (char *) NULL;
18621308Sache    }
18721308Sache  current_macro_size = current_macro_index = 0;
18821308Sache
18975406Sache  FREE (rl_executing_macro);
19075406Sache  rl_executing_macro = (char *) NULL;
19121308Sache  executing_macro_index = 0;
19221308Sache
19375406Sache  RL_UNSETSTATE(RL_STATE_MACRODEF);
19421308Sache}
19521308Sache
19621308Sache/* Begin defining a keyboard macro.
19721308Sache   Keystrokes are recorded as they are executed.
19821308Sache   End the definition with rl_end_kbd_macro ().
19921308Sache   If a numeric argument was explicitly typed, then append this
20021308Sache   definition to the end of the existing macro, and start by
20121308Sache   re-executing the existing macro. */
20221308Sacheint
20321308Sacherl_start_kbd_macro (ignore1, ignore2)
20421308Sache     int ignore1, ignore2;
20521308Sache{
206119610Sache  if (RL_ISSTATE (RL_STATE_MACRODEF))
20721308Sache    {
20821308Sache      _rl_abort_internal ();
20921308Sache      return -1;
21021308Sache    }
21121308Sache
21221308Sache  if (rl_explicit_arg)
21321308Sache    {
21421308Sache      if (current_macro)
21521308Sache	_rl_with_macro_input (savestring (current_macro));
21621308Sache    }
21721308Sache  else
21821308Sache    current_macro_index = 0;
21921308Sache
22075406Sache  RL_SETSTATE(RL_STATE_MACRODEF);
22121308Sache  return 0;
22221308Sache}
22321308Sache
22421308Sache/* Stop defining a keyboard macro.
22521308Sache   A numeric argument says to execute the macro right now,
22621308Sache   that many times, counting the definition as the first time. */
22721308Sacheint
22821308Sacherl_end_kbd_macro (count, ignore)
22921308Sache     int count, ignore;
23021308Sache{
231119610Sache  if (RL_ISSTATE (RL_STATE_MACRODEF) == 0)
23221308Sache    {
23321308Sache      _rl_abort_internal ();
23421308Sache      return -1;
23521308Sache    }
23621308Sache
23721308Sache  current_macro_index -= rl_key_sequence_length - 1;
23821308Sache  current_macro[current_macro_index] = '\0';
23921308Sache
24075406Sache  RL_UNSETSTATE(RL_STATE_MACRODEF);
24121308Sache
24221308Sache  return (rl_call_last_kbd_macro (--count, 0));
24321308Sache}
24421308Sache
24521308Sache/* Execute the most recently defined keyboard macro.
24621308Sache   COUNT says how many times to execute it. */
24721308Sacheint
24821308Sacherl_call_last_kbd_macro (count, ignore)
24921308Sache     int count, ignore;
25021308Sache{
25121308Sache  if (current_macro == 0)
25221308Sache    _rl_abort_internal ();
25321308Sache
254119610Sache  if (RL_ISSTATE (RL_STATE_MACRODEF))
25521308Sache    {
25675406Sache      rl_ding ();		/* no recursive macros */
25721308Sache      current_macro[--current_macro_index] = '\0';	/* erase this char */
25821308Sache      return 0;
25921308Sache    }
26021308Sache
26121308Sache  while (count--)
26221308Sache    _rl_with_macro_input (savestring (current_macro));
26321308Sache  return 0;
26421308Sache}
26521308Sache
26621308Sachevoid
26721308Sacherl_push_macro_input (macro)
26821308Sache     char *macro;
26921308Sache{
27021308Sache  _rl_with_macro_input (macro);
27121308Sache}
272