Home | History | Annotate | Line # | Download | only in libcurses
refresh.c revision 1.106
      1 /*	$NetBSD: refresh.c,v 1.106 2019/03/29 16:56:58 roy Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1981, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)refresh.c	8.7 (Berkeley) 8/13/94";
     36 #else
     37 __RCSID("$NetBSD: refresh.c,v 1.106 2019/03/29 16:56:58 roy Exp $");
     38 #endif
     39 #endif				/* not lint */
     40 
     41 #include <poll.h>
     42 #include <stdlib.h>
     43 #include <string.h>
     44 
     45 #include "curses.h"
     46 #include "curses_private.h"
     47 
     48 static void	domvcur(const WINDOW *, int, int, int, int);
     49 static void	putattr(__LDATA *);
     50 static void	putattr_out(__LDATA *);
     51 static int	putch(__LDATA *, __LDATA *, int, int);
     52 static int	putchbr(__LDATA *, __LDATA *, __LDATA *, int, int);
     53 static int	makech(int);
     54 static void	quickch(void);
     55 static void	scrolln(int, int, int, int, int);
     56 
     57 static int	_wnoutrefresh(WINDOW *, int, int, int, int, int, int);
     58 
     59 #ifdef HAVE_WCHAR
     60 static int celleq(__LDATA *, __LDATA *);
     61 static int lineeq(__LDATA *, __LDATA *, size_t);
     62 #else  /* !HAVE_WCHAR */
     63 static inline int
     64 celleq(__LDATA *x, __LDATA *y)
     65 {
     66 	return memcmp(x, y, sizeof(__LDATA)) == 0;
     67 }
     68 
     69 static int
     70 lineeq(__LDATA *xl, __LDATA *yl, size_t len)
     71 {
     72 	return memcmp(xl, yl, len * __LDATASIZE) == 0;
     73 }
     74 #endif /* HAVE_WCHAR */
     75 
     76 #define	CHECK_INTERVAL		5 /* Change N lines before checking typeahead */
     77 
     78 #ifndef _CURSES_USE_MACROS
     79 
     80 /*
     81  * refresh --
     82  *	Make the current screen look like "stdscr" over the area covered by
     83  *	stdscr.
     84  */
     85 int
     86 refresh(void)
     87 {
     88 
     89 	return wrefresh(stdscr);
     90 }
     91 
     92 #endif
     93 
     94 /*
     95  * wnoutrefresh --
     96  *	Add the contents of "win" to the virtual window.
     97  */
     98 int
     99 wnoutrefresh(WINDOW *win)
    100 {
    101 
    102 #ifdef DEBUG
    103 	__CTRACE(__CTRACE_REFRESH, "wnoutrefresh: win %p\n", win);
    104 #endif
    105 
    106 	return _wnoutrefresh(win, 0, 0, win->begy, win->begx,
    107 	    win->maxy, win->maxx);
    108 }
    109 
    110 /*
    111  * pnoutrefresh --
    112  *	Add the contents of "pad" to the virtual window.
    113  */
    114 int
    115 pnoutrefresh(WINDOW *pad, int pbegy, int pbegx, int sbegy, int sbegx,
    116 	     int smaxy, int smaxx)
    117 {
    118 	int pmaxy, pmaxx;
    119 
    120 #ifdef DEBUG
    121 	__CTRACE(__CTRACE_REFRESH, "pnoutrefresh: pad %p, flags 0x%08x\n",
    122 	    pad, pad->flags);
    123 	__CTRACE(__CTRACE_REFRESH,
    124 	    "pnoutrefresh: (%d, %d), (%d, %d), (%d, %d)\n",
    125 	    pbegy, pbegx, sbegy, sbegx, smaxy, smaxx);
    126 #endif
    127 
    128 	/* SUS says if these are negative, they should be treated as zero */
    129 	if (pbegy < 0)
    130 		pbegy = 0;
    131 	if (pbegx < 0)
    132 		pbegx = 0;
    133 	if (sbegy < 0)
    134 		sbegy = 0;
    135 	if (sbegx < 0)
    136 		sbegx = 0;
    137 
    138 	/* Calculate rectangle on pad - used by _wnoutrefresh */
    139 	pmaxy = pbegy + smaxy - sbegy + 1;
    140 	pmaxx = pbegx + smaxx - sbegx + 1;
    141 
    142 	/* Check rectangle fits in pad */
    143 	if (pmaxy > pad->maxy - pad->begy)
    144 		pmaxy = pad->maxy - pad->begy;
    145 	if (pmaxx > pad->maxx - pad->begx)
    146 		pmaxx = pad->maxx - pad->begx;
    147 
    148 	if (smaxy - sbegy < 0 || smaxx - sbegx < 0 )
    149 		return ERR;
    150 
    151 	return _wnoutrefresh(pad,
    152 	    pad->begy + pbegy, pad->begx + pbegx, pad->begy + sbegy,
    153 	    pad->begx + sbegx, pmaxy, pmaxx);
    154 }
    155 
    156 /*
    157  * _wnoutrefresh --
    158  *	Does the grunt work for wnoutrefresh to the given screen.
    159  *	Copies the part of the window given by the rectangle
    160  *	(begy, begx) to (maxy, maxx) at screen position (wbegy, wbegx).
    161  */
    162 static int
    163 _wnoutrefresh(WINDOW *win, int begy, int begx, int wbegy, int wbegx,
    164               int maxy, int maxx)
    165 {
    166 	SCREEN *screen = win->screen;
    167 	short	sy, wy, wx, y_off, x_off, mx, dy_off, dx_off, endy;
    168 	int newy, newx;
    169 	__LINE	*wlp, *vlp, *dwlp;
    170 	WINDOW	*sub_win, *orig, *swin, *dwin;
    171 
    172 #ifdef DEBUG
    173 	__CTRACE(__CTRACE_REFRESH, "_wnoutrefresh: win %p, flags 0x%08x\n",
    174 	    win, win->flags);
    175 	__CTRACE(__CTRACE_REFRESH,
    176 	    "_wnoutrefresh: (%d, %d), (%d, %d), (%d, %d)\n",
    177 	    begy, begx, wbegy, wbegx, maxy, maxx);
    178 #endif
    179 
    180 	if (screen->curwin)
    181 		return OK;
    182 
    183 	swin = dwin = win;
    184 	if (win->flags & __ISDERWIN)
    185 		swin = win->orig;
    186 
    187 	/*
    188 	 * Recurse through any sub-windows, mark as dirty lines on the parent
    189 	 * window that are dirty on the sub-window and clear the dirty flag on
    190 	 * the sub-window.
    191 	 */
    192 	if (dwin->orig == 0) {
    193 		orig = dwin;
    194 		for (sub_win = dwin->nextp; sub_win != orig;
    195 		    sub_win = sub_win->nextp) {
    196 			if (sub_win->flags & __ISDERWIN)
    197 				continue;
    198 #ifdef DEBUG
    199 			__CTRACE(__CTRACE_REFRESH,
    200 			    "wnout_refresh: win %p, sub_win %p\n",
    201 			    orig, sub_win);
    202 #endif
    203 			for (sy = 0; sy < sub_win->maxy; sy++) {
    204 				if (sub_win->alines[sy]->flags & __ISDIRTY) {
    205 					orig->alines[sy + sub_win->begy - orig->begy]->flags
    206 					    |= __ISDIRTY;
    207 					sub_win->alines[sy]->flags
    208 					    &= ~__ISDIRTY;
    209 				}
    210 				if (sub_win->alines[sy]->flags & __ISFORCED) {
    211 					orig->alines[sy + sub_win->begy - orig->begy]->flags
    212 					    |= __ISFORCED;
    213 					sub_win->alines[sy]->flags
    214 					    &= ~__ISFORCED;
    215 				}
    216 			}
    217 		}
    218 	}
    219 
    220 	/* Check that cursor position on "win" is valid for "__virtscr" */
    221 	newy = wbegy + dwin->cury - begy;
    222 	newx = wbegx + dwin->curx - begx;
    223 	if (begy <= dwin->cury && dwin->cury < maxy
    224 	    && 0 <= newy && newy < screen->__virtscr->maxy)
    225 		screen->__virtscr->cury = newy;
    226 	if (begx <= dwin->curx && dwin->curx < maxx
    227 	    && 0 <= newx && newx < screen->__virtscr->maxx)
    228 		screen->__virtscr->curx = newx;
    229 
    230 	/* Copy the window flags from "win" to "__virtscr" */
    231 	if (dwin->flags & __CLEAROK) {
    232 		if (dwin->flags & __FULLWIN)
    233 			screen->__virtscr->flags |= __CLEAROK;
    234 		dwin->flags &= ~__CLEAROK;
    235 	}
    236 	screen->__virtscr->flags &= ~__LEAVEOK;
    237 	screen->__virtscr->flags |= dwin->flags;
    238 
    239 	if ((dwin->flags & __ISDERWIN) != 0)
    240 		endy = begy + maxy;
    241 	else
    242 		endy = maxy;
    243 
    244 	for (wy = begy, y_off = wbegy, dy_off = 0; wy < endy &&
    245 	    y_off < screen->__virtscr->maxy; wy++, y_off++, dy_off++)
    246 	{
    247 		wlp = swin->alines[wy];
    248 		dwlp = dwin->alines[dy_off];
    249 #ifdef DEBUG
    250 		__CTRACE(__CTRACE_REFRESH,
    251 		    "_wnoutrefresh: wy %d\tf %d\tl %d\tflags %x\n",
    252 		    wy, *wlp->firstchp, *wlp->lastchp, wlp->flags);
    253 
    254 		if ((dwin->flags & __ISDERWIN) != 0) {
    255 			__CTRACE(__CTRACE_REFRESH,
    256 			"_wnoutrefresh: derwin wy %d\tf %d\tl %d\tflags %x\n",
    257 			dy_off, *dwlp->firstchp, *dwlp->lastchp, dwlp->flags);
    258 			__CTRACE(__CTRACE_REFRESH,
    259 			"_wnoutrefresh: derwin maxx %d\tch_off %d\n",
    260 			dwin->maxx, dwin->ch_off);
    261 		}
    262 #endif
    263 		if (((wlp->flags & (__ISDIRTY | __ISFORCED)) == 0) &&
    264 		    ((dwlp->flags & (__ISDIRTY | __ISFORCED)) == 0))
    265 			continue;
    266 		vlp = screen->__virtscr->alines[y_off];
    267 
    268 		if ((*wlp->firstchp < maxx + swin->ch_off &&
    269 		    *wlp->lastchp >= swin->ch_off) ||
    270 		    ((((dwin->flags & __ISDERWIN) != 0) &&
    271 		     (*dwlp->firstchp < dwin->maxx + dwin->ch_off &&
    272 		      *dwlp->lastchp >= dwin->ch_off))))
    273 		{
    274 			/* Set start column */
    275 			wx = begx;
    276 			x_off = wbegx;
    277 			dx_off = 0;
    278 			/*
    279 			 * if a derwin then source change pointers aren't
    280 			 * relevant.
    281 			 */
    282 			if ((dwin->flags & __ISDERWIN) != 0)
    283 				mx = wx + maxx;
    284 			else {
    285 				if (*wlp->firstchp - swin->ch_off > 0) {
    286 					wx += *wlp->firstchp - swin->ch_off;
    287 					x_off += *wlp->firstchp - swin->ch_off;
    288 				}
    289 				mx = maxx;
    290 				if (mx > *wlp->lastchp - swin->ch_off + 1)
    291 					mx = *dwlp->lastchp - dwin->ch_off + 1;
    292 				if (x_off + (mx - wx) > screen->__virtscr->maxx)
    293 					mx -= (x_off + maxx) -
    294 					    screen->__virtscr->maxx;
    295 			}
    296 
    297 			/* Copy line from "win" to "__virtscr". */
    298 			while (wx < mx) {
    299 #ifdef DEBUG
    300 				__CTRACE(__CTRACE_REFRESH,
    301 				    "_wnoutrefresh: copy from %d, "
    302 				    "%d to %d, %d: %s, 0x%x",
    303 				    wy, wx, y_off, x_off,
    304 				    unctrl(wlp->line[wx].ch),
    305 				    wlp->line[wx].attr);
    306 #endif
    307 				/* Copy character */
    308 				vlp->line[x_off].ch = wlp->line[wx].ch;
    309 				/* Copy attributes  */
    310 				vlp->line[x_off].attr = wlp->line[wx].attr;
    311 				/* Check for nca conflict with colour */
    312 				if ((vlp->line[x_off].attr & __COLOR) &&
    313 				    (vlp->line[x_off].attr & screen->nca))
    314 					vlp->line[x_off].attr &= ~__COLOR;
    315 				if (win->flags & __ISDERWIN) {
    316 					dwlp->line[dx_off].ch =
    317 						wlp->line[wx].ch;
    318 					dwlp->line[dx_off].attr =
    319 						wlp->line[wx].attr;
    320 				}
    321 
    322 #ifdef HAVE_WCHAR
    323 				if (wlp->line[wx].ch
    324 				    == (wchar_t)btowc((int) win->bch)) {
    325 					vlp->line[x_off].ch = win->bch;
    326 					SET_WCOL(vlp->line[x_off], 1);
    327 					if (_cursesi_copy_nsp(win->bnsp,
    328 							      &vlp->line[x_off])
    329 					    == ERR)
    330 						return ERR;
    331 					if (win->flags & __ISDERWIN) {
    332 						dwlp->line[dx_off].ch =
    333 							win->bch;
    334 						SET_WCOL(dwlp->line[dx_off], 1);
    335 						if (_cursesi_copy_nsp(win->bnsp,
    336 						     &dwlp->line[dx_off])
    337 						    == ERR)
    338 							return ERR;
    339 					}
    340 				}
    341 #endif /* HAVE_WCHAR */
    342 #ifdef DEBUG
    343 				__CTRACE(__CTRACE_REFRESH, " = %s, 0x%x\n",
    344 				    unctrl(vlp->line[x_off].ch),
    345 				    vlp->line[x_off].attr);
    346 #endif
    347 				wx++;
    348 				x_off++;
    349 				dx_off++;
    350 			}
    351 
    352 			/* Set flags on "__virtscr" and unset on "win". */
    353 			if (wlp->flags & __ISPASTEOL)
    354 				vlp->flags |= __ISPASTEOL;
    355 			else
    356 				vlp->flags &= ~__ISPASTEOL;
    357 			if (wlp->flags & __ISDIRTY)
    358 				vlp->flags |= __ISDIRTY;
    359 			if (wlp->flags & __ISFORCED)
    360 				vlp->flags |= __ISFORCED;
    361 
    362 #ifdef DEBUG
    363 			__CTRACE(__CTRACE_REFRESH,
    364 			    "win: firstch = %d, lastch = %d\n",
    365 			    *wlp->firstchp, *wlp->lastchp);
    366 			if (win->flags & __ISDERWIN) {
    367 				__CTRACE(__CTRACE_REFRESH,
    368 				    "derwin: fistch = %d, lastch = %d\n",
    369 				    *dwlp->firstchp, *dwlp->lastchp);
    370 			}
    371 #endif
    372 			/* Set change pointers on "__virtscr". */
    373 			if (*vlp->firstchp >
    374 				*wlp->firstchp + wbegx - win->ch_off)
    375 					*vlp->firstchp =
    376 					    *wlp->firstchp + wbegx - win->ch_off;
    377 			if (*vlp->lastchp <
    378 				*wlp->lastchp + wbegx - win->ch_off)
    379 					*vlp->lastchp =
    380 					    *wlp->lastchp + wbegx - win->ch_off;
    381 
    382 			if (win->flags & __ISDERWIN) {
    383 				if (*vlp->firstchp >
    384 				    *dwlp->firstchp + wbegx - dwin->ch_off)
    385 				{
    386 					*vlp->firstchp =
    387 					    *dwlp->firstchp + wbegx
    388 						- dwin->ch_off;
    389 					vlp->flags |= __ISDIRTY;
    390 				}
    391 
    392 				if (*vlp->lastchp <
    393 				    *dwlp->lastchp + wbegx - dwin->ch_off)
    394 				{
    395 					*vlp->lastchp = *dwlp->lastchp
    396 					    + wbegx - dwin->ch_off;
    397 					vlp->flags |= __ISDIRTY;
    398 				}
    399 			}
    400 
    401 #ifdef DEBUG
    402 			__CTRACE(__CTRACE_REFRESH,
    403 			    "__virtscr: firstch = %d, lastch = %d\n",
    404 			    *vlp->firstchp, *vlp->lastchp);
    405 #endif
    406 			/*
    407 			 * Unset change pointers only if a window and we
    408 			 * are not forcing a redraw. A pad can be displayed
    409 			 * again without any of the contents changing.
    410 			 */
    411 			if ((!(win->flags & __ISPAD)) ||
    412 			    ((wlp->flags & __ISFORCED) == __ISFORCED))
    413 			{
    414 				/* Set change pointers on "win". */
    415 				if (*wlp->firstchp >= win->ch_off)
    416 					*wlp->firstchp = maxx + win->ch_off;
    417 				if (*wlp->lastchp < maxx + win->ch_off)
    418 					*wlp->lastchp = win->ch_off;
    419 				if ((*wlp->lastchp < *wlp->firstchp) ||
    420 				    (*wlp->firstchp >= maxx + win->ch_off) ||
    421 				    (*wlp->lastchp <= win->ch_off)) {
    422 #ifdef DEBUG
    423 					__CTRACE(__CTRACE_REFRESH,
    424 					    "_wnoutrefresh: "
    425 					    "line %d notdirty\n", wy);
    426 #endif
    427 					wlp->flags &= ~(__ISDIRTY | __ISFORCED);
    428 				}
    429 			}
    430 		}
    431 	}
    432 	return OK;
    433 }
    434 
    435 /*
    436  * wrefresh --
    437  *	Make the current screen look like "win" over the area covered by
    438  *	win.
    439  */
    440 int
    441 wrefresh(WINDOW *win)
    442 {
    443 	int retval;
    444 	int pbegx, pbegy;
    445 
    446 #ifdef DEBUG
    447 	__CTRACE(__CTRACE_REFRESH, "wrefresh: win %p\n", win);
    448 #endif
    449 
    450 	_cursesi_screen->curwin = (win == _cursesi_screen->curscr);
    451 	if (!_cursesi_screen->curwin) {
    452 		pbegx = pbegy = 0;
    453 		if ((win->flags & __ISDERWIN) == __ISDERWIN) {
    454 			pbegx = win->derx;
    455 			pbegy = win->dery;
    456 #ifdef DEBUG
    457 	__CTRACE(__CTRACE_REFRESH, "wrefresh: derwin, begy = %d, begx = %x\n",
    458 		pbegy, pbegx);
    459 #endif
    460 		}
    461 		retval = _wnoutrefresh(win, pbegy, pbegx, win->begy, win->begx,
    462 		    win->maxy, win->maxx);
    463 	} else
    464 		retval = OK;
    465 	if (retval == OK) {
    466 		retval = doupdate();
    467 		if (!(win->flags & __LEAVEOK)) {
    468 			win->cury = max(0, curscr->cury - win->begy);
    469 			win->curx = max(0, curscr->curx - win->begx);
    470 		}
    471 	}
    472 	_cursesi_screen->curwin = 0;
    473 	return retval;
    474 }
    475 
    476  /*
    477  * prefresh --
    478  *	Make the current screen look like "pad" over the area coverd by
    479  *	the specified area of pad.
    480  */
    481 int
    482 prefresh(WINDOW *pad, int pbegy, int pbegx, int sbegy, int sbegx,
    483 	int smaxy, int smaxx)
    484 {
    485 	int retval;
    486 
    487 #ifdef DEBUG
    488 	__CTRACE(__CTRACE_REFRESH, "prefresh: pad %p, flags 0x%08x\n",
    489 	    pad, pad->flags);
    490 #endif
    491 	/* Retain values in case pechochar() is called. */
    492 	pad->pbegy = pbegy;
    493 	pad->pbegx = pbegx;
    494 	pad->sbegy = sbegy;
    495 	pad->sbegx = sbegx;
    496 	pad->smaxy = smaxy;
    497 	pad->smaxx = smaxx;
    498 
    499 	/* Use pnoutrefresh() to avoid duplicating code here */
    500 	retval = pnoutrefresh(pad, pbegy, pbegx, sbegy, sbegx, smaxy, smaxx);
    501 	if (retval == OK) {
    502 		retval = doupdate();
    503 		if (!(pad->flags & __LEAVEOK)) {
    504 			pad->cury = max(0, pbegy + (curscr->cury - sbegy));
    505 			pad->curx = max(0, pbegx + (curscr->curx - sbegx));
    506 		}
    507 	}
    508 	return retval;
    509 }
    510 
    511 /*
    512  * doupdate --
    513  *	Make the current screen look like the virtual window "__virtscr".
    514  */
    515 int
    516 doupdate(void)
    517 {
    518 	WINDOW	*win;
    519 	__LINE	*wlp, *vlp;
    520 	short	 wy;
    521 	int	 dnum, was_cleared, changed;
    522 #ifdef HAVE_WCHAR
    523 	__LDATA *lp;
    524 	nschar_t *np;
    525 	int x;
    526 #endif /* HAVE_WCHAR */
    527 
    528 	/* Check if we need to restart ... */
    529 	if (_cursesi_screen->endwin)
    530 		__restartwin();
    531 
    532 	if (_cursesi_screen->curwin)
    533 		win = curscr;
    534 	else
    535 		win = _cursesi_screen->__virtscr;
    536 
    537 	/* Initialize loop parameters. */
    538 	_cursesi_screen->ly = curscr->cury;
    539 	_cursesi_screen->lx = curscr->curx;
    540 	wy = 0;
    541 
    542 	if (!_cursesi_screen->curwin) {
    543 		for (wy = 0; wy < win->maxy; wy++) {
    544 			wlp = win->alines[wy];
    545 			if (wlp->flags & __ISDIRTY) {
    546 #ifndef HAVE_WCHAR
    547 				wlp->hash = __hash(wlp->line,
    548 				    (size_t)(win->maxx * __LDATASIZE));
    549 #else
    550 				wlp->hash = 0;
    551 				for ( x = 0; x < win->maxx; x++ ) {
    552 					lp = &wlp->line[ x ];
    553 					wlp->hash = __hash_more( &lp->ch,
    554 						sizeof(wchar_t), wlp->hash );
    555 					wlp->hash = __hash_more( &lp->attr,
    556 						sizeof(attr_t), wlp->hash );
    557 					np = lp->nsp;
    558 					if (np) {
    559 						while (np) {
    560 							wlp->hash
    561 							    = __hash_more(
    562 								&np->ch,
    563 								sizeof(wchar_t),
    564 								wlp->hash);
    565 							np = np->next;
    566 						}
    567 					}
    568 				}
    569 #endif /* HAVE_WCHAR */
    570 			}
    571 		}
    572 	}
    573 
    574 	was_cleared = 0;
    575 	if ((win->flags & __CLEAROK) || (curscr->flags & __CLEAROK) ||
    576 	    _cursesi_screen->curwin)
    577 	{
    578 		if (curscr->wattr & __COLOR)
    579 			__unsetattr(0);
    580 		tputs(clear_screen, 0, __cputchar);
    581 		_cursesi_screen->ly = 0;
    582 		_cursesi_screen->lx = 0;
    583 		if (!_cursesi_screen->curwin) {
    584 			curscr->flags &= ~__CLEAROK;
    585 			curscr->cury = 0;
    586 			curscr->curx = 0;
    587 			werase(curscr);
    588 		}
    589 		__touchwin(win);
    590 		win->flags &= ~__CLEAROK;
    591 		/* note we cleared for later */
    592 		was_cleared = 1;
    593 	}
    594 	if (!cursor_address) {
    595 		if (win->curx != 0)
    596 			__cputchar('\n');
    597 		if (!_cursesi_screen->curwin)
    598 			werase(curscr);
    599 	}
    600 #ifdef DEBUG
    601 	__CTRACE(__CTRACE_REFRESH, "doupdate: (%p): curwin = %d\n", win,
    602 	    _cursesi_screen->curwin);
    603 	__CTRACE(__CTRACE_REFRESH, "doupdate: \tfirstch\tlastch\n");
    604 #endif
    605 
    606 	if (!_cursesi_screen->curwin) {
    607 		/*
    608 		 * Invoke quickch() only if more than a quarter of the lines
    609 		 * in the window are dirty.
    610 		 */
    611 		for (wy = 0, dnum = 0; wy < win->maxy; wy++)
    612 			if (win->alines[wy]->flags & __ISDIRTY)
    613 				dnum++;
    614 		if (!__noqch && dnum > (int) win->maxy / 4)
    615 			quickch();
    616 	}
    617 
    618 #ifdef DEBUG
    619 	{
    620 		int	i, j;
    621 
    622 		__CTRACE(__CTRACE_REFRESH,
    623 		    "#####################################\n");
    624 		__CTRACE(__CTRACE_REFRESH,
    625 		    "stdscr(%p)-curscr(%p)-__virtscr(%p)\n",
    626 		    stdscr, curscr, _cursesi_screen->__virtscr);
    627 		for (i = 0; i < curscr->maxy; i++) {
    628 			__CTRACE(__CTRACE_REFRESH, "C: %d:", i);
    629 			__CTRACE(__CTRACE_REFRESH, " 0x%x \n",
    630 			    curscr->alines[i]->hash);
    631 			for (j = 0; j < curscr->maxx; j++)
    632 				__CTRACE(__CTRACE_REFRESH, "%c",
    633 				    curscr->alines[i]->line[j].ch);
    634 			__CTRACE(__CTRACE_REFRESH, "\n");
    635 			__CTRACE(__CTRACE_REFRESH, " attr:");
    636 			for (j = 0; j < curscr->maxx; j++)
    637 				__CTRACE(__CTRACE_REFRESH, " %x",
    638 				    curscr->alines[i]->line[j].attr);
    639 			__CTRACE(__CTRACE_REFRESH, "\n");
    640 			__CTRACE(__CTRACE_REFRESH, "W: %d:", i);
    641 			__CTRACE(__CTRACE_REFRESH, " 0x%x \n",
    642 			    win->alines[i]->hash);
    643 			__CTRACE(__CTRACE_REFRESH, " 0x%x ",
    644 			    win->alines[i]->flags);
    645 			for (j = 0; j < win->maxx; j++)
    646 				__CTRACE(__CTRACE_REFRESH, "%c",
    647 				    win->alines[i]->line[j].ch);
    648 			__CTRACE(__CTRACE_REFRESH, "\n");
    649 			__CTRACE(__CTRACE_REFRESH, " attr:");
    650 			for (j = 0; j < win->maxx; j++)
    651 				__CTRACE(__CTRACE_REFRESH, " %x",
    652 				    win->alines[i]->line[j].attr);
    653 			__CTRACE(__CTRACE_REFRESH, "\n");
    654 #ifdef HAVE_WCHAR
    655 			__CTRACE(__CTRACE_REFRESH, " nsp:");
    656 			for (j = 0; j < curscr->maxx; j++)
    657 				__CTRACE(__CTRACE_REFRESH, " %p",
    658 				    win->alines[i]->line[j].nsp);
    659 			__CTRACE(__CTRACE_REFRESH, "\n");
    660 			__CTRACE(__CTRACE_REFRESH, " bnsp:");
    661 			for (j = 0; j < curscr->maxx; j++)
    662 				__CTRACE(__CTRACE_REFRESH, " %p",
    663 				    win->bnsp);
    664 			__CTRACE(__CTRACE_REFRESH, "\n");
    665 #endif /* HAVE_WCHAR */
    666 		}
    667 	}
    668 #endif /* DEBUG */
    669 
    670 	changed = 0;
    671 	for (wy = 0; wy < win->maxy; wy++) {
    672 		wlp = win->alines[wy];
    673 		vlp = _cursesi_screen->__virtscr->alines[win->begy + wy];
    674 /* XXX: remove this debug */
    675 #ifdef DEBUG
    676 		__CTRACE(__CTRACE_REFRESH,
    677 		    "doupdate: wy %d\tf: %d\tl:%d\tflags %x\n",
    678 		    wy, *wlp->firstchp, *wlp->lastchp, wlp->flags);
    679 #endif /* DEBUG */
    680 		if (!_cursesi_screen->curwin)
    681 			curscr->alines[wy]->hash = wlp->hash;
    682 		if (wlp->flags & __ISDIRTY || wlp->flags & __ISFORCED) {
    683 #ifdef DEBUG
    684 			__CTRACE(__CTRACE_REFRESH,
    685 			    "doupdate: [ISDIRTY]wy:%d\tf:%d\tl:%d\n", wy,
    686 			    *wlp->firstchp, *wlp->lastchp);
    687 #endif /* DEBUG */
    688 			/*
    689 			 * We have just cleared so don't force an update
    690 			 * otherwise we spray neeedless blanks to a cleared
    691 			 * screen.
    692 			 */
    693 			if (was_cleared == 1)
    694 				win->alines[wy]->flags &= ~__ISFORCED;
    695 
    696 			if (makech(wy) == ERR)
    697 				return ERR;
    698 			else {
    699 				if (*wlp->firstchp >= 0)
    700 					*wlp->firstchp = win->maxx;
    701 				if (*wlp->lastchp < win->maxx)
    702 					*wlp->lastchp = win->ch_off;
    703 				if (*wlp->lastchp < *wlp->firstchp) {
    704 #ifdef DEBUG
    705 					__CTRACE(__CTRACE_REFRESH,
    706 					    "doupdate: line %d notdirty\n", wy);
    707 #endif /* DEBUG */
    708 					wlp->flags &= ~(__ISDIRTY | __ISFORCED);
    709 				}
    710 
    711 				/* Check if we have input after
    712 				 * changing N lines. */
    713 				if (_cursesi_screen->checkfd != -1 &&
    714 				    ++changed == CHECK_INTERVAL)
    715 				{
    716 					struct pollfd fds[1];
    717 
    718 					/* If we have input, abort. */
    719 					fds[0].fd = _cursesi_screen->checkfd;
    720 					fds[0].events = POLLIN;
    721 					if (poll(fds, 1, 0) > 0)
    722 						goto cleanup;
    723 					changed = 0;
    724 				}
    725 			}
    726 		}
    727 
    728 		/*
    729 		 * virtscr is now synced for the line, unset the change
    730 		 * pointers.
    731 		 */
    732 		if (*vlp->firstchp >= 0)
    733 			*vlp->firstchp = _cursesi_screen->__virtscr->maxx;
    734 		if (*vlp->lastchp <= _cursesi_screen->__virtscr->maxx)
    735 			*vlp->lastchp = 0;
    736 
    737 #ifdef DEBUG
    738 		__CTRACE(__CTRACE_REFRESH, "\t%d\t%d\n",
    739 		    *wlp->firstchp, *wlp->lastchp);
    740 #endif /* DEBUG */
    741 	}
    742 
    743 #ifdef DEBUG
    744 	__CTRACE(__CTRACE_REFRESH, "doupdate: ly=%d, lx=%d\n",
    745 	    _cursesi_screen->ly, _cursesi_screen->lx);
    746 #endif /* DEBUG */
    747 
    748 	if (_cursesi_screen->curwin)
    749 		domvcur(win, _cursesi_screen->ly, _cursesi_screen->lx,
    750 			win->cury, win->curx);
    751 	else {
    752 		if (win->flags & __LEAVEOK) {
    753 			curscr->cury = _cursesi_screen->ly;
    754 			curscr->curx = _cursesi_screen->lx;
    755 		} else {
    756 			domvcur(win, _cursesi_screen->ly, _cursesi_screen->lx,
    757 				win->cury, win->curx);
    758 			curscr->cury = win->cury;
    759 			curscr->curx = win->curx;
    760 		}
    761 	}
    762 
    763 cleanup:
    764 	/* Don't leave the screen with attributes set. */
    765 	__unsetattr(0);
    766 #ifdef DEBUG
    767 #ifdef HAVE_WCHAR
    768 	{
    769 		int	i, j;
    770 
    771 		__CTRACE(__CTRACE_REFRESH,
    772 		    "***********after*****************\n");
    773 		__CTRACE(__CTRACE_REFRESH,
    774 		    "stdscr(%p)-curscr(%p)-__virtscr(%p)\n",
    775 		    stdscr, curscr, _cursesi_screen->__virtscr);
    776 		for (i = 0; i < curscr->maxy; i++) {
    777 			for (j = 0; j < curscr->maxx; j++)
    778 				__CTRACE(__CTRACE_REFRESH,
    779 				    "[%d,%d](%x,%x,%p)-(%x,%x,%p)\n",
    780 				    i, j,
    781 				    curscr->alines[i]->line[j].ch,
    782 				    curscr->alines[i]->line[j].attr,
    783 				    curscr->alines[i]->line[j].nsp,
    784 				    _cursesi_screen->__virtscr->alines[i]->line[j].ch,
    785 				    _cursesi_screen->__virtscr->alines[i]->line[j].attr,
    786 				    _cursesi_screen->__virtscr->alines[i]->line[j].nsp);
    787 		}
    788 	}
    789 #endif /* HAVE_WCHAR */
    790 #endif /* DEBUG */
    791 	return fflush(_cursesi_screen->outfd) == EOF ? ERR : OK;
    792 }
    793 
    794 static void
    795 putattr(__LDATA *nsp)
    796 {
    797 	attr_t	off, on;
    798 
    799 #ifdef DEBUG
    800 	__CTRACE(__CTRACE_REFRESH,
    801 	    "makech: have attr %08x, need attr %08x\n",
    802 	    curscr->wattr
    803 #ifndef HAVE_WCHAR
    804 	    & __ATTRIBUTES
    805 #else
    806 	    & WA_ATTRIBUTES
    807 #endif
    808 	    ,  nsp->attr
    809 #ifndef HAVE_WCHAR
    810 	    & __ATTRIBUTES
    811 #else
    812 	    & WA_ATTRIBUTES
    813 #endif
    814 	    );
    815 #endif
    816 
    817 	off = (~nsp->attr & curscr->wattr)
    818 #ifndef HAVE_WCHAR
    819 	    & __ATTRIBUTES
    820 #else
    821 	    & WA_ATTRIBUTES
    822 #endif
    823 	    ;
    824 
    825 	/*
    826 	 * Unset attributes as appropriate.  Unset first
    827 	 * so that the relevant attributes can be reset
    828 	 * (because 'me' unsets 'mb', 'md', 'mh', 'mk',
    829 	 * 'mp' and 'mr').  Check to see if we also turn off
    830 	 * standout, attributes and colour.
    831 	 */
    832 	if (off & __TERMATTR && exit_attribute_mode != NULL) {
    833 		tputs(exit_attribute_mode, 0, __cputchar);
    834 		curscr->wattr &= __mask_me;
    835 		off &= __mask_me;
    836 	}
    837 
    838 	/*
    839 	 * Exit underscore mode if appropriate.
    840 	 * Check to see if we also turn off standout,
    841 	 * attributes and colour.
    842 	 */
    843 	if (off & __UNDERSCORE && exit_underline_mode != NULL) {
    844 		tputs(exit_underline_mode, 0, __cputchar);
    845 		curscr->wattr &= __mask_ue;
    846 		off &= __mask_ue;
    847 	}
    848 
    849 	/*
    850 	 * Exit standout mode as appropriate.
    851 	 * Check to see if we also turn off underscore,
    852 	 * attributes and colour.
    853 	 * XXX
    854 	 * Should use uc if so/se not available.
    855 	 */
    856 	if (off & __STANDOUT && exit_standout_mode != NULL) {
    857 		tputs(exit_standout_mode, 0, __cputchar);
    858 		curscr->wattr &= __mask_se;
    859 		off &= __mask_se;
    860 	}
    861 
    862 	if (off & __ALTCHARSET && exit_alt_charset_mode != NULL) {
    863 		tputs(exit_alt_charset_mode, 0, __cputchar);
    864 		curscr->wattr &= ~__ALTCHARSET;
    865 	}
    866 
    867 	/* Set/change colour as appropriate. */
    868 	if (__using_color)
    869 		__set_color(curscr, nsp->attr & __COLOR);
    870 
    871 	on = (nsp->attr & ~curscr->wattr)
    872 #ifndef HAVE_WCHAR
    873 	    & __ATTRIBUTES
    874 #else
    875 	    & WA_ATTRIBUTES
    876 #endif
    877 	    ;
    878 
    879 	/*
    880 	 * Enter standout mode if appropriate.
    881 	 */
    882 	if (on & __STANDOUT &&
    883 	    enter_standout_mode != NULL &&
    884 	    exit_standout_mode != NULL)
    885 	{
    886 		tputs(enter_standout_mode, 0, __cputchar);
    887 		curscr->wattr |= __STANDOUT;
    888 	}
    889 
    890 	/*
    891 	 * Enter underscore mode if appropriate.
    892 	 * XXX
    893 	 * Should use uc if us/ue not available.
    894 	 */
    895 	if (on & __UNDERSCORE &&
    896 	    enter_underline_mode != NULL &&
    897 	    exit_underline_mode != NULL)
    898 	{
    899 		tputs(enter_underline_mode, 0, __cputchar);
    900 		curscr->wattr |= __UNDERSCORE;
    901 	}
    902 
    903 	/*
    904 	 * Set other attributes as appropriate.
    905 	 */
    906 	if (exit_attribute_mode != NULL) {
    907 		if (on & __BLINK && enter_blink_mode != NULL)
    908 		{
    909 			tputs(enter_blink_mode, 0, __cputchar);
    910 			curscr->wattr |= __BLINK;
    911 		}
    912 		if (on & __BOLD && enter_bold_mode != NULL)
    913 		{
    914 			tputs(enter_bold_mode, 0, __cputchar);
    915 			curscr->wattr |= __BOLD;
    916 		}
    917 		if (on & __DIM && enter_dim_mode != NULL)
    918 		{
    919 			tputs(enter_dim_mode, 0, __cputchar);
    920 			curscr->wattr |= __DIM;
    921 		}
    922 		if (on & __BLANK && enter_secure_mode != NULL)
    923 		{
    924 			tputs(enter_secure_mode, 0, __cputchar);
    925 			curscr->wattr |= __BLANK;
    926 		}
    927 		if (on & __PROTECT && enter_protected_mode != NULL)
    928 		{
    929 			tputs(enter_protected_mode, 0, __cputchar);
    930 			curscr->wattr |= __PROTECT;
    931 		}
    932 		if (on & __REVERSE && enter_reverse_mode != NULL)
    933 		{
    934 			tputs(enter_reverse_mode, 0, __cputchar);
    935 			curscr->wattr |= __REVERSE;
    936 		}
    937 #ifdef HAVE_WCHAR
    938 		if (on & WA_TOP && enter_top_hl_mode != NULL)
    939 		{
    940 			tputs(enter_top_hl_mode, 0, __cputchar);
    941 			curscr->wattr |= WA_TOP;
    942 		}
    943 		if (on & WA_LOW && enter_low_hl_mode != NULL)
    944 		{
    945 			tputs(enter_low_hl_mode, 0, __cputchar);
    946 			curscr->wattr |= WA_LOW;
    947 		}
    948 		if (on & WA_LEFT && enter_left_hl_mode != NULL)
    949 		{
    950 			tputs(enter_left_hl_mode, 0, __cputchar);
    951 			curscr->wattr |= WA_LEFT;
    952 		}
    953 		if (on & WA_RIGHT && enter_right_hl_mode != NULL)
    954 		{
    955 			tputs(enter_right_hl_mode, 0, __cputchar);
    956 			curscr->wattr |= WA_RIGHT;
    957 		}
    958 		if (on & WA_HORIZONTAL && enter_horizontal_hl_mode != NULL)
    959 		{
    960 			tputs(enter_horizontal_hl_mode, 0, __cputchar);
    961 			curscr->wattr |= WA_HORIZONTAL;
    962 		}
    963 		if (on & WA_VERTICAL && enter_vertical_hl_mode != NULL)
    964 		{
    965 			tputs(enter_vertical_hl_mode, 0, __cputchar);
    966 			curscr->wattr |= WA_VERTICAL;
    967 		}
    968 #endif /* HAVE_WCHAR */
    969 	}
    970 
    971 	/* Enter/exit altcharset mode as appropriate. */
    972 	if (on & __ALTCHARSET && enter_alt_charset_mode != NULL &&
    973 	    exit_alt_charset_mode != NULL) {
    974 		tputs(enter_alt_charset_mode, 0, __cputchar);
    975 		curscr->wattr |= __ALTCHARSET;
    976 	}
    977 }
    978 
    979 static void
    980 putattr_out(__LDATA *nsp)
    981 {
    982 
    983 	if (underline_char &&
    984 	    ((nsp->attr & __STANDOUT) || (nsp->attr & __UNDERSCORE)))
    985 	{
    986 		__cputchar('\b');
    987 		tputs(underline_char, 0, __cputchar);
    988 	}
    989 }
    990 
    991 static int
    992 putch(__LDATA *nsp, __LDATA *csp, int wy, int wx)
    993 {
    994 
    995 	if (csp != NULL)
    996 		putattr(nsp);
    997 
    998 	if (!_cursesi_screen->curwin && csp) {
    999 		csp->attr = nsp->attr;
   1000 		csp->ch = nsp->ch;
   1001 #ifdef HAVE_WCHAR
   1002 		if (_cursesi_copy_nsp(nsp->nsp, csp) == ERR)
   1003 			return ERR;
   1004 #endif /* HAVE_WCHAR */
   1005 	}
   1006 
   1007 #ifndef HAVE_WCHAR
   1008 	__cputchar((int)nsp->ch);
   1009 #else
   1010 	if (WCOL(*nsp) <= 0)
   1011 		goto out;
   1012 	__cputwchar((int)nsp->ch);
   1013 #ifdef DEBUG
   1014 	__CTRACE(__CTRACE_REFRESH,
   1015 	    "makech: (%d,%d)putwchar(0x%x)\n", wy, wx - 1, nsp->ch);
   1016 #endif /* DEBUG */
   1017 
   1018 	/* Output non-spacing characters for the cell. */
   1019 	__cursesi_putnsp(nsp->nsp, wy, wx);
   1020 out:
   1021 #endif /* HAVE_WCHAR */
   1022 
   1023 	if (csp != NULL)
   1024 		putattr_out(nsp);
   1025 	return OK;
   1026 }
   1027 
   1028 static int
   1029 putchbr(__LDATA *nsp, __LDATA *csp, __LDATA *psp, int wy, int wx)
   1030 {
   1031 	int error, cw, pcw;
   1032 
   1033 	/* Can safely print to bottom right corner. */
   1034 	if (!auto_right_margin)
   1035 		return putch(nsp, csp, wy, wx);
   1036 
   1037 	/* Disable auto margins temporarily. */
   1038 	if (enter_am_mode && exit_am_mode) {
   1039 		tputs(exit_am_mode, 0, __cputchar);
   1040 		error = putch(nsp, csp, wy, wx);
   1041 		tputs(enter_am_mode, 0, __cputchar);
   1042 		return error;
   1043 	}
   1044 
   1045 	/* We need to insert characters. */
   1046 #ifdef HAVE_WCHAR
   1047 	cw = WCOL(*nsp);
   1048 	pcw = WCOL(*psp);
   1049 	if (cw < 1 || pcw < 1)
   1050 		return ERR; /* Nothing to insert */
   1051 
   1052 	/* When inserting a wide character, we need something other than
   1053 	 * insert_character. */
   1054 	if (pcw > 1 &&
   1055 	    !(parm_ich != NULL ||
   1056 	    (enter_insert_mode != NULL && exit_insert_mode != NULL)))
   1057 		return ERR;
   1058 #else
   1059 	cw = pcw = 1;
   1060 #endif /* HAVE_WCHAR */
   1061 
   1062 	/* Write the corner character at wx - pcw. */
   1063 	__mvcur(wy, wx, wy, wx - pcw, 1);
   1064 	if (putch(nsp, csp, wy, wx) == ERR)
   1065 		return ERR;
   1066 
   1067 	/* Move cursor back. */
   1068 	__mvcur(wy, wx - pcw + cw, wy, wx - cw, 1);
   1069 
   1070 	putattr(psp);
   1071 
   1072 	/* Enter insert mode. */
   1073 	if (pcw == 1 && insert_character != NULL)
   1074 		tputs(insert_character, 0, __cputchar);
   1075 	else if (parm_ich != NULL)
   1076 		tputs(tiparm(parm_ich, (long)pcw), 0, __cputchar);
   1077 	else if (enter_insert_mode != NULL && exit_insert_mode != NULL)
   1078 		tputs(enter_insert_mode, 0, __cputchar);
   1079 	else
   1080 		return ERR;
   1081 
   1082 	/* Insert the old character back. */
   1083 	error = putch(psp, NULL, wy, wx - pcw);
   1084 
   1085 	/* Exit insert mode. */
   1086 	if (insert_character != NULL || parm_ich != NULL)
   1087 		;
   1088 	else if (enter_insert_mode != NULL && exit_insert_mode != NULL)
   1089 		tputs(exit_insert_mode, 0, __cputchar);
   1090 
   1091 	putattr_out(psp);
   1092 
   1093 	return error;
   1094 }
   1095 
   1096 /*
   1097  * makech --
   1098  *	Make a change on the screen.
   1099  */
   1100 static int
   1101 makech(int wy)
   1102 {
   1103 	WINDOW	*win;
   1104 	static __LDATA blank;
   1105 	__LDATA *nsp, *csp, *cp, *cep, *fsp;
   1106 	__LINE *wlp;
   1107 	size_t	clsp, nlsp;	/* Last space in lines. */
   1108 	int	lch, wx, chw;
   1109 	const char	*ce;
   1110 	attr_t	lspc;		/* Last space colour */
   1111 
   1112 #ifdef __GNUC__
   1113 	nlsp = lspc = 0;	/* XXX gcc -Wuninitialized */
   1114 #endif
   1115 	if (_cursesi_screen->curwin)
   1116 		win = curscr;
   1117 	else
   1118 		win = __virtscr;
   1119 #ifdef HAVE_WCHAR
   1120 	blank.ch = (wchar_t)btowc((int) win->bch);
   1121 	blank.attr = 0;
   1122 	if (_cursesi_copy_nsp(win->bnsp, &blank) == ERR)
   1123 		return ERR;
   1124 	SET_WCOL(blank, 1);
   1125 #endif /* HAVE_WCHAR */
   1126 #ifdef DEBUG
   1127 #if HAVE_WCHAR
   1128 	{
   1129 		int x;
   1130 		__LDATA *lp, *vlp;
   1131 
   1132 		__CTRACE(__CTRACE_REFRESH,
   1133 		    "[makech-before]wy=%d,curscr(%p)-__virtscr(%p)\n",
   1134 		    wy, curscr, __virtscr);
   1135 		for (x = 0; x < curscr->maxx; x++) {
   1136 			lp = &curscr->alines[wy]->line[x];
   1137 			vlp = &__virtscr->alines[wy]->line[x];
   1138 			__CTRACE(__CTRACE_REFRESH,
   1139 			    "[%d,%d](%x,%x,%x,%x,%p)-"
   1140 			    "(%x,%x,%x,%x,%p)\n",
   1141 			    wy, x, lp->ch, lp->attr,
   1142 			    win->bch, win->battr, lp->nsp,
   1143 			    vlp->ch, vlp->attr,
   1144 			    win->bch, win->battr, vlp->nsp);
   1145 		}
   1146 	}
   1147 #endif /* HAVE_WCHAR */
   1148 #endif /* DEBUG */
   1149 	/* Is the cursor still on the end of the last line? */
   1150 	if (wy > 0 && curscr->alines[wy - 1]->flags & __ISPASTEOL) {
   1151 		domvcur(win, _cursesi_screen->ly, _cursesi_screen->lx,
   1152 			_cursesi_screen->ly + 1, 0);
   1153 		_cursesi_screen->ly++;
   1154 		_cursesi_screen->lx = 0;
   1155 	}
   1156 	wlp = win->alines[wy];
   1157 	wx = *win->alines[wy]->firstchp;
   1158 	if (wx < 0)
   1159 		wx = 0;
   1160 	else
   1161 		if (wx >= win->maxx)
   1162 			return (OK);
   1163 	lch = *win->alines[wy]->lastchp;
   1164 	if (lch < 0)
   1165 		return OK;
   1166 	else
   1167 		if (lch >= (int) win->maxx)
   1168 			lch = win->maxx - 1;
   1169 
   1170 	if (_cursesi_screen->curwin) {
   1171 		csp = &blank;
   1172 #ifdef DEBUG
   1173 		__CTRACE(__CTRACE_REFRESH, "makech: csp is blank\n");
   1174 #endif /* DEBUG */
   1175 	} else {
   1176 		csp = &curscr->alines[wy]->line[wx];
   1177 #ifdef DEBUG
   1178 		__CTRACE(__CTRACE_REFRESH,
   1179 		    "makech: csp is on curscr:(%d,%d)\n", wy, wx);
   1180 #endif /* DEBUG */
   1181 	}
   1182 
   1183 	nsp = fsp = &win->alines[wy]->line[wx];
   1184 #ifdef DEBUG
   1185 	if (_cursesi_screen->curwin)
   1186 		__CTRACE(__CTRACE_REFRESH,
   1187 		    "makech: nsp is at curscr:(%d,%d)\n", wy, wx);
   1188 	else
   1189 		__CTRACE(__CTRACE_REFRESH,
   1190 		    "makech: nsp is at __virtscr:(%d,%d)\n", wy, wx);
   1191 #endif /* DEBUG */
   1192 	if (clr_eol && !_cursesi_screen->curwin) {
   1193 		cp = &win->alines[wy]->line[win->maxx - 1];
   1194 		lspc = cp->attr & __COLOR;
   1195 #ifndef HAVE_WCHAR
   1196 		while (cp->ch == ' ' && cp->attr == lspc) /* XXX */
   1197 			if (cp-- <= win->alines[wy]->line)
   1198 				break;
   1199 #else
   1200 		while (cp->ch == ( wchar_t )btowc(( int )' ' )
   1201 				&& ( cp->attr & WA_ATTRIBUTES ) == lspc)
   1202 			if (cp-- <= win->alines[wy]->line)
   1203 				break;
   1204 #endif /* HAVE_WCHAR */
   1205 		if (win->alines[wy]->line > cp)
   1206 			nlsp = 0;
   1207 		else
   1208 			nlsp = cp - win->alines[wy]->line;
   1209 	}
   1210 	if (!_cursesi_screen->curwin)
   1211 		ce = clr_eol;
   1212 	else
   1213 		ce = NULL;
   1214 
   1215 	while (wx <= lch) {
   1216 #ifdef DEBUG
   1217 #ifndef HAVE_WCHAR
   1218 		__CTRACE(__CTRACE_REFRESH, "makech: wx=%d,lch=%d\n", wx, lch);
   1219 #else
   1220 		__CTRACE(__CTRACE_REFRESH, "makech: nsp=(%x,%x,%x,%x,%p)\n",
   1221 			nsp->ch, nsp->attr, win->bch, win->battr, nsp->nsp);
   1222 		__CTRACE(__CTRACE_REFRESH, "makech: csp=(%x,%x,%x,%x,%p)\n",
   1223 			csp->ch, csp->attr, win->bch, win->battr, csp->nsp);
   1224 #endif
   1225 #endif /* DEBUG */
   1226 		if (!(wlp->flags & __ISFORCED) &&
   1227 #ifdef HAVE_WCHAR
   1228 		    ((nsp->attr & __WCWIDTH) != __WCWIDTH) &&
   1229 #endif
   1230 		    celleq(nsp, csp))
   1231 		{
   1232 			if (wx <= lch) {
   1233 				while (wx <= lch && celleq(nsp, csp)) {
   1234 					nsp++;
   1235 					if (!_cursesi_screen->curwin)
   1236 						++csp;
   1237 					++wx;
   1238 				}
   1239 				continue;
   1240 			}
   1241 			break;
   1242 		}
   1243 
   1244 		domvcur(win, _cursesi_screen->ly, _cursesi_screen->lx, wy, wx);
   1245 
   1246 #ifdef DEBUG
   1247 		__CTRACE(__CTRACE_REFRESH, "makech: 1: wx = %d, ly= %d, "
   1248 		    "lx = %d, newy = %d, newx = %d\n",
   1249 		    wx, _cursesi_screen->ly, _cursesi_screen->lx, wy, wx);
   1250 #endif
   1251 		_cursesi_screen->ly = wy;
   1252 		_cursesi_screen->lx = wx;
   1253 		while (wx <= lch &&
   1254 		       ((wlp->flags & __ISFORCED) || !celleq(nsp, csp)))
   1255 		{
   1256 #ifndef HAVE_WCHAR
   1257 			if (ce != NULL && wx >= nlsp
   1258 			    && nsp->ch == ' ' && nsp->attr == lspc)
   1259 #else
   1260 			if (ce != NULL && wx >= nlsp
   1261 			    && nsp->ch == (wchar_t)btowc((int)' ') /* XXX */
   1262 			    && (nsp->attr & WA_ATTRIBUTES) == lspc)
   1263 #endif
   1264 			{
   1265 				/* Check for clear to end-of-line. */
   1266 				cep = &curscr->alines[wy]->line[win->maxx - 1];
   1267 #ifndef HAVE_WCHAR
   1268 				while (cep->ch == ' ' && cep->attr == lspc) /* XXX */
   1269 #else
   1270 				while (cep->ch == (wchar_t)btowc((int)' ')
   1271 				       && (cep->attr & WA_ATTRIBUTES) == lspc)
   1272 #endif /* HAVE_WCHAR */
   1273 					if (cep-- <= csp)
   1274 						break;
   1275 				if (cep > (curscr->alines[wy]->line + win->begx * __LDATASIZE))
   1276 					clsp = cep - curscr->alines[wy]->line -
   1277 					win->begx * __LDATASIZE;
   1278 				else
   1279 					clsp = 0;
   1280 #ifdef DEBUG
   1281 				__CTRACE(__CTRACE_REFRESH,
   1282 				    "makech: clsp = %zu, nlsp = %zu\n",
   1283 				    clsp, nlsp);
   1284 				__CTRACE(__CTRACE_REFRESH,
   1285 				    "makech: line = %p, cep = %p, begx = %u\n",
   1286 				    curscr->alines[wy]->line, cep, win->begx);
   1287 #endif
   1288 				if (((clsp - nlsp >= strlen(ce) &&
   1289 				    clsp < win->maxx * __LDATASIZE) ||
   1290 				    wy == win->maxy - 1) &&
   1291 				    (!(lspc & __COLOR) ||
   1292 				    ((lspc & __COLOR) && back_color_erase)))
   1293 				{
   1294 					__unsetattr(0);
   1295 					if (__using_color &&
   1296 					    ((lspc & __COLOR) !=
   1297 					    (curscr->wattr & __COLOR)))
   1298 						__set_color(curscr, lspc &
   1299 						    __COLOR);
   1300 					tputs(ce, 0, __cputchar);
   1301 					_cursesi_screen->lx = wx + win->begx;
   1302 					while (wx++ <= clsp) {
   1303 						csp->attr = lspc;
   1304 #ifndef HAVE_WCHAR
   1305 						csp->ch = ' '; /* XXX */
   1306 #else
   1307 						csp->ch = (wchar_t)btowc((int)' ');
   1308 						SET_WCOL( *csp, 1 );
   1309 #endif /* HAVE_WCHAR */
   1310 						csp++;
   1311 					}
   1312 					return OK;
   1313 				}
   1314 				ce = NULL;
   1315 			}
   1316 
   1317 #ifdef HAVE_WCHAR
   1318 			chw = WCOL(*nsp);
   1319 			if (chw < 0)
   1320 				chw = 1;
   1321 #else
   1322 			chw = 1;
   1323 #endif /* HAVE_WCHAR */
   1324 			if (wx + chw >= win->maxx &&
   1325 			    wy == win->maxy - 1 && !_cursesi_screen->curwin)
   1326 			{
   1327 				if (win->flags & __ENDLINE)
   1328 					__unsetattr(1);
   1329 				if (!(win->flags & __SCROLLWIN)) {
   1330 					int e;
   1331 
   1332 					if (win->flags & __SCROLLOK)
   1333 						e = putch(nsp, csp, wy, wx);
   1334 					else
   1335 						e = putchbr(nsp, csp,
   1336 						    nsp == fsp ? NULL : nsp - 1,
   1337 						    wy, wx);
   1338 					if (e == ERR)
   1339 						return ERR;
   1340 				}
   1341 				if (wx + chw < curscr->maxx) {
   1342 					domvcur(win,
   1343 					    _cursesi_screen->ly, wx,
   1344 					    (int)(win->maxy - 1),
   1345 					    (int)(win->maxx - 1));
   1346 				}
   1347 				_cursesi_screen->ly = win->maxy - 1;
   1348 				_cursesi_screen->lx = win->maxx - 1;
   1349 				return OK;
   1350 			}
   1351 			if (wx + chw < win->maxx || wy < win->maxy - 1 ||
   1352 			    !(win->flags & __SCROLLWIN))
   1353 			{
   1354 				if (putch(nsp, csp, wy, wx) == ERR)
   1355 					return ERR;
   1356 				csp++;
   1357 			} else {
   1358 				putattr(nsp);
   1359 				putattr_out(nsp);
   1360 			}
   1361 			wx += chw;
   1362 			nsp++;
   1363 #ifdef DEBUG
   1364 			__CTRACE(__CTRACE_REFRESH,
   1365 			    "makech: 2: wx = %d, lx = %d\n",
   1366 			    wx, _cursesi_screen->lx);
   1367 #endif
   1368 		}
   1369 		if (_cursesi_screen->lx == wx)	/* If no change. */
   1370 			break;
   1371 		_cursesi_screen->lx = wx;
   1372 		if (_cursesi_screen->lx >= COLS && auto_right_margin)
   1373 			_cursesi_screen->lx = COLS - 1;
   1374 		else
   1375 			if (wx >= win->maxx) {
   1376 				domvcur(win,
   1377 					_cursesi_screen->ly,
   1378 					_cursesi_screen->lx,
   1379 					_cursesi_screen->ly,
   1380 					(int)(win->maxx - 1));
   1381 				_cursesi_screen->lx = win->maxx - 1;
   1382 			}
   1383 #ifdef DEBUG
   1384 		__CTRACE(__CTRACE_REFRESH, "makech: 3: wx = %d, lx = %d\n",
   1385 		    wx, _cursesi_screen->lx);
   1386 #endif
   1387 	}
   1388 #ifdef DEBUG
   1389 #if HAVE_WCHAR
   1390 	{
   1391 		int x;
   1392 		__LDATA *lp, *vlp;
   1393 
   1394 		__CTRACE(__CTRACE_REFRESH,
   1395 		    "makech-after: curscr(%p)-__virtscr(%p)\n",
   1396 		    curscr, __virtscr );
   1397 		for (x = 0; x < curscr->maxx; x++) {
   1398 			lp = &curscr->alines[wy]->line[x];
   1399 			vlp = &__virtscr->alines[wy]->line[x];
   1400 			__CTRACE(__CTRACE_REFRESH,
   1401 			    "[%d,%d](%x,%x,%x,%x,%p)-"
   1402 			    "(%x,%x,%x,%x,%p)\n",
   1403 			    wy, x, lp->ch, lp->attr,
   1404 			    win->bch, win->battr, lp->nsp,
   1405 			    vlp->ch, vlp->attr,
   1406 			    win->bch, win->battr, vlp->nsp);
   1407 		}
   1408 	}
   1409 #endif /* HAVE_WCHAR */
   1410 #endif /* DEBUG */
   1411 
   1412 	return OK;
   1413 }
   1414 
   1415 /*
   1416  * domvcur --
   1417  *	Do a mvcur, leaving attributes if necessary.
   1418  */
   1419 static void
   1420 domvcur(const WINDOW *win, int oy, int ox, int ny, int nx)
   1421 {
   1422 
   1423 #ifdef DEBUG
   1424 	__CTRACE(__CTRACE_REFRESH, "domvcur: (%x,%d)=>(%d,%d)\n",
   1425 	    oy, ox, ny, nx );
   1426 #endif /* DEBUG */
   1427 
   1428 	__unsetattr(1);
   1429 
   1430 	/* Don't move the cursor unless we need to. */
   1431 	if (oy == ny && ox == nx) {
   1432 		/* Check EOL. */
   1433 		if (!(win->alines[oy]->flags & __ISPASTEOL))
   1434 			return;
   1435 	}
   1436 
   1437 	/* Clear EOL flags. */
   1438 	win->alines[oy]->flags &= ~__ISPASTEOL;
   1439 	win->alines[ny]->flags &= ~__ISPASTEOL;
   1440 
   1441 	__mvcur(oy, ox, ny, nx, 1);
   1442 }
   1443 
   1444 /*
   1445  * Quickch() attempts to detect a pattern in the change of the window
   1446  * in order to optimize the change, e.g., scroll n lines as opposed to
   1447  * repainting the screen line by line.
   1448  */
   1449 
   1450 static __LDATA buf[128];
   1451 static  unsigned int last_hash;
   1452 static  size_t last_hash_len;
   1453 #define BLANKSIZE (sizeof(buf) / sizeof(buf[0]))
   1454 
   1455 static void
   1456 quickch(void)
   1457 {
   1458 #define THRESH		(int) __virtscr->maxy / 4
   1459 
   1460 	__LINE *clp, *tmp1, *tmp2;
   1461 	int	bsize, curs, curw, starts, startw, i, j;
   1462 	int	n, target, cur_period, bot, top, sc_region;
   1463 	unsigned int	blank_hash;
   1464 	attr_t	bcolor;
   1465 
   1466 #ifdef __GNUC__
   1467 	curs = curw = starts = startw = 0;	/* XXX gcc -Wuninitialized */
   1468 #endif
   1469 	/*
   1470 	 * Find how many lines from the top of the screen are unchanged.
   1471 	 */
   1472 	for (top = 0; top < __virtscr->maxy; top++) {
   1473 		if (__virtscr->alines[top]->flags & __ISDIRTY &&
   1474 		    (__virtscr->alines[top]->hash != curscr->alines[top]->hash ||
   1475 		     !lineeq(__virtscr->alines[top]->line,
   1476 			     curscr->alines[top]->line,
   1477 			     (size_t) __virtscr->maxx)))
   1478 			break;
   1479 		else
   1480 			__virtscr->alines[top]->flags &= ~__ISDIRTY;
   1481 	}
   1482 	/*
   1483 	 * Find how many lines from bottom of screen are unchanged.
   1484 	 */
   1485 	for (bot = __virtscr->maxy - 1; bot >= 0; bot--) {
   1486 		if (__virtscr->alines[bot]->flags & __ISDIRTY &&
   1487 		    (__virtscr->alines[bot]->hash != curscr->alines[bot]->hash ||
   1488 		     !lineeq(__virtscr->alines[bot]->line,
   1489 			     curscr->alines[bot]->line,
   1490 			     (size_t) __virtscr->maxx)))
   1491 			break;
   1492 		else
   1493 			__virtscr->alines[bot]->flags &= ~__ISDIRTY;
   1494 	}
   1495 
   1496 	/*
   1497 	 * Work round an xterm bug where inserting lines causes all the
   1498 	 * inserted lines to be covered with the background colour we
   1499 	 * set on the first line (even if we unset it for subsequent
   1500 	 * lines).
   1501 	 */
   1502 	bcolor = __virtscr->alines[min(top,
   1503 	    __virtscr->maxy - 1)]->line[0].attr & __COLOR;
   1504 	for (i = top + 1, j = 0; i < bot; i++) {
   1505 		if ((__virtscr->alines[i]->line[0].attr & __COLOR) != bcolor) {
   1506 			bcolor = __virtscr->alines[i]->line[__virtscr->maxx].
   1507 			    attr & __COLOR;
   1508 			j = i - top;
   1509 		} else
   1510 			break;
   1511 	}
   1512 	top += j;
   1513 
   1514 #ifdef NO_JERKINESS
   1515 	/*
   1516 	 * If we have a bottom unchanged region return.  Scrolling the
   1517 	 * bottom region up and then back down causes a screen jitter.
   1518 	 * This will increase the number of characters sent to the screen
   1519 	 * but it looks better.
   1520 	 */
   1521 	if (bot < __virtscr->maxy - 1)
   1522 		return;
   1523 #endif				/* NO_JERKINESS */
   1524 
   1525 	/*
   1526 	 * Search for the largest block of text not changed.
   1527 	 * Invariants of the loop:
   1528 	 * - Startw is the index of the beginning of the examined block in
   1529 	 *   __virtscr.
   1530 	 * - Starts is the index of the beginning of the examined block in
   1531 	 *   curscr.
   1532 	 * - Curw is the index of one past the end of the exmined block in
   1533 	 *   __virtscr.
   1534 	 * - Curs is the index of one past the end of the exmined block in
   1535 	 *   curscr.
   1536 	 * - bsize is the current size of the examined block.
   1537 	*/
   1538 
   1539 	for (bsize = bot - top; bsize >= THRESH; bsize--) {
   1540 		for (startw = top; startw <= bot - bsize; startw++)
   1541 			for (starts = top; starts <= bot - bsize; starts++) {
   1542 				for (curw = startw, curs = starts;
   1543 				    curs < starts + bsize; curw++, curs++)
   1544 					if (__virtscr->alines[curw]->hash !=
   1545 					    curscr->alines[curs]->hash)
   1546 						break;
   1547 				if (curs != starts + bsize)
   1548 					continue;
   1549 				for (curw = startw, curs = starts;
   1550 					curs < starts + bsize; curw++, curs++)
   1551 					if (!lineeq(__virtscr->alines[curw]->line,
   1552 						    curscr->alines[curs]->line,
   1553 						    (size_t) __virtscr->maxx))
   1554 						break;
   1555 				if (curs == starts + bsize)
   1556 					goto done;
   1557 			}
   1558 	}
   1559 done:
   1560 
   1561 	/* Did not find anything */
   1562 	if (bsize < THRESH)
   1563 		return;
   1564 
   1565 #ifdef DEBUG
   1566 	__CTRACE(__CTRACE_REFRESH, "quickch:bsize=%d, starts=%d, startw=%d, "
   1567 	    "curw=%d, curs=%d, top=%d, bot=%d\n",
   1568 	    bsize, starts, startw, curw, curs, top, bot);
   1569 #endif
   1570 
   1571 	/*
   1572 	 * Make sure that there is no overlap between the bottom and top
   1573 	 * regions and the middle scrolled block.
   1574 	 */
   1575 	if (bot < curs)
   1576 		bot = curs - 1;
   1577 	if (top > starts)
   1578 		top = starts;
   1579 
   1580 	n = startw - starts;
   1581 
   1582 #ifdef DEBUG
   1583 	__CTRACE(__CTRACE_REFRESH, "#####################################\n");
   1584 	for (i = 0; i < curscr->maxy; i++) {
   1585 		__CTRACE(__CTRACE_REFRESH, "C: %d:", i);
   1586 		__CTRACE(__CTRACE_REFRESH, " 0x%x \n", curscr->alines[i]->hash);
   1587 		for (j = 0; j < curscr->maxx; j++)
   1588 			__CTRACE(__CTRACE_REFRESH, "%c",
   1589 			    curscr->alines[i]->line[j].ch);
   1590 		__CTRACE(__CTRACE_REFRESH, "\n");
   1591 		__CTRACE(__CTRACE_REFRESH, " attr:");
   1592 		for (j = 0; j < curscr->maxx; j++)
   1593 			__CTRACE(__CTRACE_REFRESH, " %x",
   1594 			    curscr->alines[i]->line[j].attr);
   1595 		__CTRACE(__CTRACE_REFRESH, "\n");
   1596 		__CTRACE(__CTRACE_REFRESH, "W: %d:", i);
   1597 		__CTRACE(__CTRACE_REFRESH, " 0x%x \n",
   1598 		    __virtscr->alines[i]->hash);
   1599 		__CTRACE(__CTRACE_REFRESH, " 0x%x ",
   1600 		    __virtscr->alines[i]->flags);
   1601 		for (j = 0; j < __virtscr->maxx; j++)
   1602 			__CTRACE(__CTRACE_REFRESH, "%c",
   1603 			    __virtscr->alines[i]->line[j].ch);
   1604 		__CTRACE(__CTRACE_REFRESH, "\n");
   1605 		__CTRACE(__CTRACE_REFRESH, " attr:");
   1606 		for (j = 0; j < __virtscr->maxx; j++)
   1607 			__CTRACE(__CTRACE_REFRESH, " %x",
   1608 			    __virtscr->alines[i]->line[j].attr);
   1609 		__CTRACE(__CTRACE_REFRESH, "\n");
   1610 	}
   1611 #endif
   1612 
   1613 #ifndef HAVE_WCHAR
   1614 	if (buf[0].ch != ' ') {
   1615 		for (i = 0; i < BLANKSIZE; i++) {
   1616 			buf[i].ch = ' ';
   1617 			buf[i].attr = 0;
   1618 		}
   1619 	}
   1620 #else
   1621 	if (buf[0].ch != (wchar_t)btowc((int)curscr->bch )) {
   1622 		for (i = 0; i < BLANKSIZE; i++) {
   1623 			buf[i].ch = (wchar_t)btowc((int)curscr->bch);
   1624 			if (_cursesi_copy_nsp(curscr->bnsp, &buf[i]) == ERR)
   1625 				return;
   1626 			buf[i].attr = 0;
   1627 			SET_WCOL(buf[i], 1);
   1628 		}
   1629 	}
   1630 #endif /* HAVE_WCHAR */
   1631 
   1632 	if (__virtscr->maxx != last_hash_len) {
   1633 		blank_hash = 0;
   1634 		for (i = __virtscr->maxx; i > BLANKSIZE; i -= BLANKSIZE) {
   1635 			blank_hash = __hash_more(buf, sizeof(buf), blank_hash);
   1636 		}
   1637 		blank_hash = __hash_more((char *)(void *)buf,
   1638 		    i * sizeof(buf[0]), blank_hash);
   1639 		/* cache result in static data - screen width doesn't change often */
   1640 		last_hash_len = __virtscr->maxx;
   1641 		last_hash = blank_hash;
   1642 	} else
   1643 		blank_hash = last_hash;
   1644 
   1645 	/*
   1646 	 * Perform the rotation to maintain the consistency of curscr.
   1647 	 * This is hairy since we are doing an *in place* rotation.
   1648 	 * Invariants of the loop:
   1649 	 * - I is the index of the current line.
   1650 	 * - Target is the index of the target of line i.
   1651 	 * - Tmp1 points to current line (i).
   1652 	 * - Tmp2 and points to target line (target);
   1653 	 * - Cur_period is the index of the end of the current period.
   1654 	 *   (see below).
   1655 	 *
   1656 	 * There are 2 major issues here that make this rotation non-trivial:
   1657 	 * 1.  Scrolling in a scrolling region bounded by the top
   1658 	 *     and bottom regions determined (whose size is sc_region).
   1659 	 * 2.  As a result of the use of the mod function, there may be a
   1660 	 *     period introduced, i.e., 2 maps to 4, 4 to 6, n-2 to 0, and
   1661 	 *     0 to 2, which then causes all odd lines not to be rotated.
   1662 	 *     To remedy this, an index of the end ( = beginning) of the
   1663 	 *     current 'period' is kept, cur_period, and when it is reached,
   1664 	 *     the next period is started from cur_period + 1 which is
   1665 	 *     guaranteed not to have been reached since that would mean that
   1666 	 *     all records would have been reached. (think about it...).
   1667 	 *
   1668 	 * Lines in the rotation can have 3 attributes which are marked on the
   1669 	 * line so that curscr is consistent with the visual screen.
   1670 	 * 1.  Not dirty -- lines inside the scrolled block, top region or
   1671 	 *                  bottom region.
   1672 	 * 2.  Blank lines -- lines in the differential of the scrolling
   1673 	 *		      region adjacent to top and bot regions
   1674 	 *                    depending on scrolling direction.
   1675 	 * 3.  Dirty line -- all other lines are marked dirty.
   1676 	 */
   1677 	sc_region = bot - top + 1;
   1678 	i = top;
   1679 	tmp1 = curscr->alines[top];
   1680 	cur_period = top;
   1681 	for (j = top; j <= bot; j++) {
   1682 		target = (i - top + n + sc_region) % sc_region + top;
   1683 		tmp2 = curscr->alines[target];
   1684 		curscr->alines[target] = tmp1;
   1685 		/* Mark block as clean and blank out scrolled lines. */
   1686 		clp = curscr->alines[target];
   1687 #ifdef DEBUG
   1688 		__CTRACE(__CTRACE_REFRESH,
   1689 		    "quickch: n=%d startw=%d curw=%d i = %d target=%d ",
   1690 		    n, startw, curw, i, target);
   1691 #endif
   1692 		if ((target >= startw && target < curw) || target < top
   1693 		    || target > bot)
   1694 		{
   1695 #ifdef DEBUG
   1696 			__CTRACE(__CTRACE_REFRESH, " notdirty\n");
   1697 #endif
   1698 			__virtscr->alines[target]->flags &= ~__ISDIRTY;
   1699 		} else
   1700 			if ((n > 0 && target >= top && target < top + n) ||
   1701 			    (n < 0 && target <= bot && target > bot + n))
   1702 			{
   1703 				if (clp->hash != blank_hash ||
   1704 				    !lineeq(clp->line, clp->line + 1,
   1705 					    (__virtscr->maxx - 1)) ||
   1706 				    !celleq(clp->line, buf))
   1707 				{
   1708 					for (i = __virtscr->maxx;
   1709 					    i > BLANKSIZE;
   1710 					    i -= BLANKSIZE) {
   1711 						(void) memcpy(clp->line + i -
   1712 						    BLANKSIZE, buf, sizeof(buf));
   1713 					}
   1714 					(void) memcpy(clp->line , buf, i *
   1715 					    sizeof(buf[0]));
   1716 #ifdef DEBUG
   1717 					__CTRACE(__CTRACE_REFRESH,
   1718 					    " blanked out: dirty\n");
   1719 #endif
   1720 					clp->hash = blank_hash;
   1721 					__touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
   1722 				} else {
   1723 #ifdef DEBUG
   1724 					__CTRACE(__CTRACE_REFRESH,
   1725 					    " -- blank line already: dirty\n");
   1726 #endif
   1727 					__touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
   1728 				}
   1729 			} else {
   1730 #ifdef DEBUG
   1731 				__CTRACE(__CTRACE_REFRESH, " -- dirty\n");
   1732 #endif
   1733 				__touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
   1734 			}
   1735 		if (target == cur_period) {
   1736 			i = target + 1;
   1737 			tmp1 = curscr->alines[i];
   1738 			cur_period = i;
   1739 		} else {
   1740 			tmp1 = tmp2;
   1741 			i = target;
   1742 		}
   1743 	}
   1744 #ifdef DEBUG
   1745 	__CTRACE(__CTRACE_REFRESH, "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
   1746 	for (i = 0; i < curscr->maxy; i++) {
   1747 		__CTRACE(__CTRACE_REFRESH, "C: %d:", i);
   1748 		for (j = 0; j < curscr->maxx; j++)
   1749 			__CTRACE(__CTRACE_REFRESH, "%c",
   1750 			    curscr->alines[i]->line[j].ch);
   1751 		__CTRACE(__CTRACE_REFRESH, "\n");
   1752 		__CTRACE(__CTRACE_REFRESH, "W: %d:", i);
   1753 		for (j = 0; j < __virtscr->maxx; j++)
   1754 			__CTRACE(__CTRACE_REFRESH, "%c",
   1755 			    __virtscr->alines[i]->line[j].ch);
   1756 		__CTRACE(__CTRACE_REFRESH, "\n");
   1757 	}
   1758 #endif
   1759 	if (n != 0)
   1760 		scrolln(starts, startw, curs, bot, top);
   1761 }
   1762 
   1763 /*
   1764  * scrolln --
   1765  *	Scroll n lines, where n is starts - startw.
   1766  */
   1767 static void /* ARGSUSED */
   1768 scrolln(int starts, int startw, int curs, int bot, int top)
   1769 {
   1770 	int	i, oy, ox, n;
   1771 
   1772 	oy = curscr->cury;
   1773 	ox = curscr->curx;
   1774 	n = starts - startw;
   1775 
   1776 	/*
   1777 	 * XXX
   1778 	 * The initial tests that set __noqch don't let us reach here unless
   1779 	 * we have either cs + ho + SF/sf/SR/sr, or AL + DL.  SF/sf and SR/sr
   1780 	 * scrolling can only shift the entire scrolling region, not just a
   1781 	 * part of it, which means that the quickch() routine is going to be
   1782 	 * sadly disappointed in us if we don't have cs as well.
   1783 	 *
   1784 	 * If cs, ho and SF/sf are set, can use the scrolling region.  Because
   1785 	 * the cursor position after cs is undefined, we need ho which gives us
   1786 	 * the ability to move to somewhere without knowledge of the current
   1787 	 * location of the cursor.  Still call __mvcur() anyway, to update its
   1788 	 * idea of where the cursor is.
   1789 	 *
   1790 	 * When the scrolling region has been set, the cursor has to be at the
   1791 	 * last line of the region to make the scroll happen.
   1792 	 *
   1793 	 * Doing SF/SR or AL/DL appears faster on the screen than either sf/sr
   1794 	 * or AL/DL, and, some terminals have AL/DL, sf/sr, and cs, but not
   1795 	 * SF/SR.  So, if we're scrolling almost all of the screen, try and use
   1796 	 * AL/DL, otherwise use the scrolling region.  The "almost all" is a
   1797 	 * shameless hack for vi.
   1798 	 */
   1799 	if (n > 0) {
   1800 		if (change_scroll_region != NULL && cursor_home != NULL &&
   1801 		    (parm_index != NULL ||
   1802 		    ((parm_insert_line == NULL || parm_delete_line == NULL ||
   1803 		    top > 3 || bot + 3 < __virtscr->maxy) &&
   1804 		    scroll_forward != NULL)))
   1805 		{
   1806 			tputs(tiparm(change_scroll_region, top, bot),
   1807 			    0, __cputchar);
   1808 			__mvcur(oy, ox, 0, 0, 1);
   1809 			tputs(cursor_home, 0, __cputchar);
   1810 			__mvcur(0, 0, bot, 0, 1);
   1811 			if (parm_index != NULL)
   1812 				tputs(tiparm(parm_index, n),
   1813 				    0, __cputchar);
   1814 			else
   1815 				for (i = 0; i < n; i++)
   1816 					tputs(scroll_forward, 0, __cputchar);
   1817 			tputs(tiparm(change_scroll_region,
   1818 			    0, (int)__virtscr->maxy - 1), 0, __cputchar);
   1819 			__mvcur(bot, 0, 0, 0, 1);
   1820 			tputs(cursor_home, 0, __cputchar);
   1821 			__mvcur(0, 0, oy, ox, 1);
   1822 			return;
   1823 		}
   1824 
   1825 		/* Scroll up the block. */
   1826 		if (parm_index != NULL && top == 0) {
   1827 			__mvcur(oy, ox, bot, 0, 1);
   1828 			tputs(tiparm(parm_index, n), 0, __cputchar);
   1829 		} else
   1830 			if (parm_delete_line != NULL) {
   1831 				__mvcur(oy, ox, top, 0, 1);
   1832 				tputs(tiparm(parm_delete_line, n),
   1833 				    0, __cputchar);
   1834 			} else
   1835 				if (delete_line != NULL) {
   1836 					__mvcur(oy, ox, top, 0, 1);
   1837 					for (i = 0; i < n; i++)
   1838 						tputs(delete_line, 0,
   1839 						    __cputchar);
   1840 				} else
   1841 					if (scroll_forward != NULL && top == 0) {
   1842 						__mvcur(oy, ox, bot, 0, 1);
   1843 						for (i = 0; i < n; i++)
   1844 							tputs(scroll_forward, 0,
   1845 							    __cputchar);
   1846 					} else
   1847 						abort();
   1848 
   1849 		/* Push down the bottom region. */
   1850 		__mvcur(top, 0, bot - n + 1, 0, 1);
   1851 		if (parm_insert_line != NULL)
   1852 			tputs(tiparm(parm_insert_line, n), 0, __cputchar);
   1853 		else {
   1854 			if (insert_line != NULL) {
   1855 				for (i = 0; i < n; i++)
   1856 					tputs(insert_line, 0, __cputchar);
   1857 			} else
   1858 				abort();
   1859 		}
   1860 		__mvcur(bot - n + 1, 0, oy, ox, 1);
   1861 	} else {
   1862 		/*
   1863 		 * !!!
   1864 		 * n < 0
   1865 		 *
   1866 		 * If cs, ho and SR/sr are set, can use the scrolling region.
   1867 		 * See the above comments for details.
   1868 		 */
   1869 		if (change_scroll_region != NULL && cursor_home != NULL &&
   1870 		    (parm_rindex != NULL ||
   1871 		    ((parm_insert_line == NULL || parm_delete_line == NULL ||
   1872 		    top > 3 ||
   1873 		    bot + 3 < __virtscr->maxy) && scroll_reverse != NULL)))
   1874 		{
   1875 			tputs(tiparm(change_scroll_region, top, bot),
   1876 			    0, __cputchar);
   1877 			__mvcur(oy, ox, 0, 0, 1);
   1878 			tputs(cursor_home, 0, __cputchar);
   1879 			__mvcur(0, 0, top, 0, 1);
   1880 
   1881 			if (parm_rindex != NULL)
   1882 				tputs(tiparm(parm_rindex, -n),
   1883 				    0, __cputchar);
   1884 			else
   1885 				for (i = n; i < 0; i++)
   1886 					tputs(scroll_reverse, 0, __cputchar);
   1887 			tputs(tiparm(change_scroll_region,
   1888 			    0, (int) __virtscr->maxy - 1), 0, __cputchar);
   1889 			__mvcur(top, 0, 0, 0, 1);
   1890 			tputs(cursor_home, 0, __cputchar);
   1891 			__mvcur(0, 0, oy, ox, 1);
   1892 			return;
   1893 		}
   1894 
   1895 		/* Preserve the bottom lines. */
   1896 		__mvcur(oy, ox, bot + n + 1, 0, 1);
   1897 		if (parm_rindex != NULL && bot == __virtscr->maxy)
   1898 			tputs(tiparm(parm_rindex, -n), 0, __cputchar);
   1899 		else {
   1900 			if (parm_delete_line != NULL)
   1901 				tputs(tiparm(parm_delete_line, -n),
   1902 				    0, __cputchar);
   1903 			else {
   1904 				if (delete_line != NULL)
   1905 					for (i = n; i < 0; i++)
   1906 						tputs(delete_line,
   1907 						    0, __cputchar);
   1908 				else {
   1909 					if (scroll_reverse != NULL &&
   1910 					    bot == __virtscr->maxy)
   1911 						for (i = n; i < 0; i++)
   1912 							tputs(scroll_reverse, 0,
   1913 							    __cputchar);
   1914 					else
   1915 						abort();
   1916 				}
   1917 			}
   1918 		}
   1919 		/* Scroll the block down. */
   1920 		__mvcur(bot + n + 1, 0, top, 0, 1);
   1921 		if (parm_insert_line != NULL)
   1922 			tputs(tiparm(parm_insert_line, -n), 0, __cputchar);
   1923 		else
   1924 			if (insert_line != NULL)
   1925 				for (i = n; i < 0; i++)
   1926 					tputs(insert_line, 0, __cputchar);
   1927 			else
   1928 				abort();
   1929 		__mvcur(top, 0, oy, ox, 1);
   1930 	}
   1931 }
   1932 
   1933 /*
   1934  * __unsetattr --
   1935  *	Unset attributes on curscr.  Leave standout, attribute and colour
   1936  *	modes if necessary (!ms).  Always leave altcharset (xterm at least
   1937  *	ignores a cursor move if we don't).
   1938  */
   1939 void /* ARGSUSED */
   1940 __unsetattr(int checkms)
   1941 {
   1942 	int	isms;
   1943 
   1944 	if (checkms) {
   1945 		if (!move_standout_mode)
   1946 			isms = 1;
   1947 		else
   1948 			isms = 0;
   1949 	} else
   1950 		isms = 1;
   1951 #ifdef DEBUG
   1952 	__CTRACE(__CTRACE_REFRESH,
   1953 	    "__unsetattr: checkms = %d, ms = %s, wattr = %08x\n",
   1954 	    checkms, move_standout_mode ? "TRUE" : "FALSE", curscr->wattr);
   1955 #endif
   1956 
   1957 	/*
   1958 	 * Don't leave the screen in standout mode (check against ms).  Check
   1959 	 * to see if we also turn off underscore, attributes and colour.
   1960 	 */
   1961 	if (curscr->wattr & __STANDOUT && isms) {
   1962 		tputs(exit_standout_mode, 0, __cputchar);
   1963 		curscr->wattr &= __mask_se;
   1964 	}
   1965 	/*
   1966 	 * Don't leave the screen in underscore mode (check against ms).
   1967 	 * Check to see if we also turn off attributes.  Assume that we
   1968 	 * also turn off colour.
   1969 	 */
   1970 	if (curscr->wattr & __UNDERSCORE && isms) {
   1971 		tputs(exit_underline_mode, 0, __cputchar);
   1972 		curscr->wattr &= __mask_ue;
   1973 	}
   1974 	/*
   1975 	 * Don't leave the screen with attributes set (check against ms).
   1976 	 * Assume that also turn off colour.
   1977 	 */
   1978 	if (curscr->wattr & __TERMATTR && isms) {
   1979 		tputs(exit_attribute_mode, 0, __cputchar);
   1980 		curscr->wattr &= __mask_me;
   1981 	}
   1982 	/* Don't leave the screen with altcharset set (don't check ms). */
   1983 	if (curscr->wattr & __ALTCHARSET) {
   1984 		tputs(exit_alt_charset_mode, 0, __cputchar);
   1985 		curscr->wattr &= ~__ALTCHARSET;
   1986 	}
   1987 	/* Don't leave the screen with colour set (check against ms). */
   1988 	if (__using_color && isms)
   1989 		__unset_color(curscr);
   1990 }
   1991 
   1992 #ifdef HAVE_WCHAR
   1993 /* compare two cells on screen, must have the same forground/background,
   1994  * and the same sequence of non-spacing characters */
   1995 static int
   1996 celleq(__LDATA *x, __LDATA *y)
   1997 {
   1998 	nschar_t *xnp = x->nsp, *ynp = y->nsp;
   1999 	int ret = ( x->ch == y->ch ) && ( x->attr == y->attr );
   2000 
   2001 	if (!ret)
   2002 		return 0;
   2003 	if (!xnp && !ynp)
   2004 		return 1;
   2005 	if ((xnp && !ynp) || (!xnp && ynp))
   2006 		return 0;
   2007 
   2008 	while (xnp && ynp) {
   2009 		if (xnp->ch != ynp->ch)
   2010 			return 0;
   2011 		xnp = xnp->next;
   2012 		ynp = ynp->next;
   2013 	}
   2014 	return !xnp && !ynp;
   2015 }
   2016 
   2017 /* compare two line segments */
   2018 static int
   2019 lineeq(__LDATA *xl, __LDATA *yl, size_t len)
   2020 {
   2021 	int i = 0;
   2022 	__LDATA *xp = xl, *yp = yl;
   2023 
   2024 	for (i = 0; i < len; i++, xp++, yp++) {
   2025 		if (!celleq(xp, yp))
   2026 			return 0;
   2027 	}
   2028 	return 1;
   2029 }
   2030 
   2031 /*
   2032  * Output the non-spacing characters associated with the given character
   2033  * cell to the screen.
   2034  */
   2035 
   2036 void
   2037 __cursesi_putnsp(nschar_t *nsp, const int wy, const int wx)
   2038 {
   2039 	nschar_t *p;
   2040 
   2041 	/* this shuts up gcc warnings about wx and wy not being used */
   2042 	if (wx > wy) {
   2043 	}
   2044 
   2045 	p = nsp;
   2046 	while (p != NULL) {
   2047 		__cputwchar((int)p->ch);
   2048 #ifdef DEBUG
   2049 		__CTRACE(__CTRACE_REFRESH,
   2050 		       "_cursesi_putnsp: (%d,%d) non-spacing putwchar(0x%x)\n",
   2051 			 wy, wx - 1, p->ch);
   2052 #endif
   2053 		p = p->next;
   2054 	}
   2055 }
   2056 
   2057 #endif /* HAVE_WCHAR */
   2058