-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsearch.c
284 lines (262 loc) · 7.04 KB
/
search.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
#include "les.h"
#include "rx.h"
#include <ctype.h>
#include <term.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
prompt_t *spr = NULL;
rx_t *rx = NULL;
matcher_t *m = NULL;
int active_search = 0;
int search_version = 0;
// First one that starts after tabb->pos, or the first match
void get_current_match () {
tabb->current_match = 0;
int i;
for (i = 0; i < tabb->matches_len; i++) {
if (tabb->matches[i].start >= tabb->pos) {
tabb->current_match = i;
return;
}
}
}
// first one at the bottom of the screen, or the last match
void get_current_match_backward () {
tabb->current_match = tabb->matches_len - 1;
int i;
for (i = tabb->matches_len - 1; i >= 0; i--) {
if (tabb->matches[i].start < tlines[tlines_len - 1].end_pos) {
tabb->current_match = i;
return;
}
}
}
void move_to_match () {
size_t pos = tabb->matches[tabb->current_match].start;
size_t pos2;
int retval;
// Move the page to the right to be able to see the match
if (!line_wrap) {
int bol = prev_line(tabb->buf, tabb->buf_len, pos, 0);
int width = strnwidth(tabb->buf + bol, pos - bol);
if (width < columns) {
tabb->column = 0;
}
else {
int end = tabb->matches[tabb->current_match].end;
int eol = next_line(tabb->buf, tabb->buf_len, pos, 1);
if (eol < end) {
end = eol;
}
int width2 = strnwidth(tabb->buf + pos, end - pos);
if (width2 > columns) {
tabb->column = width - (columns / 2);
}
else {
tabb->column = width - columns + width2;
}
}
}
if (pos >= tabb->pos && pos < tlines[tlines_len - 1].end_pos) {
return;
}
else if (pos < tabb->pos) {
retval = find_pos_backward(lines - line1 - 1, pos, &pos2);
if (retval) {
tabb->line += count_lines_atob(tabb->buf, tabb->pos, pos2);
tabb->pos = pos2;
}
else {
pos2 = line_before_pos(pos, (lines - line1 - 1) / 2);
tabb->line += count_lines_atob(tabb->buf, tabb->pos, pos2);
tabb->pos = pos2;
}
}
else if (pos >= tlines[tlines_len - 1].end_pos) {
retval = find_pos_forward(lines - line1 - 1, pos, &pos2);
if (retval) {
tabb->line += count_lines_atob(tabb->buf, tabb->pos, pos2);
tabb->pos = pos2;
}
else {
pos2 = line_before_pos(pos, (lines - line1 - 1) / 2);
tabb->line += count_lines_atob(tabb->buf, tabb->pos, pos2);
tabb->pos = pos2;
}
}
}
void find_matches () {
active_search = 0;
tabb->search_version = search_version;
tabb->matches_len = 0;
tabb->current_match = 0;
active_search = 1;
int i = 0;
while (i < tabb->buf_len) {
rx_match(rx, m, tabb->buf_len, tabb->buf, i);
if (!m->success) {
break;
}
if (tabb->matches_len + 1 > tabb->matches_size) {
if (!tabb->matches_size) {
tabb->matches_size = 16;
tabb->matches = malloc(tabb->matches_size * sizeof (match_t));
}
else {
tabb->matches_size *= 2;
tabb->matches = realloc(tabb->matches, tabb->matches_size * sizeof (match_t));
}
}
tabb->matches_len++;
tabb->matches[tabb->matches_len - 1].start = m->cap_start[0];
tabb->matches[tabb->matches_len - 1].end = m->cap_end[0];
i = m->cap_end[0];
if (m->cap_size[0] == 0) {
i += UTF8_LENGTH(tabb->buf[i]);
}
}
if (tabb->matches_len == 0) {
goto end;
}
get_current_match();
move_to_match();
end:
draw_tab();
}
char *history_file () {
char *home = getenv("HOME");
if (!home) {
return NULL;
}
static char file[256];
snprintf(file, sizeof file, "%s/.les_history", home);
return file;
}
void load_search_history () {
spr = init_prompt("/");
char *file = history_file();
if (!file) {
return;
}
FILE *fp = fopen(file, "r");
if (!fp) {
return;
}
char str[512];
while (fgets(str, sizeof str, fp)) {
int len = strlen(str);
if (str[len - 1] == '\n') {
str[len - 1] = '\0';
len--;
}
if (len) {
add_history(spr, str, len);
spr->history_new++;
}
}
fclose(fp);
}
void save_search_history () {
if (spr->history_len == spr->history_new) {
return;
}
char *file = history_file();
if (!file) {
return;
}
FILE *fp = fopen(file, "a");
if (!fp) {
fprintf(stderr, "Couldn't open %s: %s\n", file, strerror(errno));
exit(1);
}
int i;
for (i = spr->history_new; i < spr->history_len; i++) {
fprintf(fp, "%s\n", spr->history[i]);
}
fclose(fp);
}
void clear_matches () {
tabb->matches = NULL;
tabb->matches_len = 0;
tabb->matches_size = 0;
tabb->current_match = 0;
tabb->highlights = NULL;
tabb->highlights_len = 0;
tabb->highlights_size = 0;
tabb->highlights_processed = 0;
draw_tab();
}
void search2 (char *pattern) {
active_search = 0;
search_version++;
tabb->search_version = search_version;
tabb->matches_len = 0;
tabb->current_match = 0;
if (!pattern || !pattern[0]) {
draw_tab();
return;
}
if (!rx) {
rx = rx_alloc();
m = rx_matcher_alloc();
}
rx_init(rx, strlen(pattern), pattern);
if (rx->error) {
alert(rx->errorstr);
return;
}
find_matches();
}
void search () {
char *pattern = gets_prompt(spr);
if (interrupt) {
draw_tab();
return;
}
search2(pattern);
}
void next_match () {
if (search_version == 0 && spr->history_len) {
search2(spr->history[spr->history_len - 1]);
return;
}
if (tabb->search_version != search_version) {
find_matches();
return;
}
if (!tabb->matches_len) {
return;
}
size_t start = tabb->matches[tabb->current_match].start;
if (start >= tlines[0].pos && start < tlines[tlines_len - 1].end_pos) {
tabb->current_match = (tabb->current_match + 1) % tabb->matches_len;
}
else {
get_current_match();
}
move_to_match();
draw_tab();
}
void prev_match () {
if (search_version == 0 && spr->history_len) {
search2(spr->history[spr->history_len - 1]);
return;
}
if (tabb->search_version != search_version) {
find_matches();
return;
}
if (!tabb->matches_len) {
return;
}
if (tabb->matches[tabb->current_match].start >= tlines[0].pos &&
tabb->matches[tabb->current_match].end < tlines[tlines_len - 1].end_pos) {
tabb->current_match = (tabb->matches_len + tabb->current_match - 1) % tabb->matches_len;
}
else {
get_current_match_backward();
}
move_to_match();
draw_tab();
}