~martanne/vis

ref: bcde0a768c843049e3c2a4ca32be0ad91e533148 vis/vis-text-objects.c -rw-r--r-- 4.8 KiB
bcde0a76Marc André Tanner ci: verify coverity scan script before using it 7 months ago
                                                                                
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
#include "vis-core.h"
#include "text-objects.h"
#include "util.h"

int vis_textobject_register(Vis *vis, int type, void *data, VisTextObjectFunction *textobject) {

	TextObject *obj = calloc(1, sizeof *obj);
	if (!obj)
		return -1;

	obj->user = textobject;
	obj->type = type;
	obj->data = data;

	if (array_add_ptr(&vis->textobjects, obj))
		return LENGTH(vis_textobjects) + array_length(&vis->textobjects) - 1;
	free(obj);
	return -1;
}

bool vis_textobject(Vis *vis, enum VisTextObject id) {
	if (id < LENGTH(vis_textobjects))
		vis->action.textobj = &vis_textobjects[id];
	else
		vis->action.textobj = array_get_ptr(&vis->textobjects, id - LENGTH(vis_textobjects));
	if (!vis->action.textobj)
		return false;
	vis_do(vis);
	return true;
}

static Filerange search_forward(Vis *vis, Text *txt, size_t pos) {
	Filerange range = text_range_empty();
	Regex *regex = vis_regex(vis, NULL);
	if (regex)
		range = text_object_search_forward(txt, pos, regex);
	text_regex_free(regex);
	return range;
}

static Filerange search_backward(Vis *vis, Text *txt, size_t pos) {
	Filerange range = text_range_empty();
	Regex *regex = vis_regex(vis, NULL);
	if (regex)
		range = text_object_search_backward(txt, pos, regex);
	text_regex_free(regex);
	return range;
}

static Filerange object_unpaired(Text *txt, size_t pos, char obj) {
	char c;
	bool before = false;
	Iterator it = text_iterator_get(txt, pos), rit = it;

	while (text_iterator_byte_get(&rit, &c) && c != '\n') {
		if (c == obj) {
			before = true;
			break;
		}
		text_iterator_byte_prev(&rit, NULL);
	}

	/* if there is no previous occurrence on the same line, advance starting position */
	if (!before) {
		while (text_iterator_byte_get(&it, &c) && c != '\n') {
			if (c == obj) {
				pos = it.pos;
				break;
			}
			text_iterator_byte_next(&it, NULL);
		}
	}

	switch (obj) {
	case '"':
		return text_object_quote(txt, pos);
	case '\'':
		return text_object_single_quote(txt, pos);
	case '`':
		return text_object_backtick(txt, pos);
	default:
		return text_range_empty();
	}
}

static Filerange object_quote(Text *txt, size_t pos) {
	return object_unpaired(txt, pos, '"');
}

static Filerange object_single_quote(Text *txt, size_t pos) {
	return object_unpaired(txt, pos, '\'');
}

static Filerange object_backtick(Text *txt, size_t pos) {
	return object_unpaired(txt, pos, '`');
}

const TextObject vis_textobjects[] = {
	[VIS_TEXTOBJECT_INNER_WORD] = {
		.txt = text_object_word,
	},
	[VIS_TEXTOBJECT_OUTER_WORD] = {
		.txt = text_object_word_outer,
	},
	[VIS_TEXTOBJECT_INNER_LONGWORD] = {
		.txt = text_object_longword,
	},
	[VIS_TEXTOBJECT_OUTER_LONGWORD] = {
		.txt = text_object_longword_outer,
	},
	[VIS_TEXTOBJECT_SENTENCE] = {
		.txt = text_object_sentence,
	},
	[VIS_TEXTOBJECT_PARAGRAPH] = {
		.txt = text_object_paragraph,
	},
	[VIS_TEXTOBJECT_PARAGRAPH_OUTER] = {
		.txt = text_object_paragraph_outer,
	},
	[VIS_TEXTOBJECT_OUTER_SQUARE_BRACKET] = {
		.txt = text_object_square_bracket,
		.type = TEXTOBJECT_DELIMITED_OUTER,
	},
	[VIS_TEXTOBJECT_INNER_SQUARE_BRACKET] = {
		.txt = text_object_square_bracket,
		.type = TEXTOBJECT_DELIMITED_INNER,
	},
	[VIS_TEXTOBJECT_OUTER_CURLY_BRACKET] = {
		.txt = text_object_curly_bracket,
		.type = TEXTOBJECT_DELIMITED_OUTER,
	},
	[VIS_TEXTOBJECT_INNER_CURLY_BRACKET] = {
		.txt = text_object_curly_bracket,
		.type = TEXTOBJECT_DELIMITED_INNER,
	},
	[VIS_TEXTOBJECT_OUTER_ANGLE_BRACKET] = {
		.txt = text_object_angle_bracket,
		.type = TEXTOBJECT_DELIMITED_OUTER,
	},
	[VIS_TEXTOBJECT_INNER_ANGLE_BRACKET] = {
		.txt = text_object_angle_bracket,
		.type = TEXTOBJECT_DELIMITED_INNER,
	},
	[VIS_TEXTOBJECT_OUTER_PARENTHESIS] = {
		.txt = text_object_parenthesis,
		.type = TEXTOBJECT_DELIMITED_OUTER,
	},
	[VIS_TEXTOBJECT_INNER_PARENTHESIS] = {
		.txt = text_object_parenthesis,
		.type = TEXTOBJECT_DELIMITED_INNER,
	},
	[VIS_TEXTOBJECT_OUTER_QUOTE] = {
		.txt = object_quote,
		.type = TEXTOBJECT_DELIMITED_OUTER,
	},
	[VIS_TEXTOBJECT_INNER_QUOTE] = {
		.txt = object_quote,
		.type = TEXTOBJECT_DELIMITED_INNER,
	},
	[VIS_TEXTOBJECT_OUTER_SINGLE_QUOTE] = {
		.txt = object_single_quote,
		.type = TEXTOBJECT_DELIMITED_OUTER,
	},
	[VIS_TEXTOBJECT_INNER_SINGLE_QUOTE] = {
		.txt = object_single_quote,
		.type = TEXTOBJECT_DELIMITED_INNER,
	},
	[VIS_TEXTOBJECT_OUTER_BACKTICK] = {
		.txt = object_backtick,
		.type = TEXTOBJECT_DELIMITED_OUTER,
	},
	[VIS_TEXTOBJECT_INNER_BACKTICK] = {
		.txt = object_backtick,
		.type = TEXTOBJECT_DELIMITED_INNER,
	},
	[VIS_TEXTOBJECT_OUTER_LINE] = {
		.txt = text_object_line,
	},
	[VIS_TEXTOBJECT_INNER_LINE] = {
		.txt = text_object_line_inner,
	},
	[VIS_TEXTOBJECT_INDENTATION] = {
		.txt = text_object_indentation,
	},
	[VIS_TEXTOBJECT_SEARCH_FORWARD] = {
		.vis = search_forward,
		.type = TEXTOBJECT_NON_CONTIGUOUS|TEXTOBJECT_EXTEND_FORWARD,
	},
	[VIS_TEXTOBJECT_SEARCH_BACKWARD] = {
		.vis = search_backward,
		.type = TEXTOBJECT_NON_CONTIGUOUS|TEXTOBJECT_EXTEND_BACKWARD,
	},
};