forked from troglobit/MicroEMACS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.c
550 lines (441 loc) · 12.5 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
/*
* The functions in this file implement commands that search in the forward
* and backward directions. There are no special characters in the search
* strings. Probably should have a regular expression search, or something
* like that.
*
*/
#include <stdio.h>
#include "estruct.h"
#include "edef.h"
/*
* Search forward. Get a search string from the user, and search, beginning at
* ".", for the string. If found, reset the "." to be just after the match
* string, and [perhaps] repaint the display. Bound to "C-S".
*/
/* string search input parameters */
#define PTBEG 1 /* leave the point at the begining on search */
#define PTEND 2 /* leave the point at the end on search */
forwsearch(f, n)
{
register int status;
/* resolve the repeat count */
if (n == 0)
n = 1;
if (n < 1) /* search backwards */
return(backsearch(f, -n));
/* ask the user for the text of a pattern */
if ((status = readpattern("Search")) != TRUE)
return(status);
/* search for the pattern */
while (n-- > 0) {
if ((status = forscan(&pat[0],PTEND)) == FALSE)
break;
}
/* and complain if not there */
if (status == FALSE)
mlwrite("Not found");
return(status);
}
forwhunt(f, n)
{
register int status;
/* resolve the repeat count */
if (n == 0)
n = 1;
if (n < 1) /* search backwards */
return(backhunt(f, -n));
/* Make sure a pattern exists */
if (pat[0] == 0) {
mlwrite("No pattern set");
return(FALSE);
}
/* search for the pattern */
while (n-- > 0) {
if ((status = forscan(&pat[0],PTEND)) == FALSE)
break;
}
/* and complain if not there */
if (status == FALSE)
mlwrite("Not found");
return(status);
}
/*
* 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]. Bound to "C-R".
*/
backsearch(f, n)
{
register int s;
/* resolve null and negative arguments */
if (n == 0)
n = 1;
if (n < 1)
return(forwsearch(f, -n));
/* get a pattern to search */
if ((s = readpattern("Reverse search")) != TRUE)
return(s);
/* and go search for it */
bsearch(f,n);
}
backhunt(f, n) /* hunt backward for the last search string entered */
{
/* resolve null and negative arguments */
if (n == 0)
n = 1;
if (n < 1)
return(forwhunt(f, -n));
/* Make sure a pattern exists */
if (pat[0] == 0) {
mlwrite("No pattern set");
return(FALSE);
}
/* and go search for it */
bsearch(f,n);
}
bsearch(f, n)
{
register LINE *clp;
register int cbo;
register LINE *tlp;
register int tbo;
register int c;
register char *epp;
register char *pp;
/* find a pointer to the end of the pattern */
for (epp = &pat[0]; epp[1] != 0; ++epp)
;
/* make local copies of the starting location */
clp = curwp->w_dotp;
cbo = curwp->w_doto;
while (n-- > 0) {
for (;;) {
/* if we are at the begining of the line, wrap back around */
if (cbo == 0) {
clp = lback(clp);
if (clp == curbp->b_linep) {
mlwrite("Not found");
return(FALSE);
}
cbo = llength(clp)+1;
}
/* fake the <NL> at the end of a line */
if (--cbo == llength(clp))
c = '\n';
else
c = lgetc(clp, cbo);
/* check for a match against the end of the pattern */
if (eq(c, *epp) != FALSE) {
tlp = clp;
tbo = cbo;
pp = epp;
/* scanning backwards through the rest of the
pattern looking for a match */
while (pp != &pat[0]) {
/* wrap across a line break */
if (tbo == 0) {
tlp = lback(tlp);
if (tlp == curbp->b_linep)
goto fail;
tbo = llength(tlp)+1;
}
/* fake the <NL> */
if (--tbo == llength(tlp))
c = '\n';
else
c = lgetc(tlp, tbo);
if (eq(c, *--pp) == FALSE)
goto fail;
}
/* A Match! reset the current cursor */
curwp->w_dotp = tlp;
curwp->w_doto = tbo;
curwp->w_flag |= WFMOVE;
goto next;
}
fail:;
}
next:;
}
return(TRUE);
}
/*
* Compare two characters. The "bc" comes from the buffer. It has it's case
* folded out. The "pc" is from the pattern.
*/
eq(bc, pc)
int bc;
int pc;
{
if ((curwp->w_bufp->b_mode & MDEXACT) == 0) {
if (bc>='a' && bc<='z')
bc -= 0x20;
if (pc>='a' && pc<='z')
pc -= 0x20;
}
if (bc == pc)
return(TRUE);
return(FALSE);
}
/*
* Read a pattern. Stash it in the external variable "pat". The "pat" is not
* updated if the user types in an empty line. If the user typed an empty line,
* and there is no old pattern, it is an error. Display the old pattern, in the
* style of Jeff Lomicka. There is some do-it-yourself control expansion.
* change to using <ESC> to delemit the end-of-pattern to allow <NL>s in
* the search string.
*/
readpattern(prompt)
char *prompt;
{
register int s;
char tpat[NPAT+20];
strcpy(tpat, prompt); /* copy prompt to output string */
strcat(tpat, " ["); /* build new prompt string */
expandp(&pat[0], &tpat[strlen(tpat)], NPAT/2); /* add old pattern */
strcat(tpat, "]<ESC>: ");
s = mlreplyt(tpat, tpat, NPAT, 27); /* Read pattern */
if (s == TRUE) /* Specified */
strcpy(pat, tpat);
else if (s == FALSE && pat[0] != 0) /* CR, but old one */
s = TRUE;
return(s);
}
sreplace(f, n) /* Search and replace (ESC-R) */
int f; /* default flag */
int n; /* # of repetitions wanted */
{
return(replaces(FALSE, f, n));
}
qreplace(f, n) /* search and replace with query (ESC-CTRL-R) */
int f; /* default flag */
int n; /* # of repetitions wanted */
{
return(replaces(TRUE, f, n));
}
/* replaces: search for a string and replace it with another
string. query might be enabled (according to
kind). */
replaces(kind, f, n)
int kind; /* Query enabled flag */
int f; /* default flag */
int n; /* # of repetitions wanted */
{
register int i; /* loop index */
register int s; /* success flag on pattern inputs */
register int slength,
rlength; /* length of search and replace strings */
register int numsub; /* number of substitutions */
register int nummatch; /* number of found matches */
int nlflag; /* last char of search string a <NL>? */
int nlrepl; /* was a replace done on the last line? */
char tmpc; /* temporary character */
char c; /* input char for query */
char tpat[NPAT]; /* temporary to hold search pattern */
LINE *origline; /* original "." position */
int origoff; /* and offset (for . query option) */
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
/* check for negative repititions */
if (f && n < 0)
return(FALSE);
/* ask the user for the text of a pattern */
if ((s = readpattern(
(kind == FALSE ? "Replace" : "Query replace"))) != TRUE)
return(s);
strcpy(&tpat[0], &pat[0]); /* salt it away */
/* ask for the replacement string */
strcpy(&pat[0], &rpat[0]); /* set up default string */
if ((s = readpattern("with")) == ABORT)
return(s);
/* move everything to the right place and length them */
strcpy(&rpat[0], &pat[0]);
strcpy(&pat[0], &tpat[0]);
slength = strlen(&pat[0]);
rlength = strlen(&rpat[0]);
/* set up flags so we can make sure not to do a recursive
replace on the last line */
nlflag = (pat[slength - 1] == '\n');
nlrepl = FALSE;
/* build query replace question string */
strcpy(tpat, "Replace '");
expandp(&pat[0], &tpat[strlen(tpat)], NPAT/3);
strcat(tpat, "' with '");
expandp(&rpat[0], &tpat[strlen(tpat)], NPAT/3);
strcat(tpat, "'? ");
/* save original . position */
origline = curwp->w_dotp;
origoff = curwp->w_doto;
/* scan through the file */
numsub = 0;
nummatch = 0;
while ((f == FALSE || n > nummatch) &&
(nlflag == FALSE || nlrepl == FALSE)) {
/* search for the pattern */
if (forscan(&pat[0],PTBEG) != TRUE)
break; /* all done */
++nummatch; /* increment # of matches */
/* check if we are on the last line */
nlrepl = (lforw(curwp->w_dotp) == curwp->w_bufp->b_linep);
/* check for query */
if (kind) {
/* get the query */
mlwrite(&tpat[0], &pat[0], &rpat[0]);
qprompt:
update(); /* show the proposed place to change */
c = (*term.t_getchar)(); /* and input */
mlwrite(""); /* and clear it */
/* and respond appropriately */
switch (c) {
case 'y': /* yes, substitute */
case ' ':
break;
case 'n': /* no, onword */
forwchar(FALSE, 1);
continue;
case '!': /* yes/stop asking */
kind = FALSE;
break;
case '.': /* abort! and return */
/* restore old position */
curwp->w_dotp = origline;
curwp->w_doto = origoff;
curwp->w_flag |= WFMOVE;
case BELL: /* abort! and stay */
mlwrite("Aborted!");
return(FALSE);
default: /* bitch and beep */
(*term.t_beep)();
case '?': /* help me */
mlwrite("(Y)es, (N)o, (!)Do the rest, (^G)Abort, (.)Abort back, (?)Help: ");
goto qprompt;
}
}
/* delete the sucker */
if (ldelete(slength, FALSE) != TRUE) {
/* error while deleting */
mlwrite("ERROR while deleteing");
return(FALSE);
}
/* and insert its replacement */
for (i=0; i<rlength; i++) {
tmpc = rpat[i];
s = (tmpc == '\n' ? lnewline() : linsert(1, tmpc));
if (s != TRUE) {
/* error while inserting */
mlwrite("Out of memory while inserting");
return(FALSE);
}
}
numsub++; /* increment # of substitutions */
}
/* and report the results */
mlwrite("%d substitutions",numsub);
return(TRUE);
}
forscan(patrn,leavep) /* search forward for a <patrn> */
char *patrn; /* string to scan for */
int leavep; /* place to leave point
PTBEG = begining of match
PTEND = at end of match */
{
register LINE *curline; /* current line during scan */
register int curoff; /* position within current line */
register LINE *lastline; /* last line position during scan */
register int lastoff; /* position within last line */
register int c; /* character at current position */
register LINE *matchline; /* current line during matching */
register int matchoff; /* position in matching line */
register char *patptr; /* pointer into pattern */
/* setup local scan pointers to global "." */
curline = curwp->w_dotp;
curoff = curwp->w_doto;
/* scan each character until we hit the head link record */
while (curline != curbp->b_linep) {
/* save the current position in case we need to
restore it on a match */
lastline = curline;
lastoff = curoff;
/* get the current character resolving EOLs */
if (curoff == llength(curline)) { /* if at EOL */
curline = lforw(curline); /* skip to next line */
curoff = 0;
c = '\n'; /* and return a <NL> */
} else
c = lgetc(curline, curoff++); /* get the char */
/* test it against first char in pattern */
if (eq(c, patrn[0]) != FALSE) { /* if we find it..*/
/* setup match pointers */
matchline = curline;
matchoff = curoff;
patptr = &patrn[0];
/* scan through patrn for a match */
while (*++patptr != 0) {
/* advance all the pointers */
if (matchoff == llength(matchline)) {
/* advance past EOL */
matchline = lforw(matchline);
matchoff = 0;
c = '\n';
} else
c = lgetc(matchline, matchoff++);
/* and test it against the pattern */
if (eq(*patptr, c) == FALSE)
goto fail;
}
/* A SUCCESSFULL MATCH!!! */
/* reset the global "." pointers */
if (leavep == PTEND) { /* at end of string */
curwp->w_dotp = matchline;
curwp->w_doto = matchoff;
} else { /* at begining of string */
curwp->w_dotp = lastline;
curwp->w_doto = lastoff;
}
curwp->w_flag |= WFMOVE; /* flag that we have moved */
return(TRUE);
}
fail:; /* continue to search */
}
/* we could not find a match */
return(FALSE);
}
/* expandp: expand control key sequences for output */
expandp(srcstr, deststr, maxlength)
char *srcstr; /* string to expand */
char *deststr; /* destination of expanded string */
int maxlength; /* maximum chars in destination */
{
char c; /* current char to translate */
/* scan through the string */
while ((c = *srcstr++) != 0) {
if (c == '\n') { /* its an EOL */
*deststr++ = '<';
*deststr++ = 'N';
*deststr++ = 'L';
*deststr++ = '>';
maxlength -= 4;
} else if (c < 0x20 || c == 0x7f) { /* control character */
*deststr++ = '^';
*deststr++ = c ^ 0x40;
maxlength -= 2;
} else if (c == '%') {
*deststr++ = '%';
*deststr++ = '%';
maxlength -= 2;
} else { /* any other character */
*deststr++ = c;
maxlength--;
}
/* check for maxlength */
if (maxlength < 4) {
*deststr++ = '$';
*deststr = '\0';
return(FALSE);
}
}
*deststr = '\0';
return(TRUE);
}