Imported from ../bash-2.05a.tar.gz.
[platform/upstream/bash.git] / lib / readline / isearch.c
index ce2e58b..2e3e16b 100644 (file)
@@ -12,7 +12,7 @@
 
    The Library is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 1, or (at your option)
+   the Free Software Foundation; either version 2, or (at your option)
    any later version.
 
    The Library is distributed in the hope that it will be useful, but
@@ -23,7 +23,7 @@
    The GNU General Public License is often shipped with GNU software, and
    is generally kept in a file called COPYING or LICENSE.  If you do not
    have a copy of the license, write to the Free Software Foundation,
-   675 Mass Ave, Cambridge, MA 02139, USA. */
+   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
 #define READLINE_LIBRARY
 
 #if defined (HAVE_CONFIG_H)
 #include "readline.h"
 #include "history.h"
 
-/* Variables imported from other files in the readline library. */
-extern Keymap _rl_keymap;
-extern HIST_ENTRY *saved_line_for_history;
-extern int rl_line_buffer_len;
-extern int rl_point, rl_end;
-extern char *rl_line_buffer;
-
-extern void _rl_save_prompt ();
-extern void _rl_restore_prompt ();
+#include "rlprivate.h"
+#include "xmalloc.h"
 
-extern int rl_execute_next ();
-extern void rl_extend_line_buffer ();
+/* Variables exported to other files in the readline library. */
+char *_rl_isearch_terminators = (char *)NULL;
 
-extern int _rl_input_available ();
-
-extern char *xmalloc (), *xrealloc ();
+/* Variables imported from other files in the readline library. */
+extern HIST_ENTRY *_rl_saved_line_for_history;
 
-static int rl_search_history ();
+/* Forward declarations */
+static int rl_search_history PARAMS((int, int));
 
 /* Last line found by the current incremental search, so we don't `find'
    identical lines many times in a row. */
 static char *prev_line_found;
 
+/* Last search string and its length. */
+static char *last_isearch_string;
+static int last_isearch_string_len;
+
+static char *default_isearch_terminators = "\033\012";
+
 /* Search backwards through the history looking for a string which is typed
    interactively.  Start with the current line. */
 int
@@ -104,7 +103,7 @@ rl_display_search (search_string, reverse_p, where)
 
   searchlen = (search_string && *search_string) ? strlen (search_string) : 0;
 
-  message = xmalloc (searchlen + 33);
+  message = (char *)xmalloc (searchlen + 33);
   msglen = 0;
 
 #if defined (NOTDEF)
@@ -134,7 +133,7 @@ rl_display_search (search_string, reverse_p, where)
 
   strcpy (message + msglen, "': ");
 
-  rl_message ("%s", message, 0);
+  rl_message ("%s", message);
   free (message);
   (*rl_redisplay_function) ();
 }
@@ -178,14 +177,23 @@ rl_search_history (direction, invoking_key)
   /* Non-zero if we are doing a reverse search. */
   int reverse;
 
+  /* The list of characters which terminate the search, but are not
+     subsequently executed.  If the variable isearch-terminators has
+     been set, we use that value, otherwise we use ESC and C-J. */
+  char *isearch_terminators;
+
+  RL_SETSTATE(RL_STATE_ISEARCH);
   orig_point = rl_point;
   last_found_line = orig_line = where_history ();
   reverse = direction < 0;
   hlist = history_list ();
   allocated_line = (char *)NULL;
 
+  isearch_terminators = _rl_isearch_terminators ? _rl_isearch_terminators
+                                               : default_isearch_terminators;
+
   /* Create an arrary of pointers to the lines that we want to search. */
-  maybe_replace_line ();
+  rl_maybe_replace_line ();
   i = 0;
   if (hlist)
     for (i = 0; hlist[i]; i++);
@@ -196,12 +204,12 @@ rl_search_history (direction, invoking_key)
   for (i = 0; i < hlen; i++)
     lines[i] = hlist[i]->line;
 
-  if (saved_line_for_history)
-    lines[i] = saved_line_for_history->line;
+  if (_rl_saved_line_for_history)
+    lines[i] = _rl_saved_line_for_history->line;
   else
     {
       /* Keep track of this so we can free it. */
-      allocated_line = xmalloc (1 + strlen (rl_line_buffer));
+      allocated_line = (char *)xmalloc (1 + strlen (rl_line_buffer));
       strcpy (allocated_line, &rl_line_buffer[0]);
       lines[i] = allocated_line;
     }
@@ -211,10 +219,10 @@ rl_search_history (direction, invoking_key)
   /* The line where we start the search. */
   i = orig_line;
 
-  _rl_save_prompt ();
+  rl_save_prompt ();
 
   /* Initialize search parameters. */
-  search_string = xmalloc (search_string_size = 128);
+  search_string = (char *)xmalloc (search_string_size = 128);
   *search_string = '\0';
   search_string_index = 0;
   prev_line_found = (char *)0;         /* XXX */
