Skip to content

Commit

Permalink
Fix reverse-i-search behaviour
Browse files Browse the repository at this point in the history
Pressing Ctrl-R again while in reverse-i-search should search back
further, while up/down cursor should navigate to the adjacent lines
in history rather than searching for matches.

Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
  • Loading branch information
dwmw2 committed Dec 3, 2021
1 parent 76be485 commit ac8b56a
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions linenoise.c
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,7 @@ static int insert_chars(struct current *current, int pos, const char *chars)
/**
* Returns the keycode to process, or 0 if none.
*/
static int reverseIncrementalSearch(struct current *current)
static int reverseIncrementalSearch(struct current *current, int *history_index)
{
/* Display the reverse-i-search prompt and process chars */
char rbuf[50];
Expand Down Expand Up @@ -1480,20 +1480,38 @@ static int reverseIncrementalSearch(struct current *current)
c = check_special(current->fd);
}
#endif
if (c == ctrl('P') || c == SPECIAL_UP) {
if (c == ctrl('R')) {
/* Search for the previous (earlier) match */
if (searchpos > 0) {
searchpos--;
}
skipsame = 1;
}
else if (c == ctrl('P') || c == SPECIAL_UP) {
/* Exit Ctrl-R mode and go to the previous history line */
searchpos = history_len - 1 - *history_index;
if (searchpos > 0) {
searchpos--;
}
if (searchpos >= 0) {
*history_index = history_len - 1 - searchpos;
set_current(current,history[searchpos]);
}
c = 0;
break;
}
else if (c == ctrl('N') || c == SPECIAL_DOWN) {
/* Search for the next (later) match */
/* Exit Ctrl-R mode and go to the next history line */
searchpos = history_len - 1 - *history_index;
if (searchpos < history_len) {
searchpos++;
}
searchdir = 1;
skipsame = 1;
if (searchpos >= 0) {
*history_index = history_len - 1 - searchpos;
set_current(current,history[searchpos]);
}
c = 0;
break;
}
else if (c >= ' ') {
/* >= here to allow for null terminator */
Expand Down Expand Up @@ -1524,6 +1542,7 @@ static int reverseIncrementalSearch(struct current *current)
continue;
}
/* Copy the matching line and set the cursor position */
*history_index = history_len - 1 - searchpos;
set_current(current,history[searchpos]);
current->pos = utf8_strlen(history[searchpos], p - history[searchpos]);
break;
Expand All @@ -1539,13 +1558,14 @@ static int reverseIncrementalSearch(struct current *current)
if (c == ctrl('G') || c == ctrl('C')) {
/* ctrl-g terminates the search with no effect */
set_current(current, "");
*history_index = 0;
c = 0;
}
else if (c == ctrl('J')) {
/* ctrl-j terminates the search leaving the buffer in place */
*history_index = 0;
c = 0;
}

/* Go process the char normally */
refreshLine(current);
return c;
Expand Down Expand Up @@ -1575,7 +1595,7 @@ static int linenoiseEdit(struct current *current) {
#endif
if (c == ctrl('R')) {
/* reverse incremental search will provide an alternative keycode or 0 for none */
c = reverseIncrementalSearch(current);
c = reverseIncrementalSearch(current, &history_index);
/* go on to process the returned char normally */
}

Expand Down

0 comments on commit ac8b56a

Please sign in to comment.