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 if (text_iterator_byte_get(&it
, &c
) && c
== '\n')
22 while (text_iterator_byte_next(&it
, &c
) && c
== '\n');
24 it
= text_iterator_get(txt
, r
.end
);
25 while (text_iterator_char_prev(&it
, &c
) && 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 (pos
> 0 && 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_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)) {
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
);
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_boundary
);
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 static Filerange
text_object_bracket(Text
*txt
, size_t pos
, char type
) {
181 int opened
= 1, closed
= 1;
182 Filerange r
= text_range_empty();
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;
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
);
204 while (text_iterator_byte_get(&it
, &c
)) {
205 if (c
== open
&& --opened
== 0) {
206 r
.start
= it
.pos
+ 1;
208 } else if (c
== close
&& it
.pos
!= pos
) {
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) {
219 } else if (c
== open
&& it
.pos
!= pos
) {
222 text_iterator_byte_next(&it
, NULL
);
225 if (!text_range_valid(&r
))
226 return text_range_empty();
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
) {
260 size_t end
= text_size(txt
);
262 bool found
= start
< end
&& !text_search_range_forward(txt
, start
, end
- start
, regex
, 1, match
, 0);
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
) {
272 bool found
= !text_search_range_backward(txt
, start
, end
, regex
, 1, match
, 0);
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
) {
280 size_t bol
= text_line_begin(txt
, pos
);
281 size_t sol
= text_line_start(txt
, 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
);
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
)
301 bool empty
= text_byte_get(txt
, bol
, &c
) && c
== '\n';
302 if (line_empty
&& !empty
)
304 if (line_indent
== 0 && empty
)
306 text_bytes_get(txt
, bol
, line_indent
, tmp
);
307 if (memcmp(buf
, tmp
, line_indent
))
314 sol
= text_line_start(txt
, bol
);
315 size_t indent
= sol
- bol
;
316 if (indent
< line_indent
)
318 bool empty
= text_byte_get(txt
, bol
, &c
) && c
== '\n';
319 if (line_empty
&& !empty
)
321 if (line_indent
== 0 && empty
)
323 text_bytes_get(txt
, bol
, line_indent
, tmp
);
324 if (memcmp(buf
, tmp
, line_indent
))
326 end
= text_line_next(txt
, bol
);
327 } while (bol
!= end
);
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
);
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
) {
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
);
355 it
= text_iterator_get(txt
, rin
->end
);
356 do r
.end
= it
.pos
; while (text_iterator_byte_prev(&it
, &c
) && space(c
));