vis: remove __DATE__ and __TIME__ references to aid with reproducible builds
[vis.git] / text-objects.c
blobb0351cc6fecfe539e0e69feb141b04b96fd8bbb5
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 while (text_iterator_byte_get(&it, &c) && (c == '\r' || c == '\n'))
22 text_iterator_byte_next(&it, NULL);
23 r.start = it.pos;
24 it = text_iterator_get(txt, r.end);
25 while (text_iterator_byte_prev(&it, &c) && (c == '\r' || 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 (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_boundry);
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 (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_boundry);
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 Filerange text_object_function(Text *txt, size_t pos) {
180 size_t a = text_function_start_prev(txt, pos);
181 size_t b = text_function_end_next(txt, pos);
182 if (text_function_end_next(txt, a) == b) {
183 Filerange r = text_range_new(a, b+1);
184 return text_range_linewise(txt, &r);
186 return text_range_empty();
189 Filerange text_object_function_inner(Text *txt, size_t pos) {
190 Filerange r = text_object_function(txt, pos);
191 if (!text_range_valid(&r))
192 return r;
193 size_t b = text_function_end_next(txt, pos);
194 size_t a = text_bracket_match(txt, b);
195 return text_range_new(a+1, b-1);
198 static Filerange text_object_bracket(Text *txt, size_t pos, char type) {
199 char c, open, close;
200 int opened = 1, closed = 1;
201 Filerange r = text_range_empty();
203 switch (type) {
204 case '(': case ')': open = '('; close = ')'; break;
205 case '{': case '}': open = '{'; close = '}'; break;
206 case '[': case ']': open = '['; close = ']'; break;
207 case '<': case '>': open = '<'; close = '>'; break;
208 case '"': open = '"'; close = '"'; break;
209 case '`': open = '`'; close = '`'; break;
210 case '\'': open = '\''; close = '\''; break;
211 default: return r;
214 Iterator it = text_iterator_get(txt, pos);
216 if (open == close && text_iterator_byte_get(&it, &c) && (c == '"' || c == '`' || c == '\'')) {
217 size_t match = text_bracket_match(txt, pos);
218 r.start = MIN(pos, match) + 1;
219 r.end = MAX(pos, match);
220 return r;
223 while (text_iterator_byte_get(&it, &c)) {
224 if (c == open && --opened == 0) {
225 r.start = it.pos + 1;
226 break;
227 } else if (c == close && it.pos != pos) {
228 opened++;
230 text_iterator_byte_prev(&it, NULL);
233 it = text_iterator_get(txt, pos);
234 while (text_iterator_byte_get(&it, &c)) {
235 if (c == close && --closed == 0) {
236 r.end = it.pos;
237 break;
238 } else if (c == open && it.pos != pos) {
239 closed++;
241 text_iterator_byte_next(&it, NULL);
244 if (!text_range_valid(&r))
245 return text_range_empty();
246 return r;
249 Filerange text_object_square_bracket(Text *txt, size_t pos) {
250 return text_object_bracket(txt, pos, ']');
253 Filerange text_object_curly_bracket(Text *txt, size_t pos) {
254 return text_object_bracket(txt, pos, '}');
257 Filerange text_object_angle_bracket(Text *txt, size_t pos) {
258 return text_object_bracket(txt, pos, '>');
261 Filerange text_object_paranthese(Text *txt, size_t pos) {
262 return text_object_bracket(txt, pos, ')');
265 Filerange text_object_quote(Text *txt, size_t pos) {
266 return text_object_bracket(txt, pos, '"');
269 Filerange text_object_single_quote(Text *txt, size_t pos) {
270 return text_object_bracket(txt, pos, '\'');
273 Filerange text_object_backtick(Text *txt, size_t pos) {
274 return text_object_bracket(txt, pos, '`');
277 Filerange text_object_range(Text *txt, size_t pos, int (*isboundary)(int)) {
278 char c;
279 size_t start;
280 Iterator it = text_iterator_get(txt, pos), rit = it;
281 if (!text_iterator_byte_get(&rit, &c) || boundary(c))
282 return text_range_empty();
283 char tmp = c;
284 do start = rit.pos; while (text_iterator_char_prev(&rit, &c) && !boundary(c));
285 for (c = tmp; !boundary(c) && text_iterator_byte_next(&it, &c););
286 return text_range_new(start, it.pos);
289 static int is_number(int c) {
290 return !(c == '-' || c == 'x' || c == 'X' ||
291 ('0' <= c && c <= '9') ||
292 ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F'));
295 Filerange text_object_number(Text *txt, size_t pos) {
296 char *buf, *err = NULL;
297 Filerange r = text_object_range(txt, pos, is_number);
298 if (!text_range_valid(&r))
299 return r;
300 if (!(buf = text_bytes_alloc0(txt, r.start, text_range_size(&r))))
301 return text_range_empty();
302 errno = 0;
303 strtoll(buf, &err, 0);
304 if (errno || err == buf)
305 r = text_range_empty();
306 else
307 r.end = r.start + (err - buf);
308 free(buf);
309 return r;
312 static int is_filename_boundary(int c) {
313 switch (c) {
314 case ';': case ':': case '|':
315 case '"': case '\'':
316 case '<': case '>':
317 return true;
318 default:
319 return isspace(c);
323 Filerange text_object_filename(Text *txt, size_t pos) {
324 return text_object_range(txt, pos, is_filename_boundary);
327 Filerange text_object_search_forward(Text *txt, size_t pos, Regex *regex) {
328 size_t start = pos;
329 size_t end = text_size(txt);
330 RegexMatch match[1];
331 bool found = start < end && !text_search_range_forward(txt, start, end - start, regex, 1, match, 0);
332 if (found)
333 return text_range_new(match[0].start, match[0].end);
334 return text_range_empty();
337 Filerange text_object_search_backward(Text *txt, size_t pos, Regex *regex) {
338 size_t start = 0;
339 size_t end = pos;
340 RegexMatch match[1];
341 bool found = !text_search_range_backward(txt, start, end, regex, 1, match, 0);
342 if (found)
343 return text_range_new(match[0].start, match[0].end);
344 return text_range_empty();
347 Filerange text_object_indentation(Text *txt, size_t pos) {
348 char c;
349 size_t bol = text_line_begin(txt, pos);
350 size_t sol = text_line_start(txt, bol);
351 size_t start = bol;
352 size_t end = text_line_next(txt, bol);
353 size_t line_indent = sol - bol;
354 bool line_empty = text_byte_get(txt, bol, &c) && (c == '\r' || c == '\n');
356 char *buf = text_bytes_alloc0(txt, bol, line_indent);
357 char *tmp = malloc(line_indent);
359 if (!buf || !tmp) {
360 free(buf);
361 free(tmp);
362 return text_range_empty();
365 while ((bol = text_line_begin(txt, text_line_prev(txt, start))) != start) {
366 sol = text_line_start(txt, bol);
367 size_t indent = sol - bol;
368 if (indent < line_indent)
369 break;
370 bool empty = text_byte_get(txt, bol, &c) && (c == '\r' || c == '\n');
371 if (line_empty && !empty)
372 break;
373 if (line_indent == 0 && empty)
374 break;
375 text_bytes_get(txt, bol, line_indent, tmp);
376 if (memcmp(buf, tmp, line_indent))
377 break;
378 start = bol;
381 do {
382 bol = end;
383 sol = text_line_start(txt, bol);
384 size_t indent = sol - bol;
385 if (indent < line_indent)
386 break;
387 bool empty = text_byte_get(txt, bol, &c) && (c == '\r' || c == '\n');
388 if (line_empty && !empty)
389 break;
390 if (line_indent == 0 && empty)
391 break;
392 text_bytes_get(txt, bol, line_indent, tmp);
393 if (memcmp(buf, tmp, line_indent))
394 break;
395 end = text_line_next(txt, bol);
396 } while (bol != end);
398 free(buf);
399 free(tmp);
400 return text_range_new(start, end);
403 Filerange text_range_linewise(Text *txt, Filerange *rin) {
404 Filerange rout = *rin;
405 rout.start = text_line_begin(txt, rin->start);
406 if (rin->end != text_line_begin(txt, rin->end))
407 rout.end = text_line_next(txt, rin->end);
408 return rout;
411 bool text_range_is_linewise(Text *txt, Filerange *r) {
412 return text_range_valid(r) &&
413 r->start == text_line_begin(txt, r->start) &&
414 r->end == text_line_begin(txt, r->end);
417 Filerange text_range_inner(Text *txt, Filerange *rin) {
418 char c;
419 Filerange r = *rin;
420 Iterator it = text_iterator_get(txt, rin->start);
421 while (text_iterator_byte_get(&it, &c) && space(c))
422 text_iterator_byte_next(&it, NULL);
423 r.start = it.pos;
424 it = text_iterator_get(txt, rin->end);
425 do r.end = it.pos; while (text_iterator_byte_prev(&it, &c) && space(c));
426 return r;