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