2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)search.c 8.1 (Berkeley) 6/4/93
37 * $DragonFly: src/lib/libedit/search.c,v 1.4 2003/11/12 20:21:29 eirikn Exp $
41 * search.c: History and character search functions
53 * Adjust cursor in vi mode to include the character under it
55 #define EL_CURSOR(el) \
56 ((el)->el_line.cursor + (((el)->el_map.type == MAP_VI) && \
57 ((el)->el_map.current == (el)->el_map.alt)))
60 * Initialize the search stuff
66 el->el_search.patbuf = (char *) el_malloc(EL_BUFSIZ);
67 el->el_search.patlen = 0;
68 el->el_search.patdir = -1;
69 el->el_search.chacha = '\0';
70 el->el_search.chadir = -1;
76 * Initialize the search stuff
82 el_free((ptr_t) el->el_search.patbuf);
83 el->el_search.patbuf = NULL;
88 * Handle regular expression errors
99 * Return if string matches pattern
109 #elif defined (REGEXP)
113 extern char *re_comp (const char *);
114 extern int re_exec (const char *);
117 if (strstr(str, pat) != NULL)
121 if (regcomp(&re, pat, 0) == 0) {
122 rv = regexec(&re, str, 0, NULL, 0) == 0;
128 #elif defined(REGEXP)
129 if ((re = regcomp(pat)) != NULL) {
130 rv = regexec(re, str);
137 if (re_comp(pat) != NULL)
140 return re_exec(str) == 1;
146 * return True if the pattern matches the prefix
154 (void) fprintf(el->el_errfile, "match `%s' with `%s'\n",
155 el->el_search.patbuf, str);
158 return el_match(str, el->el_search.patbuf);
163 * Set the history seatch pattern
169 if (el->el_state.lastcmd != ED_SEARCH_PREV_HISTORY &&
170 el->el_state.lastcmd != ED_SEARCH_NEXT_HISTORY) {
171 el->el_search.patlen = EL_CURSOR(el) - el->el_line.buffer;
172 if (el->el_search.patlen >= EL_BUFSIZ)
173 el->el_search.patlen = EL_BUFSIZ -1;
174 if (el->el_search.patlen >= 0) {
175 (void) strncpy(el->el_search.patbuf, el->el_line.buffer,
176 el->el_search.patlen);
177 el->el_search.patbuf[el->el_search.patlen] = '\0';
180 el->el_search.patlen = strlen(el->el_search.patbuf);
183 (void) fprintf(el->el_errfile, "\neventno = %d\n", el->el_history.eventno);
184 (void) fprintf(el->el_errfile, "patlen = %d\n", el->el_search.patlen);
185 (void) fprintf(el->el_errfile, "patbuf = \"%s\"\n", el->el_search.patbuf);
186 (void) fprintf(el->el_errfile, "cursor %d lastchar %d\n",
187 EL_CURSOR(el) - el->el_line.buffer,
188 el->el_line.lastchar - el->el_line.buffer);
194 * Emacs incremental search
196 protected el_action_t
197 ce_inc_search(el, dir)
201 static char STRfwd[] = { 'f', 'w', 'd', '\0' },
202 STRbck[] = { 'b', 'c', 'k', '\0' };
203 static char pchar = ':'; /* ':' = normal, '?' = failed */
204 static char endcmd[2] = { '\0', '\0' };
205 char ch, *cp, *ocursor = el->el_line.cursor, oldpchar = pchar;
207 el_action_t ret = CC_NORM;
209 int ohisteventno = el->el_history.eventno,
210 oldpatlen = el->el_search.patlen,
214 if (el->el_line.lastchar + sizeof(STRfwd) / sizeof(char) + 2 +
215 el->el_search.patlen >= el->el_line.limit)
220 if (el->el_search.patlen == 0) { /* first round */
223 el->el_search.patbuf[el->el_search.patlen++] = '.';
224 el->el_search.patbuf[el->el_search.patlen++] = '*';
228 *el->el_line.lastchar++ = '\n';
229 for (cp = newdir == ED_SEARCH_PREV_HISTORY ? STRbck : STRfwd;
230 *cp; *el->el_line.lastchar++ = *cp++)
232 *el->el_line.lastchar++ = pchar;
233 for (cp = &el->el_search.patbuf[1];
234 cp < &el->el_search.patbuf[el->el_search.patlen];
235 *el->el_line.lastchar++ = *cp++)
237 *el->el_line.lastchar = '\0';
240 if (el_getc(el, &ch) != 1)
241 return ed_end_of_file(el, 0);
243 switch (el->el_map.current[(unsigned char) ch]) {
246 if (el->el_search.patlen > EL_BUFSIZ - 3)
249 el->el_search.patbuf[el->el_search.patlen++] = ch;
250 *el->el_line.lastchar++ = ch;
251 *el->el_line.lastchar = '\0';
256 case EM_INC_SEARCH_NEXT:
257 newdir = ED_SEARCH_NEXT_HISTORY;
261 case EM_INC_SEARCH_PREV:
262 newdir = ED_SEARCH_PREV_HISTORY;
266 case ED_DELETE_PREV_CHAR:
267 if (el->el_search.patlen > 1)
275 case 0007: /* ^G: Abort */
280 case 0027: /* ^W: Append word */
281 /* No can do if globbing characters in pattern */
282 for (cp = &el->el_search.patbuf[1]; ; cp++)
283 if (cp >= &el->el_search.patbuf[el->el_search.patlen]) {
284 el->el_line.cursor += el->el_search.patlen - 1;
285 cp = c__next_word(el->el_line.cursor,
286 el->el_line.lastchar, 1, ce__isword);
287 while (el->el_line.cursor < cp &&
288 *el->el_line.cursor != '\n') {
289 if (el->el_search.patlen > EL_BUFSIZ - 3) {
293 el->el_search.patbuf[el->el_search.patlen++] =
295 *el->el_line.lastchar++ = *el->el_line.cursor++;
297 el->el_line.cursor = ocursor;
298 *el->el_line.lastchar = '\0';
301 } else if (isglob(*cp)) {
307 default: /* Terminate and execute cmd */
312 case 0033: /* ESC: Terminate */
320 while (el->el_line.lastchar > el->el_line.buffer &&
321 *el->el_line.lastchar != '\n')
322 *el->el_line.lastchar-- = '\0';
323 *el->el_line.lastchar = '\0';
327 /* Can't search if unmatched '[' */
328 for (cp = &el->el_search.patbuf[el->el_search.patlen-1], ch = ']';
329 cp > el->el_search.patbuf; cp--)
330 if (*cp == '[' || *cp == ']') {
335 if (el->el_search.patlen > 1 && ch != '[') {
336 if (redo && newdir == dir) {
337 if (pchar == '?') { /* wrap around */
338 el->el_history.eventno =
339 newdir == ED_SEARCH_PREV_HISTORY ? 0 : 0x7fffffff;
340 if (hist_get(el) == CC_ERROR)
341 /* el->el_history.eventno was fixed by first call */
343 el->el_line.cursor = newdir == ED_SEARCH_PREV_HISTORY ?
344 el->el_line.lastchar : el->el_line.buffer;
346 el->el_line.cursor +=
347 newdir == ED_SEARCH_PREV_HISTORY ? -1 : 1;
350 el->el_search.patbuf[el->el_search.patlen++] = '.';
351 el->el_search.patbuf[el->el_search.patlen++] = '*';
353 el->el_search.patbuf[el->el_search.patlen] = '\0';
354 if (el->el_line.cursor < el->el_line.buffer ||
355 el->el_line.cursor > el->el_line.lastchar ||
356 (ret = ce_search_line(el, &el->el_search.patbuf[1],
357 newdir)) == CC_ERROR) {
359 el->el_state.lastcmd = (el_action_t) newdir;
360 ret = newdir == ED_SEARCH_PREV_HISTORY ?
361 ed_search_prev_history(el, 0) :
362 ed_search_next_history(el, 0);
363 if (ret != CC_ERROR) {
364 el->el_line.cursor = newdir == ED_SEARCH_PREV_HISTORY ?
365 el->el_line.lastchar : el->el_line.buffer;
366 (void) ce_search_line(el, &el->el_search.patbuf[1],
370 el->el_search.patbuf[--el->el_search.patlen] = '\0';
371 if (ret == CC_ERROR) {
373 if (el->el_history.eventno != ohisteventno) {
374 el->el_history.eventno = ohisteventno;
375 if (hist_get(el) == CC_ERROR)
378 el->el_line.cursor = ocursor;
385 ret = ce_inc_search(el, newdir);
387 if (ret == CC_ERROR && pchar == '?' && oldpchar == ':')
388 /* break abort of failed search at last non-failed */
393 if (ret == CC_NORM || (ret == CC_ERROR && oldpatlen == 0)) {
394 /* restore on normal return or error exit */
396 el->el_search.patlen = oldpatlen;
397 if (el->el_history.eventno != ohisteventno) {
398 el->el_history.eventno = ohisteventno;
399 if (hist_get(el) == CC_ERROR)
402 el->el_line.cursor = ocursor;
406 if (done || ret != CC_NORM)
415 protected el_action_t
421 char tmpbuf[EL_BUFSIZ];
426 tmpbuf[tmplen++] = '.';
427 tmpbuf[tmplen++] = '*';
430 el->el_line.buffer[0] = '\0';
431 el->el_line.lastchar = el->el_line.buffer;
432 el->el_line.cursor = el->el_line.buffer;
433 el->el_search.patdir = dir;
435 c_insert(el, 2); /* prompt + '\n' */
436 *el->el_line.cursor++ = '\n';
437 *el->el_line.cursor++ = dir == ED_SEARCH_PREV_HISTORY ? '/' : '?';
446 tmplen = c_gets(el, &tmpbuf[LEN]) + LEN;
448 tmpbuf[tmplen] = '\0';
452 * Use the old pattern, but wild-card it.
454 if (el->el_search.patlen == 0) {
455 el->el_line.buffer[0] = '\0';
456 el->el_line.lastchar = el->el_line.buffer;
457 el->el_line.cursor = el->el_line.buffer;
462 if (el->el_search.patbuf[0] != '.' && el->el_search.patbuf[0] != '*') {
463 (void)strncpy(tmpbuf, el->el_search.patbuf, sizeof(tmpbuf) - 1);
464 el->el_search.patbuf[0] = '.';
465 el->el_search.patbuf[1] = '*';
466 (void)strncpy(&el->el_search.patbuf[2], tmpbuf, EL_BUFSIZ - 3);
467 el->el_search.patlen++;
468 el->el_search.patbuf[el->el_search.patlen++] = '.';
469 el->el_search.patbuf[el->el_search.patlen++] = '*';
470 el->el_search.patbuf[el->el_search.patlen] = '\0';
476 tmpbuf[tmplen++] = '.';
477 tmpbuf[tmplen++] = '*';
479 tmpbuf[tmplen] = '\0';
480 (void)strncpy(el->el_search.patbuf, tmpbuf, EL_BUFSIZ - 1);
481 el->el_search.patlen = tmplen;
483 el->el_state.lastcmd = (el_action_t) dir; /* avoid c_setpat */
484 el->el_line.cursor = el->el_line.lastchar = el->el_line.buffer;
485 if ((dir == ED_SEARCH_PREV_HISTORY ? ed_search_prev_history(el, 0) :
486 ed_search_next_history(el, 0)) == CC_ERROR) {
493 *el->el_line.lastchar++ = '\n';
494 *el->el_line.lastchar = '\0';
505 * Look for a pattern inside a line
507 protected el_action_t
508 ce_search_line(el, pattern, dir)
515 if (dir == ED_SEARCH_PREV_HISTORY) {
516 for (cp = el->el_line.cursor; cp >= el->el_line.buffer; cp--)
517 if (el_match(cp, pattern)) {
518 el->el_line.cursor = cp;
523 for (cp = el->el_line.cursor; *cp != '\0' &&
524 cp < el->el_line.limit; cp++)
525 if (el_match(cp, pattern)) {
526 el->el_line.cursor = cp;
537 protected el_action_t
538 cv_repeat_srch(el, c)
543 (void) fprintf(el->el_errfile, "dir %d patlen %d patbuf %s\n",
544 c, el->el_search.patlen, el->el_search.patbuf);
547 el->el_state.lastcmd = (el_action_t) c; /* Hack to stop c_setpat */
548 el->el_line.lastchar = el->el_line.buffer;
551 case ED_SEARCH_NEXT_HISTORY:
552 return ed_search_next_history(el, 0);
553 case ED_SEARCH_PREV_HISTORY:
554 return ed_search_prev_history(el, 0);
561 /* cv_csearch_back():
562 * Vi character search reverse
564 protected el_action_t
565 cv_csearch_back(el, ch, count, tflag)
567 int ch, count, tflag;
571 cp = el->el_line.cursor;
575 while (cp > el->el_line.buffer && *cp != ch)
579 if (cp < el->el_line.buffer || (cp == el->el_line.buffer && *cp != ch))
582 if (*cp == ch && tflag)
585 el->el_line.cursor = cp;
587 if (el->el_chared.c_vcmd.action & DELETE) {
588 el->el_line.cursor++;
593 re_refresh_cursor(el);
599 * Vi character search forward
601 protected el_action_t
602 cv_csearch_fwd(el, ch, count, tflag)
604 int ch, count, tflag;
608 cp = el->el_line.cursor;
612 while (cp < el->el_line.lastchar && *cp != ch)
616 if (cp >= el->el_line.lastchar)
619 if (*cp == ch && tflag)
622 el->el_line.cursor = cp;
624 if (el->el_chared.c_vcmd.action & DELETE) {
625 el->el_line.cursor++;
629 re_refresh_cursor(el);