1 /* search.c - code for non-incremental searching in emacs and vi modes. */
3 /* Copyright (C) 1992 Free Software Foundation, Inc.
5 This file is part of the Readline Library (the Library), a set of
6 routines for providing Emacs style line input to programs that ask
9 The Library is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 1, or (at your option)
14 The Library is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 The GNU General Public License is often shipped with GNU software, and
20 is generally kept in a file called COPYING or LICENSE. If you do not
21 have a copy of the license, write to the Free Software Foundation,
22 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #define READLINE_LIBRARY
25 #if defined (HAVE_CONFIG_H)
29 #include <sys/types.h>
32 #if defined (HAVE_UNISTD_H)
40 #define abs(x) (((x) > 0) ? (x) : -(x))
42 extern char *xmalloc (), *xrealloc ();
44 /* Variables imported from readline.c */
45 extern int rl_point, rl_end, rl_line_buffer_len;
46 extern int rl_editing_mode;
47 extern char *rl_prompt;
48 extern char *rl_line_buffer;
49 extern HIST_ENTRY *saved_line_for_history;
50 extern Function *rl_last_func;
52 /* Functions imported from the rest of the library. */
53 extern int _rl_free_history_entry ();
54 extern char *_rl_make_prompt_for_search ();
55 extern void _rl_restore_prompt ();
56 extern void rl_extend_line_buffer ();
58 static char *noninc_search_string = (char *) NULL;
59 static int noninc_history_pos;
60 static char *prev_line_found = (char *) NULL;
62 /* Search the history list for STRING starting at absolute history position
63 POS. If STRING begins with `^', the search must match STRING at the
64 beginning of a history line, otherwise a full substring match is performed
65 for STRING. DIR < 0 means to search backwards through the history list,
66 DIR >= 0 means to search forward. */
68 noninc_search_from_pos (string, pos, dir)
74 old = where_history ();
75 history_set_pos (pos);
78 ret = history_search_prefix (string + 1, dir);
80 ret = history_search (string, dir);
83 ret = where_history ();
85 history_set_pos (old);
89 /* Search for a line in the history containing STRING. If DIR is < 0, the
90 search is backwards through previous entries, else through subsequent
93 noninc_dosearch (string, dir)
97 int oldpos, pos, line_len;
100 if (string == 0 || *string == '\0' || noninc_history_pos < 0)
106 pos = noninc_search_from_pos (string, noninc_history_pos + dir, dir);
109 /* Search failed, current history position unchanged. */
110 maybe_unsave_line ();
117 noninc_history_pos = pos;
119 oldpos = where_history ();
120 history_set_pos (noninc_history_pos);
121 entry = current_history ();
122 #if defined (VI_MODE)
123 if (rl_editing_mode != vi_mode)
125 history_set_pos (oldpos);
127 line_len = strlen (entry->line);
128 if (line_len >= rl_line_buffer_len)
129 rl_extend_line_buffer (line_len);
130 strcpy (rl_line_buffer, entry->line);
132 rl_undo_list = (UNDO_LIST *)entry->data;
133 rl_end = strlen (rl_line_buffer);
137 if (saved_line_for_history)
138 _rl_free_history_entry (saved_line_for_history);
139 saved_line_for_history = (HIST_ENTRY *)NULL;
142 /* Search non-interactively through the history list. DIR < 0 means to
143 search backwards through the history of previous commands; otherwise
144 the search is for commands subsequent to the current position in the
145 history list. PCHAR is the character to use for prompting when reading
146 the search string; if not specified (0), it defaults to `:'. */
148 noninc_search (dir, pchar)
156 saved_point = rl_point;
158 /* Use the line buffer to read the search string. */
159 rl_line_buffer[0] = 0;
160 rl_end = rl_point = 0;
162 p = _rl_make_prompt_for_search (pchar ? pchar : ':');
163 rl_message (p, 0, 0);
166 #define SEARCH_RETURN _rl_restore_prompt (); return
168 /* Read the search string. */
169 while (c = rl_read_key ())
177 maybe_unsave_line ();
179 rl_point = saved_point;
186 rl_unix_word_rubout (1, c);
190 rl_unix_line_discard (1, c);
201 maybe_unsave_line ();
203 rl_point = saved_point;
211 (*rl_redisplay_function) ();
215 /* If rl_point == 0, we want to re-use the previous search string and
216 start from the saved history position. If there's no previous search
220 if (!noninc_search_string)
228 /* We want to start the search from the current history position. */
229 noninc_history_pos = where_history ();
230 if (noninc_search_string)
231 free (noninc_search_string);
232 noninc_search_string = savestring (rl_line_buffer);
235 _rl_restore_prompt ();
236 noninc_dosearch (noninc_search_string, dir);
239 /* Search forward through the history list for a string. If the vi-mode
240 code calls this, KEY will be `?'. */
242 rl_noninc_forward_search (count, key)
245 noninc_search (1, (key == '?') ? '?' : 0);
249 /* Reverse search the history list for a string. If the vi-mode code
250 calls this, KEY will be `/'. */
252 rl_noninc_reverse_search (count, key)
255 noninc_search (-1, (key == '/') ? '/' : 0);
259 /* Search forward through the history list for the last string searched
260 for. If there is no saved search string, abort. */
262 rl_noninc_forward_search_again (count, key)
265 if (!noninc_search_string)
270 noninc_dosearch (noninc_search_string, 1);
274 /* Reverse search in the history list for the last string searched
275 for. If there is no saved search string, abort. */
277 rl_noninc_reverse_search_again (count, key)
280 if (!noninc_search_string)
285 noninc_dosearch (noninc_search_string, -1);
290 rl_history_search_internal (count, direction)
291 int count, direction;
293 HIST_ENTRY *temp, *old_temp;
298 temp = old_temp = (HIST_ENTRY *)NULL;
301 temp = (direction < 0) ? previous_history () : next_history ();
304 /* On an empty prefix, make this the same as previous-history. */
310 if (STREQN (rl_line_buffer, temp->line, rl_point))
312 /* Don't find multiple instances of the same line. */
313 if (prev_line_found && STREQ (prev_line_found, temp->line))
317 prev_line_found = temp->line;
324 if (direction < 0 && old_temp)
328 maybe_unsave_line ();
334 line_len = strlen (temp->line);
335 if (line_len >= rl_line_buffer_len)
336 rl_extend_line_buffer (line_len);
337 strcpy (rl_line_buffer, temp->line);
338 rl_undo_list = (UNDO_LIST *)temp->data;
343 /* Search forward in the history for the string of characters
344 from the start of the line to rl_point. This is a non-incremental
347 rl_history_search_forward (count, ignore)
352 if (rl_last_func != rl_history_search_forward)
353 prev_line_found = (char *)NULL;
354 return (rl_history_search_internal (abs (count), (count > 0) ? 1 : -1));
357 /* Search backward through the history for the string of characters
358 from the start of the line to rl_point. This is a non-incremental
361 rl_history_search_backward (count, ignore)
366 if (rl_last_func != rl_history_search_backward)
367 prev_line_found = (char *)NULL;
368 return (rl_history_search_internal (abs (count), (count > 0) ? -1 : 1));