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. */
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 /* Variables imported from other files in the readline library. */
52 extern Keymap _rl_keymap
;
53 extern HIST_ENTRY
*saved_line_for_history
;
54 extern int rl_line_buffer_len
;
55 extern int rl_point
, rl_end
;
56 extern char *rl_line_buffer
;
58 extern void _rl_save_prompt ();
59 extern void _rl_restore_prompt ();
61 extern int rl_execute_next ();
62 extern void rl_extend_line_buffer ();
64 extern int _rl_input_available ();
66 extern char *xmalloc (), *xrealloc ();
68 static int rl_search_history ();
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 /* Search backwards through the history looking for a string which is typed
75 interactively. Start with the current line. */
77 rl_reverse_search_history (sign
, key
)
80 return (rl_search_history (-sign
, key
));
83 /* Search forwards through the history looking for a string which is typed
84 interactively. Start with the current line. */
86 rl_forward_search_history (sign
, key
)
89 return (rl_search_history (sign
, key
));
92 /* Display the current state of the search in the echo-area.
93 SEARCH_STRING contains the string that is being searched for,
94 DIRECTION is zero for forward, or 1 for reverse,
95 WHERE is the history list number of the current line. If it is
96 -1, then this line is the starting one. */
98 rl_display_search (search_string
, reverse_p
, where
)
100 int reverse_p
, where
;
103 int msglen
, searchlen
;
105 searchlen
= (search_string
&& *search_string
) ? strlen (search_string
) : 0;
107 message
= xmalloc (searchlen
+ 33);
113 sprintf (message
, "[%d]", where
+ history_base
);
114 msglen
= strlen (message
);
118 message
[msglen
++] = '(';
122 strcpy (message
+ msglen
, "reverse-");
126 strcpy (message
+ msglen
, "i-search)`");
131 strcpy (message
+ msglen
, search_string
);
135 strcpy (message
+ msglen
, "': ");
137 rl_message ("%s", message
, 0);
139 (*rl_redisplay_function
) ();
142 /* Search through the history looking for an interactively typed string.
143 This is analogous to i-search. We start the search in the current line.
144 DIRECTION is which direction to search; >= 0 means forward, < 0 means
147 rl_search_history (direction
, invoking_key
)
148 int direction
, invoking_key
;
150 /* The string that the user types in to search for. */
153 /* The current length of SEARCH_STRING. */
154 int search_string_index
;
156 /* The amount of space that SEARCH_STRING has allocated to it. */
157 int search_string_size
;
159 /* The list of lines to search through. */
160 char **lines
, *allocated_line
;
162 /* The length of LINES. */
165 /* Where we get LINES from. */
169 int orig_point
, orig_line
, last_found_line
;
170 int c
, found
, failed
, sline_len
;
172 /* The line currently being searched. */
175 /* Offset in that line. */
178 /* Non-zero if we are doing a reverse search. */
181 orig_point
= rl_point
;
182 last_found_line
= orig_line
= where_history ();
183 reverse
= direction
< 0;
184 hlist
= history_list ();
185 allocated_line
= (char *)NULL
;
187 /* Create an arrary of pointers to the lines that we want to search. */
188 maybe_replace_line ();
191 for (i
= 0; hlist
[i
]; i
++);
193 /* Allocate space for this many lines, +1 for the current input line,
194 and remember those lines. */
195 lines
= (char **)xmalloc ((1 + (hlen
= i
)) * sizeof (char *));
196 for (i
= 0; i
< hlen
; i
++)
197 lines
[i
] = hlist
[i
]->line
;
199 if (saved_line_for_history
)
200 lines
[i
] = saved_line_for_history
->line
;
203 /* Keep track of this so we can free it. */
204 allocated_line
= xmalloc (1 + strlen (rl_line_buffer
));
205 strcpy (allocated_line
, &rl_line_buffer
[0]);
206 lines
[i
] = allocated_line
;
211 /* The line where we start the search. */
216 /* Initialize search parameters. */
217 search_string
= xmalloc (search_string_size
= 128);
218 *search_string
= '\0';
219 search_string_index
= 0;
220 prev_line_found
= (char *)0; /* XXX */
222 /* Normalize DIRECTION into 1 or -1. */
223 direction
= (direction
>= 0) ? 1 : -1;
225 rl_display_search (search_string
, reverse
, -1);
227 sline
= rl_line_buffer
;
228 sline_len
= strlen (sline
);
229 line_index
= rl_point
;
234 Function
*f
= (Function
*)NULL
;
236 /* Read a key and decide how to proceed. */
239 if (_rl_keymap
[c
].type
== ISFUNC
)
241 f
= _rl_keymap
[c
].function
;
243 if (f
== rl_reverse_search_history
)
244 c
= reverse
? -1 : -2;
245 else if (f
== rl_forward_search_history
)
246 c
= !reverse
? -1 : -2;
249 /* Let NEWLINE (^J) terminate the search for people who don't like
250 using ESC. ^M can still be used to terminate the search and
251 immediately execute the command. */
252 if (c
== ESC
|| c
== NEWLINE
)
254 /* ESC still terminates the search, but if there is pending
255 input or if input arrives within 0.1 seconds (on systems
256 with select(2)) it is used as a prefix character
257 with rl_execute_next. WATCH OUT FOR THIS! This is intended
258 to allow the arrow keys to be used like ^F and ^B are used
259 to terminate the search and execute the movement command. */
260 if (c
== ESC
&& _rl_input_available ()) /* XXX */
261 rl_execute_next (ESC
);
265 if (c
>= 0 && (CTRL_CHAR (c
) || META_CHAR (c
) || c
== RUBOUT
) && c
!= CTRL ('G'))
274 if (search_string_index
== 0)
278 else if (line_index
!= sline_len
)
284 /* switch directions */
286 direction
= -direction
;
287 reverse
= direction
< 0;
291 strcpy (rl_line_buffer
, lines
[orig_line
]);
292 rl_point
= orig_point
;
293 rl_end
= strlen (rl_line_buffer
);
294 _rl_restore_prompt();
297 free (allocated_line
);
302 /* delete character from search string. */
304 if (search_string_index
== 0)
308 search_string
[--search_string_index
] = '\0';
309 /* This is tricky. To do this right, we need to keep a
310 stack of search positions for the current search, with
311 sentinels marking the beginning and end. */
317 /* Add character to search string and continue search. */
318 if (search_string_index
+ 2 >= search_string_size
)
320 search_string_size
+= 128;
321 search_string
= xrealloc (search_string
, search_string_size
);
323 search_string
[search_string_index
++] = c
;
324 search_string
[search_string_index
] = '\0';
328 for (found
= failed
= 0;;)
330 int limit
= sline_len
- search_string_index
+ 1;
332 /* Search the current line. */
333 while (reverse
? (line_index
>= 0) : (line_index
< limit
))
335 if (STREQN (search_string
, sline
+ line_index
, search_string_index
))
341 line_index
+= direction
;
346 /* Move to the next line, but skip new copies of the line
347 we just found and lines shorter than the string we're
351 /* Move to the next line. */
354 /* At limit for direction? */
355 if (reverse
? (i
< 0) : (i
== hlen
))
361 /* We will need these later. */
363 sline_len
= strlen (sline
);
365 while ((prev_line_found
&& STREQ (prev_line_found
, lines
[i
])) ||
366 (search_string_index
> sline_len
));
371 /* Now set up the line for searching... */
372 line_index
= reverse
? sline_len
- search_string_index
: 0;
377 /* We cannot find the search string. Ding the bell. */
380 continue; /* XXX - was break */
383 /* We have found the search string. Just display it. But don't
384 actually move there in the history list until the user accepts
390 prev_line_found
= lines
[i
];
391 line_len
= strlen (lines
[i
]);
393 if (line_len
>= rl_line_buffer_len
)
394 rl_extend_line_buffer (line_len
);
396 strcpy (rl_line_buffer
, lines
[i
]);
397 rl_point
= line_index
;
400 rl_display_search (search_string
, reverse
, (i
== orig_line
) ? -1 : i
);
404 /* The searching is over. The user may have found the string that she
405 was looking for, or else she may have exited a failing search. If
406 LINE_INDEX is -1, then that shows that the string searched for was
407 not found. We use this to determine where to place rl_point. */
409 /* First put back the original state. */
410 strcpy (rl_line_buffer
, lines
[orig_line
]);
412 _rl_restore_prompt ();
414 /* Free the search string. */
415 free (search_string
);
417 if (last_found_line
< orig_line
)
418 rl_get_previous_history (orig_line
- last_found_line
);
420 rl_get_next_history (last_found_line
- orig_line
);
422 /* If the string was not found, put point at the end of the line. */
424 line_index
= strlen (rl_line_buffer
);
425 rl_point
= line_index
;
429 free (allocated_line
);