xref: /minix3/lib/libedit/refresh.c (revision 3e1db26a5a6252fcff0898d4cb0c3fa16ccf561d)
1*3e1db26aSLionel Sambuc /*	$NetBSD: refresh.c,v 1.37 2011/07/29 23:44:45 christos Exp $	*/
2*3e1db26aSLionel Sambuc 
3*3e1db26aSLionel Sambuc /*-
4*3e1db26aSLionel Sambuc  * Copyright (c) 1992, 1993
5*3e1db26aSLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
6*3e1db26aSLionel Sambuc  *
7*3e1db26aSLionel Sambuc  * This code is derived from software contributed to Berkeley by
8*3e1db26aSLionel Sambuc  * Christos Zoulas of Cornell University.
9*3e1db26aSLionel Sambuc  *
10*3e1db26aSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*3e1db26aSLionel Sambuc  * modification, are permitted provided that the following conditions
12*3e1db26aSLionel Sambuc  * are met:
13*3e1db26aSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*3e1db26aSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*3e1db26aSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*3e1db26aSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*3e1db26aSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*3e1db26aSLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
19*3e1db26aSLionel Sambuc  *    may be used to endorse or promote products derived from this software
20*3e1db26aSLionel Sambuc  *    without specific prior written permission.
21*3e1db26aSLionel Sambuc  *
22*3e1db26aSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*3e1db26aSLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*3e1db26aSLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*3e1db26aSLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*3e1db26aSLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*3e1db26aSLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*3e1db26aSLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*3e1db26aSLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*3e1db26aSLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*3e1db26aSLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*3e1db26aSLionel Sambuc  * SUCH DAMAGE.
33*3e1db26aSLionel Sambuc  */
34*3e1db26aSLionel Sambuc 
35*3e1db26aSLionel Sambuc #include "config.h"
36*3e1db26aSLionel Sambuc #if !defined(lint) && !defined(SCCSID)
37*3e1db26aSLionel Sambuc #if 0
38*3e1db26aSLionel Sambuc static char sccsid[] = "@(#)refresh.c	8.1 (Berkeley) 6/4/93";
39*3e1db26aSLionel Sambuc #else
40*3e1db26aSLionel Sambuc __RCSID("$NetBSD: refresh.c,v 1.37 2011/07/29 23:44:45 christos Exp $");
41*3e1db26aSLionel Sambuc #endif
42*3e1db26aSLionel Sambuc #endif /* not lint && not SCCSID */
43*3e1db26aSLionel Sambuc 
44*3e1db26aSLionel Sambuc /*
45*3e1db26aSLionel Sambuc  * refresh.c: Lower level screen refreshing functions
46*3e1db26aSLionel Sambuc  */
47*3e1db26aSLionel Sambuc #include <stdio.h>
48*3e1db26aSLionel Sambuc #include <ctype.h>
49*3e1db26aSLionel Sambuc #include <unistd.h>
50*3e1db26aSLionel Sambuc #include <string.h>
51*3e1db26aSLionel Sambuc 
52*3e1db26aSLionel Sambuc #include "el.h"
53*3e1db26aSLionel Sambuc 
54*3e1db26aSLionel Sambuc private void	re_nextline(EditLine *);
55*3e1db26aSLionel Sambuc private void	re_addc(EditLine *, Int);
56*3e1db26aSLionel Sambuc private void	re_update_line(EditLine *, Char *, Char *, int);
57*3e1db26aSLionel Sambuc private void	re_insert (EditLine *, Char *, int, int, Char *, int);
58*3e1db26aSLionel Sambuc private void	re_delete(EditLine *, Char *, int, int, int);
59*3e1db26aSLionel Sambuc private void	re_fastputc(EditLine *, Int);
60*3e1db26aSLionel Sambuc private void	re_clear_eol(EditLine *, int, int, int);
61*3e1db26aSLionel Sambuc private void	re__strncopy(Char *, Char *, size_t);
62*3e1db26aSLionel Sambuc private void	re__copy_and_pad(Char *, const Char *, size_t);
63*3e1db26aSLionel Sambuc 
64*3e1db26aSLionel Sambuc #ifdef DEBUG_REFRESH
65*3e1db26aSLionel Sambuc private void	re_printstr(EditLine *, const char *, char *, char *);
66*3e1db26aSLionel Sambuc #define	__F el->el_errfile
67*3e1db26aSLionel Sambuc #define	ELRE_ASSERT(a, b, c)	do 				\
68*3e1db26aSLionel Sambuc 				    if (/*CONSTCOND*/ a) {	\
69*3e1db26aSLionel Sambuc 					(void) fprintf b;	\
70*3e1db26aSLionel Sambuc 					c;			\
71*3e1db26aSLionel Sambuc 				    }				\
72*3e1db26aSLionel Sambuc 				while (/*CONSTCOND*/0)
73*3e1db26aSLionel Sambuc #define	ELRE_DEBUG(a, b)	ELRE_ASSERT(a,b,;)
74*3e1db26aSLionel Sambuc 
75*3e1db26aSLionel Sambuc /* re_printstr():
76*3e1db26aSLionel Sambuc  *	Print a string on the debugging pty
77*3e1db26aSLionel Sambuc  */
78*3e1db26aSLionel Sambuc private void
re_printstr(EditLine * el,const char * str,char * f,char * t)79*3e1db26aSLionel Sambuc re_printstr(EditLine *el, const char *str, char *f, char *t)
80*3e1db26aSLionel Sambuc {
81*3e1db26aSLionel Sambuc 
82*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1, (__F, "%s:\"", str));
83*3e1db26aSLionel Sambuc 	while (f < t)
84*3e1db26aSLionel Sambuc 		ELRE_DEBUG(1, (__F, "%c", *f++ & 0177));
85*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1, (__F, "\"\r\n"));
86*3e1db26aSLionel Sambuc }
87*3e1db26aSLionel Sambuc #else
88*3e1db26aSLionel Sambuc #define	ELRE_ASSERT(a, b, c)
89*3e1db26aSLionel Sambuc #define	ELRE_DEBUG(a, b)
90*3e1db26aSLionel Sambuc #endif
91*3e1db26aSLionel Sambuc 
92*3e1db26aSLionel Sambuc /* re_nextline():
93*3e1db26aSLionel Sambuc  *	Move to the next line or scroll
94*3e1db26aSLionel Sambuc  */
95*3e1db26aSLionel Sambuc private void
re_nextline(EditLine * el)96*3e1db26aSLionel Sambuc re_nextline(EditLine *el)
97*3e1db26aSLionel Sambuc {
98*3e1db26aSLionel Sambuc 	el->el_refresh.r_cursor.h = 0;	/* reset it. */
99*3e1db26aSLionel Sambuc 
100*3e1db26aSLionel Sambuc 	/*
101*3e1db26aSLionel Sambuc 	 * If we would overflow (input is longer than terminal size),
102*3e1db26aSLionel Sambuc 	 * emulate scroll by dropping first line and shuffling the rest.
103*3e1db26aSLionel Sambuc 	 * We do this via pointer shuffling - it's safe in this case
104*3e1db26aSLionel Sambuc 	 * and we avoid memcpy().
105*3e1db26aSLionel Sambuc 	 */
106*3e1db26aSLionel Sambuc 	if (el->el_refresh.r_cursor.v + 1 >= el->el_terminal.t_size.v) {
107*3e1db26aSLionel Sambuc 		int i, lins = el->el_terminal.t_size.v;
108*3e1db26aSLionel Sambuc 		Char *firstline = el->el_vdisplay[0];
109*3e1db26aSLionel Sambuc 
110*3e1db26aSLionel Sambuc 		for(i = 1; i < lins; i++)
111*3e1db26aSLionel Sambuc 			el->el_vdisplay[i - 1] = el->el_vdisplay[i];
112*3e1db26aSLionel Sambuc 
113*3e1db26aSLionel Sambuc 		firstline[0] = '\0';		/* empty the string */
114*3e1db26aSLionel Sambuc 		el->el_vdisplay[i - 1] = firstline;
115*3e1db26aSLionel Sambuc 	} else
116*3e1db26aSLionel Sambuc 		el->el_refresh.r_cursor.v++;
117*3e1db26aSLionel Sambuc 
118*3e1db26aSLionel Sambuc 	ELRE_ASSERT(el->el_refresh.r_cursor.v >= el->el_terminal.t_size.v,
119*3e1db26aSLionel Sambuc 	    (__F, "\r\nre_putc: overflow! r_cursor.v == %d > %d\r\n",
120*3e1db26aSLionel Sambuc 	    el->el_refresh.r_cursor.v, el->el_terminal.t_size.v),
121*3e1db26aSLionel Sambuc 	    abort());
122*3e1db26aSLionel Sambuc }
123*3e1db26aSLionel Sambuc 
124*3e1db26aSLionel Sambuc /* re_addc():
125*3e1db26aSLionel Sambuc  *	Draw c, expanding tabs, control chars etc.
126*3e1db26aSLionel Sambuc  */
127*3e1db26aSLionel Sambuc private void
re_addc(EditLine * el,Int c)128*3e1db26aSLionel Sambuc re_addc(EditLine *el, Int c)
129*3e1db26aSLionel Sambuc {
130*3e1db26aSLionel Sambuc 	switch (ct_chr_class((Char)c)) {
131*3e1db26aSLionel Sambuc 	case CHTYPE_TAB:        /* expand the tab */
132*3e1db26aSLionel Sambuc 		for (;;) {
133*3e1db26aSLionel Sambuc 			re_putc(el, ' ', 1);
134*3e1db26aSLionel Sambuc 			if ((el->el_refresh.r_cursor.h & 07) == 0)
135*3e1db26aSLionel Sambuc 				break;			/* go until tab stop */
136*3e1db26aSLionel Sambuc 		}
137*3e1db26aSLionel Sambuc 		break;
138*3e1db26aSLionel Sambuc 	case CHTYPE_NL: {
139*3e1db26aSLionel Sambuc 		int oldv = el->el_refresh.r_cursor.v;
140*3e1db26aSLionel Sambuc 		re_putc(el, '\0', 0);			/* assure end of line */
141*3e1db26aSLionel Sambuc 		if (oldv == el->el_refresh.r_cursor.v)	/* XXX */
142*3e1db26aSLionel Sambuc 			re_nextline(el);
143*3e1db26aSLionel Sambuc 		break;
144*3e1db26aSLionel Sambuc 	}
145*3e1db26aSLionel Sambuc 	case CHTYPE_PRINT:
146*3e1db26aSLionel Sambuc 		re_putc(el, c, 1);
147*3e1db26aSLionel Sambuc 		break;
148*3e1db26aSLionel Sambuc 	default: {
149*3e1db26aSLionel Sambuc 		Char visbuf[VISUAL_WIDTH_MAX];
150*3e1db26aSLionel Sambuc 		ssize_t i, n =
151*3e1db26aSLionel Sambuc 		    ct_visual_char(visbuf, VISUAL_WIDTH_MAX, (Char)c);
152*3e1db26aSLionel Sambuc 		for (i = 0; n-- > 0; ++i)
153*3e1db26aSLionel Sambuc 		    re_putc(el, visbuf[i], 1);
154*3e1db26aSLionel Sambuc 		break;
155*3e1db26aSLionel Sambuc 	}
156*3e1db26aSLionel Sambuc 	}
157*3e1db26aSLionel Sambuc }
158*3e1db26aSLionel Sambuc 
159*3e1db26aSLionel Sambuc 
160*3e1db26aSLionel Sambuc /* re_putc():
161*3e1db26aSLionel Sambuc  *	Draw the character given
162*3e1db26aSLionel Sambuc  */
163*3e1db26aSLionel Sambuc protected void
re_putc(EditLine * el,Int c,int shift)164*3e1db26aSLionel Sambuc re_putc(EditLine *el, Int c, int shift)
165*3e1db26aSLionel Sambuc {
166*3e1db26aSLionel Sambuc 	int i, w = Width(c);
167*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1, (__F, "printing %5x '%c'\r\n", c, c));
168*3e1db26aSLionel Sambuc 
169*3e1db26aSLionel Sambuc 	while (shift && (el->el_refresh.r_cursor.h + w > el->el_terminal.t_size.h))
170*3e1db26aSLionel Sambuc 	    re_putc(el, ' ', 1);
171*3e1db26aSLionel Sambuc 
172*3e1db26aSLionel Sambuc 	el->el_vdisplay[el->el_refresh.r_cursor.v]
173*3e1db26aSLionel Sambuc 	    [el->el_refresh.r_cursor.h] = c;
174*3e1db26aSLionel Sambuc 	/* assumes !shift is only used for single-column chars */
175*3e1db26aSLionel Sambuc 	i = w;
176*3e1db26aSLionel Sambuc 	while (--i > 0)
177*3e1db26aSLionel Sambuc 		el->el_vdisplay[el->el_refresh.r_cursor.v]
178*3e1db26aSLionel Sambuc 		    [el->el_refresh.r_cursor.h + i] = MB_FILL_CHAR;
179*3e1db26aSLionel Sambuc 
180*3e1db26aSLionel Sambuc 	if (!shift)
181*3e1db26aSLionel Sambuc 		return;
182*3e1db26aSLionel Sambuc 
183*3e1db26aSLionel Sambuc 	el->el_refresh.r_cursor.h += w;	/* advance to next place */
184*3e1db26aSLionel Sambuc 	if (el->el_refresh.r_cursor.h >= el->el_terminal.t_size.h) {
185*3e1db26aSLionel Sambuc 		/* assure end of line */
186*3e1db26aSLionel Sambuc 		el->el_vdisplay[el->el_refresh.r_cursor.v][el->el_terminal.t_size.h]
187*3e1db26aSLionel Sambuc 		    = '\0';
188*3e1db26aSLionel Sambuc 		re_nextline(el);
189*3e1db26aSLionel Sambuc 	}
190*3e1db26aSLionel Sambuc }
191*3e1db26aSLionel Sambuc 
192*3e1db26aSLionel Sambuc 
193*3e1db26aSLionel Sambuc /* re_refresh():
194*3e1db26aSLionel Sambuc  *	draws the new virtual screen image from the current input
195*3e1db26aSLionel Sambuc  *  	line, then goes line-by-line changing the real image to the new
196*3e1db26aSLionel Sambuc  *	virtual image. The routine to re-draw a line can be replaced
197*3e1db26aSLionel Sambuc  *	easily in hopes of a smarter one being placed there.
198*3e1db26aSLionel Sambuc  */
199*3e1db26aSLionel Sambuc protected void
re_refresh(EditLine * el)200*3e1db26aSLionel Sambuc re_refresh(EditLine *el)
201*3e1db26aSLionel Sambuc {
202*3e1db26aSLionel Sambuc 	int i, rhdiff;
203*3e1db26aSLionel Sambuc 	Char *cp, *st;
204*3e1db26aSLionel Sambuc 	coord_t cur;
205*3e1db26aSLionel Sambuc #ifdef notyet
206*3e1db26aSLionel Sambuc 	size_t termsz;
207*3e1db26aSLionel Sambuc #endif
208*3e1db26aSLionel Sambuc 
209*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1, (__F, "el->el_line.buffer = :%s:\r\n",
210*3e1db26aSLionel Sambuc 	    el->el_line.buffer));
211*3e1db26aSLionel Sambuc 
212*3e1db26aSLionel Sambuc 	/* reset the Drawing cursor */
213*3e1db26aSLionel Sambuc 	el->el_refresh.r_cursor.h = 0;
214*3e1db26aSLionel Sambuc 	el->el_refresh.r_cursor.v = 0;
215*3e1db26aSLionel Sambuc 
216*3e1db26aSLionel Sambuc 	/* temporarily draw rprompt to calculate its size */
217*3e1db26aSLionel Sambuc 	prompt_print(el, EL_RPROMPT);
218*3e1db26aSLionel Sambuc 
219*3e1db26aSLionel Sambuc 	/* reset the Drawing cursor */
220*3e1db26aSLionel Sambuc 	el->el_refresh.r_cursor.h = 0;
221*3e1db26aSLionel Sambuc 	el->el_refresh.r_cursor.v = 0;
222*3e1db26aSLionel Sambuc 
223*3e1db26aSLionel Sambuc 	if (el->el_line.cursor >= el->el_line.lastchar) {
224*3e1db26aSLionel Sambuc 		if (el->el_map.current == el->el_map.alt
225*3e1db26aSLionel Sambuc 		    && el->el_line.lastchar != el->el_line.buffer)
226*3e1db26aSLionel Sambuc 			el->el_line.cursor = el->el_line.lastchar - 1;
227*3e1db26aSLionel Sambuc 		else
228*3e1db26aSLionel Sambuc 			el->el_line.cursor = el->el_line.lastchar;
229*3e1db26aSLionel Sambuc 	}
230*3e1db26aSLionel Sambuc 
231*3e1db26aSLionel Sambuc 	cur.h = -1;		/* set flag in case I'm not set */
232*3e1db26aSLionel Sambuc 	cur.v = 0;
233*3e1db26aSLionel Sambuc 
234*3e1db26aSLionel Sambuc 	prompt_print(el, EL_PROMPT);
235*3e1db26aSLionel Sambuc 
236*3e1db26aSLionel Sambuc 	/* draw the current input buffer */
237*3e1db26aSLionel Sambuc #if notyet
238*3e1db26aSLionel Sambuc 	termsz = el->el_terminal.t_size.h * el->el_terminal.t_size.v;
239*3e1db26aSLionel Sambuc 	if (el->el_line.lastchar - el->el_line.buffer > termsz) {
240*3e1db26aSLionel Sambuc 		/*
241*3e1db26aSLionel Sambuc 		 * If line is longer than terminal, process only part
242*3e1db26aSLionel Sambuc 		 * of line which would influence display.
243*3e1db26aSLionel Sambuc 		 */
244*3e1db26aSLionel Sambuc 		size_t rem = (el->el_line.lastchar-el->el_line.buffer)%termsz;
245*3e1db26aSLionel Sambuc 
246*3e1db26aSLionel Sambuc 		st = el->el_line.lastchar - rem
247*3e1db26aSLionel Sambuc 			- (termsz - (((rem / el->el_terminal.t_size.v) - 1)
248*3e1db26aSLionel Sambuc 					* el->el_terminal.t_size.v));
249*3e1db26aSLionel Sambuc 	} else
250*3e1db26aSLionel Sambuc #endif
251*3e1db26aSLionel Sambuc 		st = el->el_line.buffer;
252*3e1db26aSLionel Sambuc 
253*3e1db26aSLionel Sambuc 	for (cp = st; cp < el->el_line.lastchar; cp++) {
254*3e1db26aSLionel Sambuc 		if (cp == el->el_line.cursor) {
255*3e1db26aSLionel Sambuc                         int w = Width(*cp);
256*3e1db26aSLionel Sambuc 			/* save for later */
257*3e1db26aSLionel Sambuc 			cur.h = el->el_refresh.r_cursor.h;
258*3e1db26aSLionel Sambuc 			cur.v = el->el_refresh.r_cursor.v;
259*3e1db26aSLionel Sambuc                         /* handle being at a linebroken doublewidth char */
260*3e1db26aSLionel Sambuc                         if (w > 1 && el->el_refresh.r_cursor.h + w >
261*3e1db26aSLionel Sambuc 			    el->el_terminal.t_size.h) {
262*3e1db26aSLionel Sambuc 				cur.h = 0;
263*3e1db26aSLionel Sambuc 				cur.v++;
264*3e1db26aSLionel Sambuc                         }
265*3e1db26aSLionel Sambuc 		}
266*3e1db26aSLionel Sambuc 		re_addc(el, *cp);
267*3e1db26aSLionel Sambuc 	}
268*3e1db26aSLionel Sambuc 
269*3e1db26aSLionel Sambuc 	if (cur.h == -1) {	/* if I haven't been set yet, I'm at the end */
270*3e1db26aSLionel Sambuc 		cur.h = el->el_refresh.r_cursor.h;
271*3e1db26aSLionel Sambuc 		cur.v = el->el_refresh.r_cursor.v;
272*3e1db26aSLionel Sambuc 	}
273*3e1db26aSLionel Sambuc 	rhdiff = el->el_terminal.t_size.h - el->el_refresh.r_cursor.h -
274*3e1db26aSLionel Sambuc 	    el->el_rprompt.p_pos.h;
275*3e1db26aSLionel Sambuc 	if (el->el_rprompt.p_pos.h && !el->el_rprompt.p_pos.v &&
276*3e1db26aSLionel Sambuc 	    !el->el_refresh.r_cursor.v && rhdiff > 1) {
277*3e1db26aSLionel Sambuc 		/*
278*3e1db26aSLionel Sambuc 		 * have a right-hand side prompt that will fit
279*3e1db26aSLionel Sambuc 		 * on the end of the first line with at least
280*3e1db26aSLionel Sambuc 		 * one character gap to the input buffer.
281*3e1db26aSLionel Sambuc 		 */
282*3e1db26aSLionel Sambuc 		while (--rhdiff > 0)	/* pad out with spaces */
283*3e1db26aSLionel Sambuc 			re_putc(el, ' ', 1);
284*3e1db26aSLionel Sambuc 		prompt_print(el, EL_RPROMPT);
285*3e1db26aSLionel Sambuc 	} else {
286*3e1db26aSLionel Sambuc 		el->el_rprompt.p_pos.h = 0;	/* flag "not using rprompt" */
287*3e1db26aSLionel Sambuc 		el->el_rprompt.p_pos.v = 0;
288*3e1db26aSLionel Sambuc 	}
289*3e1db26aSLionel Sambuc 
290*3e1db26aSLionel Sambuc 	re_putc(el, '\0', 0);	/* make line ended with NUL, no cursor shift */
291*3e1db26aSLionel Sambuc 
292*3e1db26aSLionel Sambuc 	el->el_refresh.r_newcv = el->el_refresh.r_cursor.v;
293*3e1db26aSLionel Sambuc 
294*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1, (__F,
295*3e1db26aSLionel Sambuc 		"term.h=%d vcur.h=%d vcur.v=%d vdisplay[0]=\r\n:%80.80s:\r\n",
296*3e1db26aSLionel Sambuc 		el->el_terminal.t_size.h, el->el_refresh.r_cursor.h,
297*3e1db26aSLionel Sambuc 		el->el_refresh.r_cursor.v, ct_encode_string(el->el_vdisplay[0])));
298*3e1db26aSLionel Sambuc 
299*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1, (__F, "updating %d lines.\r\n", el->el_refresh.r_newcv));
300*3e1db26aSLionel Sambuc 	for (i = 0; i <= el->el_refresh.r_newcv; i++) {
301*3e1db26aSLionel Sambuc 		/* NOTE THAT re_update_line MAY CHANGE el_display[i] */
302*3e1db26aSLionel Sambuc 		re_update_line(el, el->el_display[i], el->el_vdisplay[i], i);
303*3e1db26aSLionel Sambuc 
304*3e1db26aSLionel Sambuc 		/*
305*3e1db26aSLionel Sambuc 		 * Copy the new line to be the current one, and pad out with
306*3e1db26aSLionel Sambuc 		 * spaces to the full width of the terminal so that if we try
307*3e1db26aSLionel Sambuc 		 * moving the cursor by writing the character that is at the
308*3e1db26aSLionel Sambuc 		 * end of the screen line, it won't be a NUL or some old
309*3e1db26aSLionel Sambuc 		 * leftover stuff.
310*3e1db26aSLionel Sambuc 		 */
311*3e1db26aSLionel Sambuc 		re__copy_and_pad(el->el_display[i], el->el_vdisplay[i],
312*3e1db26aSLionel Sambuc 		    (size_t) el->el_terminal.t_size.h);
313*3e1db26aSLionel Sambuc 	}
314*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1, (__F,
315*3e1db26aSLionel Sambuc 	"\r\nel->el_refresh.r_cursor.v=%d,el->el_refresh.r_oldcv=%d i=%d\r\n",
316*3e1db26aSLionel Sambuc 	    el->el_refresh.r_cursor.v, el->el_refresh.r_oldcv, i));
317*3e1db26aSLionel Sambuc 
318*3e1db26aSLionel Sambuc 	if (el->el_refresh.r_oldcv > el->el_refresh.r_newcv)
319*3e1db26aSLionel Sambuc 		for (; i <= el->el_refresh.r_oldcv; i++) {
320*3e1db26aSLionel Sambuc 			terminal_move_to_line(el, i);
321*3e1db26aSLionel Sambuc 			terminal_move_to_char(el, 0);
322*3e1db26aSLionel Sambuc                         /* This Strlen should be safe even with MB_FILL_CHARs */
323*3e1db26aSLionel Sambuc 			terminal_clear_EOL(el, (int) Strlen(el->el_display[i]));
324*3e1db26aSLionel Sambuc #ifdef DEBUG_REFRESH
325*3e1db26aSLionel Sambuc 			terminal_overwrite(el, "C\b", (size_t)2);
326*3e1db26aSLionel Sambuc #endif /* DEBUG_REFRESH */
327*3e1db26aSLionel Sambuc 			el->el_display[i][0] = '\0';
328*3e1db26aSLionel Sambuc 		}
329*3e1db26aSLionel Sambuc 
330*3e1db26aSLionel Sambuc 	el->el_refresh.r_oldcv = el->el_refresh.r_newcv; /* set for next time */
331*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1, (__F,
332*3e1db26aSLionel Sambuc 	    "\r\ncursor.h = %d, cursor.v = %d, cur.h = %d, cur.v = %d\r\n",
333*3e1db26aSLionel Sambuc 	    el->el_refresh.r_cursor.h, el->el_refresh.r_cursor.v,
334*3e1db26aSLionel Sambuc 	    cur.h, cur.v));
335*3e1db26aSLionel Sambuc 	terminal_move_to_line(el, cur.v);	/* go to where the cursor is */
336*3e1db26aSLionel Sambuc 	terminal_move_to_char(el, cur.h);
337*3e1db26aSLionel Sambuc }
338*3e1db26aSLionel Sambuc 
339*3e1db26aSLionel Sambuc 
340*3e1db26aSLionel Sambuc /* re_goto_bottom():
341*3e1db26aSLionel Sambuc  *	 used to go to last used screen line
342*3e1db26aSLionel Sambuc  */
343*3e1db26aSLionel Sambuc protected void
re_goto_bottom(EditLine * el)344*3e1db26aSLionel Sambuc re_goto_bottom(EditLine *el)
345*3e1db26aSLionel Sambuc {
346*3e1db26aSLionel Sambuc 
347*3e1db26aSLionel Sambuc 	terminal_move_to_line(el, el->el_refresh.r_oldcv);
348*3e1db26aSLionel Sambuc 	terminal__putc(el, '\n');
349*3e1db26aSLionel Sambuc 	re_clear_display(el);
350*3e1db26aSLionel Sambuc 	terminal__flush(el);
351*3e1db26aSLionel Sambuc }
352*3e1db26aSLionel Sambuc 
353*3e1db26aSLionel Sambuc 
354*3e1db26aSLionel Sambuc /* re_insert():
355*3e1db26aSLionel Sambuc  *	insert num characters of s into d (in front of the character)
356*3e1db26aSLionel Sambuc  *	at dat, maximum length of d is dlen
357*3e1db26aSLionel Sambuc  */
358*3e1db26aSLionel Sambuc private void
359*3e1db26aSLionel Sambuc /*ARGSUSED*/
re_insert(EditLine * el,Char * d,int dat,int dlen,Char * s,int num)360*3e1db26aSLionel Sambuc re_insert(EditLine *el __attribute__((__unused__)),
361*3e1db26aSLionel Sambuc     Char *d, int dat, int dlen, Char *s, int num)
362*3e1db26aSLionel Sambuc {
363*3e1db26aSLionel Sambuc 	Char *a, *b;
364*3e1db26aSLionel Sambuc 
365*3e1db26aSLionel Sambuc 	if (num <= 0)
366*3e1db26aSLionel Sambuc 		return;
367*3e1db26aSLionel Sambuc 	if (num > dlen - dat)
368*3e1db26aSLionel Sambuc 		num = dlen - dat;
369*3e1db26aSLionel Sambuc 
370*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1,
371*3e1db26aSLionel Sambuc 	    (__F, "re_insert() starting: %d at %d max %d, d == \"%s\"\n",
372*3e1db26aSLionel Sambuc 	    num, dat, dlen, ct_encode_string(d)));
373*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1, (__F, "s == \"%s\"\n", ct_encode_string(s)));
374*3e1db26aSLionel Sambuc 
375*3e1db26aSLionel Sambuc 	/* open up the space for num chars */
376*3e1db26aSLionel Sambuc 	if (num > 0) {
377*3e1db26aSLionel Sambuc 		b = d + dlen - 1;
378*3e1db26aSLionel Sambuc 		a = b - num;
379*3e1db26aSLionel Sambuc 		while (a >= &d[dat])
380*3e1db26aSLionel Sambuc 			*b-- = *a--;
381*3e1db26aSLionel Sambuc 		d[dlen] = '\0';	/* just in case */
382*3e1db26aSLionel Sambuc 	}
383*3e1db26aSLionel Sambuc 
384*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1, (__F,
385*3e1db26aSLionel Sambuc 		"re_insert() after insert: %d at %d max %d, d == \"%s\"\n",
386*3e1db26aSLionel Sambuc 		num, dat, dlen, ct_encode_string(d)));
387*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1, (__F, "s == \"%s\"\n", ct_encode_string(s)));
388*3e1db26aSLionel Sambuc 
389*3e1db26aSLionel Sambuc 	/* copy the characters */
390*3e1db26aSLionel Sambuc 	for (a = d + dat; (a < d + dlen) && (num > 0); num--)
391*3e1db26aSLionel Sambuc 		*a++ = *s++;
392*3e1db26aSLionel Sambuc 
393*3e1db26aSLionel Sambuc #ifdef notyet
394*3e1db26aSLionel Sambuc         /* ct_encode_string() uses a static buffer, so we can't conveniently
395*3e1db26aSLionel Sambuc          * encode both d & s here */
396*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1,
397*3e1db26aSLionel Sambuc 	    (__F, "re_insert() after copy: %d at %d max %d, %s == \"%s\"\n",
398*3e1db26aSLionel Sambuc 	    num, dat, dlen, d, s));
399*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1, (__F, "s == \"%s\"\n", s));
400*3e1db26aSLionel Sambuc #endif
401*3e1db26aSLionel Sambuc }
402*3e1db26aSLionel Sambuc 
403*3e1db26aSLionel Sambuc 
404*3e1db26aSLionel Sambuc /* re_delete():
405*3e1db26aSLionel Sambuc  *	delete num characters d at dat, maximum length of d is dlen
406*3e1db26aSLionel Sambuc  */
407*3e1db26aSLionel Sambuc private void
408*3e1db26aSLionel Sambuc /*ARGSUSED*/
re_delete(EditLine * el,Char * d,int dat,int dlen,int num)409*3e1db26aSLionel Sambuc re_delete(EditLine *el __attribute__((__unused__)),
410*3e1db26aSLionel Sambuc     Char *d, int dat, int dlen, int num)
411*3e1db26aSLionel Sambuc {
412*3e1db26aSLionel Sambuc 	Char *a, *b;
413*3e1db26aSLionel Sambuc 
414*3e1db26aSLionel Sambuc 	if (num <= 0)
415*3e1db26aSLionel Sambuc 		return;
416*3e1db26aSLionel Sambuc 	if (dat + num >= dlen) {
417*3e1db26aSLionel Sambuc 		d[dat] = '\0';
418*3e1db26aSLionel Sambuc 		return;
419*3e1db26aSLionel Sambuc 	}
420*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1,
421*3e1db26aSLionel Sambuc 	    (__F, "re_delete() starting: %d at %d max %d, d == \"%s\"\n",
422*3e1db26aSLionel Sambuc 	    num, dat, dlen, ct_encode_string(d)));
423*3e1db26aSLionel Sambuc 
424*3e1db26aSLionel Sambuc 	/* open up the space for num chars */
425*3e1db26aSLionel Sambuc 	if (num > 0) {
426*3e1db26aSLionel Sambuc 		b = d + dat;
427*3e1db26aSLionel Sambuc 		a = b + num;
428*3e1db26aSLionel Sambuc 		while (a < &d[dlen])
429*3e1db26aSLionel Sambuc 			*b++ = *a++;
430*3e1db26aSLionel Sambuc 		d[dlen] = '\0';	/* just in case */
431*3e1db26aSLionel Sambuc 	}
432*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1,
433*3e1db26aSLionel Sambuc 	    (__F, "re_delete() after delete: %d at %d max %d, d == \"%s\"\n",
434*3e1db26aSLionel Sambuc 	    num, dat, dlen, ct_encode_string(d)));
435*3e1db26aSLionel Sambuc }
436*3e1db26aSLionel Sambuc 
437*3e1db26aSLionel Sambuc 
438*3e1db26aSLionel Sambuc /* re__strncopy():
439*3e1db26aSLionel Sambuc  *	Like strncpy without padding.
440*3e1db26aSLionel Sambuc  */
441*3e1db26aSLionel Sambuc private void
re__strncopy(Char * a,Char * b,size_t n)442*3e1db26aSLionel Sambuc re__strncopy(Char *a, Char *b, size_t n)
443*3e1db26aSLionel Sambuc {
444*3e1db26aSLionel Sambuc 
445*3e1db26aSLionel Sambuc 	while (n-- && *b)
446*3e1db26aSLionel Sambuc 		*a++ = *b++;
447*3e1db26aSLionel Sambuc }
448*3e1db26aSLionel Sambuc 
449*3e1db26aSLionel Sambuc /* re_clear_eol():
450*3e1db26aSLionel Sambuc  *	Find the number of characters we need to clear till the end of line
451*3e1db26aSLionel Sambuc  *	in order to make sure that we have cleared the previous contents of
452*3e1db26aSLionel Sambuc  *	the line. fx and sx is the number of characters inserted or deleted
453*3e1db26aSLionel Sambuc  *	in the first or second diff, diff is the difference between the
454*3e1db26aSLionel Sambuc  * 	number of characters between the new and old line.
455*3e1db26aSLionel Sambuc  */
456*3e1db26aSLionel Sambuc private void
re_clear_eol(EditLine * el,int fx,int sx,int diff)457*3e1db26aSLionel Sambuc re_clear_eol(EditLine *el, int fx, int sx, int diff)
458*3e1db26aSLionel Sambuc {
459*3e1db26aSLionel Sambuc 
460*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1, (__F, "re_clear_eol sx %d, fx %d, diff %d\n",
461*3e1db26aSLionel Sambuc 	    sx, fx, diff));
462*3e1db26aSLionel Sambuc 
463*3e1db26aSLionel Sambuc 	if (fx < 0)
464*3e1db26aSLionel Sambuc 		fx = -fx;
465*3e1db26aSLionel Sambuc 	if (sx < 0)
466*3e1db26aSLionel Sambuc 		sx = -sx;
467*3e1db26aSLionel Sambuc 	if (fx > diff)
468*3e1db26aSLionel Sambuc 		diff = fx;
469*3e1db26aSLionel Sambuc 	if (sx > diff)
470*3e1db26aSLionel Sambuc 		diff = sx;
471*3e1db26aSLionel Sambuc 
472*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1, (__F, "re_clear_eol %d\n", diff));
473*3e1db26aSLionel Sambuc 	terminal_clear_EOL(el, diff);
474*3e1db26aSLionel Sambuc }
475*3e1db26aSLionel Sambuc 
476*3e1db26aSLionel Sambuc /*****************************************************************
477*3e1db26aSLionel Sambuc     re_update_line() is based on finding the middle difference of each line
478*3e1db26aSLionel Sambuc     on the screen; vis:
479*3e1db26aSLionel Sambuc 
480*3e1db26aSLionel Sambuc 			     /old first difference
481*3e1db26aSLionel Sambuc 	/beginning of line   |              /old last same       /old EOL
482*3e1db26aSLionel Sambuc 	v		     v              v                    v
483*3e1db26aSLionel Sambuc old:	eddie> Oh, my little gruntle-buggy is to me, as lurgid as
484*3e1db26aSLionel Sambuc new:	eddie> Oh, my little buggy says to me, as lurgid as
485*3e1db26aSLionel Sambuc 	^		     ^        ^			   ^
486*3e1db26aSLionel Sambuc 	\beginning of line   |        \new last same	   \new end of line
487*3e1db26aSLionel Sambuc 			     \new first difference
488*3e1db26aSLionel Sambuc 
489*3e1db26aSLionel Sambuc     all are character pointers for the sake of speed.  Special cases for
490*3e1db26aSLionel Sambuc     no differences, as well as for end of line additions must be handled.
491*3e1db26aSLionel Sambuc **************************************************************** */
492*3e1db26aSLionel Sambuc 
493*3e1db26aSLionel Sambuc /* Minimum at which doing an insert it "worth it".  This should be about
494*3e1db26aSLionel Sambuc  * half the "cost" of going into insert mode, inserting a character, and
495*3e1db26aSLionel Sambuc  * going back out.  This should really be calculated from the termcap
496*3e1db26aSLionel Sambuc  * data...  For the moment, a good number for ANSI terminals.
497*3e1db26aSLionel Sambuc  */
498*3e1db26aSLionel Sambuc #define	MIN_END_KEEP	4
499*3e1db26aSLionel Sambuc 
500*3e1db26aSLionel Sambuc private void
re_update_line(EditLine * el,Char * old,Char * new,int i)501*3e1db26aSLionel Sambuc re_update_line(EditLine *el, Char *old, Char *new, int i)
502*3e1db26aSLionel Sambuc {
503*3e1db26aSLionel Sambuc 	Char *o, *n, *p, c;
504*3e1db26aSLionel Sambuc 	Char *ofd, *ols, *oe, *nfd, *nls, *ne;
505*3e1db26aSLionel Sambuc 	Char *osb, *ose, *nsb, *nse;
506*3e1db26aSLionel Sambuc 	int fx, sx;
507*3e1db26aSLionel Sambuc 	size_t len;
508*3e1db26aSLionel Sambuc 
509*3e1db26aSLionel Sambuc 	/*
510*3e1db26aSLionel Sambuc          * find first diff
511*3e1db26aSLionel Sambuc          */
512*3e1db26aSLionel Sambuc 	for (o = old, n = new; *o && (*o == *n); o++, n++)
513*3e1db26aSLionel Sambuc 		continue;
514*3e1db26aSLionel Sambuc 	ofd = o;
515*3e1db26aSLionel Sambuc 	nfd = n;
516*3e1db26aSLionel Sambuc 
517*3e1db26aSLionel Sambuc 	/*
518*3e1db26aSLionel Sambuc          * Find the end of both old and new
519*3e1db26aSLionel Sambuc          */
520*3e1db26aSLionel Sambuc 	while (*o)
521*3e1db26aSLionel Sambuc 		o++;
522*3e1db26aSLionel Sambuc 	/*
523*3e1db26aSLionel Sambuc          * Remove any trailing blanks off of the end, being careful not to
524*3e1db26aSLionel Sambuc          * back up past the beginning.
525*3e1db26aSLionel Sambuc          */
526*3e1db26aSLionel Sambuc 	while (ofd < o) {
527*3e1db26aSLionel Sambuc 		if (o[-1] != ' ')
528*3e1db26aSLionel Sambuc 			break;
529*3e1db26aSLionel Sambuc 		o--;
530*3e1db26aSLionel Sambuc 	}
531*3e1db26aSLionel Sambuc 	oe = o;
532*3e1db26aSLionel Sambuc 	*oe = '\0';
533*3e1db26aSLionel Sambuc 
534*3e1db26aSLionel Sambuc 	while (*n)
535*3e1db26aSLionel Sambuc 		n++;
536*3e1db26aSLionel Sambuc 
537*3e1db26aSLionel Sambuc 	/* remove blanks from end of new */
538*3e1db26aSLionel Sambuc 	while (nfd < n) {
539*3e1db26aSLionel Sambuc 		if (n[-1] != ' ')
540*3e1db26aSLionel Sambuc 			break;
541*3e1db26aSLionel Sambuc 		n--;
542*3e1db26aSLionel Sambuc 	}
543*3e1db26aSLionel Sambuc 	ne = n;
544*3e1db26aSLionel Sambuc 	*ne = '\0';
545*3e1db26aSLionel Sambuc 
546*3e1db26aSLionel Sambuc 	/*
547*3e1db26aSLionel Sambuc          * if no diff, continue to next line of redraw
548*3e1db26aSLionel Sambuc          */
549*3e1db26aSLionel Sambuc 	if (*ofd == '\0' && *nfd == '\0') {
550*3e1db26aSLionel Sambuc 		ELRE_DEBUG(1, (__F, "no difference.\r\n"));
551*3e1db26aSLionel Sambuc 		return;
552*3e1db26aSLionel Sambuc 	}
553*3e1db26aSLionel Sambuc 	/*
554*3e1db26aSLionel Sambuc          * find last same pointer
555*3e1db26aSLionel Sambuc          */
556*3e1db26aSLionel Sambuc 	while ((o > ofd) && (n > nfd) && (*--o == *--n))
557*3e1db26aSLionel Sambuc 		continue;
558*3e1db26aSLionel Sambuc 	ols = ++o;
559*3e1db26aSLionel Sambuc 	nls = ++n;
560*3e1db26aSLionel Sambuc 
561*3e1db26aSLionel Sambuc 	/*
562*3e1db26aSLionel Sambuc          * find same begining and same end
563*3e1db26aSLionel Sambuc          */
564*3e1db26aSLionel Sambuc 	osb = ols;
565*3e1db26aSLionel Sambuc 	nsb = nls;
566*3e1db26aSLionel Sambuc 	ose = ols;
567*3e1db26aSLionel Sambuc 	nse = nls;
568*3e1db26aSLionel Sambuc 
569*3e1db26aSLionel Sambuc 	/*
570*3e1db26aSLionel Sambuc          * case 1: insert: scan from nfd to nls looking for *ofd
571*3e1db26aSLionel Sambuc          */
572*3e1db26aSLionel Sambuc 	if (*ofd) {
573*3e1db26aSLionel Sambuc 		for (c = *ofd, n = nfd; n < nls; n++) {
574*3e1db26aSLionel Sambuc 			if (c == *n) {
575*3e1db26aSLionel Sambuc 				for (o = ofd, p = n;
576*3e1db26aSLionel Sambuc 				    p < nls && o < ols && *o == *p;
577*3e1db26aSLionel Sambuc 				    o++, p++)
578*3e1db26aSLionel Sambuc 					continue;
579*3e1db26aSLionel Sambuc 				/*
580*3e1db26aSLionel Sambuc 				 * if the new match is longer and it's worth
581*3e1db26aSLionel Sambuc 				 * keeping, then we take it
582*3e1db26aSLionel Sambuc 				 */
583*3e1db26aSLionel Sambuc 				if (((nse - nsb) < (p - n)) &&
584*3e1db26aSLionel Sambuc 				    (2 * (p - n) > n - nfd)) {
585*3e1db26aSLionel Sambuc 					nsb = n;
586*3e1db26aSLionel Sambuc 					nse = p;
587*3e1db26aSLionel Sambuc 					osb = ofd;
588*3e1db26aSLionel Sambuc 					ose = o;
589*3e1db26aSLionel Sambuc 				}
590*3e1db26aSLionel Sambuc 			}
591*3e1db26aSLionel Sambuc 		}
592*3e1db26aSLionel Sambuc 	}
593*3e1db26aSLionel Sambuc 	/*
594*3e1db26aSLionel Sambuc          * case 2: delete: scan from ofd to ols looking for *nfd
595*3e1db26aSLionel Sambuc          */
596*3e1db26aSLionel Sambuc 	if (*nfd) {
597*3e1db26aSLionel Sambuc 		for (c = *nfd, o = ofd; o < ols; o++) {
598*3e1db26aSLionel Sambuc 			if (c == *o) {
599*3e1db26aSLionel Sambuc 				for (n = nfd, p = o;
600*3e1db26aSLionel Sambuc 				    p < ols && n < nls && *p == *n;
601*3e1db26aSLionel Sambuc 				    p++, n++)
602*3e1db26aSLionel Sambuc 					continue;
603*3e1db26aSLionel Sambuc 				/*
604*3e1db26aSLionel Sambuc 				 * if the new match is longer and it's worth
605*3e1db26aSLionel Sambuc 				 * keeping, then we take it
606*3e1db26aSLionel Sambuc 				 */
607*3e1db26aSLionel Sambuc 				if (((ose - osb) < (p - o)) &&
608*3e1db26aSLionel Sambuc 				    (2 * (p - o) > o - ofd)) {
609*3e1db26aSLionel Sambuc 					nsb = nfd;
610*3e1db26aSLionel Sambuc 					nse = n;
611*3e1db26aSLionel Sambuc 					osb = o;
612*3e1db26aSLionel Sambuc 					ose = p;
613*3e1db26aSLionel Sambuc 				}
614*3e1db26aSLionel Sambuc 			}
615*3e1db26aSLionel Sambuc 		}
616*3e1db26aSLionel Sambuc 	}
617*3e1db26aSLionel Sambuc 	/*
618*3e1db26aSLionel Sambuc          * Pragmatics I: If old trailing whitespace or not enough characters to
619*3e1db26aSLionel Sambuc          * save to be worth it, then don't save the last same info.
620*3e1db26aSLionel Sambuc          */
621*3e1db26aSLionel Sambuc 	if ((oe - ols) < MIN_END_KEEP) {
622*3e1db26aSLionel Sambuc 		ols = oe;
623*3e1db26aSLionel Sambuc 		nls = ne;
624*3e1db26aSLionel Sambuc 	}
625*3e1db26aSLionel Sambuc 	/*
626*3e1db26aSLionel Sambuc          * Pragmatics II: if the terminal isn't smart enough, make the data
627*3e1db26aSLionel Sambuc          * dumber so the smart update doesn't try anything fancy
628*3e1db26aSLionel Sambuc          */
629*3e1db26aSLionel Sambuc 
630*3e1db26aSLionel Sambuc 	/*
631*3e1db26aSLionel Sambuc          * fx is the number of characters we need to insert/delete: in the
632*3e1db26aSLionel Sambuc          * beginning to bring the two same begins together
633*3e1db26aSLionel Sambuc          */
634*3e1db26aSLionel Sambuc 	fx = (int)((nsb - nfd) - (osb - ofd));
635*3e1db26aSLionel Sambuc 	/*
636*3e1db26aSLionel Sambuc          * sx is the number of characters we need to insert/delete: in the
637*3e1db26aSLionel Sambuc          * end to bring the two same last parts together
638*3e1db26aSLionel Sambuc          */
639*3e1db26aSLionel Sambuc 	sx = (int)((nls - nse) - (ols - ose));
640*3e1db26aSLionel Sambuc 
641*3e1db26aSLionel Sambuc 	if (!EL_CAN_INSERT) {
642*3e1db26aSLionel Sambuc 		if (fx > 0) {
643*3e1db26aSLionel Sambuc 			osb = ols;
644*3e1db26aSLionel Sambuc 			ose = ols;
645*3e1db26aSLionel Sambuc 			nsb = nls;
646*3e1db26aSLionel Sambuc 			nse = nls;
647*3e1db26aSLionel Sambuc 		}
648*3e1db26aSLionel Sambuc 		if (sx > 0) {
649*3e1db26aSLionel Sambuc 			ols = oe;
650*3e1db26aSLionel Sambuc 			nls = ne;
651*3e1db26aSLionel Sambuc 		}
652*3e1db26aSLionel Sambuc 		if ((ols - ofd) < (nls - nfd)) {
653*3e1db26aSLionel Sambuc 			ols = oe;
654*3e1db26aSLionel Sambuc 			nls = ne;
655*3e1db26aSLionel Sambuc 		}
656*3e1db26aSLionel Sambuc 	}
657*3e1db26aSLionel Sambuc 	if (!EL_CAN_DELETE) {
658*3e1db26aSLionel Sambuc 		if (fx < 0) {
659*3e1db26aSLionel Sambuc 			osb = ols;
660*3e1db26aSLionel Sambuc 			ose = ols;
661*3e1db26aSLionel Sambuc 			nsb = nls;
662*3e1db26aSLionel Sambuc 			nse = nls;
663*3e1db26aSLionel Sambuc 		}
664*3e1db26aSLionel Sambuc 		if (sx < 0) {
665*3e1db26aSLionel Sambuc 			ols = oe;
666*3e1db26aSLionel Sambuc 			nls = ne;
667*3e1db26aSLionel Sambuc 		}
668*3e1db26aSLionel Sambuc 		if ((ols - ofd) > (nls - nfd)) {
669*3e1db26aSLionel Sambuc 			ols = oe;
670*3e1db26aSLionel Sambuc 			nls = ne;
671*3e1db26aSLionel Sambuc 		}
672*3e1db26aSLionel Sambuc 	}
673*3e1db26aSLionel Sambuc 	/*
674*3e1db26aSLionel Sambuc          * Pragmatics III: make sure the middle shifted pointers are correct if
675*3e1db26aSLionel Sambuc          * they don't point to anything (we may have moved ols or nls).
676*3e1db26aSLionel Sambuc          */
677*3e1db26aSLionel Sambuc 	/* if the change isn't worth it, don't bother */
678*3e1db26aSLionel Sambuc 	/* was: if (osb == ose) */
679*3e1db26aSLionel Sambuc 	if ((ose - osb) < MIN_END_KEEP) {
680*3e1db26aSLionel Sambuc 		osb = ols;
681*3e1db26aSLionel Sambuc 		ose = ols;
682*3e1db26aSLionel Sambuc 		nsb = nls;
683*3e1db26aSLionel Sambuc 		nse = nls;
684*3e1db26aSLionel Sambuc 	}
685*3e1db26aSLionel Sambuc 	/*
686*3e1db26aSLionel Sambuc          * Now that we are done with pragmatics we recompute fx, sx
687*3e1db26aSLionel Sambuc          */
688*3e1db26aSLionel Sambuc 	fx = (int)((nsb - nfd) - (osb - ofd));
689*3e1db26aSLionel Sambuc 	sx = (int)((nls - nse) - (ols - ose));
690*3e1db26aSLionel Sambuc 
691*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1, (__F, "fx %d, sx %d\n", fx, sx));
692*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1, (__F, "ofd %d, osb %d, ose %d, ols %d, oe %d\n",
693*3e1db26aSLionel Sambuc 		ofd - old, osb - old, ose - old, ols - old, oe - old));
694*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1, (__F, "nfd %d, nsb %d, nse %d, nls %d, ne %d\n",
695*3e1db26aSLionel Sambuc 		nfd - new, nsb - new, nse - new, nls - new, ne - new));
696*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1, (__F,
697*3e1db26aSLionel Sambuc 		"xxx-xxx:\"00000000001111111111222222222233333333334\"\r\n"));
698*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1, (__F,
699*3e1db26aSLionel Sambuc 		"xxx-xxx:\"01234567890123456789012345678901234567890\"\r\n"));
700*3e1db26aSLionel Sambuc #ifdef DEBUG_REFRESH
701*3e1db26aSLionel Sambuc 	re_printstr(el, "old- oe", old, oe);
702*3e1db26aSLionel Sambuc 	re_printstr(el, "new- ne", new, ne);
703*3e1db26aSLionel Sambuc 	re_printstr(el, "old-ofd", old, ofd);
704*3e1db26aSLionel Sambuc 	re_printstr(el, "new-nfd", new, nfd);
705*3e1db26aSLionel Sambuc 	re_printstr(el, "ofd-osb", ofd, osb);
706*3e1db26aSLionel Sambuc 	re_printstr(el, "nfd-nsb", nfd, nsb);
707*3e1db26aSLionel Sambuc 	re_printstr(el, "osb-ose", osb, ose);
708*3e1db26aSLionel Sambuc 	re_printstr(el, "nsb-nse", nsb, nse);
709*3e1db26aSLionel Sambuc 	re_printstr(el, "ose-ols", ose, ols);
710*3e1db26aSLionel Sambuc 	re_printstr(el, "nse-nls", nse, nls);
711*3e1db26aSLionel Sambuc 	re_printstr(el, "ols- oe", ols, oe);
712*3e1db26aSLionel Sambuc 	re_printstr(el, "nls- ne", nls, ne);
713*3e1db26aSLionel Sambuc #endif /* DEBUG_REFRESH */
714*3e1db26aSLionel Sambuc 
715*3e1db26aSLionel Sambuc 	/*
716*3e1db26aSLionel Sambuc          * el_cursor.v to this line i MUST be in this routine so that if we
717*3e1db26aSLionel Sambuc          * don't have to change the line, we don't move to it. el_cursor.h to
718*3e1db26aSLionel Sambuc          * first diff char
719*3e1db26aSLionel Sambuc          */
720*3e1db26aSLionel Sambuc 	terminal_move_to_line(el, i);
721*3e1db26aSLionel Sambuc 
722*3e1db26aSLionel Sambuc 	/*
723*3e1db26aSLionel Sambuc          * at this point we have something like this:
724*3e1db26aSLionel Sambuc          *
725*3e1db26aSLionel Sambuc          * /old                  /ofd    /osb               /ose    /ols     /oe
726*3e1db26aSLionel Sambuc          * v.....................v       v..................v       v........v
727*3e1db26aSLionel Sambuc          * eddie> Oh, my fredded gruntle-buggy is to me, as foo var lurgid as
728*3e1db26aSLionel Sambuc          * eddie> Oh, my fredded quiux buggy is to me, as gruntle-lurgid as
729*3e1db26aSLionel Sambuc          * ^.....................^     ^..................^       ^........^
730*3e1db26aSLionel Sambuc          * \new                  \nfd  \nsb               \nse     \nls    \ne
731*3e1db26aSLionel Sambuc          *
732*3e1db26aSLionel Sambuc          * fx is the difference in length between the chars between nfd and
733*3e1db26aSLionel Sambuc          * nsb, and the chars between ofd and osb, and is thus the number of
734*3e1db26aSLionel Sambuc          * characters to delete if < 0 (new is shorter than old, as above),
735*3e1db26aSLionel Sambuc          * or insert (new is longer than short).
736*3e1db26aSLionel Sambuc          *
737*3e1db26aSLionel Sambuc          * sx is the same for the second differences.
738*3e1db26aSLionel Sambuc          */
739*3e1db26aSLionel Sambuc 
740*3e1db26aSLionel Sambuc 	/*
741*3e1db26aSLionel Sambuc          * if we have a net insert on the first difference, AND inserting the
742*3e1db26aSLionel Sambuc          * net amount ((nsb-nfd) - (osb-ofd)) won't push the last useful
743*3e1db26aSLionel Sambuc          * character (which is ne if nls != ne, otherwise is nse) off the edge
744*3e1db26aSLionel Sambuc 	 * of the screen (el->el_terminal.t_size.h) else we do the deletes first
745*3e1db26aSLionel Sambuc 	 * so that we keep everything we need to.
746*3e1db26aSLionel Sambuc          */
747*3e1db26aSLionel Sambuc 
748*3e1db26aSLionel Sambuc 	/*
749*3e1db26aSLionel Sambuc          * if the last same is the same like the end, there is no last same
750*3e1db26aSLionel Sambuc          * part, otherwise we want to keep the last same part set p to the
751*3e1db26aSLionel Sambuc          * last useful old character
752*3e1db26aSLionel Sambuc          */
753*3e1db26aSLionel Sambuc 	p = (ols != oe) ? oe : ose;
754*3e1db26aSLionel Sambuc 
755*3e1db26aSLionel Sambuc 	/*
756*3e1db26aSLionel Sambuc          * if (There is a diffence in the beginning) && (we need to insert
757*3e1db26aSLionel Sambuc          *   characters) && (the number of characters to insert is less than
758*3e1db26aSLionel Sambuc          *   the term width)
759*3e1db26aSLionel Sambuc 	 *	We need to do an insert!
760*3e1db26aSLionel Sambuc 	 * else if (we need to delete characters)
761*3e1db26aSLionel Sambuc 	 *	We need to delete characters!
762*3e1db26aSLionel Sambuc 	 * else
763*3e1db26aSLionel Sambuc 	 *	No insert or delete
764*3e1db26aSLionel Sambuc          */
765*3e1db26aSLionel Sambuc 	if ((nsb != nfd) && fx > 0 &&
766*3e1db26aSLionel Sambuc 	    ((p - old) + fx <= el->el_terminal.t_size.h)) {
767*3e1db26aSLionel Sambuc 		ELRE_DEBUG(1,
768*3e1db26aSLionel Sambuc 		    (__F, "first diff insert at %d...\r\n", nfd - new));
769*3e1db26aSLionel Sambuc 		/*
770*3e1db26aSLionel Sambuc 		 * Move to the first char to insert, where the first diff is.
771*3e1db26aSLionel Sambuc 		 */
772*3e1db26aSLionel Sambuc 		terminal_move_to_char(el, (int)(nfd - new));
773*3e1db26aSLionel Sambuc 		/*
774*3e1db26aSLionel Sambuc 		 * Check if we have stuff to keep at end
775*3e1db26aSLionel Sambuc 		 */
776*3e1db26aSLionel Sambuc 		if (nsb != ne) {
777*3e1db26aSLionel Sambuc 			ELRE_DEBUG(1, (__F, "with stuff to keep at end\r\n"));
778*3e1db26aSLionel Sambuc 			/*
779*3e1db26aSLionel Sambuc 		         * insert fx chars of new starting at nfd
780*3e1db26aSLionel Sambuc 		         */
781*3e1db26aSLionel Sambuc 			if (fx > 0) {
782*3e1db26aSLionel Sambuc 				ELRE_DEBUG(!EL_CAN_INSERT, (__F,
783*3e1db26aSLionel Sambuc 				"ERROR: cannot insert in early first diff\n"));
784*3e1db26aSLionel Sambuc 				terminal_insertwrite(el, nfd, fx);
785*3e1db26aSLionel Sambuc 				re_insert(el, old, (int)(ofd - old),
786*3e1db26aSLionel Sambuc 				    el->el_terminal.t_size.h, nfd, fx);
787*3e1db26aSLionel Sambuc 			}
788*3e1db26aSLionel Sambuc 			/*
789*3e1db26aSLionel Sambuc 		         * write (nsb-nfd) - fx chars of new starting at
790*3e1db26aSLionel Sambuc 		         * (nfd + fx)
791*3e1db26aSLionel Sambuc 			 */
792*3e1db26aSLionel Sambuc 			len = (size_t) ((nsb - nfd) - fx);
793*3e1db26aSLionel Sambuc 			terminal_overwrite(el, (nfd + fx), len);
794*3e1db26aSLionel Sambuc 			re__strncopy(ofd + fx, nfd + fx, len);
795*3e1db26aSLionel Sambuc 		} else {
796*3e1db26aSLionel Sambuc 			ELRE_DEBUG(1, (__F, "without anything to save\r\n"));
797*3e1db26aSLionel Sambuc 			len = (size_t)(nsb - nfd);
798*3e1db26aSLionel Sambuc 			terminal_overwrite(el, nfd, len);
799*3e1db26aSLionel Sambuc 			re__strncopy(ofd, nfd, len);
800*3e1db26aSLionel Sambuc 			/*
801*3e1db26aSLionel Sambuc 		         * Done
802*3e1db26aSLionel Sambuc 		         */
803*3e1db26aSLionel Sambuc 			return;
804*3e1db26aSLionel Sambuc 		}
805*3e1db26aSLionel Sambuc 	} else if (fx < 0) {
806*3e1db26aSLionel Sambuc 		ELRE_DEBUG(1,
807*3e1db26aSLionel Sambuc 		    (__F, "first diff delete at %d...\r\n", ofd - old));
808*3e1db26aSLionel Sambuc 		/*
809*3e1db26aSLionel Sambuc 		 * move to the first char to delete where the first diff is
810*3e1db26aSLionel Sambuc 		 */
811*3e1db26aSLionel Sambuc 		terminal_move_to_char(el, (int)(ofd - old));
812*3e1db26aSLionel Sambuc 		/*
813*3e1db26aSLionel Sambuc 		 * Check if we have stuff to save
814*3e1db26aSLionel Sambuc 		 */
815*3e1db26aSLionel Sambuc 		if (osb != oe) {
816*3e1db26aSLionel Sambuc 			ELRE_DEBUG(1, (__F, "with stuff to save at end\r\n"));
817*3e1db26aSLionel Sambuc 			/*
818*3e1db26aSLionel Sambuc 		         * fx is less than zero *always* here but we check
819*3e1db26aSLionel Sambuc 		         * for code symmetry
820*3e1db26aSLionel Sambuc 		         */
821*3e1db26aSLionel Sambuc 			if (fx < 0) {
822*3e1db26aSLionel Sambuc 				ELRE_DEBUG(!EL_CAN_DELETE, (__F,
823*3e1db26aSLionel Sambuc 				    "ERROR: cannot delete in first diff\n"));
824*3e1db26aSLionel Sambuc 				terminal_deletechars(el, -fx);
825*3e1db26aSLionel Sambuc 				re_delete(el, old, (int)(ofd - old),
826*3e1db26aSLionel Sambuc 				    el->el_terminal.t_size.h, -fx);
827*3e1db26aSLionel Sambuc 			}
828*3e1db26aSLionel Sambuc 			/*
829*3e1db26aSLionel Sambuc 		         * write (nsb-nfd) chars of new starting at nfd
830*3e1db26aSLionel Sambuc 		         */
831*3e1db26aSLionel Sambuc 			len = (size_t) (nsb - nfd);
832*3e1db26aSLionel Sambuc 			terminal_overwrite(el, nfd, len);
833*3e1db26aSLionel Sambuc 			re__strncopy(ofd, nfd, len);
834*3e1db26aSLionel Sambuc 
835*3e1db26aSLionel Sambuc 		} else {
836*3e1db26aSLionel Sambuc 			ELRE_DEBUG(1, (__F,
837*3e1db26aSLionel Sambuc 			    "but with nothing left to save\r\n"));
838*3e1db26aSLionel Sambuc 			/*
839*3e1db26aSLionel Sambuc 		         * write (nsb-nfd) chars of new starting at nfd
840*3e1db26aSLionel Sambuc 		         */
841*3e1db26aSLionel Sambuc 			terminal_overwrite(el, nfd, (size_t)(nsb - nfd));
842*3e1db26aSLionel Sambuc 			re_clear_eol(el, fx, sx,
843*3e1db26aSLionel Sambuc 			    (int)((oe - old) - (ne - new)));
844*3e1db26aSLionel Sambuc 			/*
845*3e1db26aSLionel Sambuc 		         * Done
846*3e1db26aSLionel Sambuc 		         */
847*3e1db26aSLionel Sambuc 			return;
848*3e1db26aSLionel Sambuc 		}
849*3e1db26aSLionel Sambuc 	} else
850*3e1db26aSLionel Sambuc 		fx = 0;
851*3e1db26aSLionel Sambuc 
852*3e1db26aSLionel Sambuc 	if (sx < 0 && (ose - old) + fx < el->el_terminal.t_size.h) {
853*3e1db26aSLionel Sambuc 		ELRE_DEBUG(1, (__F,
854*3e1db26aSLionel Sambuc 		    "second diff delete at %d...\r\n", (ose - old) + fx));
855*3e1db26aSLionel Sambuc 		/*
856*3e1db26aSLionel Sambuc 		 * Check if we have stuff to delete
857*3e1db26aSLionel Sambuc 		 */
858*3e1db26aSLionel Sambuc 		/*
859*3e1db26aSLionel Sambuc 		 * fx is the number of characters inserted (+) or deleted (-)
860*3e1db26aSLionel Sambuc 		 */
861*3e1db26aSLionel Sambuc 
862*3e1db26aSLionel Sambuc 		terminal_move_to_char(el, (int)((ose - old) + fx));
863*3e1db26aSLionel Sambuc 		/*
864*3e1db26aSLionel Sambuc 		 * Check if we have stuff to save
865*3e1db26aSLionel Sambuc 		 */
866*3e1db26aSLionel Sambuc 		if (ols != oe) {
867*3e1db26aSLionel Sambuc 			ELRE_DEBUG(1, (__F, "with stuff to save at end\r\n"));
868*3e1db26aSLionel Sambuc 			/*
869*3e1db26aSLionel Sambuc 		         * Again a duplicate test.
870*3e1db26aSLionel Sambuc 		         */
871*3e1db26aSLionel Sambuc 			if (sx < 0) {
872*3e1db26aSLionel Sambuc 				ELRE_DEBUG(!EL_CAN_DELETE, (__F,
873*3e1db26aSLionel Sambuc 				    "ERROR: cannot delete in second diff\n"));
874*3e1db26aSLionel Sambuc 				terminal_deletechars(el, -sx);
875*3e1db26aSLionel Sambuc 			}
876*3e1db26aSLionel Sambuc 			/*
877*3e1db26aSLionel Sambuc 		         * write (nls-nse) chars of new starting at nse
878*3e1db26aSLionel Sambuc 		         */
879*3e1db26aSLionel Sambuc 			terminal_overwrite(el, nse, (size_t)(nls - nse));
880*3e1db26aSLionel Sambuc 		} else {
881*3e1db26aSLionel Sambuc 			ELRE_DEBUG(1, (__F,
882*3e1db26aSLionel Sambuc 			    "but with nothing left to save\r\n"));
883*3e1db26aSLionel Sambuc 			terminal_overwrite(el, nse, (size_t)(nls - nse));
884*3e1db26aSLionel Sambuc 			re_clear_eol(el, fx, sx,
885*3e1db26aSLionel Sambuc 			    (int)((oe - old) - (ne - new)));
886*3e1db26aSLionel Sambuc 		}
887*3e1db26aSLionel Sambuc 	}
888*3e1db26aSLionel Sambuc 	/*
889*3e1db26aSLionel Sambuc          * if we have a first insert AND WE HAVEN'T ALREADY DONE IT...
890*3e1db26aSLionel Sambuc          */
891*3e1db26aSLionel Sambuc 	if ((nsb != nfd) && (osb - ofd) <= (nsb - nfd) && (fx == 0)) {
892*3e1db26aSLionel Sambuc 		ELRE_DEBUG(1, (__F, "late first diff insert at %d...\r\n",
893*3e1db26aSLionel Sambuc 		    nfd - new));
894*3e1db26aSLionel Sambuc 
895*3e1db26aSLionel Sambuc 		terminal_move_to_char(el, (int)(nfd - new));
896*3e1db26aSLionel Sambuc 		/*
897*3e1db26aSLionel Sambuc 		 * Check if we have stuff to keep at the end
898*3e1db26aSLionel Sambuc 		 */
899*3e1db26aSLionel Sambuc 		if (nsb != ne) {
900*3e1db26aSLionel Sambuc 			ELRE_DEBUG(1, (__F, "with stuff to keep at end\r\n"));
901*3e1db26aSLionel Sambuc 			/*
902*3e1db26aSLionel Sambuc 		         * We have to recalculate fx here because we set it
903*3e1db26aSLionel Sambuc 		         * to zero above as a flag saying that we hadn't done
904*3e1db26aSLionel Sambuc 		         * an early first insert.
905*3e1db26aSLionel Sambuc 		         */
906*3e1db26aSLionel Sambuc 			fx = (int)((nsb - nfd) - (osb - ofd));
907*3e1db26aSLionel Sambuc 			if (fx > 0) {
908*3e1db26aSLionel Sambuc 				/*
909*3e1db26aSLionel Sambuc 				 * insert fx chars of new starting at nfd
910*3e1db26aSLionel Sambuc 				 */
911*3e1db26aSLionel Sambuc 				ELRE_DEBUG(!EL_CAN_INSERT, (__F,
912*3e1db26aSLionel Sambuc 				 "ERROR: cannot insert in late first diff\n"));
913*3e1db26aSLionel Sambuc 				terminal_insertwrite(el, nfd, fx);
914*3e1db26aSLionel Sambuc 				re_insert(el, old, (int)(ofd - old),
915*3e1db26aSLionel Sambuc 				    el->el_terminal.t_size.h, nfd, fx);
916*3e1db26aSLionel Sambuc 			}
917*3e1db26aSLionel Sambuc 			/*
918*3e1db26aSLionel Sambuc 		         * write (nsb-nfd) - fx chars of new starting at
919*3e1db26aSLionel Sambuc 		         * (nfd + fx)
920*3e1db26aSLionel Sambuc 			 */
921*3e1db26aSLionel Sambuc 			len = (size_t) ((nsb - nfd) - fx);
922*3e1db26aSLionel Sambuc 			terminal_overwrite(el, (nfd + fx), len);
923*3e1db26aSLionel Sambuc 			re__strncopy(ofd + fx, nfd + fx, len);
924*3e1db26aSLionel Sambuc 		} else {
925*3e1db26aSLionel Sambuc 			ELRE_DEBUG(1, (__F, "without anything to save\r\n"));
926*3e1db26aSLionel Sambuc 			len = (size_t) (nsb - nfd);
927*3e1db26aSLionel Sambuc 			terminal_overwrite(el, nfd, len);
928*3e1db26aSLionel Sambuc 			re__strncopy(ofd, nfd, len);
929*3e1db26aSLionel Sambuc 		}
930*3e1db26aSLionel Sambuc 	}
931*3e1db26aSLionel Sambuc 	/*
932*3e1db26aSLionel Sambuc          * line is now NEW up to nse
933*3e1db26aSLionel Sambuc          */
934*3e1db26aSLionel Sambuc 	if (sx >= 0) {
935*3e1db26aSLionel Sambuc 		ELRE_DEBUG(1, (__F,
936*3e1db26aSLionel Sambuc 		    "second diff insert at %d...\r\n", (int)(nse - new)));
937*3e1db26aSLionel Sambuc 		terminal_move_to_char(el, (int)(nse - new));
938*3e1db26aSLionel Sambuc 		if (ols != oe) {
939*3e1db26aSLionel Sambuc 			ELRE_DEBUG(1, (__F, "with stuff to keep at end\r\n"));
940*3e1db26aSLionel Sambuc 			if (sx > 0) {
941*3e1db26aSLionel Sambuc 				/* insert sx chars of new starting at nse */
942*3e1db26aSLionel Sambuc 				ELRE_DEBUG(!EL_CAN_INSERT, (__F,
943*3e1db26aSLionel Sambuc 				    "ERROR: cannot insert in second diff\n"));
944*3e1db26aSLionel Sambuc 				terminal_insertwrite(el, nse, sx);
945*3e1db26aSLionel Sambuc 			}
946*3e1db26aSLionel Sambuc 			/*
947*3e1db26aSLionel Sambuc 		         * write (nls-nse) - sx chars of new starting at
948*3e1db26aSLionel Sambuc 			 * (nse + sx)
949*3e1db26aSLionel Sambuc 		         */
950*3e1db26aSLionel Sambuc 			terminal_overwrite(el, (nse + sx),
951*3e1db26aSLionel Sambuc 			    (size_t)((nls - nse) - sx));
952*3e1db26aSLionel Sambuc 		} else {
953*3e1db26aSLionel Sambuc 			ELRE_DEBUG(1, (__F, "without anything to save\r\n"));
954*3e1db26aSLionel Sambuc 			terminal_overwrite(el, nse, (size_t)(nls - nse));
955*3e1db26aSLionel Sambuc 
956*3e1db26aSLionel Sambuc 			/*
957*3e1db26aSLionel Sambuc 	                 * No need to do a clear-to-end here because we were
958*3e1db26aSLionel Sambuc 	                 * doing a second insert, so we will have over
959*3e1db26aSLionel Sambuc 	                 * written all of the old string.
960*3e1db26aSLionel Sambuc 		         */
961*3e1db26aSLionel Sambuc 		}
962*3e1db26aSLionel Sambuc 	}
963*3e1db26aSLionel Sambuc 	ELRE_DEBUG(1, (__F, "done.\r\n"));
964*3e1db26aSLionel Sambuc }
965*3e1db26aSLionel Sambuc 
966*3e1db26aSLionel Sambuc 
967*3e1db26aSLionel Sambuc /* re__copy_and_pad():
968*3e1db26aSLionel Sambuc  *	Copy string and pad with spaces
969*3e1db26aSLionel Sambuc  */
970*3e1db26aSLionel Sambuc private void
re__copy_and_pad(Char * dst,const Char * src,size_t width)971*3e1db26aSLionel Sambuc re__copy_and_pad(Char *dst, const Char *src, size_t width)
972*3e1db26aSLionel Sambuc {
973*3e1db26aSLionel Sambuc 	size_t i;
974*3e1db26aSLionel Sambuc 
975*3e1db26aSLionel Sambuc 	for (i = 0; i < width; i++) {
976*3e1db26aSLionel Sambuc 		if (*src == '\0')
977*3e1db26aSLionel Sambuc 			break;
978*3e1db26aSLionel Sambuc 		*dst++ = *src++;
979*3e1db26aSLionel Sambuc 	}
980*3e1db26aSLionel Sambuc 
981*3e1db26aSLionel Sambuc 	for (; i < width; i++)
982*3e1db26aSLionel Sambuc 		*dst++ = ' ';
983*3e1db26aSLionel Sambuc 
984*3e1db26aSLionel Sambuc 	*dst = '\0';
985*3e1db26aSLionel Sambuc }
986*3e1db26aSLionel Sambuc 
987*3e1db26aSLionel Sambuc 
988*3e1db26aSLionel Sambuc /* re_refresh_cursor():
989*3e1db26aSLionel Sambuc  *	Move to the new cursor position
990*3e1db26aSLionel Sambuc  */
991*3e1db26aSLionel Sambuc protected void
re_refresh_cursor(EditLine * el)992*3e1db26aSLionel Sambuc re_refresh_cursor(EditLine *el)
993*3e1db26aSLionel Sambuc {
994*3e1db26aSLionel Sambuc 	Char *cp;
995*3e1db26aSLionel Sambuc 	int h, v, th, w;
996*3e1db26aSLionel Sambuc 
997*3e1db26aSLionel Sambuc 	if (el->el_line.cursor >= el->el_line.lastchar) {
998*3e1db26aSLionel Sambuc 		if (el->el_map.current == el->el_map.alt
999*3e1db26aSLionel Sambuc 		    && el->el_line.lastchar != el->el_line.buffer)
1000*3e1db26aSLionel Sambuc 			el->el_line.cursor = el->el_line.lastchar - 1;
1001*3e1db26aSLionel Sambuc 		else
1002*3e1db26aSLionel Sambuc 			el->el_line.cursor = el->el_line.lastchar;
1003*3e1db26aSLionel Sambuc 	}
1004*3e1db26aSLionel Sambuc 
1005*3e1db26aSLionel Sambuc 	/* first we must find where the cursor is... */
1006*3e1db26aSLionel Sambuc 	h = el->el_prompt.p_pos.h;
1007*3e1db26aSLionel Sambuc 	v = el->el_prompt.p_pos.v;
1008*3e1db26aSLionel Sambuc 	th = el->el_terminal.t_size.h;	/* optimize for speed */
1009*3e1db26aSLionel Sambuc 
1010*3e1db26aSLionel Sambuc 	/* do input buffer to el->el_line.cursor */
1011*3e1db26aSLionel Sambuc 	for (cp = el->el_line.buffer; cp < el->el_line.cursor; cp++) {
1012*3e1db26aSLionel Sambuc                 switch (ct_chr_class(*cp)) {
1013*3e1db26aSLionel Sambuc 		case CHTYPE_NL:  /* handle newline in data part too */
1014*3e1db26aSLionel Sambuc 			h = 0;
1015*3e1db26aSLionel Sambuc 			v++;
1016*3e1db26aSLionel Sambuc 			break;
1017*3e1db26aSLionel Sambuc 		case CHTYPE_TAB: /* if a tab, to next tab stop */
1018*3e1db26aSLionel Sambuc 			while (++h & 07)
1019*3e1db26aSLionel Sambuc 				continue;
1020*3e1db26aSLionel Sambuc 			break;
1021*3e1db26aSLionel Sambuc 		default:
1022*3e1db26aSLionel Sambuc 			w = Width(*cp);
1023*3e1db26aSLionel Sambuc 			if (w > 1 && h + w > th) { /* won't fit on line */
1024*3e1db26aSLionel Sambuc 				h = 0;
1025*3e1db26aSLionel Sambuc 				v++;
1026*3e1db26aSLionel Sambuc 			}
1027*3e1db26aSLionel Sambuc 			h += ct_visual_width(*cp);
1028*3e1db26aSLionel Sambuc 			break;
1029*3e1db26aSLionel Sambuc                 }
1030*3e1db26aSLionel Sambuc 
1031*3e1db26aSLionel Sambuc 		if (h >= th) {	/* check, extra long tabs picked up here also */
1032*3e1db26aSLionel Sambuc 			h -= th;
1033*3e1db26aSLionel Sambuc 			v++;
1034*3e1db26aSLionel Sambuc 		}
1035*3e1db26aSLionel Sambuc 	}
1036*3e1db26aSLionel Sambuc         /* if we have a next character, and it's a doublewidth one, we need to
1037*3e1db26aSLionel Sambuc          * check whether we need to linebreak for it to fit */
1038*3e1db26aSLionel Sambuc         if (cp < el->el_line.lastchar && (w = Width(*cp)) > 1)
1039*3e1db26aSLionel Sambuc                 if (h + w > th) {
1040*3e1db26aSLionel Sambuc                     h = 0;
1041*3e1db26aSLionel Sambuc                     v++;
1042*3e1db26aSLionel Sambuc                 }
1043*3e1db26aSLionel Sambuc 
1044*3e1db26aSLionel Sambuc 	/* now go there */
1045*3e1db26aSLionel Sambuc 	terminal_move_to_line(el, v);
1046*3e1db26aSLionel Sambuc 	terminal_move_to_char(el, h);
1047*3e1db26aSLionel Sambuc 	terminal__flush(el);
1048*3e1db26aSLionel Sambuc }
1049*3e1db26aSLionel Sambuc 
1050*3e1db26aSLionel Sambuc 
1051*3e1db26aSLionel Sambuc /* re_fastputc():
1052*3e1db26aSLionel Sambuc  *	Add a character fast.
1053*3e1db26aSLionel Sambuc  */
1054*3e1db26aSLionel Sambuc private void
re_fastputc(EditLine * el,Int c)1055*3e1db26aSLionel Sambuc re_fastputc(EditLine *el, Int c)
1056*3e1db26aSLionel Sambuc {
1057*3e1db26aSLionel Sambuc 	int w = Width((Char)c);
1058*3e1db26aSLionel Sambuc 	while (w > 1 && el->el_cursor.h + w > el->el_terminal.t_size.h)
1059*3e1db26aSLionel Sambuc 	    re_fastputc(el, ' ');
1060*3e1db26aSLionel Sambuc 
1061*3e1db26aSLionel Sambuc 	terminal__putc(el, c);
1062*3e1db26aSLionel Sambuc 	el->el_display[el->el_cursor.v][el->el_cursor.h++] = c;
1063*3e1db26aSLionel Sambuc 	while (--w > 0)
1064*3e1db26aSLionel Sambuc 		el->el_display[el->el_cursor.v][el->el_cursor.h++]
1065*3e1db26aSLionel Sambuc 			= MB_FILL_CHAR;
1066*3e1db26aSLionel Sambuc 
1067*3e1db26aSLionel Sambuc 	if (el->el_cursor.h >= el->el_terminal.t_size.h) {
1068*3e1db26aSLionel Sambuc 		/* if we must overflow */
1069*3e1db26aSLionel Sambuc 		el->el_cursor.h = 0;
1070*3e1db26aSLionel Sambuc 
1071*3e1db26aSLionel Sambuc 		/*
1072*3e1db26aSLionel Sambuc 		 * If we would overflow (input is longer than terminal size),
1073*3e1db26aSLionel Sambuc 		 * emulate scroll by dropping first line and shuffling the rest.
1074*3e1db26aSLionel Sambuc 		 * We do this via pointer shuffling - it's safe in this case
1075*3e1db26aSLionel Sambuc 		 * and we avoid memcpy().
1076*3e1db26aSLionel Sambuc 		 */
1077*3e1db26aSLionel Sambuc 		if (el->el_cursor.v + 1 >= el->el_terminal.t_size.v) {
1078*3e1db26aSLionel Sambuc 			int i, lins = el->el_terminal.t_size.v;
1079*3e1db26aSLionel Sambuc 			Char *firstline = el->el_display[0];
1080*3e1db26aSLionel Sambuc 
1081*3e1db26aSLionel Sambuc 			for(i = 1; i < lins; i++)
1082*3e1db26aSLionel Sambuc 				el->el_display[i - 1] = el->el_display[i];
1083*3e1db26aSLionel Sambuc 
1084*3e1db26aSLionel Sambuc 			re__copy_and_pad(firstline, STR(""), (size_t)0);
1085*3e1db26aSLionel Sambuc 			el->el_display[i - 1] = firstline;
1086*3e1db26aSLionel Sambuc 		} else {
1087*3e1db26aSLionel Sambuc 			el->el_cursor.v++;
1088*3e1db26aSLionel Sambuc 			el->el_refresh.r_oldcv++;
1089*3e1db26aSLionel Sambuc 		}
1090*3e1db26aSLionel Sambuc 		if (EL_HAS_AUTO_MARGINS) {
1091*3e1db26aSLionel Sambuc 			if (EL_HAS_MAGIC_MARGINS) {
1092*3e1db26aSLionel Sambuc 				terminal__putc(el, ' ');
1093*3e1db26aSLionel Sambuc 				terminal__putc(el, '\b');
1094*3e1db26aSLionel Sambuc 			}
1095*3e1db26aSLionel Sambuc 		} else {
1096*3e1db26aSLionel Sambuc 			terminal__putc(el, '\r');
1097*3e1db26aSLionel Sambuc 			terminal__putc(el, '\n');
1098*3e1db26aSLionel Sambuc 		}
1099*3e1db26aSLionel Sambuc 	}
1100*3e1db26aSLionel Sambuc }
1101*3e1db26aSLionel Sambuc 
1102*3e1db26aSLionel Sambuc 
1103*3e1db26aSLionel Sambuc /* re_fastaddc():
1104*3e1db26aSLionel Sambuc  *	we added just one char, handle it fast.
1105*3e1db26aSLionel Sambuc  *	Assumes that screen cursor == real cursor
1106*3e1db26aSLionel Sambuc  */
1107*3e1db26aSLionel Sambuc protected void
re_fastaddc(EditLine * el)1108*3e1db26aSLionel Sambuc re_fastaddc(EditLine *el)
1109*3e1db26aSLionel Sambuc {
1110*3e1db26aSLionel Sambuc 	Char c;
1111*3e1db26aSLionel Sambuc 	int rhdiff;
1112*3e1db26aSLionel Sambuc 
1113*3e1db26aSLionel Sambuc 	c = el->el_line.cursor[-1];
1114*3e1db26aSLionel Sambuc 
1115*3e1db26aSLionel Sambuc 	if (c == '\t' || el->el_line.cursor != el->el_line.lastchar) {
1116*3e1db26aSLionel Sambuc 		re_refresh(el);	/* too hard to handle */
1117*3e1db26aSLionel Sambuc 		return;
1118*3e1db26aSLionel Sambuc 	}
1119*3e1db26aSLionel Sambuc 	rhdiff = el->el_terminal.t_size.h - el->el_cursor.h -
1120*3e1db26aSLionel Sambuc 	    el->el_rprompt.p_pos.h;
1121*3e1db26aSLionel Sambuc 	if (el->el_rprompt.p_pos.h && rhdiff < 3) {
1122*3e1db26aSLionel Sambuc 		re_refresh(el);	/* clear out rprompt if less than 1 char gap */
1123*3e1db26aSLionel Sambuc 		return;
1124*3e1db26aSLionel Sambuc 	}			/* else (only do at end of line, no TAB) */
1125*3e1db26aSLionel Sambuc 	switch (ct_chr_class(c)) {
1126*3e1db26aSLionel Sambuc 	case CHTYPE_TAB: /* already handled, should never happen here */
1127*3e1db26aSLionel Sambuc 		break;
1128*3e1db26aSLionel Sambuc 	case CHTYPE_NL:
1129*3e1db26aSLionel Sambuc 	case CHTYPE_PRINT:
1130*3e1db26aSLionel Sambuc 		re_fastputc(el, c);
1131*3e1db26aSLionel Sambuc 		break;
1132*3e1db26aSLionel Sambuc 	case CHTYPE_ASCIICTL:
1133*3e1db26aSLionel Sambuc 	case CHTYPE_NONPRINT: {
1134*3e1db26aSLionel Sambuc 		Char visbuf[VISUAL_WIDTH_MAX];
1135*3e1db26aSLionel Sambuc 		ssize_t i, n =
1136*3e1db26aSLionel Sambuc 		    ct_visual_char(visbuf, VISUAL_WIDTH_MAX, (Char)c);
1137*3e1db26aSLionel Sambuc 		for (i = 0; n-- > 0; ++i)
1138*3e1db26aSLionel Sambuc 			re_fastputc(el, visbuf[i]);
1139*3e1db26aSLionel Sambuc 		break;
1140*3e1db26aSLionel Sambuc 	}
1141*3e1db26aSLionel Sambuc 	}
1142*3e1db26aSLionel Sambuc 	terminal__flush(el);
1143*3e1db26aSLionel Sambuc }
1144*3e1db26aSLionel Sambuc 
1145*3e1db26aSLionel Sambuc 
1146*3e1db26aSLionel Sambuc /* re_clear_display():
1147*3e1db26aSLionel Sambuc  *	clear the screen buffers so that new new prompt starts fresh.
1148*3e1db26aSLionel Sambuc  */
1149*3e1db26aSLionel Sambuc protected void
re_clear_display(EditLine * el)1150*3e1db26aSLionel Sambuc re_clear_display(EditLine *el)
1151*3e1db26aSLionel Sambuc {
1152*3e1db26aSLionel Sambuc 	int i;
1153*3e1db26aSLionel Sambuc 
1154*3e1db26aSLionel Sambuc 	el->el_cursor.v = 0;
1155*3e1db26aSLionel Sambuc 	el->el_cursor.h = 0;
1156*3e1db26aSLionel Sambuc 	for (i = 0; i < el->el_terminal.t_size.v; i++)
1157*3e1db26aSLionel Sambuc 		el->el_display[i][0] = '\0';
1158*3e1db26aSLionel Sambuc 	el->el_refresh.r_oldcv = 0;
1159*3e1db26aSLionel Sambuc }
1160*3e1db26aSLionel Sambuc 
1161*3e1db26aSLionel Sambuc 
1162*3e1db26aSLionel Sambuc /* re_clear_lines():
1163*3e1db26aSLionel Sambuc  *	Make sure all lines are *really* blank
1164*3e1db26aSLionel Sambuc  */
1165*3e1db26aSLionel Sambuc protected void
re_clear_lines(EditLine * el)1166*3e1db26aSLionel Sambuc re_clear_lines(EditLine *el)
1167*3e1db26aSLionel Sambuc {
1168*3e1db26aSLionel Sambuc 
1169*3e1db26aSLionel Sambuc 	if (EL_CAN_CEOL) {
1170*3e1db26aSLionel Sambuc 		int i;
1171*3e1db26aSLionel Sambuc 		for (i = el->el_refresh.r_oldcv; i >= 0; i--) {
1172*3e1db26aSLionel Sambuc 			/* for each line on the screen */
1173*3e1db26aSLionel Sambuc 			terminal_move_to_line(el, i);
1174*3e1db26aSLionel Sambuc 			terminal_move_to_char(el, 0);
1175*3e1db26aSLionel Sambuc 			terminal_clear_EOL(el, el->el_terminal.t_size.h);
1176*3e1db26aSLionel Sambuc 		}
1177*3e1db26aSLionel Sambuc 	} else {
1178*3e1db26aSLionel Sambuc 		terminal_move_to_line(el, el->el_refresh.r_oldcv);
1179*3e1db26aSLionel Sambuc 					/* go to last line */
1180*3e1db26aSLionel Sambuc 		terminal__putc(el, '\r');	/* go to BOL */
1181*3e1db26aSLionel Sambuc 		terminal__putc(el, '\n');	/* go to new line */
1182*3e1db26aSLionel Sambuc 	}
1183*3e1db26aSLionel Sambuc }
1184