vis: take symbolic keys into account when evaluating key prefixes
[vis.git] / text-objects.c
blob6c209f9f5a49014bce52b6c2722218da6223484a
1 #include <errno.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include "text-motions.h"
6 #include "text-objects.h"
7 #include "text-util.h"
8 #include "util.h"
10 #define space(c) (isspace((unsigned char)c))
11 #define boundary(c) (isboundary((unsigned char)c))
13 Filerange text_object_entire(Text *txt, size_t pos) {
14 return text_range_new(0, text_size(txt));
17 Filerange text_object_entire_inner(Text *txt, size_t pos) {
18 char c;
19 Filerange r = text_object_entire(txt, pos);
20 Iterator it = text_iterator_get(txt, r.start);
21 if (text_iterator_byte_get(&it, &c) && c == '\n')
22 while (text_iterator_byte_next(&it, &c) && c == '\n');
23 r.start = it.pos;
24 it = text_iterator_get(txt, r.end);
25 while (text_iterator_char_prev(&it, &c) && c == '\n');
26 r.end = it.pos;
27 return text_range_linewise(txt, &r);
30 static Filerange text_object_customword(Text *txt, size_t pos, int (*isboundary)(int)) {
31 Filerange r;
32 char c, prev = '0', next = '0';
33 Iterator it = text_iterator_get(txt, pos);
34 if (!text_iterator_byte_get(&it, &c))
35 return text_range_empty();
36 if (pos > 0 && text_iterator_byte_prev(&it, &prev))
37 text_iterator_byte_next(&it, NULL);
38 text_iterator_byte_next(&it, &next);
39 if (space(c)) {
40 r.start = text_char_next(txt, text_customword_end_prev(txt, pos, isboundary));
41 r.end = text_customword_start_next(txt, pos, isboundary);
42 } else if (boundary(prev) && boundary(next)) {
43 if (boundary(c)) {
44 r.start = text_char_next(txt, text_customword_end_prev(txt, pos, isboundary));
45 r.end = text_char_next(txt, text_customword_end_next(txt, pos, isboundary));
46 } else {
47 /* on a single character */
48 r.start = pos;
49 r.end = text_char_next(txt, pos);
51 } else if (boundary(prev)) {
52 /* at start of a word */
53 r.start = pos;
54 r.end = text_char_next(txt, text_customword_end_next(txt, pos, isboundary));
55 } else if (boundary(next)) {
56 /* at end of a word */
57 r.start = text_customword_start_prev(txt, pos, isboundary);
58 r.end = text_char_next(txt, pos);
59 } else {
60 /* in the middle of a word */
61 r.start = text_customword_start_prev(txt, pos, isboundary);
62 r.end = text_char_next(txt, text_customword_end_next(txt, pos, isboundary));
65 return r;
68 Filerange text_object_word(Text *txt, size_t pos) {
69 return text_object_customword(txt, pos, is_word_boundary);
72 Filerange text_object_longword(Text *txt, size_t pos) {
73 return text_object_customword(txt, pos, isspace);
76 static Filerange text_object_customword_outer(Text *txt, size_t pos, int (*isboundary)(int)) {
77 Filerange r;
78 char c, prev = '0', next = '0';
79 Iterator it = text_iterator_get(txt, pos);
80 if (!text_iterator_byte_get(&it, &c))
81 return text_range_empty();
82 if (pos > 0 && text_iterator_byte_prev(&it, &prev))
83 text_iterator_byte_next(&it, NULL);
84 text_iterator_byte_next(&it, &next);
85 if (space(c)) {
86 /* middle of two words, include leading white space */
87 r.start = text_char_next(txt, text_customword_end_prev(txt, pos, isboundary));
88 r.end = text_char_next(txt, text_customword_end_next(txt, pos, isboundary));
89 } else if (boundary(prev) && boundary(next)) {
90 if (boundary(c)) {
91 r.start = text_char_next(txt, text_customword_end_prev(txt, pos, isboundary));
92 r.end = text_word_start_next(txt, text_customword_end_next(txt, pos, isboundary));
93 } else {
94 /* on a single character */
95 r.start = pos;
96 r.end = text_customword_start_next(txt, pos, isboundary);
98 } else if (boundary(prev)) {
99 /* at start of a word */
100 r.start = pos;
101 r.end = text_customword_start_next(txt, text_customword_end_next(txt, pos, isboundary), isboundary);
102 } else if (boundary(next)) {
103 /* at end of a word */
104 r.start = text_customword_start_prev(txt, pos, isboundary);
105 r.end = text_customword_start_next(txt, pos, isboundary);
106 } else {
107 /* in the middle of a word */
108 r.start = text_customword_start_prev(txt, pos, isboundary);
109 r.end = text_customword_start_next(txt, text_customword_end_next(txt, pos, isboundary), isboundary);
112 return r;
115 Filerange text_object_longword_outer(Text *txt, size_t pos) {
116 return text_object_customword_outer(txt, pos, isspace);
119 Filerange text_object_word_outer(Text *txt, size_t pos) {
120 return text_object_customword_outer(txt, pos, is_word_boundary);
123 Filerange text_object_word_find_next(Text *txt, size_t pos, const char *word) {
124 size_t len = strlen(word);
125 for (;;) {
126 size_t match_pos = text_find_next(txt, pos, word);
127 if (match_pos != pos) {
128 Filerange match_word = text_object_word(txt, match_pos);
129 if (text_range_size(&match_word) == len)
130 return match_word;
131 pos = match_word.end;
132 } else {
133 return text_range_empty();
138 Filerange text_object_word_find_prev(Text *txt, size_t pos, const char *word) {
139 size_t len = strlen(word);
140 for (;;) {
141 size_t match_pos = text_find_prev(txt, pos, word);
142 if (match_pos != pos) {
143 Filerange match_word = text_object_word(txt, match_pos);
144 if (text_range_size(&match_word) == len)
145 return match_word;
146 pos = match_pos;
147 } else {
148 return text_range_empty();
153 Filerange text_object_line(Text *txt, size_t pos) {
154 Filerange r;
155 r.start = text_line_begin(txt, pos);
156 r.end = text_line_next(txt, pos);
157 return r;
160 Filerange text_object_line_inner(Text *txt, size_t pos) {
161 Filerange r = text_object_line(txt, pos);
162 return text_range_inner(txt, &r);
165 Filerange text_object_sentence(Text *txt, size_t pos) {
166 Filerange r;
167 r.start = text_sentence_prev(txt, pos);
168 r.end = text_sentence_next(txt, pos);
169 return r;
172 Filerange text_object_paragraph(Text *txt, size_t pos) {
173 Filerange r;
174 r.start = text_paragraph_prev(txt, pos);
175 r.end = text_paragraph_next(txt, pos);
176 return r;
179 static Filerange text_object_bracket(Text *txt, size_t pos, char type) {
180 char c, open, close;
181 int opened = 1, closed = 1;
182 Filerange r = text_range_empty();
184 switch (type) {
185 case '(': case ')': open = '('; close = ')'; break;
186 case '{': case '}': open = '{'; close = '}'; break;
187 case '[': case ']': open = '['; close = ']'; break;
188 case '<': case '>': open = '<'; close = '>'; break;
189 case '"': open = '"'; close = '"'; break;
190 case '`': open = '`'; close = '`'; break;
191 case '\'': open = '\''; close = '\''; break;
192 default: return r;
195 Iterator it = text_iterator_get(txt, pos);
197 if (open == close && text_iterator_byte_get(&it, &c) && (c == '"' || c == '`' || c == '\'')) {
198 size_t match = text_bracket_match(txt, pos);
199 r.start = MIN(pos, match) + 1;
200 r.end = MAX(pos, match);
201 return r;
204 while (text_iterator_byte_get(&it, &c)) {
205 if (c == open && --opened == 0) {
206 r.start = it.pos + 1;
207 break;
208 } else if (c == close && it.pos != pos) {
209 opened++;
211 text_iterator_byte_prev(&it, NULL);
214 it = text_iterator_get(txt, pos);
215 while (text_iterator_byte_get(&it, &c)) {
216 if (c == close && --closed == 0) {
217 r.end = it.pos;
218 break;
219 } else if (c == open && it.pos != pos) {
220 closed++;
222 text_iterator_byte_next(&it, NULL);
225 if (!text_range_valid(&r))
226 return text_range_empty();
227 return r;
230 Filerange text_object_square_bracket(Text *txt, size_t pos) {
231 return text_object_bracket(txt, pos, ']');
234 Filerange text_object_curly_bracket(Text *txt, size_t pos) {
235 return text_object_bracket(txt, pos, '}');
238 Filerange text_object_angle_bracket(Text *txt, size_t pos) {
239 return text_object_bracket(txt, pos, '>');
242 Filerange text_object_paranthese(Text *txt, size_t pos) {
243 return text_object_bracket(txt, pos, ')');
246 Filerange text_object_quote(Text *txt, size_t pos) {
247 return text_object_bracket(txt, pos, '"');
250 Filerange text_object_single_quote(Text *txt, size_t pos) {
251 return text_object_bracket(txt, pos, '\'');
254 Filerange text_object_backtick(Text *txt, size_t pos) {
255 return text_object_bracket(txt, pos, '`');
258 Filerange text_object_search_forward(Text *txt, size_t pos, Regex *regex) {
259 size_t start = pos;
260 size_t end = text_size(txt);
261 RegexMatch match[1];
262 bool found = start < end && !text_search_range_forward(txt, start, end - start, regex, 1, match, 0);
263 if (found)
264 return text_range_new(match[0].start, match[0].end);
265 return text_range_empty();
268 Filerange text_object_search_backward(Text *txt, size_t pos, Regex *regex) {
269 size_t start = 0;
270 size_t end = pos;
271 RegexMatch match[1];
272 bool found = !text_search_range_backward(txt, start, end, regex, 1, match, 0);
273 if (found)
274 return text_range_new(match[0].start, match[0].end);
275 return text_range_empty();
278 Filerange text_object_indentation(Text *txt, size_t pos) {
279 char c;
280 size_t bol = text_line_begin(txt, pos);
281 size_t sol = text_line_start(txt, bol);
282 size_t start = bol;
283 size_t end = text_line_next(txt, bol);
284 size_t line_indent = sol - bol;
285 bool line_empty = text_byte_get(txt, bol, &c) && c == '\n';
287 char *buf = text_bytes_alloc0(txt, bol, line_indent);
288 char *tmp = malloc(line_indent);
290 if (!buf || !tmp) {
291 free(buf);
292 free(tmp);
293 return text_range_empty();
296 while ((bol = text_line_begin(txt, text_line_prev(txt, start))) != start) {
297 sol = text_line_start(txt, bol);
298 size_t indent = sol - bol;
299 if (indent < line_indent)
300 break;
301 bool empty = text_byte_get(txt, bol, &c) && c == '\n';
302 if (line_empty && !empty)
303 break;
304 if (line_indent == 0 && empty)
305 break;
306 text_bytes_get(txt, bol, line_indent, tmp);
307 if (memcmp(buf, tmp, line_indent))
308 break;
309 start = bol;
312 do {
313 bol = end;
314 sol = text_line_start(txt, bol);
315 size_t indent = sol - bol;
316 if (indent < line_indent)
317 break;
318 bool empty = text_byte_get(txt, bol, &c) && c == '\n';
319 if (line_empty && !empty)
320 break;
321 if (line_indent == 0 && empty)
322 break;
323 text_bytes_get(txt, bol, line_indent, tmp);
324 if (memcmp(buf, tmp, line_indent))
325 break;
326 end = text_line_next(txt, bol);
327 } while (bol != end);
329 free(buf);
330 free(tmp);
331 return text_range_new(start, end);
334 Filerange text_range_linewise(Text *txt, Filerange *rin) {
335 Filerange rout = *rin;
336 rout.start = text_line_begin(txt, rin->start);
337 if (rin->end != text_line_begin(txt, rin->end))
338 rout.end = text_line_next(txt, rin->end);
339 return rout;
342 bool text_range_is_linewise(Text *txt, Filerange *r) {
343 return text_range_valid(r) &&
344 r->start == text_line_begin(txt, r->start) &&
345 r->end == text_line_begin(txt, r->end);
348 Filerange text_range_inner(Text *txt, Filerange *rin) {
349 char c;
350 Filerange r = *rin;
351 Iterator it = text_iterator_get(txt, rin->start);
352 while (text_iterator_byte_get(&it, &c) && space(c))
353 text_iterator_byte_next(&it, NULL);
354 r.start = it.pos;
355 it = text_iterator_get(txt, rin->end);
356 do r.end = it.pos; while (text_iterator_byte_prev(&it, &c) && space(c));
357 return r;