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