PR gdb/11327, PR gdb/11328, PR breakpoints/11368:
[deliverable/binutils-gdb.git] / readline / isearch.c
index 67279e18924ae9a9e15d7ced402476a7a7957f66..d7d8520ed859b6e447c05c551f324e052e56511e 100644 (file)
@@ -4,7 +4,7 @@
 /*                                                                 */
 /* **************************************************************** */
 
 /*                                                                 */
 /* **************************************************************** */
 
-/* Copyright (C) 1987,1989 Free Software Foundation, Inc.
+/* Copyright (C) 1987-2005 Free Software Foundation, Inc.
 
    This file contains the Readline Library (the Library), a set of
    routines for providing Emacs style line input to programs that ask
 
    This file contains the Readline Library (the Library), a set of
    routines for providing Emacs style line input to programs that ask
@@ -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 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
    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,
    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)
 #define READLINE_LIBRARY
 
 #if defined (HAVE_CONFIG_H)
 #endif
 
 #include "rldefs.h"
 #endif
 
 #include "rldefs.h"
+#include "rlmbutil.h"
+
 #include "readline.h"
 #include "history.h"
 
 #include "readline.h"
 #include "history.h"
 
-/* Variables exported to other files in the readline library. */
-unsigned char *_rl_isearch_terminators = (unsigned char *)NULL;
+#include "rlprivate.h"
+#include "xmalloc.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;
+/* Variables exported to other files in the readline library. */
+char *_rl_isearch_terminators = (char *)NULL;
 
 
-extern int rl_execute_next ();
-extern void rl_extend_line_buffer ();
+_rl_search_cxt *_rl_iscxt = 0;
 
 
-extern int _rl_input_available ();
+/* Variables imported from other files in the readline library. */
+extern HIST_ENTRY *_rl_saved_line_for_history;
 
 
-extern char *xmalloc (), *xrealloc ();
+static int rl_search_history PARAMS((int, int));
 
 
-static int rl_search_history ();
+static _rl_search_cxt *_rl_isearch_init PARAMS((int));
+static void _rl_isearch_fini PARAMS((_rl_search_cxt *));
+static int _rl_isearch_cleanup PARAMS((_rl_search_cxt *, 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 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";
+
+_rl_search_cxt *
+_rl_scxt_alloc (type, flags)
+     int type, flags;
+{
+  _rl_search_cxt *cxt;
+
+  cxt = (_rl_search_cxt *)xmalloc (sizeof (_rl_search_cxt));
+
+  cxt->type = type;
+  cxt->sflags = flags;
+
+  cxt->search_string = 0;
+  cxt->search_string_size = cxt->search_string_index = 0;
+
+  cxt->lines = 0;
+  cxt->allocated_line = 0;
+  cxt->hlen = cxt->hindex = 0;
+
+  cxt->save_point = rl_point;
+  cxt->save_mark = rl_mark;
+  cxt->save_line = where_history ();
+  cxt->last_found_line = cxt->save_line;
+  cxt->prev_line_found = 0;
+
+  cxt->save_undo_list = 0;
+
+  cxt->history_pos = 0;
+  cxt->direction = 0;
+
+  cxt->lastc = 0;
+
+  cxt->sline = 0;
+  cxt->sline_len = cxt->sline_index = 0;
+
+  cxt->search_terminators = 0;
+
+  return cxt;
+}
+
+void
+_rl_scxt_dispose (cxt, flags)
+     _rl_search_cxt *cxt;
+     int flags;
+{
+  FREE (cxt->search_string);
+  FREE (cxt->allocated_line);
+  FREE (cxt->lines);
+
+  free (cxt);
+}
+
 /* Search backwards through the history looking for a string which is typed
    interactively.  Start with the current line. */
 int
 /* Search backwards through the history looking for a string which is typed
    interactively.  Start with the current line. */
 int
@@ -91,7 +148,7 @@ rl_forward_search_history (sign, key)
 
 /* Display the current state of the search in the echo-area.
    SEARCH_STRING contains the string that is being searched for,
 
 /* Display the current state of the search in the echo-area.
    SEARCH_STRING contains the string that is being searched for,
-   DIRECTION is zero for forward, or 1 for reverse,
+   DIRECTION is zero for forward, or non-zero for reverse,
    WHERE is the history list number of the current line.  If it is
    -1, then this line is the starting one. */
 static void
    WHERE is the history list number of the current line.  If it is
    -1, then this line is the starting one. */
 static void
@@ -104,7 +161,7 @@ rl_display_search (search_string, reverse_p, where)
 
   searchlen = (search_string && *search_string) ? strlen (search_string) : 0;
 
 
   searchlen = (search_string && *search_string) ? strlen (search_string) : 0;
 
-  message = xmalloc (searchlen + 33);
+  message = (char *)xmalloc (searchlen + 33);
   msglen = 0;
 
 #if defined (NOTDEF)
   msglen = 0;
 
 #if defined (NOTDEF)
@@ -134,316 +191,476 @@ rl_display_search (search_string, reverse_p, where)
 
   strcpy (message + msglen, "': ");
 
 
   strcpy (message + msglen, "': ");
 
-  rl_message ("%s", message, 0);
+  rl_message ("%s", message);
   free (message);
   (*rl_redisplay_function) ();
 }
 
   free (message);
   (*rl_redisplay_function) ();
 }
 
