1
/* histsearch.c -- searching the history list. */
3
/* Copyright (C) 1989, 1992 Free Software Foundation, Inc.
5
This file contains the GNU History Library (the Library), a set of
6
routines for managing the text of previously typed lines.
8
The Library is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2, or (at your option)
13
The Library is distributed in the hope that it will be useful, but
14
WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
General Public License for more details.
18
The GNU General Public License is often shipped with GNU software, and
19
is generally kept in a file called COPYING or LICENSE. If you do not
20
have a copy of the license, write to the Free Software Foundation,
21
59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23
#define READLINE_LIBRARY
25
#if defined (HAVE_CONFIG_H)
26
# include "config_readline.h"
30
#if defined (HAVE_STDLIB_H)
33
# include "ansi_stdlib.h"
34
#endif /* HAVE_STDLIB_H */
36
#if defined (HAVE_UNISTD_H)
38
# include <sys/types.h>
46
/* The list of alternate characters that can delimit a history search
48
char *history_search_delimiter_chars = (char *)NULL;
50
static int history_search_internal PARAMS((const char *, int, int));
52
/* Search the history for STRING, starting at history_offset.
53
If DIRECTION < 0, then the search is through previous entries, else
54
through subsequent. If ANCHORED is non-zero, the string must
55
appear at the beginning of a history line, otherwise, the string
56
may appear anywhere in the line. If the string is found, then
57
current_history () is the history entry, and the value of this
58
function is the offset in the line of that history entry that the
59
string was found in. Otherwise, nothing is changed, and a -1 is
63
history_search_internal (string, direction, anchored)
65
int direction, anchored;
67
register int i, reverse;
69
register int line_index;
71
HIST_ENTRY **the_history; /* local */
74
reverse = (direction < 0);
76
/* Take care of trivial cases first. */
77
if (string == 0 || *string == '\0')
80
if (!history_length || ((i >= history_length) && !reverse))
83
if (reverse && (i >= history_length))
84
i = history_length - 1;
86
#define NEXT_LINE() do { if (reverse) i--; else i++; } while (0)
88
the_history = history_list ();
89
string_len = strlen (string);
92
/* Search each line in the history list for STRING. */
94
/* At limit for direction? */
95
if ((reverse && i < 0) || (!reverse && i == history_length))
98
line = the_history[i]->line;
99
line_index = strlen (line);
101
/* If STRING is longer than line, no match. */
102
if (string_len > line_index)
108
/* Handle anchored searches first. */
109
if (anchored == ANCHORED_SEARCH)
111
if (STREQN (string, line, string_len))
121
/* Do substring search. */
124
line_index -= string_len;
126
while (line_index >= 0)
128
if (STREQN (string, line + line_index, string_len))
140
limit = line_index - string_len + 1;
143
while (line_index < limit)
145
if (STREQN (string, line + line_index, string_len))
157
/* Do a non-anchored search for STRING through the history in DIRECTION. */
159
history_search (string, direction)
163
return (history_search_internal (string, direction, NON_ANCHORED_SEARCH));
166
/* Do an anchored search for string through the history in DIRECTION. */
168
history_search_prefix (string, direction)
172
return (history_search_internal (string, direction, ANCHORED_SEARCH));
175
/* Search for STRING in the history list. DIR is < 0 for searching
176
backwards. POS is an absolute index into the history list at
177
which point to begin searching. */
179
history_search_pos (string, dir, pos)
185
old = where_history ();
186
history_set_pos (pos);
187
if (history_search (string, dir) == -1)
189
history_set_pos (old);
192
ret = where_history ();
193
history_set_pos (old);