-
Notifications
You must be signed in to change notification settings - Fork 0
/
emacs-search.c
1257 lines (1119 loc) · 31 KB
/
emacs-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
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright Neil Brown ©2015-2023 <neil@brown.name>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* Minor mode for emacs incremental search.
*
* emacs search attaches emacs-search-highlight to the document stack,
* then adds a popup search box and attaches emacs-search over it.
* We send a popup-get-target message to collect the target pane.
* We have a stack of "string,pos" for repeated search requests.
* We capture "Replace" to repeat search.
* We send "Move-View-Pos" to target to get it to refresh to a new location.
* We capture:
* :C-S - if we have a match, save end of match as new start
* :Backspace - is search string is same as saved start, pop
* otherwise remove whatever was last entered, which
* must be multiple chars if :Cw was used.
* :C-W - collect word from target and add to search string
* :C-C - collect char from target and add to search string.
* :C-R - search backwards.. tricky.
* :A-c - toggle case sensitivity (currently invisible)
*
*/
#include <stdlib.h>
#include <string.h>
#define PANE_DATA_TYPE struct es_info
#define PANE_DATA_TYPE_2 struct highlight_info
#define PANE_DATA_PTR_TYPE_3 struct pane *
#include "core.h"
#include "rexel.h"
struct es_info {
struct stk {
struct stk *next;
struct mark *m safe; /* Start of search */
unsigned int len; /* current length of search string */
short wrapped;
short case_sensitive;
short backwards;
} *s;
struct mark *start safe; /* where searching starts */
struct mark *end safe; /* where last success ended */
struct pane *target;
struct pane *replace_pane;
short matched;
short wrapped;
short backwards;
short case_sensitive;
short replaced;
};
struct highlight_info {
int view, replace_view;
char *patn;
int ci;
struct mark *start, *end, *match, *rpos, *oldpoint;
struct pane *popup, *replace_popup;
};
#include "core-pane.h"
static struct map *es_map, *er_map;
DEF_LOOKUP_CMD(search_handle, es_map);
DEF_LOOKUP_CMD(replace_handle, er_map);
static const char must_quote[] = ".|*+?{()?^$\\[";
DEF_CMD(search_forward)
{
struct es_info *esi = ci->home->data;
struct stk *s;
char *str;
struct mark *newstart;
const char *suffix;
if (!esi->target)
return Efail;
suffix = ksuffix(ci, "K:C-");
if (suffix[0])
esi->backwards = suffix[0] == 'R';
if (esi->s && mark_same(esi->s->m, esi->end)) {
if (esi->s->case_sensitive == esi->case_sensitive &&
esi->s->backwards == esi->backwards)
/* already pushed and didn't find anything new */
return 1;
esi->s->case_sensitive = esi->case_sensitive;
esi->s->backwards = esi->backwards;
}
str = call_ret(str, "doc:get-str", ci->focus);
if (!str || !*str) {
char *ss;
ss = call_ret(strsave, "history:get-last", ci->focus);
if (ss) {
call("Replace", ci->home, 1, NULL, ss);
return 1;
}
if (!str)
return Einval;
}
s = calloc(1, sizeof(*s));
s->m = esi->start;
s->len = strlen(str);
s->wrapped = esi->wrapped;
s->case_sensitive = esi->case_sensitive;
s->backwards = -1;
free(str);
s->next = esi->s;
esi->s = s;
newstart = NULL;
if (esi->matched) {
newstart = mark_dup(esi->end);
if (esi->matched == 1)
/* zero length match */
if (doc_move(esi->target, newstart,
esi->backwards ? -1 : 1) == WEOF) {
mark_free(newstart);
newstart = NULL;
}
}
if (!newstart) {
newstart = mark_dup(s->m);
esi->wrapped = 1;
call("doc:file", esi->target, esi->backwards ? 1 : -1,
newstart);
}
esi->start = newstart;
/* Trigger notification so isearch watcher searches again */
call("Replace", ci->home, 1, NULL, "");
if (!esi->matched && strcmp(ci->key, "search:again") == 0)
return Efail;
return 1;
}
DEF_CMD(search_retreat)
{
struct es_info *esi = ci->home->data;
char *str;
struct stk *s;
char *attr;
struct mark *mk;
if (esi->s == NULL)
goto just_delete;
str = call_ret(str, "doc:get-str", ci->focus);
if (!str)
return Einval;
if (strlen(str) > esi->s->len) {
free(str);
goto just_delete;
}
free(str);
s = esi->s;
esi->s = s->next;
mark_free(esi->start);
esi->start = s->m;
esi->wrapped = s->wrapped;
free(s);
/* Trigger notification so isearch watcher searches again */
call("Replace", ci->home, 1, NULL, "");
return 1;
just_delete:
if (doc_following(ci->focus, NULL) != WEOF)
/* Not at end-of-buffer, just delete one char */
return Efallthrough;
mk = call_ret(mark, "doc:point", ci->focus);
if (!mk)
return Efallthrough;
mk = mark_dup(mk);
do {
if (doc_prev(ci->focus, mk) == WEOF)
break;
attr = call_ret(strsave, "doc:get-attr", ci->focus, 0, mk, "auto");
} while (attr && strcmp(attr, "1") == 0);
call("Replace", ci->focus, 1, mk);
mark_free(mk);
return 1;
}
DEF_CMD(search_add)
{
struct es_info *esi = ci->home->data;
wint_t wch;
char b[5];
struct mark *m;
int limit = 1000;
char *attr = NULL;
struct mark *addpos = mark_dup(esi->end);
char *str = call_ret(strsave, "doc:get-str", ci->home);
int first = 1;
if (!str)
return 1;
if (!esi->target)
return Efail;
if (esi->backwards)
/* Move to end of match */
call("text-search", esi->target,
!esi->case_sensitive, addpos, str);
m = mark_dup(addpos);
if (strcmp(ci->key, "K:C-W")==0)
call("doc:word", esi->target, 1, m);
else
call("Move-Char", esi->target, 1, m);
/* Move cursor to end of search string */
call("doc:file", ci->focus, 1);
while (esi->matched
&& mark_ordered_not_same(addpos, m)) {
int slash = 0;
if (limit-- <= 0)
break;
wch = doc_next(esi->target, addpos);
if (wch == WEOF)
break;
put_utf8(b, wch);
if (wch == '\n') {
slash = 1;
strcpy(b, "n");
} else if (strchr(must_quote, wch)) {
slash = 1;
}
if (slash) {
call("Replace", ci->focus, 1, NULL, "\\",
!first, NULL, attr);
attr = ",auto=1";
first = 0;
}
call("Replace", ci->focus, 1, NULL, b,
!first, NULL, attr);
first = 0;
attr = ",auto=1";
}
return 1;
}
DEF_CMD(search_insert_quoted)
{
const char *suffix = ksuffix(ci, "doc:char-");
char *patn;
if (strchr(must_quote, suffix[0]) == NULL)
return Efallthrough;
patn = call_ret(strsave, "doc:get-str", ci->focus);
if (patn) {
char *open = strrchr(patn, '[');
if (open &&
(open == patn || open[-1] != '\\') &&
(open[1] == 0 || strchr(open+2, ']') == NULL))
/* There is an '[' that hasn't been closed, so don't
* quote anything.
*/
return Efallthrough;
}
call("Replace", ci->focus, 1, NULL, "\\");
call("Replace", ci->focus, 1, NULL, suffix,
1, NULL, ",auto=1");
return 1;
}
#include <stdio.h>
DEF_CMD(search_insert_meta)
{
/* Insert a regexp meta char.
* If it is 'open', insert the 'close' too.
* If it is 'close', skip over a close instead if possible
*/
char *bracket;
const char *brackets = "{}()[]";
const char *k = ksuffix(ci, "K:A-");
if (strchr(must_quote, *k) == NULL || !ci->mark)
return Efallthrough;
bracket = strchr(brackets, *k);
if (!bracket) {
call("Replace", ci->focus, 1, NULL, k);
} else if ((bracket - brackets) % 2) {
/* Close bracket */
if (doc_following(ci->focus, ci->mark) == (wint_t)k[0])
call("Move-Char", ci->focus, 1);
else
call("Replace", ci->focus, 1, NULL, k);
} else {
/* Open bracket */
char b[3];
strncpy(b, bracket, 2);
b[2] = 0;
call("Replace", ci->focus, 1, NULL, b);
call("Move-Char", ci->focus, -1);
}
return 1;
}
DEF_CMD_CLOSED(search_close)
{
struct es_info *esi = ci->home->data;
if (esi->target)
call("search:highlight", esi->target);
mark_free(esi->end);
esi->end = safe_cast NULL;
mark_free(esi->start);
while (esi->s) {
struct stk *n = esi->s;
esi->s = n->next;
mark_free(n->m);
free(n);
}
return 1;
}
DEF_CMD(search_again)
{
/* document has changed, retry search */
struct es_info *esi = ci->home->data;
struct pane *p;
char *a, *pfx;
int ret;
struct mark *m;
char *str;
if (!esi->target)
return Efail;
call("search:highlight", esi->target);
esi->matched = 0;
esi->replaced = 0;
m = mark_dup(esi->start);
str = call_ret(str, "doc:get-str", ci->home);
if (str == NULL || strlen(str) == 0)
/* empty string always matches */
ret = 1;
else if (esi->backwards && doc_prev(esi->target, m) == WEOF)
ret = Efail;
else {
ret = call("text-search", esi->target,
!esi->case_sensitive, m, str, esi->backwards);
}
if (ret == 0)
pfx = "Search (unavailable): ";
else if (ret == Efail) {
call("search:highlight", esi->target, 0,NULL, str,
!esi->case_sensitive);
pfx = "Failed Search: ";
} else if (ret == Einval) {
pfx = "Search (incomplete): ";
} else if (ret < 0) {
pfx = "Search (sys-error): ";
} else {
int len = --ret;
mark_to_mark(esi->end, m);
if (esi->backwards) {
while (ret > 0 && doc_next(esi->target, m) != WEOF)
ret -= 1;
call("search:highlight", esi->target, len, esi->end, str,
!esi->case_sensitive, m);
} else {
while (ret > 0 && doc_prev(esi->target, m) != WEOF)
ret -= 1;
call("search:highlight", esi->target, len, m, str,
!esi->case_sensitive, esi->end);
}
esi->matched = len + 1;
pfx = esi->backwards ? "Reverse Search: ":"Search: ";
if (esi->wrapped)
pfx = esi->backwards ? "Wrapped Reverse Search: ":"Wrapped Search: ";
}
/* HACK */
for (p = ci->home; p != p->parent; p = p->parent) {
a = attr_find(p->attrs, "prefix");
if (!a)
continue;
if (strcmp(a, pfx) != 0)
attr_set_str(&p->attrs, "prefix", pfx);
}
if (m)
mark_free(m);
free(str);
return 1;
}
DEF_CMD(search_done)
{
/* need to advance the target view to 'start', leaving
* mark at point
*/
struct es_info *esi = ci->home->data;
char *str;
struct mark *mk;
if (!esi->target)
return Efail;
if (esi->replace_pane && strcmp(ci->key, "K:Enter") == 0) {
/* if there is a replace pane, switch to it instead of closing */
pane_take_focus(esi->replace_pane);
return 1;
}
str = call_ret(str, "doc:get-str", ci->focus);
/* Move "mark" to last location, found */
call("Move-to", esi->target, 1);
mk = call_ret(mark2, "doc:point", esi->target);
if (mk)
attr_set_int(&mk->attrs, "selection:active", 0);
call("Move-to", esi->target, 0, esi->end, NULL, 1);
call("popup:close", safe_cast ci->focus->parent, 0, NULL, str);
free(str);
return 1;
}
DEF_CMD(search_escape)
{
return call("search:done", ci->focus);
}
DEF_CMD(search_clip)
{
struct es_info *esi = ci->home->data;
struct stk *s;
mark_clip(esi->start, ci->mark, ci->mark2, !!ci->num);
mark_clip(esi->end, ci->mark, ci->mark2, !!ci->num);
for (s = esi->s; s; s = s->next)
mark_clip(s->m, ci->mark, ci->mark2, !!ci->num);
return Efallthrough;
}
DEF_CMD(search_recentre)
{
/* Send this command through to target, at current location */
struct es_info *esi = ci->home->data;
if (!esi->target)
return Efail;
return call(ci->key, esi->target, ci->num, esi->end, NULL,
ci->num2);
}
DEF_CMD(search_toggle_ci)
{
struct es_info *esi = ci->home->data;
/* If not at end of doc, fall through */
if (ci->mark && doc_following(ci->focus, ci->mark) != WEOF)
return Efallthrough;
esi->case_sensitive = !esi->case_sensitive;
call("doc:notify:doc:replaced", ci->focus);
attr_set_str(&ci->home->attrs, "status-line",
esi->case_sensitive ? " Search: case sensitive " :
" Search: case insensitive ");
return 1;
}
DEF_CMD(search_replace)
{
struct pane *p;
struct es_info *esi = ci->home->data;
if (esi->replace_pane) {
pane_take_focus(esi->replace_pane);
return 1;
}
if (!esi->target)
return Efail;
p = call_ret(pane, "PopupTile", ci->focus, 0, NULL, "P", 0, NULL,
"");
if (!p)
return Efail;
attr_set_str(&p->attrs, "prompt", "Replacement");
attr_set_str(&p->attrs, "status-line", " Replacement ");
call("doc:set-name", p, 0, NULL, "Replacement");
p = pane_register_3(p, 0, &replace_handle.c, ci->focus);
if (!p)
return Efail;
p = call_ret(pane, "attach-history", p, 0, NULL, "*Replace History*");
esi->replace_pane = p;
if (p) {
pane_add_notify(ci->home, p, "Notify:Close");
home_call(esi->target, "highlight:set-popup", p, 1);
}
if (strcmp(ci->key, "K:A-%") == 0)
pane_take_focus(ci->focus);
else
pane_take_focus(p);
return 1;
}
DEF_CMD(search_notify_close)
{
struct es_info *esi = ci->home->data;
if (ci->focus == esi->replace_pane)
esi->replace_pane = NULL;
if (ci->focus == esi->target) {
esi->target = NULL;
//pane_close(ci->home);
}
return 1;
}
DEF_CMD(do_replace)
{
struct es_info *esi = ci->home->data;
const char *new = ci->str;
struct mark *m;
int len = esi->matched - 1;
if (!new)
return Enoarg;
if (len < 0)
return Efail;
if (esi->replaced)
return 1;
if (!esi->target)
return Efail;
esi->replaced = 1;
m = mark_dup(esi->end);
if (esi->backwards) {
while (len > 0 && doc_next(esi->target, m) != WEOF)
len -= 1;
mark_step(m, 0);
if (call("doc:replace", esi->target, 0, esi->end, new, 0, m) > 0) {
call("search:highlight-replace", esi->target,
strlen(new), esi->end, NULL, 0, m);
return 1;
}
} else {
while (len > 0 && doc_prev(esi->target, m) != WEOF)
len -= 1;
mark_step(m, 1);
if (strchr(new, '\\')) {
char *Pattern = call_ret(strsave, "doc:get-str", ci->home);
struct command *ptn = call_ret(comm, "make-search",
ci->home,
RXLF_ANCHORED | RXLF_BACKTRACK,
NULL, Pattern);
if (ptn) {
char *new2;
call_comm("doc:content", esi->target, ptn, 0, m);
new2 = comm_call_ret(strsave, ptn, "interp",
esi->target, 0, NULL, new);
if (new2)
new = new2;
command_put(ptn);
}
}
if (call("doc:replace", esi->target, 0, m, new, 0, esi->end) > 0) {
call("search:highlight-replace", esi->target,
strlen(new), m, NULL, 0, esi->end);
return 1;
}
}
return Efail;
}
DEF_CMD(replace_request_next)
{
struct pane *sp = ci->home->data3;
char *new;
new = call_ret(str, "doc:get-str", ci->focus);
if (call("search:replace", sp, 0, NULL, new) > 0) {
call("history:save", ci->focus, 0, NULL, new);
call("search:again", sp);
} else {
call("search:done", sp);
}
return 1;
}
DEF_CMD(replace_request)
{
struct pane *sp = ci->home->data3;
char *new;
new = call_ret(str, "doc:get-str", ci->focus);
if (call("search:replace", sp, 0, NULL, new) > 0)
call("history:save", ci->focus, 0, NULL, new);
free(new);
return 1;
}
DEF_CMD(replace_all)
{
struct pane *sp = ci->home->data3;
char *new;
int replaced = 0;
new = call_ret(str, "doc:get-str", ci->focus);
pane_set_time(ci->home);
while (call("search:replace", sp, 0, NULL, new) > 0 &&
call("search:again", sp) > 0 &&
!pane_too_long(ci->home, 2000))
replaced = 1;
if (replaced)
call("history:save", ci->focus, 0, NULL, new);
free(new);
return 1;
}
DEF_CMD(replace_to_search)
{
struct pane *sp = ci->home->data3;
pane_take_focus(sp);
return 1;
}
DEF_CMD(replace_forward)
{
struct pane *sp = ci->home->data3;
call(ci->key, sp);
return 1;
}
DEF_CMD(replace_undo)
{
return Efallthrough;
}
DEF_CMD(replace_escape)
{
struct pane *sp = ci->home->data3;
return call("search:done", sp);
}
DEF_CMD(replace_prev)
{
struct pane *home = ci->home->data3;
struct es_info *esi = home->data;
if (esi->target)
call("search:step-replace", esi->target, -1);
return 1;
}
DEF_CMD(replace_next)
{
struct pane *home = ci->home->data3;
struct es_info *esi = home->data;
if (esi->target)
call("search:step-replace", esi->target, 1);
return 1;
}
static void emacs_search_init_map(void)
{
/* Keys for the 'search' pane */
es_map = key_alloc();
key_add(es_map, "K:C-S", &search_forward);
key_add(es_map, "search:again", &search_forward);
key_add(es_map, "K:Backspace", &search_retreat);
key_add(es_map, "K:C-W", &search_add);
key_add(es_map, "K:C-C", &search_add);
key_add(es_map, "K:C-R", &search_forward);
key_add(es_map, "Close", &search_close);
key_add(es_map, "K:Enter", &search_done);
key_add(es_map, "search:done", &search_done);
key_add(es_map, "doc:replaced", &search_again);
key_add(es_map, "Notify:clip", &search_clip);
key_add(es_map, "K:C-L", &search_recentre);
key_add_range(es_map, "doc:char- ", "doc:char-~", &search_insert_quoted);
key_add_range(es_map, "K:A- ", "K:A-~", &search_insert_meta);
key_add(es_map, "K:A-c", &search_toggle_ci);
key_add(es_map, "K:A-r", &search_replace);
key_add(es_map, "K:S:Tab", &search_replace);
key_add(es_map, "K:A-%", &search_replace);
key_add(es_map, "Cancel", &search_escape);
key_add(es_map, "search:replace", &do_replace);
key_add(es_map, "Notify:Close", &search_notify_close);
/* keys for the 'replace' pane */
er_map = key_alloc();
key_add(er_map, "K:Enter", &replace_request_next);
key_add(er_map, "K:A:Enter", &replace_request);
key_add(er_map, "K:S:Tab", &replace_to_search);
key_add(er_map, "K:A-!", &replace_all);
key_add(er_map, "K:C-S", &replace_forward);
key_add(er_map, "K:C-R", &replace_forward);
key_add(er_map, "K:C-L", &replace_forward);
key_add(er_map, "Cancel", &replace_escape);
key_add(er_map, "K:Up", &replace_prev);
key_add(er_map, "K:Down", &replace_next);
key_add(er_map, "doc:reundo", &replace_undo);
}
DEF_CMD(emacs_search)
{
struct pane *p, *target;
struct es_info *esi;
struct mark *m;
if (!es_map)
emacs_search_init_map();
target = call_ret(pane, "popup:get-target", ci->focus);
if (!target)
return Efail;
m = mark_at_point(target, NULL, MARK_POINT);
if (!m)
return Efail;
p = pane_register(ci->focus, 0, &search_handle.c);
if (!p)
return Efail;
esi = p->data;
esi->target = target;
esi->end = m;
esi->start = mark_dup(m);
esi->s = NULL;
esi->matched = 1;
esi->wrapped = 0;
esi->replaced = 0;
esi->backwards = ci->num & 1;
call("doc:request:doc:replaced", p);
attr_set_str(&p->attrs, "status-line", " Search: case insensitive ");
comm_call(ci->comm2, "callback:attach", p);
pane_add_notify(p, esi->target, "Notify:Close");
if (ci->num & 2)
call("K:A-%", p);
return 1;
}
static void do_searches(struct pane *p safe,
struct pane *owner safe, int view, char *patn,
int ci,
struct mark *m, struct mark *end)
{
int ret;
struct highlight_info *hi = owner->data2;
struct mark *start;
if (!m)
return;
m = mark_dup(m);
start = mark_dup(m);
while ((ret = call("text-search", p, ci, m, patn, 0, end)) >= 1) {
struct mark *m2, *m3;
int len = ret - 1;
m2 = vmark_new(p, view, owner);
if (!m2)
break;
mark_to_mark(m2, m);
while (ret > 1 && doc_prev(p, m2) != WEOF)
ret -= 1;
m3 = vmark_matching(m2);
if (m3) {
mark_free(m2);
m2 = m3;
}
if (attr_find(m2->attrs, "render:search") == NULL) {
bool match = hi->match && mark_same(hi->match, m2);
attr_set_int(&m2->attrs,
match ? "render:search" : "render:search2",
len);
call("view:changed", p, 0, m2, NULL, 0, m);
m2 = vmark_new(p, view, owner);
if (m2) {
mark_to_mark(m2, m);
attr_set_int(&m2->attrs,
match ? "render:search-end"
: "render:search2-end",
0);
}
}
if (len == 0 || mark_ordered_or_same(m, start))
/* Need to move forward, or we'll just match here again*/
doc_next(p, m);
mark_free(start);
start = mark_dup(m);
}
mark_free(start);
mark_free(m);
}
static void queue_highlight_refresh(struct pane *p safe);
DEF_CMD(emacs_search_highlight)
{
/* from 'mark' for 'num' chars to 'mark2' there is a match for 'str',
* or else there are no matches (num==0).
* Here we remove any existing highlighting and highlight
* just the match. A subsequent call to emacs_search_reposition
* will highlight other near-by matches.
*/
struct mark *m, *start;
struct highlight_info *hi = ci->home->data2;
if (hi->view < 0)
return Efail;
while ((start = vmark_first(ci->focus, hi->view, ci->home)) != NULL)
mark_free(start);
free(hi->patn);
if (ci->str)
hi->patn = strdup(ci->str);
else
hi->patn = NULL;
hi->ci = ci->num2;
mark_free(hi->match);
hi->match = NULL;
if (hi->oldpoint) {
call("Move-to", ci->focus, 0, hi->oldpoint);
mark_free(hi->rpos);
mark_free(hi->oldpoint);
hi->rpos = NULL;
hi->oldpoint = NULL;
}
if (ci->mark && ci->num >= 0 && ci->str) {
m = vmark_new(ci->focus, hi->view, ci->home);
if (!m)
return Efail;
mark_to_mark(m, ci->mark);
attr_set_int(&m->attrs, "render:search", ci->num);
call("Move-View-Pos", ci->focus, 0, m);
hi->match = mark_dup(ci->mark);
if (ci->mark2 &&
(m = vmark_new(ci->focus, hi->view, ci->home)) != NULL) {
mark_to_mark(m, ci->mark2);
attr_set_int(&m->attrs, "render:search-end", 0);
}
} else if (ci->str) {
/* No destination to move to, so just refresh whatever
* is visible
*/
queue_highlight_refresh(ci->focus);
}
call("view:changed", ci->focus);
call("render:request:reposition", ci->focus);
return 1;
}
DEF_CMD(emacs_replace_highlight)
{
/* from 'mark' for 'num' chars to 'mark2' there is a recent
* replacement in a search/replace.
* The existing render:search{-end} marks which are near mark2
* need to be discarded, and new "render:replacement" need to
* be added.
*/
struct mark *m;
struct highlight_info *hi = ci->home->data2;
if (hi->replace_view < 0 || !hi->replace_popup)
return Efail;
if (!ci->mark || !ci->mark2)
return Enoarg;
while ((m = vmark_at_or_before(ci->focus, ci->mark2,
hi->view, ci->home)) != NULL &&
(attr_find_int(m->attrs, "render:search") >= 0 ||
attr_find_int(m->attrs, "render:search-end") >= 0))
mark_free(m);
m = vmark_new(ci->focus, hi->replace_view, ci->home);
if (m) {
mark_to_mark(m, ci->mark);
attr_set_int(&m->attrs, "render:replacement", ci->num);
}
m = vmark_new(ci->focus, hi->replace_view, ci->home);
if (m) {
mark_to_mark(m, ci->mark2);
attr_set_int(&m->attrs, "render:replacement-end", 0);
}
call("view:changed", ci->focus);
return 1;
}
DEF_CMD(emacs_hl_attrs)
{
struct highlight_info *hi = ci->home->data2;
if (!ci->str)
return Efallthrough;
if (!hi->popup)
return Efallthrough;
if (strcmp(ci->str, "render:search") == 0) {
/* Current search match - "220" is a priority */
if (hi->view >= 0 && ci->mark && ci->mark->viewnum == hi->view) {
int len = atoi(ci->str2) ?: 1;
return comm_call(ci->comm2, "attr:callback", ci->focus, len,
ci->mark, "fg:red,inverse,focus,vis-nl", 220);
}
}
if (strcmp(ci->str, "render:search2") == 0) {
/* alternate matches in current view */
if (hi->view >= 0 && ci->mark && ci->mark->viewnum == hi->view) {
int len = atoi(ci->str2) ?: 1;
return comm_call(ci->comm2, "attr:callback", ci->focus, len,
ci->mark, "fg:blue,inverse,vis-nl", 220);
}
}
if (strcmp(ci->str, "render:replacement") == 0) {
/* Replacement - "220" is a priority */
if (hi->replace_view >= 0 && ci->mark &&
ci->mark->viewnum == hi->replace_view) {
int len = atoi(ci->str2) ?: 1;
return comm_call(ci->comm2, "attr:callback", ci->focus, len,
ci->mark, "fg:green-40,inverse,vis-nl", 220);
}
}
if (strcmp(ci->str, "start-of-line") == 0 && ci->mark && hi->view >= 0) {
struct mark *m = vmark_at_or_before(ci->focus, ci->mark, hi->view, ci->home);
if (m && mark_same(m, ci->mark))
m = NULL;
if (m && attr_find_int(m->attrs, "render:search") > 0)
return comm_call(ci->comm2, "attr:callback", ci->focus, 0,
ci->mark, "fg:red,inverse,vis-nl", 220);
if (m && attr_find_int(m->attrs, "render:search2") > 0)
return comm_call(ci->comm2, "attr:callback", ci->focus, 0,
ci->mark, "fg:blue,inverse,vis-nl", 220);
}
if (strcmp(ci->str, "render:search-end") ==0) {
/* Here endeth the match */
return comm_call(ci->comm2, "attr:callback", ci->focus, -1,
ci->mark, "fg:red,inverse,vis-nl", 220);
}
if (strcmp(ci->str, "render:search2-end") ==0) {
/* Here endeth the match */
return comm_call(ci->comm2, "attr:callback", ci->focus, -1,
ci->mark, "fg:blue,inverse,vis-nl", 220);
}
if (strcmp(ci->str, "render:replacement-end") ==0) {
/* Here endeth the replacement */
return comm_call(ci->comm2, "attr:callback", ci->focus, -1,
ci->mark, "fg:green-40,inverse,vis-nl", 220);
}
return Efallthrough;
}
DEF_CMD(highlight_draw)
{
struct highlight_info *hi = ci->home->data2;
struct pane *pp = hi->popup;
struct pane *pp2 = hi->replace_popup;
struct xy xy;
if (!ci->str2 || !strstr(ci->str2, ",focus") || !pp)
return Efallthrough;
/* here is where the user will be looking, make sure
* the popup doesn't obscure it.
*/
xy = pane_mapxy(ci->focus, ci->home, ci->x, ci->y, False);
while (pp->parent != pp && pp->z == 0)
pp = pp->parent;
while (pp2 && pp2->parent != pp2 && pp2->z == 0)
pp2 = pp2->parent;
if (pp->x == 0) {
/* currently TL, should we move it back */
if (xy.x < pp->w ||
(xy.y > pp->h &&
(!pp2 || xy.y > pp2->y + pp2->h)))
call("popup:style", hi->popup, 0, NULL, "TR2");
} else {
/* currently TR, should we move it out of way */
if (xy.x >= pp->x &&
(xy.y <= pp->h ||
(pp2 && xy.y <= pp2->y + pp2->h)))
call("popup:style", hi->popup, 0, NULL, "TL2");
}
return Efallthrough;
}
DEF_CMD(emacs_search_reposition_delayed)
{
struct highlight_info *hi = ci->home->data2;
struct mark *start = hi->start;
struct mark *end = hi->end;
struct mark *vstart, *vend;
char *patn = hi->patn;
if (!start || !end)
return Efalse;