Skip to content

Commit

Permalink
vis: implement gn and gN text objects
Browse files Browse the repository at this point in the history
The behaviour when no match is found is not yet optimal.
  • Loading branch information
martanne committed Feb 11, 2016
1 parent 979d51b commit a9db1ca
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config.def.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ static const KeyBinding bindings_textobjects[] = {
{ "ie", ACTION(TEXT_OBJECT_ENTIRE_INNER) },
{ "if", ACTION(TEXT_OBJECT_FUNCTION_INNER) },
{ "il", ACTION(TEXT_OBJECT_LINE_INNER) },
{ "gn", ACTION(TEXT_OBJECT_SEARCH_FORWARD) },
{ "gN", ACTION(TEXT_OBJECT_SEARCH_BACKWARD) },
{ 0 /* empty last element, array terminator */ },
};

Expand Down Expand Up @@ -235,6 +237,8 @@ static const KeyBinding bindings_normal[] = {
{ "<End>", ALIAS("$") },
{ "gf", ACTION(OPEN_FILE_UNDER_CURSOR) },
{ "<C-w>gf", ACTION(OPEN_FILE_UNDER_CURSOR_NEW_WINDOW) },
{ "gn", ALIAS("vgn") },
{ "gN", ALIAS("vgN") },
{ 0 /* empty last element, array terminator */ },
};

Expand Down
12 changes: 12 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ enum {
VIS_ACTION_TEXT_OBJECT_FUNCTION_INNER,
VIS_ACTION_TEXT_OBJECT_LINE_OUTER,
VIS_ACTION_TEXT_OBJECT_LINE_INNER,
VIS_ACTION_TEXT_OBJECT_SEARCH_FORWARD,
VIS_ACTION_TEXT_OBJECT_SEARCH_BACKWARD,
VIS_ACTION_MOTION_CHARWISE,
VIS_ACTION_MOTION_LINEWISE,
VIS_ACTION_UNICODE_INFO,
Expand Down Expand Up @@ -1050,6 +1052,16 @@ static KeyAction vis_action[] = {
"The whole line, excluding leading and trailing whitespace",
textobj, { .i = VIS_TEXTOBJECT_INNER_LINE }
},
[VIS_ACTION_TEXT_OBJECT_SEARCH_FORWARD] = {
"text-object-search-forward",
"The next search match in forward direction",
textobj, { .i = VIS_TEXTOBJECT_SEARCH_FORWARD }
},
[VIS_ACTION_TEXT_OBJECT_SEARCH_BACKWARD] = {
"text-object-search-backward",
"The next search match in backward direction",
textobj, { .i = VIS_TEXTOBJECT_SEARCH_BACKWARD }
},
[VIS_ACTION_MOTION_CHARWISE] = {
"motion-charwise",
"Force motion to be charwise",
Expand Down
20 changes: 20 additions & 0 deletions text-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,26 @@ Filerange text_object_filename(Text *txt, size_t pos) {
return text_object_range(txt, pos, is_filename_boundary);
}

Filerange text_object_search_forward(Text *txt, size_t pos, Regex *regex) {
size_t start = pos;
size_t end = text_size(txt);
RegexMatch match[1];
bool found = start < end && !text_search_range_forward(txt, start, end - start, regex, 1, match, 0);
if (found)
return text_range_new(match[0].start, match[0].end);
return text_range_empty();
}

Filerange text_object_search_backward(Text *txt, size_t pos, Regex *regex) {
size_t start = 0;
size_t end = pos;
RegexMatch match[1];
bool found = !text_search_range_backward(txt, start, end, regex, 1, match, 0);
if (found)
return text_range_new(match[0].start, match[0].end);
return text_range_empty();
}

Filerange text_range_linewise(Text *txt, Filerange *rin) {
Filerange rout = *rin;
rout.start = text_line_begin(txt, rin->start);
Expand Down
3 changes: 3 additions & 0 deletions text-objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ Filerange text_object_range(Text*, size_t pos, int (*isboundary)(int));
/* a number in either decimal, hex or octal format */
Filerange text_object_number(Text*, size_t pos);
Filerange text_object_filename(Text*, size_t pos);
/* match a search term in either forward or backward direction */
Filerange text_object_search_forward(Text*, size_t pos, Regex*);
Filerange text_object_search_backward(Text*, size_t pos, Regex*);

/* extend a range to cover whole lines */
Filerange text_range_linewise(Text*, Filerange*);
Expand Down
10 changes: 10 additions & 0 deletions vis-text-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ void vis_textobject(Vis *vis, enum VisTextObject id) {
}
}

static Filerange search_forward(Vis *vis, Text *txt, size_t pos) {
return text_object_search_forward(txt, pos, vis->search_pattern);
}

static Filerange search_backward(Vis *vis, Text *txt, size_t pos) {
return text_object_search_backward(txt, pos, vis->search_pattern);
}

TextObject vis_textobjects[] = {
[VIS_TEXTOBJECT_INNER_WORD] = { .txt = text_object_word },
[VIS_TEXTOBJECT_OUTER_WORD] = { .txt = text_object_word_outer },
Expand Down Expand Up @@ -36,5 +44,7 @@ TextObject vis_textobjects[] = {
[VIS_TEXTOBJECT_INNER_FUNCTION] = { .txt = text_object_function_inner, },
[VIS_TEXTOBJECT_OUTER_LINE] = { .txt = text_object_line, },
[VIS_TEXTOBJECT_INNER_LINE] = { .txt = text_object_line_inner, },
[VIS_TEXTOBJECT_SEARCH_FORWARD] = { .vis = search_forward, .type = SPLIT },
[VIS_TEXTOBJECT_SEARCH_BACKWARD] = { .vis = search_backward, .type = SPLIT },
};

2 changes: 2 additions & 0 deletions vis.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ enum VisTextObject {
VIS_TEXTOBJECT_INNER_FUNCTION,
VIS_TEXTOBJECT_OUTER_LINE,
VIS_TEXTOBJECT_INNER_LINE,
VIS_TEXTOBJECT_SEARCH_FORWARD,
VIS_TEXTOBJECT_SEARCH_BACKWARD,
VIS_TEXTOBJECT_INVALID,
};

Expand Down

0 comments on commit a9db1ca

Please sign in to comment.