commit da73d9ada93788bad37eb4648dfa2a0ac92c56ed
parent e59b83d4d95107f5eb632849bdc15c43c2d0852b
Author: Arun Prakash Jana <engineerarun@gmail.com>
Date: Sun, 5 Jan 2025 22:15:56 +0530
Merge pull request #1971 from doremiyeon/xreadline_by_word_movement
xreadline by word movement
Diffstat:
| M | src/nnn.c | | | 54 | +++++++++++++++++++++++++++++++++++++++++++++--------- |
1 file changed, 45 insertions(+), 9 deletions(-)
diff --git a/src/nnn.c b/src/nnn.c
@@ -100,6 +100,7 @@
#include <time.h>
#include <unistd.h>
#include <stddef.h>
+#include <wctype.h>
#include <stdalign.h>
#ifndef __USE_XOPEN_EXTENDED
#define __USE_XOPEN_EXTENDED 1
@@ -179,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')
@@ -3625,7 +3627,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];
@@ -3704,13 +3706,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);
@@ -3733,8 +3735,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;