2 #include "text-objects.h"
5 int vis_textobject_register(Vis
*vis
, int type
, void *data
, VisTextObjectFunction
*textobject
) {
7 TextObject
*obj
= calloc(1, sizeof *obj
);
11 obj
->user
= textobject
;
15 if (array_add_ptr(&vis
->textobjects
, obj
))
16 return LENGTH(vis_textobjects
) + array_length(&vis
->textobjects
) - 1;
21 bool vis_textobject(Vis
*vis
, enum VisTextObject id
) {
22 if (id
< LENGTH(vis_textobjects
))
23 vis
->action
.textobj
= &vis_textobjects
[id
];
25 vis
->action
.textobj
= array_get_ptr(&vis
->textobjects
, id
- LENGTH(vis_textobjects
));
26 if (!vis
->action
.textobj
)
32 static Filerange
search_forward(Vis
*vis
, Text
*txt
, size_t pos
) {
33 Filerange range
= text_range_empty();
34 Regex
*regex
= vis_regex(vis
, NULL
);
36 range
= text_object_search_forward(txt
, pos
, regex
);
37 text_regex_free(regex
);
41 static Filerange
search_backward(Vis
*vis
, Text
*txt
, size_t pos
) {
42 Filerange range
= text_range_empty();
43 Regex
*regex
= vis_regex(vis
, NULL
);
45 range
= text_object_search_backward(txt
, pos
, regex
);
46 text_regex_free(regex
);
50 static Filerange
object_unpaired(Text
*txt
, size_t pos
, char obj
) {
53 Iterator it
= text_iterator_get(txt
, pos
), rit
= it
;
55 while (text_iterator_byte_get(&rit
, &c
) && c
!= '\n') {
60 text_iterator_byte_prev(&rit
, NULL
);
63 /* if there is no previous occurrence on the same line, advance starting position */
65 while (text_iterator_byte_get(&it
, &c
) && c
!= '\n') {
70 text_iterator_byte_next(&it
, NULL
);
76 return text_object_quote(txt
, pos
);
78 return text_object_single_quote(txt
, pos
);
80 return text_object_backtick(txt
, pos
);
82 return text_range_empty();
86 static Filerange
object_quote(Text
*txt
, size_t pos
) {
87 return object_unpaired(txt
, pos
, '"');
90 static Filerange
object_single_quote(Text
*txt
, size_t pos
) {
91 return object_unpaired(txt
, pos
, '\'');
94 static Filerange
object_backtick(Text
*txt
, size_t pos
) {
95 return object_unpaired(txt
, pos
, '`');
98 const TextObject vis_textobjects
[] = {
99 [VIS_TEXTOBJECT_INNER_WORD
] = {
100 .txt
= text_object_word
,
102 [VIS_TEXTOBJECT_OUTER_WORD
] = {
103 .txt
= text_object_word_outer
,
105 [VIS_TEXTOBJECT_INNER_LONGWORD
] = {
106 .txt
= text_object_longword
,
108 [VIS_TEXTOBJECT_OUTER_LONGWORD
] = {
109 .txt
= text_object_longword_outer
,
111 [VIS_TEXTOBJECT_SENTENCE
] = {
112 .txt
= text_object_sentence
,
114 [VIS_TEXTOBJECT_PARAGRAPH
] = {
115 .txt
= text_object_paragraph
,
117 [VIS_TEXTOBJECT_PARAGRAPH_OUTER
] = {
118 .txt
= text_object_paragraph_outer
,
120 [VIS_TEXTOBJECT_OUTER_SQUARE_BRACKET
] = {
121 .txt
= text_object_square_bracket
,
122 .type
= TEXTOBJECT_DELIMITED_OUTER
,
124 [VIS_TEXTOBJECT_INNER_SQUARE_BRACKET
] = {
125 .txt
= text_object_square_bracket
,
126 .type
= TEXTOBJECT_DELIMITED_INNER
,
128 [VIS_TEXTOBJECT_OUTER_CURLY_BRACKET
] = {
129 .txt
= text_object_curly_bracket
,
130 .type
= TEXTOBJECT_DELIMITED_OUTER
,
132 [VIS_TEXTOBJECT_INNER_CURLY_BRACKET
] = {
133 .txt
= text_object_curly_bracket
,
134 .type
= TEXTOBJECT_DELIMITED_INNER
,
136 [VIS_TEXTOBJECT_OUTER_ANGLE_BRACKET
] = {
137 .txt
= text_object_angle_bracket
,
138 .type
= TEXTOBJECT_DELIMITED_OUTER
,
140 [VIS_TEXTOBJECT_INNER_ANGLE_BRACKET
] = {
141 .txt
= text_object_angle_bracket
,
142 .type
= TEXTOBJECT_DELIMITED_INNER
,
144 [VIS_TEXTOBJECT_OUTER_PARENTHESIS
] = {
145 .txt
= text_object_parenthesis
,
146 .type
= TEXTOBJECT_DELIMITED_OUTER
,
148 [VIS_TEXTOBJECT_INNER_PARENTHESIS
] = {
149 .txt
= text_object_parenthesis
,
150 .type
= TEXTOBJECT_DELIMITED_INNER
,
152 [VIS_TEXTOBJECT_OUTER_QUOTE
] = {
154 .type
= TEXTOBJECT_DELIMITED_OUTER
,
156 [VIS_TEXTOBJECT_INNER_QUOTE
] = {
158 .type
= TEXTOBJECT_DELIMITED_INNER
,
160 [VIS_TEXTOBJECT_OUTER_SINGLE_QUOTE
] = {
161 .txt
= object_single_quote
,
162 .type
= TEXTOBJECT_DELIMITED_OUTER
,
164 [VIS_TEXTOBJECT_INNER_SINGLE_QUOTE
] = {
165 .txt
= object_single_quote
,
166 .type
= TEXTOBJECT_DELIMITED_INNER
,
168 [VIS_TEXTOBJECT_OUTER_BACKTICK
] = {
169 .txt
= object_backtick
,
170 .type
= TEXTOBJECT_DELIMITED_OUTER
,
172 [VIS_TEXTOBJECT_INNER_BACKTICK
] = {
173 .txt
= object_backtick
,
174 .type
= TEXTOBJECT_DELIMITED_INNER
,
176 [VIS_TEXTOBJECT_OUTER_ENTIRE
] = {
177 .txt
= text_object_entire
,
179 [VIS_TEXTOBJECT_INNER_ENTIRE
] = {
180 .txt
= text_object_entire_inner
,
182 [VIS_TEXTOBJECT_OUTER_LINE
] = {
183 .txt
= text_object_line
,
185 [VIS_TEXTOBJECT_INNER_LINE
] = {
186 .txt
= text_object_line_inner
,
188 [VIS_TEXTOBJECT_INDENTATION
] = {
189 .txt
= text_object_indentation
,
191 [VIS_TEXTOBJECT_SEARCH_FORWARD
] = {
192 .vis
= search_forward
,
193 .type
= TEXTOBJECT_NON_CONTIGUOUS
|TEXTOBJECT_EXTEND_FORWARD
,
195 [VIS_TEXTOBJECT_SEARCH_BACKWARD
] = {
196 .vis
= search_backward
,
197 .type
= TEXTOBJECT_NON_CONTIGUOUS
|TEXTOBJECT_EXTEND_BACKWARD
,