From 985a828346a5d3185f6ee1e00d2ab4154024a9c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Tue, 23 Dec 2014 17:21:00 +0100 Subject: Distinct between inner and outer word text objects --- text-objects.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 78 insertions(+), 6 deletions(-) (limited to 'text-objects.c') diff --git a/text-objects.c b/text-objects.c index a8aef0a..44515e1 100644 --- a/text-objects.c +++ b/text-objects.c @@ -18,7 +18,44 @@ #include "text-objects.h" #include "util.h" +#define isboundry is_word_boundry + +/* TODO: reduce code duplication? */ + Filerange text_object_longword(Text *txt, size_t pos) { + Filerange r; + char c, prev = '0', next = '0'; + Iterator it = text_iterator_get(txt, pos); + if (!text_iterator_byte_get(&it, &c)) + return text_range_empty(); + if (text_iterator_byte_prev(&it, &prev)) + text_iterator_byte_next(&it, NULL); + text_iterator_byte_next(&it, &next); + if (isspace(c)) { + /* middle of two words */ + r.start = text_char_next(txt, text_longword_end_prev(txt, pos)); + r.end = text_longword_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_longword_end_next(txt, pos)); + } else if (isspace(next)) { + /* at end of a word */ + r.start = text_longword_start_prev(txt, pos); + r.end = text_char_next(txt, pos); + } else { + /* in the middle of a word */ + r.start = text_longword_start_prev(txt, pos); + r.end = text_char_next(txt, text_longword_end_next(txt, pos)); + } + return r; +} + +Filerange text_object_longword_outer(Text *txt, size_t pos) { Filerange r; char c, prev = '0', next = '0'; Iterator it = text_iterator_get(txt, pos); @@ -52,7 +89,6 @@ Filerange text_object_longword(Text *txt, size_t pos) { } Filerange text_object_word(Text *txt, size_t pos) { - #define isboundry is_word_boundry Filerange r; char c, prev = '0', next = '0'; Iterator it = text_iterator_get(txt, pos); @@ -62,14 +98,11 @@ Filerange text_object_word(Text *txt, size_t pos) { text_iterator_byte_next(&it, NULL); text_iterator_byte_next(&it, &next); if (isspace(c)) { - /* middle of two words, include leading white space */ r.start = text_char_next(txt, text_word_end_prev(txt, pos)); - r.end = text_word_end_next(txt, pos); - if (!text_byte_get(txt, r.end, &c) && !isboundry(c)) - r.end = text_char_next(txt, r.end); + r.end = text_word_start_next(txt, pos); } else if (isboundry(prev) && isboundry(next)) { if (isboundry(c)) { - r.start = text_word_end_prev(txt, pos); + r.start = text_char_next(txt, text_word_end_prev(txt, pos)); r.end = text_char_next(txt, text_word_end_next(txt, pos)); } else { /* on a single character */ @@ -93,6 +126,45 @@ Filerange text_object_word(Text *txt, size_t pos) { return r; } +Filerange text_object_word_outer(Text *txt, size_t pos) { + Filerange r; + char c, prev = '0', next = '0'; + Iterator it = text_iterator_get(txt, pos); + if (!text_iterator_byte_get(&it, &c)) + return text_range_empty(); + if (text_iterator_byte_prev(&it, &prev)) + text_iterator_byte_next(&it, NULL); + text_iterator_byte_next(&it, &next); + if (isspace(c)) { + /* middle of two words, include leading white space */ + r.start = text_char_next(txt, text_word_end_prev(txt, pos)); + r.end = text_word_end_next(txt, pos); + } else if (isboundry(prev) && isboundry(next)) { + if (isboundry(c)) { + r.start = text_char_next(txt, text_word_end_prev(txt, pos)); + r.end = text_word_start_next(txt, text_word_end_next(txt, pos)); + } else { + /* on a single character */ + r.start = pos; + r.end = text_char_next(txt, pos); + } + } else if (isboundry(prev)) { + /* at start of a word */ + r.start = pos; + r.end = text_word_start_next(txt, text_word_end_next(txt, pos)); + } else if (isboundry(next)) { + /* at end of a word */ + r.start = text_word_start_prev(txt, pos); + r.end = text_word_start_next(txt, pos); + } else { + /* in the middle of a word */ + r.start = text_word_start_prev(txt, pos); + r.end = text_word_start_next(txt, text_word_end_next(txt, pos)); + } + + return r; +} + Filerange text_object_line(Text *txt, size_t pos) { Filerange r; r.start = text_line_begin(txt, pos); -- cgit v1.2.3