| 1 |
/* search.c - code for non-incremental searching in emacs and vi modes. */ |
| 2 |
|
| 3 |
/* Copyright (C) 1992 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is part of the Readline Library (the Library), a set of |
| 6 |
routines for providing Emacs style line input to programs that ask |
| 7 |
for it. |
| 8 |
|
| 9 |
The Library is free software; you can redistribute it and/or modify |
| 10 |
it under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation; either version 1, or (at your option) |
| 12 |
any later version. |
| 13 |
|
| 14 |
The Library is distributed in the hope that it will be useful, but |
| 15 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
General Public License for more details. |
| 18 |
|
| 19 |
The GNU General Public License is often shipped with GNU software, and |
| 20 |
is generally kept in a file called COPYING or LICENSE. If you do not |
| 21 |
have a copy of the license, write to the Free Software Foundation, |
| 22 |
675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 23 |
#define READLINE_LIBRARY |
| 24 |
|
| 25 |
#include <sys/types.h> |
| 26 |
#include <stdio.h> |
| 27 |
|
| 28 |
#if defined (HAVE_UNISTD_H) |
| 29 |
# include <unistd.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#include "memalloc.h" |
| 33 |
#include "rldefs.h" |
| 34 |
#include "readline.h" |
| 35 |
#include "history.h" |
| 36 |
#include <stdlib.h> |
| 37 |
|
| 38 |
#define STREQ(a, b) (((a)[0] == (b)[0]) && (strcmp ((a), (b)) == 0)) |
| 39 |
#define STREQN(a, b, n) (((a)[0] == (b)[0]) && (strncmp ((a), (b), (n)) == 0)) |
| 40 |
|
| 41 |
#define abs(x) (((x) > 0) ? (x) : -(x)) |
| 42 |
|
| 43 |
extern char *xmalloc (), *xrealloc (); |
| 44 |
|
| 45 |
/* Variables imported from readline.c */ |
| 46 |
extern int rl_point, rl_end, rl_line_buffer_len; |
| 47 |
extern Keymap _rl_keymap; |
| 48 |
extern char *rl_prompt; |
| 49 |
extern char *rl_line_buffer; |
| 50 |
extern HIST_ENTRY *saved_line_for_history; |
| 51 |
extern Function *rl_last_func; |
| 52 |
|
| 53 |
/* Functions imported from the rest of the library. */ |
| 54 |
extern int _rl_free_history_entry (); |
| 55 |
|
| 56 |
static char *noninc_search_string = (char *) NULL; |
| 57 |
static int noninc_history_pos = 0; |
| 58 |
static char *prev_line_found = (char *) NULL; |
| 59 |
|
| 60 |
/* Search the history list for STRING starting at absolute history position |
| 61 |
POS. If STRING begins with `^', the search must match STRING at the |
| 62 |
beginning of a history line, otherwise a full substring match is performed |
| 63 |
for STRING. DIR < 0 means to search backwards through the history list, |
| 64 |
DIR >= 0 means to search forward. */ |
| 65 |
static int |
| 66 |
noninc_search_from_pos (string, pos, dir) |
| 67 |
char *string; |
| 68 |
int pos, dir; |
| 69 |
{ |
| 70 |
int ret, old; |
| 71 |
|
| 72 |
old = where_history (); |
| 73 |
history_set_pos (pos); |
| 74 |
|
| 75 |
if (*string == '^') |
| 76 |
ret = history_search_prefix (string + 1, dir); |
| 77 |
else |
| 78 |
ret = history_search (string, dir); |
| 79 |
|
| 80 |
if (ret != -1) |
| 81 |
ret = where_history (); |
| 82 |
|
| 83 |
history_set_pos (old); |
| 84 |
return (ret); |
| 85 |
} |
| 86 |
|
| 87 |
/* Search for a line in the history containing STRING. If DIR is < 0, the |
| 88 |
search is backwards through previous entries, else through subsequent |
| 89 |
entries. */ |
| 90 |
static void |
| 91 |
noninc_dosearch (string, dir) |
| 92 |
char *string; |
| 93 |
int dir; |
| 94 |
{ |
| 95 |
int oldpos, pos; |
| 96 |
HIST_ENTRY *entry; |
| 97 |
|
| 98 |
if (string == 0 || *string == 0 || noninc_history_pos < 0) |
| 99 |
{ |
| 100 |
ding (); |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
pos = noninc_search_from_pos (string, noninc_history_pos + dir, dir); |
| 105 |
if (pos == -1) |
| 106 |
{ |
| 107 |
/* Search failed, current history position unchanged. */ |
| 108 |
maybe_unsave_line (); |
| 109 |
rl_clear_message (); |
| 110 |
rl_point = 0; |
| 111 |
ding (); |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
noninc_history_pos = pos; |
| 116 |
|
| 117 |
oldpos = where_history (); |
| 118 |
history_set_pos (noninc_history_pos); |
| 119 |
entry = current_history (); |
| 120 |
history_set_pos (oldpos); |
| 121 |
|
| 122 |
{ |
| 123 |
int line_len; |
| 124 |
|
| 125 |
line_len = strlen (entry->line); |
| 126 |
if (line_len >= rl_line_buffer_len) |
| 127 |
rl_extend_line_buffer (line_len); |
| 128 |
strcpy (rl_line_buffer, entry->line); |
| 129 |
} |
| 130 |
|
| 131 |
rl_undo_list = (UNDO_LIST *)entry->data; |
| 132 |
rl_end = strlen (rl_line_buffer); |
| 133 |
rl_point = 0; |
| 134 |
rl_clear_message (); |
| 135 |
|
| 136 |
if (saved_line_for_history) |
| 137 |
_rl_free_history_entry (saved_line_for_history); |
| 138 |
saved_line_for_history = (HIST_ENTRY *)NULL; |
| 139 |
} |
| 140 |
|
| 141 |
/* Search non-interactively through the history list. DIR < 0 means to |
| 142 |
search backwards through the history of previous commands; otherwise |
| 143 |
the search is for commands subsequent to the current position in the |
| 144 |
history list. PCHAR is the character to use for prompting when reading |
| 145 |
the search string; if not specified (0), it defaults to `:'. */ |
| 146 |
static void |
| 147 |
noninc_search (dir, pchar) |
| 148 |
int dir; |
| 149 |
int pchar; |
| 150 |
{ |
| 151 |
int saved_point, c, pmtlen; |
| 152 |
char *p; |
| 153 |
|
| 154 |
maybe_save_line (); |
| 155 |
saved_point = rl_point; |
| 156 |
|
| 157 |
/* Use the line buffer to read the search string. */ |
| 158 |
rl_line_buffer[0] = 0; |
| 159 |
rl_end = rl_point = 0; |
| 160 |
|
| 161 |
/* XXX - this needs fixing to work with the prompt expansion stuff - XXX */ |
| 162 |
pmtlen = (rl_prompt && *rl_prompt) ? strlen (rl_prompt) : 0; |
| 163 |
p = xmalloc (2 + pmtlen); |
| 164 |
if (pmtlen) |
| 165 |
strcpy (p, rl_prompt); |
| 166 |
p[pmtlen] = pchar ? pchar : ':'; |
| 167 |
p[pmtlen + 1] = '\0'; |
| 168 |
|
| 169 |
rl_message (p, 0, 0); |
| 170 |
free (p); |
| 171 |
|
| 172 |
/* Read the search string. */ |
| 173 |
while (c = rl_read_key ()) |
| 174 |
{ |
| 175 |
switch (c) |
| 176 |
{ |
| 177 |
case CTRL('H'): |
| 178 |
case RUBOUT: |
| 179 |
if (rl_point == 0) |
| 180 |
{ |
| 181 |
maybe_unsave_line (); |
| 182 |
rl_clear_message (); |
| 183 |
rl_point = saved_point; |
| 184 |
return; |
| 185 |
} |
| 186 |
rl_rubout (1); |
| 187 |
break; |
| 188 |
|
| 189 |
case CTRL('W'): |
| 190 |
rl_unix_word_rubout (); |
| 191 |
break; |
| 192 |
|
| 193 |
case CTRL('U'): |
| 194 |
rl_unix_line_discard (); |
| 195 |
break; |
| 196 |
|
| 197 |
case RETURN: |
| 198 |
case NEWLINE: |
| 199 |
goto dosearch; |
| 200 |
/* NOTREACHED */ |
| 201 |
break; |
| 202 |
|
| 203 |
case CTRL('C'): |
| 204 |
case CTRL('G'): |
| 205 |
maybe_unsave_line (); |
| 206 |
rl_clear_message (); |
| 207 |
rl_point = saved_point; |
| 208 |
ding (); |
| 209 |
return; |
| 210 |
|
| 211 |
default: |
| 212 |
rl_insert (1, c); |
| 213 |
break; |
| 214 |
} |
| 215 |
rl_redisplay (); |
| 216 |
} |
| 217 |
|
| 218 |
dosearch: |
| 219 |
/* If rl_point == 0, we want to re-use the previous search string and |
| 220 |
start from the saved history position. If there's no previous search |
| 221 |
string, punt. */ |
| 222 |
if (rl_point == 0) |
| 223 |
{ |
| 224 |
if (!noninc_search_string) |
| 225 |
{ |
| 226 |
ding (); |
| 227 |
return; |
| 228 |
} |
| 229 |
} |
| 230 |
else |
| 231 |
{ |
| 232 |
/* We want to start the search from the current history position. */ |
| 233 |
noninc_history_pos = where_history (); |
| 234 |
if (noninc_search_string) |
| 235 |
free (noninc_search_string); |
| 236 |
noninc_search_string = savestring (rl_line_buffer); |
| 237 |
} |
| 238 |
|
| 239 |
noninc_dosearch (noninc_search_string, dir); |
| 240 |
} |
| 241 |
|
| 242 |
/* Search forward through the history list for a string. If the vi-mode |
| 243 |
code calls this, KEY will be `?'. */ |
| 244 |
rl_noninc_forward_search (count, key) |
| 245 |
int count, key; |
| 246 |
{ |
| 247 |
if (key == '?') |
| 248 |
noninc_search (1, '?'); |
| 249 |
else |
| 250 |
noninc_search (1, 0); |
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
/* Reverse search the history list for a string. If the vi-mode code |
| 255 |
calls this, KEY will be `/'. */ |
| 256 |
rl_noninc_reverse_search (count, key) |
| 257 |
int count, key; |
| 258 |
{ |
| 259 |
if (key == '/') |
| 260 |
noninc_search (-1, '/'); |
| 261 |
else |
| 262 |
noninc_search (-1, 0); |
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
/* Search forward through the history list for the last string searched |
| 267 |
for. If there is no saved search string, abort. */ |
| 268 |
rl_noninc_forward_search_again (count, key) |
| 269 |
int count, key; |
| 270 |
{ |
| 271 |
if (!noninc_search_string) |
| 272 |
{ |
| 273 |
ding (); |
| 274 |
return (-1); |
| 275 |
} |
| 276 |
noninc_dosearch (noninc_search_string, 1); |
| 277 |
return 0; |
| 278 |
} |
| 279 |
|
| 280 |
/* Reverse search in the history list for the last string searched |
| 281 |
for. If there is no saved search string, abort. */ |
| 282 |
rl_noninc_reverse_search_again (count, key) |
| 283 |
int count, key; |
| 284 |
{ |
| 285 |
if (!noninc_search_string) |
| 286 |
{ |
| 287 |
ding (); |
| 288 |
return (-1); |
| 289 |
} |
| 290 |
noninc_dosearch (noninc_search_string, -1); |
| 291 |
return 0; |
| 292 |
} |
| 293 |
|
| 294 |
static int |
| 295 |
rl_history_search_internal (count, direction) |
| 296 |
int count, direction; |
| 297 |
{ |
| 298 |
HIST_ENTRY *temp, *old_temp; |
| 299 |
int line_len; |
| 300 |
|
| 301 |
maybe_save_line (); |
| 302 |
|
| 303 |
temp = old_temp = (HIST_ENTRY *)NULL; |
| 304 |
while (count) |
| 305 |
{ |
| 306 |
temp = (direction < 0) ? previous_history () : next_history (); |
| 307 |
if (!temp) |
| 308 |
break; |
| 309 |
if (STREQN (rl_line_buffer, temp->line, rl_point)) |
| 310 |
{ |
| 311 |
/* Don't find multiple instances of the same line. */ |
| 312 |
if (prev_line_found && STREQ (prev_line_found, temp->line)) |
| 313 |
continue; |
| 314 |
if (direction < 0) |
| 315 |
old_temp = temp; |
| 316 |
prev_line_found = temp->line; |
| 317 |
count--; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
if (!temp) |
| 322 |
{ |
| 323 |
if (direction < 0 && old_temp) |
| 324 |
temp = old_temp; |
| 325 |
else |
| 326 |
{ |
| 327 |
maybe_unsave_line (); |
| 328 |
ding (); |
| 329 |
return 1; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
line_len = strlen (temp->line); |
| 334 |
if (line_len >= rl_line_buffer_len) |
| 335 |
rl_extend_line_buffer (line_len); |
| 336 |
strcpy (rl_line_buffer, temp->line); |
| 337 |
rl_undo_list = (UNDO_LIST *)temp->data; |
| 338 |
rl_end = line_len; |
| 339 |
return 0; |
| 340 |
} |
| 341 |
|
| 342 |
/* Search forward in the history for the string of characters |
| 343 |
from the start of the line to rl_point. This is a non-incremental |
| 344 |
search. */ |
| 345 |
int |
| 346 |
rl_history_search_forward (count, ignore) |
| 347 |
int count, ignore; |
| 348 |
{ |
| 349 |
if (count == 0) |
| 350 |
return (0); |
| 351 |
if (rl_last_func != rl_history_search_forward) |
| 352 |
prev_line_found = (char *)NULL; |
| 353 |
return (rl_history_search_internal (abs (count), (count > 0) ? 1 : -1)); |
| 354 |
} |
| 355 |
|
| 356 |
/* Search backward through the history for the string of characters |
| 357 |
from the start of the line to rl_point. This is a non-incremental |
| 358 |
search. */ |
| 359 |
int |
| 360 |
rl_history_search_backward (count, ignore) |
| 361 |
int count, ignore; |
| 362 |
{ |
| 363 |
if (count == 0) |
| 364 |
return (0); |
| 365 |
if (rl_last_func != rl_history_search_backward) |
| 366 |
prev_line_found = (char *)NULL; |
| 367 |
return (rl_history_search_internal (abs (count), (count > 0) ? -1 : 1)); |
| 368 |
} |