| 1 |
/* **************************************************************** */ |
| 2 |
/* */ |
| 3 |
/* I-Search and Searching */ |
| 4 |
/* */ |
| 5 |
/* **************************************************************** */ |
| 6 |
|
| 7 |
/* Copyright (C) 1987,1989 Free Software Foundation, Inc. |
| 8 |
|
| 9 |
This file contains the Readline Library (the Library), a set of |
| 10 |
routines for providing Emacs style line input to programs that ask |
| 11 |
for it. |
| 12 |
|
| 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) |
| 16 |
any later version. |
| 17 |
|
| 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. |
| 22 |
|
| 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 |
| 28 |
|
| 29 |
#include <stdio.h> |
| 30 |
|
| 31 |
#if defined (HAVE_UNISTD_H) |
| 32 |
# include <unistd.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
#include "memalloc.h" |
| 36 |
#include "readline.h" |
| 37 |
#include "history.h" |
| 38 |
#include <stdlib.h> |
| 39 |
|
| 40 |
#define STREQ(a, b) (((a)[0] == (b)[0]) && (strcmp ((a), (b)) == 0)) |
| 41 |
#define STREQN(a, b, n) (((a)[0] == (b)[0]) && (strncmp ((a), (b), (n)) == 0)) |
| 42 |
|
| 43 |
/* Variables imported from other files in the readline library. */ |
| 44 |
extern Keymap _rl_keymap; |
| 45 |
extern HIST_ENTRY *saved_line_for_history; |
| 46 |
extern int rl_line_buffer_len; |
| 47 |
extern int rl_point, rl_end; |
| 48 |
extern char *rl_line_buffer; |
| 49 |
|
| 50 |
extern char *xmalloc (), *xrealloc (); |
| 51 |
|
| 52 |
static int rl_search_history (); |
| 53 |
|
| 54 |
/* Last line found by the current incremental search, so we don't `find' |
| 55 |
identical lines many times in a row. */ |
| 56 |
static char *prev_line_found; |
| 57 |
|
| 58 |
/* Search backwards through the history looking for a string which is typed |
| 59 |
interactively. Start with the current line. */ |
| 60 |
rl_reverse_search_history (sign, key) |
| 61 |
int sign; |
| 62 |
int key; |
| 63 |
{ |
| 64 |
return (rl_search_history (-sign, key)); |
| 65 |
} |
| 66 |
|
| 67 |
/* Search forwards through the history looking for a string which is typed |
| 68 |
interactively. Start with the current line. */ |
| 69 |
rl_forward_search_history (sign, key) |
| 70 |
int sign; |
| 71 |
int key; |
| 72 |
{ |
| 73 |
return (rl_search_history (sign, key)); |
| 74 |
} |
| 75 |
|
| 76 |
/* Display the current state of the search in the echo-area. |
| 77 |
SEARCH_STRING contains the string that is being searched for, |
| 78 |
DIRECTION is zero for forward, or 1 for reverse, |
| 79 |
WHERE is the history list number of the current line. If it is |
| 80 |
-1, then this line is the starting one. */ |
| 81 |
static void |
| 82 |
rl_display_search (search_string, reverse_p, where) |
| 83 |
char *search_string; |
| 84 |
int reverse_p, where; |
| 85 |
{ |
| 86 |
char *message; |
| 87 |
|
| 88 |
message = xmalloc (1 + (search_string ? strlen (search_string) : 0) + 30); |
| 89 |
*message = '\0'; |
| 90 |
|
| 91 |
#if defined (NOTDEF) |
| 92 |
if (where != -1) |
| 93 |
sprintf (message, "[%d]", where + history_base); |
| 94 |
#endif /* NOTDEF */ |
| 95 |
|
| 96 |
strcat (message, "("); |
| 97 |
|
| 98 |
if (reverse_p) |
| 99 |
strcat (message, "reverse-"); |
| 100 |
|
| 101 |
strcat (message, "i-search)`"); |
| 102 |
|
| 103 |
if (search_string) |
| 104 |
strcat (message, search_string); |
| 105 |
|
| 106 |
strcat (message, "': "); |
| 107 |
rl_message ("%s", message, 0); |
| 108 |
free (message); |
| 109 |
rl_redisplay (); |
| 110 |
} |
| 111 |
|
| 112 |
/* Search through the history looking for an interactively typed string. |
| 113 |
This is analogous to i-search. We start the search in the current line. |
| 114 |
DIRECTION is which direction to search; >= 0 means forward, < 0 means |
| 115 |
backwards. */ |
| 116 |
static int |
| 117 |
rl_search_history (direction, invoking_key) |
| 118 |
int direction; |
| 119 |
int invoking_key; |
| 120 |
{ |
| 121 |
/* The string that the user types in to search for. */ |
| 122 |
char *search_string; |
| 123 |
|
| 124 |
/* The current length of SEARCH_STRING. */ |
| 125 |
int search_string_index; |
| 126 |
|
| 127 |
/* The amount of space that SEARCH_STRING has allocated to it. */ |
| 128 |
int search_string_size; |
| 129 |
|
| 130 |
/* The list of lines to search through. */ |
| 131 |
char **lines, *allocated_line = (char *)NULL; |
| 132 |
|
| 133 |
/* The length of LINES. */ |
| 134 |
int hlen; |
| 135 |
|
| 136 |
/* Where we get LINES from. */ |
| 137 |
HIST_ENTRY **hlist = history_list (); |
| 138 |
|
| 139 |
register int i = 0; |
| 140 |
int orig_point = rl_point; |
| 141 |
int orig_line = where_history (); |
| 142 |
int last_found_line = orig_line; |
| 143 |
int c, done = 0, found, failed, sline_len; |
| 144 |
|
| 145 |
/* The line currently being searched. */ |
| 146 |
char *sline; |
| 147 |
|
| 148 |
/* Offset in that line. */ |
| 149 |
int line_index; |
| 150 |
|
| 151 |
/* Non-zero if we are doing a reverse search. */ |
| 152 |
int reverse = (direction < 0); |
| 153 |
|
| 154 |
/* Create an arrary of pointers to the lines that we want to search. */ |
| 155 |
maybe_replace_line (); |
| 156 |
if (hlist) |
| 157 |
for (i = 0; hlist[i]; i++); |
| 158 |
|
| 159 |
/* Allocate space for this many lines, +1 for the current input line, |
| 160 |
and remember those lines. */ |
| 161 |
lines = (char **)xmalloc ((1 + (hlen = i)) * sizeof (char *)); |
| 162 |
for (i = 0; i < hlen; i++) |
| 163 |
lines[i] = hlist[i]->line; |
| 164 |
|
| 165 |
if (saved_line_for_history) |
| 166 |
lines[i] = saved_line_for_history->line; |
| 167 |
else |
| 168 |
{ |
| 169 |
/* Keep track of this so we can free it. */ |
| 170 |
allocated_line = xmalloc (1 + strlen (rl_line_buffer)); |
| 171 |
strcpy (allocated_line, &rl_line_buffer[0]); |
| 172 |
lines[i] = allocated_line; |
| 173 |
} |
| 174 |
|
| 175 |
hlen++; |
| 176 |
|
| 177 |
/* The line where we start the search. */ |
| 178 |
i = orig_line; |
| 179 |
|
| 180 |
/* Initialize search parameters. */ |
| 181 |
search_string = xmalloc (search_string_size = 128); |
| 182 |
*search_string = '\0'; |
| 183 |
search_string_index = 0; |
| 184 |
prev_line_found = (char *)0; /* XXX */ |
| 185 |
|
| 186 |
/* Normalize DIRECTION into 1 or -1. */ |
| 187 |
direction = (direction >= 0) ? 1 : -1; |
| 188 |
|
| 189 |
rl_display_search (search_string, reverse, -1); |
| 190 |
|
| 191 |
sline = rl_line_buffer; |
| 192 |
sline_len = strlen (sline); |
| 193 |
line_index = rl_point; |
| 194 |
|
| 195 |
found = failed = 0; |
| 196 |
while (!done) |
| 197 |
{ |
| 198 |
Function *f = (Function *)NULL; |
| 199 |
|
| 200 |
/* Read a key and decide how to proceed. */ |
| 201 |
c = rl_read_key (); |
| 202 |
|
| 203 |
/* Hack C to Do What I Mean. */ |
| 204 |
if (_rl_keymap[c].type == ISFUNC) |
| 205 |
{ |
| 206 |
f = _rl_keymap[c].function; |
| 207 |
|
| 208 |
if (f == rl_reverse_search_history) |
| 209 |
c = reverse ? -1 : -2; |
| 210 |
else if (f == rl_forward_search_history) |
| 211 |
c = !reverse ? -1 : -2; |
| 212 |
} |
| 213 |
|
| 214 |
switch (c) |
| 215 |
{ |
| 216 |
case ESC: |
| 217 |
done = 1; |
| 218 |
continue; |
| 219 |
|
| 220 |
case -1: |
| 221 |
if (!search_string_index) |
| 222 |
continue; |
| 223 |
else |
| 224 |
{ |
| 225 |
if (reverse) |
| 226 |
--line_index; |
| 227 |
else |
| 228 |
{ |
| 229 |
if (line_index != sline_len) |
| 230 |
++line_index; |
| 231 |
else |
| 232 |
ding (); |
| 233 |
} |
| 234 |
} |
| 235 |
break; |
| 236 |
|
| 237 |
/* switch directions */ |
| 238 |
case -2: |
| 239 |
direction = -direction; |
| 240 |
reverse = (direction < 0); |
| 241 |
break; |
| 242 |
|
| 243 |
case CTRL ('G'): |
| 244 |
strcpy (rl_line_buffer, lines[orig_line]); |
| 245 |
rl_point = orig_point; |
| 246 |
rl_end = strlen (rl_line_buffer); |
| 247 |
rl_clear_message (); |
| 248 |
free (allocated_line); |
| 249 |
free (lines); |
| 250 |
return 0; |
| 251 |
|
| 252 |
default: |
| 253 |
if (CTRL_P (c) || META_CHAR (c) || c == RUBOUT) |
| 254 |
{ |
| 255 |
rl_execute_next (c); |
| 256 |
done = 1; |
| 257 |
continue; |
| 258 |
} |
| 259 |
else |
| 260 |
{ |
| 261 |
/* Add character to search string and continue search. */ |
| 262 |
if (search_string_index + 2 >= search_string_size) |
| 263 |
{ |
| 264 |
search_string_size += 128; |
| 265 |
search_string = xrealloc (search_string, search_string_size); |
| 266 |
} |
| 267 |
search_string[search_string_index++] = c; |
| 268 |
search_string[search_string_index] = '\0'; |
| 269 |
break; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
found = failed = 0; |
| 274 |
while (1) |
| 275 |
{ |
| 276 |
int limit = sline_len - search_string_index + 1; |
| 277 |
|
| 278 |
/* Search the current line. */ |
| 279 |
while (reverse ? (line_index >= 0) : (line_index < limit)) |
| 280 |
{ |
| 281 |
if (STREQN(search_string, sline + line_index, search_string_index)) |
| 282 |
{ |
| 283 |
found++; |
| 284 |
break; |
| 285 |
} |
| 286 |
else |
| 287 |
line_index += direction; |
| 288 |
} |
| 289 |
if (found) |
| 290 |
break; |
| 291 |
|
| 292 |
/* Move to the next line, but skip new copies of the line |
| 293 |
we just found and lines shorter than the string we're |
| 294 |
searching for. */ |
| 295 |
do |
| 296 |
{ |
| 297 |
/* Move to the next line. */ |
| 298 |
i += direction; |
| 299 |
|
| 300 |
/* At limit for direction? */ |
| 301 |
if (reverse ? (i < 0) : (i == hlen)) |
| 302 |
{ |
| 303 |
failed++; |
| 304 |
break; |
| 305 |
} |
| 306 |
|
| 307 |
/* We will need these later. */ |
| 308 |
sline = lines[i]; |
| 309 |
sline_len = strlen (sline); |
| 310 |
} |
| 311 |
while ((prev_line_found && STREQ (prev_line_found, lines[i])) || |
| 312 |
(search_string_index > sline_len)); |
| 313 |
|
| 314 |
if (failed) |
| 315 |
break; |
| 316 |
|
| 317 |
/* Now set up the line for searching... */ |
| 318 |
if (reverse) |
| 319 |
line_index = sline_len - search_string_index; |
| 320 |
else |
| 321 |
line_index = 0; |
| 322 |
} |
| 323 |
|
| 324 |
if (failed) |
| 325 |
{ |
| 326 |
/* We cannot find the search string. Ding the bell. */ |
| 327 |
ding (); |
| 328 |
i = last_found_line; |
| 329 |
continue; /* XXX - was break */ |
| 330 |
} |
| 331 |
|
| 332 |
/* We have found the search string. Just display it. But don't |
| 333 |
actually move there in the history list until the user accepts |
| 334 |
the location. */ |
| 335 |
if (found) |
| 336 |
{ |
| 337 |
int line_len; |
| 338 |
|
| 339 |
prev_line_found = lines[i]; |
| 340 |
line_len = strlen (lines[i]); |
| 341 |
|
| 342 |
if (line_len >= rl_line_buffer_len) |
| 343 |
rl_extend_line_buffer (line_len); |
| 344 |
|
| 345 |
strcpy (rl_line_buffer, lines[i]); |
| 346 |
rl_point = line_index; |
| 347 |
rl_end = line_len; |
| 348 |
last_found_line = i; |
| 349 |
rl_display_search (search_string, reverse, (i == orig_line) ? -1 : i); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
/* The searching is over. The user may have found the string that she |
| 354 |
was looking for, or else she may have exited a failing search. If |
| 355 |
LINE_INDEX is -1, then that shows that the string searched for was |
| 356 |
not found. We use this to determine where to place rl_point. */ |
| 357 |
|
| 358 |
/* First put back the original state. */ |
| 359 |
strcpy (rl_line_buffer, lines[orig_line]); |
| 360 |
|
| 361 |
/* Free the search string. */ |
| 362 |
free (search_string); |
| 363 |
|
| 364 |
if (last_found_line < orig_line) |
| 365 |
rl_get_previous_history (orig_line - last_found_line); |
| 366 |
else |
| 367 |
rl_get_next_history (last_found_line - orig_line); |
| 368 |
|
| 369 |
/* If the string was not found, put point at the end of the line. */ |
| 370 |
if (line_index < 0) |
| 371 |
line_index = strlen (rl_line_buffer); |
| 372 |
rl_point = line_index; |
| 373 |
rl_clear_message (); |
| 374 |
|
| 375 |
free (allocated_line); |
| 376 |
free (lines); |
| 377 |
|
| 378 |
return 0; |
| 379 |
} |