-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.c
1130 lines (946 loc) · 20.9 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
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
/*
* Search commands.
*
* The functions in this file implement the search commands (both
* plain and incremental searches are supported) and the query-replace
* command.
*
* The plain old search code is part of the original MicroEMACS
* "distribution". The incremental search code, and the query-replace
* code, is by Rich Ellison.
*/
#include "def.h"
#include "macro.h"
#define SRCH_BEGIN (0) /* Search sub-codes. */
#define SRCH_FORW (-1)
#define SRCH_BACK (-2)
#define SRCH_NOPR (-3)
#define SRCH_ACCM (-4)
#define SRCH_MARK (-5)
typedef struct {
INT s_code;
LINE *s_dotp;
INT s_doto;
} SRCHCOM;
static SRCHCOM cmds[NSRCH];
static INT cip;
INT srch_lastdir = SRCH_FORW; // Last search flags.
INT case_fold_search = 1; // Case-insensitive search
INT word_search = 0; // Word search
#ifdef SEARCHSIMPLE
INT searchissimple = 0;
#endif
static void is_cpush(INT cmd);
static void is_lpush(void);
static void is_pop(void);
static INT is_peek(void);
static void is_undo(INT *pptr, INT *dir);
static INT is_find(INT dir);
static void is_prompt(INT dir, INT flag, INT success);
static void is_dspl(char *prompt, INT flag);
static INT eq(INT bc, INT pc);
INT forwsrch(INT word_search, int incremental);
INT backsrch(INT word_search);
INT readpattern(char *prompt);
INT isearch(INT dir);
INT ctrlg(INT f, INT n);
INT forwchar(INT f, INT n);
INT backchar(INT f, INT n);
INT killwholeline(INT f, INT n);
/*
* Mg3a: word definition for word search
*/
static int
isword(INT c)
{
return c == '_' || ucs_isalnum(c) || ucs_nonspacing(c);
}
/*
* Mg3a: Toggle word search mode
*/
INT
word_search_mode(INT f, INT n)
{
if (f & FFARG) word_search = (n > 0);
else word_search = !word_search;
ewprintf("Word search is %s", word_search ? "on" : "off");
return TRUE;
}
/*
* Mg3a: Toggle case-folded search mode
*/
INT
case_fold_mode(INT f, INT n)
{
if (f & FFARG) case_fold_search = (n > 0);
else case_fold_search = !case_fold_search;
ewprintf("Case folded search is %s", case_fold_search ? "on" : "off");
return TRUE;
}
#ifdef SEARCHALL
static BUFFER *firstbuf = NULL;
static INT searchcount = 0;
/*
* Mg3a: Next non-system buffer (circular)
*/
static BUFFER *
nextbuf(BUFFER *bp)
{
BUFFER *new = bp;
while (1) {
new = new->b_bufp;
if (new == NULL) new = bheadp;
if (new->b_bname[0] != '*') return new;
if (new == bp) return bp;
}
}
/*
* Mg3a: Previous non-system buffer (circular)
*/
static BUFFER *
prevbuf(BUFFER *bp)
{
BUFFER *new = bheadp, *save=bheadp;
while (1) {
if (new->b_bname[0] != '*') save = new;
new = new->b_bufp;
if (new == NULL) return save;
if (new == bp) return save;
}
}
/*
* Mg3a: Set position in a new buffer appropriate for search
*/
static void
searchallsetpos(BUFFER *bp, INT direction)
{
curbp = bp;
showbuffer(curbp, curwp);
if (direction == SRCH_FORW) {
adjustpos3(lforw(curbp->b_linep), 0, 0);
} else {
adjustpos3(lback(curbp->b_linep), llength(lback(curbp->b_linep)), curbp->b_size);
}
}
/*
* Mg3a: Get a pattern from some source for multi-buffer search
* Return True if got a pattern.
*/
static INT
searchallsetpat(INT f, INT direction)
{
if (f & FFARG) {
if (direction == SRCH_FORW) {
return readpattern("Search all");
} else {
return readpattern("Search all backward");
}
} else if (pat[0]) {
if (firstbuf == NULL) {
firstbuf = curbp;
searchallsetpos(curbp, direction);
}
return TRUE;
}
ewprintf("No search string");
return FALSE;
}
/*
* Mg3a: Search forward through all non-system buffers
*/
INT
forwsearchall(INT f, INT n)
{
BUFFER *startbuf = curbp, *bp;
INT s;
if ((s = searchallsetpat(f, SRCH_FORW)) != TRUE) return s;
srch_lastdir = SRCH_FORW;
while (1) {
if (forwsrch(word_search, 0) == FALSE) {
bp = nextbuf(curbp);
searchallsetpos(bp, srch_lastdir);
if (bp == startbuf || bp == firstbuf) break;
} else {
searchcount++;
if (!(f & FFRAND)) ewprintf("Match #%d", searchcount);
return TRUE;
}
}
if (searchcount > 0) {
if (!(f & FFRAND)) ewprintf("%d matches found", searchcount);
searchcount = 0;
return TRUE;
} else {
ewprintf("Search all failed: \"%s\"", pat);
return FALSE;
}
}
/*
* Mg3a: Search backward through all non-system buffers
*/
INT
backsearchall(INT f, INT n)
{
BUFFER *startbuf = curbp, *bp;
INT s;
if ((s = searchallsetpat(f, SRCH_BACK)) != TRUE) return s;
srch_lastdir = SRCH_BACK;
while (1) {
if (backsrch(word_search) == FALSE) {
bp = prevbuf(curbp);
searchallsetpos(bp, srch_lastdir);
if (bp == startbuf || bp == firstbuf) break;
} else {
searchcount++;
ewprintf("Match #%d", searchcount);
return TRUE;
}
}
if (searchcount > 0) {
ewprintf("%d matches found", searchcount);
searchcount = 0;
return TRUE;
} else {
ewprintf("Search all failed: \"%s\"", pat);
return FALSE;
}
}
#endif
/*
* Search forward. Get a search string from the user, and search for
* it, starting at ".". If found, "." gets moved to just after the
* matched characters, and display does all the hard stuff. If not
* found, it just prints a message.
*/
INT
forwsearch(INT f, INT n)
{
INT s;
if ((s=readpattern("Search")) != TRUE)
return s;
if (forwsrch(word_search, 0) == FALSE) {
ewprintf("Search failed: \"%s\"", pat);
return FALSE;
}
srch_lastdir = SRCH_FORW;
return TRUE;
}
/*
* Reverse search. Get a search string from the user, and search,
* starting at "." and proceeding toward the front of the buffer. If
* found "." is left pointing at the first character of the pattern
* [the last character that was matched].
*/
INT
backsearch(INT f, INT n)
{
INT s;
if ((s=readpattern("Search backward")) != TRUE)
return (s);
if (backsrch(word_search) == FALSE) {
ewprintf("Search failed: \"%s\"", pat);
return FALSE;
}
srch_lastdir = SRCH_BACK;
return TRUE;
}
/*
* Search again, using the same search string and direction as the
* last search command. The direction has been saved in
* "srch_lastdir", so you know which way to go.
*/
INT
searchagain(INT f, INT n)
{
INT result;
#ifdef SEARCHSIMPLE
if (searchissimple) return internal_searchsimple(pat, 1);
#endif
if (pat[0] == 0 || srch_lastdir == SRCH_NOPR) {
ewprintf("No last search");
return FALSE;
}
if (srch_lastdir == SRCH_FORW) {
result = forwsrch(word_search, 0);
} else {
result = backsrch(word_search);
}
if (result == FALSE) {
ewprintf("Search failed: \"%s\"", pat);
return FALSE;
}
return TRUE;
}
/*
* Use incremental searching, initially in the forward direction.
* isearch ignores any explicit arguments.
*/
INT
forwisearch(INT f, INT n)
{
return isearch(SRCH_FORW);
}
/*
* Use incremental searching, initially in the reverse direction.
* isearch ignores any explicit arguments.
*/
INT
backisearch(INT f, INT n)
{
return isearch(SRCH_BACK);
}
/*
* Incremental Search.
* dir is used as the initial direction to search.
* ^S switch direction to forward
* ^R switch direction to reverse
* ^Q quote next character (allows searching for ^N etc.)
* <ESC> exit from Isearch
* <DEL> undoes last character typed. (tricky job to do this correctly).
* other ^ exit search, don't set mark
* else accumulate into search string
*/
INT
isearch(INT dir)
{
INT c;
LINE *clp;
INT cbo;
INT success;
INT pptr;
char opat[NPAT];
unsigned char charbuf[4];
INT i, charlen;
#ifndef NO_MACRO
if (macrodef) {
return emessage(ABORT, "Can't isearch in macro");
}
#endif
for (cip=0; cip<NSRCH; cip++)
cmds[cip].s_code = SRCH_NOPR;
stringcopy(opat, pat, NPAT);
cip = 0;
pptr = -1;
clp = curwp->w_dotp;
cbo = curwp->w_doto;
is_lpush();
is_cpush(SRCH_BEGIN);
success = TRUE;
is_prompt(dir, TRUE, success);
for (;;) {
update();
switch (c = getkey(FALSE)) {
#ifdef SEARCHENTER
/*
* Enter key will end the search and set the mark as Esc
*/
case CCHR('J'):
#endif
case CCHR('['):
srch_lastdir = dir;
curwp->w_markp = clp;
curwp->w_marko = cbo;
ewprintf("Mark set");
/*
* You may want to start an Esc-sequence here
*/
if ((c == CCHR(']') && (type_ahead())))
ungetkey(c); // Escape sequence
return (TRUE);
case CCHR('G'):
if (success != TRUE) {
while (is_peek() == SRCH_ACCM)
is_undo(&pptr, &dir);
success = TRUE;
is_prompt(dir, pptr < 0, success);
break;
}
adjustpos(clp, cbo);
srch_lastdir = dir;
ctrlg(FFRAND, 0);
stringcopy(pat, opat, NPAT);
return ABORT;
case CCHR(']'):
case CCHR('S'):
if (dir == SRCH_BACK) {
dir = SRCH_FORW;
is_lpush();
is_cpush(SRCH_FORW);
success = TRUE;
}
if (success==FALSE && dir==SRCH_FORW)
break;
is_lpush();
pptr = strlen(pat);
forwchar(FFRAND, 1);
if (is_find(SRCH_FORW) != FALSE) is_cpush(SRCH_MARK);
else {
backchar(FFRAND, 1);
ttbeep();
success = FALSE;
}
is_prompt(dir, pptr < 0, success);
break;
case CCHR('R'):
if (dir == SRCH_FORW) {
dir = SRCH_BACK;
is_lpush();
is_cpush(SRCH_BACK);
success = TRUE;
}
if (success==FALSE && dir==SRCH_BACK)
break;
is_lpush();
pptr = strlen(pat);
backchar(FFRAND, 1);
if (is_find(SRCH_BACK) != FALSE) is_cpush(SRCH_MARK);
else {
forwchar(FFRAND, 1);
ttbeep();
success = FALSE;
}
is_prompt(dir, pptr < 0, success);
break;
case CCHR('H'):
case CCHR('?'):
is_undo(&pptr, &dir);
if (is_peek() != SRCH_ACCM) success = TRUE;
is_prompt(dir, pptr < 0, success);
break;
case CCHR('\\'):
case CCHR('Q'):
c = (char) getkey(FALSE);
goto addchar;
#ifndef SEARCHENTER
case CCHR('J'):
#endif
case CCHR('M'):
if (pptr == -1) {
if (dir == SRCH_FORW) {
return forwsearch(0, 1);
} else {
return backsearch(0, 1);
}
}
c = CCHR('J');
goto addchar;
default:
if (c < 256 && ISCTRL(c)) {
ungetkey(c);
srch_lastdir = dir;
curwp->w_markp = clp;
curwp->w_marko = cbo;
ewprintf("Mark set");
return TRUE;
} /* and continue */
case CCHR('I'):
addchar:
if (pptr == -1)
pptr = 0;
if (pptr == 0)
success = TRUE;
#ifdef UTF8
ucs_chartostr(termcharset, c, charbuf, &charlen);
if (pptr + charlen >= NPAT) {
ewprintf("Pattern too long");
return FALSE;
}
for (i = 0; i < charlen; i++) {
pat[pptr++] = charbuf[i];
}
#else
pat[pptr++] = c;
if (pptr == NPAT) {
ewprintf("Pattern too long");
return FALSE;
}
#endif
pat[pptr] = '\0';
is_lpush();
if (success != FALSE) {
if (is_find(dir) != FALSE)
is_cpush(c);
else {
success = FALSE;
ttbeep();
is_cpush(SRCH_ACCM);
}
} else
is_cpush(SRCH_ACCM);
is_prompt(dir, FALSE, success);
}
}
/*NOTREACHED*/
}
static void
is_cpush(INT cmd) {
if (++cip >= NSRCH)
cip = 0;
cmds[cip].s_code = cmd;
}
static void
is_lpush() {
INT ctp;
ctp = cip+1;
if (ctp >= NSRCH)
ctp = 0;
cmds[ctp].s_code = SRCH_NOPR;
cmds[ctp].s_doto = curwp->w_doto;
cmds[ctp].s_dotp = curwp->w_dotp;
}
static void
is_pop() {
if (cmds[cip].s_code != SRCH_NOPR) {
adjustpos(cmds[cip].s_dotp, cmds[cip].s_doto);
cmds[cip].s_code = SRCH_NOPR;
}
if (--cip <= 0)
cip = NSRCH-1;
}
static INT
is_peek() {
return cmds[cip].s_code;
}
/* this used to always return TRUE (the return value was checked) */
static void
is_undo(INT *pptr, INT *dir)
{
INT redo = FALSE ;
switch (cmds[cip].s_code) {
case SRCH_BEGIN:
case SRCH_NOPR:
*pptr = -1;
case SRCH_MARK:
break;
case SRCH_FORW:
*dir = SRCH_BACK;
redo = TRUE;
break;
case SRCH_BACK:
*dir = SRCH_FORW;
redo = TRUE;
break;
case SRCH_ACCM:
default:
#ifdef UTF8
if (termisutf8) {
*pptr -= 1;
while (*pptr > 0 && (pat[*pptr] & 0xc0) == 0x80) {
*pptr -= 1;
}
if (*pptr < 0)
*pptr = 0;
pat[*pptr] = '\0';
break;
} else {
*pptr -= 1;
if (*pptr < 0)
*pptr = 0;
pat[*pptr] = '\0';
break;
}
#else
*pptr -= 1;
if (*pptr < 0)
*pptr = 0;
pat[*pptr] = '\0';
break;
#endif
}
is_pop();
if (redo) is_undo(pptr, dir);
}
static INT
is_find(INT dir)
{
INT plen;
LINE *odotp ;
INT i, len, odoto;
odoto = curwp->w_doto;
odotp = curwp->w_dotp;
i = 0;
len = strlen(pat);
plen = 0;
while (i < len) {
if (!ucs_nonspacing(ucs_strtochar(termcharset, pat, len, i, &i))) {
plen++;
}
}
if (plen != 0) {
if (dir==SRCH_FORW) {
backchar(FFARG | FFRAND, plen);
if (forwsrch(0, 1) == FALSE) {
adjustpos(odotp, odoto);
return FALSE;
}
return TRUE;
}
if (dir==SRCH_BACK) {
forwchar(FFARG | FFRAND, plen);
if (backsrch(0) == FALSE) {
adjustpos(odotp, odoto);
return FALSE;
}
return TRUE;
}
ewprintf("bad call to is_find");
return FALSE;
}
return FALSE;
}
/*
* If called with "dir" not one of SRCH_FORW or SRCH_BACK, this
* routine used to print an error message. It also used to return TRUE
* or FALSE, depending on if it liked the "dir". However, none of the
* callers looked at the status, so I just made the checking vanish.
*/
static void
is_prompt(INT dir, INT flag, INT success)
{
if (dir == SRCH_FORW) {
if (success != FALSE)
is_dspl("I-search", flag);
else
is_dspl("Failing I-search", flag);
} else if (dir == SRCH_BACK) {
if (success != FALSE)
is_dspl("I-search backward", flag);
else
is_dspl("Failing I-search backward", flag);
} else ewprintf("Broken call to is_prompt");
}
/*
* Prompt writing routine for the incremental search. The "prompt" is
* just a string. The "flag" determines whether pat should be printed.
*/
static void
is_dspl(char *prompt, INT flag)
{
if (flag != FALSE)
ewprintf("%s: ", prompt);
else
ewprintf("%s: %s", prompt, pat);
}
/*
* Query Replace. Replace strings selectively. Does a search and
* replace operation.
*/
INT
queryrepl(INT f, INT n)
{
INT s;
size_t rcnt = 0; /* Replacements made so far */
INT plen; /* length of found string */
char news[NPAT]; /* replacement string */
INT i, len, c, doreplace, case_exact;
if (curbp->b_flag & BFREADONLY) return readonly();
#ifndef NO_MACRO
if (macrodef) {
return emessage(ABORT, "Can't query replace in macro");
}
#endif
if ((s = readpattern("Query replace")) != TRUE) return s;
if (eread("Query replace %s with: ", news, NPAT, EFREPLACE, pat) == ABORT) return ABORT;
ewprintf("Query replacing %s with %s:", pat, news);
i = 0;
len = strlen(pat);
plen = 0;
while (i < len) {
if (!ucs_nonspacing(ucs_strtochar(termcharset, pat, len, i, &i))) {
plen++;
}
}
/*
* Search forward repeatedly, checking each time whether to
* insert or not. The "!" case makes the check always true, so
* it gets put into a tighter loop for efficiency.
*/
case_exact = (f & FFARG) || !case_fold_search;
doreplace = 1;
while (forwsrch(word_search, 0) == TRUE) {
retry:
update();
switch ((c = getkey(FALSE))) {
case ' ':
case '.':
case ',':
if (doreplace) {
u_boundary = 1; // UNDO
if (lreplace(plen, news, case_exact) == FALSE)
return (FALSE);
rcnt++;
}
if (c == ' ') break;
if (c == '.') goto stopsearch;
doreplace = 0;
goto retry;
case CCHR('G'): /* ^G */
ctrlg(FFRAND, 0);
goto stopsearch;
case CCHR('['): /* ESC */
if (type_ahead()) ungetkey(c); // Escape sequence
goto stopsearch;
case '!':
u_boundary = 1; // UNDO
do {
if (doreplace) {
if (lreplace(plen, news, case_exact) == FALSE)
return (FALSE);
rcnt++;
}
doreplace = 1;
} while (forwsrch(word_search, 0) == TRUE);
goto stopsearch;
case CCHR('H'):
case CCHR('?'): /* To not replace */
break;
default:
if (c < 256 && ISCTRL(c)) {
ungetkey(c);
goto stopsearch;
}
ewprintf("<SP> replace, [.] rep-end, [,] rep-now, <DEL> don't, [!] repl rest <ESC> quit");
goto retry;
}
doreplace = 1;
}
stopsearch:
if (rcnt == 0)
ewprintf("(No replacements done)");
else if (rcnt == 1)
ewprintf("(1 replacement done)");
else
ewprintf("(%z replacements done)", rcnt);
return TRUE;
}
/*
* This routine does the real work of a forward search. The pattern is
* sitting in the external variable "pat". If found, dot is updated,
* the window system is notified of the change, and TRUE is returned.
* If the string isn't found, FALSE is returned.
*
* Mg3a: Optional word search.
*/
INT
forwsrch(INT word_search, int incremental) {
LINE *clp;
INT cbo;
LINE *tlp;
INT tbo;
INT *pp;
INT c;
INT localpat[NPAT];
INT i, j, len;
INT prevc=' ', tc;
charset_t charset = curbp->charset;
#ifdef SEARCHSIMPLE
if (searchissimple && !incremental) return internal_searchsimple(pat, 0);
#endif
i = 0;
j = 0;
len = strlen(pat);
while (i < len + 1) {
c = ucs_strtochar(termcharset, pat, len + 1, i, &i);
localpat[j++] = c;
}
clp = curwp->w_dotp;
cbo = curwp->w_doto;
if (word_search && cbo != 0) {
prevc = ucs_char(charset, clp, ucs_prevc(charset, clp, cbo), NULL);
}
for(;;) {
if (cbo == llength(clp)) {
if((clp = lforw(clp)) == curbp->b_linep) break;
cbo = 0;
c = CCHR('J');
} else {
c = ucs_char(charset, clp, cbo, &cbo);
}
if (eq(c, localpat[0]) != FALSE && (!word_search || !isword(c) || !isword(prevc))) {
tlp = clp;
tbo = cbo;
tc = c;
pp = &localpat[1];
while (*pp != 0) {
if (tbo == llength(tlp)) {
tlp = lforw(tlp);
if (tlp == curbp->b_linep)
goto fail;
tbo = 0;
tc = CCHR('J');
} else {
tc = ucs_char(charset, tlp, tbo, &tbo);
}
if (eq(tc, *pp++) == FALSE)
goto fail;
}
if (word_search && isword(tc) && tbo != llength(tlp) &&
isword(ucs_char(charset, tlp, tbo, NULL))) {
goto fail;
}
adjustpos(tlp, tbo);
return TRUE;
}
fail: ;
prevc = c;
}
return FALSE;
}
/*
* This routine does the real work of a backward search. The pattern
* is sitting in the external variable "pat". If found, dot is
* updated, the window system is notified of the change, and TRUE is
* returned. If the string isn't found, FALSE is returned.
*
* Mg3a: Optional word search.
*/
INT
backsrch(INT word_search) {
LINE *clp;
INT cbo;
LINE *tlp;
INT tbo;
INT c;
INT *epp;
INT *pp;
INT localpat[NPAT];
INT i, j, len;
INT prevc=' ', tc;
charset_t charset = curbp->charset;
i = 0;
j = 0;
len = strlen(pat);
while (i < len + 1) {
c = ucs_strtochar(termcharset, pat, len + 1, i, &i);
localpat[j++] = c;
}
for (epp = &localpat[0]; epp[1] != 0; ++epp) {}
clp = curwp->w_dotp;
cbo = curwp->w_doto;
if (word_search && cbo != llength(clp)) {
prevc = ucs_char(charset, clp, cbo, NULL);
}
for (;;) {
if (cbo == 0) {
clp = lback(clp);
if (clp == curbp->b_linep) break;
cbo = llength(clp);
c = CCHR('J');
} else {
cbo = ucs_prevc(charset, clp, cbo);
c = ucs_char(charset, clp, cbo, NULL);
}
if (eq(c, *epp) != FALSE && (!word_search || !isword(c) || !isword(prevc))) {
tlp = clp;
tbo = cbo;
tc = c;
pp = epp;
while (pp != &localpat[0]) {
if (tbo == 0) {
tlp = lback(tlp);
if (tlp == curbp->b_linep)
goto fail;
tbo = llength(tlp);
tc = CCHR('J');
} else {
tbo = ucs_prevc(charset, tlp, tbo);
tc = ucs_char(charset, tlp, tbo, NULL);
}