-/* Search through the history looking for an interactively typed string.
-   This is analogous to i-search.  We start the search in the current line.
-   DIRECTION is which direction to search; >= 0 means forward, < 0 means
-   backwards. */
-static int
-rl_search_history (direction, invoking_key)
-     int direction, invoking_key;
+static _rl_search_cxt *
+_rl_isearch_init (direction)
+     int direction;
 {
 {
-  /* The string that the user types in to search for. */
-  char *search_string;
-
-  /* The current length of SEARCH_STRING. */
-  int search_string_index;
-
-  /* The amount of space that SEARCH_STRING has allocated to it. */
-  int search_string_size;
-
-  /* The list of lines to search through. */
-  char **lines, *allocated_line;
-
-  /* The length of LINES. */
-  int hlen;
-
-  /* Where we get LINES from. */
-  HIST_ENTRY **hlist;
-
+  _rl_search_cxt *cxt;
   register int i;
   register int i;
-  int orig_point, orig_line, last_found_line;
-  int c, found, failed, sline_len;
-
-  /* The line currently being searched. */
-  char *sline;
-
-  /* Offset in that line. */
-  int line_index;
-
-  /* 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. */
-  unsigned char *isearch_terminators;
+  HIST_ENTRY **hlist;
 
 
-  orig_point = rl_point;
-  last_found_line = orig_line = where_history ();
-  reverse = direction < 0;
-  hlist = history_list ();
-  allocated_line = (char *)NULL;
+  cxt = _rl_scxt_alloc (RL_SEARCH_ISEARCH, 0);
+  if (direction < 0)
+    cxt->sflags |= SF_REVERSE;
 
 
-  isearch_terminators = _rl_isearch_terminators ? _rl_isearch_terminators
-                                               : (unsigned char *)"\033\012";
+  cxt->search_terminators = _rl_isearch_terminators ? _rl_isearch_terminators
+                                               : default_isearch_terminators;
 
   /* Create an arrary of pointers to the lines that we want to search. */
 
   /* Create an arrary of pointers to the lines that we want to search. */
-  maybe_replace_line ();
+  hlist = history_list ();
+  rl_maybe_replace_line ();
   i = 0;
   if (hlist)
     for (i = 0; hlist[i]; i++);
 
   /* Allocate space for this many lines, +1 for the current input line,
      and remember those lines. */
   i = 0;
   if (hlist)
     for (i = 0; hlist[i]; i++);
 
   /* Allocate space for this many lines, +1 for the current input line,
      and remember those lines. */
-  lines = (char **)xmalloc ((1 + (hlen = i)) * sizeof (char *));
-  for (i = 0; i < hlen; i++)
-    lines[i] = hlist[i]->line;
+  cxt->lines = (char **)xmalloc ((1 + (cxt->hlen = i)) * sizeof (char *));
+  for (i = 0; i < cxt->hlen; i++)
+    cxt->lines[i] = hlist[i]->line;
 
 
-  if (saved_line_for_history)
-    lines[i] = saved_line_for_history->line;
+  if (_rl_saved_line_for_history)
+    cxt->lines[i] = _rl_saved_line_for_history->line;
   else
     {
       /* Keep track of this so we can free it. */
   else
     {
       /* Keep track of this so we can free it. */
-      allocated_line = xmalloc (1 + strlen (rl_line_buffer));
-      strcpy (allocated_line, &rl_line_buffer[0]);
-      lines[i] = allocated_line;
+      cxt->allocated_line = (char *)xmalloc (1 + strlen (rl_line_buffer));
+      strcpy (cxt->allocated_line, &rl_line_buffer[0]);
+      cxt->lines[i] = cxt->allocated_line;
     }
 
     }
 
-  hlen++;
+  cxt->hlen++;
 
   /* The line where we start the search. */
 
   /* The line where we start the search. */
-  i = orig_line;
+  cxt->history_pos = cxt->save_line;
 
   rl_save_prompt ();
 
   /* Initialize search parameters. */
 
   rl_save_prompt ();
 
   /* Initialize search parameters. */
-  search_string = xmalloc (search_string_size = 128);
-  *search_string = '\0';
-  search_string_index = 0;
-  prev_line_found = (char *)0;         /* XXX */
+  cxt->search_string = (char *)xmalloc (cxt->search_string_size = 128);
+  cxt->search_string[cxt->search_string_index = 0] = '\0';
 
   /* Normalize DIRECTION into 1 or -1. */
 
   /* Normalize DIRECTION into 1 or -1. */
-  direction = (direction >= 0) ? 1 : -1;
+  cxt->direction = (direction >= 0) ? 1 : -1;
 
 
-  rl_display_search (search_string, reverse, -1);
+  cxt->sline = rl_line_buffer;
+  cxt->sline_len = strlen (cxt->sline);
+  cxt->sline_index = rl_point;
 
 
-  sline = rl_line_buffer;
-  sline_len = strlen (sline);
-  line_index = rl_point;
+  _rl_iscxt = cxt;             /* save globally */
 
 
-  found = failed = 0;
-  for (;;)
+  return cxt;
+}
+
+static void
+_rl_isearch_fini (cxt)
+     _rl_search_cxt *cxt;
+{
+  /* First put back the original state. */
+  strcpy (rl_line_buffer, cxt->lines[cxt->save_line]);
+
+  rl_restore_prompt ();
+
+  /* Save the search string for possible later use. */
+  FREE (last_isearch_string);
+  last_isearch_string = cxt->search_string;
+  last_isearch_string_len = cxt->search_string_index;
+  cxt->search_string = 0;
+
+  if (cxt->last_found_line < cxt->save_line)
+    rl_get_previous_history (cxt->save_line - cxt->last_found_line, 0);
+  else
+    rl_get_next_history (cxt->last_found_line - cxt->save_line, 0);
+
+  /* If the string was not found, put point at the end of the last matching
+     line.  If last_found_line == orig_line, we didn't find any matching
+     history lines at all, so put point back in its original position. */
+  if (cxt->sline_index < 0)
     {
     {
-      Function *f = (Function *)NULL;
+      if (cxt->last_found_line == cxt->save_line)
+       cxt->sline_index = cxt->save_point;
+      else
+       cxt->sline_index = strlen (rl_line_buffer);
+      rl_mark = cxt->save_mark;
+    }
 
 
-      /* Read a key and decide how to proceed. */
-      c = rl_read_key ();
+  rl_point = cxt->sline_index;
+  /* Don't worry about where to put the mark here; rl_get_previous_history
+     and rl_get_next_history take care of it. */
 
 
-      if (_rl_keymap[c].type == ISFUNC)
-       {
-         f = _rl_keymap[c].function;
+  rl_clear_message ();
+}
 
 
-         if (f == rl_reverse_search_history)
-           c = reverse ? -1 : -2;
-         else if (f == rl_forward_search_history)
-           c =  !reverse ? -1 : -2;
-       }
+int
+_rl_search_getchar (cxt)
+     _rl_search_cxt *cxt;
+{
+  int c;
 
 
-#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))
+  /* Read a key and decide how to proceed. */
+  RL_SETSTATE(RL_STATE_MOREINPUT);
+  c = cxt->lastc = rl_read_key ();
+  RL_UNSETSTATE(RL_STATE_MOREINPUT);
+
+#if defined (HANDLE_MULTIBYTE)
+  if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+    c = cxt->lastc = _rl_read_mbstring (cxt->lastc, cxt->mb, MB_LEN_MAX);
 #endif
 #endif
+
+  return c;
+}
+
+/* Process just-read character C according to isearch context CXT.  Return
+   -1 if the caller should just free the context and return, 0 if we should
+   break out of the loop, and 1 if we should continue to read characters. */
+int
+_rl_isearch_dispatch (cxt, c)
+     _rl_search_cxt *cxt;
+     int c;
+{
+  int n, wstart, wlen, limit, cval;
+  rl_command_func_t *f;
+
+  f = (rl_command_func_t *)NULL;
+ /* Translate the keys we do something with to opcodes. */
+  if (c >= 0 && _rl_keymap[c].type == ISFUNC)
+    {
+      f = _rl_keymap[c].function;
+
+      if (f == rl_reverse_search_history)
+       cxt->lastc = (cxt->sflags & SF_REVERSE) ? -1 : -2;
+      else if (f == rl_forward_search_history)
+       cxt->lastc = (cxt->sflags & SF_REVERSE) ? -2 : -1;
+      else if (f == rl_rubout)
+       cxt->lastc = -3;
+      else if (c == CTRL ('G'))
+       cxt->lastc = -4;
+      else if (c == CTRL ('W'))        /* XXX */
+       cxt->lastc = -5;
+      else if (c == CTRL ('Y'))        /* XXX */
+       cxt->lastc = -6;
+    }
+
+  /* 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 (cxt->search_terminators, cxt->lastc))
+    {
+      /* ESC still terminates the search, but if there is pending
+        input or if input arrives within 0.1 seconds (on systems
+        with select(2)) it is used as a prefix character
+        with rl_execute_next.  WATCH OUT FOR THIS!  This is intended
+        to allow the arrow keys to be used like ^F and ^B are used
+        to terminate the search and execute the movement command.
+        XXX - since _rl_input_available depends on the application-
+        settable keyboard timeout value, this could alternatively
+        use _rl_input_queued(100000) */
+      if (cxt->lastc == ESC && _rl_input_available ())
+       rl_execute_next (ESC);
+      return (0);
+    }
+
+#define ENDSRCH_CHAR(c) \
+  ((CTRL_CHAR (c) || META_CHAR (c) || (c) == RUBOUT) && ((c) != CTRL ('G')))
+
+#if defined (HANDLE_MULTIBYTE)
+  if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+    {
+      if (cxt->lastc >= 0 && (cxt->mb[0] && cxt->mb[1] == '\0') && ENDSRCH_CHAR (cxt->lastc))
        {
        {
-         /* ESC still terminates the search, but if there is pending
-            input or if input arrives within 0.1 seconds (on systems
-            with select(2)) it is used as a prefix character
-            with rl_execute_next.  WATCH OUT FOR THIS!  This is intended
-            to allow the arrow keys to be used like ^F and ^B are used
-            to terminate the search and execute the movement command. */
-         if (c == ESC && _rl_input_available ())       /* XXX */
-           rl_execute_next (ESC);
-         break;
+         /* This sets rl_pending_input to c; it will be picked up the next
+            time rl_read_key is called. */
+         rl_execute_next (cxt->lastc);
+         return (0);
        }
        }
-
-      if (c >= 0 && (CTRL_CHAR (c) || META_CHAR (c) || c == RUBOUT) && c != CTRL ('G'))
+    }
+  else
+#endif
+    if (cxt->lastc >= 0 && ENDSRCH_CHAR (cxt->lastc))
+      {
+       /* This sets rl_pending_input to LASTC; it will be picked up the next
+          time rl_read_key is called. */
+       rl_execute_next (cxt->lastc);
+       return (0);
+      }
+
+  /* Now dispatch on the character.  `Opcodes' affect the search string or
+     state.  Other characters are added to the string.  */
+  switch (cxt->lastc)
+    {
+    /* search again */
+    case -1:
+      if (cxt->search_string_index == 0)
        {
        {
-         rl_execute_next (c);
-         break;
+         if (last_isearch_string)
+           {
+             cxt->search_string_size = 64 + last_isearch_string_len;
+             cxt->search_string = (char *)xrealloc (cxt->search_string, cxt->search_string_size);
+             strcpy (cxt->search_string, last_isearch_string);
+             cxt->search_string_index = last_isearch_string_len;
+             rl_display_search (cxt->search_string, (cxt->sflags & SF_REVERSE), -1);
+             break;
+           }
+         return (1);
        }
        }
-
-      switch (c)
+      else if (cxt->sflags & SF_REVERSE)
+       cxt->sline_index--;
+      else if (cxt->sline_index != cxt->sline_len)
+       cxt->sline_index++;
+      else
+       rl_ding ();
+      break;
+
+    /* switch directions */
+    case -2:
+      cxt->direction = -cxt->direction;
+      if (cxt->direction < 0)
+       cxt->sflags |= SF_REVERSE;
+      else
+       cxt->sflags &= ~SF_REVERSE;
+      break;
+
+    /* delete character from search string. */
+    case -3:   /* C-H, DEL */
+      /* 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.  But this will
+        do until we have a real isearch-undo. */
+      if (cxt->search_string_index == 0)
+       rl_ding ();
+      else
+       cxt->search_string[--cxt->search_string_index] = '\0';
+      break;
+
+    case -4:   /* C-G, abort */
+      rl_replace_line (cxt->lines[cxt->save_line], 0);
+      rl_point = cxt->save_point;
+      rl_mark = cxt->save_mark;
+      rl_restore_prompt();
+      rl_clear_message ();
+
+      return -1;
+
+    case -5:   /* C-W */
+      /* skip over portion of line we already matched and yank word */
+      wstart = rl_point + cxt->search_string_index;
+      if (wstart >= rl_end)
        {
        {
-       case -1:
-         if (search_string_index == 0)
-           continue;
-         else if (reverse)
-           --line_index;
-         else if (line_index != sline_len)
-           ++line_index;
-         else
-           ding ();
+         rl_ding ();
          break;
          break;
+       }
 
 
-         /* switch directions */
-       case -2:
-         direction = -direction;
-         reverse = direction < 0;
+      /* if not in a word, move to one. */
+      cval = _rl_char_value (rl_line_buffer, wstart);
+      if (_rl_walphabetic (cval) == 0)
+       {
+         rl_ding ();
          break;
          break;
-
-       case CTRL ('G'):
-         strcpy (rl_line_buffer, lines[orig_line]);
-         rl_point = orig_point;
-         rl_end = strlen (rl_line_buffer);
-         rl_restore_prompt();
-         rl_clear_message ();
-         if (allocated_line)
-           free (allocated_line);
-         free (lines);
-         return 0;
-
-#if 0
-       /* delete character from search string. */
-       case -3:
-         if (search_string_index == 0)
-           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. */
-           }
+       }
+      n = MB_NEXTCHAR (rl_line_buffer, wstart, 1, MB_FIND_NONZERO);;
+      while (n < rl_end)
+       {
+         cval = _rl_char_value (rl_line_buffer, n);
+         if (_rl_walphabetic (cval) == 0)
+           break;
+         n = MB_NEXTCHAR (rl_line_buffer, n, 1, MB_FIND_NONZERO);;
+       }
+      wlen = n - wstart + 1;
+      if (cxt->search_string_index + wlen + 1 >= cxt->search_string_size)
+       {
+         cxt->search_string_size += wlen + 1;
+         cxt->search_string = (char *)xrealloc (cxt->search_string, cxt->search_string_size);
+       }
+      for (; wstart < n; wstart++)
+       cxt->search_string[cxt->search_string_index++] = rl_line_buffer[wstart];
+      cxt->search_string[cxt->search_string_index] = '\0';
+      break;
+
+    case -6:   /* C-Y */
+      /* skip over portion of line we already matched and yank rest */
+      wstart = rl_point + cxt->search_string_index;
+      if (wstart >= rl_end)
+       {
+         rl_ding ();
          break;
          break;
+       }
+      n = rl_end - wstart + 1;
+      if (cxt->search_string_index + n + 1 >= cxt->search_string_size)
+       {
+         cxt->search_string_size += n + 1;
+         cxt->search_string = (char *)xrealloc (cxt->search_string, cxt->search_string_size);
+       }
+      for (n = wstart; n < rl_end; n++)
+       cxt->search_string[cxt->search_string_index++] = rl_line_buffer[n];
+      cxt->search_string[cxt->search_string_index] = '\0';
+      break;
+
+    /* Add character to search string and continue search. */
+    default:
+      if (cxt->search_string_index + 2 >= cxt->search_string_size)
+       {
+         cxt->search_string_size += 128;
+         cxt->search_string = (char *)xrealloc (cxt->search_string, cxt->search_string_size);
+       }
+#if defined (HANDLE_MULTIBYTE)
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       {
+         int j, l;
+         for (j = 0, l = strlen (cxt->mb); j < l; )
+           cxt->search_string[cxt->search_string_index++] = cxt->mb[j++];
+       }
+      else
 #endif
 #endif
+       cxt->search_string[cxt->search_string_index++] = c;
+      cxt->search_string[cxt->search_string_index] = '\0';
+      break;
+    }
+
+  for (cxt->sflags &= ~(SF_FOUND|SF_FAILED);; )
+    {
+      limit = cxt->sline_len - cxt->search_string_index + 1;
 
 
-       default:
-         /* Add character to search string and continue search. */
-         if (search_string_index + 2 >= search_string_size)
+      /* Search the current line. */
+      while ((cxt->sflags & SF_REVERSE) ? (cxt->sline_index >= 0) : (cxt->sline_index < limit))
+       {
+         if (STREQN (cxt->search_string, cxt->sline + cxt->sline_index, cxt->search_string_index))
            {
            {
-             search_string_size += 128;
-             search_string = xrealloc (search_string, search_string_size);
+             cxt->sflags |= SF_FOUND;
+             break;
            }
            }
-         search_string[search_string_index++] = c;
-         search_string[search_string_index] = '\0';
-         break;
+         else
+           cxt->sline_index += cxt->direction;
        }
        }
+      if (cxt->sflags & SF_FOUND)
+       break;
 
 
-      for (found = failed = 0;;)
+      /* Move to the next line, but skip new copies of the line
+        we just found and lines shorter than the string we're
+        searching for. */
+      do
        {
        {
-         int limit = sline_len - search_string_index + 1;
+         /* Move to the next line. */
+         cxt->history_pos += cxt->direction;
 
 
-         /* Search the current line. */
-         while (reverse ? (line_index >= 0) : (line_index < limit))
+         /* At limit for direction? */
+         if ((cxt->sflags & SF_REVERSE) ? (cxt->history_pos < 0) : (cxt->history_pos == cxt->hlen))
            {
            {
-             if (STREQN (search_string, sline + line_index, search_string_index))
-               {
-                 found++;
-                 break;
-               }
-             else
-               line_index += direction;
+             cxt->sflags |= SF_FAILED;
+             break;
            }
            }
-         if (found)
-           break;
 
 
-         /* Move to the next line, but skip new copies of the line
-            we just found and lines shorter than the string we're
-            searching for. */
-         do
-           {
-             /* Move to the next line. */
-             i += direction;
-
-             /* At limit for direction? */
-             if (reverse ? (i < 0) : (i == hlen))
-               {
-                 failed++;
-                 break;
-               }
-
-             /* We will need these later. */
-             sline = lines[i];
-             sline_len = strlen (sline);
-           }
-         while ((prev_line_found && STREQ (prev_line_found, lines[i])) ||
-                (search_string_index > sline_len));
+         /* We will need these later. */
+         cxt->sline = cxt->lines[cxt->history_pos];
+         cxt->sline_len = strlen (cxt->sline);
+       }
+      while ((cxt->prev_line_found && STREQ (cxt->prev_line_found, cxt->lines[cxt->history_pos])) ||
+            (cxt->search_string_index > cxt->sline_len));
 
 
-         if (failed)
-           break;
+      if (cxt->sflags & SF_FAILED)
+       break;
 
 
-         /* Now set up the line for searching... */
-         line_index = reverse ? sline_len - search_string_index : 0;
-       }
+      /* Now set up the line for searching... */
+      cxt->sline_index = (cxt->sflags & SF_REVERSE) ? cxt->sline_len - cxt->search_string_index : 0;
+    }
 
 
-      if (failed)
-       {
-         /* We cannot find the search string.  Ding the bell. */
-         ding ();
-         i = last_found_line;
-         continue;             /* XXX - was break */
-       }
+  if (cxt->sflags & SF_FAILED)
+    {
+      /* We cannot find the search string.  Ding the bell. */
+      rl_ding ();
+      cxt->history_pos = cxt->last_found_line;
+      return 1;
+    }
 
 
-      /* We have found the search string.  Just display it.  But don't
-        actually move there in the history list until the user accepts
-        the location. */
-      if (found)
-       {
-         int line_len;
+  /* We have found the search string.  Just display it.  But don't
+     actually move there in the history list until the user accepts
+     the location. */
+  if (cxt->sflags & SF_FOUND)
+    {
+      cxt->prev_line_found = cxt->lines[cxt->history_pos];
+      rl_replace_line (cxt->lines[cxt->history_pos], 0);
+      rl_point = cxt->sline_index;
+      cxt->last_found_line = cxt->history_pos;
+      rl_display_search (cxt->search_string, (cxt->sflags & SF_REVERSE), (cxt->history_pos == cxt->save_line) ? -1 : cxt->history_pos);
+    }
 
 
-         prev_line_found = lines[i];
-         line_len = strlen (lines[i]);
+  return 1;
+}
 
 
-         if (line_len >= rl_line_buffer_len)
-           rl_extend_line_buffer (line_len);
+static int
+_rl_isearch_cleanup (cxt, r)
+     _rl_search_cxt *cxt;
+     int r;
+{
+  if (r >= 0)
+    _rl_isearch_fini (cxt);
+  _rl_scxt_dispose (cxt, 0);
+  _rl_iscxt = 0;
 
 
-         strcpy (rl_line_buffer, lines[i]);
-         rl_point = line_index;
-         rl_end = line_len;
-         last_found_line = i;
-         rl_display_search (search_string, reverse, (i == orig_line) ? -1 : i);
-       }
+  RL_UNSETSTATE(RL_STATE_ISEARCH);
+
+  return (r != 0);
+}
+
+/* Search through the history looking for an interactively typed string.
+   This is analogous to i-search.  We start the search in the current line.
+   DIRECTION is which direction to search; >= 0 means forward, < 0 means
+   backwards. */
+static int
+rl_search_history (direction, invoking_key)
+     int direction, invoking_key;
+{
+  _rl_search_cxt *cxt;         /* local for now, but saved globally */
+  int c, r;
+
+  RL_SETSTATE(RL_STATE_ISEARCH);
+  cxt = _rl_isearch_init (direction);
+
+  rl_display_search (cxt->search_string, (cxt->sflags & SF_REVERSE), -1);
+
+  /* If we are using the callback interface, all we do is set up here and
+      return.  The key is that we leave RL_STATE_ISEARCH set. */
+  if (RL_ISSTATE (RL_STATE_CALLBACK))
+    return (0);
+
+  r = -1;
+  for (;;)
+    {
+      c = _rl_search_getchar (cxt);
+      /* We might want to handle EOF here (c == 0) */
+      r = _rl_isearch_dispatch (cxt, cxt->lastc);
+      if (r <= 0)
+        break;
     }
 
   /* The searching is over.  The user may have found the string that she
      was looking for, or else she may have exited a failing search.  If
      LINE_INDEX is -1, then that shows that the string searched for was
      not found.  We use this to determine where to place rl_point. */
     }
 
   /* The searching is over.  The user may have found the string that she
      was looking for, or else she may have exited a failing search.  If
      LINE_INDEX is -1, then that shows that the string searched for was
      not found.  We use this to determine where to place rl_point. */
