vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit 1736df4ed6f6267355a7f117b3ae5728b541e505 parent e6fcb7501dfc10a664fdd790e2af43f30a02ecac Author: Marc André Tanner <mat@brain-dump.org> Date: Tue, 23 Dec 2014 17:21:00 +0100 Distinct between inner and outer word text objects Diffstat:
| M | README | | | 2 | +- |
| M | config.def.h | | | 8 | ++++---- |
| M | text-objects.c | | | 84 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ |
| M | text-objects.h | | | 11 | +++++++---- |
| M | vis.c | | | 12 | ++++++++---- |
5 files changed, 98 insertions(+), 19 deletions(-)
diff --git a/README b/README @@ -371,7 +371,7 @@ and their current support in vis. p paragraph [,], (,), {,}, <,>, ", ', ` block enclosed by these symbols - For word, sentence and paragraph there is no difference between the + For sentence and paragraph there is no difference between the inner and normal variants. Modes diff --git a/config.def.h b/config.def.h @@ -160,8 +160,8 @@ static KeyBinding vis_movements[] = { }; static KeyBinding vis_textobjs[] = { - { { NONE('a'), NONE('w') }, textobj, { .i = TEXT_OBJ_WORD } }, - { { NONE('a'), NONE('W') }, textobj, { .i = TEXT_OBJ_LONGWORD } }, + { { NONE('a'), NONE('w') }, textobj, { .i = TEXT_OBJ_OUTER_WORD } }, + { { NONE('a'), NONE('W') }, textobj, { .i = TEXT_OBJ_OUTER_LONGWORD } }, { { NONE('a'), NONE('s') }, textobj, { .i = TEXT_OBJ_SENTENCE } }, { { NONE('a'), NONE('p') }, textobj, { .i = TEXT_OBJ_PARAGRAPH } }, { { NONE('a'), NONE('[') }, textobj, { .i = TEXT_OBJ_OUTER_SQUARE_BRACKET } }, @@ -181,8 +181,8 @@ static KeyBinding vis_textobjs[] = { }; static KeyBinding vis_inner_textobjs[] = { - { { NONE('i'), NONE('w') }, textobj, { .i = TEXT_OBJ_WORD } }, - { { NONE('i'), NONE('W') }, textobj, { .i = TEXT_OBJ_LONGWORD } }, + { { NONE('i'), NONE('w') }, textobj, { .i = TEXT_OBJ_INNER_WORD } }, + { { NONE('i'), NONE('W') }, textobj, { .i = TEXT_OBJ_INNER_LONGWORD } }, { { NONE('i'), NONE('s') }, textobj, { .i = TEXT_OBJ_SENTENCE } }, { { NONE('i'), NONE('p') }, textobj, { .i = TEXT_OBJ_PARAGRAPH } }, { { NONE('i'), NONE('[') }, textobj, { .i = TEXT_OBJ_INNER_SQUARE_BRACKET } }, diff --git a/text-objects.c b/text-objects.c @@ -18,6 +18,10 @@ #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'; @@ -28,6 +32,39 @@ Filerange text_object_longword(Text *txt, size_t pos) { 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); + 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_longword_end_prev(txt, pos)); r.end = text_char_next(txt, text_longword_end_next(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); diff --git a/text-objects.h b/text-objects.h @@ -9,12 +9,15 @@ #include <stddef.h> #include "text.h" -/* word which happens to be at pos, includes trailing white spaces. if at pos - * happens to be a whitespace include all neighbouring leading whitespaces - * and the following word. */ +/* word which happens to be at pos without any neighbouring white spaces */ Filerange text_object_word(Text*, size_t pos); -/* same semantics as above but for a longword (i.e. delimited by whitespaces) */ +/* includes trailing white spaces. if at pos happens to be a white space + * include all neighbouring leading white spaces and the following word. */ +Filerange text_object_word_outer(Text*, size_t pos); +/* same semantics as above but for a longword (i.e. delimited by white spaces) */ Filerange text_object_longword(Text*, size_t pos); +Filerange text_object_longword_outer(Text*, size_t pos); + Filerange text_object_line(Text*, size_t pos); Filerange text_object_sentence(Text*, size_t pos); Filerange text_object_paragraph(Text*, size_t pos); diff --git a/vis.c b/vis.c @@ -323,8 +323,10 @@ static Movement moves[] = { /* these can be passed as int argument to textobj(&(const Arg){ .i = TEXT_OBJ_* }) */ enum { - TEXT_OBJ_WORD, - TEXT_OBJ_LONGWORD, + TEXT_OBJ_INNER_WORD, + TEXT_OBJ_OUTER_WORD, + TEXT_OBJ_INNER_LONGWORD, + TEXT_OBJ_OUTER_LONGWORD, TEXT_OBJ_LINE_UP, TEXT_OBJ_LINE_DOWN, TEXT_OBJ_SENTENCE, @@ -346,8 +348,10 @@ enum { }; static TextObject textobjs[] = { - [TEXT_OBJ_WORD] = { text_object_word }, - [TEXT_OBJ_LONGWORD] = { text_object_longword }, + [TEXT_OBJ_INNER_WORD] = { text_object_word }, + [TEXT_OBJ_OUTER_WORD] = { text_object_word_outer }, + [TEXT_OBJ_INNER_LONGWORD] = { text_object_longword }, + [TEXT_OBJ_OUTER_LONGWORD] = { text_object_longword_outer }, [TEXT_OBJ_LINE_UP] = { text_object_line }, [TEXT_OBJ_LINE_DOWN] = { text_object_line }, [TEXT_OBJ_SENTENCE] = { text_object_sentence },