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