@@ -231,12 +239,14 @@ rl_search_history (direction, invoking_key)
   found = failed = 0;
   for (;;)
     {
-      Function *f = (Function *)NULL;
+      rl_command_func_t *f = (rl_command_func_t *)NULL;
 
       /* Read a key and decide how to proceed. */
+      RL_SETSTATE(RL_STATE_MOREINPUT);
       c = rl_read_key ();
+      RL_UNSETSTATE(RL_STATE_MOREINPUT);
 
-      if (_rl_keymap[c].type == ISFUNC)
+      if (c >= 0 && _rl_keymap[c].type == ISFUNC)
        {
          f = _rl_keymap[c].function;
 
@@ -246,10 +256,18 @@ rl_search_history (direction, invoking_key)
            c =  !reverse ? -1 : -2;
        }
 
+#if 0
       /* Let NEWLINE (^J) terminate the search for people who don't like
         using ESC.  ^M can still be used to terminate the search and
         immediately execute the command. */
       if (c == ESC || c == NEWLINE)
+#else
+      /* The characters in isearch_terminators (set from the user-settable
+        variable isearch-terminators) are used to terminate the search but
+        not subsequently execute the character as a command.  The default
+        value is "\033\012" (ESC and C-J). */
+      if (strchr (isearch_terminators, c))
+#endif
        {
          /* ESC still terminates the search, but if there is pending
             input or if input arrives within 0.1 seconds (on systems
@@ -262,8 +280,10 @@ rl_search_history (direction, invoking_key)
          break;
        }
 
-      if (c >= 0 && (CTRL_CHAR (c) || META_CHAR (c) || c == RUBOUT) && c != CTRL ('g'))
+      if (c >= 0 && (CTRL_CHAR (c) || META_CHAR (c) || c == RUBOUT) && c != CTRL ('G'))
        {
+         /* This sets rl_pending_input to c; it will be picked up the next
+            time rl_read_key is called. */
          rl_execute_next (c);
          break;
        }
@@ -272,13 +292,24 @@ rl_search_history (direction, invoking_key)
        {
        case -1:
          if (search_string_index == 0)
-           continue;
+           {
+             if (last_isearch_string)
+               {
+                 search_string_size = 64 + last_isearch_string_len;
+                 search_string = (char *)xrealloc (search_string, search_string_size);
+                 strcpy (search_string, last_isearch_string);
+                 search_string_index = last_isearch_string_len;
+                 rl_display_search (search_string, reverse, -1);
+                 break;
+               }
+             continue;
+           }
          else if (reverse)
            --line_index;
          else if (line_index != sline_len)
            ++line_index;
          else
-           ding ();
+           rl_ding ();
          break;
 
          /* switch directions */
@@ -291,19 +322,35 @@ rl_search_history (direction, invoking_key)
          strcpy (rl_line_buffer, lines[orig_line]);
          rl_point = orig_point;
          rl_end = strlen (rl_line_buffer);
-         _rl_restore_prompt();
+         rl_restore_prompt();
          rl_clear_message ();
          if (allocated_line)
            free (allocated_line);
          free (lines);
+         RL_UNSETSTATE(RL_STATE_ISEARCH);
          return 0;
 
+#if 0
+       /* delete character from search string. */
+       case -3:
+         if (search_string_index == 0)
+           rl_ding ();
+         else
+           {
+             search_string[--search_string_index] = '\0';
+             /* This is tricky.  To do this right, we need to keep a
+                stack of search positions for the current search, with
+                sentinels marking the beginning and end. */
+           }
+         break;
+#endif
+
        default:
          /* Add character to search string and continue search. */
          if (search_string_index + 2 >= search_string_size)
            {
              search_string_size += 128;
-             search_string = xrealloc (search_string, search_string_size);
+             search_string = (char *)xrealloc (search_string, search_string_size);
            }
          search_string[search_string_index++] = c;
          search_string[search_string_index] = '\0';
@@ -360,7 +407,7 @@ rl_search_history (direction, invoking_key)
       if (failed)
        {
          /* We cannot find the search string.  Ding the bell. */
-         ding ();
+         rl_ding ();
          i = last_found_line;
          continue;             /* XXX - was break */
        }
@@ -394,15 +441,22 @@ rl_search_history (direction, invoking_key)
   /* First put back the original state. */
   strcpy (rl_line_buffer, lines[orig_line]);
 
-  _rl_restore_prompt ();
+  rl_restore_prompt ();
 
+#if 1
+  /* Save the search string for possible later use. */
+  FREE (last_isearch_string);
+  last_isearch_string = search_string;
+  last_isearch_string_len = search_string_index;
+#else
   /* Free the search string. */
   free (search_string);
+#endif
 
   if (last_found_line < orig_line)
-    rl_get_previous_history (orig_line - last_found_line);
+    rl_get_previous_history (orig_line - last_found_line, 0);
   else
-    rl_get_next_history (last_found_line - orig_line);
+    rl_get_next_history (last_found_line - orig_line, 0);
 
   /* If the string was not found, put point at the end of the line. */
   if (line_index < 0)
@@ -410,9 +464,10 @@ rl_search_history (direction, invoking_key)
   rl_point = line_index;
   rl_clear_message ();
 
-  if (allocated_line)
-    free (allocated_line);
+  FREE (allocated_line);
   free (lines);
 
+  RL_UNSETSTATE(RL_STATE_ISEARCH);
+
   return 0;
 }