From 51322f6c66d153d2f008a227c5e517f6fb34dbab Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 31 Dec 2015 05:30:46 -0800 Subject: linenoise: submit and stay in history. * linenoise/linenoise.c (struct lino_state): New member save_hist_idx. (edit): If save_hist_idx is set, jump to that history position and clear it. Handle ENTER in extended (Ctrl-X) mode similarly to regular ENTER, but setting save_hist_idx. * txr.1: Documented. --- linenoise/linenoise.c | 19 +++++++++++++++++++ txr.1 | 14 ++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/linenoise/linenoise.c b/linenoise/linenoise.c index 93f4bf6c..1dd7577d 100644 --- a/linenoise/linenoise.c +++ b/linenoise/linenoise.c @@ -89,6 +89,7 @@ struct lino_state { char *clip; /* Selection */ int ifd; /* Terminal stdin file descriptor. */ int ofd; /* Terminal stdout file descriptor. */ + int save_hist_idx; /* Jump to history position on entry into edit */ /* Volatile state pertaining to just one linenoise call */ char buf[LINENOISE_MAX_DISP]; /* Displayed line bufer. */ @@ -1676,6 +1677,16 @@ static int edit(lino_t *l, const char *prompt) l->error = lino_ioerr; return ret; } + + if (l->save_hist_idx) { + int hi = l->history_len - l->save_hist_idx - 1; + l->history_index = l->save_hist_idx; + l->save_hist_idx = 0; + strcpy(l->data, l->history[hi]); + l->dpos = l->dlen = strlen(l->data); + l->need_refresh = 1; + } + while(1) { unsigned char byte; int c; @@ -1814,6 +1825,14 @@ static int edit(lino_t *l, const char *prompt) clear_sel(l); } break; + case ENTER: + if (l->mlmode) + edit_move_end(l); + if (l->need_refresh) + refresh_line(l); + ret = l->len; + l->save_hist_idx = l->history_index; + goto out; default: if (isdigit((unsigned char) c)) { if (extend_num < 0) diff --git a/txr.1 b/txr.1 index bff5c833..9cf5c862 100644 --- a/txr.1 +++ b/txr.1 @@ -38256,6 +38256,20 @@ Navigating to a history line manually using the up and down arrow keys (or Ctrl-P/Ctrl-N) has the same net effect same as locating that line using Ctrl-R search. +.NP* Submit and Stay in History + +Normally when the Enter key is used on a recalled history line, +the next time the listener is re-entered, it jumps back to the +newest history position where a new line is about to be composed. + +The alternative command sequence Ctrl-X, Enter provides a useful alternative +behavior. After the submitted line is processed, the listener doesn't jump to +the newest history position. Instead, it stays in the history, advancing +forward by one position to the successor of the submitted line. + +Ctrl-X, Enter can be used to conveniently submit a range of lines +from the history, one by one, in their original order. + .NP* Insert Previous Word The equivalent command sequences Ctrl-X, w and Ctrl-X, Ctrl-W insert -- cgit v1.2.3