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