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