Home | History | Annotate | Line # | Download | only in libcurses
refresh.c revision 1.64
      1 /*	$NetBSD: refresh.c,v 1.64 2007/01/21 13:25:36 jdc 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.64 2007/01/21 13:25:36 jdc Exp $");
     38 #endif
     39 #endif				/* not lint */
     40 
     41 #include <stdlib.h>
     42 #include <string.h>
     43 
     44 #include "curses.h"
     45 #include "curses_private.h"
     46 
     47 static void	domvcur __P((int, int, int, int));
     48 static int	makech __P((int));
     49 static void	quickch __P((void));
     50 static void	scrolln __P((int, int, int, int, int));
     51 
     52 static int _cursesi_wnoutrefresh(SCREEN *, WINDOW *, int, int, int, int, int, int);
     53 
     54 #ifndef _CURSES_USE_MACROS
     55 
     56 /*
     57  * refresh --
     58  *	Make the current screen look like "stdscr" over the area covered by
     59  *	stdscr.
     60  */
     61 int
     62 refresh(void)
     63 {
     64 	return wrefresh(stdscr);
     65 }
     66 
     67 #endif
     68 
     69 /*
     70  * wnoutrefresh --
     71  *	Add the contents of "win" to the virtual window.
     72  */
     73 int
     74 wnoutrefresh(WINDOW *win)
     75 {
     76 #ifdef DEBUG
     77 	__CTRACE(__CTRACE_REFRESH, "wnoutrefresh: win %p\n", win);
     78 #endif
     79 
     80 	return _cursesi_wnoutrefresh(_cursesi_screen, win, 0, 0, win->begy,
     81 	    win->begx, win->maxy, win->maxx);
     82 }
     83 
     84 /*
     85  * pnoutrefresh --
     86  *	Add the contents of "pad" to the virtual window.
     87  */
     88 int
     89 pnoutrefresh(WINDOW *pad, int pbegy, int pbegx, int sbegy, int sbegx,
     90     int smaxy, int smaxx)
     91 {
     92 	int pmaxy, pmaxx;
     93 
     94 #ifdef DEBUG
     95 	__CTRACE(__CTRACE_REFRESH, "pnoutrefresh: pad %p, flags 0x%08x\n",
     96 	    pad, pad->flags);
     97 	__CTRACE(__CTRACE_REFRESH,
     98 	    "pnoutrefresh: (%d, %d), (%d, %d), (%d, %d)\n",
     99 	    pbegy, pbegx, sbegy, sbegx, smaxy, smaxx);
    100 #endif
    101 
    102 	/* SUS says if these are negative, they should be treated as zero */
    103 	if (pbegy < 0)
    104 		pbegy = 0;
    105 	if (pbegx < 0)
    106 		pbegx = 0;
    107 	if (sbegy < 0)
    108 		sbegy = 0;
    109 	if (sbegx < 0)
    110 		sbegx = 0;
    111 
    112 	/* Calculate rectangle on pad - used by _cursesi_wnoutrefresh */
    113 	pmaxy = pbegy + smaxy - sbegy + 1;
    114 	pmaxx = pbegx + smaxx - sbegx + 1;
    115 
    116 	/* Check rectangle fits in pad */
    117 	if (pmaxy > pad->maxy - pad->begy)
    118 		pmaxy = pad->maxy - pad->begy;
    119 	if (pmaxx > pad->maxx - pad->begx)
    120 		pmaxx = pad->maxx - pad->begx;
    121 
    122 	if (smaxy - sbegy < 0 || smaxx - sbegx < 0 )
    123 		return ERR;
    124 
    125 	return _cursesi_wnoutrefresh(_cursesi_screen, pad,
    126 	    pad->begy + pbegy, pad->begx + pbegx, pad->begy + sbegy,
    127 	    pad->begx + sbegx, pmaxy, pmaxx);
    128 }
    129 
    130 /*
    131  * _cursesi_wnoutrefresh --
    132  *      Does the grunt work for wnoutrefresh to the given screen.
    133  *	Copies the part of the window given by the rectangle
    134  *	(begy, begx) to (maxy, maxx) at screen position (wbegy, wbegx).
    135  */
    136 int
    137 _cursesi_wnoutrefresh(SCREEN *screen, WINDOW *win, int begy, int begx,
    138     int wbegy, int wbegx, int maxy, int maxx)
    139 {
    140 
    141 	short	sy, wy, wx, y_off, x_off, mx;
    142 	__LINE	*wlp, *vlp;
    143 	WINDOW	*sub_win, *orig;
    144 
    145 #ifdef DEBUG
    146 	__CTRACE(__CTRACE_REFRESH, "_wnoutrefresh: win %p, flags 0x%08x\n",
    147 	    win, win->flags);
    148 	__CTRACE(__CTRACE_REFRESH,
    149 	    "_wnoutrefresh: (%d, %d), (%d, %d), (%d, %d)\n",
    150 	    begy, begx, wbegy, wbegx, maxy, maxx);
    151 #endif
    152 
    153 	if (screen->curwin)
    154 		return OK;
    155 
    156 	/*
    157 	 * Recurse through any sub-windows, mark as dirty lines on the parent
    158 	 * window that are dirty on the sub-window and clear the dirty flag on
    159 	 * the sub-window.
    160 	 */
    161 	if (win->orig == 0) {
    162 		orig = win;
    163 		for (sub_win = win->nextp; sub_win != win;
    164 		    sub_win = sub_win->nextp) {
    165 #ifdef DEBUG
    166 			__CTRACE(__CTRACE_REFRESH,
    167 			    "wnout_refresh: win %p, sub_win %p\n",
    168 			    orig, sub_win);
    169 #endif
    170 			for (sy = 0; sy < sub_win->maxy; sy++) {
    171 				if (sub_win->lines[sy]->flags == __ISDIRTY) {
    172 					orig->lines[sy + sub_win->begy - orig->begy]->flags |= __ISDIRTY;
    173 					sub_win->lines[sy]->flags &= ~__ISDIRTY;
    174 				}
    175 			}
    176 		}
    177 	}
    178 
    179 	/* Check that cursor position on "win" is valid for "__virtscr" */
    180 	if (win->cury + wbegy - begy < screen->__virtscr->maxy &&
    181 	    win->cury + wbegy - begy >= 0 && win->cury < maxy - begy)
    182 		screen->__virtscr->cury = win->cury + wbegy - begy;
    183 	if (win->curx + wbegx - begx < screen->__virtscr->maxx &&
    184 	    win->curx + wbegx - begx >= 0 && win->curx < maxx - begx)
    185 		screen->__virtscr->curx = win->curx + wbegx - begx;
    186 
    187 	/* Copy the window flags from "win" to "__virtscr" */
    188 	if (win->flags & __CLEAROK) {
    189 		if (win->flags & __FULLWIN)
    190 			screen->__virtscr->flags |= __CLEAROK;
    191 		win->flags &= ~__CLEAROK;
    192 	}
    193 	screen->__virtscr->flags &= ~__LEAVEOK;
    194 	screen->__virtscr->flags |= win->flags;
    195 
    196 	for (wy = begy, y_off = wbegy; wy < maxy &&
    197 	    y_off < screen->__virtscr->maxy; wy++, y_off++) {
    198  		wlp = win->lines[wy];
    199 #ifdef DEBUG
    200 		__CTRACE(__CTRACE_REFRESH,
    201 		    "_wnoutrefresh: wy %d\tf %d\tl %d\tflags %x\n",
    202 		    wy, *wlp->firstchp, *wlp->lastchp, wlp->flags);
    203 #endif
    204 		if ((wlp->flags & __ISDIRTY) == 0)
    205 			continue;
    206 		vlp = screen->__virtscr->lines[y_off];
    207 
    208 		if (*wlp->firstchp < maxx + win->ch_off &&
    209 		    *wlp->lastchp >= win->ch_off) {
    210 			/* Set start column */
    211 			wx = begx;
    212 			x_off = wbegx;
    213 			if (*wlp->firstchp - win->ch_off > 0) {
    214 				wx += *wlp->firstchp - win->ch_off;
    215 				x_off += *wlp->firstchp - win->ch_off;
    216 			}
    217 			/* Set finish column */
    218 			mx = maxx;
    219 			if (mx > *wlp->lastchp - win->ch_off + 1)
    220 				mx = *wlp->lastchp - win->ch_off + 1;
    221 			if (x_off + (mx - wx) > __virtscr->maxx)
    222 				mx -= (x_off + maxx) - __virtscr->maxx;
    223 			/* Copy line from "win" to "__virtscr". */
    224 			while (wx < mx) {
    225 #ifdef DEBUG
    226 				__CTRACE(__CTRACE_REFRESH,
    227 				    "_wnoutrefresh: copy from %d, "
    228 				    "%d to %d, %d\n",
    229 				    wy, wx, y_off, x_off);
    230 #endif
    231 				/* Copy character */
    232 				vlp->line[x_off].ch = wlp->line[wx].ch;
    233 				/* Copy attributes  */
    234 				vlp->line[x_off].attr = wlp->line[wx].attr;
    235 				/* Check for nca conflict with colour */
    236 				if ((vlp->line[x_off].attr & __COLOR) &&
    237 				    (vlp->line[x_off].attr &
    238 				    _cursesi_screen->nca))
    239 					vlp->line[x_off].attr &= ~__COLOR;
    240 				wx++;
    241 				x_off++;
    242 			}
    243 
    244 			/* Set flags on "__virtscr" and unset on "win". */
    245 			if (wlp->flags & __ISPASTEOL)
    246 				vlp->flags |= __ISPASTEOL;
    247 			else
    248 				vlp->flags &= ~__ISPASTEOL;
    249 			if (wlp->flags & __ISDIRTY)
    250 				vlp->flags |= __ISDIRTY;
    251 
    252 #ifdef DEBUG
    253 			__CTRACE(__CTRACE_REFRESH,
    254 			    "win: firstch = %d, lastch = %d\n",
    255 			    *wlp->firstchp, *wlp->lastchp);
    256 #endif
    257 			/* Set change pointers on "__virtscr". */
    258 			if (*vlp->firstchp >
    259 			    *wlp->firstchp + wbegx - win->ch_off)
    260 				*vlp->firstchp =
    261 				    *wlp->firstchp + wbegx - win->ch_off;
    262 			if (*vlp->lastchp <
    263 			    *wlp->lastchp + wbegx - win->ch_off)
    264 				*vlp->lastchp =
    265 				    *wlp->lastchp + wbegx - win->ch_off;
    266 #ifdef DEBUG
    267 			__CTRACE(__CTRACE_REFRESH,
    268 			    "__virtscr: firstch = %d, lastch = %d\n",
    269 			    *vlp->firstchp, *vlp->lastchp);
    270 #endif
    271 			/*
    272 			 * Unset change pointers only if a window, as a pad
    273 			 * can be displayed again without any of the contents
    274 			 * changing.
    275 			 */
    276 			if (!(win->flags & __ISPAD)) {
    277 				/* Set change pointers on "win". */
    278 				if (*wlp->firstchp >= win->ch_off)
    279 					*wlp->firstchp = maxx + win->ch_off;
    280 				if (*wlp->lastchp < maxx + win->ch_off)
    281 					*wlp->lastchp = win->ch_off;
    282 				if ((*wlp->lastchp < *wlp->firstchp) ||
    283 				    (*wlp->firstchp >= maxx + win->ch_off) ||
    284 				    (*wlp->lastchp <= win->ch_off)) {
    285 #ifdef DEBUG
    286 					__CTRACE(__CTRACE_REFRESH,
    287 					    "_wnoutrefresh: "
    288 					    "line %d notdirty\n", wy);
    289 #endif
    290 					wlp->flags &= ~__ISDIRTY;
    291 				}
    292 			}
    293 		}
    294 	}
    295 	return OK;
    296 }
    297 
    298 /*
    299  * wrefresh --
    300  *	Make the current screen look like "win" over the area covered by
    301  *	win.
    302  */
    303 int
    304 wrefresh(WINDOW *win)
    305 {
    306 	int retval;
    307 
    308 #ifdef DEBUG
    309 	__CTRACE(__CTRACE_REFRESH, "wrefresh: win %p\n", win);
    310 #endif
    311 
    312 	_cursesi_screen->curwin = (win == _cursesi_screen->curscr);
    313 	if (!_cursesi_screen->curwin)
    314 		retval = _cursesi_wnoutrefresh(_cursesi_screen, win, 0, 0,
    315 		    win->begy, win->begx, win->maxy, win->maxx);
    316 	else
    317 		retval = OK;
    318 	if (retval == OK) {
    319 		retval = doupdate();
    320 		if (!(win->flags & __LEAVEOK)) {
    321 			win->cury = max(0, curscr->cury - win->begy);
    322 			win->curx = max(0, curscr->curx - win->begx);
    323 		}
    324 	}
    325 	_cursesi_screen->curwin = 0;
    326 	return(retval);
    327 }
    328 
    329  /*
    330  * prefresh --
    331  *	Make the current screen look like "pad" over the area coverd by
    332  *	the specified area of pad.
    333  */
    334 int
    335 prefresh(WINDOW *pad, int pbegy, int pbegx, int sbegy, int sbegx,
    336     int smaxy, int smaxx)
    337 {
    338 	int retval;
    339 
    340 #ifdef DEBUG
    341 	__CTRACE(__CTRACE_REFRESH, "prefresh: pad %p, flags 0x%08x\n",
    342 	    pad, pad->flags);
    343 #endif
    344 	/* Retain values in case pechochar() is called. */
    345 	pad->pbegy = pbegy;
    346 	pad->pbegx = pbegx;
    347 	pad->sbegy = sbegy;
    348 	pad->sbegx = sbegx;
    349 	pad->smaxy = smaxy;
    350 	pad->smaxx = smaxx;
    351 
    352 	/* Use pnoutrefresh() to avoid duplicating code here */
    353 	retval = pnoutrefresh(pad, pbegy, pbegx, sbegy, sbegx, smaxy, smaxx);
    354 	if (retval == OK) {
    355 		retval = doupdate();
    356 		if (!(pad->flags & __LEAVEOK)) {
    357 			pad->cury = max(0, curscr->cury - pad->begy);
    358 			pad->curx = max(0, curscr->curx - pad->begx);
    359 		}
    360 	}
    361 	return(retval);
    362 }
    363 
    364 /*
    365  * doupdate --
    366  *	Make the current screen look like the virtual window "__virtscr".
    367  */
    368 int
    369 doupdate(void)
    370 {
    371 	WINDOW	*win;
    372 	__LINE	*wlp;
    373 	short	 wy;
    374 	int	 dnum;
    375 
    376 	/* Check if we need to restart ... */
    377 	if (_cursesi_screen->endwin)
    378 		__restartwin();
    379 
    380 	if (_cursesi_screen->curwin)
    381 		win = curscr;
    382 	else
    383 		win = _cursesi_screen->__virtscr;
    384 
    385 	/* Initialize loop parameters. */
    386 	_cursesi_screen->ly = curscr->cury;
    387 	_cursesi_screen->lx = curscr->curx;
    388 	wy = 0;
    389 
    390 	if (!_cursesi_screen->curwin)
    391 		for (wy = 0; wy < win->maxy; wy++) {
    392 			wlp = win->lines[wy];
    393 			if (wlp->flags & __ISDIRTY)
    394 				wlp->hash = __hash(wlp->line,
    395 				    (size_t)(win->maxx * __LDATASIZE));
    396 		}
    397 
    398 	if ((win->flags & __CLEAROK) || (curscr->flags & __CLEAROK) ||
    399 	    _cursesi_screen->curwin) {
    400 		if (curscr->wattr & __COLOR)
    401 			__unsetattr(0);
    402 		tputs(__tc_cl, 0, __cputchar);
    403 		_cursesi_screen->ly = 0;
    404 		_cursesi_screen->lx = 0;
    405 		if (!_cursesi_screen->curwin) {
    406 			curscr->flags &= ~__CLEAROK;
    407 			curscr->cury = 0;
    408 			curscr->curx = 0;
    409 			werase(curscr);
    410 		}
    411 		__touchwin(win);
    412 		win->flags &= ~__CLEAROK;
    413 	}
    414 	if (!__CA) {
    415 		if (win->curx != 0)
    416 			__cputchar('\n');
    417 		if (!_cursesi_screen->curwin)
    418 			werase(curscr);
    419 	}
    420 #ifdef DEBUG
    421 	__CTRACE(__CTRACE_REFRESH, "doupdate: (%p): curwin = %d\n", win,
    422 		 _cursesi_screen->curwin);
    423 	__CTRACE(__CTRACE_REFRESH, "doupdate: \tfirstch\tlastch\n");
    424 #endif
    425 
    426 	if (!_cursesi_screen->curwin) {
    427 		/*
    428 		 * Invoke quickch() only if more than a quarter of the lines
    429 		 * in the window are dirty.
    430 		 */
    431 		for (wy = 0, dnum = 0; wy < win->maxy; wy++)
    432 			if (win->lines[wy]->flags & __ISDIRTY)
    433 				dnum++;
    434 		if (!__noqch && dnum > (int) win->maxy / 4)
    435 			quickch();
    436 	}
    437 
    438 #ifdef DEBUG
    439 	{
    440 		int	i, j;
    441 
    442 		__CTRACE(__CTRACE_REFRESH,
    443 		    "#####################################\n");
    444 		for (i = 0; i < curscr->maxy; i++) {
    445 			__CTRACE(__CTRACE_REFRESH, "C: %d:", i);
    446 			__CTRACE(__CTRACE_REFRESH, " 0x%x \n",
    447 			    curscr->lines[i]->hash);
    448 			for (j = 0; j < curscr->maxx; j++)
    449 				__CTRACE(__CTRACE_REFRESH, "%c",
    450 				    curscr->lines[i]->line[j].ch);
    451 			__CTRACE(__CTRACE_REFRESH, "\n");
    452 			__CTRACE(__CTRACE_REFRESH, " attr:");
    453 			for (j = 0; j < curscr->maxx; j++)
    454 				__CTRACE(__CTRACE_REFRESH, " %x",
    455 					 curscr->lines[i]->line[j].attr);
    456 			__CTRACE(__CTRACE_REFRESH, "\n");
    457 			__CTRACE(__CTRACE_REFRESH, "W: %d:", i);
    458 			__CTRACE(__CTRACE_REFRESH, " 0x%x \n",
    459 			    win->lines[i]->hash);
    460 			__CTRACE(__CTRACE_REFRESH, " 0x%x ",
    461 			    win->lines[i]->flags);
    462 			for (j = 0; j < win->maxx; j++)
    463 				__CTRACE(__CTRACE_REFRESH, "%c",
    464 				    win->lines[i]->line[j].ch);
    465 			__CTRACE(__CTRACE_REFRESH, "\n");
    466 			__CTRACE(__CTRACE_REFRESH, " attr:");
    467 			for (j = 0; j < win->maxx; j++)
    468 				__CTRACE(__CTRACE_REFRESH, " %x",
    469 				    win->lines[i]->line[j].attr);
    470 			__CTRACE(__CTRACE_REFRESH, "\n");
    471 		}
    472 	}
    473 #endif				/* DEBUG */
    474 
    475 	for (wy = 0; wy < win->maxy; wy++) {
    476 		wlp = win->lines[wy];
    477 /* XXX: remove this debug */
    478 #ifdef DEBUG
    479 		__CTRACE(__CTRACE_REFRESH,
    480 		    "doupdate: wy %d\tf: %d\tl:%d\tflags %x\n",
    481 		    wy, *wlp->firstchp, *wlp->lastchp, wlp->flags);
    482 #endif
    483 		if (!_cursesi_screen->curwin)
    484 			curscr->lines[wy]->hash = wlp->hash;
    485 		if (wlp->flags & __ISDIRTY) {
    486 			if (makech(wy) == ERR)
    487 				return (ERR);
    488 			else {
    489 				if (*wlp->firstchp >= 0)
    490 					*wlp->firstchp = win->maxx;
    491 				if (*wlp->lastchp < win->maxx)
    492 					*wlp->lastchp = 0;
    493 				if (*wlp->lastchp < *wlp->firstchp) {
    494 #ifdef DEBUG
    495 					__CTRACE(__CTRACE_REFRESH,
    496 					    "doupdate: line %d notdirty\n", wy);
    497 #endif
    498 					wlp->flags &= ~__ISDIRTY;
    499 				}
    500 			}
    501 
    502 		}
    503 #ifdef DEBUG
    504 		__CTRACE(__CTRACE_REFRESH, "t%d\t%d\n",
    505 		    *wlp->firstchp, *wlp->lastchp);
    506 #endif
    507 	}
    508 
    509 #ifdef DEBUG
    510 	__CTRACE(__CTRACE_REFRESH, "doupdate: ly=%d, lx=%d\n",
    511 	    _cursesi_screen->ly, _cursesi_screen->lx);
    512 #endif
    513 
    514 	if (_cursesi_screen->curwin)
    515 		domvcur(_cursesi_screen->ly, _cursesi_screen->lx,
    516 			(int) win->cury, (int) win->curx);
    517 	else {
    518 		if (win->flags & __LEAVEOK) {
    519 			curscr->cury = _cursesi_screen->ly;
    520 			curscr->curx = _cursesi_screen->lx;
    521 		} else {
    522 			domvcur(_cursesi_screen->ly, _cursesi_screen->lx,
    523 				win->cury, win->curx);
    524 			curscr->cury = win->cury;
    525 			curscr->curx = win->curx;
    526 		}
    527 	}
    528 
    529 	/* Don't leave the screen with attributes set. */
    530 	__unsetattr(0);
    531 	return fflush(_cursesi_screen->outfd) == EOF ? ERR : OK;
    532 }
    533 
    534 /*
    535  * makech --
    536  *	Make a change on the screen.
    537  */
    538 static int
    539 makech(wy)
    540 	int	wy;
    541 {
    542 	WINDOW	*win;
    543 	static __LDATA blank = {' ', 0};
    544 	__LDATA *nsp, *csp, *cp, *cep;
    545 	int	clsp, nlsp;	/* Last space in lines. */
    546 	int	lch, wx;
    547 	char	*ce;
    548 	attr_t	lspc;		/* Last space colour */
    549 	attr_t	off, on;
    550 
    551 #ifdef __GNUC__
    552 	nlsp = lspc = 0;	/* XXX gcc -Wuninitialized */
    553 #endif
    554 	if (_cursesi_screen->curwin)
    555 		win = curscr;
    556 	else
    557 		win = __virtscr;
    558 	/* Is the cursor still on the end of the last line? */
    559 	if (wy > 0 && curscr->lines[wy - 1]->flags & __ISPASTEOL) {
    560 		domvcur(_cursesi_screen->ly, _cursesi_screen->lx,
    561 			_cursesi_screen->ly + 1, 0);
    562 		_cursesi_screen->ly++;
    563 		_cursesi_screen->lx = 0;
    564 	}
    565 	wx = *win->lines[wy]->firstchp;
    566 	if (wx < 0)
    567 		wx = 0;
    568 	else
    569 		if (wx >= win->maxx)
    570 			return (OK);
    571 	lch = *win->lines[wy]->lastchp;
    572 	if (lch < 0)
    573 		return (OK);
    574 	else
    575 		if (lch >= (int) win->maxx)
    576 			lch = win->maxx - 1;
    577 
    578 	if (_cursesi_screen->curwin)
    579 		csp = &blank;
    580 	else
    581 		csp = &curscr->lines[wy]->line[wx];
    582 
    583 	nsp = &win->lines[wy]->line[wx];
    584 	if (__tc_ce && !_cursesi_screen->curwin) {
    585 		cp = &win->lines[wy]->line[win->maxx - 1];
    586 		lspc = cp->attr & __COLOR;
    587 		while (cp->ch == ' ' && cp->attr == lspc)
    588 			if (cp-- <= win->lines[wy]->line)
    589 				break;
    590 		nlsp = cp - win->lines[wy]->line;
    591 		if (nlsp < 0)
    592 			nlsp = 0;
    593 	}
    594 	if (!_cursesi_screen->curwin)
    595 		ce = __tc_ce;
    596 	else
    597 		ce = NULL;
    598 
    599 	while (wx <= lch) {
    600 		if (memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
    601 			if (wx <= lch) {
    602 				while (wx <= lch &&
    603 				    memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
    604 					nsp++;
    605 					if (!_cursesi_screen->curwin)
    606 						++csp;
    607 					++wx;
    608 				}
    609 				continue;
    610 			}
    611 			break;
    612 		}
    613 		domvcur(_cursesi_screen->ly, _cursesi_screen->lx, wy, wx);
    614 
    615 #ifdef DEBUG
    616 		__CTRACE(__CTRACE_REFRESH, "makech: 1: wx = %d, ly= %d, "
    617 		    "lx = %d, newy = %d, newx = %d\n",
    618 		    wx, _cursesi_screen->ly, _cursesi_screen->lx, wy, wx);
    619 #endif
    620 		_cursesi_screen->ly = wy;
    621 		_cursesi_screen->lx = wx;
    622 		while (wx <= lch && memcmp(nsp, csp, sizeof(__LDATA)) != 0) {
    623 			if (ce != NULL &&
    624 			    wx >= nlsp && nsp->ch == ' ' && nsp->attr == lspc) {
    625 				/* Check for clear to end-of-line. */
    626 				cep = &curscr->lines[wy]->line[win->maxx - 1];
    627 				while (cep->ch == ' ' && cep->attr == lspc)
    628 					if (cep-- <= csp)
    629 						break;
    630 				clsp = cep - curscr->lines[wy]->line -
    631 				    win->begx * __LDATASIZE;
    632 #ifdef DEBUG
    633 				__CTRACE(__CTRACE_REFRESH,
    634 				    "makech: clsp = %d, nlsp = %d\n",
    635 				    clsp, nlsp);
    636 #endif
    637 				if (((clsp - nlsp >= strlen(__tc_ce) &&
    638 				    clsp < win->maxx * __LDATASIZE) ||
    639 				    wy == win->maxy - 1) &&
    640 				    (!(lspc & __COLOR) ||
    641 				    ((lspc & __COLOR) && __tc_ut))) {
    642 					__unsetattr(0);
    643 					if (__using_color &&
    644 					    ((lspc & __COLOR) !=
    645 					    (curscr->wattr & __COLOR)))
    646 						__set_color(curscr, lspc &
    647 						    __COLOR);
    648 					tputs(__tc_ce, 0, __cputchar);
    649 					_cursesi_screen->lx = wx + win->begx;
    650 					while (wx++ <= clsp) {
    651 						csp->ch = ' ';
    652 						csp->attr = lspc;
    653 						csp++;
    654 					}
    655 					return (OK);
    656 				}
    657 				ce = NULL;
    658 			}
    659 
    660 #ifdef DEBUG
    661 				__CTRACE(__CTRACE_REFRESH,
    662 				    "makech: have attr %08x, need attr %08x\n",
    663 				    curscr->wattr, nsp->attr);
    664 #endif
    665 
    666 			off = ~nsp->attr & curscr->wattr;
    667 
    668 			/*
    669 			 * Unset attributes as appropriate.  Unset first
    670 			 * so that the relevant attributes can be reset
    671 			 * (because 'me' unsets 'mb', 'md', 'mh', 'mk',
    672 			 * 'mp' and 'mr').  Check to see if we also turn off
    673 			 * standout, attributes and colour.
    674 			 */
    675 			if (off & __TERMATTR && __tc_me != NULL) {
    676 				tputs(__tc_me, 0, __cputchar);
    677 				curscr->wattr &= __mask_me;
    678 				off &= __mask_me;
    679 			}
    680 
    681 			/*
    682 			 * Exit underscore mode if appropriate.
    683 			 * Check to see if we also turn off standout,
    684 			 * attributes and colour.
    685 			 */
    686 			if (off & __UNDERSCORE && __tc_ue != NULL) {
    687 				tputs(__tc_ue, 0, __cputchar);
    688 				curscr->wattr &= __mask_ue;
    689 				off &= __mask_ue;
    690 			}
    691 
    692 			/*
    693 			 * Exit standout mode as appropriate.
    694 			 * Check to see if we also turn off underscore,
    695 			 * attributes and colour.
    696 			 * XXX
    697 			 * Should use uc if so/se not available.
    698 			 */
    699 			if (off & __STANDOUT && __tc_se != NULL) {
    700 				tputs(__tc_se, 0, __cputchar);
    701 				curscr->wattr &= __mask_se;
    702 				off &= __mask_se;
    703 			}
    704 
    705 			if (off & __ALTCHARSET && __tc_ae != NULL) {
    706 				tputs(__tc_ae, 0, __cputchar);
    707 				curscr->wattr &= ~__ALTCHARSET;
    708 			}
    709 
    710 			/* Set/change colour as appropriate. */
    711 			if (__using_color)
    712 				__set_color(curscr, nsp->attr & __COLOR);
    713 
    714 			on = nsp->attr & ~curscr->wattr;
    715 
    716 			/*
    717 			 * Enter standout mode if appropriate.
    718 			 */
    719 			if (on & __STANDOUT && __tc_so != NULL && __tc_se
    720 			    != NULL) {
    721 				tputs(__tc_so, 0, __cputchar);
    722 				curscr->wattr |= __STANDOUT;
    723 			}
    724 
    725 			/*
    726 			 * Enter underscore mode if appropriate.
    727 			 * XXX
    728 			 * Should use uc if us/ue not available.
    729 			 */
    730 			if (on & __UNDERSCORE && __tc_us != NULL &&
    731 			    __tc_ue != NULL) {
    732 				tputs(__tc_us, 0, __cputchar);
    733 				curscr->wattr |= __UNDERSCORE;
    734 			}
    735 
    736 			/*
    737 			 * Set other attributes as appropriate.
    738 			 */
    739 			if (__tc_me != NULL) {
    740 				if (on & __BLINK && __tc_mb != NULL) {
    741 					tputs(__tc_mb, 0, __cputchar);
    742 					curscr->wattr |= __BLINK;
    743 				}
    744 				if (on & __BOLD && __tc_md != NULL) {
    745 					tputs(__tc_md, 0, __cputchar);
    746 					curscr->wattr |= __BOLD;
    747 				}
    748 				if (on & __DIM && __tc_mh != NULL) {
    749 					tputs(__tc_mh, 0, __cputchar);
    750 					curscr->wattr |= __DIM;
    751 				}
    752 				if (on & __BLANK && __tc_mk != NULL) {
    753 					tputs(__tc_mk, 0, __cputchar);
    754 					curscr->wattr |= __BLANK;
    755 				}
    756 				if (on & __PROTECT && __tc_mp != NULL) {
    757 					tputs(__tc_mp, 0, __cputchar);
    758 					curscr->wattr |= __PROTECT;
    759 				}
    760 				if (on & __REVERSE && __tc_mr != NULL) {
    761 					tputs(__tc_mr, 0, __cputchar);
    762 					curscr->wattr |= __REVERSE;
    763 				}
    764 			}
    765 
    766 			/* Enter/exit altcharset mode as appropriate. */
    767 			if (on & __ALTCHARSET && __tc_as != NULL &&
    768 			    __tc_ae != NULL) {
    769 				tputs(__tc_as, 0, __cputchar);
    770 				curscr->wattr |= __ALTCHARSET;
    771 			}
    772 
    773 			wx++;
    774 			if (wx >= win->maxx &&
    775 			    wy == win->maxy - 1 && !_cursesi_screen->curwin)
    776 				if (win->flags & __SCROLLOK) {
    777 					if (win->flags & __ENDLINE)
    778 						__unsetattr(1);
    779 					if (!(win->flags & __SCROLLWIN)) {
    780 						if (!_cursesi_screen->curwin) {
    781 							csp->attr = nsp->attr;
    782 							__cputchar((int)
    783 							    (csp->ch =
    784 							    nsp->ch));
    785 						} else
    786 							__cputchar((int) nsp->ch);
    787 					}
    788 					if (wx < curscr->maxx) {
    789 						domvcur(_cursesi_screen->ly, wx,
    790 						    (int) (win->maxy - 1),
    791 						    (int) (win->maxx - 1));
    792 					}
    793 					_cursesi_screen->ly = win->maxy - 1;
    794 					_cursesi_screen->lx = win->maxx - 1;
    795 					return (OK);
    796 				}
    797 			if (wx < win->maxx || wy < win->maxy - 1 ||
    798 			    !(win->flags & __SCROLLWIN)) {
    799 				if (!_cursesi_screen->curwin) {
    800 					csp->attr = nsp->attr;
    801 					__cputchar((int) (csp->ch = nsp->ch));
    802 					csp++;
    803 				} else
    804 					__cputchar((int) nsp->ch);
    805 			}
    806 #ifdef DEBUG
    807 			__CTRACE(__CTRACE_REFRESH, "makech: putchar(%c)\n",
    808 			    nsp->ch & 0177);
    809 #endif
    810 			if (__tc_uc && ((nsp->attr & __STANDOUT) ||
    811 			    (nsp->attr & __UNDERSCORE))) {
    812 				__cputchar('\b');
    813 				tputs(__tc_uc, 0, __cputchar);
    814 			}
    815 			nsp++;
    816 #ifdef DEBUG
    817 			__CTRACE(__CTRACE_REFRESH,
    818 			    "makech: 2: wx = %d, lx = %d\n",
    819 			    wx, _cursesi_screen->lx);
    820 #endif
    821 		}
    822 		if (_cursesi_screen->lx == wx)	/* If no change. */
    823 			break;
    824 		_cursesi_screen->lx = wx;
    825 		if (_cursesi_screen->lx >= COLS && __tc_am)
    826 			_cursesi_screen->lx = COLS - 1;
    827 		else
    828 			if (wx >= win->maxx) {
    829 				domvcur(_cursesi_screen->ly,
    830 					_cursesi_screen->lx,
    831 					_cursesi_screen->ly,
    832 					(int) (win->maxx - 1));
    833 				_cursesi_screen->lx = win->maxx - 1;
    834 			}
    835 #ifdef DEBUG
    836 		__CTRACE(__CTRACE_REFRESH, "makech: 3: wx = %d, lx = %d\n",
    837 		    wx, _cursesi_screen->lx);
    838 #endif
    839 	}
    840 
    841 	return (OK);
    842 }
    843 
    844 /*
    845  * domvcur --
    846  *	Do a mvcur, leaving attributes if necessary.
    847  */
    848 static void
    849 domvcur(oy, ox, ny, nx)
    850 	int	oy, ox, ny, nx;
    851 {
    852 	__unsetattr(1);
    853 	__mvcur(oy, ox, ny, nx, 1);
    854 }
    855 
    856 /*
    857  * Quickch() attempts to detect a pattern in the change of the window
    858  * in order to optimize the change, e.g., scroll n lines as opposed to
    859  * repainting the screen line by line.
    860  */
    861 
    862 static __LDATA buf[128];
    863 static  u_int last_hash;
    864 static  size_t last_hash_len;
    865 #define BLANKSIZE (sizeof(buf) / sizeof(buf[0]))
    866 
    867 static void
    868 quickch(void)
    869 {
    870 #define THRESH		(int) __virtscr->maxy / 4
    871 
    872 	__LINE *clp, *tmp1, *tmp2;
    873 	int	bsize, curs, curw, starts, startw, i, j;
    874 	int	n, target, cur_period, bot, top, sc_region;
    875 	u_int	blank_hash;
    876 	attr_t	bcolor;
    877 
    878 #ifdef __GNUC__
    879 	curs = curw = starts = startw = 0;	/* XXX gcc -Wuninitialized */
    880 #endif
    881 	/*
    882 	 * Find how many lines from the top of the screen are unchanged.
    883 	 */
    884 	for (top = 0; top < __virtscr->maxy; top++)
    885 		if (__virtscr->lines[top]->flags & __ISDIRTY &&
    886 		    (__virtscr->lines[top]->hash != curscr->lines[top]->hash ||
    887 		     memcmp(__virtscr->lines[top]->line,
    888 			  curscr->lines[top]->line,
    889 			  (size_t) __virtscr->maxx * __LDATASIZE) != 0))
    890 			break;
    891 		else
    892 			__virtscr->lines[top]->flags &= ~__ISDIRTY;
    893 	/*
    894 	 * Find how many lines from bottom of screen are unchanged.
    895 	 */
    896 	for (bot = __virtscr->maxy - 1; bot >= 0; bot--)
    897 		if (__virtscr->lines[bot]->flags & __ISDIRTY &&
    898 		    (__virtscr->lines[bot]->hash != curscr->lines[bot]->hash ||
    899 		     memcmp(__virtscr->lines[bot]->line,
    900 			  curscr->lines[bot]->line,
    901 			  (size_t) __virtscr->maxx * __LDATASIZE) != 0))
    902 			break;
    903 		else
    904 			__virtscr->lines[bot]->flags &= ~__ISDIRTY;
    905 
    906 	/*
    907 	 * Work round an xterm bug where inserting lines causes all the
    908 	 * inserted lines to be covered with the background colour we
    909 	 * set on the first line (even if we unset it for subsequent
    910 	 * lines).
    911 	 */
    912 	bcolor = __virtscr->lines[min(top,
    913 	    __virtscr->maxy - 1)]->line[0].attr & __COLOR;
    914 	for (i = top + 1, j = 0; i < bot; i++) {
    915 		if ((__virtscr->lines[i]->line[0].attr & __COLOR) != bcolor) {
    916 			bcolor = __virtscr->lines[i]->line[__virtscr->maxx].
    917 			    attr & __COLOR;
    918 			j = i - top;
    919 		} else
    920 			break;
    921 	}
    922 	top += j;
    923 
    924 #ifdef NO_JERKINESS
    925 	/*
    926 	 * If we have a bottom unchanged region return.  Scrolling the
    927 	 * bottom region up and then back down causes a screen jitter.
    928 	 * This will increase the number of characters sent to the screen
    929 	 * but it looks better.
    930 	 */
    931 	if (bot < __virtscr->maxy - 1)
    932 		return;
    933 #endif				/* NO_JERKINESS */
    934 
    935 	/*
    936 	 * Search for the largest block of text not changed.
    937 	 * Invariants of the loop:
    938 	 * - Startw is the index of the beginning of the examined block in
    939 	 *   __virtscr.
    940 	 * - Starts is the index of the beginning of the examined block in
    941 	 *   curscr.
    942 	 * - Curw is the index of one past the end of the exmined block in
    943 	 *   __virtscr.
    944 	 * - Curs is the index of one past the end of the exmined block in
    945 	 *   curscr.
    946 	 * - bsize is the current size of the examined block.
    947 	*/
    948 
    949 	for (bsize = bot - top; bsize >= THRESH; bsize--) {
    950 		for (startw = top; startw <= bot - bsize; startw++)
    951 			for (starts = top; starts <= bot - bsize;
    952 			    starts++) {
    953 				for (curw = startw, curs = starts;
    954 				    curs < starts + bsize; curw++, curs++)
    955 					if (__virtscr->lines[curw]->hash !=
    956 					    curscr->lines[curs]->hash)
    957 						break;
    958 				if (curs != starts + bsize)
    959 					continue;
    960 				for (curw = startw, curs = starts;
    961 				    curs < starts + bsize; curw++, curs++)
    962 					if (memcmp(__virtscr->lines[curw]->line,
    963 					    curscr->lines[curs]->line,
    964 					    (size_t) __virtscr->maxx *
    965 					    __LDATASIZE) != 0)
    966 						break;
    967 				if (curs == starts + bsize)
    968 					goto done;
    969 			}
    970 	}
    971 done:
    972 
    973 	/* Did not find anything */
    974 	if (bsize < THRESH)
    975 		return;
    976 
    977 #ifdef DEBUG
    978 	__CTRACE(__CTRACE_REFRESH, "quickch:bsize=%d, starts=%d, startw=%d, "
    979 	    "curw=%d, curs=%d, top=%d, bot=%d\n",
    980 	    bsize, starts, startw, curw, curs, top, bot);
    981 #endif
    982 
    983 	/*
    984 	 * Make sure that there is no overlap between the bottom and top
    985 	 * regions and the middle scrolled block.
    986 	 */
    987 	if (bot < curs)
    988 		bot = curs - 1;
    989 	if (top > starts)
    990 		top = starts;
    991 
    992 	n = startw - starts;
    993 
    994 #ifdef DEBUG
    995 	__CTRACE(__CTRACE_REFRESH, "#####################################\n");
    996 	for (i = 0; i < curscr->maxy; i++) {
    997 		__CTRACE(__CTRACE_REFRESH, "C: %d:", i);
    998 		__CTRACE(__CTRACE_REFRESH, " 0x%x \n", curscr->lines[i]->hash);
    999 		for (j = 0; j < curscr->maxx; j++)
   1000 			__CTRACE(__CTRACE_REFRESH, "%c",
   1001 			    curscr->lines[i]->line[j].ch);
   1002 		__CTRACE(__CTRACE_REFRESH, "\n");
   1003 		__CTRACE(__CTRACE_REFRESH, " attr:");
   1004 		for (j = 0; j < curscr->maxx; j++)
   1005 			__CTRACE(__CTRACE_REFRESH, " %x",
   1006 			    curscr->lines[i]->line[j].attr);
   1007 		__CTRACE(__CTRACE_REFRESH, "\n");
   1008 		__CTRACE(__CTRACE_REFRESH, "W: %d:", i);
   1009 		__CTRACE(__CTRACE_REFRESH, " 0x%x \n",
   1010 		    __virtscr->lines[i]->hash);
   1011 		__CTRACE(__CTRACE_REFRESH, " 0x%x ",
   1012 		    __virtscr->lines[i]->flags);
   1013 		for (j = 0; j < __virtscr->maxx; j++)
   1014 			__CTRACE(__CTRACE_REFRESH, "%c",
   1015 			    __virtscr->lines[i]->line[j].ch);
   1016 		__CTRACE(__CTRACE_REFRESH, "\n");
   1017 		__CTRACE(__CTRACE_REFRESH, " attr:");
   1018 		for (j = 0; j < __virtscr->maxx; j++)
   1019 			__CTRACE(__CTRACE_REFRESH, " %x",
   1020 			    __virtscr->lines[i]->line[j].attr);
   1021 		__CTRACE(__CTRACE_REFRESH, "\n");
   1022 	}
   1023 #endif
   1024 
   1025 	if (buf[0].ch != ' ') {
   1026 		for (i = 0; i < BLANKSIZE; i++) {
   1027 			buf[i].ch = ' ';
   1028 			buf[i].attr = 0;
   1029 		}
   1030 	}
   1031 
   1032 	if (__virtscr->maxx != last_hash_len) {
   1033 		blank_hash = 0;
   1034 		for (i = __virtscr->maxx; i > BLANKSIZE; i -= BLANKSIZE) {
   1035 			blank_hash = __hash_more(buf, sizeof(buf), blank_hash);
   1036 		}
   1037 		blank_hash = __hash_more((char *)(void *)buf,
   1038 		    i * sizeof(buf[0]), blank_hash);
   1039 		/* cache result in static data - screen width doesn't change often */
   1040 		last_hash_len = __virtscr->maxx;
   1041 		last_hash = blank_hash;
   1042 	} else
   1043 		blank_hash = last_hash;
   1044 
   1045 	/*
   1046 	 * Perform the rotation to maintain the consistency of curscr.
   1047 	 * This is hairy since we are doing an *in place* rotation.
   1048 	 * Invariants of the loop:
   1049 	 * - I is the index of the current line.
   1050 	 * - Target is the index of the target of line i.
   1051 	 * - Tmp1 points to current line (i).
   1052 	 * - Tmp2 and points to target line (target);
   1053 	 * - Cur_period is the index of the end of the current period.
   1054 	 *   (see below).
   1055 	 *
   1056 	 * There are 2 major issues here that make this rotation non-trivial:
   1057 	 * 1.  Scrolling in a scrolling region bounded by the top
   1058 	 *     and bottom regions determined (whose size is sc_region).
   1059 	 * 2.  As a result of the use of the mod function, there may be a
   1060 	 *     period introduced, i.e., 2 maps to 4, 4 to 6, n-2 to 0, and
   1061 	 *     0 to 2, which then causes all odd lines not to be rotated.
   1062 	 *     To remedy this, an index of the end ( = beginning) of the
   1063 	 *     current 'period' is kept, cur_period, and when it is reached,
   1064 	 *     the next period is started from cur_period + 1 which is
   1065 	 *     guaranteed not to have been reached since that would mean that
   1066 	 *     all records would have been reached. (think about it...).
   1067 	 *
   1068 	 * Lines in the rotation can have 3 attributes which are marked on the
   1069 	 * line so that curscr is consistent with the visual screen.
   1070 	 * 1.  Not dirty -- lines inside the scrolled block, top region or
   1071 	 *                  bottom region.
   1072 	 * 2.  Blank lines -- lines in the differential of the scrolling
   1073 	 *		      region adjacent to top and bot regions
   1074 	 *                    depending on scrolling direction.
   1075 	 * 3.  Dirty line -- all other lines are marked dirty.
   1076 	 */
   1077 	sc_region = bot - top + 1;
   1078 	i = top;
   1079 	tmp1 = curscr->lines[top];
   1080 	cur_period = top;
   1081 	for (j = top; j <= bot; j++) {
   1082 		target = (i - top + n + sc_region) % sc_region + top;
   1083 		tmp2 = curscr->lines[target];
   1084 		curscr->lines[target] = tmp1;
   1085 		/* Mark block as clean and blank out scrolled lines. */
   1086 		clp = curscr->lines[target];
   1087 #ifdef DEBUG
   1088 		__CTRACE(__CTRACE_REFRESH,
   1089 		    "quickch: n=%d startw=%d curw=%d i = %d target=%d ",
   1090 		    n, startw, curw, i, target);
   1091 #endif
   1092 		if ((target >= startw && target < curw) || target < top
   1093 		    || target > bot) {
   1094 #ifdef DEBUG
   1095 			__CTRACE(__CTRACE_REFRESH, " notdirty\n");
   1096 #endif
   1097 			__virtscr->lines[target]->flags &= ~__ISDIRTY;
   1098 		} else
   1099 			if ((n > 0 && target >= top && target < top + n) ||
   1100 			    (n < 0 && target <= bot && target > bot + n)) {
   1101 				if (clp->hash != blank_hash || memcmp(clp->line,
   1102 				    clp->line + 1, (__virtscr->maxx - 1) *
   1103 				    __LDATASIZE) || memcmp(clp->line, buf,
   1104 				    __LDATASIZE)) {
   1105 					for (i = __virtscr->maxx; i > BLANKSIZE;
   1106 					    i -= BLANKSIZE) {
   1107 						(void)memcpy(clp->line + i -
   1108 						    BLANKSIZE, buf, sizeof(buf));
   1109 					}
   1110 					(void)memcpy(clp->line , buf, i *
   1111 					    sizeof(buf[0]));
   1112 #ifdef DEBUG
   1113 					__CTRACE(__CTRACE_REFRESH,
   1114 					    " blanked out: dirty\n");
   1115 #endif
   1116 					clp->hash = blank_hash;
   1117 					__touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
   1118 				} else {
   1119 #ifdef DEBUG
   1120 					__CTRACE(__CTRACE_REFRESH,
   1121 					    " -- blank line already: dirty\n");
   1122 #endif
   1123 					__touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
   1124 				}
   1125 			} else {
   1126 #ifdef DEBUG
   1127 				__CTRACE(__CTRACE_REFRESH, " -- dirty\n");
   1128 #endif
   1129 				__touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
   1130 			}
   1131 		if (target == cur_period) {
   1132 			i = target + 1;
   1133 			tmp1 = curscr->lines[i];
   1134 			cur_period = i;
   1135 		} else {
   1136 			tmp1 = tmp2;
   1137 			i = target;
   1138 		}
   1139 	}
   1140 #ifdef DEBUG
   1141 	__CTRACE(__CTRACE_REFRESH, "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
   1142 	for (i = 0; i < curscr->maxy; i++) {
   1143 		__CTRACE(__CTRACE_REFRESH, "C: %d:", i);
   1144 		for (j = 0; j < curscr->maxx; j++)
   1145 			__CTRACE(__CTRACE_REFRESH, "%c",
   1146 			    curscr->lines[i]->line[j].ch);
   1147 		__CTRACE(__CTRACE_REFRESH, "\n");
   1148 		__CTRACE(__CTRACE_REFRESH, "W: %d:", i);
   1149 		for (j = 0; j < __virtscr->maxx; j++)
   1150 			__CTRACE(__CTRACE_REFRESH, "%c",
   1151 			    __virtscr->lines[i]->line[j].ch);
   1152 		__CTRACE(__CTRACE_REFRESH, "\n");
   1153 	}
   1154 #endif
   1155 	if (n != 0)
   1156 		scrolln(starts, startw, curs, bot, top);
   1157 }
   1158 
   1159 /*
   1160  * scrolln --
   1161  *	Scroll n lines, where n is starts - startw.
   1162  */
   1163 static void /* ARGSUSED */
   1164 scrolln(starts, startw, curs, bot, top)
   1165 	int	starts, startw, curs, bot, top;
   1166 {
   1167 	int	i, oy, ox, n;
   1168 
   1169 	oy = curscr->cury;
   1170 	ox = curscr->curx;
   1171 	n = starts - startw;
   1172 
   1173 	/*
   1174 	 * XXX
   1175 	 * The initial tests that set __noqch don't let us reach here unless
   1176 	 * we have either cs + ho + SF/sf/SR/sr, or AL + DL.  SF/sf and SR/sr
   1177 	 * scrolling can only shift the entire scrolling region, not just a
   1178 	 * part of it, which means that the quickch() routine is going to be
   1179 	 * sadly disappointed in us if we don't have cs as well.
   1180 	 *
   1181 	 * If cs, ho and SF/sf are set, can use the scrolling region.  Because
   1182 	 * the cursor position after cs is undefined, we need ho which gives us
   1183 	 * the ability to move to somewhere without knowledge of the current
   1184 	 * location of the cursor.  Still call __mvcur() anyway, to update its
   1185 	 * idea of where the cursor is.
   1186 	 *
   1187 	 * When the scrolling region has been set, the cursor has to be at the
   1188 	 * last line of the region to make the scroll happen.
   1189 	 *
   1190 	 * Doing SF/SR or AL/DL appears faster on the screen than either sf/sr
   1191 	 * or AL/DL, and, some terminals have AL/DL, sf/sr, and cs, but not
   1192 	 * SF/SR.  So, if we're scrolling almost all of the screen, try and use
   1193 	 * AL/DL, otherwise use the scrolling region.  The "almost all" is a
   1194 	 * shameless hack for vi.
   1195 	 */
   1196 	if (n > 0) {
   1197 		if (__tc_cs != NULL && __tc_ho != NULL && (__tc_SF != NULL ||
   1198 		    ((__tc_AL == NULL || __tc_DL == NULL ||
   1199 		    top > 3 || bot + 3 < __virtscr->maxy) &&
   1200 		    __tc_sf != NULL))) {
   1201 			tputs(__tscroll(__tc_cs, top, bot + 1), 0, __cputchar);
   1202 			__mvcur(oy, ox, 0, 0, 1);
   1203 			tputs(__tc_ho, 0, __cputchar);
   1204 			__mvcur(0, 0, bot, 0, 1);
   1205 			if (__tc_SF != NULL)
   1206 				tputs(__tscroll(__tc_SF, n, 0), 0, __cputchar);
   1207 			else
   1208 				for (i = 0; i < n; i++)
   1209 					tputs(__tc_sf, 0, __cputchar);
   1210 			tputs(__tscroll(__tc_cs, 0, (int) __virtscr->maxy), 0,
   1211 			    __cputchar);
   1212 			__mvcur(bot, 0, 0, 0, 1);
   1213 			tputs(__tc_ho, 0, __cputchar);
   1214 			__mvcur(0, 0, oy, ox, 1);
   1215 			return;
   1216 		}
   1217 
   1218 		/* Scroll up the block. */
   1219 		if (__tc_SF != NULL && top == 0) {
   1220 			__mvcur(oy, ox, bot, 0, 1);
   1221 			tputs(__tscroll(__tc_SF, n, 0), 0, __cputchar);
   1222 		} else
   1223 			if (__tc_DL != NULL) {
   1224 				__mvcur(oy, ox, top, 0, 1);
   1225 				tputs(__tscroll(__tc_DL, n, 0), 0, __cputchar);
   1226 			} else
   1227 				if (__tc_dl != NULL) {
   1228 					__mvcur(oy, ox, top, 0, 1);
   1229 					for (i = 0; i < n; i++)
   1230 						tputs(__tc_dl, 0, __cputchar);
   1231 				} else
   1232 					if (__tc_sf != NULL && top == 0) {
   1233 						__mvcur(oy, ox, bot, 0, 1);
   1234 						for (i = 0; i < n; i++)
   1235 							tputs(__tc_sf, 0,
   1236 							    __cputchar);
   1237 					} else
   1238 						abort();
   1239 
   1240 		/* Push down the bottom region. */
   1241 		__mvcur(top, 0, bot - n + 1, 0, 1);
   1242 		if (__tc_AL != NULL)
   1243 			tputs(__tscroll(__tc_AL, n, 0), 0, __cputchar);
   1244 		else
   1245 			if (__tc_al != NULL)
   1246 				for (i = 0; i < n; i++)
   1247 					tputs(__tc_al, 0, __cputchar);
   1248 			else
   1249 				abort();
   1250 		__mvcur(bot - n + 1, 0, oy, ox, 1);
   1251 	} else {
   1252 		/*
   1253 		 * !!!
   1254 		 * n < 0
   1255 		 *
   1256 		 * If cs, ho and SR/sr are set, can use the scrolling region.
   1257 		 * See the above comments for details.
   1258 		 */
   1259 		if (__tc_cs != NULL && __tc_ho != NULL && (__tc_SR != NULL ||
   1260 		    ((__tc_AL == NULL || __tc_DL == NULL || top > 3 ||
   1261 		    bot + 3 < __virtscr->maxy) && __tc_sr != NULL))) {
   1262 			tputs(__tscroll(__tc_cs, top, bot + 1), 0, __cputchar);
   1263 			__mvcur(oy, ox, 0, 0, 1);
   1264 			tputs(__tc_ho, 0, __cputchar);
   1265 			__mvcur(0, 0, top, 0, 1);
   1266 
   1267 			if (__tc_SR != NULL)
   1268 				tputs(__tscroll(__tc_SR, -n, 0), 0, __cputchar);
   1269 			else
   1270 				for (i = n; i < 0; i++)
   1271 					tputs(__tc_sr, 0, __cputchar);
   1272 			tputs(__tscroll(__tc_cs, 0, (int) __virtscr->maxy), 0,
   1273 			    __cputchar);
   1274 			__mvcur(top, 0, 0, 0, 1);
   1275 			tputs(__tc_ho, 0, __cputchar);
   1276 			__mvcur(0, 0, oy, ox, 1);
   1277 			return;
   1278 		}
   1279 
   1280 		/* Preserve the bottom lines. */
   1281 		__mvcur(oy, ox, bot + n + 1, 0, 1);
   1282 		if (__tc_SR != NULL && bot == __virtscr->maxy)
   1283 			tputs(__tscroll(__tc_SR, -n, 0), 0, __cputchar);
   1284 		else
   1285 			if (__tc_DL != NULL)
   1286 				tputs(__tscroll(__tc_DL, -n, 0), 0, __cputchar);
   1287 			else
   1288 				if (__tc_dl != NULL)
   1289 					for (i = n; i < 0; i++)
   1290 						tputs(__tc_dl, 0, __cputchar);
   1291 				else
   1292 					if (__tc_sr != NULL &&
   1293 					    bot == __virtscr->maxy)
   1294 						for (i = n; i < 0; i++)
   1295 							tputs(__tc_sr, 0,
   1296 							    __cputchar);
   1297 					else
   1298 						abort();
   1299 
   1300 		/* Scroll the block down. */
   1301 		__mvcur(bot + n + 1, 0, top, 0, 1);
   1302 		if (__tc_AL != NULL)
   1303 			tputs(__tscroll(__tc_AL, -n, 0), 0, __cputchar);
   1304 		else
   1305 			if (__tc_al != NULL)
   1306 				for (i = n; i < 0; i++)
   1307 					tputs(__tc_al, 0, __cputchar);
   1308 			else
   1309 				abort();
   1310 		__mvcur(top, 0, oy, ox, 1);
   1311 	}
   1312 }
   1313 
   1314 /*
   1315  * __unsetattr --
   1316  *	Unset attributes on curscr.  Leave standout, attribute and colour
   1317  *	modes if necessary (!ms).  Always leave altcharset (xterm at least
   1318  *	ignores a cursor move if we don't).
   1319  */
   1320 void /* ARGSUSED */
   1321 __unsetattr(int checkms)
   1322 {
   1323 	int	isms;
   1324 
   1325 	if (checkms)
   1326 		if (!__tc_ms) {
   1327 			isms = 1;
   1328 		} else {
   1329 			isms = 0;
   1330 		}
   1331 	else
   1332 		isms = 1;
   1333 #ifdef DEBUG
   1334 	__CTRACE(__CTRACE_REFRESH,
   1335 	    "__unsetattr: checkms = %d, ms = %s, wattr = %08x\n",
   1336 	    checkms, __tc_ms ? "TRUE" : "FALSE", curscr->wattr);
   1337 #endif
   1338 
   1339 	/*
   1340          * Don't leave the screen in standout mode (check against ms).  Check
   1341 	 * to see if we also turn off underscore, attributes and colour.
   1342 	 */
   1343 	if (curscr->wattr & __STANDOUT && isms) {
   1344 		tputs(__tc_se, 0, __cputchar);
   1345 		curscr->wattr &= __mask_se;
   1346 	}
   1347 	/*
   1348 	 * Don't leave the screen in underscore mode (check against ms).
   1349 	 * Check to see if we also turn off attributes.  Assume that we
   1350 	 * also turn off colour.
   1351 	 */
   1352 	if (curscr->wattr & __UNDERSCORE && isms) {
   1353 		tputs(__tc_ue, 0, __cputchar);
   1354 		curscr->wattr &= __mask_ue;
   1355 	}
   1356 	/*
   1357 	 * Don't leave the screen with attributes set (check against ms).
   1358 	 * Assume that also turn off colour.
   1359 	 */
   1360 	if (curscr->wattr & __TERMATTR && isms) {
   1361 		tputs(__tc_me, 0, __cputchar);
   1362 		curscr->wattr &= __mask_me;
   1363 	}
   1364 	/* Don't leave the screen with altcharset set (don't check ms). */
   1365 	if (curscr->wattr & __ALTCHARSET) {
   1366 		tputs(__tc_ae, 0, __cputchar);
   1367 		curscr->wattr &= ~__ALTCHARSET;
   1368 	}
   1369 	/* Don't leave the screen with colour set (check against ms). */
   1370 	if (__using_color && isms)
   1371 		__unset_color(curscr);
   1372 }
   1373