From ef3ae4e03a064dbb27e204874b2e85ef5e06e15a Mon Sep 17 00:00:00 2001 From: Johannes Findeisen Date: Sat, 28 Nov 2026 23:59:39 +0100 Subject: [PATCH] Support Ctrl-arrow word navigation in input --- README.md | 1 + irc9.c | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f59f99a..7dd0b6f 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,7 @@ MODE, TOPIC, and INVITE events are shown in the relevant window. | Key | Action | | --- | --- | | `Tab` | complete a `/` command or a nick; press again to cycle matches (recently completed nicks come first) | +| `Ctrl+Left` / `Ctrl+Right` | move to the previous / next word in the input line | | `ctrl-n` / `ctrl-p` | next / previous window (tree order, wraps around) | | `PageUp` / `PageDown` | scroll the current window | | `ctrl-l` | clear the screen (history stays scrollable) | diff --git a/irc9.c b/irc9.c index d2ab28b..b6a5736 100644 --- a/irc9.c +++ b/irc9.c @@ -168,6 +168,7 @@ static long accent_color = -1; enum { KEY_UP = 256, KEY_DOWN, KEY_LEFT, KEY_RIGHT, + KEY_WORD_LEFT, KEY_WORD_RIGHT, KEY_HOME, KEY_END, KEY_PPAGE, KEY_NPAGE }; @@ -2053,6 +2054,18 @@ static void edit_line(int c, char text[MAXINPUT], int *len, int *pos) } if (c == KEY_LEFT) { if (*pos > 0) (*pos)--; input_dirty = 1; return; } if (c == KEY_RIGHT) { if (*pos < *len) (*pos)++; input_dirty = 1; return; } + if (c == KEY_WORD_LEFT) { + while (*pos > 0 && isspace((unsigned char)text[*pos - 1])) --*pos; + while (*pos > 0 && !isspace((unsigned char)text[*pos - 1])) --*pos; + input_dirty = 1; + return; + } + if (c == KEY_WORD_RIGHT) { + while (*pos < *len && !isspace((unsigned char)text[*pos])) ++*pos; + while (*pos < *len && isspace((unsigned char)text[*pos])) ++*pos; + input_dirty = 1; + return; + } if (c == KEY_HOME) { *pos = 0; input_dirty = 1; return; } if (c == KEY_END) { *pos = *len; input_dirty = 1; return; } if (((c >= 32 && c < 127) || (c >= 0x80 && c < 0x100)) && @@ -2624,7 +2637,7 @@ static void terminal_start(void) /* Decode both normal and application cursor keys, including split reads. */ static void terminal_read_keys(void) { - static int state, number, parameters; + static int state, number, parameters, modifier; static struct timespec last_escape; struct timespec now = {0}; clock_gettime(CLOCK_MONOTONIC, &now); @@ -2647,15 +2660,19 @@ static void terminal_read_keys(void) if (c == '[' || c == 'O') { state = c; number = parameters = 0; + modifier = -1; continue; } state = 0; } else if (state) { if (c >= '0' && c <= '9') { if (!parameters && number < 100) number = number * 10 + c - '0'; + else if (modifier >= 0 && modifier < 100) + modifier = modifier * 10 + c - '0'; continue; } if (c >= 0x20 && c <= 0x3f) { + modifier = (!parameters && c == ';') ? 0 : -1; parameters = 1; continue; } @@ -2670,11 +2687,12 @@ static void terminal_read_keys(void) continue; } if (sequence == -1) continue; + int ctrl_arrow = sequence == '[' && number == 1 && modifier == 5; switch (c) { case 'A': c = KEY_UP; break; case 'B': c = KEY_DOWN; break; - case 'C': c = KEY_RIGHT; break; - case 'D': c = KEY_LEFT; break; + case 'C': c = ctrl_arrow ? KEY_WORD_RIGHT : KEY_RIGHT; break; + case 'D': c = ctrl_arrow ? KEY_WORD_LEFT : KEY_LEFT; break; case 'H': c = KEY_HOME; break; case 'F': c = KEY_END; break; case '~':