vis-lua: add helper functions to implement motions/text objects in lua
[vis.git] / vis-text-objects.c
blobebb7384719daa9aeba06abb7b4cda3722233ea51
1 #include "vis-core.h"
2 #include "text-objects.h"
3 #include "util.h"
5 int vis_textobject_register(Vis *vis, int type, void *data,
6 Filerange (*textobject)(Vis*, Win*, void*, size_t pos)) {
8 TextObject *obj = calloc(1, sizeof *obj);
9 if (!obj)
10 return -1;
12 obj->user = textobject;
13 obj->type = type;
14 obj->data = data;
16 if (array_add(&vis->textobjects, obj))
17 return LENGTH(vis_textobjects) + array_length(&vis->textobjects) - 1;
18 free(obj);
19 return -1;
22 bool vis_textobject(Vis *vis, enum VisTextObject id) {
23 if (id < LENGTH(vis_textobjects))
24 vis->action.textobj = &vis_textobjects[id];
25 else
26 vis->action.textobj = array_get(&vis->textobjects, id - LENGTH(vis_textobjects));
27 if (!vis->action.textobj)
28 return false;
29 action_do(vis, &vis->action);
30 return true;
33 static Filerange search_forward(Vis *vis, Text *txt, size_t pos) {
34 return text_object_search_forward(txt, pos, vis->search_pattern);
37 static Filerange search_backward(Vis *vis, Text *txt, size_t pos) {
38 return text_object_search_backward(txt, pos, vis->search_pattern);
41 const TextObject vis_textobjects[] = {
42 [VIS_TEXTOBJECT_INNER_WORD] = { .txt = text_object_word },
43 [VIS_TEXTOBJECT_OUTER_WORD] = { .txt = text_object_word_outer },
44 [VIS_TEXTOBJECT_INNER_LONGWORD] = { .txt = text_object_longword },
45 [VIS_TEXTOBJECT_OUTER_LONGWORD] = { .txt = text_object_longword_outer },
46 [VIS_TEXTOBJECT_SENTENCE] = { .txt = text_object_sentence },
47 [VIS_TEXTOBJECT_PARAGRAPH] = { .txt = text_object_paragraph },
48 [VIS_TEXTOBJECT_OUTER_SQUARE_BRACKET] = { .txt = text_object_square_bracket, .type = OUTER },
49 [VIS_TEXTOBJECT_INNER_SQUARE_BRACKET] = { .txt = text_object_square_bracket, .type = INNER },
50 [VIS_TEXTOBJECT_OUTER_CURLY_BRACKET] = { .txt = text_object_curly_bracket, .type = OUTER },
51 [VIS_TEXTOBJECT_INNER_CURLY_BRACKET] = { .txt = text_object_curly_bracket, .type = INNER },
52 [VIS_TEXTOBJECT_OUTER_ANGLE_BRACKET] = { .txt = text_object_angle_bracket, .type = OUTER },
53 [VIS_TEXTOBJECT_INNER_ANGLE_BRACKET] = { .txt = text_object_angle_bracket, .type = INNER },
54 [VIS_TEXTOBJECT_OUTER_PARANTHESE] = { .txt = text_object_paranthese, .type = OUTER },
55 [VIS_TEXTOBJECT_INNER_PARANTHESE] = { .txt = text_object_paranthese, .type = INNER },
56 [VIS_TEXTOBJECT_OUTER_QUOTE] = { .txt = text_object_quote, .type = OUTER },
57 [VIS_TEXTOBJECT_INNER_QUOTE] = { .txt = text_object_quote, .type = INNER },
58 [VIS_TEXTOBJECT_OUTER_SINGLE_QUOTE] = { .txt = text_object_single_quote, .type = OUTER },
59 [VIS_TEXTOBJECT_INNER_SINGLE_QUOTE] = { .txt = text_object_single_quote, .type = INNER },
60 [VIS_TEXTOBJECT_OUTER_BACKTICK] = { .txt = text_object_backtick, .type = OUTER },
61 [VIS_TEXTOBJECT_INNER_BACKTICK] = { .txt = text_object_backtick, .type = INNER },
62 [VIS_TEXTOBJECT_OUTER_ENTIRE] = { .txt = text_object_entire, },
63 [VIS_TEXTOBJECT_INNER_ENTIRE] = { .txt = text_object_entire_inner, },
64 [VIS_TEXTOBJECT_OUTER_FUNCTION] = { .txt = text_object_function, },
65 [VIS_TEXTOBJECT_INNER_FUNCTION] = { .txt = text_object_function_inner, },
66 [VIS_TEXTOBJECT_OUTER_LINE] = { .txt = text_object_line, },
67 [VIS_TEXTOBJECT_INNER_LINE] = { .txt = text_object_line_inner, },
68 [VIS_TEXTOBJECT_INDENTATION] = { .txt = text_object_indentation, },
69 [VIS_TEXTOBJECT_SEARCH_FORWARD] = { .vis = search_forward, .type = SPLIT },
70 [VIS_TEXTOBJECT_SEARCH_BACKWARD] = { .vis = search_backward, .type = SPLIT },