1997-01-04 22:09:20 +00:00
|
|
|
/* search.c - code for non-incremental searching in emacs and vi modes. */
|
|
|
|
|
|
|
|
/* Copyright (C) 1992 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
This file is part of the Readline Library (the Library), a set of
|
|
|
|
routines for providing Emacs style line input to programs that ask
|
|
|
|
for it.
|
|
|
|
|
|
|
|
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
|
2000-03-19 21:18:09 +00:00
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
1997-01-04 22:09:20 +00:00
|
|
|
any later version.
|
|
|
|
|
|
|
|
The Library 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.
|
|
|
|
|
|
|
|
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,
|
2000-03-19 21:18:09 +00:00
|
|
|
59 Temple Place, Suite 330, Boston, MA 02111 USA. */
|
1997-01-04 22:09:20 +00:00
|
|
|
#define READLINE_LIBRARY
|
|
|
|
|
|
|
|
#if defined (HAVE_CONFIG_H)
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#if defined (HAVE_UNISTD_H)
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
1997-06-07 12:17:44 +00:00
|
|
|
#if defined (HAVE_STDLIB_H)
|
|
|
|
# include <stdlib.h>
|
|
|
|
#else
|
|
|
|
# include "ansi_stdlib.h"
|
|
|
|
#endif
|
|
|
|
|
1997-01-04 22:09:20 +00:00
|
|
|
#include "rldefs.h"
|
|
|
|
#include "readline.h"
|
|
|
|
#include "history.h"
|
|
|
|
|
2000-03-19 21:18:09 +00:00
|
|
|
#include "rlprivate.h"
|
|
|
|
#include "xmalloc.h"
|
|
|
|
|
1997-06-07 12:17:44 +00:00
|
|
|
#ifdef abs
|
|
|
|
# undef abs
|
|
|
|
#endif
|
|
|
|
#define abs(x) (((x) >= 0) ? (x) : -(x))
|
1997-01-04 22:09:20 +00:00
|
|
|
|
2001-04-11 02:33:07 +00:00
|
|
|
extern HIST_ENTRY *_rl_saved_line_for_history;
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
|
|
/* Functions imported from the rest of the library. */
|
2000-03-19 21:18:09 +00:00
|
|
|
extern int _rl_free_history_entry __P((HIST_ENTRY *));
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
|
|
static char *noninc_search_string = (char *) NULL;
|
|
|
|
static int noninc_history_pos;
|
2000-03-19 21:18:09 +00:00
|
|
|
|
1997-01-04 22:09:20 +00:00
|
|
|
static char *prev_line_found = (char *) NULL;
|
|
|
|
|
2000-03-19 21:18:09 +00:00
|
|
|
static int rl_history_search_len;
|
|
|
|
static int rl_history_search_pos;
|
|
|
|
static char *history_search_string;
|
|
|
|
static int history_string_size;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
{
|
|
|
|
int line_len;
|
|
|
|
|
|
|
|
line_len = strlen (entry->line);
|
|
|
|
if (line_len >= rl_line_buffer_len)
|
|
|
|
rl_extend_line_buffer (line_len);
|
|
|
|
strcpy (rl_line_buffer, entry->line);
|
|
|
|
|
|
|
|
rl_undo_list = (UNDO_LIST *)entry->data;
|
|
|
|
rl_end = line_len;
|
|
|
|
|
2001-04-11 02:33:07 +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-19 21:18:09 +00:00
|
|
|
}
|
|
|
|
|
1997-01-04 22:09:20 +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-11 02:33:07 +00:00
|
|
|
if (pos < 0)
|
|
|
|
return -1;
|
|
|
|
|
1997-01-04 22:09:20 +00:00
|
|
|
old = where_history ();
|
2001-04-11 02:33:07 +00:00
|
|
|
if (history_set_pos (pos) == 0)
|
|
|
|
return -1;
|
1997-01-04 22:09:20 +00:00
|
|
|
|
2001-04-11 02:33:07 +00:00
|
|
|
RL_SETSTATE(RL_STATE_SEARCH);
|
1997-01-04 22:09:20 +00:00
|
|
|
if (*string == '^')
|
|
|
|
ret = history_search_prefix (string + 1, dir);
|
|
|
|
else
|
|
|
|
ret = history_search (string, dir);
|
2001-04-11 02:33:07 +00:00
|
|
|
RL_UNSETSTATE(RL_STATE_SEARCH);
|
1997-01-04 22:09:20 +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
|
|
|
|
entries. */
|
|
|
|
static void
|
|
|
|
noninc_dosearch (string, dir)
|
|
|
|
char *string;
|
|
|
|
int dir;
|
|
|
|
{
|
2000-03-19 21:18:09 +00:00
|
|
|
int oldpos, pos;
|
1997-01-04 22:09:20 +00:00
|
|
|
HIST_ENTRY *entry;
|
|
|
|
|
|
|
|
if (string == 0 || *string == '\0' || noninc_history_pos < 0)
|
|
|
|
{
|
2001-04-11 02:33:07 +00:00
|
|
|
rl_ding ();
|
1997-01-04 22:09:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = noninc_search_from_pos (string, noninc_history_pos + dir, dir);
|
|
|
|
if (pos == -1)
|
|
|
|
{
|
|
|
|
/* Search failed, current history position unchanged. */
|
2001-04-11 02:33:07 +00:00
|
|
|
rl_maybe_unsave_line ();
|
1997-01-04 22:09:20 +00:00
|
|
|
rl_clear_message ();
|
|
|
|
rl_point = 0;
|
2001-04-11 02:33:07 +00:00
|
|
|
rl_ding ();
|
1997-01-04 22:09:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
noninc_history_pos = pos;
|
|
|
|
|
|
|
|
oldpos = where_history ();
|
|
|
|
history_set_pos (noninc_history_pos);
|
|
|
|
entry = current_history ();
|
|
|
|
#if defined (VI_MODE)
|
|
|
|
if (rl_editing_mode != vi_mode)
|
|
|
|
#endif
|
|
|
|
history_set_pos (oldpos);
|
|
|
|
|
2000-03-19 21:18:09 +00:00
|
|
|
make_history_line_current (entry);
|
1997-01-04 22:09:20 +00:00
|
|
|
|
|
|
|
rl_point = 0;
|
|
|
|
rl_clear_message ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 void
|
|
|
|
noninc_search (dir, pchar)
|
|
|
|
int dir;
|
|
|
|
int pchar;
|
|
|
|
{
|
|
|
|
int saved_point, c;
|
|
|
|
char *p;
|
|
|
|
|
2001-04-11 02:33:07 +00:00
|
|
|
rl_maybe_save_line ();
|
1997-01-04 22:09:20 +00:00
|
|
|
saved_point = rl_point;
|
|
|
|
|
|
|
|
/* Use the line buffer to read the search string. */
|
|
|
|
rl_line_buffer[0] = 0;
|
|
|
|
rl_end = rl_point = 0;
|
|
|
|
|
|
|
|
p = _rl_make_prompt_for_search (pchar ? pchar : ':');
|
|
|
|
rl_message (p, 0, 0);
|
|
|
|
free (p);
|
|
|
|
|
2001-04-11 02:33:07 +00:00
|
|
|
#define SEARCH_RETURN rl_restore_prompt (); RL_UNSETSTATE(RL_STATE_NSEARCH); return
|
1997-01-04 22:09:20 +00:00
|
|
|
|
2001-04-11 02:33:07 +00:00
|
|
|
RL_SETSTATE(RL_STATE_NSEARCH);
|
1997-01-04 22:09:20 +00:00
|
|
|
/* Read the search string. */
|
2001-04-11 02:33:07 +00:00
|
|
|
while (1)
|
1997-01-04 22:09:20 +00:00
|
|
|
{
|
2001-04-11 02:33:07 +00:00
|
|
|
RL_SETSTATE(RL_STATE_MOREINPUT);
|
|
|
|
c = rl_read_key ();
|
|
|
|
RL_UNSETSTATE(RL_STATE_MOREINPUT);
|
|
|
|
|
|
|
|
if (c == 0)
|
|
|
|
break;
|
|
|
|
|
1997-01-04 22:09:20 +00:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case CTRL('H'):
|
|
|
|
case RUBOUT:
|
|
|
|
if (rl_point == 0)
|
|
|
|
{
|
2001-04-11 02:33:07 +00:00
|
|
|
rl_maybe_unsave_line ();
|
1997-01-04 22:09:20 +00:00
|
|
|
rl_clear_message ();
|
|
|
|
rl_point = saved_point;
|
|
|
|
SEARCH_RETURN;
|
|
|
|
}
|
|
|
|
rl_rubout (1, c);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CTRL('W'):
|
|
|
|
rl_unix_word_rubout (1, c);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CTRL('U'):
|
|
|
|
rl_unix_line_discard (1, c);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RETURN:
|
|
|
|
case NEWLINE:
|
|
|
|
goto dosearch;
|
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CTRL('C'):
|
|
|
|
case CTRL('G'):
|
2001-04-11 02:33:07 +00:00
|
|
|
rl_maybe_unsave_line ();
|
1997-01-04 22:09:20 +00:00
|
|
|
rl_clear_message ();
|
|
|
|
rl_point = saved_point;
|
2001-04-11 02:33:07 +00:00
|
|
|
rl_ding ();
|
1997-01-04 22:09:20 +00:00
|
|
|
SEARCH_RETURN;
|
|
|
|
|
|
|
|
default:
|
|
|
|
rl_insert (1, c);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
(*rl_redisplay_function) ();
|
|
|
|
}
|
|
|
|
|
|
|
|
dosearch:
|
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
if (!noninc_search_string)
|
|
|
|
{
|
2001-04-11 02:33:07 +00:00
|
|
|
rl_ding ();
|
1997-01-04 22:09:20 +00:00
|
|
|
SEARCH_RETURN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We want to start the search from the current history position. */
|
|
|
|
noninc_history_pos = where_history ();
|
2000-03-19 21:18:09 +00:00
|
|
|
FREE (noninc_search_string);
|
1997-01-04 22:09:20 +00:00
|
|
|
noninc_search_string = savestring (rl_line_buffer);
|
|
|
|
}
|
|
|
|
|
1999-05-28 01:59:23 +00:00
|
|
|
rl_restore_prompt ();
|
1997-01-04 22:09:20 +00:00
|
|
|
noninc_dosearch (noninc_search_string, dir);
|
2001-04-11 02:33:07 +00:00
|
|
|
RL_UNSETSTATE(RL_STATE_NSEARCH);
|
1997-01-04 22:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Search forward through the history list for a string. If the vi-mode
|
|
|
|
code calls this, KEY will be `?'. */
|
|
|
|
int
|
|
|
|
rl_noninc_forward_search (count, key)
|
|
|
|
int count, key;
|
|
|
|
{
|
|
|
|
noninc_search (1, (key == '?') ? '?' : 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reverse search the history list for a string. If the vi-mode code
|
|
|
|
calls this, KEY will be `/'. */
|
|
|
|
int
|
|
|
|
rl_noninc_reverse_search (count, key)
|
|
|
|
int count, key;
|
|
|
|
{
|
|
|
|
noninc_search (-1, (key == '/') ? '/' : 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Search forward through the history list for the last string searched
|
|
|
|
for. If there is no saved search string, abort. */
|
|
|
|
int
|
|
|
|
rl_noninc_forward_search_again (count, key)
|
|
|
|
int count, key;
|
|
|
|
{
|
|
|
|
if (!noninc_search_string)
|
|
|
|
{
|
2001-04-11 02:33:07 +00:00
|
|
|
rl_ding ();
|
1997-01-04 22:09:20 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
noninc_dosearch (noninc_search_string, 1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reverse search in the history list for the last string searched
|
|
|
|
for. If there is no saved search string, abort. */
|
|
|
|
int
|
|
|
|
rl_noninc_reverse_search_again (count, key)
|
|
|
|
int count, key;
|
|
|
|
{
|
|
|
|
if (!noninc_search_string)
|
|
|
|
{
|
2001-04-11 02:33:07 +00:00
|
|
|
rl_ding ();
|
1997-01-04 22:09:20 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
noninc_dosearch (noninc_search_string, -1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-03-19 21:18:09 +00:00
|
|
|
rl_history_search_internal (count, dir)
|
|
|
|
int count, dir;
|
1997-01-04 22:09:20 +00:00
|
|
|
{
|
2000-03-19 21:18:09 +00:00
|
|
|
HIST_ENTRY *temp;
|
|
|
|
int ret, oldpos;
|
1997-01-04 22:09:20 +00:00
|
|
|
|
2001-04-11 02:33:07 +00:00
|
|
|
rl_maybe_save_line ();
|
2000-03-19 21:18:09 +00:00
|
|
|
temp = (HIST_ENTRY *)NULL;
|
1997-01-04 22:09:20 +00:00
|
|
|
|
2000-03-19 21:18:09 +00:00
|
|
|
/* Search COUNT times through the history for a line whose prefix
|
|
|
|
matches history_search_string. When this loop finishes, TEMP,
|
|
|
|
if non-null, is the history line to copy into the line buffer. */
|
1997-01-04 22:09:20 +00:00
|
|
|
while (count)
|
|
|
|
{
|
2000-03-19 21:18:09 +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 ();
|
|
|
|
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--;
|
1997-01-04 22:09:20 +00:00
|
|
|
}
|
|
|
|
|
2000-03-19 21:18:09 +00:00
|
|
|
/* If we didn't find anything at all, return. */
|
1997-01-04 22:09:20 +00:00
|
|
|
if (temp == 0)
|
|
|
|
{
|
2001-04-11 02:33:07 +00:00
|
|
|
rl_maybe_unsave_line ();
|
|
|
|
rl_ding ();
|
2000-03-19 21:18:09 +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';
|
|
|
|
}
|
|
|
|
#else
|
2001-04-11 02:33:07 +00:00
|
|
|
rl_point = rl_history_search_len; /* rl_maybe_unsave_line changes it */
|
2000-03-19 21:18:09 +00:00
|
|
|
#endif
|
|
|
|
return 1;
|
1997-01-04 22:09:20 +00:00
|
|
|
}
|
|
|
|
|
2000-03-19 21:18:09 +00:00
|
|
|
/* Copy the line we found into the current line buffer. */
|
|
|
|
make_history_line_current (temp);
|
|
|
|
|
|
|
|
rl_point = rl_history_search_len;
|
1997-01-04 22:09:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-03-19 21:18:09 +00:00
|
|
|
static void
|
|
|
|
rl_history_search_reinit ()
|
|
|
|
{
|
|
|
|
rl_history_search_pos = where_history ();
|
|
|
|
rl_history_search_len = rl_point;
|
|
|
|
prev_line_found = (char *)NULL;
|
|
|
|
if (rl_point)
|
|
|
|
{
|
|
|
|
if (rl_history_search_len >= history_string_size - 2)
|
|
|
|
{
|
|
|
|
history_string_size = rl_history_search_len + 2;
|
|
|
|
history_search_string = xrealloc (history_search_string, history_string_size);
|
|
|
|
}
|
|
|
|
history_search_string[0] = '^';
|
|
|
|
strncpy (history_search_string + 1, rl_line_buffer, rl_point);
|
|
|
|
history_search_string[rl_point + 1] = '\0';
|
|
|
|
}
|
2001-04-11 02:33:07 +00:00
|
|
|
_rl_free_saved_history_line ();
|
2000-03-19 21:18:09 +00:00
|
|
|
}
|
|
|
|
|
1997-01-04 22:09:20 +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. */
|
|
|
|
int
|
|
|
|
rl_history_search_forward (count, ignore)
|
|
|
|
int count, ignore;
|
|
|
|
{
|
|
|
|
if (count == 0)
|
|
|
|
return (0);
|
2000-03-19 21:18:09 +00:00
|
|
|
|
|
|
|
if (rl_last_func != rl_history_search_forward &&
|
|
|
|
rl_last_func != rl_history_search_backward)
|
|
|
|
rl_history_search_reinit ();
|
|
|
|
|
|
|
|
if (rl_history_search_len == 0)
|
|
|
|
return (rl_get_next_history (count, ignore));
|
1997-01-04 22:09:20 +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-19 21:18:09 +00:00
|
|
|
|
|
|
|
if (rl_last_func != rl_history_search_forward &&
|
|
|
|
rl_last_func != rl_history_search_backward)
|
|
|
|
rl_history_search_reinit ();
|
|
|
|
|
|
|
|
if (rl_history_search_len == 0)
|
|
|
|
return (rl_get_previous_history (count, ignore));
|
1997-01-04 22:09:20 +00:00
|
|
|
return (rl_history_search_internal (abs (count), (count > 0) ? -1 : 1));
|
|
|
|
}
|