vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit ce960e8ca543f8e422b62ce52aea62641b977e07 parent e981e18cc33f42fddfdd11e7c8cef7ac934a7c25 Author: Marc André Tanner <mat@brain-dump.org> Date: Thu, 6 Aug 2015 11:08:04 +0200 vis: implement al and il text objects Diffstat:
| M | README.md | | | 13 | +++++++++---- |
| M | config.def.h | | | 2 | ++ |
| M | text-objects.c | | | 17 | +++++++++++++++++ |
| M | text-objects.h | | | 3 | +++ |
| M | vis.c | | | 4 | ++++ |
5 files changed, 35 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md @@ -321,10 +321,15 @@ Operators can be forced to work line wise by specifying `V`. For sentence and paragraph there is no difference between the inner and normal variants. - Furthermore `ae` covers the entire content of a file, whereas `ie` - does not include leading and trailing empty lines. `af` tries to - match C-like function definitions, `if` only covers the function - body. + Additionally the following text objects, which are not part of stock vim + are also supported: + + ae entire file content + ie entire file content except for leading and trailing empty lines + af C-like function definition including immeadiately preceding comments + if C-like function definition only function body + al current line + il current line without leading and trailing white spaces ### Modes diff --git a/config.def.h b/config.def.h @@ -171,6 +171,7 @@ static KeyBinding vis_textobjs[] = { { { NONE('a'), NONE('`') }, textobj, { .i = TEXT_OBJ_OUTER_BACKTICK } }, { { NONE('a'), NONE('e') }, textobj, { .i = TEXT_OBJ_OUTER_ENTIRE } }, { { NONE('a'), NONE('f') }, textobj, { .i = TEXT_OBJ_OUTER_FUNCTION } }, + { { NONE('a'), NONE('l') }, textobj, { .i = TEXT_OBJ_OUTER_LINE } }, { /* empty last element, array terminator */ }, }; @@ -194,6 +195,7 @@ static KeyBinding vis_inner_textobjs[] = { { { NONE('i'), NONE('`') }, textobj, { .i = TEXT_OBJ_INNER_BACKTICK } }, { { NONE('i'), NONE('e') }, textobj, { .i = TEXT_OBJ_INNER_ENTIRE } }, { { NONE('i'), NONE('f') }, textobj, { .i = TEXT_OBJ_INNER_FUNCTION } }, + { { NONE('i'), NONE('l') }, textobj, { .i = TEXT_OBJ_INNER_LINE } }, { /* empty last element, array terminator */ }, }; diff --git a/text-objects.c b/text-objects.c @@ -221,6 +221,11 @@ Filerange text_object_line(Text *txt, size_t pos) { return r; } +Filerange text_object_line_inner(Text *txt, size_t pos) { + Filerange r = text_object_line(txt, pos); + return text_range_inner(txt, &r); +} + Filerange text_object_sentence(Text *txt, size_t pos) { Filerange r; r.start = text_sentence_prev(txt, pos); @@ -346,3 +351,15 @@ bool text_range_is_linewise(Text *txt, Filerange *r) { r->start == text_line_begin(txt, r->start) && r->end == text_line_begin(txt, r->end); } + +Filerange text_range_inner(Text *txt, Filerange *rin) { + char c; + Filerange r = *rin; + Iterator it = text_iterator_get(txt, rin->start); + while (text_iterator_byte_get(&it, &c) && isspace((unsigned char)c)) + text_iterator_byte_next(&it, NULL); + r.start = it.pos; + it = text_iterator_get(txt, rin->end); + do r.end = it.pos; while (text_iterator_byte_prev(&it, &c) && isspace((unsigned char)c)); + return r; +} diff --git a/text-objects.h b/text-objects.h @@ -26,6 +26,7 @@ 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_line_inner(Text*, size_t pos); Filerange text_object_sentence(Text*, size_t pos); Filerange text_object_paragraph(Text*, size_t pos); @@ -45,6 +46,8 @@ Filerange text_object_backtick(Text*, size_t pos); /* extend a range to cover whole lines */ Filerange text_range_linewise(Text*, Filerange*); +/* trim leading and trailing white spaces from range */ +Filerange text_range_inner(Text*, Filerange*); /* test whether a given range covers whole lines */ bool text_range_is_linewise(Text*, Filerange*); diff --git a/vis.c b/vis.c @@ -266,6 +266,8 @@ enum { TEXT_OBJ_INNER_ENTIRE, TEXT_OBJ_OUTER_FUNCTION, TEXT_OBJ_INNER_FUNCTION, + TEXT_OBJ_OUTER_LINE, + TEXT_OBJ_INNER_LINE, }; static TextObject textobjs[] = { @@ -293,6 +295,8 @@ static TextObject textobjs[] = { [TEXT_OBJ_INNER_ENTIRE] = { text_object_entire_inner, }, [TEXT_OBJ_OUTER_FUNCTION] = { text_object_function, }, [TEXT_OBJ_INNER_FUNCTION] = { text_object_function_inner, }, + [TEXT_OBJ_OUTER_LINE] = { text_object_line, }, + [TEXT_OBJ_INNER_LINE] = { text_object_line_inner, }, }; /** functions to be called from keybindings */