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 1, 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 675 Mass Ave, Cambridge, MA 02139, USA. */
30 #if defined (__GNUC__)
31 # define alloca __builtin_alloca
33 # if defined (sparc) || defined (HAVE_ALLOCA_H)
41 extern Keymap _rl_keymap;
42 extern HIST_ENTRY *saved_line_for_history;
43 extern int rl_line_buffer_len;
44 extern int rl_point, rl_end;
45 extern char *rl_prompt, *rl_line_buffer;
47 /* Remove these declarations when we have a complete libgnu.a. */
48 extern char *xmalloc (), *xrealloc ();
50 static void rl_search_history ();
52 /* Search backwards through the history looking for a string which is typed
53 interactively. Start with the current line. */
54 rl_reverse_search_history (sign, key)
58 rl_search_history (-sign, key);
61 /* Search forwards through the history looking for a string which is typed
62 interactively. Start with the current line. */
63 rl_forward_search_history (sign, key)
67 rl_search_history (sign, key);
70 /* Display the current state of the search in the echo-area.
71 SEARCH_STRING contains the string that is being searched for,
72 DIRECTION is zero for forward, or 1 for reverse,
73 WHERE is the history list number of the current line. If it is
74 -1, then this line is the starting one. */
76 rl_display_search (search_string, reverse_p, where)
80 char *message = (char *)NULL;
83 (char *)xmalloc (1 + (search_string ? strlen (search_string) : 0) + 30);
89 sprintf (message, "[%d]", where + history_base);
92 strcat (message, "(");
95 strcat (message, "reverse-");
97 strcat (message, "i-search)`");
100 strcat (message, search_string);
102 strcat (message, "': ");
103 rl_message ("%s", message, 0);
108 /* Search through the history looking for an interactively typed string.
109 This is analogous to i-search. We start the search in the current line.
110 DIRECTION is which direction to search; >= 0 means forward, < 0 means
113 rl_search_history (direction, invoking_key)
117 /* The string that the user types in to search for. */
120 /* The current length of SEARCH_STRING. */
121 int search_string_index;
123 /* The amount of space that SEARCH_STRING has allocated to it. */
124 int search_string_size;
126 /* The list of lines to search through. */
129 /* The length of LINES. */
132 /* Where we get LINES from. */
133 HIST_ENTRY **hlist = history_list ();
136 int orig_point = rl_point;
137 int orig_line = where_history ();
138 int last_found_line = orig_line;
141 /* The line currently being searched. */
144 /* Offset in that line. */
147 /* Non-zero if we are doing a reverse search. */
148 int reverse = (direction < 0);
150 /* Create an arrary of pointers to the lines that we want to search. */
151 maybe_replace_line ();
153 for (i = 0; hlist[i]; i++);
155 /* Allocate space for this many lines, +1 for the current input line,
156 and remember those lines. */
157 lines = (char **)alloca ((1 + (hlen = i)) * sizeof (char *));
158 for (i = 0; i < hlen; i++)
159 lines[i] = hlist[i]->line;
161 if (saved_line_for_history)
162 lines[i] = saved_line_for_history->line;
164 /* So I have to type it in this way instead. */
168 /* Keep that MIPS alloca () happy. */
169 alloced_line = (char *)alloca (1 + strlen (rl_line_buffer));
170 lines[i] = alloced_line;
171 strcpy (lines[i], &rl_line_buffer[0]);
176 /* The line where we start the search. */
179 /* Initialize search parameters. */
180 search_string = (char *)xmalloc (search_string_size = 128);
181 *search_string = '\0';
182 search_string_index = 0;
184 /* Normalize DIRECTION into 1 or -1. */
190 rl_display_search (search_string, reverse, -1);
192 sline = rl_line_buffer;
199 /* Hack C to Do What I Mean. */
201 Function *f = (Function *)NULL;
203 if (_rl_keymap[c].type == ISFUNC)
205 f = _rl_keymap[c].function;
207 if (f == rl_reverse_search_history)
208 c = reverse ? -1 : -2;
209 else if (f == rl_forward_search_history)
210 c = !reverse ? -1 : -2;
220 /* case invoking_key: */
224 /* switch directions */
226 direction = -direction;
227 reverse = (direction < 0);
232 strcpy (rl_line_buffer, lines[orig_line]);
233 rl_point = orig_point;
234 rl_end = strlen (rl_line_buffer);
239 if (c < 32 || c > 126)
247 if (search_string_index + 2 >= search_string_size)
248 search_string = (char *)xrealloc
249 (search_string, (search_string_size += 128));
250 search_string[search_string_index++] = c;
251 search_string[search_string_index] = '\0';
256 if (!search_string_index)
263 if (index != strlen (sline))
276 (search_string, sline + index, search_string_index)
285 (strlen (sline) - search_string_index) + 1;
287 while (index < limit)
289 if (strncmp (search_string,
291 search_string_index) == 0)
300 /* At limit for direction? */
301 if ((reverse && i < 0) ||
302 (!reverse && i == hlen))
307 index = strlen (sline);
311 /* If the search string is longer than the current
313 if (search_string_index > (int)strlen (sline))
316 /* Start actually searching. */
318 index -= search_string_index;
322 /* We cannot find the search string. Ding the bell. */
328 /* We have found the search string. Just display it. But don't
329 actually move there in the history list until the user accepts
334 line_len = strlen (lines[i]);
336 if (line_len >= rl_line_buffer_len)
337 rl_extend_line_buffer (line_len);
339 strcpy (rl_line_buffer, lines[i]);
344 (search_string, reverse, (i == orig_line) ? -1 : i);
351 /* The searching is over. The user may have found the string that she
352 was looking for, or else she may have exited a failing search. If
353 INDEX is -1, then that shows that the string searched for was not
354 found. We use this to determine where to place rl_point. */
356 int now = last_found_line;
358 /* First put back the original state. */
359 strcpy (rl_line_buffer, lines[orig_line]);
361 /* Free the search string. */
362 free (search_string);
365 rl_get_previous_history (orig_line - now);
367 rl_get_next_history (now - orig_line);
369 /* If the index of the "matched" string is less than zero, then the
370 final search string was never matched, so put point somewhere
373 index = strlen (rl_line_buffer);