forgotten commit. disabled until egl is adapted.
[AROS-Contrib.git] / fish / microemacs / search.c
blobdc01e7930c968492394f41ec4e8c521bffd8e921
1 /*
2 * The functions in this file implement commands that search in the forward
3 * and backward directions. There are no special characters in the search
4 * strings. Probably should have a regular expression search, or something
5 * like that.
7 * REVISION HISTORY:
9 * ? Steve Wilhite, 1-Dec-85
10 * - massive cleanup on code.
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include "ed.h"
19 * Search forward. Get a search string from the user, and search, beginning at
20 * ".", for the string. If found, reset the "." to be just after the match
21 * string, and [perhaps] repaint the display. Bound to "C-S".
23 int forwsearch(f, n)
24 int f;
25 int n;
27 register LINE *clp;
28 register int cbo;
29 register LINE*tlp;
30 register int tbo;
31 register int c;
32 register char *pp;
33 register int s;
35 if ((s = readpattern("Search")) != TRUE)
36 return (s);
38 clp = curwp->w_dotp;
39 cbo = curwp->w_doto;
41 while (clp != curbp->b_linep)
43 if (cbo == llength(clp))
45 clp = lforw(clp);
46 cbo = 0;
47 c = '\n';
49 else
50 c = lgetc(clp, cbo++);
52 if (eq(c, pat[0]) != FALSE)
54 tlp = clp;
55 tbo = cbo;
56 pp = &pat[1];
58 while (*pp != 0)
60 if (tlp == curbp->b_linep)
61 goto fail;
63 if (tbo == llength(tlp))
65 tlp = lforw(tlp);
66 tbo = 0;
67 c = '\n';
69 else
70 c = lgetc(tlp, tbo++);
72 if (eq(c, *pp++) == FALSE)
73 goto fail;
76 curwp->w_dotp = tlp;
77 curwp->w_doto = tbo;
78 curwp->w_flag |= WFMOVE;
79 return (TRUE);
81 fail:;
84 mlwrite("Not found");
85 return (FALSE);
89 * Reverse search. Get a search string from the user, and search, starting at
90 * "." and proceeding toward the front of the buffer. If found "." is left
91 * pointing at the first character of the pattern [the last character that was
92 j matched]. Bound to "C-R".
94 int backsearch(f, n)
95 int f;
96 int n;
98 register LINE *clp;
99 register int cbo;
100 register LINE *tlp;
101 register int tbo;
102 register int c;
103 register char *epp;
104 register char *pp;
105 register int s;
107 if ((s = readpattern("Reverse search")) != TRUE)
108 return (s);
110 for (epp = &pat[0]; epp[1] != 0; ++epp)
113 clp = curwp->w_dotp;
114 cbo = curwp->w_doto;
116 for (;;)
118 if (cbo == 0)
120 clp = lback(clp);
122 if (clp == curbp->b_linep)
124 mlwrite("Not found");
125 return (FALSE);
128 cbo = llength(clp)+1;
131 if (--cbo == llength(clp))
132 c = '\n';
133 else
134 c = lgetc(clp, cbo);
136 if (eq(c, *epp) != FALSE)
138 tlp = clp;
139 tbo = cbo;
140 pp = epp;
142 while (pp != &pat[0])
144 if (tbo == 0)
146 tlp = lback(tlp);
147 if (tlp == curbp->b_linep)
148 goto fail;
150 tbo = llength(tlp)+1;
153 if (--tbo == llength(tlp))
154 c = '\n';
155 else
156 c = lgetc(tlp, tbo);
158 if (eq(c, *--pp) == FALSE)
159 goto fail;
162 curwp->w_dotp = tlp;
163 curwp->w_doto = tbo;
164 curwp->w_flag |= WFMOVE;
165 return (TRUE);
167 fail:;
172 * Compare two characters. The "bc" comes from the buffer. It has it's case
173 * folded out. The "pc" is from the pattern.
175 int eq(bc, pc)
176 int bc;
177 int pc;
179 if (bc>='a' && bc<='z')
180 bc -= 0x20;
182 if (pc>='a' && pc<='z')
183 pc -= 0x20;
185 if (bc == pc)
186 return (TRUE);
188 return (FALSE);
192 * Read a pattern. Stash it in the external variable "pat". The "pat" is not
193 * updated if the user types in an empty line. If the user typed an empty line,
194 * and there is no old pattern, it is an error. Display the old pattern, in the
195 * style of Jeff Lomicka. There is some do-it-yourself control expansion.
197 int readpattern(prompt)
198 const char *prompt;
200 register char *cp1;
201 register const char *cp2;
202 register int c;
203 register int s;
204 char tpat[NPAT+20];
206 cp1 = &tpat[0]; /* Copy prompt */
207 cp2 = prompt;
209 while ((c = *cp2++) != '\0')
210 *cp1++ = c;
212 if (pat[0] != '\0') /* Old pattern */
214 *cp1++ = ' ';
215 *cp1++ = '[';
216 cp2 = &pat[0];
218 while ((c = *cp2++) != 0)
220 if (cp1 < &tpat[NPAT+20-6]) /* "??]: \0" */
222 if (c<0x20 || c==0x7F) {
223 *cp1++ = '^';
224 c ^= 0x40;
226 else if (c == '%') /* Map "%" to */
227 *cp1++ = c; /* "%%". */
229 *cp1++ = c;
233 *cp1++ = ']';
236 *cp1++ = ':'; /* Finish prompt */
237 *cp1++ = ' ';
238 *cp1++ = '\0';
239 s = mlreply(tpat, tpat, NPAT); /* Read pattern */
241 if (s == TRUE) /* Specified */
242 strcpy(pat, tpat);
243 else if (s == FALSE && pat[0] != 0) /* CR, but old one */
244 s = TRUE;
246 return (s);