summaryrefslogtreecommitdiffstats
path: root/linenoise
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2015-09-18 20:21:14 -0700
committerKaz Kylheku <kaz@kylheku.com>2015-09-18 20:21:14 -0700
commitd8ef238e540a00b77d7c66645b266922a5ecd6fa (patch)
treef8c363521fa6aa89c43ec0b3449cc92aa6dc4a95 /linenoise
parentf73367555810f19906e659622c3677eaa9cce59b (diff)
downloadtxr-d8ef238e540a00b77d7c66645b266922a5ecd6fa.tar.gz
txr-d8ef238e540a00b77d7c66645b266922a5ecd6fa.tar.bz2
txr-d8ef238e540a00b77d7c66645b266922a5ecd6fa.zip
linenoise: insert previous word feature
* linenoise/linenoise.c (edit): Ctrl-X Ctrl-W, or Ctrl-X w, with an optional number in between, cause a word from the previous line to be inserted. * txr.1: Documented.
Diffstat (limited to 'linenoise')
-rw-r--r--linenoise/linenoise.c47
1 files changed, 44 insertions, 3 deletions
diff --git a/linenoise/linenoise.c b/linenoise/linenoise.c
index d1973e94..0bdd14c1 100644
--- a/linenoise/linenoise.c
+++ b/linenoise/linenoise.c
@@ -1083,7 +1083,7 @@ static void edit_in_editor(lino_t *l) {
* The function returns the length of the current buffer. */
static int edit(lino_t *l, const char *prompt)
{
- int verbatim = 0, extended = 0, paste = 0;
+ int verbatim = 0, extended = 0, paste = 0, extend_num = -1;
/* Populate the linenoise state that we pass to functions implementing
* specific editing functionalities. */
@@ -1141,16 +1141,56 @@ static int edit(lino_t *l, const char *prompt)
}
if (extended) {
- extended = 0;
-
switch (c) {
case CTL('E'):
+ extended = 0;
edit_in_editor(l);
break;
case CTL('V'):
+ extended = 0;
paste = 1;
break;
+ case CTL('W'): case 'w':
+ extended = 0;
+ if (l->history_len > 1 && extend_num != 0) {
+ char *prev_line = l->history[l->history_len - 2];
+ char *word_end = prev_line + strlen(prev_line);
+ char *word_start = word_end;
+
+ if (extend_num < 0)
+ extend_num = 1;
+
+ for (; extend_num--; word_end = word_start) {
+ while (word_end > prev_line && isspace((unsigned char) word_end[-1]))
+ word_end--;
+
+ word_start = word_end;
+
+ while (word_start > prev_line && !isspace((unsigned char) word_start[-1]))
+ word_start--;
+
+ if (extend_num == 0)
+ break;
+ }
+
+ while (word_start < word_end)
+ if (edit_insert(l, *word_start++)) {
+ l->error = lino_ioerr;
+ return -1;
+ }
+
+ }
+ break;
default:
+ if (isdigit((unsigned char) c)) {
+ if (extend_num < 0)
+ extend_num = 0;
+ extend_num %= 100;
+ extend_num *= 10;
+ extend_num += (c - '0');
+ break;
+ }
+ extended = 0;
generate_beep(l);
break;
}
@@ -1312,6 +1352,7 @@ static int edit(lino_t *l, const char *prompt)
break;
case CTL('X'):
extended = 1;
+ extend_num = -1;
continue;
case CTL('K'): /* delete from current to end of line. */
l->data[l->dpos] = '\0';