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