forked from martanne/vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-objects.c
348 lines (315 loc) · 10.4 KB
/
text-objects.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* Copyright (c) 2014 Marc André Tanner <mat at brain-dump.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <string.h>
#include <ctype.h>
#include "text-motions.h"
#include "text-objects.h"
#include "text-util.h"
#include "util.h"
#define isboundry is_word_boundry
Filerange text_object_entire(Text *txt, size_t pos) {
return text_range_new(0, text_size(txt));
}
Filerange text_object_entire_inner(Text *txt, size_t pos) {
char c;
Filerange r = text_object_entire(txt, pos);
Iterator it = text_iterator_get(txt, r.start);
while (text_iterator_byte_get(&it, &c) && (c == '\r' || c == '\n'))
text_iterator_byte_next(&it, NULL);
r.start = it.pos;
it = text_iterator_get(txt, r.end);
while (text_iterator_byte_prev(&it, &c) && (c == '\r' || c == '\n'));
r.end = it.pos;
return text_range_linewise(txt, &r);
}
/* TODO: reduce code duplication? */
Filerange text_object_longword(Text *txt, size_t pos) {
Filerange r;
char c, prev = '0', next = '0';
Iterator it = text_iterator_get(txt, pos);
if (!text_iterator_byte_get(&it, &c))
return text_range_empty();
if (text_iterator_byte_prev(&it, &prev))
text_iterator_byte_next(&it, NULL);
text_iterator_byte_next(&it, &next);
if (isspace(c)) {
/* middle of two words */
r.start = text_char_next(txt, text_longword_end_prev(txt, pos));
r.end = text_longword_start_next(txt, pos);
} else if (isspace(prev) && isspace(next)) {
/* on a single character */
r.start = pos;
r.end = text_char_next(txt, pos);
} else if (isspace(prev)) {
/* at start of a word */
r.start = pos;
r.end = text_char_next(txt, text_longword_end_next(txt, pos));
} else if (isspace(next)) {
/* at end of a word */
r.start = text_longword_start_prev(txt, pos);
r.end = text_char_next(txt, pos);
} else {
/* in the middle of a word */
r.start = text_longword_start_prev(txt, pos);
r.end = text_char_next(txt, text_longword_end_next(txt, pos));
}
return r;
}
Filerange text_object_longword_outer(Text *txt, size_t pos) {
Filerange r;
char c, prev = '0', next = '0';
Iterator it = text_iterator_get(txt, pos);
if (!text_iterator_byte_get(&it, &c))
return text_range_empty();
if (text_iterator_byte_prev(&it, &prev))
text_iterator_byte_next(&it, NULL);
text_iterator_byte_next(&it, &next);
if (isspace(c)) {
/* middle of two words, include leading white space */
r.start = text_char_next(txt, text_longword_end_prev(txt, pos));
r.end = text_char_next(txt, text_longword_end_next(txt, pos));
} else if (isspace(prev) && isspace(next)) {
/* on a single character */
r.start = pos;
r.end = text_longword_start_next(txt, pos);
} else if (isspace(prev)) {
/* at start of a word */
r.start = pos;
r.end = text_longword_start_next(txt, text_longword_end_next(txt, pos));
} else if (isspace(next)) {
/* at end of a word */
r.start = text_longword_start_prev(txt, pos);
r.end = text_longword_start_next(txt, pos);
} else {
/* in the middle of a word */
r.start = text_longword_start_prev(txt, pos);
r.end = text_longword_start_next(txt, text_longword_end_next(txt, pos));
}
return r;
}
Filerange text_object_word(Text *txt, size_t pos) {
Filerange r;
char c, prev = '0', next = '0';
Iterator it = text_iterator_get(txt, pos);
if (!text_iterator_byte_get(&it, &c))
return text_range_empty();
if (text_iterator_byte_prev(&it, &prev))
text_iterator_byte_next(&it, NULL);
text_iterator_byte_next(&it, &next);
if (isspace(c)) {
r.start = text_char_next(txt, text_word_end_prev(txt, pos));
r.end = text_word_start_next(txt, pos);
} else if (isboundry(prev) && isboundry(next)) {
if (isboundry(c)) {
r.start = text_char_next(txt, text_word_end_prev(txt, pos));
r.end = text_char_next(txt, text_word_end_next(txt, pos));
} else {
/* on a single character */
r.start = pos;
r.end = text_char_next(txt, pos);
}
} else if (isboundry(prev)) {
/* at start of a word */
r.start = pos;
r.end = text_char_next(txt, text_word_end_next(txt, pos));
} else if (isboundry(next)) {
/* at end of a word */
r.start = text_word_start_prev(txt, pos);
r.end = text_char_next(txt, pos);
} else {
/* in the middle of a word */
r.start = text_word_start_prev(txt, pos);
r.end = text_char_next(txt, text_word_end_next(txt, pos));
}
return r;
}
Filerange text_object_word_outer(Text *txt, size_t pos) {
Filerange r;
char c, prev = '0', next = '0';
Iterator it = text_iterator_get(txt, pos);
if (!text_iterator_byte_get(&it, &c))
return text_range_empty();
if (text_iterator_byte_prev(&it, &prev))
text_iterator_byte_next(&it, NULL);
text_iterator_byte_next(&it, &next);
if (isspace(c)) {
/* middle of two words, include leading white space */
r.start = text_char_next(txt, text_word_end_prev(txt, pos));
r.end = text_word_end_next(txt, pos);
} else if (isboundry(prev) && isboundry(next)) {
if (isboundry(c)) {
r.start = text_char_next(txt, text_word_end_prev(txt, pos));
r.end = text_word_start_next(txt, text_word_end_next(txt, pos));
} else {
/* on a single character */
r.start = pos;
r.end = text_char_next(txt, pos);
}
} else if (isboundry(prev)) {
/* at start of a word */
r.start = pos;
r.end = text_word_start_next(txt, text_word_end_next(txt, pos));
} else if (isboundry(next)) {
/* at end of a word */
r.start = text_word_start_prev(txt, pos);
r.end = text_word_start_next(txt, pos);
} else {
/* in the middle of a word */
r.start = text_word_start_prev(txt, pos);
r.end = text_word_start_next(txt, text_word_end_next(txt, pos));
}
return r;
}
Filerange text_object_word_find_next(Text *txt, size_t pos, const char *word) {
size_t len = strlen(word);
for (;;) {
size_t match_pos = text_find_next(txt, pos, word);
if (match_pos != pos) {
Filerange match_word = text_object_word(txt, match_pos);
if (text_range_size(&match_word) == len)
return match_word;
pos = match_pos;
} else {
return text_range_empty();
}
}
}
Filerange text_object_word_find_prev(Text *txt, size_t pos, const char *word) {
size_t len = strlen(word);
for (;;) {
size_t match_pos = text_find_prev(txt, pos, word);
if (match_pos != pos) {
Filerange match_word = text_object_word(txt, match_pos);
if (text_range_size(&match_word) == len)
return match_word;
pos = match_pos;
} else {
return text_range_empty();
}
}
}
Filerange text_object_line(Text *txt, size_t pos) {
Filerange r;
r.start = text_line_begin(txt, pos);
r.end = text_line_next(txt, pos);
return r;
}
Filerange text_object_sentence(Text *txt, size_t pos) {
Filerange r;
r.start = text_sentence_prev(txt, pos);
r.end = text_sentence_next(txt, pos);
return r;
}
Filerange text_object_paragraph(Text *txt, size_t pos) {
Filerange r;
r.start = text_paragraph_prev(txt, pos);
r.end = text_paragraph_next(txt, pos);
return r;
}
Filerange text_object_function(Text *txt, size_t pos) {
size_t a = text_function_start_prev(txt, pos);
size_t b = text_function_end_next(txt, pos);
if (text_function_end_next(txt, a) == b) {
Filerange r = text_range_new(a, b+1);
return text_range_linewise(txt, &r);
}
return text_range_empty();
}
Filerange text_object_function_inner(Text *txt, size_t pos) {
Filerange r = text_object_function(txt, pos);
if (!text_range_valid(&r))
return r;
size_t b = text_function_end_next(txt, pos);
size_t a = text_bracket_match(txt, b);
return text_range_new(a+1, b-1);
}
static Filerange text_object_bracket(Text *txt, size_t pos, char type) {
char c, open, close;
int opened = 1, closed = 1;
Filerange r = text_range_empty();
switch (type) {
case '(': case ')': open = '('; close = ')'; break;
case '{': case '}': open = '{'; close = '}'; break;
case '[': case ']': open = '['; close = ']'; break;
case '<': case '>': open = '<'; close = '>'; break;
case '"': open = '"'; close = '"'; break;
case '`': open = '`'; close = '`'; break;
case '\'': open = '\''; close = '\''; break;
default: return r;
}
Iterator it = text_iterator_get(txt, pos);
if (open == close && text_iterator_byte_get(&it, &c) && (c == '"' || c == '`' || c == '\'')) {
size_t match = text_bracket_match(txt, pos);
r.start = MIN(pos, match) + 1;
r.end = MAX(pos, match);
return r;
}
while (text_iterator_byte_get(&it, &c)) {
if (c == open && --opened == 0) {
r.start = it.pos + 1;
break;
} else if (c == close && it.pos != pos) {
opened++;
}
text_iterator_byte_prev(&it, NULL);
}
it = text_iterator_get(txt, pos);
while (text_iterator_byte_get(&it, &c)) {
if (c == close && --closed == 0) {
r.end = it.pos;
break;
} else if (c == open && it.pos != pos) {
closed++;
}
text_iterator_byte_next(&it, NULL);
}
if (!text_range_valid(&r))
return text_range_empty();
return r;
}
Filerange text_object_square_bracket(Text *txt, size_t pos) {
return text_object_bracket(txt, pos, ']');
}
Filerange text_object_curly_bracket(Text *txt, size_t pos) {
return text_object_bracket(txt, pos, '}');
}
Filerange text_object_angle_bracket(Text *txt, size_t pos) {
return text_object_bracket(txt, pos, '>');
}
Filerange text_object_paranthese(Text *txt, size_t pos) {
return text_object_bracket(txt, pos, ')');
}
Filerange text_object_quote(Text *txt, size_t pos) {
return text_object_bracket(txt, pos, '"');
}
Filerange text_object_single_quote(Text *txt, size_t pos) {
return text_object_bracket(txt, pos, '\'');
}
Filerange text_object_backtick(Text *txt, size_t pos) {
return text_object_bracket(txt, pos, '`');
}
Filerange text_range_linewise(Text *txt, Filerange *rin) {
Filerange rout = *rin;
rout.start = text_line_begin(txt, rin->start);
if (rin->end != text_line_begin(txt, rin->end))
rout.end = text_line_next(txt, rin->end);
return rout;
}
bool text_range_is_linewise(Text *txt, Filerange *r) {
return text_range_valid(r) &&
r->start == text_line_begin(txt, r->start) &&
r->end == text_line_begin(txt, r->end);
}