1 /* **************************************************************** */
3 /* I-Search and Searching */
5 /* **************************************************************** */
7 /* Copyright (C) 1987-2002 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"
53 #include "rlprivate.h"
56 /* Variables exported to other files in the readline library. */
57 char *_rl_isearch_terminators = (char *)NULL;
59 /* Variables imported from other files in the readline library. */
60 extern HIST_ENTRY *_rl_saved_line_for_history;
62 /* Forward declarations */
63 static int rl_search_history PARAMS((int, int));
65 /* Last line found by the current incremental search, so we don't `find'
66 identical lines many times in a row. */
67 static char *prev_line_found;
69 /* Last search string and its length. */
70 static char *last_isearch_string;
71 static int last_isearch_string_len;
73 static char *default_isearch_terminators = "\033\012";
75 /* Search backwards through the history looking for a string which is typed
76 interactively. Start with the current line. */
78 rl_reverse_search_history (sign, key)
81 return (rl_search_history (-sign, key));
84 /* Search forwards through the history looking for a string which is typed
85 interactively. Start with the current line. */
87 rl_forward_search_history (sign, key)
90 return (rl_search_history (sign, key));
93 /* Display the current state of the search in the echo-area.
94 SEARCH_STRING contains the string that is being searched for,
95 DIRECTION is zero for forward, or 1 for reverse,
96 WHERE is the history list number of the current line. If it is
97 -1, then this line is the starting one. */
99 rl_display_search (search_string, reverse_p, where)
101 int reverse_p, where;
104 int msglen, searchlen;
106 searchlen = (search_string && *search_string) ? strlen (search_string) : 0;
108 message = (char *)xmalloc (searchlen + 33);
114 sprintf (message, "[%d]", where + history_base);
115 msglen = strlen (message);
119 message[msglen++] = '(';
123 strcpy (message + msglen, "reverse-");
127 strcpy (message + msglen, "i-search)`");
132 strcpy (message + msglen, search_string);
136 strcpy (message + msglen, "': ");
138 rl_message ("%s", message);
140 (*rl_redisplay_function) ();
143 /* Search through the history looking for an interactively typed string.
144 This is analogous to i-search. We start the search in the current line.
145 DIRECTION is which direction to search; >= 0 means forward, < 0 means
148 rl_search_history (direction, invoking_key)
149 int direction, invoking_key;
151 /* The string that the user types in to search for. */
154 /* The current length of SEARCH_STRING. */
155 int search_string_index;
157 /* The amount of space that SEARCH_STRING has allocated to it. */
158 int search_string_size;
160 /* The list of lines to search through. */
161 char **lines, *allocated_line;
163 /* The length of LINES. */
166 /* Where we get LINES from. */
170 int orig_point, orig_mark, orig_line, last_found_line;
171 int c, found, failed, sline_len;
173 #if defined (HANDLE_MULTIBYTE)
177 /* The line currently being searched. */
180 /* Offset in that line. */
183 /* Non-zero if we are doing a reverse search. */
186 /* The list of characters which terminate the search, but are not
187 subsequently executed. If the variable isearch-terminators has
188 been set, we use that value, otherwise we use ESC and C-J. */
189 char *isearch_terminators;
191 RL_SETSTATE(RL_STATE_ISEARCH);
192 orig_point = rl_point;
194 last_found_line = orig_line = where_history ();
195 reverse = direction < 0;
196 hlist = history_list ();
197 allocated_line = (char *)NULL;
199 isearch_terminators = _rl_isearch_terminators ? _rl_isearch_terminators
200 : default_isearch_terminators;
202 /* Create an arrary of pointers to the lines that we want to search. */
203 rl_maybe_replace_line ();
206 for (i = 0; hlist[i]; i++);
208 /* Allocate space for this many lines, +1 for the current input line,
209 and remember those lines. */
210 lines = (char **)xmalloc ((1 + (hlen = i)) * sizeof (char *));
211 for (i = 0; i < hlen; i++)
212 lines[i] = hlist[i]->line;
214 if (_rl_saved_line_for_history)
215 lines[i] = _rl_saved_line_for_history->line;
218 /* Keep track of this so we can free it. */
219 allocated_line = (char *)xmalloc (1 + strlen (rl_line_buffer));
220 strcpy (allocated_line, &rl_line_buffer[0]);
221 lines[i] = allocated_line;
226 /* The line where we start the search. */
231 /* Initialize search parameters. */
232 search_string = (char *)xmalloc (search_string_size = 128);
233 *search_string = '\0';
234 search_string_index = 0;
235 prev_line_found = (char *)0; /* XXX */
237 /* Normalize DIRECTION into 1 or -1. */
238 direction = (direction >= 0) ? 1 : -1;
240 rl_display_search (search_string, reverse, -1);
242 sline = rl_line_buffer;
243 sline_len = strlen (sline);
244 line_index = rl_point;
249 rl_command_func_t *f = (rl_command_func_t *)NULL;
251 /* Read a key and decide how to proceed. */
252 RL_SETSTATE(RL_STATE_MOREINPUT);
254 RL_UNSETSTATE(RL_STATE_MOREINPUT);
256 #if defined (HANDLE_MULTIBYTE)
257 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
258 c = _rl_read_mbstring (c, mb, MB_LEN_MAX);
261 /* Translate the keys we do something with to opcodes. */
262 if (c >= 0 && _rl_keymap[c].type == ISFUNC)
264 f = _rl_keymap[c].function;
266 if (f == rl_reverse_search_history)
267 c = reverse ? -1 : -2;
268 else if (f == rl_forward_search_history)
269 c = !reverse ? -1 : -2;
270 else if (f == rl_rubout)
272 else if (c == CTRL ('G'))
274 else if (c == CTRL ('W')) /* XXX */
276 else if (c == CTRL ('Y')) /* XXX */
280 /* The characters in isearch_terminators (set from the user-settable
281 variable isearch-terminators) are used to terminate the search but
282 not subsequently execute the character as a command. The default
283 value is "\033\012" (ESC and C-J). */
284 if (strchr (isearch_terminators, c))
286 /* ESC still terminates the search, but if there is pending
287 input or if input arrives within 0.1 seconds (on systems
288 with select(2)) it is used as a prefix character
289 with rl_execute_next. WATCH OUT FOR THIS! This is intended
290 to allow the arrow keys to be used like ^F and ^B are used
291 to terminate the search and execute the movement command.
292 XXX - since _rl_input_available depends on the application-
293 settable keyboard timeout value, this could alternatively
294 use _rl_input_queued(100000) */
295 if (c == ESC && _rl_input_available ())
296 rl_execute_next (ESC);
300 #define ENDSRCH_CHAR(c) \
301 ((CTRL_CHAR (c) || META_CHAR (c) || (c) == RUBOUT) && ((c) != CTRL ('G')))
303 #if defined (HANDLE_MULTIBYTE)
304 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
306 if (c >= 0 && strlen (mb) == 1 && ENDSRCH_CHAR (c))
308 /* This sets rl_pending_input to c; it will be picked up the next
309 time rl_read_key is called. */
316 if (c >= 0 && ENDSRCH_CHAR (c))
318 /* This sets rl_pending_input to c; it will be picked up the next
319 time rl_read_key is called. */
327 if (search_string_index == 0)
329 if (last_isearch_string)
331 search_string_size = 64 + last_isearch_string_len;
332 search_string = (char *)xrealloc (search_string, search_string_size);
333 strcpy (search_string, last_isearch_string);
334 search_string_index = last_isearch_string_len;
335 rl_display_search (search_string, reverse, -1);
342 else if (line_index != sline_len)
348 /* switch directions */
350 direction = -direction;
351 reverse = direction < 0;
354 /* delete character from search string. */
355 case -3: /* C-H, DEL */
356 /* This is tricky. To do this right, we need to keep a
357 stack of search positions for the current search, with
358 sentinels marking the beginning and end. But this will
359 do until we have a real isearch-undo. */
360 if (search_string_index == 0)
363 search_string[--search_string_index] = '\0';
368 rl_replace_line (lines[orig_line], 0);
369 rl_point = orig_point;
374 free (allocated_line);
376 RL_UNSETSTATE(RL_STATE_ISEARCH);
380 /* skip over portion of line we already matched */
381 wstart = rl_point + search_string_index;
382 if (wstart >= rl_end)
388 /* if not in a word, move to one. */
389 if (rl_alphabetic(rl_line_buffer[wstart]) == 0)
395 while (n < rl_end && rl_alphabetic(rl_line_buffer[n]))
397 wlen = n - wstart + 1;
398 if (search_string_index + wlen + 1 >= search_string_size)
400 search_string_size += wlen + 1;
401 search_string = (char *)xrealloc (search_string, search_string_size);
403 for (; wstart < n; wstart++)
404 search_string[search_string_index++] = rl_line_buffer[wstart];
405 search_string[search_string_index] = '\0';
409 /* skip over portion of line we already matched */
410 wstart = rl_point + search_string_index;
411 if (wstart >= rl_end)
416 n = rl_end - wstart + 1;
417 if (search_string_index + n + 1 >= search_string_size)
419 search_string_size += n + 1;
420 search_string = (char *)xrealloc (search_string, search_string_size);
422 for (n = wstart; n < rl_end; n++)
423 search_string[search_string_index++] = rl_line_buffer[n];
424 search_string[search_string_index] = '\0';
428 /* Add character to search string and continue search. */
429 if (search_string_index + 2 >= search_string_size)
431 search_string_size += 128;
432 search_string = (char *)xrealloc (search_string, search_string_size);
434 #if defined (HANDLE_MULTIBYTE)
435 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
438 for (j = 0, l = strlen (mb); j < l; )
439 search_string[search_string_index++] = mb[j++];
443 search_string[search_string_index++] = c;
444 search_string[search_string_index] = '\0';
448 for (found = failed = 0;;)
450 int limit = sline_len - search_string_index + 1;
452 /* Search the current line. */
453 while (reverse ? (line_index >= 0) : (line_index < limit))
455 if (STREQN (search_string, sline + line_index, search_string_index))
461 line_index += direction;
466 /* Move to the next line, but skip new copies of the line
467 we just found and lines shorter than the string we're
471 /* Move to the next line. */
474 /* At limit for direction? */
475 if (reverse ? (i < 0) : (i == hlen))
481 /* We will need these later. */
483 sline_len = strlen (sline);
485 while ((prev_line_found && STREQ (prev_line_found, lines[i])) ||
486 (search_string_index > sline_len));
491 /* Now set up the line for searching... */
492 line_index = reverse ? sline_len - search_string_index : 0;
497 /* We cannot find the search string. Ding the bell. */
500 continue; /* XXX - was break */
503 /* We have found the search string. Just display it. But don't
504 actually move there in the history list until the user accepts
508 prev_line_found = lines[i];
509 rl_replace_line (lines[i], 0);
510 rl_point = line_index;
512 rl_display_search (search_string, reverse, (i == orig_line) ? -1 : i);
516 /* The searching is over. The user may have found the string that she
517 was looking for, or else she may have exited a failing search. If
518 LINE_INDEX is -1, then that shows that the string searched for was
519 not found. We use this to determine where to place rl_point. */
521 /* First put back the original state. */
522 strcpy (rl_line_buffer, lines[orig_line]);
524 rl_restore_prompt ();
526 /* Save the search string for possible later use. */
527 FREE (last_isearch_string);
528 last_isearch_string = search_string;
529 last_isearch_string_len = search_string_index;
531 if (last_found_line < orig_line)
532 rl_get_previous_history (orig_line - last_found_line, 0);
534 rl_get_next_history (last_found_line - orig_line, 0);
536 /* If the string was not found, put point at the end of the last matching
537 line. If last_found_line == orig_line, we didn't find any matching
538 history lines at all, so put point back in its original position. */
541 if (last_found_line == orig_line)
542 line_index = orig_point;
544 line_index = strlen (rl_line_buffer);
548 rl_point = line_index;
549 /* Don't worry about where to put the mark here; rl_get_previous_history
550 and rl_get_next_history take care of it. */
554 FREE (allocated_line);
557 RL_UNSETSTATE(RL_STATE_ISEARCH);