mirror of
git://git.code.sf.net/p/zsh/code
synced 2025-09-29 06:51:03 +02:00
users/13902: make isearch return status useful
add LASTABORTEDSEARCH variable
This commit is contained in:
parent
1f37225708
commit
ea29159571
4 changed files with 83 additions and 25 deletions
|
@ -1,5 +1,9 @@
|
||||||
2009-03-13 Peter Stephenson <pws@csr.com>
|
2009-03-13 Peter Stephenson <pws@csr.com>
|
||||||
|
|
||||||
|
* users/13902: Doc/Zsh/zle.yo, Src/Zle/zle_hist.yo,
|
||||||
|
Src/Zle/zle_params.yo: improve return value from isearch functions
|
||||||
|
and add LASTABORTEDSEARCH variable.
|
||||||
|
|
||||||
* 26723: Completion/Unix/Type/.distfiles,
|
* 26723: Completion/Unix/Type/.distfiles,
|
||||||
Completion/Unix/Type/_have_glob_qual,
|
Completion/Unix/Type/_have_glob_qual,
|
||||||
Completion/Unix/Type_path_files: centralise test for whether
|
Completion/Unix/Type_path_files: centralise test for whether
|
||||||
|
@ -11384,5 +11388,5 @@
|
||||||
|
|
||||||
*****************************************************
|
*****************************************************
|
||||||
* This is used by the shell to define $ZSH_PATCHLEVEL
|
* This is used by the shell to define $ZSH_PATCHLEVEL
|
||||||
* $Revision: 1.4611 $
|
* $Revision: 1.4612 $
|
||||||
*****************************************************
|
*****************************************************
|
||||||
|
|
|
@ -721,10 +721,16 @@ effectively sets the maximum length of the kill ring, while the number of
|
||||||
non-zero strings gives the current length, both as seen by the user at the
|
non-zero strings gives the current length, both as seen by the user at the
|
||||||
command line.
|
command line.
|
||||||
)
|
)
|
||||||
|
vindex(LASTABORTEDSEARCH)
|
||||||
|
item(tt(LASTABORTEDSEARCH) (scalar))(
|
||||||
|
The last search string used by an interactive search that was
|
||||||
|
aborted by the user (status 3 returned by the search widget).
|
||||||
|
)
|
||||||
vindex(LASTSEARCH)
|
vindex(LASTSEARCH)
|
||||||
item(tt(LASTSEARCH) (scalar))(
|
item(tt(LASTSEARCH) (scalar))(
|
||||||
The last search string used by an interactive search ; read-only.
|
The last search string used by an interactive search; read-only.
|
||||||
|
This is set even if the search failed (status 0, 1 or 2 returned
|
||||||
|
by the search widget), but not if it was aborted by the user.
|
||||||
)
|
)
|
||||||
vindex(LASTWIDGET)
|
vindex(LASTWIDGET)
|
||||||
item(tt(LASTWIDGET) (scalar))(
|
item(tt(LASTWIDGET) (scalar))(
|
||||||
|
@ -1116,7 +1122,10 @@ item(tt(history-incremental-search-backward) (^R ^Xr) (unbound) (unbound))(
|
||||||
Search backward incrementally for a specified string. The search is
|
Search backward incrementally for a specified string. The search is
|
||||||
case-insensitive if the search string does not have uppercase letters and no
|
case-insensitive if the search string does not have uppercase letters and no
|
||||||
numeric argument was given. The string may begin with `tt(^)' to anchor the
|
numeric argument was given. The string may begin with `tt(^)' to anchor the
|
||||||
search to the beginning of the line.
|
search to the beginning of the line. When called from a user-defined
|
||||||
|
function returns the following statuses: 0, if the search succeeded;
|
||||||
|
1, if the search failed; 2, if the search term was a bad pattern;
|
||||||
|
3, if the search was aborted by the tt(send-break) command.
|
||||||
|
|
||||||
A restricted set of editing functions
|
A restricted set of editing functions
|
||||||
is available in the mini-buffer. Keys are looked up in the special
|
is available in the mini-buffer. Keys are looked up in the special
|
||||||
|
|
|
@ -44,10 +44,15 @@ int histline;
|
||||||
/* Previous search string use in an incremental search */
|
/* Previous search string use in an incremental search */
|
||||||
|
|
||||||
/**/
|
/**/
|
||||||
ZLE_STRING_T previous_search = NULL;
|
char *previous_search = NULL;
|
||||||
|
|
||||||
/**/
|
/**/
|
||||||
int previous_search_len = 0;
|
int previous_search_len;
|
||||||
|
|
||||||
|
/* Previous aborted search string use in an incremental search */
|
||||||
|
|
||||||
|
/**/
|
||||||
|
char *previous_aborted_search = NULL;
|
||||||
|
|
||||||
/* Local keymap in isearch mode */
|
/* Local keymap in isearch mode */
|
||||||
|
|
||||||
|
@ -891,32 +896,28 @@ zgetline(UNUSED(char **args))
|
||||||
int
|
int
|
||||||
historyincrementalsearchbackward(char **args)
|
historyincrementalsearchbackward(char **args)
|
||||||
{
|
{
|
||||||
doisearch(args, -1, 0);
|
return doisearch(args, -1, 0);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**/
|
/**/
|
||||||
int
|
int
|
||||||
historyincrementalsearchforward(char **args)
|
historyincrementalsearchforward(char **args)
|
||||||
{
|
{
|
||||||
doisearch(args, 1, 0);
|
return doisearch(args, 1, 0);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**/
|
/**/
|
||||||
int
|
int
|
||||||
historyincrementalpatternsearchbackward(char **args)
|
historyincrementalpatternsearchbackward(char **args)
|
||||||
{
|
{
|
||||||
doisearch(args, -1, 1);
|
return doisearch(args, -1, 1);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**/
|
/**/
|
||||||
int
|
int
|
||||||
historyincrementalpatternsearchforward(char **args)
|
historyincrementalpatternsearchforward(char **args)
|
||||||
{
|
{
|
||||||
doisearch(args, 1, 1);
|
return doisearch(args, 1, 1);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct isrch_spot {
|
static struct isrch_spot {
|
||||||
|
@ -1023,6 +1024,24 @@ isearch_newpos(LinkList matchlist, int curpos, int dir,
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Save an isearch buffer from sbuf to sbuf+sbptr
|
||||||
|
* into the string *search with length *searchlen.
|
||||||
|
* searchlen may be NULL; the string is a NULL-terminated metafied string.
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
save_isearch_buffer(char *sbuf, int sbptr,
|
||||||
|
char **search, int *searchlen)
|
||||||
|
{
|
||||||
|
if (*search)
|
||||||
|
free(*search);
|
||||||
|
*search = zalloc(sbptr+1);
|
||||||
|
memcpy(*search, sbuf, sbptr);
|
||||||
|
if (searchlen)
|
||||||
|
*searchlen = sbptr;
|
||||||
|
(*search)[sbptr] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
#define ISEARCH_PROMPT "XXXXXXX XXX-i-search: "
|
#define ISEARCH_PROMPT "XXXXXXX XXX-i-search: "
|
||||||
#define FAILING_TEXT "failing"
|
#define FAILING_TEXT "failing"
|
||||||
#define INVALID_TEXT "invalid"
|
#define INVALID_TEXT "invalid"
|
||||||
|
@ -1034,12 +1053,15 @@ isearch_newpos(LinkList matchlist, int curpos, int dir,
|
||||||
int isearch_active, isearch_startpos, isearch_endpos;
|
int isearch_active, isearch_startpos, isearch_endpos;
|
||||||
|
|
||||||
/**/
|
/**/
|
||||||
static void
|
static int
|
||||||
doisearch(char **args, int dir, int pattern)
|
doisearch(char **args, int dir, int pattern)
|
||||||
{
|
{
|
||||||
/* The full search buffer, including space for all prompts */
|
/* The full search buffer, including space for all prompts */
|
||||||
char *ibuf = zhalloc(80);
|
char *ibuf = zhalloc(80);
|
||||||
/* The part of the search buffer with the search string */
|
/*
|
||||||
|
* The part of the search buffer with the search string.
|
||||||
|
* This is a normal metafied string.
|
||||||
|
*/
|
||||||
char *sbuf = ibuf + FIRST_SEARCH_CHAR;
|
char *sbuf = ibuf + FIRST_SEARCH_CHAR;
|
||||||
/* The previous line shown to the user */
|
/* The previous line shown to the user */
|
||||||
char *last_line = NULL;
|
char *last_line = NULL;
|
||||||
|
@ -1141,9 +1163,13 @@ doisearch(char **args, int dir, int pattern)
|
||||||
* command line.
|
* command line.
|
||||||
*/
|
*/
|
||||||
ZleIntFunc exitfn = (ZleIntFunc)0;
|
ZleIntFunc exitfn = (ZleIntFunc)0;
|
||||||
|
/*
|
||||||
|
* Flag that the search was aborted.
|
||||||
|
*/
|
||||||
|
int aborted = 0;
|
||||||
|
|
||||||
if (!(he = quietgethist(hl)))
|
if (!(he = quietgethist(hl)))
|
||||||
return;
|
return 1;
|
||||||
|
|
||||||
selectlocalmap(isearch_keymap);
|
selectlocalmap(isearch_keymap);
|
||||||
|
|
||||||
|
@ -1446,6 +1472,9 @@ doisearch(char **args, int dir, int pattern)
|
||||||
zrefresh();
|
zrefresh();
|
||||||
if (!(cmd = getkeycmd()) || cmd == Th(z_sendbreak)) {
|
if (!(cmd = getkeycmd()) || cmd == Th(z_sendbreak)) {
|
||||||
int i;
|
int i;
|
||||||
|
aborted = 1;
|
||||||
|
save_isearch_buffer(sbuf, sbptr,
|
||||||
|
&previous_aborted_search, NULL);
|
||||||
get_isrch_spot(0, &hl, &pos, &pat_hl, &pat_pos, &end_pos,
|
get_isrch_spot(0, &hl, &pos, &pat_hl, &pat_pos, &end_pos,
|
||||||
&i, &sbptr, &dir, &nomatch);
|
&i, &sbptr, &dir, &nomatch);
|
||||||
he = quietgethist(hl);
|
he = quietgethist(hl);
|
||||||
|
@ -1597,8 +1626,12 @@ doisearch(char **args, int dir, int pattern)
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
ungetkeycmd();
|
ungetkeycmd();
|
||||||
if (cmd == Th(z_sendbreak))
|
if (cmd == Th(z_sendbreak)) {
|
||||||
|
aborted = 1;
|
||||||
|
save_isearch_buffer(sbuf, sbptr,
|
||||||
|
&previous_aborted_search, NULL);
|
||||||
sbptr = 0;
|
sbptr = 0;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ins:
|
ins:
|
||||||
|
@ -1629,9 +1662,8 @@ doisearch(char **args, int dir, int pattern)
|
||||||
feep = 0;
|
feep = 0;
|
||||||
}
|
}
|
||||||
if (sbptr) {
|
if (sbptr) {
|
||||||
zfree(previous_search, previous_search_len);
|
save_isearch_buffer(sbuf, sbptr,
|
||||||
previous_search = zalloc(sbptr);
|
&previous_search, &previous_search_len);
|
||||||
memcpy(previous_search, sbuf, previous_search_len = sbptr);
|
|
||||||
}
|
}
|
||||||
statusline = NULL;
|
statusline = NULL;
|
||||||
unmetafy_line();
|
unmetafy_line();
|
||||||
|
@ -1650,6 +1682,8 @@ doisearch(char **args, int dir, int pattern)
|
||||||
kungetct = savekeys;
|
kungetct = savekeys;
|
||||||
|
|
||||||
selectlocalmap(NULL);
|
selectlocalmap(NULL);
|
||||||
|
|
||||||
|
return aborted ? 3 : nomatch;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Histent
|
static Histent
|
||||||
|
|
|
@ -54,6 +54,8 @@ static const struct gsu_scalar keymap_gsu =
|
||||||
{ get_keymap, nullstrsetfn, zleunsetfn };
|
{ get_keymap, nullstrsetfn, zleunsetfn };
|
||||||
static const struct gsu_scalar keys_gsu =
|
static const struct gsu_scalar keys_gsu =
|
||||||
{ get_keys, nullstrsetfn, zleunsetfn };
|
{ get_keys, nullstrsetfn, zleunsetfn };
|
||||||
|
static const struct gsu_scalar lastabortedsearch_gsu =
|
||||||
|
{ get_lasearch, nullstrsetfn, zleunsetfn };
|
||||||
static const struct gsu_scalar lastsearch_gsu =
|
static const struct gsu_scalar lastsearch_gsu =
|
||||||
{ get_lsearch, nullstrsetfn, zleunsetfn };
|
{ get_lsearch, nullstrsetfn, zleunsetfn };
|
||||||
static const struct gsu_scalar lastwidget_gsu =
|
static const struct gsu_scalar lastwidget_gsu =
|
||||||
|
@ -115,6 +117,8 @@ static struct zleparam {
|
||||||
{ "KEYMAP", PM_SCALAR | PM_READONLY, GSU(keymap_gsu), NULL },
|
{ "KEYMAP", PM_SCALAR | PM_READONLY, GSU(keymap_gsu), NULL },
|
||||||
{ "KEYS", PM_SCALAR | PM_READONLY, GSU(keys_gsu), NULL },
|
{ "KEYS", PM_SCALAR | PM_READONLY, GSU(keys_gsu), NULL },
|
||||||
{ "killring", PM_ARRAY, GSU(killring_gsu), NULL },
|
{ "killring", PM_ARRAY, GSU(killring_gsu), NULL },
|
||||||
|
{ "LASTABORTEDSEARCH", PM_SCALAR | PM_READONLY, GSU(lastabortedsearch_gsu),
|
||||||
|
NULL },
|
||||||
{ "LASTSEARCH", PM_SCALAR | PM_READONLY, GSU(lastsearch_gsu), NULL },
|
{ "LASTSEARCH", PM_SCALAR | PM_READONLY, GSU(lastsearch_gsu), NULL },
|
||||||
{ "LASTWIDGET", PM_SCALAR | PM_READONLY, GSU(lastwidget_gsu), NULL },
|
{ "LASTWIDGET", PM_SCALAR | PM_READONLY, GSU(lastwidget_gsu), NULL },
|
||||||
{ "LBUFFER", PM_SCALAR, GSU(lbuffer_gsu), NULL },
|
{ "LBUFFER", PM_SCALAR, GSU(lbuffer_gsu), NULL },
|
||||||
|
@ -639,14 +643,21 @@ free_prepostdisplay(void)
|
||||||
set_prepost(&postdisplay, &postdisplaylen, NULL);
|
set_prepost(&postdisplay, &postdisplaylen, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**/
|
||||||
|
static char *
|
||||||
|
get_lasearch(UNUSED(Param pm))
|
||||||
|
{
|
||||||
|
if (previous_aborted_search)
|
||||||
|
return previous_aborted_search;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
/**/
|
/**/
|
||||||
static char *
|
static char *
|
||||||
get_lsearch(UNUSED(Param pm))
|
get_lsearch(UNUSED(Param pm))
|
||||||
{
|
{
|
||||||
if (previous_search_len) {
|
if (previous_search)
|
||||||
return zlelineasstring(previous_search, previous_search_len, 0,
|
return previous_search;
|
||||||
NULL, NULL, 1);
|
|
||||||
}
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue