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 * $NetBSD: search.c,v 1.10 2001/01/04 15:56:32 christos Exp $
39 #if !defined(lint) && !defined(SCCSID)
40 static char sccsid[] = "@(#)search.c 8.1 (Berkeley) 6/4/93";
41 #endif /* not lint && not SCCSID */
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
46 * search.c: History and character search functions
58 * Adjust cursor in vi mode to include the character under it
60 #define EL_CURSOR(el) \
61 ((el)->el_line.cursor + (((el)->el_map.type == MAP_VI) && \
62 ((el)->el_map.current == (el)->el_map.alt)))
65 * Initialize the search stuff
68 search_init(EditLine *el)
71 el->el_search.patbuf = (char *) el_malloc(EL_BUFSIZ);
72 if (el->el_search.patbuf == NULL)
74 el->el_search.patlen = 0;
75 el->el_search.patdir = -1;
76 el->el_search.chacha = '\0';
77 el->el_search.chadir = -1;
83 * Initialize the search stuff
86 search_end(EditLine *el)
89 el_free((ptr_t) el->el_search.patbuf);
90 el->el_search.patbuf = NULL;
96 * Handle regular expression errors
100 regerror(const char *msg)
107 * Return if string matches pattern
110 el_match(const char *str, const char *pat)
115 #elif defined (REGEXP)
119 extern char *re_comp(const char *);
120 extern int re_exec(const char *);
123 if (strstr(str, pat) != NULL)
127 if (regcomp(&re, pat, 0) == 0) {
128 rv = regexec(&re, str, 0, NULL, 0) == 0;
134 #elif defined(REGEXP)
135 if ((re = regcomp(pat)) != NULL) {
136 rv = regexec(re, str);
143 if (re_comp(pat) != NULL)
146 return (re_exec(str) == 1);
152 * return True if the pattern matches the prefix
155 c_hmatch(EditLine *el, const char *str)
158 (void) fprintf(el->el_errfile, "match `%s' with `%s'\n",
159 el->el_search.patbuf, str);
162 return (el_match(str, el->el_search.patbuf));
167 * Set the history seatch pattern
170 c_setpat(EditLine *el)
172 if (el->el_state.lastcmd != ED_SEARCH_PREV_HISTORY &&
173 el->el_state.lastcmd != ED_SEARCH_NEXT_HISTORY) {
174 el->el_search.patlen = EL_CURSOR(el) - el->el_line.buffer;
175 if (el->el_search.patlen >= EL_BUFSIZ)
176 el->el_search.patlen = EL_BUFSIZ - 1;
177 if (el->el_search.patlen != 0) {
178 (void) strncpy(el->el_search.patbuf, el->el_line.buffer,
179 el->el_search.patlen);
180 el->el_search.patbuf[el->el_search.patlen] = '\0';
182 el->el_search.patlen = strlen(el->el_search.patbuf);
185 (void) fprintf(el->el_errfile, "\neventno = %d\n",
186 el->el_history.eventno);
187 (void) fprintf(el->el_errfile, "patlen = %d\n", el->el_search.patlen);
188 (void) fprintf(el->el_errfile, "patbuf = \"%s\"\n",
189 el->el_search.patbuf);
190 (void) fprintf(el->el_errfile, "cursor %d lastchar %d\n",
191 EL_CURSOR(el) - el->el_line.buffer,
192 el->el_line.lastchar - el->el_line.buffer);
198 * Emacs incremental search
200 protected el_action_t
201 ce_inc_search(EditLine *el, int dir)
203 static const char STRfwd[] = {'f', 'w', 'd', '\0'},
204 STRbck[] = {'b', 'c', 'k', '\0'};
205 static char pchar = ':';/* ':' = normal, '?' = failed */
206 static char endcmd[2] = {'\0', '\0'};
207 char ch, *ocursor = el->el_line.cursor, oldpchar = pchar;
210 el_action_t ret = CC_NORM;
212 int ohisteventno = el->el_history.eventno;
213 int oldpatlen = el->el_search.patlen;
217 if (el->el_line.lastchar + sizeof(STRfwd) / sizeof(char) + 2 +
218 el->el_search.patlen >= el->el_line.limit)
223 if (el->el_search.patlen == 0) { /* first round */
226 el->el_search.patbuf[el->el_search.patlen++] = '.';
227 el->el_search.patbuf[el->el_search.patlen++] = '*';
231 *el->el_line.lastchar++ = '\n';
232 for (cp = (newdir == ED_SEARCH_PREV_HISTORY) ? STRbck : STRfwd;
233 *cp; *el->el_line.lastchar++ = *cp++)
235 *el->el_line.lastchar++ = pchar;
236 for (cp = &el->el_search.patbuf[1];
237 cp < &el->el_search.patbuf[el->el_search.patlen];
238 *el->el_line.lastchar++ = *cp++)
240 *el->el_line.lastchar = '\0';
243 if (el_getc(el, &ch) != 1)
244 return (ed_end_of_file(el, 0));
246 switch (el->el_map.current[(unsigned char) ch]) {
249 if (el->el_search.patlen > EL_BUFSIZ - 3)
252 el->el_search.patbuf[el->el_search.patlen++] =
254 *el->el_line.lastchar++ = ch;
255 *el->el_line.lastchar = '\0';
260 case EM_INC_SEARCH_NEXT:
261 newdir = ED_SEARCH_NEXT_HISTORY;
265 case EM_INC_SEARCH_PREV:
266 newdir = ED_SEARCH_PREV_HISTORY;
270 case ED_DELETE_PREV_CHAR:
271 if (el->el_search.patlen > 1)
279 case 0007: /* ^G: Abort */
284 case 0027: /* ^W: Append word */
285 /* No can do if globbing characters in pattern */
286 for (cp = &el->el_search.patbuf[1];; cp++)
287 if (cp >= &el->el_search.patbuf[el->el_search.patlen]) {
288 el->el_line.cursor +=
289 el->el_search.patlen - 1;
290 cp = c__next_word(el->el_line.cursor,
291 el->el_line.lastchar, 1,
293 while (el->el_line.cursor < cp &&
294 *el->el_line.cursor != '\n') {
295 if (el->el_search.patlen >
300 el->el_search.patbuf[el->el_search.patlen++] =
302 *el->el_line.lastchar++ =
303 *el->el_line.cursor++;
305 el->el_line.cursor = ocursor;
306 *el->el_line.lastchar = '\0';
309 } else if (isglob(*cp)) {
315 default: /* Terminate and execute cmd */
320 case 0033: /* ESC: Terminate */
328 while (el->el_line.lastchar > el->el_line.buffer &&
329 *el->el_line.lastchar != '\n')
330 *el->el_line.lastchar-- = '\0';
331 *el->el_line.lastchar = '\0';
335 /* Can't search if unmatched '[' */
336 for (cp = &el->el_search.patbuf[el->el_search.patlen-1],
338 cp > el->el_search.patbuf;
340 if (*cp == '[' || *cp == ']') {
344 if (el->el_search.patlen > 1 && ch != '[') {
345 if (redo && newdir == dir) {
346 if (pchar == '?') { /* wrap around */
347 el->el_history.eventno =
348 newdir == ED_SEARCH_PREV_HISTORY ? 0 : 0x7fffffff;
349 if (hist_get(el) == CC_ERROR)
350 /* el->el_history.event
354 el->el_line.cursor = newdir ==
355 ED_SEARCH_PREV_HISTORY ?
356 el->el_line.lastchar :
359 el->el_line.cursor +=
361 ED_SEARCH_PREV_HISTORY ?
365 el->el_search.patbuf[el->el_search.patlen++] =
367 el->el_search.patbuf[el->el_search.patlen++] =
370 el->el_search.patbuf[el->el_search.patlen] =
372 if (el->el_line.cursor < el->el_line.buffer ||
373 el->el_line.cursor > el->el_line.lastchar ||
374 (ret = ce_search_line(el,
375 &el->el_search.patbuf[1],
376 newdir)) == CC_ERROR) {
378 el->el_state.lastcmd =
379 (el_action_t) newdir;
380 ret = newdir == ED_SEARCH_PREV_HISTORY ?
381 ed_search_prev_history(el, 0) :
382 ed_search_next_history(el, 0);
383 if (ret != CC_ERROR) {
384 el->el_line.cursor = newdir ==
385 ED_SEARCH_PREV_HISTORY ?
386 el->el_line.lastchar :
388 (void) ce_search_line(el,
389 &el->el_search.patbuf[1],
393 el->el_search.patbuf[--el->el_search.patlen] =
395 if (ret == CC_ERROR) {
397 if (el->el_history.eventno !=
399 el->el_history.eventno =
401 if (hist_get(el) == CC_ERROR)
404 el->el_line.cursor = ocursor;
410 ret = ce_inc_search(el, newdir);
412 if (ret == CC_ERROR && pchar == '?' && oldpchar == ':')
414 * break abort of failed search at last
420 if (ret == CC_NORM || (ret == CC_ERROR && oldpatlen == 0)) {
421 /* restore on normal return or error exit */
423 el->el_search.patlen = oldpatlen;
424 if (el->el_history.eventno != ohisteventno) {
425 el->el_history.eventno = ohisteventno;
426 if (hist_get(el) == CC_ERROR)
429 el->el_line.cursor = ocursor;
433 if (done || ret != CC_NORM)
442 protected el_action_t
443 cv_search(EditLine *el, int dir)
446 char tmpbuf[EL_BUFSIZ];
451 tmpbuf[tmplen++] = '.';
452 tmpbuf[tmplen++] = '*';
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;
458 el->el_search.patdir = dir;
460 c_insert(el, 2); /* prompt + '\n' */
461 *el->el_line.cursor++ = '\n';
462 *el->el_line.cursor++ = dir == ED_SEARCH_PREV_HISTORY ? '/' : '?';
471 tmplen = c_gets(el, &tmpbuf[LEN]) + LEN;
473 tmpbuf[tmplen] = '\0';
477 * Use the old pattern, but wild-card it.
479 if (el->el_search.patlen == 0) {
480 el->el_line.buffer[0] = '\0';
481 el->el_line.lastchar = el->el_line.buffer;
482 el->el_line.cursor = el->el_line.buffer;
487 if (el->el_search.patbuf[0] != '.' &&
488 el->el_search.patbuf[0] != '*') {
489 (void) strncpy(tmpbuf, el->el_search.patbuf,
491 el->el_search.patbuf[0] = '.';
492 el->el_search.patbuf[1] = '*';
493 (void) strncpy(&el->el_search.patbuf[2], tmpbuf,
495 el->el_search.patlen++;
496 el->el_search.patbuf[el->el_search.patlen++] = '.';
497 el->el_search.patbuf[el->el_search.patlen++] = '*';
498 el->el_search.patbuf[el->el_search.patlen] = '\0';
503 tmpbuf[tmplen++] = '.';
504 tmpbuf[tmplen++] = '*';
506 tmpbuf[tmplen] = '\0';
507 (void) strncpy(el->el_search.patbuf, tmpbuf, EL_BUFSIZ - 1);
508 el->el_search.patlen = tmplen;
510 el->el_state.lastcmd = (el_action_t) dir; /* avoid c_setpat */
511 el->el_line.cursor = el->el_line.lastchar = el->el_line.buffer;
512 if ((dir == ED_SEARCH_PREV_HISTORY ? ed_search_prev_history(el, 0) :
513 ed_search_next_history(el, 0)) == CC_ERROR) {
519 *el->el_line.lastchar++ = '\n';
520 *el->el_line.lastchar = '\0';
530 * Look for a pattern inside a line
532 protected el_action_t
533 ce_search_line(EditLine *el, char *pattern, int dir)
537 if (dir == ED_SEARCH_PREV_HISTORY) {
538 for (cp = el->el_line.cursor; cp >= el->el_line.buffer; cp--)
539 if (el_match(cp, pattern)) {
540 el->el_line.cursor = cp;
545 for (cp = el->el_line.cursor; *cp != '\0' &&
546 cp < el->el_line.limit; cp++)
547 if (el_match(cp, pattern)) {
548 el->el_line.cursor = cp;
559 protected el_action_t
560 cv_repeat_srch(EditLine *el, int c)
564 (void) fprintf(el->el_errfile, "dir %d patlen %d patbuf %s\n",
565 c, el->el_search.patlen, el->el_search.patbuf);
568 el->el_state.lastcmd = (el_action_t) c; /* Hack to stop c_setpat */
569 el->el_line.lastchar = el->el_line.buffer;
572 case ED_SEARCH_NEXT_HISTORY:
573 return (ed_search_next_history(el, 0));
574 case ED_SEARCH_PREV_HISTORY:
575 return (ed_search_prev_history(el, 0));
582 /* cv_csearch_back():
583 * Vi character search reverse
585 protected el_action_t
586 cv_csearch_back(EditLine *el, int ch, int count, int tflag)
590 cp = el->el_line.cursor;
594 while (cp > el->el_line.buffer && *cp != ch)
598 if (cp < el->el_line.buffer || (cp == el->el_line.buffer && *cp != ch))
601 if (*cp == ch && tflag)
604 el->el_line.cursor = cp;
606 if (el->el_chared.c_vcmd.action & DELETE) {
607 el->el_line.cursor++;
611 re_refresh_cursor(el);
617 * Vi character search forward
619 protected el_action_t
620 cv_csearch_fwd(EditLine *el, int ch, int count, int tflag)
624 cp = el->el_line.cursor;
628 while (cp < el->el_line.lastchar && *cp != ch)
632 if (cp >= el->el_line.lastchar)
635 if (*cp == ch && tflag)
638 el->el_line.cursor = cp;
640 if (el->el_chared.c_vcmd.action & DELETE) {
641 el->el_line.cursor++;
645 re_refresh_cursor(el);