aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2014-08-28 15:08:57 +0200
committerMarc André Tanner <mat@brain-dump.org>2014-08-28 15:08:57 +0200
commit191daea3627689121de3bac73961056426b943d3 (patch)
treee032ccf4a9e3af62b2c944b728ecd68c6ecba972
parente2662dc195e8666beb5bed4cf0f50cfeb5e3df10 (diff)
downloadvis-191daea3627689121de3bac73961056426b943d3.tar.gz
vis-191daea3627689121de3bac73961056426b943d3.tar.xz
text-object: correctly detect word boundries
-rw-r--r--text-objects.c31
1 files changed, 24 insertions, 7 deletions
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;
}