-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib-linefilter.c
557 lines (504 loc) · 13.1 KB
/
lib-linefilter.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
* Copyright Neil Brown ©2015-2023 <[email protected]>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* line-filter: hide (un)selected lines from display
*
* This can be layered over render-format or similar and will restrict
* which lines are shown, based on some attribute visible at the start
* of the line, or the content of the line. How this content is assessed
* can be set by a call to Filter:set, or by setting various attributes.
* - filter:match - a string that must appear in the content
* - filter:attr - the text attribute which contains the content
* - filter:at_start - whether match must be at start of content
* - filter:ignore_case - whether to ignore case when comparing match
* against content.
*
* This module doesn't hold any marks on any document. The marks
* held by the renderer should be sufficient.
*/
#define _GNU_SOURCE for strcasestr
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define PANE_DATA_TYPE struct filter_data
#include "core.h"
#include "misc.h"
struct filter_data {
char *match;
int match_len;
char *attr;
bool at_start;
bool ignore_case;
bool explicit_set;
bool implicit_set;
};
#include "core-pane.h"
struct rlcb {
struct command c;
struct filter_data *fd;
int keep, cmp;
const char *str;
int cursor_offset;
};
static void strip_attrs(char *c safe)
{
char *n = c;
if (*c == ack) {
for (; *c; c++) {
if (*c == ack || *c == etx)
continue;
if (*c != soh) {
*n++ = *c;
continue;
}
while (*c != stx)
c++;
}
} else {
for (; *c ; c++) {
if (*c == '<' && c[1] == '<') {
*n++ = *c++;
continue;
}
if (*c != '<') {
*n++ = *c;
continue;
}
while (*c != '>')
c++;
}
}
*n = 0;
}
DEF_CB(rlcb)
{
struct rlcb *cb = container_of(ci->comm, struct rlcb, c);
char *c = ci->str ? strdup(ci->str) : NULL;
if (c && strcmp(ci->key, "attr") != 0)
/* This is a rendered line so we need to strip out attrs. */
strip_attrs(c);
if (cb->fd == NULL)
cb->cmp = 0; /* Don't compare, just save */
else if (c == NULL)
cb->cmp = -1;
else if (cb->fd->match == NULL)
cb->cmp = 0;
else if (cb->fd->at_start && cb->fd->ignore_case)
cb->cmp = strncasecmp(c, cb->fd->match, cb->fd->match_len);
else if (cb->fd->at_start)
cb->cmp = strncmp(c, cb->fd->match, cb->fd->match_len);
else if (cb->fd->ignore_case)
cb->cmp = strcasestr(c, cb->fd->match) ? 0 : 1;
else
cb->cmp = strstr(c, cb->fd->match) ? 0 : 1;
if (cb->cmp == 0 && cb->keep && !cb->str && c && ci->str) {
if (cb->keep > 1)
/* Want the original with markup */
strcpy(c, ci->str);
cb->str = c;
cb->cursor_offset = ci->num;
} else
free(c);
return 1;
}
static bool check_settings(struct pane *focus safe,
struct filter_data *fd safe)
{
char *s;
bool changed = False;
bool v;
if (fd->explicit_set || fd->implicit_set)
return False;
s = pane_attr_get(focus, "filter:match");
if (s && (!fd->match || strcmp(s, fd->match) != 0)) {
free(fd->match);
fd->match = strdup(s);
fd->match_len = strlen(s);
changed = True;
}
s = pane_attr_get(focus, "filter:attr");
if ((!!s != !!fd->attr) ||
(s && fd->attr && strcmp(s, fd->attr) != 0)) {
free(fd->attr);
fd->attr = s ? strdup(s) : NULL;
changed = True;
}
s = pane_attr_get(focus, "filter:at_start");
v = s && *s && strchr("Yy1Tt", *s) != NULL;
if (v != fd->at_start) {
changed = True;
fd->at_start = v;
}
s = pane_attr_get(focus, "filter:ignore_case");
v = s && *s && strchr("Yy1Tt", *s) != NULL;
if (v != fd->ignore_case) {
changed = True;
fd->ignore_case = v;
}
fd->implicit_set = True;
return changed;
}
DEF_CMD(render_filter_line)
{
/* Skip any line that doesn't match, and
* return a highlighted version of the first one
* that does.
* Then skip over any other non-matches.
*/
struct filter_data *fd = ci->home->data;
struct rlcb cb;
int ret;
struct mark *m;
struct mark *m2;
if (!ci->mark)
return Enoarg;
check_settings(ci->focus, fd);
m = mark_dup(ci->mark);
cb.c = rlcb;
cb.fd = fd;
cb.str = NULL;
cb.keep = 0;
cb.cmp = 0;
do {
mark_to_mark(ci->mark, m);
cb.cmp = 0;
if (fd->attr) {
comm_call(&cb.c, "attr", ci->focus,
NO_NUMERIC, NULL,
pane_mark_attr(ci->focus, m, fd->attr));
home_call(ci->home->parent, ci->key, ci->focus,
NO_NUMERIC, m);
} else
home_call_comm(ci->home->parent, ci->key, ci->focus,
&cb.c, NO_NUMERIC, m);
} while (cb.cmp && !mark_same(ci->mark, m));
mark_free(m);
cb.keep = 2;
cb.str = NULL;
cb.fd = NULL;
m2 = ci->mark2;
if (home_call_comm(ci->home->parent, ci->key, ci->focus, &cb.c,
ci->num, ci->mark, NULL, 0, m2) < 0)
return Efail;
ret = comm_call(ci->comm2, "callback:render", ci->focus,
cb.cursor_offset, NULL, cb.str);
free((void*)cb.str);
if (m2)
/* Was rendering to find a cursor, don't need to skip */
return ret;
/* Need to continue over other non-matching lines */
m = mark_dup(ci->mark);
cb.keep = 0;
cb.str = NULL;
cb.fd = fd;
do {
/* have a non-match, so move the mark over it. */
mark_to_mark(ci->mark, m);
cb.cmp = 0;
if (fd->attr) {
comm_call(&cb.c, "attr", ci->focus,
NO_NUMERIC, NULL,
pane_mark_attr(ci->focus, m, fd->attr));
home_call(ci->home->parent, ci->key, ci->focus,
NO_NUMERIC, m);
} else
home_call_comm(ci->home->parent, ci->key, ci->focus,
&cb.c, NO_NUMERIC, m);
} while (cb.cmp && !mark_same(ci->mark, m));
mark_free(m);
return ret ?: 1;
}
static int do_filter_line_prev(struct filter_data *fd safe,
struct mark *m safe,
struct pane *home safe,
struct pane *focus safe, int n,
const char **savestr)
{
/* Move to start of this or previous real line and
* check if it passes the filter.
* If we get an error, return that. else 0 if it doesn't
* pass, else 1.
*/
struct rlcb cb;
int ret;
if (savestr)
*savestr = NULL;
cb.c = rlcb;
cb.str = NULL;
cb.fd = fd;
cb.cmp = 0;
ret = home_call(home, "doc:render-line-prev", focus, n, m);
if (ret < 0)
/* Probably hit start-of-file */
return ret;
if (doc_following(home, m) == WEOF)
/* End of file, no match possible */
return 0;
/* we must be looking at a possible option for the previous line */
cb.keep = !!savestr;
cb.str = NULL;
if (fd->attr) {
comm_call(&cb.c, "attr", focus,
NO_NUMERIC, NULL,
pane_mark_attr(focus, m, fd->attr));
} else {
struct mark *m2 = mark_dup(m);
ret = home_call_comm(home, "doc:render-line", focus, &cb.c,
NO_NUMERIC, m2);
mark_free(m2);
if (ret <= 0)
return Efail;
}
if (savestr)
*savestr = cb.str;
return cb.cmp == 0;
}
DEF_CMD(render_filter_prev)
{
struct filter_data *fd = ci->home->data;
struct mark *m = ci->mark;
int ret;
if (!m)
return Enoarg;
check_settings(ci->focus, fd);
if (!fd->match)
return Efallthrough;
/* First, make sure we are at a start-of-line */
ret = do_filter_line_prev(fd, m, ci->home->parent, ci->focus, 0, NULL);
if (ret < 0)
return ret;
while (ret == 0) {
/* That wasn't a matching line, try again */
ret = do_filter_line_prev(fd, m, ci->home->parent, ci->focus,
1, NULL);
}
if (!ci->num)
/* Only wanted start of line - found */
return 1;
ret = 0;
while (ret == 0) {
ret = do_filter_line_prev(fd, m, ci->home->parent, ci->focus,
1, NULL);
if (ret < 0)
/* Error */
return ret;
}
return ret;
}
DEF_CMD(filter_changed)
{
/* Update match info from arg (Filter:set) or pane attrs (unless
* Filter:set has been used), then walk over a range of marks
* calling Notify:clip as needed, and for Filter:set, call comm2
* with the matching string.
*
* If no marks are given, we walk entire doc.
* otherwise find first visible line after all marks, and last before,
* and walk that range
*/
struct filter_data *fd = ci->home->data;
struct mark *start, *end, *m;
struct command *comm = NULL;
bool found_one = False;
if (strcmp(ci->key, "Filter:set") == 0) {
if (!ci->str)
return Enoarg;
call("view:changed", pane_focus(ci->home));
comm = ci->comm2;
fd->explicit_set = True;
free(fd->match);
free(fd->attr);
fd->match = strdup(ci->str);
fd->match_len = strlen(fd->match);
fd->attr = ci->str2 ? strdup(ci->str2) : NULL;
fd->at_start = !!(ci->num & 1);
fd->ignore_case = !!(ci->num & 2);
}
if (!fd->explicit_set) {
fd->implicit_set = False;
if (check_settings(ci->focus, fd))
/* Something changed */
call("view:changed", pane_focus(ci->home));
}
if (!fd->match)
return 1;
start = mark_new(ci->focus);
if (!start)
return Efail;
if (ci->mark && (!ci->mark2 || ci->mark2->seq > ci->mark->seq))
/* mark is first */
mark_to_mark(start, ci->mark);
else if (ci->mark2)
/* mark2 is first */
mark_to_mark(start, ci->mark2);
else if (strcmp(ci->key, "Filter:set") == 0)
call("doc:file", ci->focus, -1, start);
else {
struct mark *m2;
m = start;
while ((m2 = mark_prev(m)) != NULL)
m = m2;
mark_to_mark(start, m);
}
end = mark_new(ci->focus);
if (!end) {
mark_free(start);
return Efail;
}
if (ci->mark && (!ci->mark2 || ci->mark2->seq < ci->mark->seq))
/* mark is last */
mark_to_mark(end, ci->mark);
else if (ci->mark2)
/* mark2 is last */
mark_to_mark(end, ci->mark2);
else if (strcmp(ci->key, "Filter:set") == 0)
call("doc:file", ci->focus, 1, end);
else {
struct mark *m2;
m = end;
while ((m2 = mark_next(m)) != NULL)
m = m2;
mark_to_mark(end, m);
}
if (call("doc:render-line", ci->focus, -1, end) > 0)
found_one = True;
m = mark_dup(end);
while (m->seq > start->seq || !found_one) {
int ret;
const char *str = NULL;
struct mark *m2 = mark_dup(m);
ret = do_filter_line_prev(fd, m, ci->home->parent, ci->focus, 1,
comm ? &str : NULL);
if (ret > 0) {
/* m is a good line, m2 is like end */
found_one = True;
if (!mark_same(m2, end))
call("Notify:clip", ci->focus, 0, m2, NULL,
0, end);
mark_to_mark(end, m);
if (comm && str)
comm_call(comm, "", ci->focus, 0, m, str);
}
free((void*)str);
mark_free(m2);
if (ret < 0)
break;
}
/* No matching lines found between m and end, so clip them */
if (!mark_same(m, end))
call("Notify:clip", ci->focus, 0, m, NULL, 0, end);
mark_free(m);
mark_free(start);
mark_free(end);
if (!found_one)
/* filtered document is now empty - maybe someone cares */
home_call(ci->focus, "Notify:filter:empty", ci->home);
return 1;
}
DEF_CMD(eol_cb)
{
/* don't save anything */
return 1;
}
DEF_CMD(filter_eol)
{
struct filter_data *fd = ci->home->data;
int rpt = RPT_NUM(ci);
bool one_more = ci->num2 > 0;
int line;
check_settings(ci->focus, fd);
if (!ci->mark)
return Enoarg;
if (rpt < 0)
line = rpt + 1 - one_more;
else
line = rpt - 1 + one_more;
/* 'line' is which line to go to relative to here */
if (line == 0) {
if (rpt < 0)
/* Start of line */
call("doc:EOL", ci->home->parent, -1, ci->mark);
else
/* End of line */
call("doc:EOL", ci->home->parent, 1, ci->mark);
return 1;
}
/* Must be at start of line for filtering to work */
call("doc:EOL", ci->home->parent, -1, ci->mark);
while (line < 0) {
int ret;
ret = do_filter_line_prev(fd, ci->mark,
ci->home->parent, ci->focus, 1, NULL);
if (ret < 0)
line = 0;
if (ret > 0)
line += 1;
}
while (line > 0) {
struct call_return cr;
cr.c = eol_cb;
if (home_call(ci->home, "doc:render-line",
ci->focus, -1, ci->mark, NULL,
0, NULL, NULL, 0,0, &cr.c) <= 0)
line = 1;
line -= 1;
}
if ((rpt < 0 && !one_more) || (rpt > 0 && one_more))
/* Target was start of a line, so we are there */
return 1;
call("doc:EOL", ci->home->parent, 1, ci->mark);
return 1;
}
DEF_CMD(filter_damaged)
{
// We need this for doc:replaced so that when the
// view becomes empty we can Notify:filter:empty.
// We used to do it for view:changed as well, but
// causes lots of updates for large directories
// that are slow, and don't seem to change anything.
pane_damaged(ci->home, DAMAGED_VIEW);
return Efallthrough;
}
DEF_CMD(filter_attach);
DEF_CMD(filter_clone)
{
struct pane *parent = ci->focus;
filter_attach.func(ci);
pane_clone_children(ci->home, parent->focus);
return 1;
}
static struct map *filter_map;
DEF_LOOKUP_CMD(filter_handle, filter_map);
static void filter_register_map(void)
{
if (filter_map)
return;
filter_map = key_alloc();
key_add(filter_map, "doc:render-line", &render_filter_line);
key_add(filter_map, "doc:render-line-prev", &render_filter_prev);
key_add(filter_map, "Clone", &filter_clone);
key_add(filter_map, "doc:EOL", &filter_eol);
key_add(filter_map, "Filter:set", &filter_changed);
// handling view:changed seems to cause unnecesary
// work.
// key_add(filter_map, "view:changed", &filter_damaged);
key_add(filter_map, "doc:replaced", &filter_damaged);
key_add(filter_map, "Refresh:view", &filter_changed);
}
REDEF_CMD(filter_attach)
{
struct pane *filter;
filter_register_map();
filter = pane_register(ci->focus, 0, &filter_handle.c);
if (!filter)
return Efail;
pane_damaged(filter, DAMAGED_VIEW);
call("doc:request:doc:replaced", filter);
return comm_call(ci->comm2, "", filter);
}
void edlib_init(struct pane *ed safe)
{
call_comm("global-set-command", ed, &filter_attach,
0, NULL, "attach-linefilter");
}