From d6b1b80e40b39adf1871fe8d1c18a370961efdef Mon Sep 17 00:00:00 2001 From: k-pop connoisseur Date: Wed, 11 Dec 2024 01:41:01 +0100 Subject: [PATCH] extend xreadline() with some by-word movement added M-b, M-f, M-d, M-bspc according to GNU readline specifications. --- src/nnn.c | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/nnn.c b/src/nnn.c index d66dde7f1..c2b7e08e7 100644 --- a/src/nnn.c +++ b/src/nnn.c @@ -100,6 +100,7 @@ #include #include #include +#include #include #ifndef __USE_XOPEN_EXTENDED #define __USE_XOPEN_EXTENDED 1 @@ -3637,7 +3638,7 @@ static void addcmdtohist(char *cmd) /* Show a prompt with input string and return the changes */ static char *xreadline(const char *prefill, const char *prompt) { - size_t len, pos; + size_t len, pos, lpos; int x, r; const int WCHAR_T_WIDTH = sizeof(wchar_t); wint_t ch[1]; @@ -3745,8 +3746,42 @@ static char *xreadline(const char *prefill, const char *prompt) pos = 0; continue; case ESC: /* Exit prompt on Esc, but just filter out Alt+key */ - if (handle_alt_key(ch) != ERR) + if (handle_alt_key(ch) != ERR) { + switch (*ch) { + case 'd': + printmsg(prompt); + lpos = pos; + while (pos < len && !iswalnum(buf[pos + 1])) + ++pos; + while (pos < len && iswalnum(buf[++pos])); + memmove(buf + lpos, buf + pos, (len - pos) * WCHAR_T_WIDTH); + len -= pos - lpos; + pos = lpos; + continue; + case KEY_BACKSPACE: + printmsg(prompt); + lpos = pos; + while (pos > 0 && !iswalnum(buf[pos - 1])) + --pos; + while (pos > 0 && iswalnum(buf[pos - 1])) + --pos; + memmove(buf + pos, buf + lpos, (len - lpos) * WCHAR_T_WIDTH); + len -= lpos - pos; + continue; + case 'f': + while (pos < len && !iswalnum(buf[pos + 1])) + ++pos; + while (pos < len && iswalnum(buf[++pos])); + continue; + case 'b': + while (pos > 0 && !iswalnum(buf[pos - 1])) + --pos; + while (pos > 0 && iswalnum(buf[pos - 1])) + --pos; + continue; + } continue; + } len = 0; goto END;