5 #include "text-motions.h"
6 #include "text-objects.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
) {
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
);
24 it
= text_iterator_get(txt
, r
.end
);
25 while (text_iterator_byte_prev(&it
, &c
) && (c
== '\r' || c
== '\n'));
27 return text_range_linewise(txt
, &r
);
30 static Filerange
text_object_customword(Text
*txt
, size_t pos
, int (*isboundary
)(int)) {
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
);
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
)) {
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
));
47 /* on a single character */
49 r
.end
= text_char_next(txt
, pos
);
51 } else if (boundary(prev
)) {
52 /* at start of a word */
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
);
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
));
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)) {
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
);
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
)) {
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
));
94 /* on a single character */
96 r
.end
= text_customword_start_next(txt
, pos
, isboundary
);
98 } else if (boundary(prev
)) {
99 /* at start of a word */
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
);
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
);
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
);
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
)
131 pos
= match_word
.end
;
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
);
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
)
148 return text_range_empty();
153 Filerange
text_object_line(Text
*txt
, size_t pos
) {
155 r
.start
= text_line_begin(txt
, pos
);
156 r
.end
= text_line_next(txt
, pos
);
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
) {
167 r
.start
= text_sentence_prev(txt
, pos
);
168 r
.end
= text_sentence_next(txt
, pos
);
172 Filerange
text_object_paragraph(Text
*txt
, size_t pos
) {
174 r
.start
= text_paragraph_prev(txt
, pos
);
175 r
.end
= text_paragraph_next(txt
, pos
);
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
))
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
) {
200 int opened
= 1, closed
= 1;
201 Filerange r
= text_range_empty();
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;
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
);
223 while (text_iterator_byte_get(&it
, &c
)) {
224 if (c
== open
&& --opened
== 0) {
225 r
.start
= it
.pos
+ 1;
227 } else if (c
== close
&& it
.pos
!= pos
) {
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) {
238 } else if (c
== open
&& it
.pos
!= pos
) {
241 text_iterator_byte_next(&it
, NULL
);
244 if (!text_range_valid(&r
))
245 return text_range_empty();
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)) {
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();
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
))
300 if (!(buf
= text_bytes_alloc0(txt
, r
.start
, text_range_size(&r
))))
301 return text_range_empty();
303 strtoll(buf
, &err
, 0);
304 if (errno
|| err
== buf
)
305 r
= text_range_empty();
307 r
.end
= r
.start
+ (err
- buf
);
312 static int is_filename_boundary(int c
) {
314 case ';': case ':': case '|':
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
) {
329 size_t end
= text_size(txt
);
331 bool found
= start
< end
&& !text_search_range_forward(txt
, start
, end
- start
, regex
, 1, match
, 0);
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
) {
341 bool found
= !text_search_range_backward(txt
, start
, end
, regex
, 1, match
, 0);
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
) {
349 size_t bol
= text_line_begin(txt
, pos
);
350 size_t sol
= text_line_start(txt
, 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
);
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
)
370 bool empty
= text_byte_get(txt
, bol
, &c
) && (c
== '\r' || c
== '\n');
371 if (line_empty
&& !empty
)
373 if (line_indent
== 0 && empty
)
375 text_bytes_get(txt
, bol
, line_indent
, tmp
);
376 if (memcmp(buf
, tmp
, line_indent
))
383 sol
= text_line_start(txt
, bol
);
384 size_t indent
= sol
- bol
;
385 if (indent
< line_indent
)
387 bool empty
= text_byte_get(txt
, bol
, &c
) && (c
== '\r' || c
== '\n');
388 if (line_empty
&& !empty
)
390 if (line_indent
== 0 && empty
)
392 text_bytes_get(txt
, bol
, line_indent
, tmp
);
393 if (memcmp(buf
, tmp
, line_indent
))
395 end
= text_line_next(txt
, bol
);
396 } while (bol
!= end
);
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
);
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
) {
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
);
424 it
= text_iterator_get(txt
, rin
->end
);
425 do r
.end
= it
.pos
; while (text_iterator_byte_prev(&it
, &c
) && space(c
));