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