xref: /csrg-svn/lib/libcurses/refresh.c (revision 59774)
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*59774Selan static char sccsid[] = "@(#)refresh.c	5.40 (Berkeley) 05/06/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 =
5158192Selan 				   __hash((char *) 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;
10058369Selan 		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 			}
11858369Selan 
119*59774Selan 		}
12055976Sbostic #ifdef DEBUG
12156715Selan 		__TRACE("\t%d\t%d\n", *win->lines[wy]->firstchp,
12256715Selan 			*win->lines[wy]->lastchp);
12355976Sbostic #endif
1242261Sarnold 	}
12556238Selan 
12656302Selan #ifdef DEBUG
12756238Selan 	__TRACE("refresh: ly=%d, lx=%d\n", ly, lx);
12856302Selan #endif
12956472Selan 
13012358Sarnold 	if (win == curscr)
13156238Selan 		domvcur(ly, lx, win->cury, win->curx);
13219893Sbloom 	else {
13356238Selan 		if (win->flags & __LEAVEOK) {
13456238Selan 			curscr->cury = ly;
13556238Selan 			curscr->curx = lx;
13656238Selan 			ly -= win->begy;
13756238Selan 			lx -= win->begx;
13856238Selan 			if (ly >= 0 && ly < win->maxy && lx >= 0 &&
13956238Selan 			    lx < win->maxx) {
14056238Selan 				win->cury = ly;
14156238Selan 				win->curx = lx;
14255976Sbostic 			} else
14356238Selan 				win->cury = win->curx = 0;
14455976Sbostic 		} else {
14556238Selan 			domvcur(ly, lx, win->cury + win->begy,
14656238Selan 			    win->curx + win->begx);
14756238Selan 			curscr->cury = win->cury + win->begy;
14856238Selan 			curscr->curx = win->curx + win->begx;
14919893Sbloom 		}
1502261Sarnold 	}
15157472Sbostic 	retval = OK;
15255986Sbostic 
1532261Sarnold 	_win = NULL;
15455976Sbostic 	(void)fflush(stdout);
15555976Sbostic 	return (retval);
1562261Sarnold }
1572261Sarnold 
1582261Sarnold /*
15955976Sbostic  * makech --
16055976Sbostic  *	Make a change on the screen.
1612261Sarnold  */
16255976Sbostic static int
1632261Sarnold makech(win, wy)
16455976Sbostic 	register WINDOW *win;
16555976Sbostic 	int wy;
1662261Sarnold {
16758049Selan 	register int nlsp;		/* Last space in lines. */
16859352Selan 	register int wx, lch, y;
16956648Selan 	register __LDATA *nsp, *csp, *cp;
17056651Selan 	u_int force;
17156648Selan 	char *ce;
17256648Selan 	__LDATA blank = {' ', 0};
17356648Selan 
17456472Selan 	/* Is the cursor still on the end of the last line? */
17556472Selan 	if (wy > 0 && win->lines[wy - 1]->flags & __ISPASTEOL) {
17656551Selan 		domvcur(ly, lx, ly + 1, 0);
17756551Selan 		ly++;
17856551Selan 		lx = 0;
17956551Selan 	}
18056715Selan 	wx = *win->lines[wy]->firstchp - win->ch_off;
18159352Selan 	if (wx < 0)
18259352Selan 		wx = 0;
18359352Selan 	else if (wx >= win->maxx)
18457472Sbostic 		return (OK);
18556715Selan 	lch = *win->lines[wy]->lastchp - win->ch_off;
18619893Sbloom 	if (lch < 0)
18757472Sbostic 		return (OK);
18859352Selan 	else if (lch >= (int) win->maxx)
18956238Selan 		lch = win->maxx - 1;
19056238Selan 	y = wy + win->begy;
19119893Sbloom 
1922287Sarnold 	if (curwin)
19356648Selan 		csp = &blank;
1942287Sarnold 	else
19556238Selan 		csp = &curscr->lines[wy + win->begy]->line[wx + win->begx];
19619893Sbloom 
19756238Selan 	nsp = &win->lines[wy]->line[wx];
19856651Selan 	force = win->lines[wy]->flags & __FORCEPAINT;
19956651Selan 	win->lines[wy]->flags &= ~__FORCEPAINT;
2002287Sarnold 	if (CE && !curwin) {
20156648Selan 		for (cp = &win->lines[wy]->line[win->maxx - 1];
20256648Selan 		     cp->ch == ' ' && cp->attr == 0; cp--)
20356648Selan 			if (cp <= win->lines[wy]->line)
2042261Sarnold 				break;
20556648Selan 		nlsp = cp - win->lines[wy]->line;
2062261Sarnold 	}
2072287Sarnold 	if (!curwin)
2082287Sarnold 		ce = CE;
2092287Sarnold 	else
2102287Sarnold 		ce = NULL;
21119893Sbloom 
21256651Selan 	if (force) {
21356651Selan 		if (CM)
21456651Selan 			tputs(tgoto(CM, lx, ly), 0, __cputchar);
21556651Selan 		else {
21656651Selan 			tputs(HO, 0, __cputchar);
21756651Selan 			mvcur(0, 0, ly, lx);
21856651Selan 		}
21956651Selan 	}
2202261Sarnold 	while (wx <= lch) {
22157956Selan 		if (!force && memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
22255986Sbostic 			if (wx <= lch) {
22357956Selan 				while (memcmp(nsp, csp, sizeof(__LDATA)) == 0 &&
22456648Selan 			            wx <= lch) {
22556596Selan 					    nsp++;
22656648Selan 					    if (!curwin)
22756648Selan 						    csp++;
22856648Selan 					    ++wx;
22956648Selan 				    }
23055986Sbostic 				continue;
23155986Sbostic 			}
23255986Sbostic 			break;
23355986Sbostic 		}
23456238Selan 		domvcur(ly, lx, y, wx + win->begx);
23556378Selan 
23655976Sbostic #ifdef DEBUG
23756651Selan 		__TRACE("makech: 1: wx = %d, ly= %d, lx = %d, newy = %d, newx = %d, force =%d\n",
23856651Selan 		    wx, ly, lx, y, wx + win->begx, force);
23955976Sbostic #endif
24055986Sbostic 		ly = y;
24156238Selan 		lx = wx + win->begx;
24257956Selan 		while ((force || memcmp(nsp, csp, sizeof(__LDATA)) != 0)
24356651Selan 		    && wx <= lch) {
24455986Sbostic 
24555986Sbostic 			/* Enter/exit standout mode as appropriate. */
24656648Selan 			if (SO && (nsp->attr & __STANDOUT) !=
24756378Selan 			    (curscr->flags & __WSTANDOUT)) {
24856648Selan 				if (nsp->attr & __STANDOUT) {
24955986Sbostic 					tputs(SO, 0, __cputchar);
25056238Selan 					curscr->flags |= __WSTANDOUT;
25155986Sbostic 				} else {
25255986Sbostic 					tputs(SE, 0, __cputchar);
25356238Selan 					curscr->flags &= ~__WSTANDOUT;
2542261Sarnold 				}
25555986Sbostic 			}
25655986Sbostic 
25755986Sbostic 			wx++;
25856472Selan 			if (wx >= win->maxx && wy == win->maxy - 1 && !curwin)
25956238Selan 				if (win->flags & __SCROLLOK) {
26056238Selan 					if (curscr->flags & __WSTANDOUT
26156238Selan 					    && win->flags & __ENDLINE)
26255986Sbostic 						if (!MS) {
26355986Sbostic 							tputs(SE, 0,
26455986Sbostic 							    __cputchar);
26556238Selan 							curscr->flags &=
26656238Selan 							    ~__WSTANDOUT;
26755976Sbostic 						}
26859760Selan 					if (!(win->flags & __SCROLLWIN)) {
26959760Selan 						if (!curwin) {
27059760Selan 							csp->attr = nsp->attr;
27159760Selan 							putchar(csp->ch = nsp->ch);
27259760Selan 						} else
27359760Selan 							putchar(nsp->ch);
27459760Selan 					}
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);
28359760Selan 				}
28459760Selan 			if (wx < win->maxx || wy < win->maxy - 1 ||
28559760Selan 			    !(win->flags & __SCROLLWIN)) {
28659760Selan 				if (!curwin) {
28759760Selan 					csp->attr = nsp->attr;
28859760Selan 					putchar(csp->ch = nsp->ch);
28959760Selan 					csp++;
29059760Selan 				} else
29159760Selan 					putchar(nsp->ch);
29259760Selan 			}
29355976Sbostic #ifdef DEBUG
29456648Selan 			__TRACE("makech: putchar(%c)\n", nsp->ch & 0177);
29555976Sbostic #endif
29656648Selan 			if (UC && (nsp->attr & __STANDOUT)) {
29755986Sbostic 				putchar('\b');
29855986Sbostic 				tputs(UC, 0, __cputchar);
2992261Sarnold 			}
30055986Sbostic 			nsp++;
30155976Sbostic #ifdef DEBUG
30255986Sbostic 		__TRACE("makech: 2: wx = %d, lx = %d\n", wx, lx);
30355976Sbostic #endif
30459760Selan 		}
30556238Selan 		if (lx == wx + win->begx)	/* If no change. */
30655986Sbostic 			break;
30756238Selan 		lx = wx + win->begx;
308*59774Selan 		if (lx >= COLS && AM)
30958040Selan 			lx = COLS - 1;
310*59774Selan 		else if (wx >= win->maxx) {
31156596Selan 			domvcur(ly, lx, ly, win->maxx + win->begx - 1);
31256596Selan 			lx = win->maxx + win->begx - 1;
31355986Sbostic 		}
31456596Selan 
31555976Sbostic #ifdef DEBUG
31655976Sbostic 		__TRACE("makech: 3: wx = %d, lx = %d\n", wx, lx);
31755976Sbostic #endif
3182261Sarnold 	}
31957472Sbostic 	return (OK);
32011736Sarnold }
32111736Sarnold 
32211736Sarnold /*
32355976Sbostic  * domvcur --
32455976Sbostic  *	Do a mvcur, leaving standout mode if necessary.
32511736Sarnold  */
32655976Sbostic static void
32711736Sarnold domvcur(oy, ox, ny, nx)
32855976Sbostic 	int oy, ox, ny, nx;
32955976Sbostic {
33056238Selan 	if (curscr->flags & __WSTANDOUT && !MS) {
33155976Sbostic 		tputs(SE, 0, __cputchar);
33256238Selan 		curscr->flags &= ~__WSTANDOUT;
3332261Sarnold 	}
33456596Selan 
33511736Sarnold 	mvcur(oy, ox, ny, nx);
3362261Sarnold }
33756302Selan 
33856302Selan /*
33956302Selan  * Quickch() attempts to detect a pattern in the change of the window
34056552Selan  * in order to optimize the change, e.g., scroll n lines as opposed to
34156302Selan  * repainting the screen line by line.
34256302Selan  */
34356302Selan 
34456302Selan static void
34556302Selan quickch(win)
34656302Selan 	WINDOW *win;
34756302Selan {
34858019Selan #define THRESH		(int) win->maxy / 4
34956302Selan 
35056648Selan 	register __LINE *clp, *tmp1, *tmp2;
35156378Selan 	register int bsize, curs, curw, starts, startw, i, j;
35256652Selan 	int n, target, cur_period, bot, top, sc_region;
35356648Selan 	__LDATA buf[1024];
35456378Selan 	u_int blank_hash;
35556302Selan 
35658019Selan 	/*
35758019Selan 	 * Find how many lines from the top of the screen are unchanged.
35858019Selan 	 */
35958034Selan 	for (top = 0; top < win->maxy; top++)
36058019Selan 		if (win->lines[top]->flags & __FORCEPAINT ||
36158019Selan 		    win->lines[top]->hash != curscr->lines[top]->hash
36258019Selan 		    || memcmp(win->lines[top]->line,
36358019Selan 		    curscr->lines[top]->line,
36458019Selan 		    win->maxx * __LDATASIZE) != 0)
36558019Selan 			break;
36658034Selan 		else
36758034Selan 			win->lines[top]->flags &= ~__ISDIRTY;
36858019Selan        /*
36958019Selan 	* Find how many lines from bottom of screen are unchanged.
37058019Selan 	*/
37158019Selan 	for (bot = win->maxy - 1; bot >= 0; bot--)
37258019Selan 		if (win->lines[bot]->flags & __FORCEPAINT ||
37358019Selan 		    win->lines[bot]->hash != curscr->lines[bot]->hash
37458019Selan 		    || memcmp(win->lines[bot]->line,
37558019Selan 		    curscr->lines[bot]->line,
37658019Selan 		    win->maxx * __LDATASIZE) != 0)
37758019Selan 			break;
37858034Selan 		else
37958034Selan 			win->lines[bot]->flags &= ~__ISDIRTY;
38058019Selan 
38159352Selan #ifdef NO_JERKINESS
38256552Selan 	/*
38359062Selan 	 * If we have a bottom unchanged region return.  Scrolling the
38459062Selan 	 * bottom region up and then back down causes a screen jitter.
38559062Selan 	 * This will increase the number of characters sent to the screen
38659062Selan 	 * but it looks better.
38759062Selan 	 */
38859062Selan 	if (bot < win->maxy - 1)
38959062Selan 		return;
39059352Selan #endif /* NO_JERKINESS */
39159062Selan 
39259062Selan 	/*
39356552Selan 	 * Search for the largest block of text not changed.
39456652Selan 	 * Invariants of the loop:
39556652Selan 	 * - Startw is the index of the beginning of the examined block in win.
39656652Selan          * - Starts is the index of the beginning of the examined block in
39756652Selan 	 *    curscr.
39856652Selan 	 * - Curs is the index of one past the end of the exmined block in win.
39956652Selan 	 * - Curw is the index of one past the end of the exmined block in
40056652Selan 	 *   curscr.
40156652Selan 	 * - bsize is the current size of the examined block.
40256552Selan          */
40358019Selan 	for (bsize = bot - top; bsize >= THRESH; bsize--) {
40458019Selan 		for (startw = top; startw <= bot - bsize; startw++)
40558019Selan 			for (starts = top; starts <= bot - bsize;
40656302Selan 			     starts++) {
40756302Selan 				for (curw = startw, curs = starts;
40856302Selan 				     curs < starts + bsize; curw++, curs++)
40956651Selan 					if (win->lines[curw]->flags &
41056651Selan 					    __FORCEPAINT ||
41156651Selan 					    (win->lines[curw]->hash !=
41256552Selan 					    curscr->lines[curs]->hash ||
41357956Selan 				            memcmp(win->lines[curw]->line,
41456648Selan 					    curscr->lines[curs]->line,
41556651Selan 					    win->maxx * __LDATASIZE) != 0))
41656302Selan 						break;
41756302Selan 				if (curs == starts + bsize)
41856302Selan 					goto done;
41956302Selan 			}
42058019Selan 	}
42156302Selan  done:
42256651Selan 	/* Did not find anything */
42356651Selan 	if (bsize < THRESH)
42456302Selan 		return;
42556302Selan 
42656302Selan #ifdef DEBUG
42756559Selan 	__TRACE("quickch:bsize=%d,starts=%d,startw=%d,curw=%d,curs=%d,top=%d,bot=%d\n",
42856559Selan 		bsize, starts, startw, curw, curs, top, bot);
42956302Selan #endif
43056378Selan 
43156559Selan 	/*
43256559Selan 	 * Make sure that there is no overlap between the bottom and top
43356559Selan 	 * regions and the middle scrolled block.
43456559Selan 	 */
43556691Selan 	if (bot < curs)
43656691Selan 		bot = curs - 1;
43756691Selan 	if (top > starts)
43856691Selan 		top = starts;
43956559Selan 
44056378Selan 	n = startw - starts;
44156378Selan 
44256691Selan #ifdef DEBUG
44356691Selan 		__TRACE("#####################################\n");
44456691Selan 		for (i = 0; i < curscr->maxy; i++) {
44556691Selan 			__TRACE("C: %d:", i);
44657356Selan 			__TRACE(" 0x%x \n", curscr->lines[i]->hash);
44756691Selan 			for (j = 0; j < curscr->maxx; j++)
44856691Selan 				__TRACE("%c",
44956691Selan 			           curscr->lines[i]->line[j].ch);
45056691Selan 			__TRACE("\n");
45157356Selan 			for (j = 0; j < curscr->maxx; j++)
45257356Selan 				__TRACE("%x",
45357356Selan 			           curscr->lines[i]->line[j].attr);
45457356Selan 			__TRACE("\n");
45556691Selan 			__TRACE("W: %d:", i);
45657356Selan 			__TRACE(" 0x%x \n", win->lines[i]->hash);
45757356Selan 			__TRACE(" 0x%x ", win->lines[i]->flags);
45856691Selan 			for (j = 0; j < win->maxx; j++)
45956691Selan 				__TRACE("%c",
46056691Selan 			           win->lines[i]->line[j].ch);
46156691Selan 			__TRACE("\n");
46257356Selan 			for (j = 0; j < win->maxx; j++)
46357356Selan 				__TRACE("%x",
46457356Selan 			           win->lines[i]->line[j].attr);
46557356Selan 			__TRACE("\n");
46656691Selan 		}
46756691Selan #endif
46856651Selan 	if (n != 0)
46956705Selan 		scrolln(win, starts, startw, curs, bot, top);
47058019Selan 
47156378Selan 	/* So we don't have to call __hash() each time */
47256648Selan 	for (i = 0; i < win->maxx; i++) {
47356648Selan 		buf[i].ch = ' ';
47456648Selan 		buf[i].attr = 0;
47556648Selan 	}
47658192Selan 	blank_hash = __hash((char *) buf, win->maxx * __LDATASIZE);
47756378Selan 
47856378Selan 	/*
47956378Selan 	 * Perform the rotation to maintain the consistency of curscr.
48056691Selan 	 * This is hairy since we are doing an *in place* rotation.
48156652Selan 	 * Invariants of the loop:
48256652Selan 	 * - I is the index of the current line.
48356652Selan 	 * - Target is the index of the target of line i.
48456652Selan 	 * - Tmp1 points to current line (i).
48556652Selan 	 * - Tmp2 and points to target line (target);
48656652Selan 	 * - Cur_period is the index of the end of the current period.
48756652Selan 	 *   (see below).
48856652Selan 	 *
48956652Selan 	 * There are 2 major issues here that make this rotation non-trivial:
49056652Selan 	 * 1.  Scrolling in a scrolling region bounded by the top
49156652Selan 	 *     and bottom regions determined (whose size is sc_region).
49256652Selan 	 * 2.  As a result of the use of the mod function, there may be a
49356652Selan 	 *     period introduced, i.e., 2 maps to 4, 4 to 6, n-2 to 0, and
49456652Selan 	 *     0 to 2, which then causes all odd lines not to be rotated.
49556652Selan 	 *     To remedy this, an index of the end ( = beginning) of the
49656652Selan 	 *     current 'period' is kept, cur_period, and when it is reached,
49756652Selan 	 *     the next period is started from cur_period + 1 which is
49856652Selan 	 *     guaranteed not to have been reached since that would mean that
49956652Selan 	 *     all records would have been reached. (think about it...).
50056652Selan 	 *
50156652Selan 	 * Lines in the rotation can have 3 attributes which are marked on the
50256652Selan 	 * line so that curscr is consistent with the visual screen.
50356705Selan 	 * 1.  Not dirty -- lines inside the scrolled block, top region or
50456652Selan 	 *                  bottom region.
50556705Selan 	 * 2.  Blank lines -- lines in the differential of the scrolling
50656705Selan 	 *		      region adjacent to top and bot regions
50756705Selan 	 *                    depending on scrolling direction.
50856652Selan 	 * 3.  Dirty line -- all other lines are marked dirty.
50956378Selan 	 */
51056648Selan 	sc_region = bot - top + 1;
51156648Selan 	i = top;
51256648Selan 	tmp1 = curscr->lines[top];
51356652Selan 	cur_period = top;
51456648Selan 	for (j = top; j <= bot; j++) {
51556648Selan 		target = (i - top + n + sc_region) % sc_region + top;
51656378Selan 		tmp2 = curscr->lines[target];
51756378Selan 		curscr->lines[target] = tmp1;
51856378Selan 		/* Mark block as clean and blank out scrolled lines. */
51956378Selan 		clp = curscr->lines[target];
52056472Selan #ifdef DEBUG
52156378Selan 		__TRACE("quickch: n=%d startw=%d curw=%d i = %d target=%d ",
52256378Selan 			n, startw, curw, i, target);
52356472Selan #endif
52456691Selan 		if ((target >= startw && target < curw) || target < top
52556691Selan 		    || target > bot) {
52656472Selan #ifdef DEBUG
52756378Selan 			__TRACE("-- notdirty");
52856472Selan #endif
52956378Selan 			win->lines[target]->flags &= ~__ISDIRTY;
53056705Selan 		} else if ((n > 0 && target >= top && target < top + n) ||
53156705Selan 		           (n < 0 && target <= bot && target > bot + n)) {
53257956Selan 			if (clp->hash != blank_hash ||  memcmp(clp->line,
53356648Selan 			    buf, win->maxx * __LDATASIZE) !=0) {
53458041Selan 				(void)memcpy(clp->line,  buf,
53556648Selan 				    win->maxx * __LDATASIZE);
53656472Selan #ifdef DEBUG
53756648Selan 				__TRACE("-- blanked out: dirty");
53856472Selan #endif
53956378Selan 				clp->hash = blank_hash;
54056652Selan 				__touchline(win, target, 0, win->maxx - 1, 0);
54156705Selan 			} else {
54256652Selan 				__touchline(win, target, 0, win->maxx - 1, 0);
54356472Selan #ifdef DEBUG
54456648Selan 				__TRACE(" -- blank line already: dirty");
54556472Selan #endif
54656705Selan 			}
54756378Selan 		} else {
54856472Selan #ifdef DEBUG
54956648Selan 			__TRACE(" -- dirty");
55056472Selan #endif
55156651Selan 			__touchline(win, target, 0, win->maxx - 1, 0);
55256378Selan 		}
55356472Selan #ifdef DEBUG
55456378Selan 		__TRACE("\n");
55556472Selan #endif
55656652Selan 		if (target == cur_period) {
55756378Selan 			i = target + 1;
55856378Selan 			tmp1 = curscr->lines[i];
55956652Selan 			cur_period = i;
56056378Selan 		} else {
56156378Selan 			tmp1 = tmp2;
56256378Selan 			i = target;
56356378Selan 		}
56456302Selan 	}
56556472Selan #ifdef DEBUG
56656648Selan 		__TRACE("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
56756648Selan 		for (i = 0; i < curscr->maxy; i++) {
56856691Selan 			__TRACE("C: %d:", i);
56956648Selan 			for (j = 0; j < curscr->maxx; j++)
57056648Selan 				__TRACE("%c",
57156648Selan 			           curscr->lines[i]->line[j].ch);
57256648Selan 			__TRACE("\n");
57356691Selan 			__TRACE("W: %d:", i);
57456691Selan 			for (j = 0; j < win->maxx; j++)
57556691Selan 				__TRACE("%c",
57656691Selan 			           win->lines[i]->line[j].ch);
57756691Selan 			__TRACE("\n");
57856648Selan 		}
57956472Selan #endif
58056302Selan }
58156302Selan 
58256652Selan /*
58356652Selan  * Scrolln performs the scroll by n lines, where n is starts - startw.
58456652Selan  */
58556302Selan static void
58656705Selan scrolln(win, starts, startw, curs, bot, top)
58756302Selan 	WINDOW *win;
58856705Selan 	int starts, startw, curs, bot, top;
58956302Selan {
59056302Selan 	int i, oy, ox, n;
59156302Selan 
59256302Selan 	oy = curscr->cury;
59356302Selan 	ox = curscr->curx;
59456302Selan 	n = starts - startw;
59556302Selan 
59656302Selan 	if (n > 0) {
59756559Selan 		mvcur(oy, ox, top, 0);
59856559Selan 		/* Scroll up the block */
59956302Selan 		if (DL)
60057475Sbostic 			tputs(__tscroll(DL, n), 0, __cputchar);
60156302Selan 		else
60256302Selan 			for(i = 0; i < n; i++)
60356302Selan 				tputs(dl, 0, __cputchar);
60456652Selan 
60556651Selan 		/*
60656652Selan 		 * Push down the bottom region.
60756651Selan 		 */
60856705Selan 		mvcur(top, 0, bot - n + 1, 0);
60958019Selan 		if (AL)
61057475Sbostic 			tputs(__tscroll(AL, n), 0, __cputchar);
61156652Selan 		else
61256652Selan 			for(i = 0; i < n; i++)
61356652Selan 				tputs(al, 0, __cputchar);
61456705Selan 		mvcur(bot - n + 1, 0, oy, ox);
61556302Selan 	} else {
61656651Selan 		/* Preserve the bottom lines */
61756705Selan 		mvcur(oy, ox, bot + n + 1, 0);	/* n < 0 */
61856652Selan 		if (DL)
61957475Sbostic 			tputs(__tscroll(DL, -n), 0, __cputchar);
62056652Selan 		else
62156652Selan 		       	for(i = n; i < 0; i++)
62256652Selan 				tputs(dl, 0, __cputchar);
62356705Selan 		mvcur(bot + n + 1, 0, top, 0);
62456302Selan 
62556302Selan 		/* Scroll the block down */
62658019Selan 		if (AL)
62757475Sbostic 			tputs(__tscroll(AL, -n), 0, __cputchar);
62856302Selan 		else
62956302Selan 			for(i = n; i < 0; i++)
63056302Selan 				tputs(al, 0, __cputchar);
63156705Selan 		mvcur(top, 0, oy, ox);
63256378Selan 	}
63356302Selan }
63456378Selan 
63556378Selan 
636