forked from martanne/vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-objects.c
163 lines (144 loc) · 4.8 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
/*
* 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 <ctype.h>
#include "text-motions.h"
#include "text-objects.h"
#include "util.h"
static Filerange empty = {
.start = -1,
.end = -1,
};
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 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_char_next(txt, text_word_end_next(txt, pos));
} else if (isspace(prev) && isspace(next)) {
/* on a single character */
r.start = pos;
r.end = text_word_start_next(txt, pos);
} else if (isspace(prev)) {
/* at start of a word */
r.start = pos;
r.end = text_word_start_next(txt, text_word_end_next(txt, pos));
} else if (isspace(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_line(Text *txt, size_t pos) {
char c;
Filerange r;
r.start = text_line_begin(txt, pos);
Iterator it = text_iterator_get(txt, text_line_end(txt, pos));
if (text_iterator_byte_get(&it, &c) && c == '\n')
text_iterator_byte_next(&it, NULL);
if (text_iterator_byte_get(&it, &c) && c == '\r')
text_iterator_byte_next(&it, NULL);
r.end = it.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;
}
static Filerange text_object_bracket(Text *txt, size_t pos, char type) {
char c, open, close;
int opened = 1, closed = 1;
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 empty;
}
Filerange r = empty;
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 (r.start == (size_t)-1 || r.end == (size_t)-1 || r.start > r.end)
return 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, '`');
}