From 191daea3627689121de3bac73961056426b943d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Thu, 28 Aug 2014 15:08:57 +0200 Subject: text-object: correctly detect word boundries --- text-objects.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) (limited to 'text-objects.c') diff --git a/text-objects.c b/text-objects.c index d6ac4a3..f1181ef 100644 --- 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; } -- cgit v1.2.3