xref: /minix3/lib/libedit/chared.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: chared.c,v 1.40 2014/06/18 18:12:28 christos Exp $	*/
23e1db26aSLionel Sambuc 
33e1db26aSLionel Sambuc /*-
43e1db26aSLionel Sambuc  * Copyright (c) 1992, 1993
53e1db26aSLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
63e1db26aSLionel Sambuc  *
73e1db26aSLionel Sambuc  * This code is derived from software contributed to Berkeley by
83e1db26aSLionel Sambuc  * Christos Zoulas of Cornell University.
93e1db26aSLionel Sambuc  *
103e1db26aSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
113e1db26aSLionel Sambuc  * modification, are permitted provided that the following conditions
123e1db26aSLionel Sambuc  * are met:
133e1db26aSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
143e1db26aSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
153e1db26aSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
163e1db26aSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
173e1db26aSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
183e1db26aSLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
193e1db26aSLionel Sambuc  *    may be used to endorse or promote products derived from this software
203e1db26aSLionel Sambuc  *    without specific prior written permission.
213e1db26aSLionel Sambuc  *
223e1db26aSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
233e1db26aSLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
243e1db26aSLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
253e1db26aSLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
263e1db26aSLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
273e1db26aSLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
283e1db26aSLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
293e1db26aSLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
303e1db26aSLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
313e1db26aSLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
323e1db26aSLionel Sambuc  * SUCH DAMAGE.
333e1db26aSLionel Sambuc  */
343e1db26aSLionel Sambuc 
353e1db26aSLionel Sambuc #include "config.h"
363e1db26aSLionel Sambuc #if !defined(lint) && !defined(SCCSID)
373e1db26aSLionel Sambuc #if 0
383e1db26aSLionel Sambuc static char sccsid[] = "@(#)chared.c	8.1 (Berkeley) 6/4/93";
393e1db26aSLionel Sambuc #else
40*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: chared.c,v 1.40 2014/06/18 18:12:28 christos Exp $");
413e1db26aSLionel Sambuc #endif
423e1db26aSLionel Sambuc #endif /* not lint && not SCCSID */
433e1db26aSLionel Sambuc 
443e1db26aSLionel Sambuc /*
453e1db26aSLionel Sambuc  * chared.c: Character editor utilities
463e1db26aSLionel Sambuc  */
473e1db26aSLionel Sambuc #include <stdlib.h>
483e1db26aSLionel Sambuc #include "el.h"
493e1db26aSLionel Sambuc 
503e1db26aSLionel Sambuc private void ch__clearmacro (EditLine *);
513e1db26aSLionel Sambuc 
523e1db26aSLionel Sambuc /* value to leave unused in line buffer */
533e1db26aSLionel Sambuc #define	EL_LEAVE	2
543e1db26aSLionel Sambuc 
553e1db26aSLionel Sambuc /* cv_undo():
563e1db26aSLionel Sambuc  *	Handle state for the vi undo command
573e1db26aSLionel Sambuc  */
583e1db26aSLionel Sambuc protected void
cv_undo(EditLine * el)593e1db26aSLionel Sambuc cv_undo(EditLine *el)
603e1db26aSLionel Sambuc {
613e1db26aSLionel Sambuc 	c_undo_t *vu = &el->el_chared.c_undo;
623e1db26aSLionel Sambuc 	c_redo_t *r = &el->el_chared.c_redo;
633e1db26aSLionel Sambuc 	size_t size;
643e1db26aSLionel Sambuc 
653e1db26aSLionel Sambuc 	/* Save entire line for undo */
663e1db26aSLionel Sambuc 	size = (size_t)(el->el_line.lastchar - el->el_line.buffer);
673e1db26aSLionel Sambuc 	vu->len = (ssize_t)size;
683e1db26aSLionel Sambuc 	vu->cursor = (int)(el->el_line.cursor - el->el_line.buffer);
693e1db26aSLionel Sambuc 	(void)memcpy(vu->buf, el->el_line.buffer, size * sizeof(*vu->buf));
703e1db26aSLionel Sambuc 
713e1db26aSLionel Sambuc 	/* save command info for redo */
723e1db26aSLionel Sambuc 	r->count = el->el_state.doingarg ? el->el_state.argument : 0;
733e1db26aSLionel Sambuc 	r->action = el->el_chared.c_vcmd.action;
743e1db26aSLionel Sambuc 	r->pos = r->buf;
753e1db26aSLionel Sambuc 	r->cmd = el->el_state.thiscmd;
763e1db26aSLionel Sambuc 	r->ch = el->el_state.thisch;
773e1db26aSLionel Sambuc }
783e1db26aSLionel Sambuc 
793e1db26aSLionel Sambuc /* cv_yank():
803e1db26aSLionel Sambuc  *	Save yank/delete data for paste
813e1db26aSLionel Sambuc  */
823e1db26aSLionel Sambuc protected void
cv_yank(EditLine * el,const Char * ptr,int size)833e1db26aSLionel Sambuc cv_yank(EditLine *el, const Char *ptr, int size)
843e1db26aSLionel Sambuc {
853e1db26aSLionel Sambuc 	c_kill_t *k = &el->el_chared.c_kill;
863e1db26aSLionel Sambuc 
873e1db26aSLionel Sambuc 	(void)memcpy(k->buf, ptr, (size_t)size * sizeof(*k->buf));
883e1db26aSLionel Sambuc 	k->last = k->buf + size;
893e1db26aSLionel Sambuc }
903e1db26aSLionel Sambuc 
913e1db26aSLionel Sambuc 
923e1db26aSLionel Sambuc /* c_insert():
933e1db26aSLionel Sambuc  *	Insert num characters
943e1db26aSLionel Sambuc  */
953e1db26aSLionel Sambuc protected void
c_insert(EditLine * el,int num)963e1db26aSLionel Sambuc c_insert(EditLine *el, int num)
973e1db26aSLionel Sambuc {
983e1db26aSLionel Sambuc 	Char *cp;
993e1db26aSLionel Sambuc 
1003e1db26aSLionel Sambuc 	if (el->el_line.lastchar + num >= el->el_line.limit) {
1013e1db26aSLionel Sambuc 		if (!ch_enlargebufs(el, (size_t)num))
1023e1db26aSLionel Sambuc 			return;		/* can't go past end of buffer */
1033e1db26aSLionel Sambuc 	}
1043e1db26aSLionel Sambuc 
1053e1db26aSLionel Sambuc 	if (el->el_line.cursor < el->el_line.lastchar) {
1063e1db26aSLionel Sambuc 		/* if I must move chars */
1073e1db26aSLionel Sambuc 		for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
1083e1db26aSLionel Sambuc 			cp[num] = *cp;
1093e1db26aSLionel Sambuc 	}
1103e1db26aSLionel Sambuc 	el->el_line.lastchar += num;
1113e1db26aSLionel Sambuc }
1123e1db26aSLionel Sambuc 
1133e1db26aSLionel Sambuc 
1143e1db26aSLionel Sambuc /* c_delafter():
1153e1db26aSLionel Sambuc  *	Delete num characters after the cursor
1163e1db26aSLionel Sambuc  */
1173e1db26aSLionel Sambuc protected void
c_delafter(EditLine * el,int num)1183e1db26aSLionel Sambuc c_delafter(EditLine *el, int num)
1193e1db26aSLionel Sambuc {
1203e1db26aSLionel Sambuc 
1213e1db26aSLionel Sambuc 	if (el->el_line.cursor + num > el->el_line.lastchar)
1223e1db26aSLionel Sambuc 		num = (int)(el->el_line.lastchar - el->el_line.cursor);
1233e1db26aSLionel Sambuc 
1243e1db26aSLionel Sambuc 	if (el->el_map.current != el->el_map.emacs) {
1253e1db26aSLionel Sambuc 		cv_undo(el);
1263e1db26aSLionel Sambuc 		cv_yank(el, el->el_line.cursor, num);
1273e1db26aSLionel Sambuc 	}
1283e1db26aSLionel Sambuc 
1293e1db26aSLionel Sambuc 	if (num > 0) {
1303e1db26aSLionel Sambuc 		Char *cp;
1313e1db26aSLionel Sambuc 
1323e1db26aSLionel Sambuc 		for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
1333e1db26aSLionel Sambuc 			*cp = cp[num];
1343e1db26aSLionel Sambuc 
1353e1db26aSLionel Sambuc 		el->el_line.lastchar -= num;
1363e1db26aSLionel Sambuc 	}
1373e1db26aSLionel Sambuc }
1383e1db26aSLionel Sambuc 
1393e1db26aSLionel Sambuc 
1403e1db26aSLionel Sambuc /* c_delafter1():
1413e1db26aSLionel Sambuc  *	Delete the character after the cursor, do not yank
1423e1db26aSLionel Sambuc  */
1433e1db26aSLionel Sambuc protected void
c_delafter1(EditLine * el)1443e1db26aSLionel Sambuc c_delafter1(EditLine *el)
1453e1db26aSLionel Sambuc {
1463e1db26aSLionel Sambuc 	Char *cp;
1473e1db26aSLionel Sambuc 
1483e1db26aSLionel Sambuc 	for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
1493e1db26aSLionel Sambuc 		*cp = cp[1];
1503e1db26aSLionel Sambuc 
1513e1db26aSLionel Sambuc 	el->el_line.lastchar--;
1523e1db26aSLionel Sambuc }
1533e1db26aSLionel Sambuc 
1543e1db26aSLionel Sambuc 
1553e1db26aSLionel Sambuc /* c_delbefore():
1563e1db26aSLionel Sambuc  *	Delete num characters before the cursor
1573e1db26aSLionel Sambuc  */
1583e1db26aSLionel Sambuc protected void
c_delbefore(EditLine * el,int num)1593e1db26aSLionel Sambuc c_delbefore(EditLine *el, int num)
1603e1db26aSLionel Sambuc {
1613e1db26aSLionel Sambuc 
1623e1db26aSLionel Sambuc 	if (el->el_line.cursor - num < el->el_line.buffer)
1633e1db26aSLionel Sambuc 		num = (int)(el->el_line.cursor - el->el_line.buffer);
1643e1db26aSLionel Sambuc 
1653e1db26aSLionel Sambuc 	if (el->el_map.current != el->el_map.emacs) {
1663e1db26aSLionel Sambuc 		cv_undo(el);
1673e1db26aSLionel Sambuc 		cv_yank(el, el->el_line.cursor - num, num);
1683e1db26aSLionel Sambuc 	}
1693e1db26aSLionel Sambuc 
1703e1db26aSLionel Sambuc 	if (num > 0) {
1713e1db26aSLionel Sambuc 		Char *cp;
1723e1db26aSLionel Sambuc 
1733e1db26aSLionel Sambuc 		for (cp = el->el_line.cursor - num;
1743e1db26aSLionel Sambuc 		    cp <= el->el_line.lastchar;
1753e1db26aSLionel Sambuc 		    cp++)
1763e1db26aSLionel Sambuc 			*cp = cp[num];
1773e1db26aSLionel Sambuc 
1783e1db26aSLionel Sambuc 		el->el_line.lastchar -= num;
1793e1db26aSLionel Sambuc 	}
1803e1db26aSLionel Sambuc }
1813e1db26aSLionel Sambuc 
1823e1db26aSLionel Sambuc 
1833e1db26aSLionel Sambuc /* c_delbefore1():
1843e1db26aSLionel Sambuc  *	Delete the character before the cursor, do not yank
1853e1db26aSLionel Sambuc  */
1863e1db26aSLionel Sambuc protected void
c_delbefore1(EditLine * el)1873e1db26aSLionel Sambuc c_delbefore1(EditLine *el)
1883e1db26aSLionel Sambuc {
1893e1db26aSLionel Sambuc 	Char *cp;
1903e1db26aSLionel Sambuc 
1913e1db26aSLionel Sambuc 	for (cp = el->el_line.cursor - 1; cp <= el->el_line.lastchar; cp++)
1923e1db26aSLionel Sambuc 		*cp = cp[1];
1933e1db26aSLionel Sambuc 
1943e1db26aSLionel Sambuc 	el->el_line.lastchar--;
1953e1db26aSLionel Sambuc }
1963e1db26aSLionel Sambuc 
1973e1db26aSLionel Sambuc 
1983e1db26aSLionel Sambuc /* ce__isword():
1993e1db26aSLionel Sambuc  *	Return if p is part of a word according to emacs
2003e1db26aSLionel Sambuc  */
2013e1db26aSLionel Sambuc protected int
ce__isword(Int p)2023e1db26aSLionel Sambuc ce__isword(Int p)
2033e1db26aSLionel Sambuc {
2043e1db26aSLionel Sambuc 	return Isalnum(p) || Strchr(STR("*?_-.[]~="), p) != NULL;
2053e1db26aSLionel Sambuc }
2063e1db26aSLionel Sambuc 
2073e1db26aSLionel Sambuc 
2083e1db26aSLionel Sambuc /* cv__isword():
2093e1db26aSLionel Sambuc  *	Return if p is part of a word according to vi
2103e1db26aSLionel Sambuc  */
2113e1db26aSLionel Sambuc protected int
cv__isword(Int p)2123e1db26aSLionel Sambuc cv__isword(Int p)
2133e1db26aSLionel Sambuc {
2143e1db26aSLionel Sambuc 	if (Isalnum(p) || p == '_')
2153e1db26aSLionel Sambuc 		return 1;
2163e1db26aSLionel Sambuc 	if (Isgraph(p))
2173e1db26aSLionel Sambuc 		return 2;
2183e1db26aSLionel Sambuc 	return 0;
2193e1db26aSLionel Sambuc }
2203e1db26aSLionel Sambuc 
2213e1db26aSLionel Sambuc 
2223e1db26aSLionel Sambuc /* cv__isWord():
2233e1db26aSLionel Sambuc  *	Return if p is part of a big word according to vi
2243e1db26aSLionel Sambuc  */
2253e1db26aSLionel Sambuc protected int
cv__isWord(Int p)2263e1db26aSLionel Sambuc cv__isWord(Int p)
2273e1db26aSLionel Sambuc {
2283e1db26aSLionel Sambuc 	return !Isspace(p);
2293e1db26aSLionel Sambuc }
2303e1db26aSLionel Sambuc 
2313e1db26aSLionel Sambuc 
2323e1db26aSLionel Sambuc /* c__prev_word():
2333e1db26aSLionel Sambuc  *	Find the previous word
2343e1db26aSLionel Sambuc  */
2353e1db26aSLionel Sambuc protected Char *
c__prev_word(Char * p,Char * low,int n,int (* wtest)(Int))2363e1db26aSLionel Sambuc c__prev_word(Char *p, Char *low, int n, int (*wtest)(Int))
2373e1db26aSLionel Sambuc {
2383e1db26aSLionel Sambuc 	p--;
2393e1db26aSLionel Sambuc 
2403e1db26aSLionel Sambuc 	while (n--) {
2413e1db26aSLionel Sambuc 		while ((p >= low) && !(*wtest)(*p))
2423e1db26aSLionel Sambuc 			p--;
2433e1db26aSLionel Sambuc 		while ((p >= low) && (*wtest)(*p))
2443e1db26aSLionel Sambuc 			p--;
2453e1db26aSLionel Sambuc 	}
2463e1db26aSLionel Sambuc 
2473e1db26aSLionel Sambuc 	/* cp now points to one character before the word */
2483e1db26aSLionel Sambuc 	p++;
2493e1db26aSLionel Sambuc 	if (p < low)
2503e1db26aSLionel Sambuc 		p = low;
2513e1db26aSLionel Sambuc 	/* cp now points where we want it */
2523e1db26aSLionel Sambuc 	return p;
2533e1db26aSLionel Sambuc }
2543e1db26aSLionel Sambuc 
2553e1db26aSLionel Sambuc 
2563e1db26aSLionel Sambuc /* c__next_word():
2573e1db26aSLionel Sambuc  *	Find the next word
2583e1db26aSLionel Sambuc  */
2593e1db26aSLionel Sambuc protected Char *
c__next_word(Char * p,Char * high,int n,int (* wtest)(Int))2603e1db26aSLionel Sambuc c__next_word(Char *p, Char *high, int n, int (*wtest)(Int))
2613e1db26aSLionel Sambuc {
2623e1db26aSLionel Sambuc 	while (n--) {
2633e1db26aSLionel Sambuc 		while ((p < high) && !(*wtest)(*p))
2643e1db26aSLionel Sambuc 			p++;
2653e1db26aSLionel Sambuc 		while ((p < high) && (*wtest)(*p))
2663e1db26aSLionel Sambuc 			p++;
2673e1db26aSLionel Sambuc 	}
2683e1db26aSLionel Sambuc 	if (p > high)
2693e1db26aSLionel Sambuc 		p = high;
2703e1db26aSLionel Sambuc 	/* p now points where we want it */
2713e1db26aSLionel Sambuc 	return p;
2723e1db26aSLionel Sambuc }
2733e1db26aSLionel Sambuc 
2743e1db26aSLionel Sambuc /* cv_next_word():
2753e1db26aSLionel Sambuc  *	Find the next word vi style
2763e1db26aSLionel Sambuc  */
2773e1db26aSLionel Sambuc protected Char *
cv_next_word(EditLine * el,Char * p,Char * high,int n,int (* wtest)(Int))2783e1db26aSLionel Sambuc cv_next_word(EditLine *el, Char *p, Char *high, int n, int (*wtest)(Int))
2793e1db26aSLionel Sambuc {
2803e1db26aSLionel Sambuc 	int test;
2813e1db26aSLionel Sambuc 
2823e1db26aSLionel Sambuc 	while (n--) {
2833e1db26aSLionel Sambuc 		test = (*wtest)(*p);
2843e1db26aSLionel Sambuc 		while ((p < high) && (*wtest)(*p) == test)
2853e1db26aSLionel Sambuc 			p++;
2863e1db26aSLionel Sambuc 		/*
2873e1db26aSLionel Sambuc 		 * vi historically deletes with cw only the word preserving the
2883e1db26aSLionel Sambuc 		 * trailing whitespace! This is not what 'w' does..
2893e1db26aSLionel Sambuc 		 */
2903e1db26aSLionel Sambuc 		if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT))
2913e1db26aSLionel Sambuc 			while ((p < high) && Isspace(*p))
2923e1db26aSLionel Sambuc 				p++;
2933e1db26aSLionel Sambuc 	}
2943e1db26aSLionel Sambuc 
2953e1db26aSLionel Sambuc 	/* p now points where we want it */
2963e1db26aSLionel Sambuc 	if (p > high)
2973e1db26aSLionel Sambuc 		return high;
2983e1db26aSLionel Sambuc 	else
2993e1db26aSLionel Sambuc 		return p;
3003e1db26aSLionel Sambuc }
3013e1db26aSLionel Sambuc 
3023e1db26aSLionel Sambuc 
3033e1db26aSLionel Sambuc /* cv_prev_word():
3043e1db26aSLionel Sambuc  *	Find the previous word vi style
3053e1db26aSLionel Sambuc  */
3063e1db26aSLionel Sambuc protected Char *
cv_prev_word(Char * p,Char * low,int n,int (* wtest)(Int))3073e1db26aSLionel Sambuc cv_prev_word(Char *p, Char *low, int n, int (*wtest)(Int))
3083e1db26aSLionel Sambuc {
3093e1db26aSLionel Sambuc 	int test;
3103e1db26aSLionel Sambuc 
3113e1db26aSLionel Sambuc 	p--;
3123e1db26aSLionel Sambuc 	while (n--) {
3133e1db26aSLionel Sambuc 		while ((p > low) && Isspace(*p))
3143e1db26aSLionel Sambuc 			p--;
3153e1db26aSLionel Sambuc 		test = (*wtest)(*p);
3163e1db26aSLionel Sambuc 		while ((p >= low) && (*wtest)(*p) == test)
3173e1db26aSLionel Sambuc 			p--;
3183e1db26aSLionel Sambuc 	}
3193e1db26aSLionel Sambuc 	p++;
3203e1db26aSLionel Sambuc 
3213e1db26aSLionel Sambuc 	/* p now points where we want it */
3223e1db26aSLionel Sambuc 	if (p < low)
3233e1db26aSLionel Sambuc 		return low;
3243e1db26aSLionel Sambuc 	else
3253e1db26aSLionel Sambuc 		return p;
3263e1db26aSLionel Sambuc }
3273e1db26aSLionel Sambuc 
3283e1db26aSLionel Sambuc 
3293e1db26aSLionel Sambuc /* cv_delfini():
3303e1db26aSLionel Sambuc  *	Finish vi delete action
3313e1db26aSLionel Sambuc  */
3323e1db26aSLionel Sambuc protected void
cv_delfini(EditLine * el)3333e1db26aSLionel Sambuc cv_delfini(EditLine *el)
3343e1db26aSLionel Sambuc {
3353e1db26aSLionel Sambuc 	int size;
3363e1db26aSLionel Sambuc 	int action = el->el_chared.c_vcmd.action;
3373e1db26aSLionel Sambuc 
3383e1db26aSLionel Sambuc 	if (action & INSERT)
3393e1db26aSLionel Sambuc 		el->el_map.current = el->el_map.key;
3403e1db26aSLionel Sambuc 
3413e1db26aSLionel Sambuc 	if (el->el_chared.c_vcmd.pos == 0)
3423e1db26aSLionel Sambuc 		/* sanity */
3433e1db26aSLionel Sambuc 		return;
3443e1db26aSLionel Sambuc 
3453e1db26aSLionel Sambuc 	size = (int)(el->el_line.cursor - el->el_chared.c_vcmd.pos);
3463e1db26aSLionel Sambuc 	if (size == 0)
3473e1db26aSLionel Sambuc 		size = 1;
3483e1db26aSLionel Sambuc 	el->el_line.cursor = el->el_chared.c_vcmd.pos;
3493e1db26aSLionel Sambuc 	if (action & YANK) {
3503e1db26aSLionel Sambuc 		if (size > 0)
3513e1db26aSLionel Sambuc 			cv_yank(el, el->el_line.cursor, size);
3523e1db26aSLionel Sambuc 		else
3533e1db26aSLionel Sambuc 			cv_yank(el, el->el_line.cursor + size, -size);
3543e1db26aSLionel Sambuc 	} else {
3553e1db26aSLionel Sambuc 		if (size > 0) {
3563e1db26aSLionel Sambuc 			c_delafter(el, size);
3573e1db26aSLionel Sambuc 			re_refresh_cursor(el);
3583e1db26aSLionel Sambuc 		} else  {
3593e1db26aSLionel Sambuc 			c_delbefore(el, -size);
3603e1db26aSLionel Sambuc 			el->el_line.cursor += size;
3613e1db26aSLionel Sambuc 		}
3623e1db26aSLionel Sambuc 	}
3633e1db26aSLionel Sambuc 	el->el_chared.c_vcmd.action = NOP;
3643e1db26aSLionel Sambuc }
3653e1db26aSLionel Sambuc 
3663e1db26aSLionel Sambuc 
3673e1db26aSLionel Sambuc /* cv__endword():
3683e1db26aSLionel Sambuc  *	Go to the end of this word according to vi
3693e1db26aSLionel Sambuc  */
3703e1db26aSLionel Sambuc protected Char *
cv__endword(Char * p,Char * high,int n,int (* wtest)(Int))3713e1db26aSLionel Sambuc cv__endword(Char *p, Char *high, int n, int (*wtest)(Int))
3723e1db26aSLionel Sambuc {
3733e1db26aSLionel Sambuc 	int test;
3743e1db26aSLionel Sambuc 
3753e1db26aSLionel Sambuc 	p++;
3763e1db26aSLionel Sambuc 
3773e1db26aSLionel Sambuc 	while (n--) {
3783e1db26aSLionel Sambuc 		while ((p < high) && Isspace(*p))
3793e1db26aSLionel Sambuc 			p++;
3803e1db26aSLionel Sambuc 
3813e1db26aSLionel Sambuc 		test = (*wtest)(*p);
3823e1db26aSLionel Sambuc 		while ((p < high) && (*wtest)(*p) == test)
3833e1db26aSLionel Sambuc 			p++;
3843e1db26aSLionel Sambuc 	}
3853e1db26aSLionel Sambuc 	p--;
3863e1db26aSLionel Sambuc 	return p;
3873e1db26aSLionel Sambuc }
3883e1db26aSLionel Sambuc 
3893e1db26aSLionel Sambuc /* ch_init():
3903e1db26aSLionel Sambuc  *	Initialize the character editor
3913e1db26aSLionel Sambuc  */
3923e1db26aSLionel Sambuc protected int
ch_init(EditLine * el)3933e1db26aSLionel Sambuc ch_init(EditLine *el)
3943e1db26aSLionel Sambuc {
3953e1db26aSLionel Sambuc 	c_macro_t *ma = &el->el_chared.c_macro;
3963e1db26aSLionel Sambuc 
3973e1db26aSLionel Sambuc 	el->el_line.buffer		= el_malloc(EL_BUFSIZ *
3983e1db26aSLionel Sambuc 	    sizeof(*el->el_line.buffer));
3993e1db26aSLionel Sambuc 	if (el->el_line.buffer == NULL)
4003e1db26aSLionel Sambuc 		return -1;
4013e1db26aSLionel Sambuc 
4023e1db26aSLionel Sambuc 	(void) memset(el->el_line.buffer, 0, EL_BUFSIZ *
4033e1db26aSLionel Sambuc 	    sizeof(*el->el_line.buffer));
4043e1db26aSLionel Sambuc 	el->el_line.cursor		= el->el_line.buffer;
4053e1db26aSLionel Sambuc 	el->el_line.lastchar		= el->el_line.buffer;
4063e1db26aSLionel Sambuc 	el->el_line.limit		= &el->el_line.buffer[EL_BUFSIZ - EL_LEAVE];
4073e1db26aSLionel Sambuc 
4083e1db26aSLionel Sambuc 	el->el_chared.c_undo.buf	= el_malloc(EL_BUFSIZ *
4093e1db26aSLionel Sambuc 	    sizeof(*el->el_chared.c_undo.buf));
4103e1db26aSLionel Sambuc 	if (el->el_chared.c_undo.buf == NULL)
4113e1db26aSLionel Sambuc 		return -1;
4123e1db26aSLionel Sambuc 	(void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ *
4133e1db26aSLionel Sambuc 	    sizeof(*el->el_chared.c_undo.buf));
4143e1db26aSLionel Sambuc 	el->el_chared.c_undo.len	= -1;
4153e1db26aSLionel Sambuc 	el->el_chared.c_undo.cursor	= 0;
4163e1db26aSLionel Sambuc 	el->el_chared.c_redo.buf	= el_malloc(EL_BUFSIZ *
4173e1db26aSLionel Sambuc 	    sizeof(*el->el_chared.c_redo.buf));
4183e1db26aSLionel Sambuc 	if (el->el_chared.c_redo.buf == NULL)
4193e1db26aSLionel Sambuc 		return -1;
4203e1db26aSLionel Sambuc 	el->el_chared.c_redo.pos	= el->el_chared.c_redo.buf;
4213e1db26aSLionel Sambuc 	el->el_chared.c_redo.lim	= el->el_chared.c_redo.buf + EL_BUFSIZ;
4223e1db26aSLionel Sambuc 	el->el_chared.c_redo.cmd	= ED_UNASSIGNED;
4233e1db26aSLionel Sambuc 
4243e1db26aSLionel Sambuc 	el->el_chared.c_vcmd.action	= NOP;
4253e1db26aSLionel Sambuc 	el->el_chared.c_vcmd.pos	= el->el_line.buffer;
4263e1db26aSLionel Sambuc 
4273e1db26aSLionel Sambuc 	el->el_chared.c_kill.buf	= el_malloc(EL_BUFSIZ *
4283e1db26aSLionel Sambuc 	    sizeof(*el->el_chared.c_kill.buf));
4293e1db26aSLionel Sambuc 	if (el->el_chared.c_kill.buf == NULL)
4303e1db26aSLionel Sambuc 		return -1;
4313e1db26aSLionel Sambuc 	(void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ *
4323e1db26aSLionel Sambuc 	    sizeof(*el->el_chared.c_kill.buf));
4333e1db26aSLionel Sambuc 	el->el_chared.c_kill.mark	= el->el_line.buffer;
4343e1db26aSLionel Sambuc 	el->el_chared.c_kill.last	= el->el_chared.c_kill.buf;
4353e1db26aSLionel Sambuc 	el->el_chared.c_resizefun	= NULL;
4363e1db26aSLionel Sambuc 	el->el_chared.c_resizearg	= NULL;
437*0a6a1f1dSLionel Sambuc 	el->el_chared.c_aliasfun	= NULL;
438*0a6a1f1dSLionel Sambuc 	el->el_chared.c_aliasarg	= NULL;
4393e1db26aSLionel Sambuc 
4403e1db26aSLionel Sambuc 	el->el_map.current		= el->el_map.key;
4413e1db26aSLionel Sambuc 
4423e1db26aSLionel Sambuc 	el->el_state.inputmode		= MODE_INSERT; /* XXX: save a default */
4433e1db26aSLionel Sambuc 	el->el_state.doingarg		= 0;
4443e1db26aSLionel Sambuc 	el->el_state.metanext		= 0;
4453e1db26aSLionel Sambuc 	el->el_state.argument		= 1;
4463e1db26aSLionel Sambuc 	el->el_state.lastcmd		= ED_UNASSIGNED;
4473e1db26aSLionel Sambuc 
4483e1db26aSLionel Sambuc 	ma->level	= -1;
4493e1db26aSLionel Sambuc 	ma->offset	= 0;
4503e1db26aSLionel Sambuc 	ma->macro	= el_malloc(EL_MAXMACRO * sizeof(*ma->macro));
4513e1db26aSLionel Sambuc 	if (ma->macro == NULL)
4523e1db26aSLionel Sambuc 		return -1;
4533e1db26aSLionel Sambuc 	return 0;
4543e1db26aSLionel Sambuc }
4553e1db26aSLionel Sambuc 
4563e1db26aSLionel Sambuc /* ch_reset():
4573e1db26aSLionel Sambuc  *	Reset the character editor
4583e1db26aSLionel Sambuc  */
4593e1db26aSLionel Sambuc protected void
ch_reset(EditLine * el,int mclear)4603e1db26aSLionel Sambuc ch_reset(EditLine *el, int mclear)
4613e1db26aSLionel Sambuc {
4623e1db26aSLionel Sambuc 	el->el_line.cursor		= el->el_line.buffer;
4633e1db26aSLionel Sambuc 	el->el_line.lastchar		= el->el_line.buffer;
4643e1db26aSLionel Sambuc 
4653e1db26aSLionel Sambuc 	el->el_chared.c_undo.len	= -1;
4663e1db26aSLionel Sambuc 	el->el_chared.c_undo.cursor	= 0;
4673e1db26aSLionel Sambuc 
4683e1db26aSLionel Sambuc 	el->el_chared.c_vcmd.action	= NOP;
4693e1db26aSLionel Sambuc 	el->el_chared.c_vcmd.pos	= el->el_line.buffer;
4703e1db26aSLionel Sambuc 
4713e1db26aSLionel Sambuc 	el->el_chared.c_kill.mark	= el->el_line.buffer;
4723e1db26aSLionel Sambuc 
4733e1db26aSLionel Sambuc 	el->el_map.current		= el->el_map.key;
4743e1db26aSLionel Sambuc 
4753e1db26aSLionel Sambuc 	el->el_state.inputmode		= MODE_INSERT; /* XXX: save a default */
4763e1db26aSLionel Sambuc 	el->el_state.doingarg		= 0;
4773e1db26aSLionel Sambuc 	el->el_state.metanext		= 0;
4783e1db26aSLionel Sambuc 	el->el_state.argument		= 1;
4793e1db26aSLionel Sambuc 	el->el_state.lastcmd		= ED_UNASSIGNED;
4803e1db26aSLionel Sambuc 
4813e1db26aSLionel Sambuc 	el->el_history.eventno		= 0;
4823e1db26aSLionel Sambuc 
4833e1db26aSLionel Sambuc 	if (mclear)
4843e1db26aSLionel Sambuc 		ch__clearmacro(el);
4853e1db26aSLionel Sambuc }
4863e1db26aSLionel Sambuc 
4873e1db26aSLionel Sambuc private void
ch__clearmacro(EditLine * el)4883e1db26aSLionel Sambuc ch__clearmacro(EditLine *el)
4893e1db26aSLionel Sambuc {
4903e1db26aSLionel Sambuc 	c_macro_t *ma = &el->el_chared.c_macro;
4913e1db26aSLionel Sambuc 	while (ma->level >= 0)
4923e1db26aSLionel Sambuc 		el_free(ma->macro[ma->level--]);
4933e1db26aSLionel Sambuc }
4943e1db26aSLionel Sambuc 
4953e1db26aSLionel Sambuc /* ch_enlargebufs():
4963e1db26aSLionel Sambuc  *	Enlarge line buffer to be able to hold twice as much characters.
4973e1db26aSLionel Sambuc  *	Returns 1 if successful, 0 if not.
4983e1db26aSLionel Sambuc  */
4993e1db26aSLionel Sambuc protected int
ch_enlargebufs(EditLine * el,size_t addlen)5003e1db26aSLionel Sambuc ch_enlargebufs(EditLine *el, size_t addlen)
5013e1db26aSLionel Sambuc {
5023e1db26aSLionel Sambuc 	size_t sz, newsz;
5033e1db26aSLionel Sambuc 	Char *newbuffer, *oldbuf, *oldkbuf;
5043e1db26aSLionel Sambuc 
5053e1db26aSLionel Sambuc 	sz = (size_t)(el->el_line.limit - el->el_line.buffer + EL_LEAVE);
5063e1db26aSLionel Sambuc 	newsz = sz * 2;
5073e1db26aSLionel Sambuc 	/*
5083e1db26aSLionel Sambuc 	 * If newly required length is longer than current buffer, we need
5093e1db26aSLionel Sambuc 	 * to make the buffer big enough to hold both old and new stuff.
5103e1db26aSLionel Sambuc 	 */
5113e1db26aSLionel Sambuc 	if (addlen > sz) {
5123e1db26aSLionel Sambuc 		while(newsz - sz < addlen)
5133e1db26aSLionel Sambuc 			newsz *= 2;
5143e1db26aSLionel Sambuc 	}
5153e1db26aSLionel Sambuc 
5163e1db26aSLionel Sambuc 	/*
5173e1db26aSLionel Sambuc 	 * Reallocate line buffer.
5183e1db26aSLionel Sambuc 	 */
5193e1db26aSLionel Sambuc 	newbuffer = el_realloc(el->el_line.buffer, newsz * sizeof(*newbuffer));
5203e1db26aSLionel Sambuc 	if (!newbuffer)
5213e1db26aSLionel Sambuc 		return 0;
5223e1db26aSLionel Sambuc 
5233e1db26aSLionel Sambuc 	/* zero the newly added memory, leave old data in */
5243e1db26aSLionel Sambuc 	(void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
5253e1db26aSLionel Sambuc 
5263e1db26aSLionel Sambuc 	oldbuf = el->el_line.buffer;
5273e1db26aSLionel Sambuc 
5283e1db26aSLionel Sambuc 	el->el_line.buffer = newbuffer;
5293e1db26aSLionel Sambuc 	el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf);
5303e1db26aSLionel Sambuc 	el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf);
5313e1db26aSLionel Sambuc 	/* don't set new size until all buffers are enlarged */
5323e1db26aSLionel Sambuc 	el->el_line.limit  = &newbuffer[sz - EL_LEAVE];
5333e1db26aSLionel Sambuc 
5343e1db26aSLionel Sambuc 	/*
5353e1db26aSLionel Sambuc 	 * Reallocate kill buffer.
5363e1db26aSLionel Sambuc 	 */
5373e1db26aSLionel Sambuc 	newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz *
5383e1db26aSLionel Sambuc 	    sizeof(*newbuffer));
5393e1db26aSLionel Sambuc 	if (!newbuffer)
5403e1db26aSLionel Sambuc 		return 0;
5413e1db26aSLionel Sambuc 
5423e1db26aSLionel Sambuc 	/* zero the newly added memory, leave old data in */
5433e1db26aSLionel Sambuc 	(void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
5443e1db26aSLionel Sambuc 
5453e1db26aSLionel Sambuc 	oldkbuf = el->el_chared.c_kill.buf;
5463e1db26aSLionel Sambuc 
5473e1db26aSLionel Sambuc 	el->el_chared.c_kill.buf = newbuffer;
5483e1db26aSLionel Sambuc 	el->el_chared.c_kill.last = newbuffer +
5493e1db26aSLionel Sambuc 					(el->el_chared.c_kill.last - oldkbuf);
5503e1db26aSLionel Sambuc 	el->el_chared.c_kill.mark = el->el_line.buffer +
5513e1db26aSLionel Sambuc 					(el->el_chared.c_kill.mark - oldbuf);
5523e1db26aSLionel Sambuc 
5533e1db26aSLionel Sambuc 	/*
5543e1db26aSLionel Sambuc 	 * Reallocate undo buffer.
5553e1db26aSLionel Sambuc 	 */
5563e1db26aSLionel Sambuc 	newbuffer = el_realloc(el->el_chared.c_undo.buf,
5573e1db26aSLionel Sambuc 	    newsz * sizeof(*newbuffer));
5583e1db26aSLionel Sambuc 	if (!newbuffer)
5593e1db26aSLionel Sambuc 		return 0;
5603e1db26aSLionel Sambuc 
5613e1db26aSLionel Sambuc 	/* zero the newly added memory, leave old data in */
5623e1db26aSLionel Sambuc 	(void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
5633e1db26aSLionel Sambuc 	el->el_chared.c_undo.buf = newbuffer;
5643e1db26aSLionel Sambuc 
5653e1db26aSLionel Sambuc 	newbuffer = el_realloc(el->el_chared.c_redo.buf,
5663e1db26aSLionel Sambuc 	    newsz * sizeof(*newbuffer));
5673e1db26aSLionel Sambuc 	if (!newbuffer)
5683e1db26aSLionel Sambuc 		return 0;
5693e1db26aSLionel Sambuc 	el->el_chared.c_redo.pos = newbuffer +
5703e1db26aSLionel Sambuc 			(el->el_chared.c_redo.pos - el->el_chared.c_redo.buf);
5713e1db26aSLionel Sambuc 	el->el_chared.c_redo.lim = newbuffer +
5723e1db26aSLionel Sambuc 			(el->el_chared.c_redo.lim - el->el_chared.c_redo.buf);
5733e1db26aSLionel Sambuc 	el->el_chared.c_redo.buf = newbuffer;
5743e1db26aSLionel Sambuc 
5753e1db26aSLionel Sambuc 	if (!hist_enlargebuf(el, sz, newsz))
5763e1db26aSLionel Sambuc 		return 0;
5773e1db26aSLionel Sambuc 
5783e1db26aSLionel Sambuc 	/* Safe to set enlarged buffer size */
5793e1db26aSLionel Sambuc 	el->el_line.limit  = &el->el_line.buffer[newsz - EL_LEAVE];
5803e1db26aSLionel Sambuc 	if (el->el_chared.c_resizefun)
5813e1db26aSLionel Sambuc 		(*el->el_chared.c_resizefun)(el, el->el_chared.c_resizearg);
5823e1db26aSLionel Sambuc 	return 1;
5833e1db26aSLionel Sambuc }
5843e1db26aSLionel Sambuc 
5853e1db26aSLionel Sambuc /* ch_end():
5863e1db26aSLionel Sambuc  *	Free the data structures used by the editor
5873e1db26aSLionel Sambuc  */
5883e1db26aSLionel Sambuc protected void
ch_end(EditLine * el)5893e1db26aSLionel Sambuc ch_end(EditLine *el)
5903e1db26aSLionel Sambuc {
5913e1db26aSLionel Sambuc 	el_free(el->el_line.buffer);
5923e1db26aSLionel Sambuc 	el->el_line.buffer = NULL;
5933e1db26aSLionel Sambuc 	el->el_line.limit = NULL;
5943e1db26aSLionel Sambuc 	el_free(el->el_chared.c_undo.buf);
5953e1db26aSLionel Sambuc 	el->el_chared.c_undo.buf = NULL;
5963e1db26aSLionel Sambuc 	el_free(el->el_chared.c_redo.buf);
5973e1db26aSLionel Sambuc 	el->el_chared.c_redo.buf = NULL;
5983e1db26aSLionel Sambuc 	el->el_chared.c_redo.pos = NULL;
5993e1db26aSLionel Sambuc 	el->el_chared.c_redo.lim = NULL;
6003e1db26aSLionel Sambuc 	el->el_chared.c_redo.cmd = ED_UNASSIGNED;
6013e1db26aSLionel Sambuc 	el_free(el->el_chared.c_kill.buf);
6023e1db26aSLionel Sambuc 	el->el_chared.c_kill.buf = NULL;
6033e1db26aSLionel Sambuc 	ch_reset(el, 1);
6043e1db26aSLionel Sambuc 	el_free(el->el_chared.c_macro.macro);
6053e1db26aSLionel Sambuc 	el->el_chared.c_macro.macro = NULL;
6063e1db26aSLionel Sambuc }
6073e1db26aSLionel Sambuc 
6083e1db26aSLionel Sambuc 
6093e1db26aSLionel Sambuc /* el_insertstr():
6103e1db26aSLionel Sambuc  *	Insert string at cursorI
6113e1db26aSLionel Sambuc  */
6123e1db26aSLionel Sambuc public int
FUN(el,insertstr)6133e1db26aSLionel Sambuc FUN(el,insertstr)(EditLine *el, const Char *s)
6143e1db26aSLionel Sambuc {
6153e1db26aSLionel Sambuc 	size_t len;
6163e1db26aSLionel Sambuc 
6173e1db26aSLionel Sambuc 	if (s == NULL || (len = Strlen(s)) == 0)
6183e1db26aSLionel Sambuc 		return -1;
6193e1db26aSLionel Sambuc 	if (el->el_line.lastchar + len >= el->el_line.limit) {
6203e1db26aSLionel Sambuc 		if (!ch_enlargebufs(el, len))
6213e1db26aSLionel Sambuc 			return -1;
6223e1db26aSLionel Sambuc 	}
6233e1db26aSLionel Sambuc 
6243e1db26aSLionel Sambuc 	c_insert(el, (int)len);
6253e1db26aSLionel Sambuc 	while (*s)
6263e1db26aSLionel Sambuc 		*el->el_line.cursor++ = *s++;
6273e1db26aSLionel Sambuc 	return 0;
6283e1db26aSLionel Sambuc }
6293e1db26aSLionel Sambuc 
6303e1db26aSLionel Sambuc 
6313e1db26aSLionel Sambuc /* el_deletestr():
6323e1db26aSLionel Sambuc  *	Delete num characters before the cursor
6333e1db26aSLionel Sambuc  */
6343e1db26aSLionel Sambuc public void
el_deletestr(EditLine * el,int n)6353e1db26aSLionel Sambuc el_deletestr(EditLine *el, int n)
6363e1db26aSLionel Sambuc {
6373e1db26aSLionel Sambuc 	if (n <= 0)
6383e1db26aSLionel Sambuc 		return;
6393e1db26aSLionel Sambuc 
6403e1db26aSLionel Sambuc 	if (el->el_line.cursor < &el->el_line.buffer[n])
6413e1db26aSLionel Sambuc 		return;
6423e1db26aSLionel Sambuc 
6433e1db26aSLionel Sambuc 	c_delbefore(el, n);		/* delete before dot */
6443e1db26aSLionel Sambuc 	el->el_line.cursor -= n;
6453e1db26aSLionel Sambuc 	if (el->el_line.cursor < el->el_line.buffer)
6463e1db26aSLionel Sambuc 		el->el_line.cursor = el->el_line.buffer;
6473e1db26aSLionel Sambuc }
6483e1db26aSLionel Sambuc 
64984d9c625SLionel Sambuc /* el_cursor():
65084d9c625SLionel Sambuc  *	Move the cursor to the left or the right of the current position
65184d9c625SLionel Sambuc  */
65284d9c625SLionel Sambuc public int
el_cursor(EditLine * el,int n)65384d9c625SLionel Sambuc el_cursor(EditLine *el, int n)
65484d9c625SLionel Sambuc {
65584d9c625SLionel Sambuc 	if (n == 0)
65684d9c625SLionel Sambuc 		goto out;
65784d9c625SLionel Sambuc 
65884d9c625SLionel Sambuc 	el->el_line.cursor += n;
65984d9c625SLionel Sambuc 
66084d9c625SLionel Sambuc 	if (el->el_line.cursor < el->el_line.buffer)
66184d9c625SLionel Sambuc 		el->el_line.cursor = el->el_line.buffer;
66284d9c625SLionel Sambuc 	if (el->el_line.cursor > el->el_line.lastchar)
66384d9c625SLionel Sambuc 		el->el_line.cursor = el->el_line.lastchar;
66484d9c625SLionel Sambuc out:
66584d9c625SLionel Sambuc 	return (int)(el->el_line.cursor - el->el_line.buffer);
66684d9c625SLionel Sambuc }
66784d9c625SLionel Sambuc 
6683e1db26aSLionel Sambuc /* c_gets():
6693e1db26aSLionel Sambuc  *	Get a string
6703e1db26aSLionel Sambuc  */
6713e1db26aSLionel Sambuc protected int
c_gets(EditLine * el,Char * buf,const Char * prompt)6723e1db26aSLionel Sambuc c_gets(EditLine *el, Char *buf, const Char *prompt)
6733e1db26aSLionel Sambuc {
6743e1db26aSLionel Sambuc 	Char ch;
6753e1db26aSLionel Sambuc 	ssize_t len;
6763e1db26aSLionel Sambuc 	Char *cp = el->el_line.buffer;
6773e1db26aSLionel Sambuc 
6783e1db26aSLionel Sambuc 	if (prompt) {
6793e1db26aSLionel Sambuc 		len = (ssize_t)Strlen(prompt);
6803e1db26aSLionel Sambuc 		(void)memcpy(cp, prompt, (size_t)len * sizeof(*cp));
6813e1db26aSLionel Sambuc 		cp += len;
6823e1db26aSLionel Sambuc 	}
6833e1db26aSLionel Sambuc 	len = 0;
6843e1db26aSLionel Sambuc 
6853e1db26aSLionel Sambuc 	for (;;) {
6863e1db26aSLionel Sambuc 		el->el_line.cursor = cp;
6873e1db26aSLionel Sambuc 		*cp = ' ';
6883e1db26aSLionel Sambuc 		el->el_line.lastchar = cp + 1;
6893e1db26aSLionel Sambuc 		re_refresh(el);
6903e1db26aSLionel Sambuc 
6913e1db26aSLionel Sambuc 		if (FUN(el,getc)(el, &ch) != 1) {
6923e1db26aSLionel Sambuc 			ed_end_of_file(el, 0);
6933e1db26aSLionel Sambuc 			len = -1;
6943e1db26aSLionel Sambuc 			break;
6953e1db26aSLionel Sambuc 		}
6963e1db26aSLionel Sambuc 
6973e1db26aSLionel Sambuc 		switch (ch) {
6983e1db26aSLionel Sambuc 
6993e1db26aSLionel Sambuc 		case 0010:	/* Delete and backspace */
7003e1db26aSLionel Sambuc 		case 0177:
7013e1db26aSLionel Sambuc 			if (len == 0) {
7023e1db26aSLionel Sambuc 				len = -1;
7033e1db26aSLionel Sambuc 				break;
7043e1db26aSLionel Sambuc 			}
7053e1db26aSLionel Sambuc 			cp--;
7063e1db26aSLionel Sambuc 			continue;
7073e1db26aSLionel Sambuc 
7083e1db26aSLionel Sambuc 		case 0033:	/* ESC */
7093e1db26aSLionel Sambuc 		case '\r':	/* Newline */
7103e1db26aSLionel Sambuc 		case '\n':
7113e1db26aSLionel Sambuc 			buf[len] = ch;
7123e1db26aSLionel Sambuc 			break;
7133e1db26aSLionel Sambuc 
7143e1db26aSLionel Sambuc 		default:
7153e1db26aSLionel Sambuc 			if (len >= (ssize_t)(EL_BUFSIZ - 16))
7163e1db26aSLionel Sambuc 				terminal_beep(el);
7173e1db26aSLionel Sambuc 			else {
7183e1db26aSLionel Sambuc 				buf[len++] = ch;
7193e1db26aSLionel Sambuc 				*cp++ = ch;
7203e1db26aSLionel Sambuc 			}
7213e1db26aSLionel Sambuc 			continue;
7223e1db26aSLionel Sambuc 		}
7233e1db26aSLionel Sambuc 		break;
7243e1db26aSLionel Sambuc 	}
7253e1db26aSLionel Sambuc 
7263e1db26aSLionel Sambuc 	el->el_line.buffer[0] = '\0';
7273e1db26aSLionel Sambuc 	el->el_line.lastchar = el->el_line.buffer;
7283e1db26aSLionel Sambuc 	el->el_line.cursor = el->el_line.buffer;
7293e1db26aSLionel Sambuc 	return (int)len;
7303e1db26aSLionel Sambuc }
7313e1db26aSLionel Sambuc 
7323e1db26aSLionel Sambuc 
7333e1db26aSLionel Sambuc /* c_hpos():
7343e1db26aSLionel Sambuc  *	Return the current horizontal position of the cursor
7353e1db26aSLionel Sambuc  */
7363e1db26aSLionel Sambuc protected int
c_hpos(EditLine * el)7373e1db26aSLionel Sambuc c_hpos(EditLine *el)
7383e1db26aSLionel Sambuc {
7393e1db26aSLionel Sambuc 	Char *ptr;
7403e1db26aSLionel Sambuc 
7413e1db26aSLionel Sambuc 	/*
7423e1db26aSLionel Sambuc 	 * Find how many characters till the beginning of this line.
7433e1db26aSLionel Sambuc 	 */
7443e1db26aSLionel Sambuc 	if (el->el_line.cursor == el->el_line.buffer)
7453e1db26aSLionel Sambuc 		return 0;
7463e1db26aSLionel Sambuc 	else {
7473e1db26aSLionel Sambuc 		for (ptr = el->el_line.cursor - 1;
7483e1db26aSLionel Sambuc 		     ptr >= el->el_line.buffer && *ptr != '\n';
7493e1db26aSLionel Sambuc 		     ptr--)
7503e1db26aSLionel Sambuc 			continue;
7513e1db26aSLionel Sambuc 		return (int)(el->el_line.cursor - ptr - 1);
7523e1db26aSLionel Sambuc 	}
7533e1db26aSLionel Sambuc }
7543e1db26aSLionel Sambuc 
7553e1db26aSLionel Sambuc protected int
ch_resizefun(EditLine * el,el_zfunc_t f,void * a)7563e1db26aSLionel Sambuc ch_resizefun(EditLine *el, el_zfunc_t f, void *a)
7573e1db26aSLionel Sambuc {
7583e1db26aSLionel Sambuc 	el->el_chared.c_resizefun = f;
7593e1db26aSLionel Sambuc 	el->el_chared.c_resizearg = a;
7603e1db26aSLionel Sambuc 	return 0;
7613e1db26aSLionel Sambuc }
762*0a6a1f1dSLionel Sambuc 
763*0a6a1f1dSLionel Sambuc protected int
ch_aliasfun(EditLine * el,el_afunc_t f,void * a)764*0a6a1f1dSLionel Sambuc ch_aliasfun(EditLine *el, el_afunc_t f, void *a)
765*0a6a1f1dSLionel Sambuc {
766*0a6a1f1dSLionel Sambuc 	el->el_chared.c_aliasfun = f;
767*0a6a1f1dSLionel Sambuc 	el->el_chared.c_aliasarg = a;
768*0a6a1f1dSLionel Sambuc 	return 0;
769*0a6a1f1dSLionel Sambuc }
770