+  return (_rl_isearch_cleanup (cxt, r));
+}
 
 
-  /* First put back the original state. */
-  strcpy (rl_line_buffer, lines[orig_line]);
-
-  rl_restore_prompt ();
-
-  /* Free the search string. */
-  free (search_string);
-
-  if (last_found_line < orig_line)
-    rl_get_previous_history (orig_line - last_found_line, 0);
-  else
-    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)
-    line_index = strlen (rl_line_buffer);
-  rl_point = line_index;
-  rl_clear_message ();
+#if defined (READLINE_CALLBACKS)
+/* Called from the callback functions when we are ready to read a key.  The
+   callback functions know to call this because RL_ISSTATE(RL_STATE_ISEARCH).
+   If _rl_isearch_dispatch finishes searching, this function is responsible
+   for turning off RL_STATE_ISEARCH, which it does using _rl_isearch_cleanup. */
+int
+_rl_isearch_callback (cxt)
+     _rl_search_cxt *cxt;
+{
+  int c, r;
 
 
-  if (allocated_line)
-    free (allocated_line);
-  free (lines);
+  c = _rl_search_getchar (cxt);
+  /* We might want to handle EOF here */
+  r = _rl_isearch_dispatch (cxt, cxt->lastc);
 
 
-  return 0;
+  return (r <= 0) ? _rl_isearch_cleanup (cxt, r) : 0;
 }
 }
+#endif
This page took 0.031735 seconds and 4 git commands to generate.