version 1.1.1.1, 2014/07/30 08:16:45
|
version 1.1.1.2, 2021/03/17 01:01:01
|
Line 1
|
Line 1
|
/* search.c - code for non-incremental searching in emacs and vi modes. */ |
/* search.c - code for non-incremental searching in emacs and vi modes. */ |
|
|
/* Copyright (C) 1992-2013 Free Software Foundation, Inc. | /* Copyright (C) 1992-2020 Free Software Foundation, Inc. |
|
|
This file is part of the GNU Readline Library (Readline), a library |
This file is part of the GNU Readline Library (Readline), a library |
for reading lines of text with interactive input and history editing. |
for reading lines of text with interactive input and history editing. |
Line 58 _rl_search_cxt *_rl_nscxt = 0;
|
Line 58 _rl_search_cxt *_rl_nscxt = 0;
|
extern HIST_ENTRY *_rl_saved_line_for_history; |
extern HIST_ENTRY *_rl_saved_line_for_history; |
|
|
/* Functions imported from the rest of the library. */ |
/* Functions imported from the rest of the library. */ |
extern int _rl_free_history_entry PARAMS((HIST_ENTRY *)); | extern void _rl_free_history_entry PARAMS((HIST_ENTRY *)); |
|
|
static char *noninc_search_string = (char *) NULL; |
static char *noninc_search_string = (char *) NULL; |
static int noninc_history_pos; |
static int noninc_history_pos; |
Line 73 static char *history_search_string;
|
Line 73 static char *history_search_string;
|
static int history_string_size; |
static int history_string_size; |
|
|
static void make_history_line_current PARAMS((HIST_ENTRY *)); |
static void make_history_line_current PARAMS((HIST_ENTRY *)); |
static int noninc_search_from_pos PARAMS((char *, int, int)); | static int noninc_search_from_pos PARAMS((char *, int, int, int, int *)); |
static int noninc_dosearch PARAMS((char *, int)); | static int noninc_dosearch PARAMS((char *, int, int)); |
static int noninc_search PARAMS((int, int)); |
static int noninc_search PARAMS((int, int)); |
static int rl_history_search_internal PARAMS((int, int)); |
static int rl_history_search_internal PARAMS((int, int)); |
static void rl_history_search_reinit PARAMS((int)); |
static void rl_history_search_reinit PARAMS((int)); |
|
|
static _rl_search_cxt *_rl_nsearch_init PARAMS((int, int)); |
static _rl_search_cxt *_rl_nsearch_init PARAMS((int, int)); |
static int _rl_nsearch_cleanup PARAMS((_rl_search_cxt *, int)); |
|
static void _rl_nsearch_abort PARAMS((_rl_search_cxt *)); |
static void _rl_nsearch_abort PARAMS((_rl_search_cxt *)); |
static int _rl_nsearch_dispatch PARAMS((_rl_search_cxt *, int)); |
static int _rl_nsearch_dispatch PARAMS((_rl_search_cxt *, int)); |
|
|
Line 88 static int _rl_nsearch_dispatch PARAMS((_rl_search_cxt
|
Line 87 static int _rl_nsearch_dispatch PARAMS((_rl_search_cxt
|
current line. This doesn't do anything with rl_point; the caller |
current line. This doesn't do anything with rl_point; the caller |
must set it. */ |
must set it. */ |
static void |
static void |
make_history_line_current (entry) | make_history_line_current (HIST_ENTRY *entry) |
HIST_ENTRY *entry; | |
{ |
{ |
_rl_replace_text (entry->line, 0, rl_end); |
_rl_replace_text (entry->line, 0, rl_end); |
_rl_fix_point (1); |
_rl_fix_point (1); |
Line 113 make_history_line_current (entry)
|
Line 111 make_history_line_current (entry)
|
for STRING. DIR < 0 means to search backwards through the history list, |
for STRING. DIR < 0 means to search backwards through the history list, |
DIR >= 0 means to search forward. */ |
DIR >= 0 means to search forward. */ |
static int |
static int |
noninc_search_from_pos (string, pos, dir) | noninc_search_from_pos (char *string, int pos, int dir, int flags, int *ncp) |
char *string; | |
int pos, dir; | |
{ |
{ |
int ret, old; | int ret, old, sflags; |
| char *s; |
|
|
if (pos < 0) |
if (pos < 0) |
return -1; |
return -1; |
Line 127 noninc_search_from_pos (string, pos, dir)
|
Line 124 noninc_search_from_pos (string, pos, dir)
|
return -1; |
return -1; |
|
|
RL_SETSTATE(RL_STATE_SEARCH); |
RL_SETSTATE(RL_STATE_SEARCH); |
if (*string == '^') | /* These functions return the match offset in the line; history_offset gives |
| the matching line in the history list */ |
| if (flags & SF_PATTERN) |
| { |
| s = string; |
| sflags = 0; /* Non-anchored search */ |
| if (*s == '^') |
| { |
| sflags |= ANCHORED_SEARCH; |
| s++; |
| } |
| ret = _hs_history_patsearch (s, dir, sflags); |
| } |
| else if (*string == '^') |
ret = history_search_prefix (string + 1, dir); |
ret = history_search_prefix (string + 1, dir); |
else |
else |
ret = history_search (string, dir); |
ret = history_search (string, dir); |
RL_UNSETSTATE(RL_STATE_SEARCH); |
RL_UNSETSTATE(RL_STATE_SEARCH); |
|
|
|
if (ncp) |
|
*ncp = ret; /* caller will catch -1 to indicate no-op */ |
|
|
if (ret != -1) |
if (ret != -1) |
ret = where_history (); |
ret = where_history (); |
|
|
Line 144 noninc_search_from_pos (string, pos, dir)
|
Line 157 noninc_search_from_pos (string, pos, dir)
|
search is backwards through previous entries, else through subsequent |
search is backwards through previous entries, else through subsequent |
entries. Returns 1 if the search was successful, 0 otherwise. */ |
entries. Returns 1 if the search was successful, 0 otherwise. */ |
static int |
static int |
noninc_dosearch (string, dir) | noninc_dosearch (char *string, int dir, int flags) |
char *string; | |
int dir; | |
{ |
{ |
int oldpos, pos; | int oldpos, pos, ind; |
HIST_ENTRY *entry; |
HIST_ENTRY *entry; |
|
|
if (string == 0 || *string == '\0' || noninc_history_pos < 0) |
if (string == 0 || *string == '\0' || noninc_history_pos < 0) |
Line 157 noninc_dosearch (string, dir)
|
Line 168 noninc_dosearch (string, dir)
|
return 0; |
return 0; |
} |
} |
|
|
pos = noninc_search_from_pos (string, noninc_history_pos + dir, dir); | pos = noninc_search_from_pos (string, noninc_history_pos + dir, dir, flags, &ind); |
if (pos == -1) |
if (pos == -1) |
{ |
{ |
/* Search failed, current history position unchanged. */ |
/* Search failed, current history position unchanged. */ |
Line 181 noninc_dosearch (string, dir)
|
Line 192 noninc_dosearch (string, dir)
|
|
|
make_history_line_current (entry); |
make_history_line_current (entry); |
|
|
rl_point = 0; | if (_rl_enable_active_region && ((flags & SF_PATTERN) == 0) && ind > 0 && ind < rl_end) |
rl_mark = rl_end; | { |
| rl_point = ind; |
| rl_mark = ind + strlen (string); |
| if (rl_mark > rl_end) |
| rl_mark = rl_end; /* can't happen? */ |
| rl_activate_mark (); |
| } |
| else |
| { |
| rl_point = 0; |
| rl_mark = rl_end; |
| } |
|
|
rl_clear_message (); |
rl_clear_message (); |
return 1; |
return 1; |
} |
} |
|
|
static _rl_search_cxt * |
static _rl_search_cxt * |
_rl_nsearch_init (dir, pchar) | _rl_nsearch_init (int dir, int pchar) |
int dir, pchar; | |
{ |
{ |
_rl_search_cxt *cxt; |
_rl_search_cxt *cxt; |
char *p; |
char *p; |
Line 198 _rl_nsearch_init (dir, pchar)
|
Line 219 _rl_nsearch_init (dir, pchar)
|
cxt = _rl_scxt_alloc (RL_SEARCH_NSEARCH, 0); |
cxt = _rl_scxt_alloc (RL_SEARCH_NSEARCH, 0); |
if (dir < 0) |
if (dir < 0) |
cxt->sflags |= SF_REVERSE; /* not strictly needed */ |
cxt->sflags |= SF_REVERSE; /* not strictly needed */ |
|
#if defined (VI_MODE) |
|
if (VI_COMMAND_MODE() && (pchar == '?' || pchar == '/')) |
|
cxt->sflags |= SF_PATTERN; |
|
#endif |
|
|
cxt->direction = dir; |
cxt->direction = dir; |
cxt->history_pos = cxt->save_line; |
cxt->history_pos = cxt->save_line; |
Line 224 _rl_nsearch_init (dir, pchar)
|
Line 249 _rl_nsearch_init (dir, pchar)
|
return cxt; |
return cxt; |
} |
} |
|
|
static int | int |
_rl_nsearch_cleanup (cxt, r) | _rl_nsearch_cleanup (_rl_search_cxt *cxt, int r) |
_rl_search_cxt *cxt; | |
int r; | |
{ |
{ |
_rl_scxt_dispose (cxt, 0); |
_rl_scxt_dispose (cxt, 0); |
_rl_nscxt = 0; |
_rl_nscxt = 0; |
Line 238 _rl_nsearch_cleanup (cxt, r)
|
Line 261 _rl_nsearch_cleanup (cxt, r)
|
} |
} |
|
|
static void |
static void |
_rl_nsearch_abort (cxt) | _rl_nsearch_abort (_rl_search_cxt *cxt) |
_rl_search_cxt *cxt; | |
{ |
{ |
rl_maybe_unsave_line (); |
rl_maybe_unsave_line (); |
rl_clear_message (); |
rl_clear_message (); |
rl_point = cxt->save_point; |
rl_point = cxt->save_point; |
rl_mark = cxt->save_mark; |
rl_mark = cxt->save_mark; |
|
_rl_fix_point (1); |
rl_restore_prompt (); |
rl_restore_prompt (); |
|
|
RL_UNSETSTATE (RL_STATE_NSEARCH); |
RL_UNSETSTATE (RL_STATE_NSEARCH); |
Line 254 _rl_nsearch_abort (cxt)
|
Line 277 _rl_nsearch_abort (cxt)
|
if the caller should abort the search, 0 if we should break out of the |
if the caller should abort the search, 0 if we should break out of the |
loop, and 1 if we should continue to read characters. */ |
loop, and 1 if we should continue to read characters. */ |
static int |
static int |
_rl_nsearch_dispatch (cxt, c) | _rl_nsearch_dispatch (_rl_search_cxt *cxt, int c) |
_rl_search_cxt *cxt; | |
int c; | |
{ |
{ |
|
int n; |
|
|
|
if (c < 0) |
|
c = CTRL ('C'); |
|
|
switch (c) |
switch (c) |
{ |
{ |
case CTRL('W'): |
case CTRL('W'): |
Line 288 _rl_nsearch_dispatch (cxt, c)
|
Line 314 _rl_nsearch_dispatch (cxt, c)
|
_rl_nsearch_abort (cxt); |
_rl_nsearch_abort (cxt); |
return -1; |
return -1; |
|
|
|
case ESC: |
|
/* XXX - experimental code to allow users to bracketed-paste into the |
|
search string. Similar code is in isearch.c:_rl_isearch_dispatch(). |
|
The difference here is that the bracketed paste sometimes doesn't |
|
paste everything, so checking for the prefix and the suffix in the |
|
input queue doesn't work well. We just have to check to see if the |
|
number of chars in the input queue is enough for the bracketed paste |
|
prefix and hope for the best. */ |
|
if (_rl_enable_bracketed_paste && ((n = _rl_nchars_available ()) >= (BRACK_PASTE_SLEN-1))) |
|
{ |
|
if (_rl_read_bracketed_paste_prefix (c) == 1) |
|
rl_bracketed_paste_begin (1, c); |
|
else |
|
{ |
|
c = rl_read_key (); /* get the ESC that got pushed back */ |
|
_rl_insert_char (1, c); |
|
} |
|
} |
|
else |
|
_rl_insert_char (1, c); |
|
break; |
|
|
default: |
default: |
#if defined (HANDLE_MULTIBYTE) |
#if defined (HANDLE_MULTIBYTE) |
if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) |
if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) |
Line 299 _rl_nsearch_dispatch (cxt, c)
|
Line 347 _rl_nsearch_dispatch (cxt, c)
|
} |
} |
|
|
(*rl_redisplay_function) (); |
(*rl_redisplay_function) (); |
|
rl_deactivate_mark (); |
return 1; |
return 1; |
} |
} |
|
|
Line 307 _rl_nsearch_dispatch (cxt, c)
|
Line 356 _rl_nsearch_dispatch (cxt, c)
|
using _rl_nsearch_cleanup (). Returns 1 if the search was successful, |
using _rl_nsearch_cleanup (). Returns 1 if the search was successful, |
0 otherwise. */ |
0 otherwise. */ |
static int |
static int |
_rl_nsearch_dosearch (cxt) | _rl_nsearch_dosearch (_rl_search_cxt *cxt) |
_rl_search_cxt *cxt; | |
{ |
{ |
rl_mark = cxt->save_mark; |
rl_mark = cxt->save_mark; |
|
|
Line 341 _rl_nsearch_dosearch (cxt)
|
Line 389 _rl_nsearch_dosearch (cxt)
|
} |
} |
|
|
rl_restore_prompt (); |
rl_restore_prompt (); |
return (noninc_dosearch (noninc_search_string, cxt->direction)); | return (noninc_dosearch (noninc_search_string, cxt->direction, cxt->sflags&SF_PATTERN)); |
} |
} |
|
|
/* Search non-interactively through the history list. DIR < 0 means to |
/* Search non-interactively through the history list. DIR < 0 means to |
Line 350 _rl_nsearch_dosearch (cxt)
|
Line 398 _rl_nsearch_dosearch (cxt)
|
history list. PCHAR is the character to use for prompting when reading |
history list. PCHAR is the character to use for prompting when reading |
the search string; if not specified (0), it defaults to `:'. */ |
the search string; if not specified (0), it defaults to `:'. */ |
static int |
static int |
noninc_search (dir, pchar) | noninc_search (int dir, int pchar) |
int dir; | |
int pchar; | |
{ |
{ |
_rl_search_cxt *cxt; |
_rl_search_cxt *cxt; |
int c, r; |
int c, r; |
Line 368 noninc_search (dir, pchar)
|
Line 414 noninc_search (dir, pchar)
|
{ |
{ |
c = _rl_search_getchar (cxt); |
c = _rl_search_getchar (cxt); |
|
|
|
if (c < 0) |
|
{ |
|
_rl_nsearch_abort (cxt); |
|
return 1; |
|
} |
|
|
if (c == 0) |
if (c == 0) |
break; |
break; |
|
|
Line 385 noninc_search (dir, pchar)
|
Line 437 noninc_search (dir, pchar)
|
/* Search forward through the history list for a string. If the vi-mode |
/* Search forward through the history list for a string. If the vi-mode |
code calls this, KEY will be `?'. */ |
code calls this, KEY will be `?'. */ |
int |
int |
rl_noninc_forward_search (count, key) | rl_noninc_forward_search (int count, int key) |
int count, key; | |
{ |
{ |
return noninc_search (1, (key == '?') ? '?' : 0); |
return noninc_search (1, (key == '?') ? '?' : 0); |
} |
} |
Line 394 rl_noninc_forward_search (count, key)
|
Line 445 rl_noninc_forward_search (count, key)
|
/* Reverse search the history list for a string. If the vi-mode code |
/* Reverse search the history list for a string. If the vi-mode code |
calls this, KEY will be `/'. */ |
calls this, KEY will be `/'. */ |
int |
int |
rl_noninc_reverse_search (count, key) | rl_noninc_reverse_search (int count, int key) |
int count, key; | |
{ |
{ |
return noninc_search (-1, (key == '/') ? '/' : 0); |
return noninc_search (-1, (key == '/') ? '/' : 0); |
} |
} |
|
|
/* Search forward through the history list for the last string searched |
/* Search forward through the history list for the last string searched |
for. If there is no saved search string, abort. */ | for. If there is no saved search string, abort. If the vi-mode code |
| calls this, KEY will be `N'. */ |
int |
int |
rl_noninc_forward_search_again (count, key) | rl_noninc_forward_search_again (int count, int key) |
int count, key; | |
{ |
{ |
int r; |
int r; |
|
|
if (!noninc_search_string) |
if (!noninc_search_string) |
{ |
{ |
rl_ding (); |
rl_ding (); |
return (-1); | return (1); |
} |
} |
r = noninc_dosearch (noninc_search_string, 1); | #if defined (VI_MODE) |
| if (VI_COMMAND_MODE() && key == 'N') |
| r = noninc_dosearch (noninc_search_string, 1, SF_PATTERN); |
| else |
| #endif |
| r = noninc_dosearch (noninc_search_string, 1, 0); |
return (r != 1); |
return (r != 1); |
} |
} |
|
|
/* Reverse search in the history list for the last string searched |
/* Reverse search in the history list for the last string searched |
for. If there is no saved search string, abort. */ | for. If there is no saved search string, abort. If the vi-mode code |
| calls this, KEY will be `n'. */ |
int |
int |
rl_noninc_reverse_search_again (count, key) | rl_noninc_reverse_search_again (int count, int key) |
int count, key; | |
{ |
{ |
int r; |
int r; |
|
|
if (!noninc_search_string) |
if (!noninc_search_string) |
{ |
{ |
rl_ding (); |
rl_ding (); |
return (-1); | return (1); |
} |
} |
r = noninc_dosearch (noninc_search_string, -1); | #if defined (VI_MODE) |
| if (VI_COMMAND_MODE() && key == 'n') |
| r = noninc_dosearch (noninc_search_string, -1, SF_PATTERN); |
| else |
| #endif |
| r = noninc_dosearch (noninc_search_string, -1, 0); |
return (r != 1); |
return (r != 1); |
} |
} |
|
|
#if defined (READLINE_CALLBACKS) |
#if defined (READLINE_CALLBACKS) |
int |
int |
_rl_nsearch_callback (cxt) | _rl_nsearch_callback (_rl_search_cxt *cxt) |
_rl_search_cxt *cxt; | |
{ |
{ |
int c, r; |
int c, r; |
|
|
c = _rl_search_getchar (cxt); |
c = _rl_search_getchar (cxt); |
|
if (c <= 0) |
|
{ |
|
if (c < 0) |
|
_rl_nsearch_abort (cxt); |
|
return 1; |
|
} |
r = _rl_nsearch_dispatch (cxt, c); |
r = _rl_nsearch_dispatch (cxt, c); |
if (r != 0) |
if (r != 0) |
return 1; |
return 1; |
Line 452 _rl_nsearch_callback (cxt)
|
Line 517 _rl_nsearch_callback (cxt)
|
#endif |
#endif |
|
|
static int |
static int |
rl_history_search_internal (count, dir) | rl_history_search_internal (int count, int dir) |
int count, dir; | |
{ |
{ |
HIST_ENTRY *temp; |
HIST_ENTRY *temp; |
int ret, oldpos; | int ret, oldpos, newcol; |
char *t; |
char *t; |
|
|
rl_maybe_save_line (); |
rl_maybe_save_line (); |
Line 470 rl_history_search_internal (count, dir)
|
Line 534 rl_history_search_internal (count, dir)
|
while (count) |
while (count) |
{ |
{ |
RL_CHECK_SIGNALS (); |
RL_CHECK_SIGNALS (); |
ret = noninc_search_from_pos (history_search_string, rl_history_search_pos + dir, dir); | ret = noninc_search_from_pos (history_search_string, rl_history_search_pos + dir, dir, 0, &newcol); |
if (ret == -1) |
if (ret == -1) |
break; |
break; |
|
|
Line 513 rl_history_search_internal (count, dir)
|
Line 577 rl_history_search_internal (count, dir)
|
/* Copy the line we found into the current line buffer. */ |
/* Copy the line we found into the current line buffer. */ |
make_history_line_current (temp); |
make_history_line_current (temp); |
|
|
|
/* decide where to put rl_point -- need to change this for pattern search */ |
if (rl_history_search_flags & ANCHORED_SEARCH) |
if (rl_history_search_flags & ANCHORED_SEARCH) |
rl_point = rl_history_search_len; /* easy case */ |
rl_point = rl_history_search_len; /* easy case */ |
else |
else |
{ |
{ |
t = strstr (rl_line_buffer, history_search_string); | #if 0 |
| t = strstr (rl_line_buffer, history_search_string); /* XXX */ |
rl_point = t ? (int)(t - rl_line_buffer) + rl_history_search_len : rl_end; |
rl_point = t ? (int)(t - rl_line_buffer) + rl_history_search_len : rl_end; |
|
#else |
|
rl_point = (newcol >= 0) ? newcol : rl_end; |
|
#endif |
} |
} |
rl_mark = rl_end; |
rl_mark = rl_end; |
|
|
Line 526 rl_history_search_internal (count, dir)
|
Line 595 rl_history_search_internal (count, dir)
|
} |
} |
|
|
static void |
static void |
rl_history_search_reinit (flags) | rl_history_search_reinit (int flags) |
int flags; | |
{ |
{ |
int sind; |
int sind; |
|
|
Line 557 rl_history_search_reinit (flags)
|
Line 625 rl_history_search_reinit (flags)
|
from the start of the line to rl_point. This is a non-incremental |
from the start of the line to rl_point. This is a non-incremental |
search. The search is anchored to the beginning of the history line. */ |
search. The search is anchored to the beginning of the history line. */ |
int |
int |
rl_history_search_forward (count, ignore) | rl_history_search_forward (int count, int ignore) |
int count, ignore; | |
{ |
{ |
if (count == 0) |
if (count == 0) |
return (0); |
return (0); |
Line 576 rl_history_search_forward (count, ignore)
|
Line 643 rl_history_search_forward (count, ignore)
|
from the start of the line to rl_point. This is a non-incremental |
from the start of the line to rl_point. This is a non-incremental |
search. */ |
search. */ |
int |
int |
rl_history_search_backward (count, ignore) | rl_history_search_backward (int count, int ignore) |
int count, ignore; | |
{ |
{ |
if (count == 0) |
if (count == 0) |
return (0); |
return (0); |
Line 596 rl_history_search_backward (count, ignore)
|
Line 662 rl_history_search_backward (count, ignore)
|
search. The search succeeds if the search string is present anywhere |
search. The search succeeds if the search string is present anywhere |
in the history line. */ |
in the history line. */ |
int |
int |
rl_history_substr_search_forward (count, ignore) | rl_history_substr_search_forward (int count, int ignore) |
int count, ignore; | |
{ |
{ |
if (count == 0) |
if (count == 0) |
return (0); |
return (0); |
Line 615 rl_history_substr_search_forward (count, ignore)
|
Line 680 rl_history_substr_search_forward (count, ignore)
|
from the start of the line to rl_point. This is a non-incremental |
from the start of the line to rl_point. This is a non-incremental |
search. */ |
search. */ |
int |
int |
rl_history_substr_search_backward (count, ignore) | rl_history_substr_search_backward (int count, int ignore) |
int count, ignore; | |
{ |
{ |
if (count == 0) |
if (count == 0) |
return (0); |
return (0); |