forked from martanne/vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vis-operators.c
306 lines (274 loc) · 8.31 KB
/
vis-operators.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
#include <string.h>
#include <ctype.h>
#include "vis-core.h"
#include "text-motions.h"
#include "text-objects.h"
#include "text-util.h"
#include "util.h"
static size_t op_delete(Vis *vis, Text *txt, OperatorContext *c) {
c->reg->linewise = c->linewise;
register_put_range(vis, c->reg, txt, &c->range);
text_delete_range(txt, &c->range);
size_t pos = c->range.start;
if (c->linewise && pos == text_size(txt))
pos = text_line_begin(txt, text_line_prev(txt, pos));
return pos;
}
static size_t op_change(Vis *vis, Text *txt, OperatorContext *c) {
op_delete(vis, txt, c);
macro_operator_record(vis);
return c->range.start;
}
static size_t op_yank(Vis *vis, Text *txt, OperatorContext *c) {
c->reg->linewise = c->linewise;
register_put_range(vis, c->reg, txt, &c->range);
if (c->reg == &vis->registers[VIS_REG_DEFAULT])
register_put_range(vis, &vis->registers[VIS_REG_ZERO], txt, &c->range);
return c->linewise ? c->pos : c->range.start;
}
static size_t op_put(Vis *vis, Text *txt, OperatorContext *c) {
char b;
size_t pos = c->pos;
bool sel = text_range_size(&c->range) > 0;
bool sel_linewise = sel && text_range_is_linewise(txt, &c->range);
if (sel) {
text_delete_range(txt, &c->range);
pos = c->pos = c->range.start;
}
switch (c->arg->i) {
case VIS_OP_PUT_AFTER:
case VIS_OP_PUT_AFTER_END:
if (c->reg->linewise && !sel_linewise)
pos = text_line_next(txt, pos);
else if (!sel && text_byte_get(txt, pos, &b) && b != '\r' && b != '\n')
pos = text_char_next(txt, pos);
break;
case VIS_OP_PUT_BEFORE:
case VIS_OP_PUT_BEFORE_END:
if (c->reg->linewise)
pos = text_line_begin(txt, pos);
break;
}
size_t len;
const char *data = register_get(vis, c->reg, &len);
for (int i = 0; i < c->count; i++) {
char nl;
if (c->reg->linewise && pos > 0 && text_byte_get(txt, pos-1, &nl) && nl != '\n')
pos += text_insert_newline(txt, pos);
text_insert(txt, pos, data, len);
pos += len;
if (c->reg->linewise && pos > 0 && text_byte_get(txt, pos-1, &nl) && nl != '\n')
pos += text_insert_newline(txt, pos);
}
if (c->reg->linewise) {
switch (c->arg->i) {
case VIS_OP_PUT_BEFORE_END:
case VIS_OP_PUT_AFTER_END:
pos = text_line_start(txt, pos);
break;
case VIS_OP_PUT_AFTER:
pos = text_line_start(txt, text_line_next(txt, c->pos));
break;
case VIS_OP_PUT_BEFORE:
pos = text_line_start(txt, c->pos);
break;
}
} else {
switch (c->arg->i) {
case VIS_OP_PUT_AFTER:
case VIS_OP_PUT_BEFORE:
pos = text_char_prev(txt, pos);
break;
}
}
return pos;
}
static size_t op_shift_right(Vis *vis, Text *txt, OperatorContext *c) {
char spaces[9] = " ";
spaces[MIN(vis->tabwidth, LENGTH(spaces) - 1)] = '\0';
const char *tab = vis->expandtab ? spaces : "\t";
size_t tablen = strlen(tab);
size_t pos = text_line_begin(txt, c->range.end), prev_pos;
size_t inserted = 0;
/* if range ends at the begin of a line, skip line break */
if (pos == c->range.end)
pos = text_line_prev(txt, pos);
do {
prev_pos = pos = text_line_begin(txt, pos);
text_insert(txt, pos, tab, tablen);
pos = text_line_prev(txt, pos);
inserted += tablen;
} while (pos >= c->range.start && pos != prev_pos);
return c->pos + inserted;
}
static size_t op_shift_left(Vis *vis, Text *txt, OperatorContext *c) {
size_t pos = text_line_begin(txt, c->range.end), prev_pos;
size_t tabwidth = vis->tabwidth, tablen;
size_t deleted = 0;
/* if range ends at the begin of a line, skip line break */
if (pos == c->range.end)
pos = text_line_prev(txt, pos);
do {
char c;
size_t len = 0;
prev_pos = pos = text_line_begin(txt, pos);
Iterator it = text_iterator_get(txt, pos);
if (text_iterator_byte_get(&it, &c) && c == '\t') {
len = 1;
} else {
for (len = 0; text_iterator_byte_get(&it, &c) && c == ' '; len++)
text_iterator_byte_next(&it, NULL);
}
tablen = MIN(len, tabwidth);
text_delete(txt, pos, tablen);
pos = text_line_prev(txt, pos);
deleted += tablen;
} while (pos >= c->range.start && pos != prev_pos);
return c->pos - deleted;
}
static size_t op_case_change(Vis *vis, Text *txt, OperatorContext *c) {
size_t len = text_range_size(&c->range);
char *buf = malloc(len);
if (!buf)
return c->pos;
len = text_bytes_get(txt, c->range.start, len, buf);
size_t rem = len;
for (char *cur = buf; rem > 0; cur++, rem--) {
int ch = (unsigned char)*cur;
if (isascii(ch)) {
if (c->arg->i == VIS_OP_CASE_SWAP)
*cur = islower(ch) ? toupper(ch) : tolower(ch);
else if (c->arg->i == VIS_OP_CASE_UPPER)
*cur = toupper(ch);
else
*cur = tolower(ch);
}
}
text_delete(txt, c->range.start, len);
text_insert(txt, c->range.start, buf, len);
free(buf);
return c->pos;
}
static size_t op_cursor(Vis *vis, Text *txt, OperatorContext *c) {
View *view = vis->win->view;
Filerange r = text_range_linewise(txt, &c->range);
for (size_t line = text_range_line_first(txt, &r); line != EPOS; line = text_range_line_next(txt, &r, line)) {
size_t pos;
if (c->arg->i == VIS_OP_CURSOR_EOL)
pos = text_line_finish(txt, line);
else
pos = text_line_start(txt, line);
view_cursors_new_force(view, pos);
}
return EPOS;
}
static size_t op_join(Vis *vis, Text *txt, OperatorContext *c) {
size_t pos = text_line_begin(txt, c->range.end), prev_pos;
/* if operator and range are both linewise, skip last line break */
if (c->linewise && text_range_is_linewise(txt, &c->range)) {
size_t line_prev = text_line_prev(txt, pos);
size_t line_prev_prev = text_line_prev(txt, line_prev);
if (line_prev_prev >= c->range.start)
pos = line_prev;
}
do {
prev_pos = pos;
size_t end = text_line_start(txt, pos);
pos = text_char_next(txt, text_line_finish(txt, text_line_prev(txt, end)));
if (pos >= c->range.start && end > pos) {
text_delete(txt, pos, end - pos);
text_insert(txt, pos, " ", 1);
} else {
break;
}
} while (pos != prev_pos);
return c->range.start;
}
static size_t op_insert(Vis *vis, Text *txt, OperatorContext *c) {
macro_operator_record(vis);
return c->newpos != EPOS ? c->newpos : c->pos;
}
static size_t op_replace(Vis *vis, Text *txt, OperatorContext *c) {
macro_operator_record(vis);
return c->newpos != EPOS ? c->newpos : c->pos;
}
static size_t op_filter(Vis *vis, Text *txt, OperatorContext *c) {
if (!c->arg->s)
macro_operator_record(vis);
return text_size(txt) + 1; /* do not change cursor position, would destroy selection */
}
bool vis_operator(Vis *vis, enum VisOperator id, ...) {
va_list ap;
va_start(ap, id);
switch (id) {
case VIS_OP_CASE_LOWER:
case VIS_OP_CASE_UPPER:
case VIS_OP_CASE_SWAP:
vis->action.arg.i = id;
id = VIS_OP_CASE_SWAP;
break;
case VIS_OP_CURSOR_SOL:
case VIS_OP_CURSOR_EOL:
vis->action.arg.i = id;
id = VIS_OP_CURSOR_SOL;
break;
case VIS_OP_PUT_AFTER:
case VIS_OP_PUT_AFTER_END:
case VIS_OP_PUT_BEFORE:
case VIS_OP_PUT_BEFORE_END:
vis->action.arg.i = id;
id = VIS_OP_PUT_AFTER;
break;
case VIS_OP_FILTER:
vis->action.arg.s = va_arg(ap, char*);
/* fall through */
case VIS_OP_SHIFT_LEFT:
case VIS_OP_SHIFT_RIGHT:
vis_motion_type(vis, VIS_MOTIONTYPE_LINEWISE);
break;
default:
break;
}
if (id >= LENGTH(vis_operators))
goto err;
const Operator *op = &vis_operators[id];
if (vis->mode->visual) {
vis->action.op = op;
action_do(vis, &vis->action);
goto out;
}
/* switch to operator mode inorder to make operator options and
* text-object available */
vis_mode_switch(vis, VIS_MODE_OPERATOR_PENDING);
if (vis->action.op == op) {
/* hacky way to handle double operators i.e. things like
* dd, yy etc where the second char isn't a movement */
vis->action.type = LINEWISE;
vis_motion(vis, VIS_MOVE_LINE_NEXT);
} else {
vis->action.op = op;
}
/* put is not a real operator, does not need a range to operate on */
if (id == VIS_OP_PUT_AFTER)
vis_motion(vis, VIS_MOVE_NOP);
out:
va_end(ap);
return true;
err:
va_end(ap);
return false;
}
const Operator vis_operators[] = {
[VIS_OP_DELETE] = { op_delete },
[VIS_OP_CHANGE] = { op_change },
[VIS_OP_YANK] = { op_yank },
[VIS_OP_PUT_AFTER] = { op_put },
[VIS_OP_SHIFT_RIGHT] = { op_shift_right },
[VIS_OP_SHIFT_LEFT] = { op_shift_left },
[VIS_OP_CASE_SWAP] = { op_case_change },
[VIS_OP_JOIN] = { op_join },
[VIS_OP_INSERT] = { op_insert },
[VIS_OP_REPLACE] = { op_replace },
[VIS_OP_CURSOR_SOL] = { op_cursor },
[VIS_OP_FILTER] = { op_filter },
};