xref: /minix3/lib/libcurses/refresh.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: refresh.c,v 1.79 2014/02/20 09:42:42 blymn Exp $	*/
2b7061124SArun Thomas 
351ffecc1SBen Gras /*
451ffecc1SBen Gras  * Copyright (c) 1981, 1993, 1994
551ffecc1SBen Gras  *	The Regents of the University of California.  All rights reserved.
651ffecc1SBen Gras  *
751ffecc1SBen Gras  * Redistribution and use in source and binary forms, with or without
851ffecc1SBen Gras  * modification, are permitted provided that the following conditions
951ffecc1SBen Gras  * are met:
1051ffecc1SBen Gras  * 1. Redistributions of source code must retain the above copyright
1151ffecc1SBen Gras  *    notice, this list of conditions and the following disclaimer.
1251ffecc1SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
1351ffecc1SBen Gras  *    notice, this list of conditions and the following disclaimer in the
1451ffecc1SBen Gras  *    documentation and/or other materials provided with the distribution.
1551ffecc1SBen Gras  * 3. Neither the name of the University nor the names of its contributors
1651ffecc1SBen Gras  *    may be used to endorse or promote products derived from this software
1751ffecc1SBen Gras  *    without specific prior written permission.
1851ffecc1SBen Gras  *
1951ffecc1SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2051ffecc1SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2151ffecc1SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2251ffecc1SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2351ffecc1SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2451ffecc1SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2551ffecc1SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2651ffecc1SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2751ffecc1SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2851ffecc1SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2951ffecc1SBen Gras  * SUCH DAMAGE.
3051ffecc1SBen Gras  */
31b7061124SArun Thomas 
3251ffecc1SBen Gras #include <sys/cdefs.h>
3351ffecc1SBen Gras #ifndef lint
3451ffecc1SBen Gras #if 0
3551ffecc1SBen Gras static char sccsid[] = "@(#)refresh.c	8.7 (Berkeley) 8/13/94";
3651ffecc1SBen Gras #else
37*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: refresh.c,v 1.79 2014/02/20 09:42:42 blymn Exp $");
3851ffecc1SBen Gras #endif
3951ffecc1SBen Gras #endif				/* not lint */
4051ffecc1SBen Gras 
4151ffecc1SBen Gras #include <stdlib.h>
4251ffecc1SBen Gras #include <string.h>
4351ffecc1SBen Gras 
4451ffecc1SBen Gras #include "curses.h"
4551ffecc1SBen Gras #include "curses_private.h"
4651ffecc1SBen Gras 
4751ffecc1SBen Gras static void	domvcur(int, int, int, int);
4851ffecc1SBen Gras static int	makech(int);
4951ffecc1SBen Gras static void	quickch(void);
5051ffecc1SBen Gras static void	scrolln(int, int, int, int, int);
5151ffecc1SBen Gras 
5251ffecc1SBen Gras static int _cursesi_wnoutrefresh(SCREEN *, WINDOW *,
5351ffecc1SBen Gras 				 int, int, int, int, int, int);
5451ffecc1SBen Gras 
5551ffecc1SBen Gras #ifdef HAVE_WCHAR
5651ffecc1SBen Gras int cellcmp( __LDATA *, __LDATA * );
5751ffecc1SBen Gras int linecmp( __LDATA *, __LDATA *, size_t );
5851ffecc1SBen Gras #endif /* HAVE_WCHAR */
5951ffecc1SBen Gras 
6051ffecc1SBen Gras #ifndef _CURSES_USE_MACROS
6151ffecc1SBen Gras 
6251ffecc1SBen Gras /*
6351ffecc1SBen Gras  * refresh --
6451ffecc1SBen Gras  *	Make the current screen look like "stdscr" over the area covered by
6551ffecc1SBen Gras  *	stdscr.
6651ffecc1SBen Gras  */
6751ffecc1SBen Gras int
refresh(void)6851ffecc1SBen Gras refresh(void)
69b7061124SArun Thomas {
7051ffecc1SBen Gras 	return wrefresh(stdscr);
71b7061124SArun Thomas }
72b7061124SArun Thomas 
7351ffecc1SBen Gras #endif
74b7061124SArun Thomas 
7551ffecc1SBen Gras /*
7651ffecc1SBen Gras  * wnoutrefresh --
7751ffecc1SBen Gras  *	Add the contents of "win" to the virtual window.
7851ffecc1SBen Gras  */
7951ffecc1SBen Gras int
wnoutrefresh(WINDOW * win)8051ffecc1SBen Gras wnoutrefresh(WINDOW *win)
81b7061124SArun Thomas {
8251ffecc1SBen Gras #ifdef DEBUG
8351ffecc1SBen Gras 	__CTRACE(__CTRACE_REFRESH, "wnoutrefresh: win %p\n", win);
8451ffecc1SBen Gras #endif
85b7061124SArun Thomas 
8651ffecc1SBen Gras 	return _cursesi_wnoutrefresh(_cursesi_screen, win, 0, 0, win->begy,
8751ffecc1SBen Gras 	    win->begx, win->maxy, win->maxx);
8851ffecc1SBen Gras }
89b7061124SArun Thomas 
9051ffecc1SBen Gras /*
9151ffecc1SBen Gras  * pnoutrefresh --
9251ffecc1SBen Gras  *	Add the contents of "pad" to the virtual window.
9351ffecc1SBen Gras  */
9451ffecc1SBen Gras int
pnoutrefresh(WINDOW * pad,int pbegy,int pbegx,int sbegy,int sbegx,int smaxy,int smaxx)9551ffecc1SBen Gras pnoutrefresh(WINDOW *pad, int pbegy, int pbegx, int sbegy, int sbegx,
9651ffecc1SBen Gras 	int smaxy, int smaxx)
9751ffecc1SBen Gras {
9851ffecc1SBen Gras 	int pmaxy, pmaxx;
99b7061124SArun Thomas 
10051ffecc1SBen Gras #ifdef DEBUG
10151ffecc1SBen Gras 	__CTRACE(__CTRACE_REFRESH, "pnoutrefresh: pad %p, flags 0x%08x\n",
10251ffecc1SBen Gras 	    pad, pad->flags);
10351ffecc1SBen Gras 	__CTRACE(__CTRACE_REFRESH,
10451ffecc1SBen Gras 	    "pnoutrefresh: (%d, %d), (%d, %d), (%d, %d)\n",
10551ffecc1SBen Gras 	    pbegy, pbegx, sbegy, sbegx, smaxy, smaxx);
10651ffecc1SBen Gras #endif
107b7061124SArun Thomas 
10851ffecc1SBen Gras 	/* SUS says if these are negative, they should be treated as zero */
10951ffecc1SBen Gras 	if (pbegy < 0)
11051ffecc1SBen Gras 		pbegy = 0;
11151ffecc1SBen Gras 	if (pbegx < 0)
11251ffecc1SBen Gras 		pbegx = 0;
11351ffecc1SBen Gras 	if (sbegy < 0)
11451ffecc1SBen Gras 		sbegy = 0;
11551ffecc1SBen Gras 	if (sbegx < 0)
11651ffecc1SBen Gras 		sbegx = 0;
117b7061124SArun Thomas 
11851ffecc1SBen Gras 	/* Calculate rectangle on pad - used by _cursesi_wnoutrefresh */
11951ffecc1SBen Gras 	pmaxy = pbegy + smaxy - sbegy + 1;
12051ffecc1SBen Gras 	pmaxx = pbegx + smaxx - sbegx + 1;
121b7061124SArun Thomas 
12251ffecc1SBen Gras 	/* Check rectangle fits in pad */
12351ffecc1SBen Gras 	if (pmaxy > pad->maxy - pad->begy)
12451ffecc1SBen Gras 		pmaxy = pad->maxy - pad->begy;
12551ffecc1SBen Gras 	if (pmaxx > pad->maxx - pad->begx)
12651ffecc1SBen Gras 		pmaxx = pad->maxx - pad->begx;
127b7061124SArun Thomas 
12851ffecc1SBen Gras 	if (smaxy - sbegy < 0 || smaxx - sbegx < 0 )
12951ffecc1SBen Gras 		return ERR;
13051ffecc1SBen Gras 
13151ffecc1SBen Gras 	return _cursesi_wnoutrefresh(_cursesi_screen, pad,
13251ffecc1SBen Gras 	    pad->begy + pbegy, pad->begx + pbegx, pad->begy + sbegy,
13351ffecc1SBen Gras 	    pad->begx + sbegx, pmaxy, pmaxx);
13451ffecc1SBen Gras }
13551ffecc1SBen Gras 
13651ffecc1SBen Gras /*
13751ffecc1SBen Gras  * _cursesi_wnoutrefresh --
13851ffecc1SBen Gras  *	Does the grunt work for wnoutrefresh to the given screen.
13951ffecc1SBen Gras  *	Copies the part of the window given by the rectangle
14051ffecc1SBen Gras  *	(begy, begx) to (maxy, maxx) at screen position (wbegy, wbegx).
14151ffecc1SBen Gras  */
14251ffecc1SBen Gras int
_cursesi_wnoutrefresh(SCREEN * screen,WINDOW * win,int begy,int begx,int wbegy,int wbegx,int maxy,int maxx)14351ffecc1SBen Gras _cursesi_wnoutrefresh(SCREEN *screen, WINDOW *win, int begy, int begx,
14451ffecc1SBen Gras 	int wbegy, int wbegx, int maxy, int maxx)
14551ffecc1SBen Gras {
14651ffecc1SBen Gras 
147*0a6a1f1dSLionel Sambuc 	short	sy, wy, wx, y_off, x_off, mx, dy_off, dx_off, endy;
148*0a6a1f1dSLionel Sambuc 	__LINE	*wlp, *vlp, *dwlp;
149*0a6a1f1dSLionel Sambuc 	WINDOW	*sub_win, *orig, *swin, *dwin;
15051ffecc1SBen Gras 
15151ffecc1SBen Gras #ifdef DEBUG
15251ffecc1SBen Gras 	__CTRACE(__CTRACE_REFRESH, "_wnoutrefresh: win %p, flags 0x%08x\n",
15351ffecc1SBen Gras 	    win, win->flags);
15451ffecc1SBen Gras 	__CTRACE(__CTRACE_REFRESH,
15551ffecc1SBen Gras 	    "_wnoutrefresh: (%d, %d), (%d, %d), (%d, %d)\n",
15651ffecc1SBen Gras 	    begy, begx, wbegy, wbegx, maxy, maxx);
15751ffecc1SBen Gras #endif
15851ffecc1SBen Gras 
15951ffecc1SBen Gras 	if (screen->curwin)
16051ffecc1SBen Gras 		return OK;
16151ffecc1SBen Gras 
162*0a6a1f1dSLionel Sambuc 	swin = dwin = win;
163*0a6a1f1dSLionel Sambuc 	if (win->flags & __ISDERWIN)
164*0a6a1f1dSLionel Sambuc 		swin = win->orig;
165*0a6a1f1dSLionel Sambuc 
16651ffecc1SBen Gras 	/*
16751ffecc1SBen Gras 	 * Recurse through any sub-windows, mark as dirty lines on the parent
16851ffecc1SBen Gras 	 * window that are dirty on the sub-window and clear the dirty flag on
16951ffecc1SBen Gras 	 * the sub-window.
17051ffecc1SBen Gras 	 */
171*0a6a1f1dSLionel Sambuc 	if (dwin->orig == 0) {
172*0a6a1f1dSLionel Sambuc 		orig = dwin;
173*0a6a1f1dSLionel Sambuc 		for (sub_win = dwin->nextp; sub_win != orig;
17451ffecc1SBen Gras 		    sub_win = sub_win->nextp) {
175*0a6a1f1dSLionel Sambuc 			if (sub_win->flags & __ISDERWIN)
176*0a6a1f1dSLionel Sambuc 				continue;
17751ffecc1SBen Gras #ifdef DEBUG
17851ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH,
17951ffecc1SBen Gras 			    "wnout_refresh: win %p, sub_win %p\n",
18051ffecc1SBen Gras 			    orig, sub_win);
18151ffecc1SBen Gras #endif
18251ffecc1SBen Gras 			for (sy = 0; sy < sub_win->maxy; sy++) {
1830c3ae37fSLionel Sambuc 				if (sub_win->alines[sy]->flags & __ISDIRTY) {
18451ffecc1SBen Gras 					orig->alines[sy + sub_win->begy - orig->begy]->flags
18551ffecc1SBen Gras 					    |= __ISDIRTY;
18651ffecc1SBen Gras 					sub_win->alines[sy]->flags
18751ffecc1SBen Gras 					    &= ~__ISDIRTY;
18851ffecc1SBen Gras 				}
189*0a6a1f1dSLionel Sambuc 				if (sub_win->alines[sy]->flags & __ISFORCED) {
190*0a6a1f1dSLionel Sambuc 					orig->alines[sy + sub_win->begy - orig->begy]->flags
191*0a6a1f1dSLionel Sambuc 					    |= __ISFORCED;
192*0a6a1f1dSLionel Sambuc 					sub_win->alines[sy]->flags
193*0a6a1f1dSLionel Sambuc 					    &= ~__ISFORCED;
194*0a6a1f1dSLionel Sambuc 				}
19551ffecc1SBen Gras 			}
19651ffecc1SBen Gras 		}
19751ffecc1SBen Gras 	}
19851ffecc1SBen Gras 
19951ffecc1SBen Gras 	/* Check that cursor position on "win" is valid for "__virtscr" */
200*0a6a1f1dSLionel Sambuc 	if (dwin->cury + wbegy - begy < screen->__virtscr->maxy &&
201*0a6a1f1dSLionel Sambuc 	    dwin->cury + wbegy - begy >= 0 && dwin->cury < maxy - begy)
202*0a6a1f1dSLionel Sambuc 		screen->__virtscr->cury = dwin->cury + wbegy - begy;
203*0a6a1f1dSLionel Sambuc 	if (dwin->curx + wbegx - begx < screen->__virtscr->maxx &&
204*0a6a1f1dSLionel Sambuc 	    dwin->curx + wbegx - begx >= 0 && dwin->curx < maxx - begx)
205*0a6a1f1dSLionel Sambuc 		screen->__virtscr->curx = dwin->curx + wbegx - begx;
20651ffecc1SBen Gras 
20751ffecc1SBen Gras 	/* Copy the window flags from "win" to "__virtscr" */
208*0a6a1f1dSLionel Sambuc 	if (dwin->flags & __CLEAROK) {
209*0a6a1f1dSLionel Sambuc 		if (dwin->flags & __FULLWIN)
21051ffecc1SBen Gras 			screen->__virtscr->flags |= __CLEAROK;
211*0a6a1f1dSLionel Sambuc 		dwin->flags &= ~__CLEAROK;
21251ffecc1SBen Gras 	}
21351ffecc1SBen Gras 	screen->__virtscr->flags &= ~__LEAVEOK;
214*0a6a1f1dSLionel Sambuc 	screen->__virtscr->flags |= dwin->flags;
21551ffecc1SBen Gras 
216*0a6a1f1dSLionel Sambuc 	if ((dwin->flags & __ISDERWIN) != 0)
217*0a6a1f1dSLionel Sambuc 		endy = begy + maxy;
218*0a6a1f1dSLionel Sambuc 	else
219*0a6a1f1dSLionel Sambuc 		endy = maxy;
220*0a6a1f1dSLionel Sambuc 
221*0a6a1f1dSLionel Sambuc 	for (wy = begy, y_off = wbegy, dy_off = 0; wy < endy &&
222*0a6a1f1dSLionel Sambuc 	    y_off < screen->__virtscr->maxy; wy++, y_off++, dy_off++) {
223*0a6a1f1dSLionel Sambuc 		wlp = swin->alines[wy];
224*0a6a1f1dSLionel Sambuc 		dwlp = dwin->alines[dy_off];
22551ffecc1SBen Gras #ifdef DEBUG
22651ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH,
22751ffecc1SBen Gras 		    "_wnoutrefresh: wy %d\tf %d\tl %d\tflags %x\n",
22851ffecc1SBen Gras 		    wy, *wlp->firstchp, *wlp->lastchp, wlp->flags);
229*0a6a1f1dSLionel Sambuc 
230*0a6a1f1dSLionel Sambuc 		if ((dwin->flags & __ISDERWIN) != 0) {
231*0a6a1f1dSLionel Sambuc 			__CTRACE(__CTRACE_REFRESH,
232*0a6a1f1dSLionel Sambuc 		    	"_wnoutrefresh: derwin wy %d\tf %d\tl %d\tflags %x\n",
233*0a6a1f1dSLionel Sambuc 		    	dy_off, *dwlp->firstchp, *dwlp->lastchp, dwlp->flags);
234*0a6a1f1dSLionel Sambuc 			__CTRACE(__CTRACE_REFRESH,
235*0a6a1f1dSLionel Sambuc 			"_wnoutrefresh: derwin maxx %d\tch_off %d\n",
236*0a6a1f1dSLionel Sambuc 			dwin->maxx, dwin->ch_off);
237*0a6a1f1dSLionel Sambuc 		}
23851ffecc1SBen Gras #endif
239*0a6a1f1dSLionel Sambuc 		if (((wlp->flags & (__ISDIRTY | __ISFORCED)) == 0) &&
240*0a6a1f1dSLionel Sambuc 		    ((dwlp->flags & (__ISDIRTY | __ISFORCED)) == 0))
24151ffecc1SBen Gras 			continue;
24251ffecc1SBen Gras 		vlp = screen->__virtscr->alines[y_off];
24351ffecc1SBen Gras 
244*0a6a1f1dSLionel Sambuc 		if ((*wlp->firstchp < maxx + swin->ch_off &&
245*0a6a1f1dSLionel Sambuc 		    *wlp->lastchp >= swin->ch_off) ||
246*0a6a1f1dSLionel Sambuc 		    ((((dwin->flags & __ISDERWIN) != 0) &&
247*0a6a1f1dSLionel Sambuc 		     (*dwlp->firstchp < dwin->maxx + dwin->ch_off &&
248*0a6a1f1dSLionel Sambuc 		      *dwlp->lastchp >= dwin->ch_off)))) {
24951ffecc1SBen Gras 			/* Set start column */
25051ffecc1SBen Gras 			wx = begx;
25151ffecc1SBen Gras 			x_off = wbegx;
252*0a6a1f1dSLionel Sambuc 			dx_off = 0;
253*0a6a1f1dSLionel Sambuc 			/*
254*0a6a1f1dSLionel Sambuc 			 * if a derwin then source change pointers aren't
255*0a6a1f1dSLionel Sambuc 			 * relevant.
256*0a6a1f1dSLionel Sambuc 			 */
257*0a6a1f1dSLionel Sambuc 			if ((dwin->flags & __ISDERWIN) != 0)
258*0a6a1f1dSLionel Sambuc 				mx = wx + maxx;
259*0a6a1f1dSLionel Sambuc 			else {
260*0a6a1f1dSLionel Sambuc 				if (*wlp->firstchp - swin->ch_off > 0) {
261*0a6a1f1dSLionel Sambuc 					wx += *wlp->firstchp - swin->ch_off;
262*0a6a1f1dSLionel Sambuc 					x_off += *wlp->firstchp - swin->ch_off;
26351ffecc1SBen Gras 				}
26451ffecc1SBen Gras 				mx = maxx;
265*0a6a1f1dSLionel Sambuc 				if (mx > *wlp->lastchp - swin->ch_off + 1)
266*0a6a1f1dSLionel Sambuc 					mx = *dwlp->lastchp - dwin->ch_off + 1;
26751ffecc1SBen Gras 				if (x_off + (mx - wx) > __virtscr->maxx)
26851ffecc1SBen Gras 					mx -= (x_off + maxx) - __virtscr->maxx;
269*0a6a1f1dSLionel Sambuc 			}
270*0a6a1f1dSLionel Sambuc 
27151ffecc1SBen Gras 			/* Copy line from "win" to "__virtscr". */
27251ffecc1SBen Gras 			while (wx < mx) {
27351ffecc1SBen Gras #ifdef DEBUG
27451ffecc1SBen Gras 				__CTRACE(__CTRACE_REFRESH,
27551ffecc1SBen Gras 				    "_wnoutrefresh: copy from %d, "
27684d9c625SLionel Sambuc 				    "%d to %d, %d: %s, 0x%x",
27784d9c625SLionel Sambuc 				    wy, wx, y_off, x_off,
27884d9c625SLionel Sambuc 				    unctrl(wlp->line[wx].ch),
27984d9c625SLionel Sambuc 				    wlp->line[wx].attr);
28051ffecc1SBen Gras #endif
28151ffecc1SBen Gras 				/* Copy character */
28251ffecc1SBen Gras 				vlp->line[x_off].ch = wlp->line[wx].ch;
28351ffecc1SBen Gras 				/* Copy attributes  */
28451ffecc1SBen Gras 				vlp->line[x_off].attr = wlp->line[wx].attr;
28551ffecc1SBen Gras 				/* Check for nca conflict with colour */
28651ffecc1SBen Gras 				if ((vlp->line[x_off].attr & __COLOR) &&
28751ffecc1SBen Gras 				    (vlp->line[x_off].attr &
28851ffecc1SBen Gras 				    _cursesi_screen->nca))
28951ffecc1SBen Gras 					vlp->line[x_off].attr &= ~__COLOR;
290*0a6a1f1dSLionel Sambuc 				if (win->flags & __ISDERWIN) {
291*0a6a1f1dSLionel Sambuc 					dwlp->line[dx_off].ch =
292*0a6a1f1dSLionel Sambuc 						wlp->line[wx].ch;
293*0a6a1f1dSLionel Sambuc 					dwlp->line[dx_off].attr =
294*0a6a1f1dSLionel Sambuc 						wlp->line[wx].attr;
295*0a6a1f1dSLionel Sambuc 				}
296*0a6a1f1dSLionel Sambuc 
29751ffecc1SBen Gras #ifdef HAVE_WCHAR
29851ffecc1SBen Gras 				if (wlp->line[wx].ch
29951ffecc1SBen Gras 				    == (wchar_t)btowc((int) win->bch)) {
30051ffecc1SBen Gras 					vlp->line[x_off].ch = win->bch;
30151ffecc1SBen Gras 					SET_WCOL( vlp->line[x_off], 1 );
30251ffecc1SBen Gras 					if (_cursesi_copy_nsp(win->bnsp,
30351ffecc1SBen Gras 							      &vlp->line[x_off])
30451ffecc1SBen Gras 					    == ERR)
30551ffecc1SBen Gras 						return ERR;
306*0a6a1f1dSLionel Sambuc 					if (win->flags & __ISDERWIN) {
307*0a6a1f1dSLionel Sambuc 						dwlp->line[dx_off].ch =
308*0a6a1f1dSLionel Sambuc 							win->bch;
309*0a6a1f1dSLionel Sambuc 						SET_WCOL(dwlp->line[dx_off], 1);
310*0a6a1f1dSLionel Sambuc 						if (_cursesi_copy_nsp(win->bnsp,
311*0a6a1f1dSLionel Sambuc 						     &dwlp->line[dx_off])
312*0a6a1f1dSLionel Sambuc 					    	    == ERR)
313*0a6a1f1dSLionel Sambuc 							return ERR;
314*0a6a1f1dSLionel Sambuc 					}
31551ffecc1SBen Gras 				}
31651ffecc1SBen Gras #endif /* HAVE_WCHAR */
31784d9c625SLionel Sambuc #ifdef DEBUG
31884d9c625SLionel Sambuc 				__CTRACE(__CTRACE_REFRESH, " = %s, 0x%x\n",
31984d9c625SLionel Sambuc 				    unctrl(vlp->line[x_off].ch),
32084d9c625SLionel Sambuc 				    vlp->line[x_off].attr);
32184d9c625SLionel Sambuc #endif
32251ffecc1SBen Gras 				wx++;
32351ffecc1SBen Gras 				x_off++;
324*0a6a1f1dSLionel Sambuc 				dx_off++;
32551ffecc1SBen Gras 			}
32651ffecc1SBen Gras 
32751ffecc1SBen Gras 			/* Set flags on "__virtscr" and unset on "win". */
32851ffecc1SBen Gras 			if (wlp->flags & __ISPASTEOL)
32951ffecc1SBen Gras 				vlp->flags |= __ISPASTEOL;
33051ffecc1SBen Gras 			else
33151ffecc1SBen Gras 				vlp->flags &= ~__ISPASTEOL;
33251ffecc1SBen Gras 			if (wlp->flags & __ISDIRTY)
33351ffecc1SBen Gras 				vlp->flags |= __ISDIRTY;
334*0a6a1f1dSLionel Sambuc 			if (wlp->flags & __ISFORCED)
335*0a6a1f1dSLionel Sambuc 				vlp->flags |= __ISFORCED;
33651ffecc1SBen Gras 
33751ffecc1SBen Gras #ifdef DEBUG
33851ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH,
33951ffecc1SBen Gras 			    "win: firstch = %d, lastch = %d\n",
34051ffecc1SBen Gras 			    *wlp->firstchp, *wlp->lastchp);
341*0a6a1f1dSLionel Sambuc 			if (win->flags & __ISDERWIN) {
342*0a6a1f1dSLionel Sambuc 				__CTRACE(__CTRACE_REFRESH,
343*0a6a1f1dSLionel Sambuc 				    "derwin: fistch = %d, lastch = %d\n",
344*0a6a1f1dSLionel Sambuc 				    *dwlp->firstchp, *dwlp->lastchp);
345*0a6a1f1dSLionel Sambuc 			}
34651ffecc1SBen Gras #endif
34751ffecc1SBen Gras 			/* Set change pointers on "__virtscr". */
34851ffecc1SBen Gras 			if (*vlp->firstchp >
34951ffecc1SBen Gras 				*wlp->firstchp + wbegx - win->ch_off)
35051ffecc1SBen Gras 					*vlp->firstchp =
35151ffecc1SBen Gras 					    *wlp->firstchp + wbegx - win->ch_off;
35251ffecc1SBen Gras 			if (*vlp->lastchp <
35351ffecc1SBen Gras 				*wlp->lastchp + wbegx - win->ch_off)
35451ffecc1SBen Gras 					*vlp->lastchp =
35551ffecc1SBen Gras 					    *wlp->lastchp + wbegx - win->ch_off;
356*0a6a1f1dSLionel Sambuc 
357*0a6a1f1dSLionel Sambuc 			if (win->flags & __ISDERWIN) {
358*0a6a1f1dSLionel Sambuc 				if (*vlp->firstchp >
359*0a6a1f1dSLionel Sambuc 				    *dwlp->firstchp + wbegx - dwin->ch_off) {
360*0a6a1f1dSLionel Sambuc 					*vlp->firstchp =
361*0a6a1f1dSLionel Sambuc 					    *dwlp->firstchp + wbegx
362*0a6a1f1dSLionel Sambuc 						- dwin->ch_off;
363*0a6a1f1dSLionel Sambuc 					vlp->flags |= __ISDIRTY;
364*0a6a1f1dSLionel Sambuc 				}
365*0a6a1f1dSLionel Sambuc 
366*0a6a1f1dSLionel Sambuc 				if (*vlp->lastchp <
367*0a6a1f1dSLionel Sambuc 				    *dwlp->lastchp + wbegx - dwin->ch_off) {
368*0a6a1f1dSLionel Sambuc 					*vlp->lastchp = *dwlp->lastchp
369*0a6a1f1dSLionel Sambuc 					    + wbegx - dwin->ch_off;
370*0a6a1f1dSLionel Sambuc 					vlp->flags |= __ISDIRTY;
371*0a6a1f1dSLionel Sambuc 				}
372*0a6a1f1dSLionel Sambuc 			}
373*0a6a1f1dSLionel Sambuc 
37451ffecc1SBen Gras #ifdef DEBUG
37551ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH,
37651ffecc1SBen Gras 			    "__virtscr: firstch = %d, lastch = %d\n",
37751ffecc1SBen Gras 			    *vlp->firstchp, *vlp->lastchp);
37851ffecc1SBen Gras #endif
37951ffecc1SBen Gras 			/*
380*0a6a1f1dSLionel Sambuc 			 * Unset change pointers only if a window and we
381*0a6a1f1dSLionel Sambuc 			 * are not forcing a redraw. A pad can be displayed
382*0a6a1f1dSLionel Sambuc 			 * again without any of the contents changing.
38351ffecc1SBen Gras 			 */
384*0a6a1f1dSLionel Sambuc 			if ((!(win->flags & __ISPAD)) ||
385*0a6a1f1dSLionel Sambuc 			    ((wlp->flags & __ISFORCED) == __ISFORCED)) {
38651ffecc1SBen Gras 				/* Set change pointers on "win". */
38751ffecc1SBen Gras 				if (*wlp->firstchp >= win->ch_off)
38851ffecc1SBen Gras 					*wlp->firstchp = maxx + win->ch_off;
38951ffecc1SBen Gras 				if (*wlp->lastchp < maxx + win->ch_off)
39051ffecc1SBen Gras 					*wlp->lastchp = win->ch_off;
39151ffecc1SBen Gras 				if ((*wlp->lastchp < *wlp->firstchp) ||
39251ffecc1SBen Gras 				    (*wlp->firstchp >= maxx + win->ch_off) ||
39351ffecc1SBen Gras 				    (*wlp->lastchp <= win->ch_off)) {
39451ffecc1SBen Gras #ifdef DEBUG
39551ffecc1SBen Gras 					__CTRACE(__CTRACE_REFRESH,
39651ffecc1SBen Gras 					    "_wnoutrefresh: "
39751ffecc1SBen Gras 					    "line %d notdirty\n", wy);
39851ffecc1SBen Gras #endif
39951ffecc1SBen Gras 					wlp->flags &= ~__ISDIRTY;
40051ffecc1SBen Gras 				}
40151ffecc1SBen Gras 			}
40251ffecc1SBen Gras 		}
40351ffecc1SBen Gras 	}
40451ffecc1SBen Gras 	return OK;
40551ffecc1SBen Gras }
40651ffecc1SBen Gras 
40751ffecc1SBen Gras /*
40851ffecc1SBen Gras  * wrefresh --
40951ffecc1SBen Gras  *	Make the current screen look like "win" over the area covered by
41051ffecc1SBen Gras  *	win.
41151ffecc1SBen Gras  */
41251ffecc1SBen Gras int
wrefresh(WINDOW * win)41351ffecc1SBen Gras wrefresh(WINDOW *win)
41451ffecc1SBen Gras {
41551ffecc1SBen Gras 	int retval;
416*0a6a1f1dSLionel Sambuc 	int pbegx, pbegy;
41751ffecc1SBen Gras 
41851ffecc1SBen Gras #ifdef DEBUG
41951ffecc1SBen Gras 	__CTRACE(__CTRACE_REFRESH, "wrefresh: win %p\n", win);
42051ffecc1SBen Gras #endif
42151ffecc1SBen Gras 
42251ffecc1SBen Gras 	_cursesi_screen->curwin = (win == _cursesi_screen->curscr);
423*0a6a1f1dSLionel Sambuc 	if (!_cursesi_screen->curwin) {
424*0a6a1f1dSLionel Sambuc 		pbegx = pbegy = 0;
425*0a6a1f1dSLionel Sambuc 		if ((win->flags & __ISDERWIN) == __ISDERWIN) {
426*0a6a1f1dSLionel Sambuc 			pbegx = win->derx;
427*0a6a1f1dSLionel Sambuc 			pbegy = win->dery;
428*0a6a1f1dSLionel Sambuc #ifdef DEBUG
429*0a6a1f1dSLionel Sambuc 	__CTRACE(__CTRACE_REFRESH, "wrefresh: derwin, begy = %d, begx = %x\n",
430*0a6a1f1dSLionel Sambuc 		pbegy, pbegx);
431*0a6a1f1dSLionel Sambuc #endif
432*0a6a1f1dSLionel Sambuc 		}
433*0a6a1f1dSLionel Sambuc 		retval = _cursesi_wnoutrefresh(_cursesi_screen, win, pbegy,
434*0a6a1f1dSLionel Sambuc 		    pbegx, win->begy, win->begx, win->maxy, win->maxx);
435*0a6a1f1dSLionel Sambuc 	} else
43651ffecc1SBen Gras 		retval = OK;
43751ffecc1SBen Gras 	if (retval == OK) {
43851ffecc1SBen Gras 		retval = doupdate();
43951ffecc1SBen Gras 		if (!(win->flags & __LEAVEOK)) {
44051ffecc1SBen Gras 			win->cury = max(0, curscr->cury - win->begy);
44151ffecc1SBen Gras 			win->curx = max(0, curscr->curx - win->begx);
44251ffecc1SBen Gras 		}
44351ffecc1SBen Gras 	}
44451ffecc1SBen Gras 	_cursesi_screen->curwin = 0;
44551ffecc1SBen Gras 	return(retval);
44651ffecc1SBen Gras }
44751ffecc1SBen Gras 
44851ffecc1SBen Gras  /*
44951ffecc1SBen Gras  * prefresh --
45051ffecc1SBen Gras  *	Make the current screen look like "pad" over the area coverd by
45151ffecc1SBen Gras  *	the specified area of pad.
45251ffecc1SBen Gras  */
45351ffecc1SBen Gras int
prefresh(WINDOW * pad,int pbegy,int pbegx,int sbegy,int sbegx,int smaxy,int smaxx)45451ffecc1SBen Gras prefresh(WINDOW *pad, int pbegy, int pbegx, int sbegy, int sbegx,
45551ffecc1SBen Gras 	int smaxy, int smaxx)
45651ffecc1SBen Gras {
45751ffecc1SBen Gras 	int retval;
45851ffecc1SBen Gras 
45951ffecc1SBen Gras #ifdef DEBUG
46051ffecc1SBen Gras 	__CTRACE(__CTRACE_REFRESH, "prefresh: pad %p, flags 0x%08x\n",
46151ffecc1SBen Gras 	    pad, pad->flags);
46251ffecc1SBen Gras #endif
46351ffecc1SBen Gras 	/* Retain values in case pechochar() is called. */
46451ffecc1SBen Gras 	pad->pbegy = pbegy;
46551ffecc1SBen Gras 	pad->pbegx = pbegx;
46651ffecc1SBen Gras 	pad->sbegy = sbegy;
46751ffecc1SBen Gras 	pad->sbegx = sbegx;
46851ffecc1SBen Gras 	pad->smaxy = smaxy;
46951ffecc1SBen Gras 	pad->smaxx = smaxx;
47051ffecc1SBen Gras 
47151ffecc1SBen Gras 	/* Use pnoutrefresh() to avoid duplicating code here */
47251ffecc1SBen Gras 	retval = pnoutrefresh(pad, pbegy, pbegx, sbegy, sbegx, smaxy, smaxx);
47351ffecc1SBen Gras 	if (retval == OK) {
47451ffecc1SBen Gras 		retval = doupdate();
47551ffecc1SBen Gras 		if (!(pad->flags & __LEAVEOK)) {
47651ffecc1SBen Gras 			pad->cury = max(0, curscr->cury - pad->begy);
47751ffecc1SBen Gras 			pad->curx = max(0, curscr->curx - pad->begx);
47851ffecc1SBen Gras 		}
47951ffecc1SBen Gras 	}
48051ffecc1SBen Gras 	return(retval);
48151ffecc1SBen Gras }
48251ffecc1SBen Gras 
48351ffecc1SBen Gras /*
48451ffecc1SBen Gras  * doupdate --
48551ffecc1SBen Gras  *	Make the current screen look like the virtual window "__virtscr".
48651ffecc1SBen Gras  */
48751ffecc1SBen Gras int
doupdate(void)48851ffecc1SBen Gras doupdate(void)
48951ffecc1SBen Gras {
49051ffecc1SBen Gras 	WINDOW	*win;
491*0a6a1f1dSLionel Sambuc 	__LINE	*wlp, *vlp;
49251ffecc1SBen Gras 	short	 wy;
493*0a6a1f1dSLionel Sambuc 	int	 dnum, was_cleared;
49451ffecc1SBen Gras #ifdef HAVE_WCHAR
49551ffecc1SBen Gras 	__LDATA *lp;
49651ffecc1SBen Gras 	nschar_t *np;
49751ffecc1SBen Gras 	int x;
49851ffecc1SBen Gras #endif /* HAVE_WCHAR */
49951ffecc1SBen Gras 
50051ffecc1SBen Gras 	/* Check if we need to restart ... */
50151ffecc1SBen Gras 	if (_cursesi_screen->endwin)
50251ffecc1SBen Gras 		__restartwin();
50351ffecc1SBen Gras 
50451ffecc1SBen Gras 	if (_cursesi_screen->curwin)
50551ffecc1SBen Gras 		win = curscr;
50651ffecc1SBen Gras 	else
50751ffecc1SBen Gras 		win = _cursesi_screen->__virtscr;
50851ffecc1SBen Gras 
50951ffecc1SBen Gras 	/* Initialize loop parameters. */
51051ffecc1SBen Gras 	_cursesi_screen->ly = curscr->cury;
51151ffecc1SBen Gras 	_cursesi_screen->lx = curscr->curx;
51251ffecc1SBen Gras 	wy = 0;
51351ffecc1SBen Gras 
51451ffecc1SBen Gras 	if (!_cursesi_screen->curwin) {
51551ffecc1SBen Gras 		for (wy = 0; wy < win->maxy; wy++) {
51651ffecc1SBen Gras 			wlp = win->alines[wy];
51751ffecc1SBen Gras 			if (wlp->flags & __ISDIRTY) {
51851ffecc1SBen Gras #ifndef HAVE_WCHAR
51951ffecc1SBen Gras 				wlp->hash = __hash(wlp->line,
52051ffecc1SBen Gras 				    (size_t)(win->maxx * __LDATASIZE));
52151ffecc1SBen Gras #else
52251ffecc1SBen Gras 				wlp->hash = 0;
52351ffecc1SBen Gras 				for ( x = 0; x < win->maxx; x++ ) {
52451ffecc1SBen Gras 					lp = &wlp->line[ x ];
52551ffecc1SBen Gras 					wlp->hash = __hash_more( &lp->ch,
52651ffecc1SBen Gras 						sizeof( wchar_t ), wlp->hash );
52751ffecc1SBen Gras 					wlp->hash = __hash_more( &lp->attr,
52851ffecc1SBen Gras 						sizeof( attr_t ), wlp->hash );
52951ffecc1SBen Gras 					np = lp->nsp;
53051ffecc1SBen Gras 					if (np) {
53151ffecc1SBen Gras 						while ( np ) {
53251ffecc1SBen Gras 							wlp->hash
53351ffecc1SBen Gras 							    = __hash_more(
53451ffecc1SBen Gras 								&np->ch,
53551ffecc1SBen Gras 								sizeof(wchar_t),
53651ffecc1SBen Gras 								wlp->hash );
53751ffecc1SBen Gras 							np = np->next;
53851ffecc1SBen Gras 						}
53951ffecc1SBen Gras 					}
54051ffecc1SBen Gras 				}
54151ffecc1SBen Gras #endif /* HAVE_WCHAR */
54251ffecc1SBen Gras 			}
54351ffecc1SBen Gras 		}
54451ffecc1SBen Gras 	}
54551ffecc1SBen Gras 
546*0a6a1f1dSLionel Sambuc 	was_cleared = 0;
54751ffecc1SBen Gras 	if ((win->flags & __CLEAROK) || (curscr->flags & __CLEAROK) ||
54851ffecc1SBen Gras 	    _cursesi_screen->curwin) {
54951ffecc1SBen Gras 		if (curscr->wattr & __COLOR)
55051ffecc1SBen Gras 			__unsetattr(0);
55151ffecc1SBen Gras 		tputs(clear_screen, 0, __cputchar);
55251ffecc1SBen Gras 		_cursesi_screen->ly = 0;
55351ffecc1SBen Gras 		_cursesi_screen->lx = 0;
55451ffecc1SBen Gras 		if (!_cursesi_screen->curwin) {
55551ffecc1SBen Gras 			curscr->flags &= ~__CLEAROK;
55651ffecc1SBen Gras 			curscr->cury = 0;
55751ffecc1SBen Gras 			curscr->curx = 0;
55851ffecc1SBen Gras 			werase(curscr);
55951ffecc1SBen Gras 		}
56051ffecc1SBen Gras 		__touchwin(win);
56151ffecc1SBen Gras 		win->flags &= ~__CLEAROK;
562*0a6a1f1dSLionel Sambuc 		/* note we cleared for later */
563*0a6a1f1dSLionel Sambuc 		was_cleared = 1;
56451ffecc1SBen Gras 	}
56551ffecc1SBen Gras 	if (!cursor_address) {
56651ffecc1SBen Gras 		if (win->curx != 0)
56751ffecc1SBen Gras 			__cputchar('\n');
56851ffecc1SBen Gras 		if (!_cursesi_screen->curwin)
56951ffecc1SBen Gras 			werase(curscr);
57051ffecc1SBen Gras 	}
57151ffecc1SBen Gras #ifdef DEBUG
57251ffecc1SBen Gras 	__CTRACE(__CTRACE_REFRESH, "doupdate: (%p): curwin = %d\n", win,
57351ffecc1SBen Gras 	    _cursesi_screen->curwin);
57451ffecc1SBen Gras 	__CTRACE(__CTRACE_REFRESH, "doupdate: \tfirstch\tlastch\n");
57551ffecc1SBen Gras #endif
57651ffecc1SBen Gras 
57751ffecc1SBen Gras 	if (!_cursesi_screen->curwin) {
57851ffecc1SBen Gras 		/*
57951ffecc1SBen Gras 		 * Invoke quickch() only if more than a quarter of the lines
58051ffecc1SBen Gras 		 * in the window are dirty.
58151ffecc1SBen Gras 		 */
58251ffecc1SBen Gras 		for (wy = 0, dnum = 0; wy < win->maxy; wy++)
58351ffecc1SBen Gras 			if (win->alines[wy]->flags & __ISDIRTY)
58451ffecc1SBen Gras 				dnum++;
58551ffecc1SBen Gras 		if (!__noqch && dnum > (int) win->maxy / 4)
58651ffecc1SBen Gras 			quickch();
58751ffecc1SBen Gras 	}
58851ffecc1SBen Gras 
58951ffecc1SBen Gras #ifdef DEBUG
59051ffecc1SBen Gras 	{
59151ffecc1SBen Gras 		int	i, j;
59251ffecc1SBen Gras 
59351ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH,
59451ffecc1SBen Gras 		    "#####################################\n");
59551ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH,
59651ffecc1SBen Gras 		    "stdscr(%p)-curscr(%p)-__virtscr(%p)\n",
59751ffecc1SBen Gras 		    stdscr, curscr, _cursesi_screen->__virtscr);
59851ffecc1SBen Gras 		for (i = 0; i < curscr->maxy; i++) {
59951ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, "C: %d:", i);
60051ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, " 0x%x \n",
60151ffecc1SBen Gras 			    curscr->alines[i]->hash);
60251ffecc1SBen Gras 			for (j = 0; j < curscr->maxx; j++)
60351ffecc1SBen Gras 				__CTRACE(__CTRACE_REFRESH, "%c",
60451ffecc1SBen Gras 				    curscr->alines[i]->line[j].ch);
60551ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, "\n");
60651ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, " attr:");
60751ffecc1SBen Gras 			for (j = 0; j < curscr->maxx; j++)
60851ffecc1SBen Gras 				__CTRACE(__CTRACE_REFRESH, " %x",
60951ffecc1SBen Gras 				    curscr->alines[i]->line[j].attr);
61051ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, "\n");
61151ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, "W: %d:", i);
61251ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, " 0x%x \n",
61351ffecc1SBen Gras 			    win->alines[i]->hash);
61451ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, " 0x%x ",
61551ffecc1SBen Gras 			    win->alines[i]->flags);
61651ffecc1SBen Gras 			for (j = 0; j < win->maxx; j++)
61751ffecc1SBen Gras 				__CTRACE(__CTRACE_REFRESH, "%c",
61851ffecc1SBen Gras 				    win->alines[i]->line[j].ch);
61951ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, "\n");
62051ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, " attr:");
62151ffecc1SBen Gras 			for (j = 0; j < win->maxx; j++)
62251ffecc1SBen Gras 				__CTRACE(__CTRACE_REFRESH, " %x",
62351ffecc1SBen Gras 				    win->alines[i]->line[j].attr);
62451ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, "\n");
62551ffecc1SBen Gras #ifdef HAVE_WCHAR
62651ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, " nsp:");
62751ffecc1SBen Gras 			for (j = 0; j < curscr->maxx; j++)
62851ffecc1SBen Gras 				__CTRACE(__CTRACE_REFRESH, " %p",
62951ffecc1SBen Gras 				    win->alines[i]->line[j].nsp);
63051ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, "\n");
63151ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, " bnsp:");
63251ffecc1SBen Gras 			for (j = 0; j < curscr->maxx; j++)
63351ffecc1SBen Gras 				__CTRACE(__CTRACE_REFRESH, " %p",
63451ffecc1SBen Gras 				    win->bnsp);
63551ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, "\n");
63651ffecc1SBen Gras #endif /* HAVE_WCHAR */
63751ffecc1SBen Gras 		}
63851ffecc1SBen Gras 	}
63951ffecc1SBen Gras #endif /* DEBUG */
64051ffecc1SBen Gras 
64151ffecc1SBen Gras 	for (wy = 0; wy < win->maxy; wy++) {
64251ffecc1SBen Gras 		wlp = win->alines[wy];
643*0a6a1f1dSLionel Sambuc 		vlp = _cursesi_screen->__virtscr->alines[win->begy + wy];
64451ffecc1SBen Gras /* XXX: remove this debug */
64551ffecc1SBen Gras #ifdef DEBUG
64651ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH,
64751ffecc1SBen Gras 		    "doupdate: wy %d\tf: %d\tl:%d\tflags %x\n",
64851ffecc1SBen Gras 		    wy, *wlp->firstchp, *wlp->lastchp, wlp->flags);
64951ffecc1SBen Gras #endif /* DEBUG */
65051ffecc1SBen Gras 		if (!_cursesi_screen->curwin)
65151ffecc1SBen Gras 			curscr->alines[wy]->hash = wlp->hash;
652*0a6a1f1dSLionel Sambuc 		if ((wlp->flags & __ISDIRTY) ||
653*0a6a1f1dSLionel Sambuc 		    (wlp->flags & __ISFORCED)) {
65451ffecc1SBen Gras #ifdef DEBUG
65551ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH,
65651ffecc1SBen Gras 			    "doupdate: [ISDIRTY]wy:%d\tf:%d\tl:%d\n", wy,
65751ffecc1SBen Gras 			    *wlp->firstchp, *wlp->lastchp);
65851ffecc1SBen Gras #endif /* DEBUG */
659*0a6a1f1dSLionel Sambuc 			/*
660*0a6a1f1dSLionel Sambuc 		 	* We have just cleared so don't force an update
661*0a6a1f1dSLionel Sambuc 		 	* otherwise we spray neeedless blanks to a cleared
662*0a6a1f1dSLionel Sambuc 		 	* screen.
663*0a6a1f1dSLionel Sambuc 		 	*/
664*0a6a1f1dSLionel Sambuc 			if (was_cleared == 1)
665*0a6a1f1dSLionel Sambuc 				win->alines[wy]->flags &= ~__ISFORCED;
666*0a6a1f1dSLionel Sambuc 
66751ffecc1SBen Gras 			if (makech(wy) == ERR)
66851ffecc1SBen Gras 				return (ERR);
66951ffecc1SBen Gras 			else {
67051ffecc1SBen Gras 				if (*wlp->firstchp >= 0)
67151ffecc1SBen Gras 					*wlp->firstchp = win->maxx;
67251ffecc1SBen Gras 				if (*wlp->lastchp < win->maxx)
673*0a6a1f1dSLionel Sambuc 					*wlp->lastchp = win->ch_off;
67451ffecc1SBen Gras 				if (*wlp->lastchp < *wlp->firstchp) {
67551ffecc1SBen Gras #ifdef DEBUG
67651ffecc1SBen Gras 					__CTRACE(__CTRACE_REFRESH,
67751ffecc1SBen Gras 					    "doupdate: line %d notdirty\n", wy);
67851ffecc1SBen Gras #endif /* DEBUG */
679*0a6a1f1dSLionel Sambuc 					wlp->flags &= ~(__ISDIRTY | __ISFORCED);
680*0a6a1f1dSLionel Sambuc 				}
68151ffecc1SBen Gras 			}
68251ffecc1SBen Gras 		}
68351ffecc1SBen Gras 
684*0a6a1f1dSLionel Sambuc 		/*
685*0a6a1f1dSLionel Sambuc 		 * virtscr is now synced for the line, unset the change
686*0a6a1f1dSLionel Sambuc 		 * pointers.
687*0a6a1f1dSLionel Sambuc 		 */
688*0a6a1f1dSLionel Sambuc 		if (*vlp->firstchp >= 0)
689*0a6a1f1dSLionel Sambuc 			*vlp->firstchp = _cursesi_screen->__virtscr->maxx;
690*0a6a1f1dSLionel Sambuc 		if (*vlp->lastchp <= _cursesi_screen->__virtscr->maxx)
691*0a6a1f1dSLionel Sambuc 			*vlp->lastchp = 0;
692*0a6a1f1dSLionel Sambuc 
69351ffecc1SBen Gras #ifdef DEBUG
69451ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, "\t%d\t%d\n",
69551ffecc1SBen Gras 		    *wlp->firstchp, *wlp->lastchp);
69651ffecc1SBen Gras #endif /* DEBUG */
69751ffecc1SBen Gras 	}
69851ffecc1SBen Gras 
69951ffecc1SBen Gras #ifdef DEBUG
70051ffecc1SBen Gras 	__CTRACE(__CTRACE_REFRESH, "doupdate: ly=%d, lx=%d\n",
70151ffecc1SBen Gras 	    _cursesi_screen->ly, _cursesi_screen->lx);
70251ffecc1SBen Gras #endif /* DEBUG */
70351ffecc1SBen Gras 
70451ffecc1SBen Gras 	if (_cursesi_screen->curwin)
70551ffecc1SBen Gras 		domvcur(_cursesi_screen->ly, _cursesi_screen->lx,
70651ffecc1SBen Gras 			win->cury, win->curx);
70751ffecc1SBen Gras 	else {
70851ffecc1SBen Gras 		if (win->flags & __LEAVEOK) {
70951ffecc1SBen Gras 			curscr->cury = _cursesi_screen->ly;
71051ffecc1SBen Gras 			curscr->curx = _cursesi_screen->lx;
71151ffecc1SBen Gras 		} else {
71251ffecc1SBen Gras 			domvcur(_cursesi_screen->ly, _cursesi_screen->lx,
71351ffecc1SBen Gras 				win->cury, win->curx);
71451ffecc1SBen Gras 			curscr->cury = win->cury;
71551ffecc1SBen Gras 			curscr->curx = win->curx;
71651ffecc1SBen Gras 		}
71751ffecc1SBen Gras 	}
71851ffecc1SBen Gras 
71951ffecc1SBen Gras 	/* Don't leave the screen with attributes set. */
72051ffecc1SBen Gras 	__unsetattr(0);
72151ffecc1SBen Gras #ifdef DEBUG
72251ffecc1SBen Gras #ifdef HAVE_WCHAR
72351ffecc1SBen Gras 	{
72451ffecc1SBen Gras 		int	i, j;
72551ffecc1SBen Gras 
72651ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH,
72751ffecc1SBen Gras 		    "***********after*****************\n");
72851ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH,
72951ffecc1SBen Gras 		    "stdscr(%p)-curscr(%p)-__virtscr(%p)\n",
73051ffecc1SBen Gras 		    stdscr, curscr, _cursesi_screen->__virtscr);
73151ffecc1SBen Gras 		for (i = 0; i < curscr->maxy; i++) {
73251ffecc1SBen Gras 			for (j = 0; j < curscr->maxx; j++)
73351ffecc1SBen Gras 				__CTRACE(__CTRACE_REFRESH,
73451ffecc1SBen Gras 				    "[%d,%d](%x,%x,%p)-(%x,%x,%p)\n",
73551ffecc1SBen Gras 				    i, j,
73651ffecc1SBen Gras 				    curscr->alines[i]->line[j].ch,
73751ffecc1SBen Gras 				    curscr->alines[i]->line[j].attr,
73851ffecc1SBen Gras 				    curscr->alines[i]->line[j].nsp,
73951ffecc1SBen Gras 				    _cursesi_screen->__virtscr->alines[i]->line[j].ch,
74051ffecc1SBen Gras 				    _cursesi_screen->__virtscr->alines[i]->line[j].attr,
74151ffecc1SBen Gras 				    _cursesi_screen->__virtscr->alines[i]->line[j].nsp);
74251ffecc1SBen Gras 		}
74351ffecc1SBen Gras 	}
74451ffecc1SBen Gras #endif /* HAVE_WCHAR */
74551ffecc1SBen Gras #endif /* DEBUG */
74651ffecc1SBen Gras 	return fflush(_cursesi_screen->outfd) == EOF ? ERR : OK;
74751ffecc1SBen Gras }
74851ffecc1SBen Gras 
74951ffecc1SBen Gras /*
75051ffecc1SBen Gras  * makech --
75151ffecc1SBen Gras  *	Make a change on the screen.
75251ffecc1SBen Gras  */
75351ffecc1SBen Gras static int
makech(int wy)75451ffecc1SBen Gras makech(int wy)
75551ffecc1SBen Gras {
75651ffecc1SBen Gras 	WINDOW	*win;
75751ffecc1SBen Gras 	static __LDATA blank;
75851ffecc1SBen Gras 	__LDATA *nsp, *csp, *cp, *cep;
759*0a6a1f1dSLionel Sambuc 	__LINE *wlp;
76051ffecc1SBen Gras 	size_t	clsp, nlsp;	/* Last space in lines. */
76151ffecc1SBen Gras 	int	lch, wx;
76251ffecc1SBen Gras 	const char	*ce;
76351ffecc1SBen Gras 	attr_t	lspc;		/* Last space colour */
76451ffecc1SBen Gras 	attr_t	off, on;
76551ffecc1SBen Gras 
76651ffecc1SBen Gras #ifdef __GNUC__
76751ffecc1SBen Gras 	nlsp = lspc = 0;	/* XXX gcc -Wuninitialized */
76851ffecc1SBen Gras #endif
76951ffecc1SBen Gras 	if (_cursesi_screen->curwin)
77051ffecc1SBen Gras 		win = curscr;
77151ffecc1SBen Gras 	else
77251ffecc1SBen Gras 		win = __virtscr;
77351ffecc1SBen Gras #ifdef HAVE_WCHAR
77451ffecc1SBen Gras 	blank.ch = ( wchar_t )btowc(( int ) win->bch );
77551ffecc1SBen Gras 	blank.attr = 0;
77651ffecc1SBen Gras 	if (_cursesi_copy_nsp(win->bnsp, &blank) == ERR)
77751ffecc1SBen Gras 		return ERR;
77851ffecc1SBen Gras 	SET_WCOL( blank, 1 );
77951ffecc1SBen Gras #endif /* HAVE_WCHAR */
78051ffecc1SBen Gras #ifdef DEBUG
78151ffecc1SBen Gras #if HAVE_WCHAR
78251ffecc1SBen Gras 	{
78351ffecc1SBen Gras 		int x;
78451ffecc1SBen Gras 		__LDATA *lp, *vlp;
78551ffecc1SBen Gras 
78651ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH,
78751ffecc1SBen Gras 		    "[makech-before]wy=%d,curscr(%p)-__virtscr(%p)\n",
78851ffecc1SBen Gras 		    wy, curscr, __virtscr);
78951ffecc1SBen Gras 		for (x = 0; x < curscr->maxx; x++) {
79051ffecc1SBen Gras 			lp = &curscr->alines[wy]->line[x];
79151ffecc1SBen Gras 			vlp = &__virtscr->alines[wy]->line[x];
79251ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH,
79351ffecc1SBen Gras 			    "[%d,%d](%x,%x,%x,%x,%p)-"
79451ffecc1SBen Gras 			    "(%x,%x,%x,%x,%p)\n",
79551ffecc1SBen Gras 			    wy, x, lp->ch, lp->attr,
79651ffecc1SBen Gras 			    win->bch, win->battr, lp->nsp,
79751ffecc1SBen Gras 			    vlp->ch, vlp->attr,
79851ffecc1SBen Gras 			    win->bch, win->battr, vlp->nsp);
79951ffecc1SBen Gras 		}
80051ffecc1SBen Gras 	}
80151ffecc1SBen Gras #endif /* HAVE_WCHAR */
80251ffecc1SBen Gras #endif /* DEBUG */
80351ffecc1SBen Gras 	/* Is the cursor still on the end of the last line? */
80451ffecc1SBen Gras 	if (wy > 0 && curscr->alines[wy - 1]->flags & __ISPASTEOL) {
80551ffecc1SBen Gras 		domvcur(_cursesi_screen->ly, _cursesi_screen->lx,
80651ffecc1SBen Gras 			_cursesi_screen->ly + 1, 0);
80751ffecc1SBen Gras 		_cursesi_screen->ly++;
80851ffecc1SBen Gras 		_cursesi_screen->lx = 0;
80951ffecc1SBen Gras 	}
810*0a6a1f1dSLionel Sambuc 	wlp = win->alines[wy];
81151ffecc1SBen Gras 	wx = *win->alines[wy]->firstchp;
81251ffecc1SBen Gras 	if (wx < 0)
81351ffecc1SBen Gras 		wx = 0;
81451ffecc1SBen Gras 	else
81551ffecc1SBen Gras 		if (wx >= win->maxx)
81651ffecc1SBen Gras 			return (OK);
81751ffecc1SBen Gras 	lch = *win->alines[wy]->lastchp;
81851ffecc1SBen Gras 	if (lch < 0)
81951ffecc1SBen Gras 		return (OK);
82051ffecc1SBen Gras 	else
82151ffecc1SBen Gras 		if (lch >= (int) win->maxx)
82251ffecc1SBen Gras 			lch = win->maxx - 1;
82351ffecc1SBen Gras 
82451ffecc1SBen Gras 	if (_cursesi_screen->curwin) {
82551ffecc1SBen Gras 		csp = &blank;
82651ffecc1SBen Gras #ifdef DEBUG
82751ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, "makech: csp is blank\n");
82851ffecc1SBen Gras #endif /* DEBUG */
82951ffecc1SBen Gras 	} else {
83051ffecc1SBen Gras 		csp = &curscr->alines[wy]->line[wx];
83151ffecc1SBen Gras #ifdef DEBUG
83251ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH,
83351ffecc1SBen Gras 		    "makech: csp is on curscr:(%d,%d)\n", wy, wx);
83451ffecc1SBen Gras #endif /* DEBUG */
83551ffecc1SBen Gras 	}
83651ffecc1SBen Gras 
83751ffecc1SBen Gras 	nsp = &win->alines[wy]->line[wx];
83851ffecc1SBen Gras #ifdef DEBUG
83951ffecc1SBen Gras 	if ( _cursesi_screen->curwin )
84051ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH,
84151ffecc1SBen Gras 		    "makech: nsp is at curscr:(%d,%d)\n", wy, wx);
84251ffecc1SBen Gras 	else
84351ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH,
84451ffecc1SBen Gras 		    "makech: nsp is at __virtscr:(%d,%d)\n", wy, wx);
84551ffecc1SBen Gras #endif /* DEBUG */
84651ffecc1SBen Gras 	if (clr_eol && !_cursesi_screen->curwin) {
84751ffecc1SBen Gras 		cp = &win->alines[wy]->line[win->maxx - 1];
84851ffecc1SBen Gras 		lspc = cp->attr & __COLOR;
84951ffecc1SBen Gras #ifndef HAVE_WCHAR
85051ffecc1SBen Gras 		while (cp->ch == ' ' && cp->attr == lspc) /* XXX */
85151ffecc1SBen Gras 			if (cp-- <= win->alines[wy]->line)
85251ffecc1SBen Gras 				break;
85351ffecc1SBen Gras #else
85451ffecc1SBen Gras 		while (cp->ch == ( wchar_t )btowc(( int )' ' )
85551ffecc1SBen Gras 				&& ( cp->attr & WA_ATTRIBUTES ) == lspc)
85651ffecc1SBen Gras 			if (cp-- <= win->alines[wy]->line)
85751ffecc1SBen Gras 				break;
85851ffecc1SBen Gras #endif /* HAVE_WCHAR */
85951ffecc1SBen Gras 		if (win->alines[wy]->line > cp)
86051ffecc1SBen Gras 			nlsp = 0;
86151ffecc1SBen Gras 		else
86251ffecc1SBen Gras 			nlsp = cp - win->alines[wy]->line;
86351ffecc1SBen Gras 	}
86451ffecc1SBen Gras 	if (!_cursesi_screen->curwin)
86551ffecc1SBen Gras 		ce = clr_eol;
86651ffecc1SBen Gras 	else
86751ffecc1SBen Gras 		ce = NULL;
86851ffecc1SBen Gras 
86951ffecc1SBen Gras 	while (wx <= lch) {
87051ffecc1SBen Gras #ifdef DEBUG
87151ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, "makech: wx=%d,lch=%d\n", wx, lch);
87251ffecc1SBen Gras #endif /* DEBUG */
87351ffecc1SBen Gras #ifndef HAVE_WCHAR
874*0a6a1f1dSLionel Sambuc 		if (!(wlp->flags & __ISFORCED) &&
875*0a6a1f1dSLionel Sambuc 		    (memcmp(nsp, csp, sizeof(__LDATA)) == 0)) {
87651ffecc1SBen Gras 			if (wx <= lch) {
87751ffecc1SBen Gras 				while (wx <= lch &&
87851ffecc1SBen Gras 				    memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
87951ffecc1SBen Gras 					nsp++;
88051ffecc1SBen Gras 					if (!_cursesi_screen->curwin)
88151ffecc1SBen Gras 						++csp;
88251ffecc1SBen Gras 					++wx;
88351ffecc1SBen Gras 				}
88451ffecc1SBen Gras 				continue;
88551ffecc1SBen Gras 			}
88651ffecc1SBen Gras 			break;
88751ffecc1SBen Gras 		}
88851ffecc1SBen Gras #else
88951ffecc1SBen Gras #ifdef DEBUG
89051ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, "makech: nsp=(%x,%x,%x,%x,%p)\n",
89151ffecc1SBen Gras 			nsp->ch, nsp->attr, win->bch, win->battr, nsp->nsp);
89251ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, "makech: csp=(%x,%x,%x,%x,%p)\n",
89351ffecc1SBen Gras 			csp->ch, csp->attr, win->bch, win->battr, csp->nsp);
89451ffecc1SBen Gras #endif /* DEBUG */
895*0a6a1f1dSLionel Sambuc 		if (!(wlp->flags & __ISFORCED) &&
896*0a6a1f1dSLionel Sambuc 		     (((nsp->attr & __WCWIDTH) != __WCWIDTH) &&
897*0a6a1f1dSLionel Sambuc 		       cellcmp(nsp, csp))) {
89851ffecc1SBen Gras 			if (wx <= lch) {
89951ffecc1SBen Gras 				while (wx <= lch && cellcmp( csp, nsp )) {
90051ffecc1SBen Gras 					nsp++;
90151ffecc1SBen Gras 					if (!_cursesi_screen->curwin)
90251ffecc1SBen Gras 						++csp;
90351ffecc1SBen Gras 					++wx;
90451ffecc1SBen Gras 				}
90551ffecc1SBen Gras 				continue;
90651ffecc1SBen Gras 			}
90751ffecc1SBen Gras 			break;
90851ffecc1SBen Gras 		}
90951ffecc1SBen Gras #endif /* HAVE_WCHAR */
91051ffecc1SBen Gras 		domvcur(_cursesi_screen->ly, _cursesi_screen->lx, wy, wx);
91151ffecc1SBen Gras 
91251ffecc1SBen Gras #ifdef DEBUG
91351ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, "makech: 1: wx = %d, ly= %d, "
91451ffecc1SBen Gras 		    "lx = %d, newy = %d, newx = %d\n",
91551ffecc1SBen Gras 		    wx, _cursesi_screen->ly, _cursesi_screen->lx, wy, wx);
91651ffecc1SBen Gras #endif
91751ffecc1SBen Gras 		_cursesi_screen->ly = wy;
91851ffecc1SBen Gras 		_cursesi_screen->lx = wx;
91951ffecc1SBen Gras #ifndef HAVE_WCHAR
920*0a6a1f1dSLionel Sambuc 		while (wx <= lch && (memcmp(nsp, csp, sizeof(__LDATA)) != 0) ||
921*0a6a1f1dSLionel Sambuc 			(wlp->flags & __ISFORCED)) {
92251ffecc1SBen Gras 			if (ce != NULL &&
92351ffecc1SBen Gras 			    wx >= nlsp && nsp->ch == ' ' && nsp->attr == lspc) {
92451ffecc1SBen Gras #else
925*0a6a1f1dSLionel Sambuc 		while ((!cellcmp(nsp, csp) || (wlp->flags & __ISFORCED)) &&
926*0a6a1f1dSLionel Sambuc 			wx <= lch) {
92751ffecc1SBen Gras 			if (ce != NULL && wx >= nlsp
92851ffecc1SBen Gras 			   && nsp->ch == (wchar_t)btowc((int)' ') /* XXX */
92951ffecc1SBen Gras 			   && (nsp->attr & WA_ATTRIBUTES) == lspc) {
93051ffecc1SBen Gras 
93151ffecc1SBen Gras #endif
93251ffecc1SBen Gras 				/* Check for clear to end-of-line. */
93351ffecc1SBen Gras 				cep = &curscr->alines[wy]->line[win->maxx - 1];
93451ffecc1SBen Gras #ifndef HAVE_WCHAR
93551ffecc1SBen Gras 				while (cep->ch == ' ' && cep->attr == lspc) /* XXX */
93651ffecc1SBen Gras #else
93751ffecc1SBen Gras 				while (cep->ch == (wchar_t)btowc((int)' ')
93851ffecc1SBen Gras 				       && (cep->attr & WA_ATTRIBUTES) == lspc)
93951ffecc1SBen Gras #endif /* HAVE_WCHAR */
94051ffecc1SBen Gras 					if (cep-- <= csp)
94151ffecc1SBen Gras 						break;
942*0a6a1f1dSLionel Sambuc 				if (cep > (curscr->alines[wy]->line + win->begx * __LDATASIZE))
94351ffecc1SBen Gras 					clsp = cep - curscr->alines[wy]->line -
94451ffecc1SBen Gras 				    	win->begx * __LDATASIZE;
945*0a6a1f1dSLionel Sambuc 				else
946*0a6a1f1dSLionel Sambuc 					clsp = 0;
94751ffecc1SBen Gras #ifdef DEBUG
94851ffecc1SBen Gras 				__CTRACE(__CTRACE_REFRESH,
94951ffecc1SBen Gras 				    "makech: clsp = %zu, nlsp = %zu\n",
95051ffecc1SBen Gras 				    clsp, nlsp);
951*0a6a1f1dSLionel Sambuc 				__CTRACE(__CTRACE_REFRESH,
952*0a6a1f1dSLionel Sambuc 				    "makech: line = %p, cep = %p, begx = %u\n",
953*0a6a1f1dSLionel Sambuc 				    curscr->alines[wy]->line, cep, win->begx);
95451ffecc1SBen Gras #endif
95551ffecc1SBen Gras 				if (((clsp - nlsp >= strlen(clr_eol) &&
95651ffecc1SBen Gras 				    clsp < win->maxx * __LDATASIZE) ||
95751ffecc1SBen Gras 				    wy == win->maxy - 1) &&
95851ffecc1SBen Gras 				    (!(lspc & __COLOR) ||
95951ffecc1SBen Gras 				    ((lspc & __COLOR) && back_color_erase))) {
96051ffecc1SBen Gras 					__unsetattr(0);
96151ffecc1SBen Gras 					if (__using_color &&
96251ffecc1SBen Gras 					    ((lspc & __COLOR) !=
96351ffecc1SBen Gras 					    (curscr->wattr & __COLOR)))
96451ffecc1SBen Gras 						__set_color(curscr, lspc &
96551ffecc1SBen Gras 						    __COLOR);
96651ffecc1SBen Gras 					tputs(clr_eol, 0, __cputchar);
96751ffecc1SBen Gras 					_cursesi_screen->lx = wx + win->begx;
96851ffecc1SBen Gras 					while (wx++ <= clsp) {
96951ffecc1SBen Gras 						csp->attr = lspc;
97051ffecc1SBen Gras #ifndef HAVE_WCHAR
97151ffecc1SBen Gras 						csp->ch = ' '; /* XXX */
97251ffecc1SBen Gras #else
97351ffecc1SBen Gras 						csp->ch = (wchar_t)btowc((int)' ');
97451ffecc1SBen Gras 						SET_WCOL( *csp, 1 );
97551ffecc1SBen Gras #endif /* HAVE_WCHAR */
97651ffecc1SBen Gras 						csp++;
97751ffecc1SBen Gras 					}
97851ffecc1SBen Gras 					return (OK);
97951ffecc1SBen Gras 				}
98051ffecc1SBen Gras 				ce = NULL;
98151ffecc1SBen Gras 			}
98251ffecc1SBen Gras 
98351ffecc1SBen Gras #ifdef DEBUG
98451ffecc1SBen Gras 				__CTRACE(__CTRACE_REFRESH,
98551ffecc1SBen Gras 				    "makech: have attr %08x, need attr %08x\n",
9860c3ae37fSLionel Sambuc 				    curscr->wattr
9870c3ae37fSLionel Sambuc #ifndef HAVE_WCHAR
9880c3ae37fSLionel Sambuc 				 & __ATTRIBUTES
9890c3ae37fSLionel Sambuc #else
9900c3ae37fSLionel Sambuc 				 & WA_ATTRIBUTES
9910c3ae37fSLionel Sambuc #endif
9920c3ae37fSLionel Sambuc 					 ,  nsp->attr
9930c3ae37fSLionel Sambuc #ifndef HAVE_WCHAR
9940c3ae37fSLionel Sambuc 				 & __ATTRIBUTES
9950c3ae37fSLionel Sambuc #else
9960c3ae37fSLionel Sambuc 				 & WA_ATTRIBUTES
9970c3ae37fSLionel Sambuc #endif
9980c3ae37fSLionel Sambuc 					);
99951ffecc1SBen Gras #endif
100051ffecc1SBen Gras 
100151ffecc1SBen Gras 			off = (~nsp->attr & curscr->wattr)
100251ffecc1SBen Gras #ifndef HAVE_WCHAR
100351ffecc1SBen Gras 				 & __ATTRIBUTES
100451ffecc1SBen Gras #else
100551ffecc1SBen Gras 				 & WA_ATTRIBUTES
100651ffecc1SBen Gras #endif
100751ffecc1SBen Gras 				;
100851ffecc1SBen Gras 
100951ffecc1SBen Gras 			/*
101051ffecc1SBen Gras 			 * Unset attributes as appropriate.  Unset first
101151ffecc1SBen Gras 			 * so that the relevant attributes can be reset
101251ffecc1SBen Gras 			 * (because 'me' unsets 'mb', 'md', 'mh', 'mk',
101351ffecc1SBen Gras 			 * 'mp' and 'mr').  Check to see if we also turn off
101451ffecc1SBen Gras 			 * standout, attributes and colour.
101551ffecc1SBen Gras 			 */
101651ffecc1SBen Gras 			if (off & __TERMATTR && exit_attribute_mode != NULL) {
101751ffecc1SBen Gras 				tputs(exit_attribute_mode, 0, __cputchar);
101851ffecc1SBen Gras 				curscr->wattr &= __mask_me;
101951ffecc1SBen Gras 				off &= __mask_me;
102051ffecc1SBen Gras 			}
102151ffecc1SBen Gras 
102251ffecc1SBen Gras 			/*
102351ffecc1SBen Gras 			 * Exit underscore mode if appropriate.
102451ffecc1SBen Gras 			 * Check to see if we also turn off standout,
102551ffecc1SBen Gras 			 * attributes and colour.
102651ffecc1SBen Gras 			 */
102751ffecc1SBen Gras 			if (off & __UNDERSCORE && exit_underline_mode != NULL) {
102851ffecc1SBen Gras 				tputs(exit_underline_mode, 0, __cputchar);
102951ffecc1SBen Gras 				curscr->wattr &= __mask_ue;
103051ffecc1SBen Gras 				off &= __mask_ue;
103151ffecc1SBen Gras 			}
103251ffecc1SBen Gras 
103351ffecc1SBen Gras 			/*
103451ffecc1SBen Gras 			 * Exit standout mode as appropriate.
103551ffecc1SBen Gras 			 * Check to see if we also turn off underscore,
103651ffecc1SBen Gras 			 * attributes and colour.
103751ffecc1SBen Gras 			 * XXX
103851ffecc1SBen Gras 			 * Should use uc if so/se not available.
103951ffecc1SBen Gras 			 */
104051ffecc1SBen Gras 			if (off & __STANDOUT && exit_standout_mode != NULL) {
104151ffecc1SBen Gras 				tputs(exit_standout_mode, 0, __cputchar);
104251ffecc1SBen Gras 				curscr->wattr &= __mask_se;
104351ffecc1SBen Gras 				off &= __mask_se;
104451ffecc1SBen Gras 			}
104551ffecc1SBen Gras 
104651ffecc1SBen Gras 			if (off & __ALTCHARSET && exit_alt_charset_mode != NULL) {
104751ffecc1SBen Gras 				tputs(exit_alt_charset_mode, 0, __cputchar);
104851ffecc1SBen Gras 				curscr->wattr &= ~__ALTCHARSET;
104951ffecc1SBen Gras 			}
105051ffecc1SBen Gras 
105151ffecc1SBen Gras 			/* Set/change colour as appropriate. */
105251ffecc1SBen Gras 			if (__using_color)
105351ffecc1SBen Gras 				__set_color(curscr, nsp->attr & __COLOR);
105451ffecc1SBen Gras 
105551ffecc1SBen Gras 			on = (nsp->attr & ~curscr->wattr)
105651ffecc1SBen Gras #ifndef HAVE_WCHAR
105751ffecc1SBen Gras 				 & __ATTRIBUTES
105851ffecc1SBen Gras #else
105951ffecc1SBen Gras 				 & WA_ATTRIBUTES
106051ffecc1SBen Gras #endif
106151ffecc1SBen Gras 				 ;
106251ffecc1SBen Gras 
106351ffecc1SBen Gras 			/*
106451ffecc1SBen Gras 			 * Enter standout mode if appropriate.
106551ffecc1SBen Gras 			 */
106651ffecc1SBen Gras 			if (on & __STANDOUT &&
106751ffecc1SBen Gras 			    enter_standout_mode != NULL &&
106851ffecc1SBen Gras 			    exit_standout_mode != NULL)
106951ffecc1SBen Gras 			{
107051ffecc1SBen Gras 				tputs(enter_standout_mode, 0, __cputchar);
107151ffecc1SBen Gras 				curscr->wattr |= __STANDOUT;
107251ffecc1SBen Gras 			}
107351ffecc1SBen Gras 
107451ffecc1SBen Gras 			/*
107551ffecc1SBen Gras 			 * Enter underscore mode if appropriate.
107651ffecc1SBen Gras 			 * XXX
107751ffecc1SBen Gras 			 * Should use uc if us/ue not available.
107851ffecc1SBen Gras 			 */
107951ffecc1SBen Gras 			if (on & __UNDERSCORE &&
108051ffecc1SBen Gras 			    enter_underline_mode != NULL &&
108151ffecc1SBen Gras 			    exit_underline_mode != NULL)
108251ffecc1SBen Gras 			{
108351ffecc1SBen Gras 				tputs(enter_underline_mode, 0, __cputchar);
108451ffecc1SBen Gras 				curscr->wattr |= __UNDERSCORE;
108551ffecc1SBen Gras 			}
108651ffecc1SBen Gras 
108751ffecc1SBen Gras 			/*
108851ffecc1SBen Gras 			 * Set other attributes as appropriate.
108951ffecc1SBen Gras 			 */
109051ffecc1SBen Gras 			if (exit_attribute_mode != NULL) {
109151ffecc1SBen Gras 				if (on & __BLINK &&
109251ffecc1SBen Gras 				    enter_blink_mode != NULL)
109351ffecc1SBen Gras 				{
109451ffecc1SBen Gras 					tputs(enter_blink_mode, 0, __cputchar);
109551ffecc1SBen Gras 					curscr->wattr |= __BLINK;
109651ffecc1SBen Gras 				}
109751ffecc1SBen Gras 				if (on & __BOLD &&
109851ffecc1SBen Gras 				    enter_bold_mode != NULL)
109951ffecc1SBen Gras 				{
110051ffecc1SBen Gras 					tputs(enter_bold_mode, 0, __cputchar);
110151ffecc1SBen Gras 					curscr->wattr |= __BOLD;
110251ffecc1SBen Gras 				}
110351ffecc1SBen Gras 				if (on & __DIM &&
110451ffecc1SBen Gras 				    enter_dim_mode != NULL)
110551ffecc1SBen Gras 				{
110651ffecc1SBen Gras 					tputs(enter_dim_mode, 0, __cputchar);
110751ffecc1SBen Gras 					curscr->wattr |= __DIM;
110851ffecc1SBen Gras 				}
110951ffecc1SBen Gras 				if (on & __BLANK &&
111051ffecc1SBen Gras 				    enter_secure_mode != NULL)
111151ffecc1SBen Gras 				{
111251ffecc1SBen Gras 					tputs(enter_secure_mode, 0, __cputchar);
111351ffecc1SBen Gras 					curscr->wattr |= __BLANK;
111451ffecc1SBen Gras 				}
111551ffecc1SBen Gras 				if (on & __PROTECT &&
111651ffecc1SBen Gras 				    enter_protected_mode != NULL)
111751ffecc1SBen Gras 				{
111851ffecc1SBen Gras 					tputs(enter_protected_mode, 0, __cputchar);
111951ffecc1SBen Gras 					curscr->wattr |= __PROTECT;
112051ffecc1SBen Gras 				}
112151ffecc1SBen Gras 				if (on & __REVERSE &&
112251ffecc1SBen Gras 				    enter_reverse_mode != NULL)
112351ffecc1SBen Gras 				{
112451ffecc1SBen Gras 					tputs(enter_reverse_mode, 0, __cputchar);
112551ffecc1SBen Gras 					curscr->wattr |= __REVERSE;
112651ffecc1SBen Gras 				}
112751ffecc1SBen Gras #ifdef HAVE_WCHAR
112851ffecc1SBen Gras 				if (on & WA_TOP &&
112951ffecc1SBen Gras 				    enter_top_hl_mode != NULL)
113051ffecc1SBen Gras 				{
113151ffecc1SBen Gras 					tputs(enter_top_hl_mode, 0, __cputchar);
113251ffecc1SBen Gras 					curscr->wattr |= WA_TOP;
113351ffecc1SBen Gras 				}
113451ffecc1SBen Gras 				if (on & WA_LOW &&
113551ffecc1SBen Gras 				    enter_low_hl_mode != NULL)
113651ffecc1SBen Gras 				{
113751ffecc1SBen Gras 					tputs(enter_low_hl_mode, 0, __cputchar);
113851ffecc1SBen Gras 					curscr->wattr |= WA_LOW;
113951ffecc1SBen Gras 				}
114051ffecc1SBen Gras 				if (on & WA_LEFT &&
114151ffecc1SBen Gras 				    enter_left_hl_mode != NULL)
114251ffecc1SBen Gras 				{
114351ffecc1SBen Gras 					tputs(enter_left_hl_mode, 0, __cputchar);
114451ffecc1SBen Gras 					curscr->wattr |= WA_LEFT;
114551ffecc1SBen Gras 				}
114651ffecc1SBen Gras 				if (on & WA_RIGHT &&
114751ffecc1SBen Gras 				    enter_right_hl_mode != NULL)
114851ffecc1SBen Gras 				{
114951ffecc1SBen Gras 					tputs(enter_right_hl_mode, 0, __cputchar);
115051ffecc1SBen Gras 					curscr->wattr |= WA_RIGHT;
115151ffecc1SBen Gras 				}
115251ffecc1SBen Gras 				if (on & WA_HORIZONTAL &&
115351ffecc1SBen Gras 				    enter_horizontal_hl_mode != NULL)
115451ffecc1SBen Gras 				{
115551ffecc1SBen Gras 					tputs(enter_horizontal_hl_mode, 0, __cputchar);
115651ffecc1SBen Gras 					curscr->wattr |= WA_HORIZONTAL;
115751ffecc1SBen Gras 				}
115851ffecc1SBen Gras 				if (on & WA_VERTICAL &&
115951ffecc1SBen Gras 				    enter_vertical_hl_mode != NULL)
116051ffecc1SBen Gras 				{
116151ffecc1SBen Gras 					tputs(enter_vertical_hl_mode, 0, __cputchar);
116251ffecc1SBen Gras 					curscr->wattr |= WA_VERTICAL;
116351ffecc1SBen Gras 				}
116451ffecc1SBen Gras #endif /* HAVE_WCHAR */
116551ffecc1SBen Gras 			}
116651ffecc1SBen Gras 
116751ffecc1SBen Gras 			/* Enter/exit altcharset mode as appropriate. */
116851ffecc1SBen Gras 			if (on & __ALTCHARSET && enter_alt_charset_mode != NULL &&
116951ffecc1SBen Gras 			    exit_alt_charset_mode != NULL) {
117051ffecc1SBen Gras 				tputs(enter_alt_charset_mode, 0, __cputchar);
117151ffecc1SBen Gras 				curscr->wattr |= __ALTCHARSET;
117251ffecc1SBen Gras 			}
117351ffecc1SBen Gras 
117451ffecc1SBen Gras 			wx++;
117551ffecc1SBen Gras 			if (wx >= win->maxx &&
117651ffecc1SBen Gras 			    wy == win->maxy - 1 && !_cursesi_screen->curwin) {
117751ffecc1SBen Gras 				if (win->flags & __SCROLLOK) {
117851ffecc1SBen Gras 					if (win->flags & __ENDLINE)
117951ffecc1SBen Gras 						__unsetattr(1);
118051ffecc1SBen Gras 					if (!(win->flags & __SCROLLWIN)) {
118151ffecc1SBen Gras 						if (!_cursesi_screen->curwin) {
118251ffecc1SBen Gras 							csp->attr = nsp->attr;
118351ffecc1SBen Gras 							csp->ch = nsp->ch;
118451ffecc1SBen Gras #ifdef HAVE_WCHAR
118551ffecc1SBen Gras 							if (_cursesi_copy_nsp(nsp->nsp, csp) == ERR)
118651ffecc1SBen Gras 								return ERR;
118751ffecc1SBen Gras #endif /* HAVE_WCHAR */
118851ffecc1SBen Gras 						}
118951ffecc1SBen Gras #ifndef HAVE_WCHAR
119051ffecc1SBen Gras 						__cputchar((int) nsp->ch);
119151ffecc1SBen Gras #else
119251ffecc1SBen Gras 						if ( WCOL( *nsp ) > 0 ) {
119351ffecc1SBen Gras 							__cputwchar((int)nsp->ch);
119451ffecc1SBen Gras #ifdef DEBUG
119551ffecc1SBen Gras 							__CTRACE(__CTRACE_REFRESH,
119651ffecc1SBen Gras 							    "makech: (%d,%d)putwchar(0x%x)\n",
119751ffecc1SBen Gras 								wy, wx - 1,
119851ffecc1SBen Gras 								nsp->ch );
119951ffecc1SBen Gras #endif /* DEBUG */
120051ffecc1SBen Gras 							/*
120151ffecc1SBen Gras 							 * Output non-spacing
120251ffecc1SBen Gras 							 * characters for the
120351ffecc1SBen Gras 							 * cell.
120451ffecc1SBen Gras 							 */
120551ffecc1SBen Gras 							__cursesi_putnsp(nsp->nsp,
120651ffecc1SBen Gras 									 wy, wx);
120751ffecc1SBen Gras 
120851ffecc1SBen Gras 						}
120951ffecc1SBen Gras #endif /* HAVE_WCHAR */
121051ffecc1SBen Gras 					}
121151ffecc1SBen Gras 					if (wx < curscr->maxx) {
121251ffecc1SBen Gras 						domvcur(_cursesi_screen->ly, wx,
121351ffecc1SBen Gras 						    (int) (win->maxy - 1),
121451ffecc1SBen Gras 						    (int) (win->maxx - 1));
121551ffecc1SBen Gras 					}
121651ffecc1SBen Gras 					_cursesi_screen->ly = win->maxy - 1;
121751ffecc1SBen Gras 					_cursesi_screen->lx = win->maxx - 1;
121851ffecc1SBen Gras 					return (OK);
121951ffecc1SBen Gras 				}
122051ffecc1SBen Gras 			}
122151ffecc1SBen Gras 			if (wx < win->maxx || wy < win->maxy - 1 ||
122251ffecc1SBen Gras 			    !(win->flags & __SCROLLWIN)) {
122351ffecc1SBen Gras 				if (!_cursesi_screen->curwin) {
122451ffecc1SBen Gras 					csp->attr = nsp->attr;
122551ffecc1SBen Gras 					csp->ch = nsp->ch;
122651ffecc1SBen Gras #ifdef HAVE_WCHAR
122751ffecc1SBen Gras 					if (_cursesi_copy_nsp(nsp->nsp,
122851ffecc1SBen Gras 							      csp) == ERR)
122951ffecc1SBen Gras 						return ERR;
123051ffecc1SBen Gras #endif /* HAVE_WCHAR */
123151ffecc1SBen Gras 					csp++;
123251ffecc1SBen Gras 				}
123351ffecc1SBen Gras #ifndef HAVE_WCHAR
123451ffecc1SBen Gras 				__cputchar((int) nsp->ch);
123551ffecc1SBen Gras #ifdef DEBUG
123651ffecc1SBen Gras 				__CTRACE(__CTRACE_REFRESH,
123751ffecc1SBen Gras 				    "makech: putchar(%c)\n", nsp->ch & 0177);
123851ffecc1SBen Gras #endif
123951ffecc1SBen Gras #else
124051ffecc1SBen Gras 				if (WCOL(*nsp) > 0) {
124151ffecc1SBen Gras 					__cputwchar((int) nsp->ch);
124251ffecc1SBen Gras #ifdef DEBUG
124351ffecc1SBen Gras 					__CTRACE(__CTRACE_REFRESH,
124451ffecc1SBen Gras 					    "makech:(%d,%d) putwchar(%x)\n",
124551ffecc1SBen Gras 					    wy, wx - 1, nsp->ch);
124651ffecc1SBen Gras 					__cursesi_putnsp(nsp->nsp, wy, wx);
124751ffecc1SBen Gras #endif /* DEBUG */
124851ffecc1SBen Gras 				}
124951ffecc1SBen Gras #endif /* HAVE_WCHAR */
125051ffecc1SBen Gras 			}
125151ffecc1SBen Gras 			if (underline_char && ((nsp->attr & __STANDOUT) ||
125251ffecc1SBen Gras 			    (nsp->attr & __UNDERSCORE))) {
125351ffecc1SBen Gras 				__cputchar('\b');
125451ffecc1SBen Gras 				tputs(underline_char, 0, __cputchar);
125551ffecc1SBen Gras 			}
125651ffecc1SBen Gras 			nsp++;
125751ffecc1SBen Gras #ifdef DEBUG
125851ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH,
125951ffecc1SBen Gras 			    "makech: 2: wx = %d, lx = %d\n",
126051ffecc1SBen Gras 			    wx, _cursesi_screen->lx);
126151ffecc1SBen Gras #endif
126251ffecc1SBen Gras 		}
126351ffecc1SBen Gras 		if (_cursesi_screen->lx == wx)	/* If no change. */
126451ffecc1SBen Gras 			break;
126551ffecc1SBen Gras 		_cursesi_screen->lx = wx;
126651ffecc1SBen Gras 		if (_cursesi_screen->lx >= COLS && auto_right_margin)
126751ffecc1SBen Gras 			_cursesi_screen->lx = COLS - 1;
126851ffecc1SBen Gras 		else
126951ffecc1SBen Gras 			if (wx >= win->maxx) {
127051ffecc1SBen Gras 				domvcur(_cursesi_screen->ly,
127151ffecc1SBen Gras 					_cursesi_screen->lx,
127251ffecc1SBen Gras 					_cursesi_screen->ly,
127351ffecc1SBen Gras 					(int) (win->maxx - 1));
127451ffecc1SBen Gras 				_cursesi_screen->lx = win->maxx - 1;
127551ffecc1SBen Gras 			}
127651ffecc1SBen Gras #ifdef DEBUG
127751ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, "makech: 3: wx = %d, lx = %d\n",
127851ffecc1SBen Gras 		    wx, _cursesi_screen->lx);
127951ffecc1SBen Gras #endif
128051ffecc1SBen Gras 	}
128151ffecc1SBen Gras #ifdef DEBUG
128251ffecc1SBen Gras #if HAVE_WCHAR
128351ffecc1SBen Gras 	{
128451ffecc1SBen Gras 		int x;
128551ffecc1SBen Gras 		__LDATA *lp, *vlp;
128651ffecc1SBen Gras 
128751ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH,
128851ffecc1SBen Gras 		    "makech-after: curscr(%p)-__virtscr(%p)\n",
128951ffecc1SBen Gras 		    curscr, __virtscr );
129051ffecc1SBen Gras 		for (x = 0; x < curscr->maxx; x++) {
129151ffecc1SBen Gras 			lp = &curscr->alines[wy]->line[x];
129251ffecc1SBen Gras 			vlp = &__virtscr->alines[wy]->line[x];
129351ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH,
129451ffecc1SBen Gras 			    "[%d,%d](%x,%x,%x,%x,%p)-"
129551ffecc1SBen Gras 			    "(%x,%x,%x,%x,%p)\n",
129651ffecc1SBen Gras 			    wy, x, lp->ch, lp->attr,
129751ffecc1SBen Gras 			    win->bch, win->battr, lp->nsp,
129851ffecc1SBen Gras 			    vlp->ch, vlp->attr,
129951ffecc1SBen Gras 			    win->bch, win->battr, vlp->nsp);
130051ffecc1SBen Gras 		}
130151ffecc1SBen Gras 	}
130251ffecc1SBen Gras #endif /* HAVE_WCHAR */
130351ffecc1SBen Gras #endif /* DEBUG */
130451ffecc1SBen Gras 
130551ffecc1SBen Gras 	return (OK);
130651ffecc1SBen Gras }
130751ffecc1SBen Gras 
130851ffecc1SBen Gras /*
130951ffecc1SBen Gras  * domvcur --
131051ffecc1SBen Gras  *	Do a mvcur, leaving attributes if necessary.
131151ffecc1SBen Gras  */
131251ffecc1SBen Gras static void
131351ffecc1SBen Gras domvcur(oy, ox, ny, nx)
131451ffecc1SBen Gras 	int	oy, ox, ny, nx;
131551ffecc1SBen Gras {
131651ffecc1SBen Gras #ifdef DEBUG
131751ffecc1SBen Gras 	__CTRACE(__CTRACE_REFRESH, "domvcur: (%x,%d)=>(%d,%d)\n",
131851ffecc1SBen Gras 	    oy, ox, ny, nx );
131951ffecc1SBen Gras #endif /* DEBUG */
132051ffecc1SBen Gras 	__unsetattr(1);
132151ffecc1SBen Gras 	if ( oy == ny && ox == nx )
132251ffecc1SBen Gras 		return;
132351ffecc1SBen Gras 	__mvcur(oy, ox, ny, nx, 1);
132451ffecc1SBen Gras }
132551ffecc1SBen Gras 
132651ffecc1SBen Gras /*
132751ffecc1SBen Gras  * Quickch() attempts to detect a pattern in the change of the window
132851ffecc1SBen Gras  * in order to optimize the change, e.g., scroll n lines as opposed to
132951ffecc1SBen Gras  * repainting the screen line by line.
133051ffecc1SBen Gras  */
133151ffecc1SBen Gras 
133251ffecc1SBen Gras static __LDATA buf[128];
133351ffecc1SBen Gras static  u_int last_hash;
133451ffecc1SBen Gras static  size_t last_hash_len;
133551ffecc1SBen Gras #define BLANKSIZE (sizeof(buf) / sizeof(buf[0]))
133651ffecc1SBen Gras 
133751ffecc1SBen Gras static void
133851ffecc1SBen Gras quickch(void)
133951ffecc1SBen Gras {
134051ffecc1SBen Gras #define THRESH		(int) __virtscr->maxy / 4
134151ffecc1SBen Gras 
134251ffecc1SBen Gras 	__LINE *clp, *tmp1, *tmp2;
134351ffecc1SBen Gras 	int	bsize, curs, curw, starts, startw, i, j;
134451ffecc1SBen Gras 	int	n, target, cur_period, bot, top, sc_region;
134551ffecc1SBen Gras 	u_int	blank_hash;
134651ffecc1SBen Gras 	attr_t	bcolor;
134751ffecc1SBen Gras 
134851ffecc1SBen Gras #ifdef __GNUC__
134951ffecc1SBen Gras 	curs = curw = starts = startw = 0;	/* XXX gcc -Wuninitialized */
135051ffecc1SBen Gras #endif
135151ffecc1SBen Gras 	/*
135251ffecc1SBen Gras 	 * Find how many lines from the top of the screen are unchanged.
135351ffecc1SBen Gras 	 */
135451ffecc1SBen Gras 	for (top = 0; top < __virtscr->maxy; top++)
135551ffecc1SBen Gras #ifndef HAVE_WCHAR
135651ffecc1SBen Gras 		if (__virtscr->alines[top]->flags & __ISDIRTY &&
135751ffecc1SBen Gras 		    (__virtscr->alines[top]->hash != curscr->alines[top]->hash ||
135851ffecc1SBen Gras 		    memcmp(__virtscr->alines[top]->line,
135951ffecc1SBen Gras 		    curscr->alines[top]->line,
136051ffecc1SBen Gras 		    (size_t) __virtscr->maxx * __LDATASIZE)
136151ffecc1SBen Gras 		    != 0))
136251ffecc1SBen Gras 			break;
136351ffecc1SBen Gras #else
136451ffecc1SBen Gras 		if (__virtscr->alines[top]->flags & __ISDIRTY &&
136551ffecc1SBen Gras 		    (__virtscr->alines[top]->hash != curscr->alines[top]->hash ||
136651ffecc1SBen Gras 		    !linecmp(__virtscr->alines[top]->line,
136751ffecc1SBen Gras 		    curscr->alines[top]->line,
136851ffecc1SBen Gras 	(size_t) __virtscr->maxx )))
136951ffecc1SBen Gras 			break;
137051ffecc1SBen Gras #endif /* HAVE_WCHAR */
137151ffecc1SBen Gras 		else
137251ffecc1SBen Gras 			__virtscr->alines[top]->flags &= ~__ISDIRTY;
137351ffecc1SBen Gras 	/*
137451ffecc1SBen Gras 	 * Find how many lines from bottom of screen are unchanged.
137551ffecc1SBen Gras 	 */
137651ffecc1SBen Gras 	for (bot = __virtscr->maxy - 1; bot >= 0; bot--)
137751ffecc1SBen Gras #ifndef HAVE_WCHAR
137851ffecc1SBen Gras 		if (__virtscr->alines[bot]->flags & __ISDIRTY &&
137951ffecc1SBen Gras 		    (__virtscr->alines[bot]->hash != curscr->alines[bot]->hash ||
138051ffecc1SBen Gras 		    memcmp(__virtscr->alines[bot]->line,
138151ffecc1SBen Gras 		    curscr->alines[bot]->line,
138251ffecc1SBen Gras 		    (size_t) __virtscr->maxx * __LDATASIZE)
138351ffecc1SBen Gras 		    != 0))
138451ffecc1SBen Gras 			break;
138551ffecc1SBen Gras #else
138651ffecc1SBen Gras 		if (__virtscr->alines[bot]->flags & __ISDIRTY &&
138751ffecc1SBen Gras 		    (__virtscr->alines[bot]->hash != curscr->alines[bot]->hash ||
138851ffecc1SBen Gras 		    !linecmp(__virtscr->alines[bot]->line,
138951ffecc1SBen Gras 		    curscr->alines[bot]->line,
139051ffecc1SBen Gras 		    (size_t) __virtscr->maxx )))
139151ffecc1SBen Gras 			break;
139251ffecc1SBen Gras #endif /* HAVE_WCHAR */
139351ffecc1SBen Gras 		else
139451ffecc1SBen Gras 			__virtscr->alines[bot]->flags &= ~__ISDIRTY;
139551ffecc1SBen Gras 
139651ffecc1SBen Gras 	/*
139751ffecc1SBen Gras 	 * Work round an xterm bug where inserting lines causes all the
139851ffecc1SBen Gras 	 * inserted lines to be covered with the background colour we
139951ffecc1SBen Gras 	 * set on the first line (even if we unset it for subsequent
140051ffecc1SBen Gras 	 * lines).
140151ffecc1SBen Gras 	 */
140251ffecc1SBen Gras 	bcolor = __virtscr->alines[min(top,
140351ffecc1SBen Gras 	    __virtscr->maxy - 1)]->line[0].attr & __COLOR;
140451ffecc1SBen Gras 	for (i = top + 1, j = 0; i < bot; i++) {
140551ffecc1SBen Gras 		if ((__virtscr->alines[i]->line[0].attr & __COLOR) != bcolor) {
140651ffecc1SBen Gras 			bcolor = __virtscr->alines[i]->line[__virtscr->maxx].
140751ffecc1SBen Gras 			    attr & __COLOR;
140851ffecc1SBen Gras 			j = i - top;
140951ffecc1SBen Gras 		} else
141051ffecc1SBen Gras 			break;
141151ffecc1SBen Gras 	}
141251ffecc1SBen Gras 	top += j;
141351ffecc1SBen Gras 
141451ffecc1SBen Gras #ifdef NO_JERKINESS
141551ffecc1SBen Gras 	/*
141651ffecc1SBen Gras 	 * If we have a bottom unchanged region return.  Scrolling the
141751ffecc1SBen Gras 	 * bottom region up and then back down causes a screen jitter.
141851ffecc1SBen Gras 	 * This will increase the number of characters sent to the screen
141951ffecc1SBen Gras 	 * but it looks better.
142051ffecc1SBen Gras 	 */
142151ffecc1SBen Gras 	if (bot < __virtscr->maxy - 1)
142251ffecc1SBen Gras 		return;
142351ffecc1SBen Gras #endif				/* NO_JERKINESS */
142451ffecc1SBen Gras 
142551ffecc1SBen Gras 	/*
142651ffecc1SBen Gras 	 * Search for the largest block of text not changed.
142751ffecc1SBen Gras 	 * Invariants of the loop:
142851ffecc1SBen Gras 	 * - Startw is the index of the beginning of the examined block in
142951ffecc1SBen Gras 	 *   __virtscr.
143051ffecc1SBen Gras 	 * - Starts is the index of the beginning of the examined block in
143151ffecc1SBen Gras 	 *   curscr.
143251ffecc1SBen Gras 	 * - Curw is the index of one past the end of the exmined block in
143351ffecc1SBen Gras 	 *   __virtscr.
143451ffecc1SBen Gras 	 * - Curs is the index of one past the end of the exmined block in
143551ffecc1SBen Gras 	 *   curscr.
143651ffecc1SBen Gras 	 * - bsize is the current size of the examined block.
143751ffecc1SBen Gras 	*/
143851ffecc1SBen Gras 
143951ffecc1SBen Gras 	for (bsize = bot - top; bsize >= THRESH; bsize--) {
144051ffecc1SBen Gras 		for (startw = top; startw <= bot - bsize; startw++)
144151ffecc1SBen Gras 			for (starts = top; starts <= bot - bsize;
144251ffecc1SBen Gras 				starts++) {
144351ffecc1SBen Gras 				for (curw = startw, curs = starts;
144451ffecc1SBen Gras 				    curs < starts + bsize; curw++, curs++)
144551ffecc1SBen Gras 					if (__virtscr->alines[curw]->hash !=
144651ffecc1SBen Gras 					    curscr->alines[curs]->hash)
144751ffecc1SBen Gras 						break;
144851ffecc1SBen Gras 				if (curs != starts + bsize)
144951ffecc1SBen Gras 					continue;
145051ffecc1SBen Gras 				for (curw = startw, curs = starts;
145151ffecc1SBen Gras 					curs < starts + bsize; curw++, curs++)
145251ffecc1SBen Gras #ifndef HAVE_WCHAR
145351ffecc1SBen Gras 					if (memcmp(__virtscr->alines[curw]->line,
145451ffecc1SBen Gras 					    curscr->alines[curs]->line,
145551ffecc1SBen Gras 					    (size_t) __virtscr->maxx *
145651ffecc1SBen Gras 					    __LDATASIZE) != 0)
145751ffecc1SBen Gras 						break;
145851ffecc1SBen Gras #else
145951ffecc1SBen Gras 					if (!linecmp(__virtscr->alines[curw]->line,
146051ffecc1SBen Gras 					    curscr->alines[curs]->line,
146151ffecc1SBen Gras 					    (size_t) __virtscr->maxx))
146251ffecc1SBen Gras 						break;
146351ffecc1SBen Gras #endif /* HAVE_WCHAR */
146451ffecc1SBen Gras 				if (curs == starts + bsize)
146551ffecc1SBen Gras 					goto done;
146651ffecc1SBen Gras 			}
146751ffecc1SBen Gras 	}
146851ffecc1SBen Gras done:
146951ffecc1SBen Gras 
147051ffecc1SBen Gras 	/* Did not find anything */
147151ffecc1SBen Gras 	if (bsize < THRESH)
147251ffecc1SBen Gras 		return;
147351ffecc1SBen Gras 
147451ffecc1SBen Gras #ifdef DEBUG
147551ffecc1SBen Gras 	__CTRACE(__CTRACE_REFRESH, "quickch:bsize=%d, starts=%d, startw=%d, "
147651ffecc1SBen Gras 	    "curw=%d, curs=%d, top=%d, bot=%d\n",
147751ffecc1SBen Gras 	    bsize, starts, startw, curw, curs, top, bot);
147851ffecc1SBen Gras #endif
147951ffecc1SBen Gras 
148051ffecc1SBen Gras 	/*
148151ffecc1SBen Gras 	 * Make sure that there is no overlap between the bottom and top
148251ffecc1SBen Gras 	 * regions and the middle scrolled block.
148351ffecc1SBen Gras 	 */
148451ffecc1SBen Gras 	if (bot < curs)
148551ffecc1SBen Gras 		bot = curs - 1;
148651ffecc1SBen Gras 	if (top > starts)
148751ffecc1SBen Gras 		top = starts;
148851ffecc1SBen Gras 
148951ffecc1SBen Gras 	n = startw - starts;
149051ffecc1SBen Gras 
149151ffecc1SBen Gras #ifdef DEBUG
149251ffecc1SBen Gras 	__CTRACE(__CTRACE_REFRESH, "#####################################\n");
149351ffecc1SBen Gras 	for (i = 0; i < curscr->maxy; i++) {
149451ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, "C: %d:", i);
149551ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, " 0x%x \n", curscr->alines[i]->hash);
149651ffecc1SBen Gras 		for (j = 0; j < curscr->maxx; j++)
149751ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, "%c",
149851ffecc1SBen Gras 			    curscr->alines[i]->line[j].ch);
149951ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, "\n");
150051ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, " attr:");
150151ffecc1SBen Gras 		for (j = 0; j < curscr->maxx; j++)
150251ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, " %x",
150351ffecc1SBen Gras 			    curscr->alines[i]->line[j].attr);
150451ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, "\n");
150551ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, "W: %d:", i);
150651ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, " 0x%x \n",
150751ffecc1SBen Gras 		    __virtscr->alines[i]->hash);
150851ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, " 0x%x ",
150951ffecc1SBen Gras 		    __virtscr->alines[i]->flags);
151051ffecc1SBen Gras 		for (j = 0; j < __virtscr->maxx; j++)
151151ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, "%c",
151251ffecc1SBen Gras 			    __virtscr->alines[i]->line[j].ch);
151351ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, "\n");
151451ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, " attr:");
151551ffecc1SBen Gras 		for (j = 0; j < __virtscr->maxx; j++)
151651ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, " %x",
151751ffecc1SBen Gras 			    __virtscr->alines[i]->line[j].attr);
151851ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, "\n");
151951ffecc1SBen Gras 	}
152051ffecc1SBen Gras #endif
152151ffecc1SBen Gras 
152251ffecc1SBen Gras #ifndef HAVE_WCHAR
152351ffecc1SBen Gras 	if (buf[0].ch != ' ') {
152451ffecc1SBen Gras 		for (i = 0; i < BLANKSIZE; i++) {
152551ffecc1SBen Gras 			buf[i].ch = ' ';
152651ffecc1SBen Gras 			buf[i].attr = 0;
152751ffecc1SBen Gras 		}
152851ffecc1SBen Gras 	}
152951ffecc1SBen Gras #else
153051ffecc1SBen Gras 	if (buf[0].ch != ( wchar_t )btowc(( int ) curscr->bch )) {
153151ffecc1SBen Gras 		for (i = 0; i < BLANKSIZE; i++) {
153251ffecc1SBen Gras 			buf[i].ch = ( wchar_t )btowc(( int ) curscr->bch );
153351ffecc1SBen Gras 			if (_cursesi_copy_nsp(curscr->bnsp, &buf[i]) == ERR)
153451ffecc1SBen Gras 				return;
153551ffecc1SBen Gras 			buf[i].attr = 0;
153651ffecc1SBen Gras 			SET_WCOL( buf[ i ], 1 );
153751ffecc1SBen Gras 		}
153851ffecc1SBen Gras 	}
153951ffecc1SBen Gras #endif /* HAVE_WCHAR */
154051ffecc1SBen Gras 
154151ffecc1SBen Gras 	if (__virtscr->maxx != last_hash_len) {
154251ffecc1SBen Gras 		blank_hash = 0;
154351ffecc1SBen Gras 		for (i = __virtscr->maxx; i > BLANKSIZE; i -= BLANKSIZE) {
154451ffecc1SBen Gras 			blank_hash = __hash_more(buf, sizeof(buf), blank_hash);
154551ffecc1SBen Gras 		}
154651ffecc1SBen Gras 		blank_hash = __hash_more((char *)(void *)buf,
154751ffecc1SBen Gras 		    i * sizeof(buf[0]), blank_hash);
154851ffecc1SBen Gras 		/* cache result in static data - screen width doesn't change often */
154951ffecc1SBen Gras 		last_hash_len = __virtscr->maxx;
155051ffecc1SBen Gras 		last_hash = blank_hash;
155151ffecc1SBen Gras 	} else
155251ffecc1SBen Gras 		blank_hash = last_hash;
155351ffecc1SBen Gras 
155451ffecc1SBen Gras 	/*
155551ffecc1SBen Gras 	 * Perform the rotation to maintain the consistency of curscr.
155651ffecc1SBen Gras 	 * This is hairy since we are doing an *in place* rotation.
155751ffecc1SBen Gras 	 * Invariants of the loop:
155851ffecc1SBen Gras 	 * - I is the index of the current line.
155951ffecc1SBen Gras 	 * - Target is the index of the target of line i.
156051ffecc1SBen Gras 	 * - Tmp1 points to current line (i).
156151ffecc1SBen Gras 	 * - Tmp2 and points to target line (target);
156251ffecc1SBen Gras 	 * - Cur_period is the index of the end of the current period.
156351ffecc1SBen Gras 	 *   (see below).
156451ffecc1SBen Gras 	 *
156551ffecc1SBen Gras 	 * There are 2 major issues here that make this rotation non-trivial:
156651ffecc1SBen Gras 	 * 1.  Scrolling in a scrolling region bounded by the top
156751ffecc1SBen Gras 	 *     and bottom regions determined (whose size is sc_region).
156851ffecc1SBen Gras 	 * 2.  As a result of the use of the mod function, there may be a
156951ffecc1SBen Gras 	 *     period introduced, i.e., 2 maps to 4, 4 to 6, n-2 to 0, and
157051ffecc1SBen Gras 	 *     0 to 2, which then causes all odd lines not to be rotated.
157151ffecc1SBen Gras 	 *     To remedy this, an index of the end ( = beginning) of the
157251ffecc1SBen Gras 	 *     current 'period' is kept, cur_period, and when it is reached,
157351ffecc1SBen Gras 	 *     the next period is started from cur_period + 1 which is
157451ffecc1SBen Gras 	 *     guaranteed not to have been reached since that would mean that
157551ffecc1SBen Gras 	 *     all records would have been reached. (think about it...).
157651ffecc1SBen Gras 	 *
157751ffecc1SBen Gras 	 * Lines in the rotation can have 3 attributes which are marked on the
157851ffecc1SBen Gras 	 * line so that curscr is consistent with the visual screen.
157951ffecc1SBen Gras 	 * 1.  Not dirty -- lines inside the scrolled block, top region or
158051ffecc1SBen Gras 	 *                  bottom region.
158151ffecc1SBen Gras 	 * 2.  Blank lines -- lines in the differential of the scrolling
158251ffecc1SBen Gras 	 *		      region adjacent to top and bot regions
158351ffecc1SBen Gras 	 *                    depending on scrolling direction.
158451ffecc1SBen Gras 	 * 3.  Dirty line -- all other lines are marked dirty.
158551ffecc1SBen Gras 	 */
158651ffecc1SBen Gras 	sc_region = bot - top + 1;
158751ffecc1SBen Gras 	i = top;
158851ffecc1SBen Gras 	tmp1 = curscr->alines[top];
158951ffecc1SBen Gras 	cur_period = top;
159051ffecc1SBen Gras 	for (j = top; j <= bot; j++) {
159151ffecc1SBen Gras 		target = (i - top + n + sc_region) % sc_region + top;
159251ffecc1SBen Gras 		tmp2 = curscr->alines[target];
159351ffecc1SBen Gras 		curscr->alines[target] = tmp1;
159451ffecc1SBen Gras 		/* Mark block as clean and blank out scrolled lines. */
159551ffecc1SBen Gras 		clp = curscr->alines[target];
159651ffecc1SBen Gras #ifdef DEBUG
159751ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH,
159851ffecc1SBen Gras 		    "quickch: n=%d startw=%d curw=%d i = %d target=%d ",
159951ffecc1SBen Gras 		    n, startw, curw, i, target);
160051ffecc1SBen Gras #endif
160151ffecc1SBen Gras 		if ((target >= startw && target < curw) || target < top
160251ffecc1SBen Gras 		    || target > bot) {
160351ffecc1SBen Gras #ifdef DEBUG
160451ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, " notdirty\n");
160551ffecc1SBen Gras #endif
160651ffecc1SBen Gras 			__virtscr->alines[target]->flags &= ~__ISDIRTY;
160751ffecc1SBen Gras 		} else
160851ffecc1SBen Gras 			if ((n > 0 && target >= top && target < top + n) ||
160951ffecc1SBen Gras 			    (n < 0 && target <= bot && target > bot + n)) {
161051ffecc1SBen Gras #ifndef HAVE_WCHAR
161151ffecc1SBen Gras 				if (clp->hash != blank_hash ||
161251ffecc1SBen Gras 				    memcmp(clp->line, clp->line + 1,
161351ffecc1SBen Gras 				    (__virtscr->maxx - 1)
161451ffecc1SBen Gras 				    * __LDATASIZE) ||
161551ffecc1SBen Gras 				    memcmp(clp->line, buf, __LDATASIZE)) {
161651ffecc1SBen Gras #else
161751ffecc1SBen Gras 				if (clp->hash != blank_hash
161851ffecc1SBen Gras 				    || linecmp(clp->line, clp->line + 1,
161951ffecc1SBen Gras 				    (unsigned int) (__virtscr->maxx - 1))
162051ffecc1SBen Gras 				    || cellcmp(clp->line, buf)) {
162151ffecc1SBen Gras #endif /* HAVE_WCHAR */
162251ffecc1SBen Gras 					for (i = __virtscr->maxx;
162351ffecc1SBen Gras 					    i > BLANKSIZE;
162451ffecc1SBen Gras 					    i -= BLANKSIZE) {
162551ffecc1SBen Gras 						(void) memcpy(clp->line + i -
162651ffecc1SBen Gras 						    BLANKSIZE, buf, sizeof(buf));
162751ffecc1SBen Gras 					}
162851ffecc1SBen Gras 					(void) memcpy(clp->line , buf, i *
162951ffecc1SBen Gras 					    sizeof(buf[0]));
163051ffecc1SBen Gras #ifdef DEBUG
163151ffecc1SBen Gras 					__CTRACE(__CTRACE_REFRESH,
163251ffecc1SBen Gras 					    " blanked out: dirty\n");
163351ffecc1SBen Gras #endif
163451ffecc1SBen Gras 					clp->hash = blank_hash;
163551ffecc1SBen Gras 					__touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
163651ffecc1SBen Gras 				} else {
163751ffecc1SBen Gras #ifdef DEBUG
163851ffecc1SBen Gras 					__CTRACE(__CTRACE_REFRESH,
163951ffecc1SBen Gras 					    " -- blank line already: dirty\n");
164051ffecc1SBen Gras #endif
164151ffecc1SBen Gras 					__touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
164251ffecc1SBen Gras 				}
164351ffecc1SBen Gras 			} else {
164451ffecc1SBen Gras #ifdef DEBUG
164551ffecc1SBen Gras 				__CTRACE(__CTRACE_REFRESH, " -- dirty\n");
164651ffecc1SBen Gras #endif
164751ffecc1SBen Gras 				__touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
164851ffecc1SBen Gras 			}
164951ffecc1SBen Gras 		if (target == cur_period) {
165051ffecc1SBen Gras 			i = target + 1;
165151ffecc1SBen Gras 			tmp1 = curscr->alines[i];
165251ffecc1SBen Gras 			cur_period = i;
165351ffecc1SBen Gras 		} else {
165451ffecc1SBen Gras 			tmp1 = tmp2;
165551ffecc1SBen Gras 			i = target;
165651ffecc1SBen Gras 		}
165751ffecc1SBen Gras 	}
165851ffecc1SBen Gras #ifdef DEBUG
165951ffecc1SBen Gras 	__CTRACE(__CTRACE_REFRESH, "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
166051ffecc1SBen Gras 	for (i = 0; i < curscr->maxy; i++) {
166151ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, "C: %d:", i);
166251ffecc1SBen Gras 		for (j = 0; j < curscr->maxx; j++)
166351ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, "%c",
166451ffecc1SBen Gras 			    curscr->alines[i]->line[j].ch);
166551ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, "\n");
166651ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, "W: %d:", i);
166751ffecc1SBen Gras 		for (j = 0; j < __virtscr->maxx; j++)
166851ffecc1SBen Gras 			__CTRACE(__CTRACE_REFRESH, "%c",
166951ffecc1SBen Gras 			    __virtscr->alines[i]->line[j].ch);
167051ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH, "\n");
167151ffecc1SBen Gras 	}
167251ffecc1SBen Gras #endif
167351ffecc1SBen Gras 	if (n != 0)
167451ffecc1SBen Gras 		scrolln(starts, startw, curs, bot, top);
167551ffecc1SBen Gras }
167651ffecc1SBen Gras 
167751ffecc1SBen Gras /*
167851ffecc1SBen Gras  * scrolln --
167951ffecc1SBen Gras  *	Scroll n lines, where n is starts - startw.
168051ffecc1SBen Gras  */
168151ffecc1SBen Gras static void /* ARGSUSED */
168251ffecc1SBen Gras scrolln(starts, startw, curs, bot, top)
168351ffecc1SBen Gras 	int	starts, startw, curs, bot, top;
168451ffecc1SBen Gras {
168551ffecc1SBen Gras 	int	i, oy, ox, n;
168651ffecc1SBen Gras 
168751ffecc1SBen Gras 	oy = curscr->cury;
168851ffecc1SBen Gras 	ox = curscr->curx;
168951ffecc1SBen Gras 	n = starts - startw;
169051ffecc1SBen Gras 
169151ffecc1SBen Gras 	/*
169251ffecc1SBen Gras 	 * XXX
169351ffecc1SBen Gras 	 * The initial tests that set __noqch don't let us reach here unless
169451ffecc1SBen Gras 	 * we have either cs + ho + SF/sf/SR/sr, or AL + DL.  SF/sf and SR/sr
169551ffecc1SBen Gras 	 * scrolling can only shift the entire scrolling region, not just a
169651ffecc1SBen Gras 	 * part of it, which means that the quickch() routine is going to be
169751ffecc1SBen Gras 	 * sadly disappointed in us if we don't have cs as well.
169851ffecc1SBen Gras 	 *
169951ffecc1SBen Gras 	 * If cs, ho and SF/sf are set, can use the scrolling region.  Because
170051ffecc1SBen Gras 	 * the cursor position after cs is undefined, we need ho which gives us
170151ffecc1SBen Gras 	 * the ability to move to somewhere without knowledge of the current
170251ffecc1SBen Gras 	 * location of the cursor.  Still call __mvcur() anyway, to update its
170351ffecc1SBen Gras 	 * idea of where the cursor is.
170451ffecc1SBen Gras 	 *
170551ffecc1SBen Gras 	 * When the scrolling region has been set, the cursor has to be at the
170651ffecc1SBen Gras 	 * last line of the region to make the scroll happen.
170751ffecc1SBen Gras 	 *
170851ffecc1SBen Gras 	 * Doing SF/SR or AL/DL appears faster on the screen than either sf/sr
170951ffecc1SBen Gras 	 * or AL/DL, and, some terminals have AL/DL, sf/sr, and cs, but not
171051ffecc1SBen Gras 	 * SF/SR.  So, if we're scrolling almost all of the screen, try and use
171151ffecc1SBen Gras 	 * AL/DL, otherwise use the scrolling region.  The "almost all" is a
171251ffecc1SBen Gras 	 * shameless hack for vi.
171351ffecc1SBen Gras 	 */
171451ffecc1SBen Gras 	if (n > 0) {
171551ffecc1SBen Gras 		if (change_scroll_region != NULL && cursor_home != NULL &&
171651ffecc1SBen Gras 		    (parm_index != NULL ||
171751ffecc1SBen Gras 		    ((parm_insert_line == NULL || parm_delete_line == NULL ||
171851ffecc1SBen Gras 		    top > 3 || bot + 3 < __virtscr->maxy) &&
171951ffecc1SBen Gras 		    scroll_forward != NULL)))
172051ffecc1SBen Gras 		{
17210c3ae37fSLionel Sambuc 			tputs(tiparm(change_scroll_region, top, bot),
172251ffecc1SBen Gras 			    0, __cputchar);
172351ffecc1SBen Gras 			__mvcur(oy, ox, 0, 0, 1);
172451ffecc1SBen Gras 			tputs(cursor_home, 0, __cputchar);
172551ffecc1SBen Gras 			__mvcur(0, 0, bot, 0, 1);
172651ffecc1SBen Gras 			if (parm_index != NULL)
17270c3ae37fSLionel Sambuc 				tputs(tiparm(parm_index, n),
172851ffecc1SBen Gras 				    0, __cputchar);
172951ffecc1SBen Gras 			else
173051ffecc1SBen Gras 				for (i = 0; i < n; i++)
173151ffecc1SBen Gras 					tputs(scroll_forward, 0, __cputchar);
17320c3ae37fSLionel Sambuc 			tputs(tiparm(change_scroll_region,
173351ffecc1SBen Gras 			    0, (int) __virtscr->maxy - 1), 0, __cputchar);
173451ffecc1SBen Gras 			__mvcur(bot, 0, 0, 0, 1);
173551ffecc1SBen Gras 			tputs(cursor_home, 0, __cputchar);
173651ffecc1SBen Gras 			__mvcur(0, 0, oy, ox, 1);
173751ffecc1SBen Gras 			return;
173851ffecc1SBen Gras 		}
173951ffecc1SBen Gras 
174051ffecc1SBen Gras 		/* Scroll up the block. */
174151ffecc1SBen Gras 		if (parm_index != NULL && top == 0) {
174251ffecc1SBen Gras 			__mvcur(oy, ox, bot, 0, 1);
17430c3ae37fSLionel Sambuc 			tputs(tiparm(parm_index, n), 0, __cputchar);
174451ffecc1SBen Gras 		} else
174551ffecc1SBen Gras 			if (parm_delete_line != NULL) {
174651ffecc1SBen Gras 				__mvcur(oy, ox, top, 0, 1);
17470c3ae37fSLionel Sambuc 				tputs(tiparm(parm_delete_line, n),
174851ffecc1SBen Gras 				    0, __cputchar);
174951ffecc1SBen Gras 			} else
175051ffecc1SBen Gras 				if (delete_line != NULL) {
175151ffecc1SBen Gras 					__mvcur(oy, ox, top, 0, 1);
175251ffecc1SBen Gras 					for (i = 0; i < n; i++)
175351ffecc1SBen Gras 						tputs(delete_line,
175451ffecc1SBen Gras 						    0, __cputchar);
175551ffecc1SBen Gras 				} else
175651ffecc1SBen Gras 					if (scroll_forward != NULL && top == 0) {
175751ffecc1SBen Gras 						__mvcur(oy, ox, bot, 0, 1);
175851ffecc1SBen Gras 						for (i = 0; i < n; i++)
175951ffecc1SBen Gras 							tputs(scroll_forward, 0,
176051ffecc1SBen Gras 							    __cputchar);
176151ffecc1SBen Gras 					} else
176251ffecc1SBen Gras 						abort();
176351ffecc1SBen Gras 
176451ffecc1SBen Gras 		/* Push down the bottom region. */
176551ffecc1SBen Gras 		__mvcur(top, 0, bot - n + 1, 0, 1);
176651ffecc1SBen Gras 		if (parm_insert_line != NULL)
17670c3ae37fSLionel Sambuc 			tputs(tiparm(parm_insert_line, n), 0, __cputchar);
176851ffecc1SBen Gras 		else
176951ffecc1SBen Gras 			if (insert_line != NULL)
177051ffecc1SBen Gras 				for (i = 0; i < n; i++)
177151ffecc1SBen Gras 					tputs(insert_line, 0, __cputchar);
177251ffecc1SBen Gras 			else
177351ffecc1SBen Gras 				abort();
177451ffecc1SBen Gras 		__mvcur(bot - n + 1, 0, oy, ox, 1);
177551ffecc1SBen Gras 	} else {
177651ffecc1SBen Gras 		/*
177751ffecc1SBen Gras 		 * !!!
177851ffecc1SBen Gras 		 * n < 0
177951ffecc1SBen Gras 		 *
178051ffecc1SBen Gras 		 * If cs, ho and SR/sr are set, can use the scrolling region.
178151ffecc1SBen Gras 		 * See the above comments for details.
178251ffecc1SBen Gras 		 */
178351ffecc1SBen Gras 		if (change_scroll_region != NULL && cursor_home != NULL &&
178451ffecc1SBen Gras 		    (parm_rindex != NULL ||
178551ffecc1SBen Gras 		    ((parm_insert_line == NULL || parm_delete_line == NULL ||
178651ffecc1SBen Gras 		    top > 3 ||
178751ffecc1SBen Gras 		    bot + 3 < __virtscr->maxy) && scroll_reverse != NULL)))
178851ffecc1SBen Gras 		{
17890c3ae37fSLionel Sambuc 			tputs(tiparm(change_scroll_region, top, bot),
179051ffecc1SBen Gras 			    0, __cputchar);
179151ffecc1SBen Gras 			__mvcur(oy, ox, 0, 0, 1);
179251ffecc1SBen Gras 			tputs(cursor_home, 0, __cputchar);
179351ffecc1SBen Gras 			__mvcur(0, 0, top, 0, 1);
179451ffecc1SBen Gras 
179551ffecc1SBen Gras 			if (parm_rindex != NULL)
17960c3ae37fSLionel Sambuc 				tputs(tiparm(parm_rindex, -n),
179751ffecc1SBen Gras 				    0, __cputchar);
179851ffecc1SBen Gras 			else
179951ffecc1SBen Gras 				for (i = n; i < 0; i++)
180051ffecc1SBen Gras 					tputs(scroll_reverse, 0, __cputchar);
18010c3ae37fSLionel Sambuc 			tputs(tiparm(change_scroll_region,
180251ffecc1SBen Gras 			    0, (int) __virtscr->maxy - 1), 0, __cputchar);
180351ffecc1SBen Gras 			__mvcur(top, 0, 0, 0, 1);
180451ffecc1SBen Gras 			tputs(cursor_home, 0, __cputchar);
180551ffecc1SBen Gras 			__mvcur(0, 0, oy, ox, 1);
180651ffecc1SBen Gras 			return;
180751ffecc1SBen Gras 		}
180851ffecc1SBen Gras 
180951ffecc1SBen Gras 		/* Preserve the bottom lines. */
181051ffecc1SBen Gras 		__mvcur(oy, ox, bot + n + 1, 0, 1);
181151ffecc1SBen Gras 		if (parm_rindex != NULL && bot == __virtscr->maxy)
18120c3ae37fSLionel Sambuc 			tputs(tiparm(parm_rindex, -n), 0, __cputchar);
181351ffecc1SBen Gras 		else
181451ffecc1SBen Gras 			if (parm_delete_line != NULL)
18150c3ae37fSLionel Sambuc 				tputs(tiparm(parm_delete_line, -n),
181651ffecc1SBen Gras 				    0, __cputchar);
181751ffecc1SBen Gras 			else
181851ffecc1SBen Gras 				if (delete_line != NULL)
181951ffecc1SBen Gras 					for (i = n; i < 0; i++)
182051ffecc1SBen Gras 						tputs(delete_line,
182151ffecc1SBen Gras 						    0, __cputchar);
182251ffecc1SBen Gras 				else
182351ffecc1SBen Gras 					if (scroll_reverse != NULL &&
182451ffecc1SBen Gras 					    bot == __virtscr->maxy)
182551ffecc1SBen Gras 						for (i = n; i < 0; i++)
182651ffecc1SBen Gras 							tputs(scroll_reverse, 0,
182751ffecc1SBen Gras 							    __cputchar);
182851ffecc1SBen Gras 					else
182951ffecc1SBen Gras 						abort();
183051ffecc1SBen Gras 
183151ffecc1SBen Gras 		/* Scroll the block down. */
183251ffecc1SBen Gras 		__mvcur(bot + n + 1, 0, top, 0, 1);
183351ffecc1SBen Gras 		if (parm_insert_line != NULL)
18340c3ae37fSLionel Sambuc 			tputs(tiparm(parm_insert_line, -n), 0, __cputchar);
183551ffecc1SBen Gras 		else
183651ffecc1SBen Gras 			if (insert_line != NULL)
183751ffecc1SBen Gras 				for (i = n; i < 0; i++)
183851ffecc1SBen Gras 					tputs(insert_line, 0, __cputchar);
183951ffecc1SBen Gras 			else
184051ffecc1SBen Gras 				abort();
184151ffecc1SBen Gras 		__mvcur(top, 0, oy, ox, 1);
184251ffecc1SBen Gras 	}
184351ffecc1SBen Gras }
184451ffecc1SBen Gras 
184551ffecc1SBen Gras /*
184651ffecc1SBen Gras  * __unsetattr --
184751ffecc1SBen Gras  *	Unset attributes on curscr.  Leave standout, attribute and colour
184851ffecc1SBen Gras  *	modes if necessary (!ms).  Always leave altcharset (xterm at least
184951ffecc1SBen Gras  *	ignores a cursor move if we don't).
185051ffecc1SBen Gras  */
185151ffecc1SBen Gras void /* ARGSUSED */
185251ffecc1SBen Gras __unsetattr(int checkms)
185351ffecc1SBen Gras {
185451ffecc1SBen Gras 	int	isms;
185551ffecc1SBen Gras 
185651ffecc1SBen Gras 	if (checkms)
185751ffecc1SBen Gras 		if (!move_standout_mode) {
185851ffecc1SBen Gras 			isms = 1;
185951ffecc1SBen Gras 		} else {
186051ffecc1SBen Gras 			isms = 0;
186151ffecc1SBen Gras 		}
186251ffecc1SBen Gras 	else
186351ffecc1SBen Gras 		isms = 1;
186451ffecc1SBen Gras #ifdef DEBUG
186551ffecc1SBen Gras 	__CTRACE(__CTRACE_REFRESH,
186651ffecc1SBen Gras 	    "__unsetattr: checkms = %d, ms = %s, wattr = %08x\n",
186751ffecc1SBen Gras 	    checkms, move_standout_mode ? "TRUE" : "FALSE", curscr->wattr);
186851ffecc1SBen Gras #endif
186951ffecc1SBen Gras 
187051ffecc1SBen Gras 	/*
187151ffecc1SBen Gras 	 * Don't leave the screen in standout mode (check against ms).  Check
187251ffecc1SBen Gras 	 * to see if we also turn off underscore, attributes and colour.
187351ffecc1SBen Gras 	 */
187451ffecc1SBen Gras 	if (curscr->wattr & __STANDOUT && isms) {
187551ffecc1SBen Gras 		tputs(exit_standout_mode, 0, __cputchar);
187651ffecc1SBen Gras 		curscr->wattr &= __mask_se;
187751ffecc1SBen Gras 	}
187851ffecc1SBen Gras 	/*
187951ffecc1SBen Gras 	 * Don't leave the screen in underscore mode (check against ms).
188051ffecc1SBen Gras 	 * Check to see if we also turn off attributes.  Assume that we
188151ffecc1SBen Gras 	 * also turn off colour.
188251ffecc1SBen Gras 	 */
188351ffecc1SBen Gras 	if (curscr->wattr & __UNDERSCORE && isms) {
188451ffecc1SBen Gras 		tputs(exit_underline_mode, 0, __cputchar);
188551ffecc1SBen Gras 		curscr->wattr &= __mask_ue;
188651ffecc1SBen Gras 	}
188751ffecc1SBen Gras 	/*
188851ffecc1SBen Gras 	 * Don't leave the screen with attributes set (check against ms).
188951ffecc1SBen Gras 	 * Assume that also turn off colour.
189051ffecc1SBen Gras 	 */
189151ffecc1SBen Gras 	if (curscr->wattr & __TERMATTR && isms) {
189251ffecc1SBen Gras 		tputs(exit_attribute_mode, 0, __cputchar);
189351ffecc1SBen Gras 		curscr->wattr &= __mask_me;
189451ffecc1SBen Gras 	}
189551ffecc1SBen Gras 	/* Don't leave the screen with altcharset set (don't check ms). */
189651ffecc1SBen Gras 	if (curscr->wattr & __ALTCHARSET) {
189751ffecc1SBen Gras 		tputs(exit_alt_charset_mode, 0, __cputchar);
189851ffecc1SBen Gras 		curscr->wattr &= ~__ALTCHARSET;
189951ffecc1SBen Gras 	}
190051ffecc1SBen Gras 	/* Don't leave the screen with colour set (check against ms). */
190151ffecc1SBen Gras 	if (__using_color && isms)
190251ffecc1SBen Gras 		__unset_color(curscr);
190351ffecc1SBen Gras }
190451ffecc1SBen Gras 
190551ffecc1SBen Gras #ifdef HAVE_WCHAR
190651ffecc1SBen Gras /* compare two cells on screen, must have the same forground/background,
190751ffecc1SBen Gras  * and the same sequence of non-spacing characters */
190851ffecc1SBen Gras int
190951ffecc1SBen Gras cellcmp( __LDATA *x, __LDATA *y )
191051ffecc1SBen Gras {
191151ffecc1SBen Gras 	nschar_t *xnp = x->nsp, *ynp = y->nsp;
191251ffecc1SBen Gras 	int ret = ( x->ch == y->ch ) & ( x->attr == y->attr );
191351ffecc1SBen Gras 
191451ffecc1SBen Gras 	if ( !ret )
191551ffecc1SBen Gras 		return 0;
191651ffecc1SBen Gras 	if ( !xnp && !ynp )
191751ffecc1SBen Gras 		return 1;
191851ffecc1SBen Gras 	if (( xnp && !ynp ) || ( !xnp && ynp ))
191951ffecc1SBen Gras 		return 0;
192051ffecc1SBen Gras 
192151ffecc1SBen Gras 	while ( xnp && ynp ) {
192251ffecc1SBen Gras 		if ( xnp->ch != ynp->ch )
192351ffecc1SBen Gras 			return 0;
192451ffecc1SBen Gras 		xnp = xnp->next;
192551ffecc1SBen Gras 		ynp = ynp->next;
192651ffecc1SBen Gras 	}
192751ffecc1SBen Gras 	return ( !xnp && !ynp );
192851ffecc1SBen Gras }
192951ffecc1SBen Gras 
193051ffecc1SBen Gras /* compare two line segments */
193151ffecc1SBen Gras int
193251ffecc1SBen Gras linecmp( __LDATA *xl, __LDATA *yl, size_t len )
193351ffecc1SBen Gras {
193451ffecc1SBen Gras 	int i = 0;
193551ffecc1SBen Gras 	__LDATA *xp = xl, *yp = yl;
193651ffecc1SBen Gras 
193751ffecc1SBen Gras 	for ( i = 0; i < len; i++, xp++, yp++ ) {
193851ffecc1SBen Gras 		if ( !cellcmp( xp, yp ))
193951ffecc1SBen Gras 			return 0;
194051ffecc1SBen Gras 	}
194151ffecc1SBen Gras 	return 1;
194251ffecc1SBen Gras }
194351ffecc1SBen Gras 
194451ffecc1SBen Gras /*
194551ffecc1SBen Gras  * Output the non-spacing characters associated with the given character
194651ffecc1SBen Gras  * cell to the screen.
194751ffecc1SBen Gras  */
194851ffecc1SBen Gras 
194951ffecc1SBen Gras void
195051ffecc1SBen Gras __cursesi_putnsp(nschar_t *nsp, const int wy, const int wx)
195151ffecc1SBen Gras {
195251ffecc1SBen Gras 	nschar_t *p;
195351ffecc1SBen Gras 
195451ffecc1SBen Gras 	/* this shuts up gcc warnings about wx and wy not being used */
195551ffecc1SBen Gras 	if (wx > wy) {
195651ffecc1SBen Gras 	}
195751ffecc1SBen Gras 
195851ffecc1SBen Gras 	p = nsp;
195951ffecc1SBen Gras 	while (p != NULL) {
196051ffecc1SBen Gras 		__cputwchar((int) p->ch);
196151ffecc1SBen Gras #ifdef DEBUG
196251ffecc1SBen Gras 		__CTRACE(__CTRACE_REFRESH,
196351ffecc1SBen Gras 		       "_cursesi_putnsp: (%d,%d) non-spacing putwchar(0x%x)\n",
196451ffecc1SBen Gras 			 wy, wx - 1, p->ch);
196551ffecc1SBen Gras #endif
196651ffecc1SBen Gras 		p = p->next;
196751ffecc1SBen Gras 	}
196851ffecc1SBen Gras }
196951ffecc1SBen Gras 
197051ffecc1SBen Gras #endif /* HAVE_WCHAR */
1971