aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config.def.h2
-rw-r--r--text-objects.c19
-rw-r--r--text-objects.h4
-rw-r--r--vis.c4
4 files changed, 29 insertions, 0 deletions
diff --git a/config.def.h b/config.def.h
index 910e1e7..a2eabee 100644
--- a/config.def.h
+++ b/config.def.h
@@ -170,6 +170,7 @@ static KeyBinding vis_textobjs[] = {
{ { NONE('a'), NONE('\'') }, textobj, { .i = TEXT_OBJ_OUTER_SINGLE_QUOTE } },
{ { 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 } },
{ /* empty last element, array terminator */ },
};
@@ -192,6 +193,7 @@ static KeyBinding vis_inner_textobjs[] = {
{ { NONE('i'), NONE('\'') }, textobj, { .i = TEXT_OBJ_INNER_SINGLE_QUOTE } },
{ { 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 } },
{ /* empty last element, array terminator */ },
};
diff --git a/text-objects.c b/text-objects.c
index 0308e8c..9f646dc 100644
--- a/text-objects.c
+++ b/text-objects.c
@@ -235,6 +235,25 @@ Filerange text_object_paragraph(Text *txt, size_t pos) {
return r;
}
+Filerange text_object_function(Text *txt, size_t pos) {
+ size_t a = text_function_start_prev(txt, pos);
+ size_t b = text_function_end_next(txt, pos);
+ if (text_function_end_next(txt, a) == b) {
+ Filerange r = text_range_new(a, b+1);
+ return text_range_linewise(txt, &r);
+ }
+ return text_range_empty();
+}
+
+Filerange text_object_function_inner(Text *txt, size_t pos) {
+ Filerange r = text_object_function(txt, pos);
+ if (!text_range_valid(&r))
+ return r;
+ size_t b = text_function_end_next(txt, pos);
+ size_t a = text_bracket_match(txt, b);
+ return text_range_new(a+1, b-1);
+}
+
static Filerange text_object_bracket(Text *txt, size_t pos, char type) {
char c, open, close;
int opened = 1, closed = 1;
diff --git a/text-objects.h b/text-objects.h
index fb35d13..1ff728a 100644
--- a/text-objects.h
+++ b/text-objects.h
@@ -29,6 +29,10 @@ Filerange text_object_line(Text*, size_t pos);
Filerange text_object_sentence(Text*, size_t pos);
Filerange text_object_paragraph(Text*, size_t pos);
+/* C like function definition */
+Filerange text_object_function(Text*, size_t pos);
+/* inner variant only contains the function body */
+Filerange text_object_function_inner(Text*, size_t pos);
/* these are inner text objects i.e. the delimiters themself are not
* included in the range */
Filerange text_object_square_bracket(Text*, size_t pos);
diff --git a/vis.c b/vis.c
index aae95f8..ba2e94d 100644
--- a/vis.c
+++ b/vis.c
@@ -257,6 +257,8 @@ enum {
TEXT_OBJ_INNER_BACKTICK,
TEXT_OBJ_OUTER_ENTIRE,
TEXT_OBJ_INNER_ENTIRE,
+ TEXT_OBJ_OUTER_FUNCTION,
+ TEXT_OBJ_INNER_FUNCTION,
};
static TextObject textobjs[] = {
@@ -282,6 +284,8 @@ static TextObject textobjs[] = {
[TEXT_OBJ_INNER_BACKTICK] = { text_object_backtick, INNER },
[TEXT_OBJ_OUTER_ENTIRE] = { text_object_entire, },
[TEXT_OBJ_INNER_ENTIRE] = { text_object_entire_inner, },
+ [TEXT_OBJ_OUTER_FUNCTION] = { text_object_function, },
+ [TEXT_OBJ_INNER_FUNCTION] = { text_object_function_inner, },
};
/** functions to be called from keybindings */