2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
27 typedef enum { S_EMPTY, S_EOF, S_NOPREV, S_NOTFOUND, S_SOF, S_WRAP } smsg_t;
29 static void search_msg(SCR *, smsg_t);
30 static int search_init(SCR *, dir_t, CHAR_T *, size_t, CHAR_T **, u_int);
37 search_init(SCR *sp, dir_t dir, CHAR_T *ptrn, size_t plen, CHAR_T **epp,
44 /* If the file is empty, it's a fast search. */
46 if (db_last(sp, &lno))
49 if (LF_ISSET(SEARCH_MSG))
50 search_msg(sp, S_EMPTY);
55 if (LF_ISSET(SEARCH_PARSE)) { /* Parse the string. */
57 * Use the saved pattern if no pattern specified, or if only
58 * one or two delimiter characters specified.
61 * Historically, only the pattern itself was saved, vi didn't
62 * preserve addressing or delta information.
71 if (ptrn[0] == ptrn[1]) {
75 /* Complain if we don't have a previous pattern. */
76 prev: if (sp->re == NULL) {
77 search_msg(sp, S_NOPREV);
80 /* Re-compile the search pattern if necessary. */
81 if (!F_ISSET(sp, SC_RE_SEARCH) && re_compile(sp,
82 sp->re, sp->re_len, NULL, NULL, &sp->re_c,
84 (LF_ISSET(SEARCH_MSG) ? 0 : RE_C_SILENT)))
87 /* Set the search direction. */
88 if (LF_ISSET(SEARCH_SET))
94 * Set the delimiter, and move forward to the terminating
95 * delimiter, handling escaped delimiters.
98 * Only discard an escape character if it escapes a delimiter.
100 for (delim = *ptrn, p = t = ++ptrn;; *t++ = *p++) {
101 if (--plen == 0 || p[0] == delim) {
106 if (plen > 1 && p[0] == '\\' && p[1] == delim) {
117 /* Compile the RE. */
118 if (re_compile(sp, ptrn, plen, &sp->re, &sp->re_len, &sp->re_c,
120 (LF_ISSET(SEARCH_MSG) ? 0 : RE_C_SILENT) |
121 (LF_ISSET(SEARCH_TAG) ? RE_C_TAG : 0) |
122 (LF_ISSET(SEARCH_CSCOPE) ? RE_C_CSCOPE : 0)))
125 /* Set the search direction. */
126 if (LF_ISSET(SEARCH_SET))
134 * Do a forward search.
136 * PUBLIC: int f_search(SCR *,
137 * PUBLIC: MARK *, MARK *, CHAR_T *, size_t, CHAR_T **, u_int);
140 f_search(SCR *sp, MARK *fm, MARK *rm, CHAR_T *ptrn, size_t plen,
141 CHAR_T **eptrn, u_int flags)
147 int cnt, eval, rval, wrapped = 0;
150 if (search_init(sp, FORWARD, ptrn, plen, eptrn, flags))
153 if (LF_ISSET(SEARCH_FILE)) {
157 if (db_get(sp, fm->lno, DBG_FATAL, &l, &len))
162 * If doing incremental search, start searching at the previous
163 * column, so that we search a minimal distance and still match
164 * special patterns, e.g., \< for beginning of a word.
166 * Otherwise, start searching immediately after the cursor. If
167 * at the end of the line, start searching on the next line.
168 * This is incompatible (read bug fix) with the historic vi --
169 * searches for the '$' pattern never moved forward, and the
170 * "-t foo" didn't work if the 'f' was the first character in
173 if (LF_ISSET(SEARCH_INCR)) {
174 if ((coff = fm->cno) != 0)
176 } else if (fm->cno + 1 >= len) {
179 if (db_get(sp, lno, 0, &l, &len)) {
180 if (!O_ISSET(sp, O_WRAPSCAN)) {
181 if (LF_ISSET(SEARCH_MSG))
182 search_msg(sp, S_EOF);
193 for (cnt = INTERRUPT_CHECK, rval = 1;; ++lno, coff = 0) {
197 if (LF_ISSET(SEARCH_MSG)) {
198 search_busy(sp, btype);
201 cnt = INTERRUPT_CHECK;
203 if ((wrapped && lno > fm->lno) || db_get(sp, lno, 0, &l, &len)) {
205 if (LF_ISSET(SEARCH_MSG))
206 search_msg(sp, S_NOTFOUND);
209 if (!O_ISSET(sp, O_WRAPSCAN)) {
210 if (LF_ISSET(SEARCH_MSG))
211 search_msg(sp, S_EOF);
219 /* If already at EOL, just keep going. */
220 if (len != 0 && coff == len)
223 /* Set the termination. */
224 match[0].rm_so = coff;
225 match[0].rm_eo = len;
227 #if defined(DEBUG) && 0
228 TRACE(sp, "F search: %lu from %u to %u\n",
229 lno, coff, len != 0 ? len - 1 : len);
231 /* Search the line. */
232 eval = regexec(&sp->re_c, l, 1, match,
233 (match[0].rm_so == 0 ? 0 : REG_NOTBOL) | REG_STARTEND);
234 if (eval == REG_NOMATCH)
237 if (LF_ISSET(SEARCH_MSG))
238 re_error(sp, eval, &sp->re_c);
240 (void)sp->gp->scr_bell(sp);
244 /* Warn if the search wrapped. */
245 if (wrapped && LF_ISSET(SEARCH_WMSG))
246 search_msg(sp, S_WRAP);
248 #if defined(DEBUG) && 0
249 TRACE(sp, "F search: %qu to %qu\n",
250 match[0].rm_so, match[0].rm_eo);
253 rm->cno = match[0].rm_so;
256 * If a change command, it's possible to move beyond the end
257 * of a line. Historic vi generally got this wrong (e.g. try
258 * "c?$<cr>"). Not all that sure this gets it right, there
259 * are lots of strange cases.
261 if (!LF_ISSET(SEARCH_EOL) && rm->cno >= len)
262 rm->cno = len != 0 ? len - 1 : 0;
268 if (LF_ISSET(SEARCH_MSG))
269 search_busy(sp, BUSY_OFF);
275 * Do a backward search.
277 * PUBLIC: int b_search(SCR *,
278 * PUBLIC: MARK *, MARK *, CHAR_T *, size_t, CHAR_T **, u_int);
281 b_search(SCR *sp, MARK *fm, MARK *rm, CHAR_T *ptrn, size_t plen,
282 CHAR_T **eptrn, u_int flags)
287 size_t coff, last, len;
288 int cnt, eval, rval, wrapped;
291 if (search_init(sp, BACKWARD, ptrn, plen, eptrn, flags))
295 * If doing incremental search, set the "starting" position past the
296 * current column, so that we search a minimal distance and still
297 * match special patterns, e.g., \> for the end of a word. This is
298 * safe when the cursor is at the end of a line because we only use
299 * it for comparison with the location of the match.
301 * Otherwise, start searching immediately before the cursor. If in
302 * the first column, start search on the previous line.
304 if (LF_ISSET(SEARCH_INCR)) {
309 if (fm->lno == 1 && !O_ISSET(sp, O_WRAPSCAN)) {
310 if (LF_ISSET(SEARCH_MSG))
311 search_msg(sp, S_SOF);
321 for (cnt = INTERRUPT_CHECK, rval = 1, wrapped = 0;; --lno, coff = 0) {
325 if (LF_ISSET(SEARCH_MSG)) {
326 search_busy(sp, btype);
329 cnt = INTERRUPT_CHECK;
331 if ((wrapped && lno < fm->lno) || lno == 0) {
333 if (LF_ISSET(SEARCH_MSG))
334 search_msg(sp, S_NOTFOUND);
337 if (!O_ISSET(sp, O_WRAPSCAN)) {
338 if (LF_ISSET(SEARCH_MSG))
339 search_msg(sp, S_SOF);
342 if (db_last(sp, &lno))
345 if (LF_ISSET(SEARCH_MSG))
346 search_msg(sp, S_EMPTY);
354 if (db_get(sp, lno, 0, &l, &len))
357 /* Set the termination. */
359 match[0].rm_eo = len;
361 #if defined(DEBUG) && 0
362 TRACE(sp, "B search: %lu from 0 to %qu\n", lno, match[0].rm_eo);
364 /* Search the line. */
365 eval = regexec(&sp->re_c, l, 1, match,
366 (match[0].rm_eo == len ? 0 : REG_NOTEOL) | REG_STARTEND);
367 if (eval == REG_NOMATCH)
370 if (LF_ISSET(SEARCH_MSG))
371 re_error(sp, eval, &sp->re_c);
373 (void)sp->gp->scr_bell(sp);
377 /* Check for a match starting past the cursor. */
378 if (coff != 0 && match[0].rm_so >= coff)
381 /* Warn if the search wrapped. */
382 if (wrapped && LF_ISSET(SEARCH_WMSG))
383 search_msg(sp, S_WRAP);
385 #if defined(DEBUG) && 0
386 TRACE(sp, "B found: %qu to %qu\n",
387 match[0].rm_so, match[0].rm_eo);
390 * We now have the first match on the line. Step through the
391 * line character by character until find the last acceptable
392 * match. This is painful, we need a better interface to regex
396 last = match[0].rm_so++;
397 if (match[0].rm_so >= len)
399 match[0].rm_eo = len;
400 eval = regexec(&sp->re_c, l, 1, match,
401 (match[0].rm_so == 0 ? 0 : REG_NOTBOL) |
403 if (eval == REG_NOMATCH)
406 if (LF_ISSET(SEARCH_MSG))
407 re_error(sp, eval, &sp->re_c);
409 (void)sp->gp->scr_bell(sp);
412 if (coff && match[0].rm_so >= coff)
417 /* See comment in f_search(). */
418 if (!LF_ISSET(SEARCH_EOL) && last >= len)
419 rm->cno = len != 0 ? len - 1 : 0;
426 err: if (LF_ISSET(SEARCH_MSG))
427 search_busy(sp, BUSY_OFF);
433 * Display one of the search messages.
436 search_msg(SCR *sp, smsg_t msg)
440 msgq(sp, M_ERR, "072|File empty; nothing to search");
444 "073|Reached end-of-file without finding the pattern");
447 msgq(sp, M_ERR, "074|No previous search pattern");
450 msgq(sp, M_ERR, "075|Pattern not found");
454 "076|Reached top-of-file without finding the pattern");
457 msgq(sp, M_ERR, "077|Search wrapped");
466 * Put up the busy searching message.
468 * PUBLIC: void search_busy(SCR *, busy_t);
471 search_busy(SCR *sp, busy_t btype)
473 sp->gp->scr_busy(sp, "078|Searching...", btype);