i-bash/lib/readline/search.c

632 lines
16 KiB
C
Raw Normal View History

1996-08-26 18:22:31 +00:00
/* search.c - code for non-incremental searching in emacs and vi modes. */
/* Copyright (C) 1992-2013 Free Software Foundation, Inc.
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
This file is part of the GNU Readline Library (Readline), a library
for reading lines of text with interactive input and history editing.
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
Readline is free software: you can redistribute it and/or modify
1996-08-26 18:22:31 +00:00
it under the terms of the GNU General Public License as published by
2009-01-12 13:36:28 +00:00
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Readline is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Readline. If not, see <http://www.gnu.org/licenses/>.
*/
1996-08-26 18:22:31 +00:00
#define READLINE_LIBRARY
1996-12-23 17:02:34 +00:00
#if defined (HAVE_CONFIG_H)
# include <config.h>
#endif
1996-08-26 18:22:31 +00:00
#include <sys/types.h>
#include <stdio.h>
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
1997-06-05 14:59:13 +00:00
#if defined (HAVE_STDLIB_H)
# include <stdlib.h>
#else
# include "ansi_stdlib.h"
#endif
1996-08-26 18:22:31 +00:00
#include "rldefs.h"
2002-07-17 14:10:11 +00:00
#include "rlmbutil.h"
1996-08-26 18:22:31 +00:00
#include "readline.h"
#include "history.h"
#include "histlib.h"
1996-08-26 18:22:31 +00:00
2000-03-17 21:46:59 +00:00
#include "rlprivate.h"
#include "xmalloc.h"
1997-06-05 14:59:13 +00:00
#ifdef abs
# undef abs
#endif
#define abs(x) (((x) >= 0) ? (x) : -(x))
1996-08-26 18:22:31 +00:00
2005-12-07 14:08:12 +00:00
_rl_search_cxt *_rl_nscxt = 0;
2001-04-06 19:14:31 +00:00
extern HIST_ENTRY *_rl_saved_line_for_history;
1996-08-26 18:22:31 +00:00
/* Functions imported from the rest of the library. */
2001-11-13 17:56:06 +00:00
extern int _rl_free_history_entry PARAMS((HIST_ENTRY *));
1996-08-26 18:22:31 +00:00
static char *noninc_search_string = (char *) NULL;
1996-12-23 17:02:34 +00:00
static int noninc_history_pos;
2000-03-17 21:46:59 +00:00
1996-08-26 18:22:31 +00:00
static char *prev_line_found = (char *) NULL;
2000-03-17 21:46:59 +00:00
static int rl_history_search_len;
static int rl_history_search_pos;
static int rl_history_search_flags;
2000-03-17 21:46:59 +00:00
static char *history_search_string;
static int history_string_size;
2001-11-13 17:56:06 +00:00
static void make_history_line_current PARAMS((HIST_ENTRY *));
static int noninc_search_from_pos PARAMS((char *, int, int));
2005-12-07 14:08:12 +00:00
static int noninc_dosearch PARAMS((char *, int));
static int noninc_search PARAMS((int, int));
2001-11-13 17:56:06 +00:00
static int rl_history_search_internal PARAMS((int, int));
static void rl_history_search_reinit PARAMS((int));
2001-11-13 17:56:06 +00:00
2005-12-07 14:08:12 +00:00
static _rl_search_cxt *_rl_nsearch_init PARAMS((int, int));
static int _rl_nsearch_cleanup PARAMS((_rl_search_cxt *, int));
static void _rl_nsearch_abort PARAMS((_rl_search_cxt *));
static int _rl_nsearch_dispatch PARAMS((_rl_search_cxt *, int));
2000-03-17 21:46:59 +00:00
/* Make the data from the history entry ENTRY be the contents of the
current line. This doesn't do anything with rl_point; the caller
must set it. */
static void
make_history_line_current (entry)
HIST_ENTRY *entry;
{
2004-07-27 13:29:18 +00:00
_rl_replace_text (entry->line, 0, rl_end);
_rl_fix_point (1);
2005-12-07 14:08:12 +00:00
#if defined (VI_MODE)
if (rl_editing_mode == vi_mode)
/* POSIX.2 says that the `U' command doesn't affect the copy of any
command lines to the edit line. We're going to implement that by
making the undo list start after the matching line is copied to the
current editing buffer. */
rl_free_undo_list ();
2004-07-27 13:29:18 +00:00
#endif
2000-03-17 21:46:59 +00:00
2001-04-06 19:14:31 +00:00
if (_rl_saved_line_for_history)
_rl_free_history_entry (_rl_saved_line_for_history);
_rl_saved_line_for_history = (HIST_ENTRY *)NULL;
2000-03-17 21:46:59 +00:00
}
1996-08-26 18:22:31 +00:00
/* Search the history list for STRING starting at absolute history position
POS. If STRING begins with `^', the search must match STRING at the
beginning of a history line, otherwise a full substring match is performed
for STRING. DIR < 0 means to search backwards through the history list,
DIR >= 0 means to search forward. */
static int
noninc_search_from_pos (string, pos, dir)
char *string;
int pos, dir;
{
int ret, old;
2001-04-06 19:14:31 +00:00
if (pos < 0)
return -1;
1996-08-26 18:22:31 +00:00
old = where_history ();
2001-04-06 19:14:31 +00:00
if (history_set_pos (pos) == 0)
return -1;
1996-08-26 18:22:31 +00:00
2001-04-06 19:14:31 +00:00
RL_SETSTATE(RL_STATE_SEARCH);
1996-08-26 18:22:31 +00:00
if (*string == '^')
ret = history_search_prefix (string + 1, dir);
else
ret = history_search (string, dir);
2001-04-06 19:14:31 +00:00
RL_UNSETSTATE(RL_STATE_SEARCH);
1996-08-26 18:22:31 +00:00
if (ret != -1)
ret = where_history ();
history_set_pos (old);
return (ret);
}
/* Search for a line in the history containing STRING. If DIR is < 0, the
search is backwards through previous entries, else through subsequent
2005-12-07 14:08:12 +00:00
entries. Returns 1 if the search was successful, 0 otherwise. */
static int
1996-08-26 18:22:31 +00:00
noninc_dosearch (string, dir)
char *string;
int dir;
{
2000-03-17 21:46:59 +00:00
int oldpos, pos;
1996-08-26 18:22:31 +00:00
HIST_ENTRY *entry;
if (string == 0 || *string == '\0' || noninc_history_pos < 0)
{
2001-04-06 19:14:31 +00:00
rl_ding ();
2005-12-07 14:08:12 +00:00
return 0;
1996-08-26 18:22:31 +00:00
}
pos = noninc_search_from_pos (string, noninc_history_pos + dir, dir);
if (pos == -1)
{
/* Search failed, current history position unchanged. */
2001-04-06 19:14:31 +00:00
rl_maybe_unsave_line ();
1996-08-26 18:22:31 +00:00
rl_clear_message ();
rl_point = 0;
2001-04-06 19:14:31 +00:00
rl_ding ();
2005-12-07 14:08:12 +00:00
return 0;
1996-08-26 18:22:31 +00:00
}
noninc_history_pos = pos;
oldpos = where_history ();
history_set_pos (noninc_history_pos);
entry = current_history (); /* will never be NULL after successful search */
1996-08-26 18:22:31 +00:00
#if defined (VI_MODE)
if (rl_editing_mode != vi_mode)
#endif
2005-12-07 14:08:12 +00:00
history_set_pos (oldpos);
1996-08-26 18:22:31 +00:00
2000-03-17 21:46:59 +00:00
make_history_line_current (entry);
1996-08-26 18:22:31 +00:00
rl_point = 0;
2002-07-17 14:10:11 +00:00
rl_mark = rl_end;
1996-08-26 18:22:31 +00:00
rl_clear_message ();
2005-12-07 14:08:12 +00:00
return 1;
1996-08-26 18:22:31 +00:00
}
2005-12-07 14:08:12 +00:00
static _rl_search_cxt *
_rl_nsearch_init (dir, pchar)
int dir, pchar;
1996-08-26 18:22:31 +00:00
{
2005-12-07 14:08:12 +00:00
_rl_search_cxt *cxt;
1996-08-26 18:22:31 +00:00
char *p;
2005-12-07 14:08:12 +00:00
cxt = _rl_scxt_alloc (RL_SEARCH_NSEARCH, 0);
if (dir < 0)
cxt->sflags |= SF_REVERSE; /* not strictly needed */
cxt->direction = dir;
cxt->history_pos = cxt->save_line;
1996-08-26 18:22:31 +00:00
2001-04-06 19:14:31 +00:00
rl_maybe_save_line ();
1996-08-26 18:22:31 +00:00
2004-07-27 13:29:18 +00:00
/* Clear the undo list, since reading the search string should create its
own undo list, and the whole list will end up being freed when we
finish reading the search string. */
rl_undo_list = 0;
1996-08-26 18:22:31 +00:00
/* Use the line buffer to read the search string. */
rl_line_buffer[0] = 0;
rl_end = rl_point = 0;
1996-12-23 17:02:34 +00:00
p = _rl_make_prompt_for_search (pchar ? pchar : ':');
rl_message ("%s", p);
xfree (p);
1996-08-26 18:22:31 +00:00
2001-04-06 19:14:31 +00:00
RL_SETSTATE(RL_STATE_NSEARCH);
2005-12-07 14:08:12 +00:00
_rl_nscxt = cxt;
return cxt;
}
static int
_rl_nsearch_cleanup (cxt, r)
_rl_search_cxt *cxt;
int r;
{
_rl_scxt_dispose (cxt, 0);
_rl_nscxt = 0;
RL_UNSETSTATE(RL_STATE_NSEARCH);
return (r != 1);
}
static void
_rl_nsearch_abort (cxt)
_rl_search_cxt *cxt;
{
rl_maybe_unsave_line ();
rl_clear_message ();
rl_point = cxt->save_point;
rl_mark = cxt->save_mark;
rl_restore_prompt ();
RL_UNSETSTATE (RL_STATE_NSEARCH);
}
/* Process just-read character C according to search context CXT. Return -1
if the caller should abort the search, 0 if we should break out of the
loop, and 1 if we should continue to read characters. */
static int
_rl_nsearch_dispatch (cxt, c)
_rl_search_cxt *cxt;
int c;
{
switch (c)
1996-08-26 18:22:31 +00:00
{
2005-12-07 14:08:12 +00:00
case CTRL('W'):
rl_unix_word_rubout (1, c);
break;
2001-04-06 19:14:31 +00:00
2005-12-07 14:08:12 +00:00
case CTRL('U'):
rl_unix_line_discard (1, c);
break;
2002-07-17 14:10:11 +00:00
2005-12-07 14:08:12 +00:00
case RETURN:
case NEWLINE:
return 0;
2001-04-06 19:14:31 +00:00
2005-12-07 14:08:12 +00:00
case CTRL('H'):
case RUBOUT:
if (rl_point == 0)
1996-08-26 18:22:31 +00:00
{
2005-12-07 14:08:12 +00:00
_rl_nsearch_abort (cxt);
return -1;
}
_rl_rubout_char (1, c);
break;
1996-08-26 18:22:31 +00:00
2005-12-07 14:08:12 +00:00
case CTRL('C'):
case CTRL('G'):
rl_ding ();
_rl_nsearch_abort (cxt);
return -1;
default:
2002-07-17 14:10:11 +00:00
#if defined (HANDLE_MULTIBYTE)
2005-12-07 14:08:12 +00:00
if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
rl_insert_text (cxt->mb);
else
2002-07-17 14:10:11 +00:00
#endif
2005-12-07 14:08:12 +00:00
_rl_insert_char (1, c);
break;
1996-08-26 18:22:31 +00:00
}
2005-12-07 14:08:12 +00:00
(*rl_redisplay_function) ();
return 1;
}
/* Perform one search according to CXT, using NONINC_SEARCH_STRING. Return
-1 if the search should be aborted, any other value means to clean up
using _rl_nsearch_cleanup (). Returns 1 if the search was successful,
0 otherwise. */
static int
_rl_nsearch_dosearch (cxt)
_rl_search_cxt *cxt;
{
rl_mark = cxt->save_mark;
2002-07-17 14:10:11 +00:00
1996-08-26 18:22:31 +00:00
/* If rl_point == 0, we want to re-use the previous search string and
start from the saved history position. If there's no previous search
string, punt. */
if (rl_point == 0)
{
2005-12-07 14:08:12 +00:00
if (noninc_search_string == 0)
1996-08-26 18:22:31 +00:00
{
2001-04-06 19:14:31 +00:00
rl_ding ();
2005-12-07 14:08:12 +00:00
rl_restore_prompt ();
RL_UNSETSTATE (RL_STATE_NSEARCH);
return -1;
1996-08-26 18:22:31 +00:00
}
}
else
{
/* We want to start the search from the current history position. */
2005-12-07 14:08:12 +00:00
noninc_history_pos = cxt->save_line;
2000-03-17 21:46:59 +00:00
FREE (noninc_search_string);
1996-08-26 18:22:31 +00:00
noninc_search_string = savestring (rl_line_buffer);
2005-12-07 14:08:12 +00:00
/* If we don't want the subsequent undo list generated by the search
matching a history line to include the contents of the search string,
we need to clear rl_line_buffer here. For now, we just clear the
undo list generated by reading the search string. (If the search
fails, the old undo list will be restored by rl_maybe_unsave_line.) */
rl_free_undo_list ();
1996-08-26 18:22:31 +00:00
}
1999-02-19 17:11:39 +00:00
rl_restore_prompt ();
2005-12-07 14:08:12 +00:00
return (noninc_dosearch (noninc_search_string, cxt->direction));
}
/* Search non-interactively through the history list. DIR < 0 means to
search backwards through the history of previous commands; otherwise
the search is for commands subsequent to the current position in the
history list. PCHAR is the character to use for prompting when reading
the search string; if not specified (0), it defaults to `:'. */
static int
noninc_search (dir, pchar)
int dir;
int pchar;
{
_rl_search_cxt *cxt;
int c, r;
cxt = _rl_nsearch_init (dir, pchar);
if (RL_ISSTATE (RL_STATE_CALLBACK))
return (0);
/* Read the search string. */
r = 0;
while (1)
{
c = _rl_search_getchar (cxt);
if (c == 0)
break;
r = _rl_nsearch_dispatch (cxt, c);
if (r < 0)
return 1;
else if (r == 0)
break;
}
r = _rl_nsearch_dosearch (cxt);
return ((r >= 0) ? _rl_nsearch_cleanup (cxt, r) : (r != 1));
1996-08-26 18:22:31 +00:00
}
/* Search forward through the history list for a string. If the vi-mode
code calls this, KEY will be `?'. */
1996-12-23 17:02:34 +00:00
int
1996-08-26 18:22:31 +00:00
rl_noninc_forward_search (count, key)
int count, key;
{
2005-12-07 14:08:12 +00:00
return noninc_search (1, (key == '?') ? '?' : 0);
1996-08-26 18:22:31 +00:00
}
/* Reverse search the history list for a string. If the vi-mode code
calls this, KEY will be `/'. */
1996-12-23 17:02:34 +00:00
int
1996-08-26 18:22:31 +00:00
rl_noninc_reverse_search (count, key)
int count, key;
{
2005-12-07 14:08:12 +00:00
return noninc_search (-1, (key == '/') ? '/' : 0);
1996-08-26 18:22:31 +00:00
}
/* Search forward through the history list for the last string searched
for. If there is no saved search string, abort. */
1996-12-23 17:02:34 +00:00
int
1996-08-26 18:22:31 +00:00
rl_noninc_forward_search_again (count, key)
int count, key;
{
2005-12-07 14:08:12 +00:00
int r;
1996-08-26 18:22:31 +00:00
if (!noninc_search_string)
{
2001-04-06 19:14:31 +00:00
rl_ding ();
1996-08-26 18:22:31 +00:00
return (-1);
}
2005-12-07 14:08:12 +00:00
r = noninc_dosearch (noninc_search_string, 1);
return (r != 1);
1996-08-26 18:22:31 +00:00
}
/* Reverse search in the history list for the last string searched
for. If there is no saved search string, abort. */
1996-12-23 17:02:34 +00:00
int
1996-08-26 18:22:31 +00:00
rl_noninc_reverse_search_again (count, key)
int count, key;
{
2005-12-07 14:08:12 +00:00
int r;
1996-08-26 18:22:31 +00:00
if (!noninc_search_string)
{
2001-04-06 19:14:31 +00:00
rl_ding ();
1996-08-26 18:22:31 +00:00
return (-1);
}
2005-12-07 14:08:12 +00:00
r = noninc_dosearch (noninc_search_string, -1);
return (r != 1);
1996-08-26 18:22:31 +00:00
}
2005-12-07 14:08:12 +00:00
#if defined (READLINE_CALLBACKS)
int
_rl_nsearch_callback (cxt)
_rl_search_cxt *cxt;
{
int c, r;
c = _rl_search_getchar (cxt);
r = _rl_nsearch_dispatch (cxt, c);
if (r != 0)
return 1;
r = _rl_nsearch_dosearch (cxt);
return ((r >= 0) ? _rl_nsearch_cleanup (cxt, r) : (r != 1));
}
#endif
1996-08-26 18:22:31 +00:00
static int
2000-03-17 21:46:59 +00:00
rl_history_search_internal (count, dir)
int count, dir;
1996-08-26 18:22:31 +00:00
{
2000-03-17 21:46:59 +00:00
HIST_ENTRY *temp;
int ret, oldpos;
char *t;
1996-08-26 18:22:31 +00:00
2001-04-06 19:14:31 +00:00
rl_maybe_save_line ();
2000-03-17 21:46:59 +00:00
temp = (HIST_ENTRY *)NULL;
1996-08-26 18:22:31 +00:00
/* Search COUNT times through the history for a line matching
history_search_string. If history_search_string[0] == '^', the
line must match from the start; otherwise any substring can match.
When this loop finishes, TEMP, if non-null, is the history line to
copy into the line buffer. */
1996-08-26 18:22:31 +00:00
while (count)
{
RL_CHECK_SIGNALS ();
2000-03-17 21:46:59 +00:00
ret = noninc_search_from_pos (history_search_string, rl_history_search_pos + dir, dir);
if (ret == -1)
break;
/* Get the history entry we found. */
rl_history_search_pos = ret;
oldpos = where_history ();
history_set_pos (rl_history_search_pos);
temp = current_history (); /* will never be NULL after successful search */
2000-03-17 21:46:59 +00:00
history_set_pos (oldpos);
/* Don't find multiple instances of the same line. */
if (prev_line_found && STREQ (prev_line_found, temp->line))
continue;
prev_line_found = temp->line;
count--;
1996-08-26 18:22:31 +00:00
}
2000-03-17 21:46:59 +00:00
/* If we didn't find anything at all, return. */
1996-12-23 17:02:34 +00:00
if (temp == 0)
1996-08-26 18:22:31 +00:00
{
2001-04-06 19:14:31 +00:00
rl_maybe_unsave_line ();
rl_ding ();
2000-03-17 21:46:59 +00:00
/* If you don't want the saved history line (last match) to show up
in the line buffer after the search fails, change the #if 0 to
#if 1 */
#if 0
if (rl_point > rl_history_search_len)
{
rl_point = rl_end = rl_history_search_len;
rl_line_buffer[rl_end] = '\0';
2002-07-17 14:10:11 +00:00
rl_mark = 0;
2000-03-17 21:46:59 +00:00
}
#else
2001-04-06 19:14:31 +00:00
rl_point = rl_history_search_len; /* rl_maybe_unsave_line changes it */
2002-07-17 14:10:11 +00:00
rl_mark = rl_end;
2000-03-17 21:46:59 +00:00
#endif
return 1;
1996-08-26 18:22:31 +00:00
}
2000-03-17 21:46:59 +00:00
/* Copy the line we found into the current line buffer. */
make_history_line_current (temp);
if (rl_history_search_flags & ANCHORED_SEARCH)
rl_point = rl_history_search_len; /* easy case */
else
{
t = strstr (rl_line_buffer, history_search_string);
rl_point = t ? (int)(t - rl_line_buffer) + rl_history_search_len : rl_end;
}
2002-07-17 14:10:11 +00:00
rl_mark = rl_end;
1996-08-26 18:22:31 +00:00
return 0;
}
2000-03-17 21:46:59 +00:00
static void
rl_history_search_reinit (flags)
int flags;
2000-03-17 21:46:59 +00:00
{
int sind;
2000-03-17 21:46:59 +00:00
rl_history_search_pos = where_history ();
rl_history_search_len = rl_point;
rl_history_search_flags = flags;
2000-03-17 21:46:59 +00:00
prev_line_found = (char *)NULL;
if (rl_point)
{
/* Allocate enough space for anchored and non-anchored searches */
2000-03-17 21:46:59 +00:00
if (rl_history_search_len >= history_string_size - 2)
{
history_string_size = rl_history_search_len + 2;
2001-11-13 17:56:06 +00:00
history_search_string = (char *)xrealloc (history_search_string, history_string_size);
2000-03-17 21:46:59 +00:00
}
sind = 0;
if (flags & ANCHORED_SEARCH)
history_search_string[sind++] = '^';
strncpy (history_search_string + sind, rl_line_buffer, rl_point);
history_search_string[rl_point + sind] = '\0';
2000-03-17 21:46:59 +00:00
}
2001-04-06 19:14:31 +00:00
_rl_free_saved_history_line ();
2000-03-17 21:46:59 +00:00
}
1996-08-26 18:22:31 +00:00
/* Search forward in the history for the string of characters
from the start of the line to rl_point. This is a non-incremental
search. The search is anchored to the beginning of the history line. */
1996-08-26 18:22:31 +00:00
int
rl_history_search_forward (count, ignore)
int count, ignore;
{
if (count == 0)
return (0);
2000-03-17 21:46:59 +00:00
if (rl_last_func != rl_history_search_forward &&
rl_last_func != rl_history_search_backward)
rl_history_search_reinit (ANCHORED_SEARCH);
2000-03-17 21:46:59 +00:00
if (rl_history_search_len == 0)
return (rl_get_next_history (count, ignore));
1996-08-26 18:22:31 +00:00
return (rl_history_search_internal (abs (count), (count > 0) ? 1 : -1));
}
/* Search backward through the history for the string of characters
from the start of the line to rl_point. This is a non-incremental
search. */
int
rl_history_search_backward (count, ignore)
int count, ignore;
{
if (count == 0)
return (0);
2000-03-17 21:46:59 +00:00
if (rl_last_func != rl_history_search_forward &&
rl_last_func != rl_history_search_backward)
rl_history_search_reinit (ANCHORED_SEARCH);
if (rl_history_search_len == 0)
return (rl_get_previous_history (count, ignore));
return (rl_history_search_internal (abs (count), (count > 0) ? -1 : 1));
}
/* Search forward in the history for the string of characters
from the start of the line to rl_point. This is a non-incremental
search. The search succeeds if the search string is present anywhere
in the history line. */
int
rl_history_substr_search_forward (count, ignore)
int count, ignore;
{
if (count == 0)
return (0);
if (rl_last_func != rl_history_substr_search_forward &&
rl_last_func != rl_history_substr_search_backward)
rl_history_search_reinit (NON_ANCHORED_SEARCH);
if (rl_history_search_len == 0)
return (rl_get_next_history (count, ignore));
return (rl_history_search_internal (abs (count), (count > 0) ? 1 : -1));
}
/* Search backward through the history for the string of characters
from the start of the line to rl_point. This is a non-incremental
search. */
int
rl_history_substr_search_backward (count, ignore)
int count, ignore;
{
if (count == 0)
return (0);
if (rl_last_func != rl_history_substr_search_forward &&
rl_last_func != rl_history_substr_search_backward)
rl_history_search_reinit (NON_ANCHORED_SEARCH);
2000-03-17 21:46:59 +00:00
if (rl_history_search_len == 0)
return (rl_get_previous_history (count, ignore));
1996-08-26 18:22:31 +00:00
return (rl_history_search_internal (abs (count), (count > 0) ? -1 : 1));
}