Skip to content

Commit

Permalink
Merge pull request #1971 from doremiyeon/xreadline_by_word_movement
Browse files Browse the repository at this point in the history
xreadline by word movement
  • Loading branch information
jarun authored Jan 5, 2025
2 parents e59b83d + 942afdf commit da73d9a
Showing 1 changed file with 45 additions and 9 deletions.
54 changes: 45 additions & 9 deletions src/nnn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit da73d9a

Please sign in to comment.