main: handle multi-byte characters when editing prompt
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Daniel Eklöf · committed by Daniel Eklöf · parent 99da78c · Browse files · 1 file·+48 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Note: assumes locale is utf-8
Diff stats
1 file+48 added−10 removed
- other:+48 added−10 removed
Provenance
- Commit
a099247- Tree
97ad030- Author
Daniel Eklöf <daniel@ekloef.se>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent
99da78c
1 file changed+48 added−10 removed
modified:main.c+48 added-10 removed
208 unmodified lines209 c->keep_running = false;210 }211 212 static size_t213+prompt_next_char(const struct prompt *prompt)214+{215+ const char *c = &prompt->text[prompt->cursor];216+ const char *end = prompt->text + strlen(prompt->text) + 1;217+218+ if (prompt->cursor >= strlen(prompt->text))219+ return prompt->cursor;220+221+ do {222+ c++;223+ } while (c < end && ((unsigned char)*c >> 6) == 2);224+225+ return c - prompt->text;226+}227+228+static size_t229+prompt_prev_char(const struct prompt *prompt)230+{231+ if (prompt->cursor == 0)232+ return 0;233+234+ const char *c = &prompt->text[prompt->cursor];235+236+ do {237+ c--;238+ } while (c > prompt->text && !((unsigned char)*c >> 7 == 0 ||239+ (unsigned char)*c >> 6 == 3));240+241+ assert(c >= prompt->text);242+ return c - prompt->text;243+}244+245+static size_t246 prompt_prev_word(const struct prompt *prompt)247 {248+ size_t prev_char = prompt_prev_char(prompt);215- const char *space = &prompt->text[prompt->cursor - 1];249+ const char *space = &prompt->text[prev_char];250 251 /* Ignore initial spaces */252 while (space >= prompt->text && *space == ' ')253 space--;114 unmodified lines368 }369 370 else if ((sym == XKB_KEY_b && effective_mods == ctrl) ||371 (sym == XKB_KEY_Left && effective_mods == 0)) {338- if (c->prompt.cursor > 0) {339- c->prompt.cursor--;372+ size_t new_cursor = prompt_prev_char(&c->prompt);373+ if (new_cursor != c->prompt.cursor) {374+ c->prompt.cursor = new_cursor;375 refresh(c);376 }377 }378 379 else if ((sym == XKB_KEY_f && effective_mods == ctrl) ||380 (sym == XKB_KEY_Right && effective_mods == 0)) {346- if (c->prompt.cursor + 1 <= strlen(c->prompt.text)) {347- c->prompt.cursor++;381+ size_t new_cursor = prompt_next_char(&c->prompt);382+ if (new_cursor != c->prompt.cursor) {383+ c->prompt.cursor = new_cursor;384 refresh(c);385 }386 }387 388 else if ((sym == XKB_KEY_d && effective_mods == ctrl) ||389 (sym == XKB_KEY_Delete && effective_mods == 0)) {390 if (c->prompt.cursor < strlen(c->prompt.text)) {391+ size_t next_char = prompt_next_char(&c->prompt);392 memmove(&c->prompt.text[c->prompt.cursor],356- &c->prompt.text[c->prompt.cursor + 1],357- strlen(c->prompt.text) - c->prompt.cursor);393+ &c->prompt.text[next_char],394+ strlen(c->prompt.text) - next_char + 1);395 update_matches(c);396 refresh(c);397 }398 }399 400 else if (sym == XKB_KEY_BackSpace && effective_mods == 0) {401 if (c->prompt.cursor > 0) {365- c->prompt.text[strlen(c->prompt.text) - 1] = '\0';366- c->prompt.cursor--;402+ size_t prev_char = prompt_prev_char(&c->prompt);403+ c->prompt.text[prev_char] = '\0';404+ c->prompt.cursor = prev_char;405 406 update_matches(c);407 refresh(c);408 }61 unmodified lines470 return;471 472 memmove(&new_text[c->prompt.cursor + count],473 &new_text[c->prompt.cursor],436- strlen(c->prompt.text) - c->prompt.cursor + 1);474+ strlen(new_text) - c->prompt.cursor + 1);475 memcpy(&new_text[c->prompt.cursor], buf, count);476 477 c->prompt.text = new_text;478 c->prompt.cursor += count;530 unmodified lines