1 /* **************************************************************** */
3 /* I-Search and Searching */
5 /* **************************************************************** */
7 /* Copyright (C) 1987-2005 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 _rl_search_cxt *_rl_iscxt = 0;
61 /* Variables imported from other files in the readline library. */
62 extern HIST_ENTRY *_rl_saved_line_for_history;
64 static int rl_search_history PARAMS((int, int));
66 static _rl_search_cxt *_rl_isearch_init PARAMS((int));
67 static void _rl_isearch_fini PARAMS((_rl_search_cxt *));
68 static int _rl_isearch_cleanup PARAMS((_rl_search_cxt *, int));
70 /* Last line found by the current incremental search, so we don't `find'
71 identical lines many times in a row. */
72 static char *prev_line_found;
74 /* Last search string and its length. */
75 static char *last_isearch_string;
76 static int last_isearch_string_len;
78 static char *default_isearch_terminators = "\033\012";
81 _rl_scxt_alloc (type, flags)
86 cxt = (_rl_search_cxt *)xmalloc (sizeof (_rl_search_cxt));
91 cxt->search_string = 0;
92 cxt->search_string_size = cxt->search_string_index = 0;
95 cxt->allocated_line = 0;
96 cxt->hlen = cxt->hindex = 0;
98 cxt->save_point = rl_point;
99 cxt->save_mark = rl_mark;
100 cxt->save_line = where_history ();
101 cxt->last_found_line = cxt->save_line;
102 cxt->prev_line_found = 0;
104 cxt->save_undo_list = 0;
106 cxt->history_pos = 0;
112 cxt->sline_len = cxt->sline_index = 0;
114 cxt->search_terminators = 0;
120 _rl_scxt_dispose (cxt, flags)
124 FREE (cxt->search_string);
125 FREE (cxt->allocated_line);
131 /* Search backwards through the history looking for a string which is typed
132 interactively. Start with the current line. */
134 rl_reverse_search_history (sign, key)
137 return (rl_search_history (-sign, key));
140 /* Search forwards through the history looking for a string which is typed
141 interactively. Start with the current line. */
143 rl_forward_search_history (sign, key)
146 return (rl_search_history (sign, key));
149 /* Display the current state of the search in the echo-area.
150 SEARCH_STRING contains the string that is being searched for,
151 DIRECTION is zero for forward, or non-zero for reverse,
152 WHERE is the history list number of the current line. If it is
153 -1, then this line is the starting one. */
155 rl_display_search (search_string, reverse_p, where)
157 int reverse_p, where;
160 int msglen, searchlen;
162 searchlen = (search_string && *search_string) ? strlen (search_string) : 0;
164 message = (char *)xmalloc (searchlen + 33);
170 sprintf (message, "[%d]", where + history_base);
171 msglen = strlen (message);
175 message[msglen++] = '(';
179 strcpy (message + msglen, "reverse-");
183 strcpy (message + msglen, "i-search)`");
188 strcpy (message + msglen, search_string);
192 strcpy (message + msglen, "': ");
194 rl_message ("%s", message);
196 (*rl_redisplay_function) ();
199 static _rl_search_cxt *
200 _rl_isearch_init (direction)
207 cxt = _rl_scxt_alloc (RL_SEARCH_ISEARCH, 0);
209 cxt->sflags |= SF_REVERSE;
211 cxt->search_terminators = _rl_isearch_terminators ? _rl_isearch_terminators
212 : default_isearch_terminators;
214 /* Create an arrary of pointers to the lines that we want to search. */
215 hlist = history_list ();
216 rl_maybe_replace_line ();
219 for (i = 0; hlist[i]; i++);
221 /* Allocate space for this many lines, +1 for the current input line,
222 and remember those lines. */
223 cxt->lines = (char **)xmalloc ((1 + (cxt->hlen = i)) * sizeof (char *));
224 for (i = 0; i < cxt->hlen; i++)
225 cxt->lines[i] = hlist[i]->line;
227 if (_rl_saved_line_for_history)
228 cxt->lines[i] = _rl_saved_line_for_history->line;
231 /* Keep track of this so we can free it. */
232 cxt->allocated_line = (char *)xmalloc (1 + strlen (rl_line_buffer));
233 strcpy (cxt->allocated_line, &rl_line_buffer[0]);
234 cxt->lines[i] = cxt->allocated_line;
239 /* The line where we start the search. */
240 cxt->history_pos = cxt->save_line;
244 /* Initialize search parameters. */
245 cxt->search_string = (char *)xmalloc (cxt->search_string_size = 128);
246 cxt->search_string[cxt->search_string_index = 0] = '\0';
248 /* Normalize DIRECTION into 1 or -1. */
249 cxt->direction = (direction >= 0) ? 1 : -1;
251 cxt->sline = rl_line_buffer;
252 cxt->sline_len = strlen (cxt->sline);
253 cxt->sline_index = rl_point;
255 _rl_iscxt = cxt; /* save globally */
261 _rl_isearch_fini (cxt)
264 /* First put back the original state. */
265 strcpy (rl_line_buffer, cxt->lines[cxt->save_line]);
267 rl_restore_prompt ();
269 /* Save the search string for possible later use. */
270 FREE (last_isearch_string);
271 last_isearch_string = cxt->search_string;
272 last_isearch_string_len = cxt->search_string_index;
273 cxt->search_string = 0;
275 if (cxt->last_found_line < cxt->save_line)
276 rl_get_previous_history (cxt->save_line - cxt->last_found_line, 0);
278 rl_get_next_history (cxt->last_found_line - cxt->save_line, 0);
280 /* If the string was not found, put point at the end of the last matching
281 line. If last_found_line == orig_line, we didn't find any matching
282 history lines at all, so put point back in its original position. */
283 if (cxt->sline_index < 0)
285 if (cxt->last_found_line == cxt->save_line)
286 cxt->sline_index = cxt->save_point;
288 cxt->sline_index = strlen (rl_line_buffer);
289 rl_mark = cxt->save_mark;
292 rl_point = cxt->sline_index;
293 /* Don't worry about where to put the mark here; rl_get_previous_history
294 and rl_get_next_history take care of it. */
300 _rl_search_getchar (cxt)
305 /* Read a key and decide how to proceed. */
306 RL_SETSTATE(RL_STATE_MOREINPUT);
307 c = cxt->lastc = rl_read_key ();
308 RL_UNSETSTATE(RL_STATE_MOREINPUT);
310 #if defined (HANDLE_MULTIBYTE)
311 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
312 c = cxt->lastc = _rl_read_mbstring (cxt->lastc, cxt->mb, MB_LEN_MAX);
318 /* Process just-read character C according to isearch context CXT. Return
319 -1 if the caller should just free the context and return, 0 if we should
320 break out of the loop, and 1 if we should continue to read characters. */
322 _rl_isearch_dispatch (cxt, c)
326 int n, wstart, wlen, limit, cval;
327 rl_command_func_t *f;
329 f = (rl_command_func_t *)NULL;
331 /* Translate the keys we do something with to opcodes. */
332 if (c >= 0 && _rl_keymap[c].type == ISFUNC)
334 f = _rl_keymap[c].function;
336 if (f == rl_reverse_search_history)
337 cxt->lastc = (cxt->sflags & SF_REVERSE) ? -1 : -2;
338 else if (f == rl_forward_search_history)
339 cxt->lastc = (cxt->sflags & SF_REVERSE) ? -2 : -1;
340 else if (f == rl_rubout)
342 else if (c == CTRL ('G'))
344 else if (c == CTRL ('W')) /* XXX */
346 else if (c == CTRL ('Y')) /* XXX */
350 /* The characters in isearch_terminators (set from the user-settable
351 variable isearch-terminators) are used to terminate the search but
352 not subsequently execute the character as a command. The default
353 value is "\033\012" (ESC and C-J). */
354 if (strchr (cxt->search_terminators, cxt->lastc))
356 /* ESC still terminates the search, but if there is pending
357 input or if input arrives within 0.1 seconds (on systems
358 with select(2)) it is used as a prefix character
359 with rl_execute_next. WATCH OUT FOR THIS! This is intended
360 to allow the arrow keys to be used like ^F and ^B are used
361 to terminate the search and execute the movement command.
362 XXX - since _rl_input_available depends on the application-
363 settable keyboard timeout value, this could alternatively
364 use _rl_input_queued(100000) */
365 if (cxt->lastc == ESC && _rl_input_available ())
366 rl_execute_next (ESC);
370 #define ENDSRCH_CHAR(c) \
371 ((CTRL_CHAR (c) || META_CHAR (c) || (c) == RUBOUT) && ((c) != CTRL ('G')))
373 #if defined (HANDLE_MULTIBYTE)
374 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
376 if (cxt->lastc >= 0 && (cxt->mb[0] && cxt->mb[1] == '\0') && ENDSRCH_CHAR (cxt->lastc))
378 /* This sets rl_pending_input to c; it will be picked up the next
379 time rl_read_key is called. */
380 rl_execute_next (cxt->lastc);
386 if (cxt->lastc >= 0 && ENDSRCH_CHAR (cxt->lastc))
388 /* This sets rl_pending_input to LASTC; it will be picked up the next
389 time rl_read_key is called. */
390 rl_execute_next (cxt->lastc);
394 /* Now dispatch on the character. `Opcodes' affect the search string or
395 state. Other characters are added to the string. */
400 if (cxt->search_string_index == 0)
402 if (last_isearch_string)
404 cxt->search_string_size = 64 + last_isearch_string_len;
405 cxt->search_string = (char *)xrealloc (cxt->search_string, cxt->search_string_size);
406 strcpy (cxt->search_string, last_isearch_string);
407 cxt->search_string_index = last_isearch_string_len;
408 rl_display_search (cxt->search_string, (cxt->sflags & SF_REVERSE), -1);
413 else if (cxt->sflags & SF_REVERSE)
415 else if (cxt->sline_index != cxt->sline_len)
421 /* switch directions */
423 cxt->direction = -cxt->direction;
424 if (cxt->direction < 0)
425 cxt->sflags |= SF_REVERSE;
427 cxt->sflags &= ~SF_REVERSE;
430 /* delete character from search string. */
431 case -3: /* C-H, DEL */
432 /* This is tricky. To do this right, we need to keep a
433 stack of search positions for the current search, with
434 sentinels marking the beginning and end. But this will
435 do until we have a real isearch-undo. */
436 if (cxt->search_string_index == 0)
439 cxt->search_string[--cxt->search_string_index] = '\0';
442 case -4: /* C-G, abort */
443 rl_replace_line (cxt->lines[cxt->save_line], 0);
444 rl_point = cxt->save_point;
445 rl_mark = cxt->save_mark;
452 /* skip over portion of line we already matched and yank word */
453 wstart = rl_point + cxt->search_string_index;
454 if (wstart >= rl_end)
460 /* if not in a word, move to one. */
461 cval = _rl_char_value (rl_line_buffer, wstart);
462 if (_rl_walphabetic (cval) == 0)
467 n = MB_NEXTCHAR (rl_line_buffer, wstart, 1, MB_FIND_NONZERO);;
470 cval = _rl_char_value (rl_line_buffer, n);
471 if (_rl_walphabetic (cval) == 0)
473 n = MB_NEXTCHAR (rl_line_buffer, n, 1, MB_FIND_NONZERO);;
475 wlen = n - wstart + 1;
476 if (cxt->search_string_index + wlen + 1 >= cxt->search_string_size)
478 cxt->search_string_size += wlen + 1;
479 cxt->search_string = (char *)xrealloc (cxt->search_string, cxt->search_string_size);
481 for (; wstart < n; wstart++)
482 cxt->search_string[cxt->search_string_index++] = rl_line_buffer[wstart];
483 cxt->search_string[cxt->search_string_index] = '\0';
487 /* skip over portion of line we already matched and yank rest */
488 wstart = rl_point + cxt->search_string_index;
489 if (wstart >= rl_end)
494 n = rl_end - wstart + 1;
495 if (cxt->search_string_index + n + 1 >= cxt->search_string_size)
497 cxt->search_string_size += n + 1;
498 cxt->search_string = (char *)xrealloc (cxt->search_string, cxt->search_string_size);
500 for (n = wstart; n < rl_end; n++)
501 cxt->search_string[cxt->search_string_index++] = rl_line_buffer[n];
502 cxt->search_string[cxt->search_string_index] = '\0';
505 /* Add character to search string and continue search. */
507 if (cxt->search_string_index + 2 >= cxt->search_string_size)
509 cxt->search_string_size += 128;
510 cxt->search_string = (char *)xrealloc (cxt->search_string, cxt->search_string_size);
512 #if defined (HANDLE_MULTIBYTE)
513 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
516 for (j = 0, l = strlen (cxt->mb); j < l; )
517 cxt->search_string[cxt->search_string_index++] = cxt->mb[j++];
521 cxt->search_string[cxt->search_string_index++] = c;
522 cxt->search_string[cxt->search_string_index] = '\0';
526 for (cxt->sflags &= ~(SF_FOUND|SF_FAILED);; )
528 limit = cxt->sline_len - cxt->search_string_index + 1;
530 /* Search the current line. */
531 while ((cxt->sflags & SF_REVERSE) ? (cxt->sline_index >= 0) : (cxt->sline_index < limit))
533 if (STREQN (cxt->search_string, cxt->sline + cxt->sline_index, cxt->search_string_index))
535 cxt->sflags |= SF_FOUND;
539 cxt->sline_index += cxt->direction;
541 if (cxt->sflags & SF_FOUND)
544 /* Move to the next line, but skip new copies of the line
545 we just found and lines shorter than the string we're
549 /* Move to the next line. */
550 cxt->history_pos += cxt->direction;
552 /* At limit for direction? */
553 if ((cxt->sflags & SF_REVERSE) ? (cxt->history_pos < 0) : (cxt->history_pos == cxt->hlen))
555 cxt->sflags |= SF_FAILED;
559 /* We will need these later. */
560 cxt->sline = cxt->lines[cxt->history_pos];
561 cxt->sline_len = strlen (cxt->sline);
563 while ((cxt->prev_line_found && STREQ (cxt->prev_line_found, cxt->lines[cxt->history_pos])) ||
564 (cxt->search_string_index > cxt->sline_len));
566 if (cxt->sflags & SF_FAILED)
569 /* Now set up the line for searching... */
570 cxt->sline_index = (cxt->sflags & SF_REVERSE) ? cxt->sline_len - cxt->search_string_index : 0;
573 if (cxt->sflags & SF_FAILED)
575 /* We cannot find the search string. Ding the bell. */
577 cxt->history_pos = cxt->last_found_line;
581 /* We have found the search string. Just display it. But don't
582 actually move there in the history list until the user accepts
584 if (cxt->sflags & SF_FOUND)
586 cxt->prev_line_found = cxt->lines[cxt->history_pos];
587 rl_replace_line (cxt->lines[cxt->history_pos], 0);
588 rl_point = cxt->sline_index;
589 cxt->last_found_line = cxt->history_pos;
590 rl_display_search (cxt->search_string, (cxt->sflags & SF_REVERSE), (cxt->history_pos == cxt->save_line) ? -1 : cxt->history_pos);
597 _rl_isearch_cleanup (cxt, r)
602 _rl_isearch_fini (cxt);
603 _rl_scxt_dispose (cxt, 0);
606 RL_UNSETSTATE(RL_STATE_ISEARCH);
611 /* Search through the history looking for an interactively typed string.
612 This is analogous to i-search. We start the search in the current line.
613 DIRECTION is which direction to search; >= 0 means forward, < 0 means
616 rl_search_history (direction, invoking_key)
617 int direction, invoking_key;
619 _rl_search_cxt *cxt; /* local for now, but saved globally */
622 RL_SETSTATE(RL_STATE_ISEARCH);
623 cxt = _rl_isearch_init (direction);
625 rl_display_search (cxt->search_string, (cxt->sflags & SF_REVERSE), -1);
627 /* If we are using the callback interface, all we do is set up here and
628 return. The key is that we leave RL_STATE_ISEARCH set. */
629 if (RL_ISSTATE (RL_STATE_CALLBACK))
635 c = _rl_search_getchar (cxt);
636 /* We might want to handle EOF here (c == 0) */
637 r = _rl_isearch_dispatch (cxt, cxt->lastc);
642 /* The searching is over. The user may have found the string that she
643 was looking for, or else she may have exited a failing search. If
644 LINE_INDEX is -1, then that shows that the string searched for was
645 not found. We use this to determine where to place rl_point. */
646 return (_rl_isearch_cleanup (cxt, r));
649 #if defined (READLINE_CALLBACKS)
650 /* Called from the callback functions when we are ready to read a key. The
651 callback functions know to call this because RL_ISSTATE(RL_STATE_ISEARCH).
652 If _rl_isearch_dispatch finishes searching, this function is responsible
653 for turning off RL_STATE_ISEARCH, which it does using _rl_isearch_cleanup. */
655 _rl_isearch_callback (cxt)
660 c = _rl_search_getchar (cxt);
661 /* We might want to handle EOF here */
662 r = _rl_isearch_dispatch (cxt, cxt->lastc);
664 return (r <= 0) ? _rl_isearch_cleanup (cxt, r) : 0;