diff options
| -rw-r--r-- | main.c | 46 |
1 files changed, 34 insertions, 12 deletions
@@ -50,18 +50,36 @@ txt_delete_byte(DynStr *s, int i, int n) return i - n; } +static inline int +utf8contp(unsigned char c) +{ + return (c & 0xc0) == 0x80; +} + int txt_delete(DynStr *s, int i, int n) { while (n--) { - i = txt_delete_byte(s, i, 1); - while (i > 0 && (s->v[i-1] & 0x80)) + while (i > 0 && utf8contp(s->v[i-1])) i = txt_delete_byte(s, i, 1); + i = txt_delete_byte(s, i, 1); } return i; } int +read_all(FILE *f, DynStr *out, Arena *a) +{ + char buf[256]; + while (!feof(f)) { + size_t n = fread(buf, 1, sizeof buf, f); + if (ferror(f)) return -1; + DA_APUSH_MULT(out, a, buf, n); + } + return 0; +} + +int delete_word(DynStr *s, int i) { while (i > 0 && is_space(s->v[i-1])) @@ -92,15 +110,19 @@ next_word(DynStr *s, int i) } int -read_all(FILE *f, DynStr *out, Arena *a) +prev_char(DynStr *s, int i) { - char buf[256]; - while (!feof(f)) { - size_t n = fread(buf, 1, sizeof buf, f); - if (ferror(f)) return -1; - DA_APUSH_MULT(out, a, buf, n); - } - return 0; + while (i > 0 && utf8contp(s->v[i-1])) i--; + if (i > 0) i--; + return i; +} + +int +next_char(DynStr *s, int i) +{ + if (i < s->n) i++; + while (i < s->n && utf8contp(s->v[i])) i++; + return i; } int @@ -166,13 +188,13 @@ main(int argc, char **argv) if (ev.key.mod & UIM_CTRL) inpi = prev_word(&input, inpi); else if (inpi > 0) - inpi--; + inpi = prev_char(&input, inpi); goto draw; case UIK_RIGHT: if (ev.key.mod & UIM_CTRL) inpi = next_word(&input, inpi); else if (inpi < input.n) - inpi++; + inpi = next_char(&input, inpi); goto draw; case UIK_HOME: if (ev.key.mod & UIM_CTRL) |
