xref: /csrg-svn/lib/libcurses/refresh.c (revision 58049)
12261Sarnold /*
234677Sbostic  * Copyright (c) 1981 Regents of the University of California.
334677Sbostic  * All rights reserved.
434677Sbostic  *
542657Sbostic  * %sccs.include.redist.c%
622791Smckusick  */
722791Smckusick 
822791Smckusick #ifndef lint
9*58049Selan static char sccsid[] = "@(#)refresh.c	5.34 (Berkeley) 02/18/93";
1034677Sbostic #endif /* not lint */
1122791Smckusick 
1255976Sbostic #include <curses.h>
1355986Sbostic #include <string.h>
142261Sarnold 
1555976Sbostic static int curwin;
1655976Sbostic static short ly, lx;
172261Sarnold 
1855976Sbostic WINDOW *_win;
192261Sarnold 
2055976Sbostic static void	domvcur __P((int, int, int, int));
2155976Sbostic static int	makech __P((WINDOW *, int));
2256302Selan static void	quickch __P((WINDOW *));
2356559Selan static void	scrolln __P((WINDOW *, int, int, int, int, int));
2456648Selan 
2555976Sbostic /*
2655976Sbostic  * wrefresh --
2755976Sbostic  *	Make the current screen look like "win" over the area coverd by
2855976Sbostic  *	win.
2955976Sbostic  */
3055976Sbostic int
312261Sarnold wrefresh(win)
3255976Sbostic 	register WINDOW *win;
332261Sarnold {
3456648Selan 	register __LINE *wlp;
3555976Sbostic 	register int retval;
3655976Sbostic 	register short wy;
3758019Selan 	int dnum;
3858019Selan 
3955976Sbostic 	/* Initialize loop parameters. */
4056238Selan 	ly = curscr->cury;
4156238Selan 	lx = curscr->curx;
422287Sarnold 	wy = 0;
432287Sarnold 	_win = win;
442287Sarnold 	curwin = (win == curscr);
452287Sarnold 
4656378Selan 	if (!curwin)
4756378Selan 		for (wy = 0; wy < win->maxy; wy++) {
4856378Selan 			wlp = win->lines[wy];
4956378Selan 			if (wlp->flags & __ISDIRTY)
5056648Selan 				wlp->hash =
5156648Selan 				   __hash(wlp->line, win->maxx * __LDATASIZE);
5256378Selan 		}
5356378Selan 
5456238Selan 	if (win->flags & __CLEAROK || curscr->flags & __CLEAROK || curwin) {
5556238Selan 		if ((win->flags & __FULLWIN) || curscr->flags & __CLEAROK) {
5655976Sbostic 			tputs(CL, 0, __cputchar);
5712358Sarnold 			ly = 0;
5812358Sarnold 			lx = 0;
5912358Sarnold 			if (!curwin) {
6056238Selan 				curscr->flags &= ~__CLEAROK;
6156238Selan 				curscr->cury = 0;
6256238Selan 				curscr->curx = 0;
632287Sarnold 				werase(curscr);
6412358Sarnold 			}
6556651Selan 			__touchwin(win);
662261Sarnold 		}
6756238Selan 		win->flags &= ~__CLEAROK;
682261Sarnold 	}
692261Sarnold 	if (!CA) {
7056238Selan 		if (win->curx != 0)
7155976Sbostic 			putchar('\n');
722287Sarnold 		if (!curwin)
732287Sarnold 			werase(curscr);
742261Sarnold 	}
7555976Sbostic #ifdef DEBUG
7655976Sbostic 	__TRACE("wrefresh: (%0.2o): curwin = %d\n", win, curwin);
7755976Sbostic 	__TRACE("wrefresh: \tfirstch\tlastch\n");
7855976Sbostic #endif
7956302Selan 
8056302Selan #ifndef NOQCH
8158019Selan 	if ((win->flags & __FULLWIN) && !curwin) {
8258019Selan 		/*
8358019Selan 		 * Invoke quickch() only if more than a quarter of the lines
8458019Selan 		 * in the window are dirty.
8558019Selan 		 */
8658019Selan 		for (wy = 0, dnum = 0; wy < win->maxy; wy++)
8758019Selan 			if (win->lines[wy]->flags & (__ISDIRTY | __FORCEPAINT))
8858019Selan 				dnum++;
8958019Selan 		if (!__noqch && dnum > (int) win->maxy / 4)
9058019Selan 			quickch(win);
9158019Selan 	}
9256302Selan #endif
9356238Selan 	for (wy = 0; wy < win->maxy; wy++) {
9455976Sbostic #ifdef DEBUG
9555976Sbostic 		__TRACE("%d\t%d\t%d\n",
9656715Selan 		    wy, *win->lines[wy]->firstchp, *win->lines[wy]->lastchp);
9755976Sbostic #endif
9856378Selan 		if (!curwin)
9956378Selan 			curscr->lines[wy]->hash = win->lines[wy]->hash;
10058019Selan 		if (win->lines[wy]->flags & (__ISDIRTY | __FORCEPAINT))
10157472Sbostic 			if (makech(win, wy) == ERR)
10257472Sbostic 				return (ERR);
10319893Sbloom 			else {
10456715Selan 				if (*win->lines[wy]->firstchp >= win->ch_off)
10556715Selan 					*win->lines[wy]->firstchp = win->maxx +
10656238Selan 					    win->ch_off;
10756715Selan 				if (*win->lines[wy]->lastchp < win->maxx +
10856238Selan 				    win->ch_off)
10956715Selan 					*win->lines[wy]->lastchp = win->ch_off;
11056715Selan 				if (*win->lines[wy]->lastchp <
11158034Selan 				    *win->lines[wy]->firstchp) {
11258034Selan #ifdef DEBUG
11358034Selan 					__TRACE("wrefresh: line %d notdirty \n", wy);
11458034Selan #endif
11556238Selan 					win->lines[wy]->flags &= ~__ISDIRTY;
11658034Selan 				}
11719893Sbloom 			}
11855976Sbostic #ifdef DEBUG
11956715Selan 		__TRACE("\t%d\t%d\n", *win->lines[wy]->firstchp,
12056715Selan 			*win->lines[wy]->lastchp);
12155976Sbostic #endif
1222261Sarnold 	}
12356238Selan 
12456302Selan #ifdef DEBUG
12556238Selan 	__TRACE("refresh: ly=%d, lx=%d\n", ly, lx);
12656302Selan #endif
12756472Selan 
12812358Sarnold 	if (win == curscr)
12956238Selan 		domvcur(ly, lx, win->cury, win->curx);
13019893Sbloom 	else {
13156238Selan 		if (win->flags & __LEAVEOK) {
13256238Selan 			curscr->cury = ly;
13356238Selan 			curscr->curx = lx;
13456238Selan 			ly -= win->begy;
13556238Selan 			lx -= win->begx;
13656238Selan 			if (ly >= 0 && ly < win->maxy && lx >= 0 &&
13756238Selan 			    lx < win->maxx) {
13856238Selan 				win->cury = ly;
13956238Selan 				win->curx = lx;
14055976Sbostic 			} else
14156238Selan 				win->cury = win->curx = 0;
14255976Sbostic 		} else {
14356238Selan 			domvcur(ly, lx, win->cury + win->begy,
14456238Selan 			    win->curx + win->begx);
14556238Selan 			curscr->cury = win->cury + win->begy;
14656238Selan 			curscr->curx = win->curx + win->begx;
14719893Sbloom 		}
1482261Sarnold 	}
14957472Sbostic 	retval = OK;
15055986Sbostic 
1512261Sarnold 	_win = NULL;
15255976Sbostic 	(void)fflush(stdout);
15355976Sbostic 	return (retval);
1542261Sarnold }
1552261Sarnold 
1562261Sarnold /*
15755976Sbostic  * makech --
15855976Sbostic  *	Make a change on the screen.
1592261Sarnold  */
16055976Sbostic static int
1612261Sarnold makech(win, wy)
16255976Sbostic 	register WINDOW *win;
16355976Sbostic 	int wy;
1642261Sarnold {
165*58049Selan 	register int nlsp;		/* Last space in lines. */
16655976Sbostic 	register short wx, lch, y;
16756648Selan 	register __LDATA *nsp, *csp, *cp;
16856651Selan 	u_int force;
16956648Selan 	char *ce;
17056648Selan 	__LDATA blank = {' ', 0};
17156648Selan 
17256472Selan 	/* Is the cursor still on the end of the last line? */
17356472Selan 	if (wy > 0 && win->lines[wy - 1]->flags & __ISPASTEOL) {
17456551Selan 		win->lines[wy - 1]->flags &= ~__ISPASTEOL;
17556551Selan 		domvcur(ly, lx, ly + 1, 0);
17656551Selan 		ly++;
17756551Selan 		lx = 0;
17856551Selan 	}
17956238Selan 	if (!(win->lines[wy]->flags & __ISDIRTY))
18057472Sbostic 		return (OK);
18156715Selan 	wx = *win->lines[wy]->firstchp - win->ch_off;
18256238Selan 	if (wx >= win->maxx)
18357472Sbostic 		return (OK);
18419893Sbloom 	else if (wx < 0)
18519893Sbloom 		wx = 0;
18656715Selan 	lch = *win->lines[wy]->lastchp - win->ch_off;
18719893Sbloom 	if (lch < 0)
18857472Sbostic 		return (OK);
18956238Selan 	else if (lch >= win->maxx)
19056238Selan 		lch = win->maxx - 1;
19156238Selan 	y = wy + win->begy;
19219893Sbloom 
1932287Sarnold 	if (curwin)
19456648Selan 		csp = &blank;
1952287Sarnold 	else
19656238Selan 		csp = &curscr->lines[wy + win->begy]->line[wx + win->begx];
19719893Sbloom 
19856238Selan 	nsp = &win->lines[wy]->line[wx];
19956651Selan 	force = win->lines[wy]->flags & __FORCEPAINT;
20056651Selan 	win->lines[wy]->flags &= ~__FORCEPAINT;
2012287Sarnold 	if (CE && !curwin) {
20256648Selan 		for (cp = &win->lines[wy]->line[win->maxx - 1];
20356648Selan 		     cp->ch == ' ' && cp->attr == 0; cp--)
20456648Selan 			if (cp <= win->lines[wy]->line)
2052261Sarnold 				break;
20656648Selan 		nlsp = cp - win->lines[wy]->line;
2072261Sarnold 	}
2082287Sarnold 	if (!curwin)
2092287Sarnold 		ce = CE;
2102287Sarnold 	else
2112287Sarnold 		ce = NULL;
21219893Sbloom 
21356651Selan 	if (force) {
21456651Selan 		if (CM)
21556651Selan 			tputs(tgoto(CM, lx, ly), 0, __cputchar);
21656651Selan 		else {
21756651Selan 			tputs(HO, 0, __cputchar);
21856651Selan 			mvcur(0, 0, ly, lx);
21956651Selan 		}
22056651Selan 	}
2212261Sarnold 	while (wx <= lch) {
22257956Selan 		if (!force && memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
22355986Sbostic 			if (wx <= lch) {
22457956Selan 				while (memcmp(nsp, csp, sizeof(__LDATA)) == 0 &&
22556648Selan 			            wx <= lch) {
22656596Selan 					    nsp++;
22756648Selan 					    if (!curwin)
22856648Selan 						    csp++;
22956648Selan 					    ++wx;
23056648Selan 				    }
23155986Sbostic 				continue;
23255986Sbostic 			}
23355986Sbostic 			break;
23455986Sbostic 		}
23556238Selan 		domvcur(ly, lx, y, wx + win->begx);
23656378Selan 
23755976Sbostic #ifdef DEBUG
23856651Selan 		__TRACE("makech: 1: wx = %d, ly= %d, lx = %d, newy = %d, newx = %d, force =%d\n",
23956651Selan 		    wx, ly, lx, y, wx + win->begx, force);
24055976Sbostic #endif
24155986Sbostic 		ly = y;
24256238Selan 		lx = wx + win->begx;
24357956Selan 		while ((force || memcmp(nsp, csp, sizeof(__LDATA)) != 0)
24456651Selan 		    && wx <= lch) {
24555986Sbostic 
24655986Sbostic 			/* Enter/exit standout mode as appropriate. */
24756648Selan 			if (SO && (nsp->attr & __STANDOUT) !=
24856378Selan 			    (curscr->flags & __WSTANDOUT)) {
24956648Selan 				if (nsp->attr & __STANDOUT) {
25055986Sbostic 					tputs(SO, 0, __cputchar);
25156238Selan 					curscr->flags |= __WSTANDOUT;
25255986Sbostic 				} else {
25355986Sbostic 					tputs(SE, 0, __cputchar);
25456238Selan 					curscr->flags &= ~__WSTANDOUT;
2552261Sarnold 				}
25655986Sbostic 			}
25755986Sbostic 
25855986Sbostic 			wx++;
25956472Selan 			if (wx >= win->maxx && wy == win->maxy - 1 && !curwin)
26056238Selan 				if (win->flags & __SCROLLOK) {
26156238Selan 					if (curscr->flags & __WSTANDOUT
26256238Selan 					    && win->flags & __ENDLINE)
26355986Sbostic 						if (!MS) {
26455986Sbostic 							tputs(SE, 0,
26555986Sbostic 							    __cputchar);
26656238Selan 							curscr->flags &=
26756238Selan 							    ~__WSTANDOUT;
26855976Sbostic 						}
26956596Selan 					if (!curwin) {
27056648Selan 						csp->attr = nsp->attr;
27156648Selan 						putchar(csp->ch = nsp->ch);
27256596Selan 					} else
27356648Selan 						putchar(nsp->ch);
27458040Selan 
27556599Selan 					if (wx + win->begx < curscr->maxx) {
27656599Selan 						domvcur(ly, wx + win->begx,
27756599Selan 						    win->begy + win->maxy - 1,
27856599Selan 						    win->begx + win->maxx - 1);
27956599Selan 					}
28056378Selan 					ly = win->begy + win->maxy - 1;
28156378Selan 					lx = win->begx + win->maxx - 1;
28257472Sbostic 					return (OK);
28355986Sbostic 				} else
28456238Selan 					if (win->flags & __SCROLLWIN) {
28555986Sbostic 						lx = --wx;
28657472Sbostic 						return (ERR);
28755986Sbostic 					}
28856596Selan 			if (!curwin) {
28956648Selan 				csp->attr = nsp->attr;
29056648Selan 				putchar(csp->ch = nsp->ch);
29156648Selan 				csp++;
29256648Selan 		       	} else
29356648Selan 				putchar(nsp->ch);
29456472Selan 
29555976Sbostic #ifdef DEBUG
29656648Selan 			__TRACE("makech: putchar(%c)\n", nsp->ch & 0177);
29755976Sbostic #endif
29856648Selan 			if (UC && (nsp->attr & __STANDOUT)) {
29955986Sbostic 				putchar('\b');
30055986Sbostic 				tputs(UC, 0, __cputchar);
3012261Sarnold 			}
30255986Sbostic 			nsp++;
30355986Sbostic 		}
30455976Sbostic #ifdef DEBUG
30555986Sbostic 		__TRACE("makech: 2: wx = %d, lx = %d\n", wx, lx);
30655976Sbostic #endif
30756238Selan 		if (lx == wx + win->begx)	/* If no change. */
30855986Sbostic 			break;
30956238Selan 		lx = wx + win->begx;
31055986Sbostic 		if (lx >= COLS && AM) {
31158040Selan 			if (wy != LINES)
31258040Selan 				win->lines[wy]->flags |= __ISPASTEOL;
31358040Selan 			lx = COLS - 1;
31456596Selan 		} else if (wx >= win->maxx) {
31556596Selan 			if (wy != win->maxy)
31656596Selan 				win->lines[wy]->flags |= __ISPASTEOL;
31756596Selan 			domvcur(ly, lx, ly, win->maxx + win->begx - 1);
31856596Selan 			lx = win->maxx + win->begx - 1;
31955986Sbostic 		}
32056596Selan 
32155976Sbostic #ifdef DEBUG
32255976Sbostic 		__TRACE("makech: 3: wx = %d, lx = %d\n", wx, lx);
32355976Sbostic #endif
3242261Sarnold 	}
32557472Sbostic 	return (OK);
32611736Sarnold }
32711736Sarnold 
32811736Sarnold /*
32955976Sbostic  * domvcur --
33055976Sbostic  *	Do a mvcur, leaving standout mode if necessary.
33111736Sarnold  */
33255976Sbostic static void
33311736Sarnold domvcur(oy, ox, ny, nx)
33455976Sbostic 	int oy, ox, ny, nx;
33555976Sbostic {
33656238Selan 	if (curscr->flags & __WSTANDOUT && !MS) {
33755976Sbostic 		tputs(SE, 0, __cputchar);
33856238Selan 		curscr->flags &= ~__WSTANDOUT;
3392261Sarnold 	}
34056596Selan 
34111736Sarnold 	mvcur(oy, ox, ny, nx);
3422261Sarnold }
34356302Selan 
34456302Selan /*
34556302Selan  * Quickch() attempts to detect a pattern in the change of the window
34656552Selan  * in order to optimize the change, e.g., scroll n lines as opposed to
34756302Selan  * repainting the screen line by line.
34856302Selan  */
34956302Selan 
35056715Selan 
35156302Selan static void
35256302Selan quickch(win)
35356302Selan 	WINDOW *win;
35456302Selan {
35558019Selan #define THRESH		(int) win->maxy / 4
35656302Selan 
35756648Selan 	register __LINE *clp, *tmp1, *tmp2;
35856378Selan 	register int bsize, curs, curw, starts, startw, i, j;
35956652Selan 	int n, target, cur_period, bot, top, sc_region;
36056648Selan 	__LDATA buf[1024];
36156378Selan 	u_int blank_hash;
36256302Selan 
36358019Selan 	/*
36458019Selan 	 * Find how many lines from the top of the screen are unchanged.
36558019Selan 	 */
36658034Selan 	for (top = 0; top < win->maxy; top++)
36758019Selan 		if (win->lines[top]->flags & __FORCEPAINT ||
36858019Selan 		    win->lines[top]->hash != curscr->lines[top]->hash
36958019Selan 		    || memcmp(win->lines[top]->line,
37058019Selan 		    curscr->lines[top]->line,
37158019Selan 		    win->maxx * __LDATASIZE) != 0)
37258019Selan 			break;
37358034Selan 		else
37458034Selan 			win->lines[top]->flags &= ~__ISDIRTY;
37558019Selan        /*
37658019Selan 	* Find how many lines from bottom of screen are unchanged.
37758019Selan 	*/
37858019Selan 	for (bot = win->maxy - 1; bot >= 0; bot--)
37958019Selan 		if (win->lines[bot]->flags & __FORCEPAINT ||
38058019Selan 		    win->lines[bot]->hash != curscr->lines[bot]->hash
38158019Selan 		    || memcmp(win->lines[bot]->line,
38258019Selan 		    curscr->lines[bot]->line,
38358019Selan 		    win->maxx * __LDATASIZE) != 0)
38458019Selan 			break;
38558034Selan 		else
38658034Selan 			win->lines[bot]->flags &= ~__ISDIRTY;
38758019Selan 
38856552Selan 	/*
38956552Selan 	 * Search for the largest block of text not changed.
39056652Selan 	 * Invariants of the loop:
39156652Selan 	 * - Startw is the index of the beginning of the examined block in win.
39256652Selan          * - Starts is the index of the beginning of the examined block in
39356652Selan 	 *    curscr.
39456652Selan 	 * - Curs is the index of one past the end of the exmined block in win.
39556652Selan 	 * - Curw is the index of one past the end of the exmined block in
39656652Selan 	 *   curscr.
39756652Selan 	 * - bsize is the current size of the examined block.
39856552Selan          */
39958019Selan 	for (bsize = bot - top; bsize >= THRESH; bsize--) {
40058019Selan 		for (startw = top; startw <= bot - bsize; startw++)
40158019Selan 			for (starts = top; starts <= bot - bsize;
40256302Selan 			     starts++) {
40356302Selan 				for (curw = startw, curs = starts;
40456302Selan 				     curs < starts + bsize; curw++, curs++)
40556651Selan 					if (win->lines[curw]->flags &
40656651Selan 					    __FORCEPAINT ||
40756651Selan 					    (win->lines[curw]->hash !=
40856552Selan 					    curscr->lines[curs]->hash ||
40957956Selan 				            memcmp(win->lines[curw]->line,
41056648Selan 					    curscr->lines[curs]->line,
41156651Selan 					    win->maxx * __LDATASIZE) != 0))
41256302Selan 						break;
41356302Selan 				if (curs == starts + bsize)
41456302Selan 					goto done;
41556302Selan 			}
41658019Selan 	}
41756302Selan  done:
41856651Selan 	/* Did not find anything */
41956651Selan 	if (bsize < THRESH)
42056302Selan 		return;
42156302Selan 
42256302Selan #ifdef DEBUG
42356559Selan 	__TRACE("quickch:bsize=%d,starts=%d,startw=%d,curw=%d,curs=%d,top=%d,bot=%d\n",
42456559Selan 		bsize, starts, startw, curw, curs, top, bot);
42556302Selan #endif
42656378Selan 
42756559Selan 	/*
42856559Selan 	 * Make sure that there is no overlap between the bottom and top
42956559Selan 	 * regions and the middle scrolled block.
43056559Selan 	 */
43156691Selan 	if (bot < curs)
43256691Selan 		bot = curs - 1;
43356691Selan 	if (top > starts)
43456691Selan 		top = starts;
43556559Selan 
43656378Selan 	n = startw - starts;
43756378Selan 
43856691Selan #ifdef DEBUG
43956691Selan 		__TRACE("#####################################\n");
44056691Selan 		for (i = 0; i < curscr->maxy; i++) {
44156691Selan 			__TRACE("C: %d:", i);
44257356Selan 			__TRACE(" 0x%x \n", curscr->lines[i]->hash);
44356691Selan 			for (j = 0; j < curscr->maxx; j++)
44456691Selan 				__TRACE("%c",
44556691Selan 			           curscr->lines[i]->line[j].ch);
44656691Selan 			__TRACE("\n");
44757356Selan 			for (j = 0; j < curscr->maxx; j++)
44857356Selan 				__TRACE("%x",
44957356Selan 			           curscr->lines[i]->line[j].attr);
45057356Selan 			__TRACE("\n");
45156691Selan 			__TRACE("W: %d:", i);
45257356Selan 			__TRACE(" 0x%x \n", win->lines[i]->hash);
45357356Selan 			__TRACE(" 0x%x ", win->lines[i]->flags);
45456691Selan 			for (j = 0; j < win->maxx; j++)
45556691Selan 				__TRACE("%c",
45656691Selan 			           win->lines[i]->line[j].ch);
45756691Selan 			__TRACE("\n");
45857356Selan 			for (j = 0; j < win->maxx; j++)
45957356Selan 				__TRACE("%x",
46057356Selan 			           win->lines[i]->line[j].attr);
46157356Selan 			__TRACE("\n");
46256691Selan 		}
46356691Selan #endif
46456651Selan 	if (n != 0)
46556705Selan 		scrolln(win, starts, startw, curs, bot, top);
46658019Selan 
46756378Selan 	/* So we don't have to call __hash() each time */
46856648Selan 	for (i = 0; i < win->maxx; i++) {
46956648Selan 		buf[i].ch = ' ';
47056648Selan 		buf[i].attr = 0;
47156648Selan 	}
47256648Selan 	blank_hash = __hash(buf, win->maxx * __LDATASIZE);
47356378Selan 
47456378Selan 	/*
47556378Selan 	 * Perform the rotation to maintain the consistency of curscr.
47656691Selan 	 * This is hairy since we are doing an *in place* rotation.
47756652Selan 	 * Invariants of the loop:
47856652Selan 	 * - I is the index of the current line.
47956652Selan 	 * - Target is the index of the target of line i.
48056652Selan 	 * - Tmp1 points to current line (i).
48156652Selan 	 * - Tmp2 and points to target line (target);
48256652Selan 	 * - Cur_period is the index of the end of the current period.
48356652Selan 	 *   (see below).
48456652Selan 	 *
48556652Selan 	 * There are 2 major issues here that make this rotation non-trivial:
48656652Selan 	 * 1.  Scrolling in a scrolling region bounded by the top
48756652Selan 	 *     and bottom regions determined (whose size is sc_region).
48856652Selan 	 * 2.  As a result of the use of the mod function, there may be a
48956652Selan 	 *     period introduced, i.e., 2 maps to 4, 4 to 6, n-2 to 0, and
49056652Selan 	 *     0 to 2, which then causes all odd lines not to be rotated.
49156652Selan 	 *     To remedy this, an index of the end ( = beginning) of the
49256652Selan 	 *     current 'period' is kept, cur_period, and when it is reached,
49356652Selan 	 *     the next period is started from cur_period + 1 which is
49456652Selan 	 *     guaranteed not to have been reached since that would mean that
49556652Selan 	 *     all records would have been reached. (think about it...).
49656652Selan 	 *
49756652Selan 	 * Lines in the rotation can have 3 attributes which are marked on the
49856652Selan 	 * line so that curscr is consistent with the visual screen.
49956705Selan 	 * 1.  Not dirty -- lines inside the scrolled block, top region or
50056652Selan 	 *                  bottom region.
50156705Selan 	 * 2.  Blank lines -- lines in the differential of the scrolling
50256705Selan 	 *		      region adjacent to top and bot regions
50356705Selan 	 *                    depending on scrolling direction.
50456652Selan 	 * 3.  Dirty line -- all other lines are marked dirty.
50556378Selan 	 */
50656648Selan 	sc_region = bot - top + 1;
50756648Selan 	i = top;
50856648Selan 	tmp1 = curscr->lines[top];
50956652Selan 	cur_period = top;
51056648Selan 	for (j = top; j <= bot; j++) {
51156648Selan 		target = (i - top + n + sc_region) % sc_region + top;
51256378Selan 		tmp2 = curscr->lines[target];
51356378Selan 		curscr->lines[target] = tmp1;
51456378Selan 		/* Mark block as clean and blank out scrolled lines. */
51556378Selan 		clp = curscr->lines[target];
51656472Selan #ifdef DEBUG
51756378Selan 		__TRACE("quickch: n=%d startw=%d curw=%d i = %d target=%d ",
51856378Selan 			n, startw, curw, i, target);
51956472Selan #endif
52056691Selan 		if ((target >= startw && target < curw) || target < top
52156691Selan 		    || target > bot) {
52256472Selan #ifdef DEBUG
52356378Selan 			__TRACE("-- notdirty");
52456472Selan #endif
52556378Selan 			win->lines[target]->flags &= ~__ISDIRTY;
52656705Selan 		} else if ((n > 0 && target >= top && target < top + n) ||
52756705Selan 		           (n < 0 && target <= bot && target > bot + n)) {
52857956Selan 			if (clp->hash != blank_hash ||  memcmp(clp->line,
52956648Selan 			    buf, win->maxx * __LDATASIZE) !=0) {
53058041Selan 				(void)memcpy(clp->line,  buf,
53156648Selan 				    win->maxx * __LDATASIZE);
53256472Selan #ifdef DEBUG
53356648Selan 				__TRACE("-- blanked out: dirty");
53456472Selan #endif
53556378Selan 				clp->hash = blank_hash;
53656652Selan 				__touchline(win, target, 0, win->maxx - 1, 0);
53756705Selan 			} else {
53856652Selan 				__touchline(win, target, 0, win->maxx - 1, 0);
53956472Selan #ifdef DEBUG
54056648Selan 				__TRACE(" -- blank line already: dirty");
54156472Selan #endif
54256705Selan 			}
54356378Selan 		} else {
54456472Selan #ifdef DEBUG
54556648Selan 			__TRACE(" -- dirty");
54656472Selan #endif
54756651Selan 			__touchline(win, target, 0, win->maxx - 1, 0);
54856378Selan 		}
54956472Selan #ifdef DEBUG
55056378Selan 		__TRACE("\n");
55156472Selan #endif
55256652Selan 		if (target == cur_period) {
55356378Selan 			i = target + 1;
55456378Selan 			tmp1 = curscr->lines[i];
55556652Selan 			cur_period = i;
55656378Selan 		} else {
55756378Selan 			tmp1 = tmp2;
55856378Selan 			i = target;
55956378Selan 		}
56056302Selan 	}
56156472Selan #ifdef DEBUG
56256648Selan 		__TRACE("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
56356648Selan 		for (i = 0; i < curscr->maxy; i++) {
56456691Selan 			__TRACE("C: %d:", i);
56556648Selan 			for (j = 0; j < curscr->maxx; j++)
56656648Selan 				__TRACE("%c",
56756648Selan 			           curscr->lines[i]->line[j].ch);
56856648Selan 			__TRACE("\n");
56956691Selan 			__TRACE("W: %d:", i);
57056691Selan 			for (j = 0; j < win->maxx; j++)
57156691Selan 				__TRACE("%c",
57256691Selan 			           win->lines[i]->line[j].ch);
57356691Selan 			__TRACE("\n");
57456648Selan 		}
57556472Selan #endif
57656302Selan }
57756302Selan 
57856652Selan /*
57956652Selan  * Scrolln performs the scroll by n lines, where n is starts - startw.
58056652Selan  */
58156302Selan static void
58256705Selan scrolln(win, starts, startw, curs, bot, top)
58356302Selan 	WINDOW *win;
58456705Selan 	int starts, startw, curs, bot, top;
58556302Selan {
58656302Selan 	int i, oy, ox, n;
58756302Selan 
58856302Selan 	oy = curscr->cury;
58956302Selan 	ox = curscr->curx;
59056302Selan 	n = starts - startw;
59156302Selan 
59256302Selan 	if (n > 0) {
59356559Selan 		mvcur(oy, ox, top, 0);
59456559Selan 		/* Scroll up the block */
59556302Selan 		if (DL)
59657475Sbostic 			tputs(__tscroll(DL, n), 0, __cputchar);
59756302Selan 		else
59856302Selan 			for(i = 0; i < n; i++)
59956302Selan 				tputs(dl, 0, __cputchar);
60056652Selan 
60156651Selan 		/*
60256652Selan 		 * Push down the bottom region.
60356651Selan 		 */
60456705Selan 		mvcur(top, 0, bot - n + 1, 0);
60558019Selan 		if (AL)
60657475Sbostic 			tputs(__tscroll(AL, n), 0, __cputchar);
60756652Selan 		else
60856652Selan 			for(i = 0; i < n; i++)
60956652Selan 				tputs(al, 0, __cputchar);
61056705Selan 		mvcur(bot - n + 1, 0, oy, ox);
61156302Selan 	} else {
61256651Selan 		/* Preserve the bottom lines */
61356705Selan 		mvcur(oy, ox, bot + n + 1, 0);	/* n < 0 */
61456652Selan 		if (DL)
61557475Sbostic 			tputs(__tscroll(DL, -n), 0, __cputchar);
61656652Selan 		else
61756652Selan 		       	for(i = n; i < 0; i++)
61856652Selan 				tputs(dl, 0, __cputchar);
61956705Selan 		mvcur(bot + n + 1, 0, top, 0);
62056302Selan 
62156302Selan 		/* Scroll the block down */
62258019Selan 		if (AL)
62357475Sbostic 			tputs(__tscroll(AL, -n), 0, __cputchar);
62456302Selan 		else
62556302Selan 			for(i = n; i < 0; i++)
62656302Selan 				tputs(al, 0, __cputchar);
62756705Selan 		mvcur(top, 0, oy, ox);
62856378Selan 	}
62956302Selan }
63056378Selan 
63156378Selan 
632