man: remove authors section from tool's man pages
[vis.git] / vis-text-objects.c
blobb3ed4632b4c47b6e9f124d6c54dcdc2633010aa5
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_ptr(&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_ptr(&vis->textobjects, id - LENGTH(vis_textobjects));
27 if (!vis->action.textobj)
28 return false;
29 vis_do(vis);
30 return true;
33 static Filerange search_forward(Vis *vis, Text *txt, size_t pos) {
34 Filerange range = text_range_empty();
35 Regex *regex = vis_regex(vis, NULL);
36 if (regex)
37 range = text_object_search_forward(txt, pos, regex);
38 text_regex_free(regex);
39 return range;
42 static Filerange search_backward(Vis *vis, Text *txt, size_t pos) {
43 Filerange range = text_range_empty();
44 Regex *regex = vis_regex(vis, NULL);
45 if (regex)
46 range = text_object_search_backward(txt, pos, regex);
47 text_regex_free(regex);
48 return range;
51 static Filerange object_unpaired(Text *txt, size_t pos, char obj) {
52 char c;
53 bool before = false;
54 Iterator it = text_iterator_get(txt, pos), rit = it;
56 while (text_iterator_byte_get(&rit, &c) && c != '\n') {
57 if (c == obj) {
58 before = true;
59 break;
61 text_iterator_byte_prev(&rit, NULL);
64 /* if there is no previous occurence on the same line, advance starting position */
65 if (!before) {
66 while (text_iterator_byte_get(&it, &c) && c != '\n') {
67 if (c == obj) {
68 pos = it.pos;
69 break;
71 text_iterator_byte_next(&it, NULL);
75 switch (obj) {
76 case '"':
77 return text_object_quote(txt, pos);
78 case '\'':
79 return text_object_single_quote(txt, pos);
80 case '`':
81 return text_object_backtick(txt, pos);
82 default:
83 return text_range_empty();
87 static Filerange object_quote(Text *txt, size_t pos) {
88 return object_unpaired(txt, pos, '"');
91 static Filerange object_single_quote(Text *txt, size_t pos) {
92 return object_unpaired(txt, pos, '\'');
95 static Filerange object_backtick(Text *txt, size_t pos) {
96 return object_unpaired(txt, pos, '`');
99 const TextObject vis_textobjects[] = {
100 [VIS_TEXTOBJECT_INNER_WORD] = { .txt = text_object_word },
101 [VIS_TEXTOBJECT_OUTER_WORD] = { .txt = text_object_word_outer },
102 [VIS_TEXTOBJECT_INNER_LONGWORD] = { .txt = text_object_longword },
103 [VIS_TEXTOBJECT_OUTER_LONGWORD] = { .txt = text_object_longword_outer },
104 [VIS_TEXTOBJECT_SENTENCE] = { .txt = text_object_sentence },
105 [VIS_TEXTOBJECT_PARAGRAPH] = { .txt = text_object_paragraph },
106 [VIS_TEXTOBJECT_OUTER_SQUARE_BRACKET] = { .txt = text_object_square_bracket, .type = OUTER },
107 [VIS_TEXTOBJECT_INNER_SQUARE_BRACKET] = { .txt = text_object_square_bracket, .type = INNER },
108 [VIS_TEXTOBJECT_OUTER_CURLY_BRACKET] = { .txt = text_object_curly_bracket, .type = OUTER },
109 [VIS_TEXTOBJECT_INNER_CURLY_BRACKET] = { .txt = text_object_curly_bracket, .type = INNER },
110 [VIS_TEXTOBJECT_OUTER_ANGLE_BRACKET] = { .txt = text_object_angle_bracket, .type = OUTER },
111 [VIS_TEXTOBJECT_INNER_ANGLE_BRACKET] = { .txt = text_object_angle_bracket, .type = INNER },
112 [VIS_TEXTOBJECT_OUTER_PARANTHESE] = { .txt = text_object_paranthese, .type = OUTER },
113 [VIS_TEXTOBJECT_INNER_PARANTHESE] = { .txt = text_object_paranthese, .type = INNER },
114 [VIS_TEXTOBJECT_OUTER_QUOTE] = { .txt = object_quote, .type = OUTER },
115 [VIS_TEXTOBJECT_INNER_QUOTE] = { .txt = object_quote, .type = INNER },
116 [VIS_TEXTOBJECT_OUTER_SINGLE_QUOTE] = { .txt = object_single_quote, .type = OUTER },
117 [VIS_TEXTOBJECT_INNER_SINGLE_QUOTE] = { .txt = object_single_quote, .type = INNER },
118 [VIS_TEXTOBJECT_OUTER_BACKTICK] = { .txt = object_backtick, .type = OUTER },
119 [VIS_TEXTOBJECT_INNER_BACKTICK] = { .txt = object_backtick, .type = INNER },
120 [VIS_TEXTOBJECT_OUTER_ENTIRE] = { .txt = text_object_entire, },
121 [VIS_TEXTOBJECT_INNER_ENTIRE] = { .txt = text_object_entire_inner, },
122 [VIS_TEXTOBJECT_OUTER_FUNCTION] = { .txt = text_object_function, },
123 [VIS_TEXTOBJECT_INNER_FUNCTION] = { .txt = text_object_function_inner, },
124 [VIS_TEXTOBJECT_OUTER_LINE] = { .txt = text_object_line, },
125 [VIS_TEXTOBJECT_INNER_LINE] = { .txt = text_object_line_inner, },
126 [VIS_TEXTOBJECT_INDENTATION] = { .txt = text_object_indentation, },
127 [VIS_TEXTOBJECT_SEARCH_FORWARD] = { .vis = search_forward, .type = SPLIT },
128 [VIS_TEXTOBJECT_SEARCH_BACKWARD] = { .vis = search_backward, .type = SPLIT },