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