-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions-yank.c
281 lines (240 loc) Β· 6.97 KB
/
actions-yank.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
#include <string.h>
#include "actions.h"
// Yank actions
// Global Yank buffer
YankBuffer yank_buffer = {0};
void action_yank_forward(Line *l, Action *a)
{
(void) a;
size_t buf_len = (l->cursor + a->mov.count) - l->cursor;
clear_yank_buffer();
yank_buffer.len = buf_len;
line_copy_range(l, l->cursor, l->cursor + a->mov.count - 1, yank_buffer.buf, buf_len);
}
void action_yank_backward(Line *l, Action *a)
{
(void) a;
size_t buf_len = (l->cursor - 1) - (l->cursor - a->mov.count) + 1;
clear_yank_buffer();
yank_buffer.len = buf_len;
line_copy_range(l, l->cursor - a->mov.count, l->cursor - 1, yank_buffer.buf, buf_len);
}
void action_yank_word_forward(Line *l, Action *a)
{
size_t i;
char *start, *end;
size_t cur_idx = l->cur_word_idx;
start = l->cursor;
end = l->cursor;
for (i = 0; i < a->mov.count; i++) {
next_word_start(l);
}
if (l->cur_word_idx != l->n_words - 1 || cur_idx != l->cur_word_idx) {
prev_word_end(l);
end = l->cursor;
} else {
end = line_get_end_ptr(l);
}
size_t buf_len = end - start + 1;
clear_yank_buffer();
yank_buffer.len = buf_len;
line_copy_range(l, start, end, yank_buffer.buf, yank_buffer.len);
//Put cursor back at original pos
l->cursor = start;
}
void action_yank_word_backward(Line *l, Action *a)
{
size_t i;
char *start, *end;
start = l->cursor;
end = l->cursor - 1;
for (i = 0; i < a->mov.count; i++) {
prev_word_start(l);
}
start = l->cursor;
size_t buf_len = end - start + 1;
clear_yank_buffer();
yank_buffer.len = buf_len;
line_copy_range(l, start, end, yank_buffer.buf, yank_buffer.len);
//Put cursor back at original pos
l->cursor = end + 1;
}
void action_yank_to_line_start(Line *l, Action *a)
{
(void) a;
char *end;
char *cur_pos = l->cursor;
prev_char(l);
end = l->cursor;
clear_yank_buffer();
yank_buffer.len = end - l->src + 1;
line_copy_range(l, l->src, end, yank_buffer.buf, yank_buffer.len);
l->cursor = cur_pos;
}
void action_yank_to_line_end(Line *l, Action *a)
{
(void) a;
char *start;
start = l->cursor;
clear_yank_buffer();
yank_buffer.len = line_get_end_ptr(l) - start + 1;
line_copy_range(l, start, line_get_end_ptr(l), yank_buffer.buf, yank_buffer.len);
}
void action_yank_to_find(Line *l, Action *a)
{
char *start, *end;
size_t i;
start = l->cursor;
end = l->cursor;
for (i = 0; i < a->mov.count; i++) {
if (a->mov.arg == 0)
continue;
end = search_char_forward(l, a->mov.arg);
l->cursor = end;
l->cur_word_idx = word_idx_from_cursor(l);
}
if (start == end)
return;
size_t buf_len = end - start + 1;
clear_yank_buffer();
yank_buffer.len = buf_len;
line_copy_range(l, start, end, yank_buffer.buf, yank_buffer.len);
}
void action_yank_to_find_backward(Line *l, Action *a)
{
char *start, *end;
size_t i;
end = l->cursor;
start = l->cursor;
for (i = 0; i < a->mov.count; i++) {
if (a->mov.arg == 0)
continue;
start = search_char_backward(l, a->mov.arg);
l->cursor = start;
l->cur_word_idx = word_idx_from_cursor(l);
}
if (start == end)
return;
size_t buf_len = end - start + 1;
clear_yank_buffer();
yank_buffer.len = buf_len;
line_copy_range(l, start, end, yank_buffer.buf, yank_buffer.len);
}
void action_yank_till(Line *l, Action *a)
{
char *start, *end;
size_t i;
start = l->cursor;
end = l->cursor;
for (i = 0; i < a->mov.count; i++) {
if (a->mov.arg == 0)
continue;
end = search_until_char_forward(l, a->mov.arg);
l->cursor = end;
l->cur_word_idx = word_idx_from_cursor(l);
}
if (start == end)
return;
size_t buf_len = end - start + 1;
clear_yank_buffer();
yank_buffer.len = buf_len;
line_copy_range(l, start, end, yank_buffer.buf, yank_buffer.len);
}
void action_yank_till_backward(Line *l, Action *a)
{
char *start, *end;
size_t i;
end = l->cursor;
start = l->cursor;
for (i = 0; i < a->mov.count; i++) {
if (a->mov.arg == 0)
continue;
start = search_until_char_backward(l, a->mov.arg);
l->cursor = end;
l->cur_word_idx = word_idx_from_cursor(l);
}
if (start == end)
return;
size_t buf_len = end - start + 1;
clear_yank_buffer();
yank_buffer.len = buf_len;
line_copy_range(l, start, end, yank_buffer.buf, yank_buffer.len);
}
void action_yank_search(Line *l, Action *a)
{
char *cur_pos;
char *old_pos = l->cursor;
if (a->mov.s_text.text[0] == '\0')
return;
next_char(l);
cur_pos = line_search_str(l, a->mov.s_text.text, a->mov.count);
cur_pos = (cur_pos) ? cur_pos : old_pos;
if (cur_pos != old_pos) {
size_t buffer_len = (cur_pos - 1) - old_pos + 1;
clear_yank_buffer();
yank_buffer.len = buffer_len;
line_copy_range(l, old_pos, cur_pos - 1, yank_buffer.buf, yank_buffer.len);
}
}
void action_yank_search_backward(Line *l, Action *a)
{
char *cur_pos;
char *old_pos = l->cursor;
if (a->mov.s_text.text[0] == '\0')
return;
prev_char(l);
cur_pos = line_search_str_backward(l, a->mov.s_text.text, a->mov.count);
cur_pos = (cur_pos) ? cur_pos : old_pos;
if (cur_pos != old_pos) {
size_t buffer_len = old_pos - (cur_pos + 1) + 1;
clear_yank_buffer();
yank_buffer.len = buffer_len;
line_copy_range(l, cur_pos + 1, old_pos, yank_buffer.buf, yank_buffer.len);
}
}
void action_yank_match_pair(Line *l, Action *a)
{
(void) a;
char *cur_pos = l->cursor;
char cur_char = (is_at_line(l, cur_pos)) ? *(cur_pos) : 0;
char matching_char = get_matching_char(cur_char);
if (matching_char == 0)
return;
char *search_pos;
search_pos = (is_opening(cur_char)) ? search_char_forward(l, matching_char) : search_char_backward(l, matching_char);
if (search_pos == cur_pos)
return;
// line_copy_range might have problems with
// calculation if start is bigger than end
char *near, *far;
near = (cur_pos < search_pos) ? cur_pos : search_pos;
far = (cur_pos > search_pos) ? cur_pos : search_pos;
size_t buf_len = far - near + 1;
clear_yank_buffer();
yank_buffer.len = buf_len;
line_copy_range(l, near, far, yank_buffer.buf, yank_buffer.len);
}
// Paste action
void action_paste_at_cursor(Line *l, Action *a)
{
(void) a;
// Empty buffer
if (yank_buffer.len == 0)
return;
next_char(l);
insert_at_cursor(l, yank_buffer.buf, yank_buffer.len);
}
void action_paste_backward_at_cursor(Line *l, Action *a)
{
(void) a;
// Empty buffer
if (yank_buffer.len == 0)
return;
insert_at_cursor(l, yank_buffer.buf, yank_buffer.len);
}
// Helper functions
void clear_yank_buffer()
{
memset(yank_buffer.buf, 0, YANK_BUF_MAX);
yank_buffer.len = 0;
}