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