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