vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit 9b5c6389c1e561e1016969ee60726209565ace65 parent 0726291fb97ecbc0045c651381eed8097dcc5642 Author: Marc André Tanner <mat@brain-dump.org> Date: Thu, 28 Aug 2014 15:08:57 +0200 text-object: correctly detect word boundries Diffstat:
| M | text-objects.c | | | 31 | ++++++++++++++++++++++++------- |
1 file changed, 24 insertions(+), 7 deletions(-)
diff --git a/text-objects.c b/text-objects.c @@ -7,18 +7,35 @@ static Filerange empty = { .end = -1, }; -// TODO: fix problems with inclusive / exclusive Filerange text_object_word(Text *txt, size_t pos) { - char c; + char c, prev = '0', next = '0'; Filerange r; - if (!text_byte_get(txt, pos, &c)) + Iterator it = text_iterator_get(txt, pos); + if (!text_iterator_byte_get(&it, &c)) return empty; - if (!isspace(c)) { + if (text_iterator_byte_prev(&it, &prev)) + text_iterator_byte_next(&it, NULL); + text_iterator_byte_next(&it, &next); + if (isspace(c)) { + /* we are in the middle of two words */ + r.start = text_char_next(txt, text_word_end_prev(txt, pos)); + r.end = text_word_start_next(txt, pos); + } else if (isspace(prev) && isspace(next)) { + /* on a single character */ + r.start = pos; + r.end = text_char_next(txt, pos); + } else if (isspace(prev)) { + /* at start of a word */ + r.start = pos; + r.end = text_char_next(txt, text_word_end_next(txt, pos)); + } else if (isspace(next)) { + /* at end of a word */ r.start = text_word_start_prev(txt, pos); - r.end = text_word_end_next(txt, pos); + r.end = text_char_next(txt, pos); } else { - r.start = text_word_end_prev(txt, pos); - r.end = text_word_start_next(txt, pos); + /* in the middle of a word */ + r.start = text_word_start_prev(txt, pos); + r.end = text_char_next(txt, text_word_end_next(txt, pos)); } return r; }