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