From d6b1b80e40b39adf1871fe8d1c18a370961efdef Mon Sep 17 00:00:00 2001 From: k-pop connoisseur Date: Wed, 11 Dec 2024 01:41:01 +0100 Subject: [PATCH 1/2] 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; From 942afdf4454743be6065e022fc31271c75c0f01c Mon Sep 17 00:00:00 2001 From: NRK Date: Mon, 16 Dec 2024 14:45:37 +0000 Subject: [PATCH 2/2] make Ctrl+w in xreadline similar to readline's when there's multiple spaces, the previous logic didn't erase them, e.g: a word | < before a word | < after Ctrl-w this patch brings the behavior closer to readline's: a word | < before a | < after Ctrl-w this also slightly changes the behavior since '/' is no longer considered a boundary. --- src/nnn.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/nnn.c b/src/nnn.c index c2b7e08e7..b0f2e0b28 100644 --- a/src/nnn.c +++ b/src/nnn.c @@ -180,6 +180,7 @@ #define MAX(x, y) ((x) > (y) ? (x) : (y)) #define ISODD(x) ((x) & 1) #define ISBLANK(x) ((x) == ' ' || (x) == '\t') +#define ISSPACE(x) (ISBLANK(x) || (x) == '\n' || (x) == '\r' || (x) == '\f' || (x) == '\v') #define TOUPPER(ch) (((ch) >= 'a' && (ch) <= 'z') ? ((ch) - 'a' + 'A') : (ch)) #define TOLOWER(ch) (((ch) >= 'A' && (ch) <= 'Z') ? ((ch) - 'A' + 'a') : (ch)) #define ISUPPER_(ch) ((ch) >= 'A' && (ch) <= 'Z') @@ -3717,13 +3718,13 @@ static char *xreadline(const char *prefill, const char *prompt) continue; case CONTROL('W'): printmsg(prompt); - do { - if (pos == 0) - break; - memmove(buf + pos - 1, buf + pos, - (len - pos) * WCHAR_T_WIDTH); - --pos, --len; - } while (buf[pos - 1] != ' ' && buf[pos - 1] != '/'); // NOLINT + lpos = pos; + while (pos > 0 && ISSPACE(buf[pos-1])) + --pos; + while (pos > 0 && !ISSPACE(buf[pos-1])) + --pos; + memmove(buf + pos, buf + lpos, (len - lpos) * WCHAR_T_WIDTH); + len -= lpos - pos; continue; case CONTROL('K'): printmsg(prompt);