1 /* **************************************************************** */
3 /* I-Search and Searching */
5 /* **************************************************************** */
7 /* Copyright (C) 1987,1989 Free Software Foundation, Inc.
9 This file contains the Readline Library (the Library), a set of
10 routines for providing Emacs style line input to programs that ask
13 The Library is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2, or (at your option)
18 The Library is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 The GNU General Public License is often shipped with GNU software, and
24 is generally kept in a file called COPYING or LICENSE. If you do not
25 have a copy of the license, write to the Free Software Foundation,
26 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
27 #define READLINE_LIBRARY
29 #if defined (HAVE_CONFIG_H)
33 #include <sys/types.h>
37 #if defined (HAVE_UNISTD_H)
41 #if defined (HAVE_STDLIB_H)
44 # include "ansi_stdlib.h"
51 #include "rlprivate.h"
54 /* Variables exported to other files in the readline library. */
55 char *_rl_isearch_terminators = (char *)NULL;
57 /* Variables imported from other files in the readline library. */
58 extern HIST_ENTRY *_rl_saved_line_for_history;
60 /* Forward declarations */
61 static int rl_search_history PARAMS((int, int));
63 /* Last line found by the current incremental search, so we don't `find'
64 identical lines many times in a row. */
65 static char *prev_line_found;
67 /* Last search string and its length. */
68 static char *last_isearch_string;
69 static int last_isearch_string_len;
71 static char *default_isearch_terminators = "\033\012";
73 /* Search backwards through the history looking for a string which is typed
74 interactively. Start with the current line. */
76 rl_reverse_search_history (sign, key)
79 return (rl_search_history (-sign, key));
82 /* Search forwards through the history looking for a string which is typed
83 interactively. Start with the current line. */
85 rl_forward_search_history (sign, key)
88 return (rl_search_history (sign, key));
91 /* Display the current state of the search in the echo-area.
92 SEARCH_STRING contains the string that is being searched for,
93 DIRECTION is zero for forward, or 1 for reverse,
94 WHERE is the history list number of the current line. If it is
95 -1, then this line is the starting one. */
97 rl_display_search (search_string, reverse_p, where)
102 int msglen, searchlen;
104 searchlen = (search_string && *search_string) ? strlen (search_string) : 0;
106 message = (char *)xmalloc (searchlen + 33);
112 sprintf (message, "[%d]", where + history_base);
113 msglen = strlen (message);
117 message[msglen++] = '(';
121 strcpy (message + msglen, "reverse-");
125 strcpy (message + msglen, "i-search)`");
130 strcpy (message + msglen, search_string);
134 strcpy (message + msglen, "': ");
136 rl_message ("%s", message);
138 (*rl_redisplay_function) ();
141 /* Search through the history looking for an interactively typed string.
142 This is analogous to i-search. We start the search in the current line.
143 DIRECTION is which direction to search; >= 0 means forward, < 0 means
146 rl_search_history (direction, invoking_key)
147 int direction, invoking_key;
149 /* The string that the user types in to search for. */
152 /* The current length of SEARCH_STRING. */
153 int search_string_index;
155 /* The amount of space that SEARCH_STRING has allocated to it. */
156 int search_string_size;
158 /* The list of lines to search through. */
159 char **lines, *allocated_line;
161 /* The length of LINES. */
164 /* Where we get LINES from. */
168 int orig_point, orig_line, last_found_line;
169 int c, found, failed, sline_len;
171 /* The line currently being searched. */
174 /* Offset in that line. */
177 /* Non-zero if we are doing a reverse search. */
180 /* The list of characters which terminate the search, but are not
181 subsequently executed. If the variable isearch-terminators has
182 been set, we use that value, otherwise we use ESC and C-J. */
183 char *isearch_terminators;
185 RL_SETSTATE(RL_STATE_ISEARCH);
186 orig_point = rl_point;
187 last_found_line = orig_line = where_history ();
188 reverse = direction < 0;
189 hlist = history_list ();
190 allocated_line = (char *)NULL;
192 isearch_terminators = _rl_isearch_terminators ? _rl_isearch_terminators
193 : default_isearch_terminators;
195 /* Create an arrary of pointers to the lines that we want to search. */
196 rl_maybe_replace_line ();
199 for (i = 0; hlist[i]; i++);
201 /* Allocate space for this many lines, +1 for the current input line,
202 and remember those lines. */
203 lines = (char **)xmalloc ((1 + (hlen = i)) * sizeof (char *));
204 for (i = 0; i < hlen; i++)
205 lines[i] = hlist[i]->line;
207 if (_rl_saved_line_for_history)
208 lines[i] = _rl_saved_line_for_history->line;
211 /* Keep track of this so we can free it. */
212 allocated_line = (char *)xmalloc (1 + strlen (rl_line_buffer));
213 strcpy (allocated_line, &rl_line_buffer[0]);
214 lines[i] = allocated_line;
219 /* The line where we start the search. */
224 /* Initialize search parameters. */
225 search_string = (char *)xmalloc (search_string_size = 128);
226 *search_string = '\0';
227 search_string_index = 0;
228 prev_line_found = (char *)0; /* XXX */
230 /* Normalize DIRECTION into 1 or -1. */
231 direction = (direction >= 0) ? 1 : -1;
233 rl_display_search (search_string, reverse, -1);
235 sline = rl_line_buffer;
236 sline_len = strlen (sline);
237 line_index = rl_point;
242 rl_command_func_t *f = (rl_command_func_t *)NULL;
244 /* Read a key and decide how to proceed. */
245 RL_SETSTATE(RL_STATE_MOREINPUT);
247 RL_UNSETSTATE(RL_STATE_MOREINPUT);
249 if (c >= 0 && _rl_keymap[c].type == ISFUNC)
251 f = _rl_keymap[c].function;
253 if (f == rl_reverse_search_history)
254 c = reverse ? -1 : -2;
255 else if (f == rl_forward_search_history)
256 c = !reverse ? -1 : -2;
260 /* Let NEWLINE (^J) terminate the search for people who don't like
261 using ESC. ^M can still be used to terminate the search and
262 immediately execute the command. */
263 if (c == ESC || c == NEWLINE)
265 /* The characters in isearch_terminators (set from the user-settable
266 variable isearch-terminators) are used to terminate the search but
267 not subsequently execute the character as a command. The default
268 value is "\033\012" (ESC and C-J). */
269 if (strchr (isearch_terminators, c))
272 /* ESC still terminates the search, but if there is pending
273 input or if input arrives within 0.1 seconds (on systems
274 with select(2)) it is used as a prefix character
275 with rl_execute_next. WATCH OUT FOR THIS! This is intended
276 to allow the arrow keys to be used like ^F and ^B are used
277 to terminate the search and execute the movement command. */
278 if (c == ESC && _rl_input_available ()) /* XXX */
279 rl_execute_next (ESC);
283 if (c >= 0 && (CTRL_CHAR (c) || META_CHAR (c) || c == RUBOUT) && c != CTRL ('G'))
285 /* This sets rl_pending_input to c; it will be picked up the next
286 time rl_read_key is called. */
294 if (search_string_index == 0)
296 if (last_isearch_string)
298 search_string_size = 64 + last_isearch_string_len;
299 search_string = (char *)xrealloc (search_string, search_string_size);
300 strcpy (search_string, last_isearch_string);
301 search_string_index = last_isearch_string_len;
302 rl_display_search (search_string, reverse, -1);
309 else if (line_index != sline_len)
315 /* switch directions */
317 direction = -direction;
318 reverse = direction < 0;
322 strcpy (rl_line_buffer, lines[orig_line]);
323 rl_point = orig_point;
324 rl_end = strlen (rl_line_buffer);
328 free (allocated_line);
330 RL_UNSETSTATE(RL_STATE_ISEARCH);
334 /* delete character from search string. */
336 if (search_string_index == 0)
340 search_string[--search_string_index] = '\0';
341 /* This is tricky. To do this right, we need to keep a
342 stack of search positions for the current search, with
343 sentinels marking the beginning and end. */
349 /* Add character to search string and continue search. */
350 if (search_string_index + 2 >= search_string_size)
352 search_string_size += 128;
353 search_string = (char *)xrealloc (search_string, search_string_size);
355 search_string[search_string_index++] = c;
356 search_string[search_string_index] = '\0';
360 for (found = failed = 0;;)
362 int limit = sline_len - search_string_index + 1;
364 /* Search the current line. */
365 while (reverse ? (line_index >= 0) : (line_index < limit))
367 if (STREQN (search_string, sline + line_index, search_string_index))
373 line_index += direction;
378 /* Move to the next line, but skip new copies of the line
379 we just found and lines shorter than the string we're
383 /* Move to the next line. */
386 /* At limit for direction? */
387 if (reverse ? (i < 0) : (i == hlen))
393 /* We will need these later. */
395 sline_len = strlen (sline);
397 while ((prev_line_found && STREQ (prev_line_found, lines[i])) ||
398 (search_string_index > sline_len));
403 /* Now set up the line for searching... */
404 line_index = reverse ? sline_len - search_string_index : 0;
409 /* We cannot find the search string. Ding the bell. */
412 continue; /* XXX - was break */
415 /* We have found the search string. Just display it. But don't
416 actually move there in the history list until the user accepts
422 prev_line_found = lines[i];
423 line_len = strlen (lines[i]);
425 if (line_len >= rl_line_buffer_len)
426 rl_extend_line_buffer (line_len);
428 strcpy (rl_line_buffer, lines[i]);
429 rl_point = line_index;
432 rl_display_search (search_string, reverse, (i == orig_line) ? -1 : i);
436 /* The searching is over. The user may have found the string that she
437 was looking for, or else she may have exited a failing search. If
438 LINE_INDEX is -1, then that shows that the string searched for was
439 not found. We use this to determine where to place rl_point. */
441 /* First put back the original state. */
442 strcpy (rl_line_buffer, lines[orig_line]);
444 rl_restore_prompt ();
447 /* Save the search string for possible later use. */
448 FREE (last_isearch_string);
449 last_isearch_string = search_string;
450 last_isearch_string_len = search_string_index;
452 /* Free the search string. */
453 free (search_string);
456 if (last_found_line < orig_line)
457 rl_get_previous_history (orig_line - last_found_line, 0);
459 rl_get_next_history (last_found_line - orig_line, 0);
461 /* If the string was not found, put point at the end of the line. */
463 line_index = strlen (rl_line_buffer);
464 rl_point = line_index;
467 FREE (allocated_line);
470 RL_UNSETSTATE(RL_STATE_ISEARCH);