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