Home | History | Annotate | Line # | Download | only in libedit
search.c revision 1.15
      1  1.15       agc /*	$NetBSD: search.c,v 1.15 2003/08/07 16:44:33 agc Exp $	*/
      2   1.3     lukem 
      3   1.1       cgd /*-
      4   1.1       cgd  * Copyright (c) 1992, 1993
      5   1.1       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Christos Zoulas of Cornell University.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.15       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35  1.12  christos #include "config.h"
     36   1.1       cgd #if !defined(lint) && !defined(SCCSID)
     37   1.3     lukem #if 0
     38   1.1       cgd static char sccsid[] = "@(#)search.c	8.1 (Berkeley) 6/4/93";
     39   1.3     lukem #else
     40  1.15       agc __RCSID("$NetBSD: search.c,v 1.15 2003/08/07 16:44:33 agc Exp $");
     41   1.3     lukem #endif
     42   1.1       cgd #endif /* not lint && not SCCSID */
     43   1.1       cgd 
     44   1.1       cgd /*
     45   1.1       cgd  * search.c: History and character search functions
     46   1.1       cgd  */
     47   1.1       cgd #include <stdlib.h>
     48   1.2       jtc #if defined(REGEX)
     49   1.2       jtc #include <regex.h>
     50   1.2       jtc #elif defined(REGEXP)
     51   1.1       cgd #include <regexp.h>
     52   1.1       cgd #endif
     53   1.1       cgd #include "el.h"
     54   1.1       cgd 
     55   1.1       cgd /*
     56   1.1       cgd  * Adjust cursor in vi mode to include the character under it
     57   1.1       cgd  */
     58   1.9     lukem #define	EL_CURSOR(el) \
     59   1.1       cgd     ((el)->el_line.cursor + (((el)->el_map.type == MAP_VI) && \
     60   1.1       cgd 			    ((el)->el_map.current == (el)->el_map.alt)))
     61   1.1       cgd 
     62   1.1       cgd /* search_init():
     63   1.1       cgd  *	Initialize the search stuff
     64   1.1       cgd  */
     65   1.1       cgd protected int
     66   1.9     lukem search_init(EditLine *el)
     67   1.1       cgd {
     68   1.9     lukem 
     69   1.9     lukem 	el->el_search.patbuf = (char *) el_malloc(EL_BUFSIZ);
     70  1.10  christos 	if (el->el_search.patbuf == NULL)
     71  1.10  christos 		return (-1);
     72   1.9     lukem 	el->el_search.patlen = 0;
     73   1.9     lukem 	el->el_search.patdir = -1;
     74   1.9     lukem 	el->el_search.chacha = '\0';
     75  1.13  christos 	el->el_search.chadir = CHAR_FWD;
     76  1.13  christos 	el->el_search.chatflg = 0;
     77   1.9     lukem 	return (0);
     78   1.1       cgd }
     79   1.1       cgd 
     80   1.1       cgd 
     81   1.1       cgd /* search_end():
     82   1.1       cgd  *	Initialize the search stuff
     83   1.1       cgd  */
     84   1.1       cgd protected void
     85   1.9     lukem search_end(EditLine *el)
     86   1.1       cgd {
     87   1.9     lukem 
     88   1.9     lukem 	el_free((ptr_t) el->el_search.patbuf);
     89   1.9     lukem 	el->el_search.patbuf = NULL;
     90   1.1       cgd }
     91   1.1       cgd 
     92   1.9     lukem 
     93   1.1       cgd #ifdef REGEXP
     94   1.1       cgd /* regerror():
     95   1.1       cgd  *	Handle regular expression errors
     96   1.1       cgd  */
     97   1.8    simonb public void
     98   1.1       cgd /*ARGSUSED*/
     99   1.9     lukem regerror(const char *msg)
    100   1.1       cgd {
    101   1.1       cgd }
    102   1.1       cgd #endif
    103   1.1       cgd 
    104   1.9     lukem 
    105   1.1       cgd /* el_match():
    106   1.1       cgd  *	Return if string matches pattern
    107   1.1       cgd  */
    108   1.1       cgd protected int
    109   1.9     lukem el_match(const char *str, const char *pat)
    110   1.1       cgd {
    111   1.2       jtc #if defined (REGEX)
    112   1.9     lukem 	regex_t re;
    113   1.9     lukem 	int rv;
    114   1.2       jtc #elif defined (REGEXP)
    115   1.9     lukem 	regexp *rp;
    116   1.9     lukem 	int rv;
    117   1.8    simonb #else
    118   1.9     lukem 	extern char	*re_comp(const char *);
    119   1.9     lukem 	extern int	 re_exec(const char *);
    120   1.1       cgd #endif
    121   1.1       cgd 
    122   1.9     lukem 	if (strstr(str, pat) != NULL)
    123   1.9     lukem 		return (1);
    124   1.2       jtc 
    125   1.2       jtc #if defined(REGEX)
    126   1.9     lukem 	if (regcomp(&re, pat, 0) == 0) {
    127   1.9     lukem 		rv = regexec(&re, str, 0, NULL, 0) == 0;
    128   1.9     lukem 		regfree(&re);
    129   1.9     lukem 	} else {
    130   1.9     lukem 		rv = 0;
    131   1.9     lukem 	}
    132   1.9     lukem 	return (rv);
    133   1.2       jtc #elif defined(REGEXP)
    134   1.9     lukem 	if ((re = regcomp(pat)) != NULL) {
    135   1.9     lukem 		rv = regexec(re, str);
    136   1.9     lukem 		free((ptr_t) re);
    137   1.9     lukem 	} else {
    138   1.9     lukem 		rv = 0;
    139   1.9     lukem 	}
    140   1.9     lukem 	return (rv);
    141   1.2       jtc #else
    142   1.9     lukem 	if (re_comp(pat) != NULL)
    143   1.9     lukem 		return (0);
    144   1.9     lukem 	else
    145   1.9     lukem 		return (re_exec(str) == 1);
    146   1.1       cgd #endif
    147   1.1       cgd }
    148   1.1       cgd 
    149   1.1       cgd 
    150   1.1       cgd /* c_hmatch():
    151   1.1       cgd  *	 return True if the pattern matches the prefix
    152   1.1       cgd  */
    153   1.1       cgd protected int
    154   1.9     lukem c_hmatch(EditLine *el, const char *str)
    155   1.1       cgd {
    156   1.1       cgd #ifdef SDEBUG
    157   1.9     lukem 	(void) fprintf(el->el_errfile, "match `%s' with `%s'\n",
    158   1.9     lukem 	    el->el_search.patbuf, str);
    159   1.1       cgd #endif /* SDEBUG */
    160   1.8    simonb 
    161   1.9     lukem 	return (el_match(str, el->el_search.patbuf));
    162   1.1       cgd }
    163   1.1       cgd 
    164   1.1       cgd 
    165   1.8    simonb /* c_setpat():
    166   1.1       cgd  *	Set the history seatch pattern
    167   1.1       cgd  */
    168   1.1       cgd protected void
    169   1.9     lukem c_setpat(EditLine *el)
    170   1.1       cgd {
    171   1.9     lukem 	if (el->el_state.lastcmd != ED_SEARCH_PREV_HISTORY &&
    172   1.9     lukem 	    el->el_state.lastcmd != ED_SEARCH_NEXT_HISTORY) {
    173   1.9     lukem 		el->el_search.patlen = EL_CURSOR(el) - el->el_line.buffer;
    174   1.9     lukem 		if (el->el_search.patlen >= EL_BUFSIZ)
    175   1.9     lukem 			el->el_search.patlen = EL_BUFSIZ - 1;
    176   1.9     lukem 		if (el->el_search.patlen != 0) {
    177   1.9     lukem 			(void) strncpy(el->el_search.patbuf, el->el_line.buffer,
    178   1.9     lukem 			    el->el_search.patlen);
    179   1.9     lukem 			el->el_search.patbuf[el->el_search.patlen] = '\0';
    180   1.9     lukem 		} else
    181   1.9     lukem 			el->el_search.patlen = strlen(el->el_search.patbuf);
    182   1.1       cgd 	}
    183   1.1       cgd #ifdef SDEBUG
    184   1.9     lukem 	(void) fprintf(el->el_errfile, "\neventno = %d\n",
    185   1.9     lukem 	    el->el_history.eventno);
    186   1.9     lukem 	(void) fprintf(el->el_errfile, "patlen = %d\n", el->el_search.patlen);
    187   1.9     lukem 	(void) fprintf(el->el_errfile, "patbuf = \"%s\"\n",
    188   1.9     lukem 	    el->el_search.patbuf);
    189   1.9     lukem 	(void) fprintf(el->el_errfile, "cursor %d lastchar %d\n",
    190   1.9     lukem 	    EL_CURSOR(el) - el->el_line.buffer,
    191   1.9     lukem 	    el->el_line.lastchar - el->el_line.buffer);
    192   1.1       cgd #endif
    193   1.1       cgd }
    194   1.1       cgd 
    195   1.1       cgd 
    196   1.1       cgd /* ce_inc_search():
    197   1.1       cgd  *	Emacs incremental search
    198   1.1       cgd  */
    199   1.1       cgd protected el_action_t
    200   1.9     lukem ce_inc_search(EditLine *el, int dir)
    201   1.9     lukem {
    202  1.11  jdolecek 	static const char STRfwd[] = {'f', 'w', 'd', '\0'},
    203   1.9     lukem 	     STRbck[] = {'b', 'c', 'k', '\0'};
    204   1.9     lukem 	static char pchar = ':';/* ':' = normal, '?' = failed */
    205   1.9     lukem 	static char endcmd[2] = {'\0', '\0'};
    206  1.11  jdolecek 	char ch, *ocursor = el->el_line.cursor, oldpchar = pchar;
    207  1.11  jdolecek 	const char *cp;
    208   1.9     lukem 
    209   1.9     lukem 	el_action_t ret = CC_NORM;
    210   1.9     lukem 
    211   1.9     lukem 	int ohisteventno = el->el_history.eventno;
    212   1.9     lukem 	int oldpatlen = el->el_search.patlen;
    213   1.9     lukem 	int newdir = dir;
    214   1.9     lukem 	int done, redo;
    215   1.9     lukem 
    216   1.9     lukem 	if (el->el_line.lastchar + sizeof(STRfwd) / sizeof(char) + 2 +
    217   1.9     lukem 	    el->el_search.patlen >= el->el_line.limit)
    218   1.9     lukem 		return (CC_ERROR);
    219   1.1       cgd 
    220   1.9     lukem 	for (;;) {
    221   1.1       cgd 
    222   1.9     lukem 		if (el->el_search.patlen == 0) {	/* first round */
    223   1.9     lukem 			pchar = ':';
    224   1.1       cgd #ifdef ANCHOR
    225   1.9     lukem 			el->el_search.patbuf[el->el_search.patlen++] = '.';
    226   1.9     lukem 			el->el_search.patbuf[el->el_search.patlen++] = '*';
    227   1.1       cgd #endif
    228   1.9     lukem 		}
    229   1.9     lukem 		done = redo = 0;
    230   1.9     lukem 		*el->el_line.lastchar++ = '\n';
    231  1.11  jdolecek 		for (cp = (newdir == ED_SEARCH_PREV_HISTORY) ? STRbck : STRfwd;
    232   1.9     lukem 		    *cp; *el->el_line.lastchar++ = *cp++)
    233   1.9     lukem 			continue;
    234   1.9     lukem 		*el->el_line.lastchar++ = pchar;
    235   1.9     lukem 		for (cp = &el->el_search.patbuf[1];
    236   1.9     lukem 		    cp < &el->el_search.patbuf[el->el_search.patlen];
    237   1.9     lukem 		    *el->el_line.lastchar++ = *cp++)
    238   1.9     lukem 			continue;
    239   1.1       cgd 		*el->el_line.lastchar = '\0';
    240   1.1       cgd 		re_refresh(el);
    241   1.1       cgd 
    242   1.9     lukem 		if (el_getc(el, &ch) != 1)
    243   1.9     lukem 			return (ed_end_of_file(el, 0));
    244   1.1       cgd 
    245   1.9     lukem 		switch (el->el_map.current[(unsigned char) ch]) {
    246   1.9     lukem 		case ED_INSERT:
    247   1.9     lukem 		case ED_DIGIT:
    248   1.9     lukem 			if (el->el_search.patlen > EL_BUFSIZ - 3)
    249   1.1       cgd 				term_beep(el);
    250   1.9     lukem 			else {
    251   1.9     lukem 				el->el_search.patbuf[el->el_search.patlen++] =
    252   1.9     lukem 				    ch;
    253   1.9     lukem 				*el->el_line.lastchar++ = ch;
    254   1.9     lukem 				*el->el_line.lastchar = '\0';
    255   1.9     lukem 				re_refresh(el);
    256   1.1       cgd 			}
    257   1.1       cgd 			break;
    258   1.9     lukem 
    259   1.9     lukem 		case EM_INC_SEARCH_NEXT:
    260   1.9     lukem 			newdir = ED_SEARCH_NEXT_HISTORY;
    261   1.9     lukem 			redo++;
    262   1.9     lukem 			break;
    263   1.9     lukem 
    264   1.9     lukem 		case EM_INC_SEARCH_PREV:
    265   1.9     lukem 			newdir = ED_SEARCH_PREV_HISTORY;
    266   1.9     lukem 			redo++;
    267   1.9     lukem 			break;
    268   1.9     lukem 
    269   1.9     lukem 		case ED_DELETE_PREV_CHAR:
    270   1.9     lukem 			if (el->el_search.patlen > 1)
    271   1.9     lukem 				done++;
    272   1.9     lukem 			else
    273   1.9     lukem 				term_beep(el);
    274   1.1       cgd 			break;
    275   1.8    simonb 
    276   1.9     lukem 		default:
    277   1.9     lukem 			switch (ch) {
    278   1.9     lukem 			case 0007:	/* ^G: Abort */
    279   1.9     lukem 				ret = CC_ERROR;
    280   1.9     lukem 				done++;
    281   1.9     lukem 				break;
    282   1.9     lukem 
    283   1.9     lukem 			case 0027:	/* ^W: Append word */
    284   1.9     lukem 			/* No can do if globbing characters in pattern */
    285   1.9     lukem 				for (cp = &el->el_search.patbuf[1];; cp++)
    286   1.9     lukem 				    if (cp >= &el->el_search.patbuf[el->el_search.patlen]) {
    287   1.9     lukem 					el->el_line.cursor +=
    288   1.9     lukem 					    el->el_search.patlen - 1;
    289   1.9     lukem 					cp = c__next_word(el->el_line.cursor,
    290   1.9     lukem 					    el->el_line.lastchar, 1,
    291   1.9     lukem 					    ce__isword);
    292   1.9     lukem 					while (el->el_line.cursor < cp &&
    293   1.9     lukem 					    *el->el_line.cursor != '\n') {
    294   1.9     lukem 						if (el->el_search.patlen >
    295   1.9     lukem 						    EL_BUFSIZ - 3) {
    296   1.9     lukem 							term_beep(el);
    297   1.9     lukem 							break;
    298   1.9     lukem 						}
    299   1.9     lukem 						el->el_search.patbuf[el->el_search.patlen++] =
    300   1.9     lukem 						    *el->el_line.cursor;
    301   1.9     lukem 						*el->el_line.lastchar++ =
    302   1.9     lukem 						    *el->el_line.cursor++;
    303   1.9     lukem 					}
    304   1.9     lukem 					el->el_line.cursor = ocursor;
    305   1.9     lukem 					*el->el_line.lastchar = '\0';
    306   1.9     lukem 					re_refresh(el);
    307   1.9     lukem 					break;
    308   1.9     lukem 				    } else if (isglob(*cp)) {
    309   1.9     lukem 					    term_beep(el);
    310   1.9     lukem 					    break;
    311   1.9     lukem 				    }
    312   1.9     lukem 				break;
    313   1.9     lukem 
    314   1.9     lukem 			default:	/* Terminate and execute cmd */
    315   1.9     lukem 				endcmd[0] = ch;
    316   1.9     lukem 				el_push(el, endcmd);
    317   1.9     lukem 				/* FALLTHROUGH */
    318   1.9     lukem 
    319   1.9     lukem 			case 0033:	/* ESC: Terminate */
    320   1.9     lukem 				ret = CC_REFRESH;
    321   1.9     lukem 				done++;
    322   1.9     lukem 				break;
    323   1.9     lukem 			}
    324   1.9     lukem 			break;
    325   1.1       cgd 		}
    326   1.1       cgd 
    327   1.9     lukem 		while (el->el_line.lastchar > el->el_line.buffer &&
    328   1.9     lukem 		    *el->el_line.lastchar != '\n')
    329   1.9     lukem 			*el->el_line.lastchar-- = '\0';
    330   1.9     lukem 		*el->el_line.lastchar = '\0';
    331   1.1       cgd 
    332   1.9     lukem 		if (!done) {
    333   1.1       cgd 
    334   1.9     lukem 			/* Can't search if unmatched '[' */
    335   1.9     lukem 			for (cp = &el->el_search.patbuf[el->el_search.patlen-1],
    336   1.9     lukem 			    ch = ']';
    337   1.9     lukem 			    cp > el->el_search.patbuf;
    338   1.9     lukem 			    cp--)
    339   1.9     lukem 				if (*cp == '[' || *cp == ']') {
    340   1.9     lukem 					ch = *cp;
    341   1.9     lukem 					break;
    342   1.9     lukem 				}
    343   1.9     lukem 			if (el->el_search.patlen > 1 && ch != '[') {
    344   1.9     lukem 				if (redo && newdir == dir) {
    345   1.9     lukem 					if (pchar == '?') { /* wrap around */
    346   1.9     lukem 						el->el_history.eventno =
    347   1.9     lukem 						    newdir == ED_SEARCH_PREV_HISTORY ? 0 : 0x7fffffff;
    348   1.9     lukem 						if (hist_get(el) == CC_ERROR)
    349   1.9     lukem 							/* el->el_history.event
    350   1.9     lukem 							 * no was fixed by
    351   1.9     lukem 							 * first call */
    352   1.9     lukem 							(void) hist_get(el);
    353   1.9     lukem 						el->el_line.cursor = newdir ==
    354   1.9     lukem 						    ED_SEARCH_PREV_HISTORY ?
    355   1.9     lukem 						    el->el_line.lastchar :
    356   1.9     lukem 						    el->el_line.buffer;
    357   1.9     lukem 					} else
    358   1.9     lukem 						el->el_line.cursor +=
    359   1.9     lukem 						    newdir ==
    360   1.9     lukem 						    ED_SEARCH_PREV_HISTORY ?
    361   1.9     lukem 						    -1 : 1;
    362   1.9     lukem 				}
    363   1.9     lukem #ifdef ANCHOR
    364   1.9     lukem 				el->el_search.patbuf[el->el_search.patlen++] =
    365   1.9     lukem 				    '.';
    366   1.9     lukem 				el->el_search.patbuf[el->el_search.patlen++] =
    367   1.9     lukem 				    '*';
    368   1.9     lukem #endif
    369   1.9     lukem 				el->el_search.patbuf[el->el_search.patlen] =
    370   1.9     lukem 				    '\0';
    371   1.9     lukem 				if (el->el_line.cursor < el->el_line.buffer ||
    372   1.9     lukem 				    el->el_line.cursor > el->el_line.lastchar ||
    373   1.9     lukem 				    (ret = ce_search_line(el,
    374   1.9     lukem 				    &el->el_search.patbuf[1],
    375   1.9     lukem 				    newdir)) == CC_ERROR) {
    376   1.9     lukem 					/* avoid c_setpat */
    377   1.9     lukem 					el->el_state.lastcmd =
    378   1.9     lukem 					    (el_action_t) newdir;
    379   1.9     lukem 					ret = newdir == ED_SEARCH_PREV_HISTORY ?
    380   1.9     lukem 					    ed_search_prev_history(el, 0) :
    381   1.9     lukem 					    ed_search_next_history(el, 0);
    382   1.9     lukem 					if (ret != CC_ERROR) {
    383   1.9     lukem 						el->el_line.cursor = newdir ==
    384   1.9     lukem 						    ED_SEARCH_PREV_HISTORY ?
    385   1.9     lukem 						    el->el_line.lastchar :
    386   1.9     lukem 						    el->el_line.buffer;
    387   1.9     lukem 						(void) ce_search_line(el,
    388   1.9     lukem 						    &el->el_search.patbuf[1],
    389   1.9     lukem 						    newdir);
    390   1.9     lukem 					}
    391   1.9     lukem 				}
    392   1.9     lukem 				el->el_search.patbuf[--el->el_search.patlen] =
    393   1.9     lukem 				    '\0';
    394   1.9     lukem 				if (ret == CC_ERROR) {
    395   1.9     lukem 					term_beep(el);
    396   1.9     lukem 					if (el->el_history.eventno !=
    397   1.9     lukem 					    ohisteventno) {
    398   1.9     lukem 						el->el_history.eventno =
    399   1.9     lukem 						    ohisteventno;
    400   1.9     lukem 						if (hist_get(el) == CC_ERROR)
    401   1.9     lukem 							return (CC_ERROR);
    402   1.9     lukem 					}
    403   1.9     lukem 					el->el_line.cursor = ocursor;
    404   1.9     lukem 					pchar = '?';
    405   1.9     lukem 				} else {
    406   1.9     lukem 					pchar = ':';
    407   1.9     lukem 				}
    408   1.9     lukem 			}
    409   1.9     lukem 			ret = ce_inc_search(el, newdir);
    410   1.1       cgd 
    411   1.9     lukem 			if (ret == CC_ERROR && pchar == '?' && oldpchar == ':')
    412   1.9     lukem 				/*
    413   1.9     lukem 				 * break abort of failed search at last
    414   1.9     lukem 				 * non-failed
    415   1.9     lukem 				 */
    416   1.9     lukem 				ret = CC_NORM;
    417   1.1       cgd 
    418   1.9     lukem 		}
    419   1.9     lukem 		if (ret == CC_NORM || (ret == CC_ERROR && oldpatlen == 0)) {
    420   1.9     lukem 			/* restore on normal return or error exit */
    421   1.9     lukem 			pchar = oldpchar;
    422   1.9     lukem 			el->el_search.patlen = oldpatlen;
    423   1.9     lukem 			if (el->el_history.eventno != ohisteventno) {
    424   1.9     lukem 				el->el_history.eventno = ohisteventno;
    425   1.9     lukem 				if (hist_get(el) == CC_ERROR)
    426   1.9     lukem 					return (CC_ERROR);
    427   1.9     lukem 			}
    428   1.9     lukem 			el->el_line.cursor = ocursor;
    429   1.9     lukem 			if (ret == CC_ERROR)
    430   1.9     lukem 				re_refresh(el);
    431   1.9     lukem 		}
    432   1.9     lukem 		if (done || ret != CC_NORM)
    433   1.9     lukem 			return (ret);
    434   1.1       cgd 	}
    435   1.1       cgd }
    436   1.1       cgd 
    437   1.1       cgd 
    438   1.1       cgd /* cv_search():
    439   1.1       cgd  *	Vi search.
    440   1.1       cgd  */
    441   1.1       cgd protected el_action_t
    442   1.9     lukem cv_search(EditLine *el, int dir)
    443   1.9     lukem {
    444   1.9     lukem 	char ch;
    445   1.9     lukem 	char tmpbuf[EL_BUFSIZ];
    446   1.9     lukem 	int tmplen;
    447   1.1       cgd 
    448   1.1       cgd #ifdef ANCHOR
    449  1.14  christos 	tmpbuf[0] = '.';
    450  1.14  christos 	tmpbuf[1] = '*';
    451  1.14  christos #define	LEN	2
    452  1.14  christos #else
    453  1.14  christos #define	LEN	0
    454   1.1       cgd #endif
    455  1.14  christos 	tmplen = LEN;
    456   1.1       cgd 
    457   1.9     lukem 	el->el_search.patdir = dir;
    458   1.9     lukem 
    459  1.14  christos 	tmplen = c_gets(el, &tmpbuf[LEN],
    460  1.14  christos 		dir == ED_SEARCH_PREV_HISTORY ? "\n/" : "\n?" );
    461  1.14  christos 	if (tmplen == -1)
    462  1.14  christos 		return CC_REFRESH;
    463   1.1       cgd 
    464  1.14  christos 	tmplen += LEN;
    465   1.9     lukem 	ch = tmpbuf[tmplen];
    466   1.9     lukem 	tmpbuf[tmplen] = '\0';
    467   1.9     lukem 
    468   1.9     lukem 	if (tmplen == LEN) {
    469   1.9     lukem 		/*
    470   1.9     lukem 		 * Use the old pattern, but wild-card it.
    471   1.9     lukem 		 */
    472   1.9     lukem 		if (el->el_search.patlen == 0) {
    473   1.9     lukem 			re_refresh(el);
    474   1.9     lukem 			return (CC_ERROR);
    475   1.9     lukem 		}
    476   1.1       cgd #ifdef ANCHOR
    477   1.9     lukem 		if (el->el_search.patbuf[0] != '.' &&
    478   1.9     lukem 		    el->el_search.patbuf[0] != '*') {
    479   1.9     lukem 			(void) strncpy(tmpbuf, el->el_search.patbuf,
    480   1.9     lukem 			    sizeof(tmpbuf) - 1);
    481   1.9     lukem 			el->el_search.patbuf[0] = '.';
    482   1.9     lukem 			el->el_search.patbuf[1] = '*';
    483   1.9     lukem 			(void) strncpy(&el->el_search.patbuf[2], tmpbuf,
    484   1.9     lukem 			    EL_BUFSIZ - 3);
    485   1.9     lukem 			el->el_search.patlen++;
    486   1.9     lukem 			el->el_search.patbuf[el->el_search.patlen++] = '.';
    487   1.9     lukem 			el->el_search.patbuf[el->el_search.patlen++] = '*';
    488   1.9     lukem 			el->el_search.patbuf[el->el_search.patlen] = '\0';
    489   1.9     lukem 		}
    490   1.1       cgd #endif
    491   1.9     lukem 	} else {
    492   1.1       cgd #ifdef ANCHOR
    493   1.9     lukem 		tmpbuf[tmplen++] = '.';
    494   1.9     lukem 		tmpbuf[tmplen++] = '*';
    495   1.1       cgd #endif
    496   1.9     lukem 		tmpbuf[tmplen] = '\0';
    497   1.9     lukem 		(void) strncpy(el->el_search.patbuf, tmpbuf, EL_BUFSIZ - 1);
    498   1.9     lukem 		el->el_search.patlen = tmplen;
    499   1.9     lukem 	}
    500   1.9     lukem 	el->el_state.lastcmd = (el_action_t) dir;	/* avoid c_setpat */
    501   1.9     lukem 	el->el_line.cursor = el->el_line.lastchar = el->el_line.buffer;
    502   1.9     lukem 	if ((dir == ED_SEARCH_PREV_HISTORY ? ed_search_prev_history(el, 0) :
    503  1.14  christos 	    ed_search_next_history(el, 0)) == CC_ERROR) {
    504   1.9     lukem 		re_refresh(el);
    505   1.9     lukem 		return (CC_ERROR);
    506   1.1       cgd 	}
    507  1.14  christos 	if (ch == 0033) {
    508  1.14  christos 		re_refresh(el);
    509  1.14  christos 		return ed_newline(el, 0);
    510  1.14  christos 	}
    511  1.14  christos 	return (CC_REFRESH);
    512   1.1       cgd }
    513   1.1       cgd 
    514   1.1       cgd 
    515   1.1       cgd /* ce_search_line():
    516   1.1       cgd  *	Look for a pattern inside a line
    517   1.1       cgd  */
    518   1.1       cgd protected el_action_t
    519   1.9     lukem ce_search_line(EditLine *el, char *pattern, int dir)
    520   1.9     lukem {
    521   1.9     lukem 	char *cp;
    522   1.9     lukem 
    523   1.9     lukem 	if (dir == ED_SEARCH_PREV_HISTORY) {
    524   1.9     lukem 		for (cp = el->el_line.cursor; cp >= el->el_line.buffer; cp--)
    525   1.9     lukem 			if (el_match(cp, pattern)) {
    526   1.9     lukem 				el->el_line.cursor = cp;
    527   1.9     lukem 				return (CC_NORM);
    528   1.9     lukem 			}
    529   1.9     lukem 		return (CC_ERROR);
    530   1.9     lukem 	} else {
    531   1.9     lukem 		for (cp = el->el_line.cursor; *cp != '\0' &&
    532   1.9     lukem 		    cp < el->el_line.limit; cp++)
    533   1.9     lukem 			if (el_match(cp, pattern)) {
    534   1.9     lukem 				el->el_line.cursor = cp;
    535   1.9     lukem 				return (CC_NORM);
    536   1.9     lukem 			}
    537   1.9     lukem 		return (CC_ERROR);
    538   1.9     lukem 	}
    539   1.1       cgd }
    540   1.1       cgd 
    541   1.1       cgd 
    542   1.1       cgd /* cv_repeat_srch():
    543   1.1       cgd  *	Vi repeat search
    544   1.1       cgd  */
    545   1.1       cgd protected el_action_t
    546   1.9     lukem cv_repeat_srch(EditLine *el, int c)
    547   1.1       cgd {
    548   1.9     lukem 
    549   1.1       cgd #ifdef SDEBUG
    550   1.9     lukem 	(void) fprintf(el->el_errfile, "dir %d patlen %d patbuf %s\n",
    551   1.9     lukem 	    c, el->el_search.patlen, el->el_search.patbuf);
    552   1.1       cgd #endif
    553   1.1       cgd 
    554   1.9     lukem 	el->el_state.lastcmd = (el_action_t) c;	/* Hack to stop c_setpat */
    555   1.9     lukem 	el->el_line.lastchar = el->el_line.buffer;
    556   1.1       cgd 
    557   1.9     lukem 	switch (c) {
    558   1.9     lukem 	case ED_SEARCH_NEXT_HISTORY:
    559   1.9     lukem 		return (ed_search_next_history(el, 0));
    560   1.9     lukem 	case ED_SEARCH_PREV_HISTORY:
    561   1.9     lukem 		return (ed_search_prev_history(el, 0));
    562   1.9     lukem 	default:
    563   1.9     lukem 		return (CC_ERROR);
    564   1.9     lukem 	}
    565   1.1       cgd }
    566   1.1       cgd 
    567   1.1       cgd 
    568  1.13  christos /* cv_csearch():
    569  1.13  christos  *	Vi character search
    570   1.1       cgd  */
    571   1.1       cgd protected el_action_t
    572  1.13  christos cv_csearch(EditLine *el, int direction, int ch, int count, int tflag)
    573   1.9     lukem {
    574   1.9     lukem 	char *cp;
    575   1.9     lukem 
    576  1.13  christos 	if (ch == 0)
    577  1.13  christos 		return CC_ERROR;
    578   1.9     lukem 
    579  1.13  christos 	if (ch == -1) {
    580  1.13  christos 		char c;
    581  1.13  christos 		if (el_getc(el, &c) != 1)
    582  1.13  christos 			return ed_end_of_file(el, 0);
    583  1.13  christos 		ch = c;
    584   1.9     lukem 	}
    585   1.1       cgd 
    586  1.13  christos 	/* Save for ';' and ',' commands */
    587  1.13  christos 	el->el_search.chacha = ch;
    588  1.13  christos 	el->el_search.chadir = direction;
    589  1.13  christos 	el->el_search.chatflg = tflag;
    590   1.9     lukem 
    591   1.9     lukem 	cp = el->el_line.cursor;
    592   1.9     lukem 	while (count--) {
    593   1.9     lukem 		if (*cp == ch)
    594  1.13  christos 			cp += direction;
    595  1.13  christos 		for (;;cp += direction) {
    596  1.13  christos 			if (cp >= el->el_line.lastchar)
    597  1.13  christos 				return CC_ERROR;
    598  1.13  christos 			if (cp < el->el_line.buffer)
    599  1.13  christos 				return CC_ERROR;
    600  1.13  christos 			if (*cp == ch)
    601  1.13  christos 				break;
    602  1.13  christos 		}
    603   1.9     lukem 	}
    604   1.9     lukem 
    605  1.13  christos 	if (tflag)
    606  1.13  christos 		cp -= direction;
    607   1.9     lukem 
    608   1.9     lukem 	el->el_line.cursor = cp;
    609   1.9     lukem 
    610  1.13  christos 	if (el->el_chared.c_vcmd.action != NOP) {
    611  1.13  christos 		if (direction > 0)
    612  1.13  christos 			el->el_line.cursor++;
    613   1.9     lukem 		cv_delfini(el);
    614  1.13  christos 		return CC_REFRESH;
    615   1.9     lukem 	}
    616  1.13  christos 	return CC_CURSOR;
    617   1.1       cgd }
    618