xref: /csrg-svn/lib/libcurses/refresh.c (revision 59352)
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*59352Selan static char sccsid[] = "@(#)refresh.c	5.38 (Berkeley) 04/27/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 
11958369Selan 		} else
12058369Selan 			win->lines[wy]->flags &= ~__ISPASTEOL;
12155976Sbostic #ifdef DEBUG
12256715Selan 		__TRACE("\t%d\t%d\n", *win->lines[wy]->firstchp,
12356715Selan 			*win->lines[wy]->lastchp);
12455976Sbostic #endif
1252261Sarnold 	}
12656238Selan 
12756302Selan #ifdef DEBUG
12856238Selan 	__TRACE("refresh: ly=%d, lx=%d\n", ly, lx);
12956302Selan #endif
13056472Selan 
13112358Sarnold 	if (win == curscr)
13256238Selan 		domvcur(ly, lx, win->cury, win->curx);
13319893Sbloom 	else {
13456238Selan 		if (win->flags & __LEAVEOK) {
13556238Selan 			curscr->cury = ly;
13656238Selan 			curscr->curx = lx;
13756238Selan 			ly -= win->begy;
13856238Selan 			lx -= win->begx;
13956238Selan 			if (ly >= 0 && ly < win->maxy && lx >= 0 &&
14056238Selan 			    lx < win->maxx) {
14156238Selan 				win->cury = ly;
14256238Selan 				win->curx = lx;
14355976Sbostic 			} else
14456238Selan 				win->cury = win->curx = 0;
14555976Sbostic 		} else {
14656238Selan 			domvcur(ly, lx, win->cury + win->begy,
14756238Selan 			    win->curx + win->begx);
14856238Selan 			curscr->cury = win->cury + win->begy;
14956238Selan 			curscr->curx = win->curx + win->begx;
15019893Sbloom 		}
1512261Sarnold 	}
15257472Sbostic 	retval = OK;
15355986Sbostic 
1542261Sarnold 	_win = NULL;
15555976Sbostic 	(void)fflush(stdout);
15655976Sbostic 	return (retval);
1572261Sarnold }
1582261Sarnold 
1592261Sarnold /*
16055976Sbostic  * makech --
16155976Sbostic  *	Make a change on the screen.
1622261Sarnold  */
16355976Sbostic static int
1642261Sarnold makech(win, wy)
16555976Sbostic 	register WINDOW *win;
16655976Sbostic 	int wy;
1672261Sarnold {
16858049Selan 	register int nlsp;		/* Last space in lines. */
169*59352Selan 	register int wx, lch, y;
17056648Selan 	register __LDATA *nsp, *csp, *cp;
17156651Selan 	u_int force;
17256648Selan 	char *ce;
17356648Selan 	__LDATA blank = {' ', 0};
17456648Selan 
17556472Selan 	/* Is the cursor still on the end of the last line? */
17656472Selan 	if (wy > 0 && win->lines[wy - 1]->flags & __ISPASTEOL) {
17756551Selan 		win->lines[wy - 1]->flags &= ~__ISPASTEOL;
17856551Selan 		domvcur(ly, lx, ly + 1, 0);
17956551Selan 		ly++;
18056551Selan 		lx = 0;
18156551Selan 	}
18256238Selan 	if (!(win->lines[wy]->flags & __ISDIRTY))
18357472Sbostic 		return (OK);
184*59352Selan 
18556715Selan 	wx = *win->lines[wy]->firstchp - win->ch_off;
186*59352Selan 	if (wx < 0)
187*59352Selan 		wx = 0;
188*59352Selan 	else if (wx >= win->maxx)
18957472Sbostic 		return (OK);
19056715Selan 	lch = *win->lines[wy]->lastchp - win->ch_off;
19119893Sbloom 	if (lch < 0)
19257472Sbostic 		return (OK);
193*59352Selan 	else if (lch >= (int) win->maxx)
19456238Selan 		lch = win->maxx - 1;
19556238Selan 	y = wy + win->begy;
19619893Sbloom 
1972287Sarnold 	if (curwin)
19856648Selan 		csp = &blank;
1992287Sarnold 	else
20056238Selan 		csp = &curscr->lines[wy + win->begy]->line[wx + win->begx];
20119893Sbloom 
20256238Selan 	nsp = &win->lines[wy]->line[wx];
20356651Selan 	force = win->lines[wy]->flags & __FORCEPAINT;
20456651Selan 	win->lines[wy]->flags &= ~__FORCEPAINT;
2052287Sarnold 	if (CE && !curwin) {
20656648Selan 		for (cp = &win->lines[wy]->line[win->maxx - 1];
20756648Selan 		     cp->ch == ' ' && cp->attr == 0; cp--)
20856648Selan 			if (cp <= win->lines[wy]->line)
2092261Sarnold 				break;
21056648Selan 		nlsp = cp - win->lines[wy]->line;
2112261Sarnold 	}
2122287Sarnold 	if (!curwin)
2132287Sarnold 		ce = CE;
2142287Sarnold 	else
2152287Sarnold 		ce = NULL;
21619893Sbloom 
21756651Selan 	if (force) {
21856651Selan 		if (CM)
21956651Selan 			tputs(tgoto(CM, lx, ly), 0, __cputchar);
22056651Selan 		else {
22156651Selan 			tputs(HO, 0, __cputchar);
22256651Selan 			mvcur(0, 0, ly, lx);
22356651Selan 		}
22456651Selan 	}
2252261Sarnold 	while (wx <= lch) {
22657956Selan 		if (!force && memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
22755986Sbostic 			if (wx <= lch) {
22857956Selan 				while (memcmp(nsp, csp, sizeof(__LDATA)) == 0 &&
22956648Selan 			            wx <= lch) {
23056596Selan 					    nsp++;
23156648Selan 					    if (!curwin)
23256648Selan 						    csp++;
23356648Selan 					    ++wx;
23456648Selan 				    }
23555986Sbostic 				continue;
23655986Sbostic 			}
23755986Sbostic 			break;
23855986Sbostic 		}
23956238Selan 		domvcur(ly, lx, y, wx + win->begx);
24056378Selan 
24155976Sbostic #ifdef DEBUG
24256651Selan 		__TRACE("makech: 1: wx = %d, ly= %d, lx = %d, newy = %d, newx = %d, force =%d\n",
24356651Selan 		    wx, ly, lx, y, wx + win->begx, force);
24455976Sbostic #endif
24555986Sbostic 		ly = y;
24656238Selan 		lx = wx + win->begx;
24757956Selan 		while ((force || memcmp(nsp, csp, sizeof(__LDATA)) != 0)
24856651Selan 		    && wx <= lch) {
24955986Sbostic 
25055986Sbostic 			/* Enter/exit standout mode as appropriate. */
25156648Selan 			if (SO && (nsp->attr & __STANDOUT) !=
25256378Selan 			    (curscr->flags & __WSTANDOUT)) {
25356648Selan 				if (nsp->attr & __STANDOUT) {
25455986Sbostic 					tputs(SO, 0, __cputchar);
25556238Selan 					curscr->flags |= __WSTANDOUT;
25655986Sbostic 				} else {
25755986Sbostic 					tputs(SE, 0, __cputchar);
25856238Selan 					curscr->flags &= ~__WSTANDOUT;
2592261Sarnold 				}
26055986Sbostic 			}
26155986Sbostic 
26255986Sbostic 			wx++;
26356472Selan 			if (wx >= win->maxx && wy == win->maxy - 1 && !curwin)
26456238Selan 				if (win->flags & __SCROLLOK) {
26556238Selan 					if (curscr->flags & __WSTANDOUT
26656238Selan 					    && win->flags & __ENDLINE)
26755986Sbostic 						if (!MS) {
26855986Sbostic 							tputs(SE, 0,
26955986Sbostic 							    __cputchar);
27056238Selan 							curscr->flags &=
27156238Selan 							    ~__WSTANDOUT;
27255976Sbostic 						}
27356596Selan 					if (!curwin) {
27456648Selan 						csp->attr = nsp->attr;
27556648Selan 						putchar(csp->ch = nsp->ch);
27656596Selan 					} else
27756648Selan 						putchar(nsp->ch);
27858040Selan 
27956599Selan 					if (wx + win->begx < curscr->maxx) {
28056599Selan 						domvcur(ly, wx + win->begx,
28156599Selan 						    win->begy + win->maxy - 1,
28256599Selan 						    win->begx + win->maxx - 1);
28356599Selan 					}
28456378Selan 					ly = win->begy + win->maxy - 1;
28556378Selan 					lx = win->begx + win->maxx - 1;
28657472Sbostic 					return (OK);
28755986Sbostic 				} else
28856238Selan 					if (win->flags & __SCROLLWIN) {
28955986Sbostic 						lx = --wx;
29057472Sbostic 						return (ERR);
29155986Sbostic 					}
29256596Selan 			if (!curwin) {
29356648Selan 				csp->attr = nsp->attr;
29456648Selan 				putchar(csp->ch = nsp->ch);
29556648Selan 				csp++;
29656648Selan 		       	} else
29756648Selan 				putchar(nsp->ch);
29856472Selan 
29955976Sbostic #ifdef DEBUG
30056648Selan 			__TRACE("makech: putchar(%c)\n", nsp->ch & 0177);
30155976Sbostic #endif
30256648Selan 			if (UC && (nsp->attr & __STANDOUT)) {
30355986Sbostic 				putchar('\b');
30455986Sbostic 				tputs(UC, 0, __cputchar);
3052261Sarnold 			}
30655986Sbostic 			nsp++;
30755986Sbostic 		}
30855976Sbostic #ifdef DEBUG
30955986Sbostic 		__TRACE("makech: 2: wx = %d, lx = %d\n", wx, lx);
31055976Sbostic #endif
31156238Selan 		if (lx == wx + win->begx)	/* If no change. */
31255986Sbostic 			break;
31356238Selan 		lx = wx + win->begx;
31455986Sbostic 		if (lx >= COLS && AM) {
31558040Selan 			if (wy != LINES)
31658040Selan 				win->lines[wy]->flags |= __ISPASTEOL;
31758040Selan 			lx = COLS - 1;
31856596Selan 		} else if (wx >= win->maxx) {
31956596Selan 			if (wy != win->maxy)
32056596Selan 				win->lines[wy]->flags |= __ISPASTEOL;
32156596Selan 			domvcur(ly, lx, ly, win->maxx + win->begx - 1);
32256596Selan 			lx = win->maxx + win->begx - 1;
32355986Sbostic 		}
32456596Selan 
32555976Sbostic #ifdef DEBUG
32655976Sbostic 		__TRACE("makech: 3: wx = %d, lx = %d\n", wx, lx);
32755976Sbostic #endif
3282261Sarnold 	}
32957472Sbostic 	return (OK);
33011736Sarnold }
33111736Sarnold 
33211736Sarnold /*
33355976Sbostic  * domvcur --
33455976Sbostic  *	Do a mvcur, leaving standout mode if necessary.
33511736Sarnold  */
33655976Sbostic static void
33711736Sarnold domvcur(oy, ox, ny, nx)
33855976Sbostic 	int oy, ox, ny, nx;
33955976Sbostic {
34056238Selan 	if (curscr->flags & __WSTANDOUT && !MS) {
34155976Sbostic 		tputs(SE, 0, __cputchar);
34256238Selan 		curscr->flags &= ~__WSTANDOUT;
3432261Sarnold 	}
34456596Selan 
34511736Sarnold 	mvcur(oy, ox, ny, nx);
3462261Sarnold }
34756302Selan 
34856302Selan /*
34956302Selan  * Quickch() attempts to detect a pattern in the change of the window
35056552Selan  * in order to optimize the change, e.g., scroll n lines as opposed to
35156302Selan  * repainting the screen line by line.
35256302Selan  */
35356302Selan 
35456302Selan static void
35556302Selan quickch(win)
35656302Selan 	WINDOW *win;
35756302Selan {
35858019Selan #define THRESH		(int) win->maxy / 4
35956302Selan 
36056648Selan 	register __LINE *clp, *tmp1, *tmp2;
36156378Selan 	register int bsize, curs, curw, starts, startw, i, j;
36256652Selan 	int n, target, cur_period, bot, top, sc_region;
36356648Selan 	__LDATA buf[1024];
36456378Selan 	u_int blank_hash;
36556302Selan 
36658019Selan 	/*
36758019Selan 	 * Find how many lines from the top of the screen are unchanged.
36858019Selan 	 */
36958034Selan 	for (top = 0; top < win->maxy; top++)
37058019Selan 		if (win->lines[top]->flags & __FORCEPAINT ||
37158019Selan 		    win->lines[top]->hash != curscr->lines[top]->hash
37258019Selan 		    || memcmp(win->lines[top]->line,
37358019Selan 		    curscr->lines[top]->line,
37458019Selan 		    win->maxx * __LDATASIZE) != 0)
37558019Selan 			break;
37658034Selan 		else
37758034Selan 			win->lines[top]->flags &= ~__ISDIRTY;
37858019Selan        /*
37958019Selan 	* Find how many lines from bottom of screen are unchanged.
38058019Selan 	*/
38158019Selan 	for (bot = win->maxy - 1; bot >= 0; bot--)
38258019Selan 		if (win->lines[bot]->flags & __FORCEPAINT ||
38358019Selan 		    win->lines[bot]->hash != curscr->lines[bot]->hash
38458019Selan 		    || memcmp(win->lines[bot]->line,
38558019Selan 		    curscr->lines[bot]->line,
38658019Selan 		    win->maxx * __LDATASIZE) != 0)
38758019Selan 			break;
38858034Selan 		else
38958034Selan 			win->lines[bot]->flags &= ~__ISDIRTY;
39058019Selan 
391*59352Selan #ifdef NO_JERKINESS
39256552Selan 	/*
39359062Selan 	 * If we have a bottom unchanged region return.  Scrolling the
39459062Selan 	 * bottom region up and then back down causes a screen jitter.
39559062Selan 	 * This will increase the number of characters sent to the screen
39659062Selan 	 * but it looks better.
39759062Selan 	 */
39859062Selan 	if (bot < win->maxy - 1)
39959062Selan 		return;
400*59352Selan #endif /* NO_JERKINESS */
40159062Selan 
40259062Selan 	/*
40356552Selan 	 * Search for the largest block of text not changed.
40456652Selan 	 * Invariants of the loop:
40556652Selan 	 * - Startw is the index of the beginning of the examined block in win.
40656652Selan          * - Starts is the index of the beginning of the examined block in
40756652Selan 	 *    curscr.
40856652Selan 	 * - Curs is the index of one past the end of the exmined block in win.
40956652Selan 	 * - Curw is the index of one past the end of the exmined block in
41056652Selan 	 *   curscr.
41156652Selan 	 * - bsize is the current size of the examined block.
41256552Selan          */
41358019Selan 	for (bsize = bot - top; bsize >= THRESH; bsize--) {
41458019Selan 		for (startw = top; startw <= bot - bsize; startw++)
41558019Selan 			for (starts = top; starts <= bot - bsize;
41656302Selan 			     starts++) {
41756302Selan 				for (curw = startw, curs = starts;
41856302Selan 				     curs < starts + bsize; curw++, curs++)
41956651Selan 					if (win->lines[curw]->flags &
42056651Selan 					    __FORCEPAINT ||
42156651Selan 					    (win->lines[curw]->hash !=
42256552Selan 					    curscr->lines[curs]->hash ||
42357956Selan 				            memcmp(win->lines[curw]->line,
42456648Selan 					    curscr->lines[curs]->line,
42556651Selan 					    win->maxx * __LDATASIZE) != 0))
42656302Selan 						break;
42756302Selan 				if (curs == starts + bsize)
42856302Selan 					goto done;
42956302Selan 			}
43058019Selan 	}
43156302Selan  done:
43256651Selan 	/* Did not find anything */
43356651Selan 	if (bsize < THRESH)
43456302Selan 		return;
43556302Selan 
43656302Selan #ifdef DEBUG
43756559Selan 	__TRACE("quickch:bsize=%d,starts=%d,startw=%d,curw=%d,curs=%d,top=%d,bot=%d\n",
43856559Selan 		bsize, starts, startw, curw, curs, top, bot);
43956302Selan #endif
44056378Selan 
44156559Selan 	/*
44256559Selan 	 * Make sure that there is no overlap between the bottom and top
44356559Selan 	 * regions and the middle scrolled block.
44456559Selan 	 */
44556691Selan 	if (bot < curs)
44656691Selan 		bot = curs - 1;
44756691Selan 	if (top > starts)
44856691Selan 		top = starts;
44956559Selan 
45056378Selan 	n = startw - starts;
45156378Selan 
45256691Selan #ifdef DEBUG
45356691Selan 		__TRACE("#####################################\n");
45456691Selan 		for (i = 0; i < curscr->maxy; i++) {
45556691Selan 			__TRACE("C: %d:", i);
45657356Selan 			__TRACE(" 0x%x \n", curscr->lines[i]->hash);
45756691Selan 			for (j = 0; j < curscr->maxx; j++)
45856691Selan 				__TRACE("%c",
45956691Selan 			           curscr->lines[i]->line[j].ch);
46056691Selan 			__TRACE("\n");
46157356Selan 			for (j = 0; j < curscr->maxx; j++)
46257356Selan 				__TRACE("%x",
46357356Selan 			           curscr->lines[i]->line[j].attr);
46457356Selan 			__TRACE("\n");
46556691Selan 			__TRACE("W: %d:", i);
46657356Selan 			__TRACE(" 0x%x \n", win->lines[i]->hash);
46757356Selan 			__TRACE(" 0x%x ", win->lines[i]->flags);
46856691Selan 			for (j = 0; j < win->maxx; j++)
46956691Selan 				__TRACE("%c",
47056691Selan 			           win->lines[i]->line[j].ch);
47156691Selan 			__TRACE("\n");
47257356Selan 			for (j = 0; j < win->maxx; j++)
47357356Selan 				__TRACE("%x",
47457356Selan 			           win->lines[i]->line[j].attr);
47557356Selan 			__TRACE("\n");
47656691Selan 		}
47756691Selan #endif
47856651Selan 	if (n != 0)
47956705Selan 		scrolln(win, starts, startw, curs, bot, top);
48058019Selan 
48156378Selan 	/* So we don't have to call __hash() each time */
48256648Selan 	for (i = 0; i < win->maxx; i++) {
48356648Selan 		buf[i].ch = ' ';
48456648Selan 		buf[i].attr = 0;
48556648Selan 	}
48658192Selan 	blank_hash = __hash((char *) buf, win->maxx * __LDATASIZE);
48756378Selan 
48856378Selan 	/*
48956378Selan 	 * Perform the rotation to maintain the consistency of curscr.
49056691Selan 	 * This is hairy since we are doing an *in place* rotation.
49156652Selan 	 * Invariants of the loop:
49256652Selan 	 * - I is the index of the current line.
49356652Selan 	 * - Target is the index of the target of line i.
49456652Selan 	 * - Tmp1 points to current line (i).
49556652Selan 	 * - Tmp2 and points to target line (target);
49656652Selan 	 * - Cur_period is the index of the end of the current period.
49756652Selan 	 *   (see below).
49856652Selan 	 *
49956652Selan 	 * There are 2 major issues here that make this rotation non-trivial:
50056652Selan 	 * 1.  Scrolling in a scrolling region bounded by the top
50156652Selan 	 *     and bottom regions determined (whose size is sc_region).
50256652Selan 	 * 2.  As a result of the use of the mod function, there may be a
50356652Selan 	 *     period introduced, i.e., 2 maps to 4, 4 to 6, n-2 to 0, and
50456652Selan 	 *     0 to 2, which then causes all odd lines not to be rotated.
50556652Selan 	 *     To remedy this, an index of the end ( = beginning) of the
50656652Selan 	 *     current 'period' is kept, cur_period, and when it is reached,
50756652Selan 	 *     the next period is started from cur_period + 1 which is
50856652Selan 	 *     guaranteed not to have been reached since that would mean that
50956652Selan 	 *     all records would have been reached. (think about it...).
51056652Selan 	 *
51156652Selan 	 * Lines in the rotation can have 3 attributes which are marked on the
51256652Selan 	 * line so that curscr is consistent with the visual screen.
51356705Selan 	 * 1.  Not dirty -- lines inside the scrolled block, top region or
51456652Selan 	 *                  bottom region.
51556705Selan 	 * 2.  Blank lines -- lines in the differential of the scrolling
51656705Selan 	 *		      region adjacent to top and bot regions
51756705Selan 	 *                    depending on scrolling direction.
51856652Selan 	 * 3.  Dirty line -- all other lines are marked dirty.
51956378Selan 	 */
52056648Selan 	sc_region = bot - top + 1;
52156648Selan 	i = top;
52256648Selan 	tmp1 = curscr->lines[top];
52356652Selan 	cur_period = top;
52456648Selan 	for (j = top; j <= bot; j++) {
52556648Selan 		target = (i - top + n + sc_region) % sc_region + top;
52656378Selan 		tmp2 = curscr->lines[target];
52756378Selan 		curscr->lines[target] = tmp1;
52856378Selan 		/* Mark block as clean and blank out scrolled lines. */
52956378Selan 		clp = curscr->lines[target];
53056472Selan #ifdef DEBUG
53156378Selan 		__TRACE("quickch: n=%d startw=%d curw=%d i = %d target=%d ",
53256378Selan 			n, startw, curw, i, target);
53356472Selan #endif
53456691Selan 		if ((target >= startw && target < curw) || target < top
53556691Selan 		    || target > bot) {
53656472Selan #ifdef DEBUG
53756378Selan 			__TRACE("-- notdirty");
53856472Selan #endif
53956378Selan 			win->lines[target]->flags &= ~__ISDIRTY;
54056705Selan 		} else if ((n > 0 && target >= top && target < top + n) ||
54156705Selan 		           (n < 0 && target <= bot && target > bot + n)) {
54257956Selan 			if (clp->hash != blank_hash ||  memcmp(clp->line,
54356648Selan 			    buf, win->maxx * __LDATASIZE) !=0) {
54458041Selan 				(void)memcpy(clp->line,  buf,
54556648Selan 				    win->maxx * __LDATASIZE);
54656472Selan #ifdef DEBUG
54756648Selan 				__TRACE("-- blanked out: dirty");
54856472Selan #endif
54956378Selan 				clp->hash = blank_hash;
55056652Selan 				__touchline(win, target, 0, win->maxx - 1, 0);
55156705Selan 			} else {
55256652Selan 				__touchline(win, target, 0, win->maxx - 1, 0);
55356472Selan #ifdef DEBUG
55456648Selan 				__TRACE(" -- blank line already: dirty");
55556472Selan #endif
55656705Selan 			}
55756378Selan 		} else {
55856472Selan #ifdef DEBUG
55956648Selan 			__TRACE(" -- dirty");
56056472Selan #endif
56156651Selan 			__touchline(win, target, 0, win->maxx - 1, 0);
56256378Selan 		}
56356472Selan #ifdef DEBUG
56456378Selan 		__TRACE("\n");
56556472Selan #endif
56656652Selan 		if (target == cur_period) {
56756378Selan 			i = target + 1;
56856378Selan 			tmp1 = curscr->lines[i];
56956652Selan 			cur_period = i;
57056378Selan 		} else {
57156378Selan 			tmp1 = tmp2;
57256378Selan 			i = target;
57356378Selan 		}
57456302Selan 	}
57556472Selan #ifdef DEBUG
57656648Selan 		__TRACE("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
57756648Selan 		for (i = 0; i < curscr->maxy; i++) {
57856691Selan 			__TRACE("C: %d:", i);
57956648Selan 			for (j = 0; j < curscr->maxx; j++)
58056648Selan 				__TRACE("%c",
58156648Selan 			           curscr->lines[i]->line[j].ch);
58256648Selan 			__TRACE("\n");
58356691Selan 			__TRACE("W: %d:", i);
58456691Selan 			for (j = 0; j < win->maxx; j++)
58556691Selan 				__TRACE("%c",
58656691Selan 			           win->lines[i]->line[j].ch);
58756691Selan 			__TRACE("\n");
58856648Selan 		}
58956472Selan #endif
59056302Selan }
59156302Selan 
59256652Selan /*
59356652Selan  * Scrolln performs the scroll by n lines, where n is starts - startw.
59456652Selan  */
59556302Selan static void
59656705Selan scrolln(win, starts, startw, curs, bot, top)
59756302Selan 	WINDOW *win;
59856705Selan 	int starts, startw, curs, bot, top;
59956302Selan {
60056302Selan 	int i, oy, ox, n;
60156302Selan 
60256302Selan 	oy = curscr->cury;
60356302Selan 	ox = curscr->curx;
60456302Selan 	n = starts - startw;
60556302Selan 
60656302Selan 	if (n > 0) {
60756559Selan 		mvcur(oy, ox, top, 0);
60856559Selan 		/* Scroll up the block */
60956302Selan 		if (DL)
61057475Sbostic 			tputs(__tscroll(DL, n), 0, __cputchar);
61156302Selan 		else
61256302Selan 			for(i = 0; i < n; i++)
61356302Selan 				tputs(dl, 0, __cputchar);
61456652Selan 
61556651Selan 		/*
61656652Selan 		 * Push down the bottom region.
61756651Selan 		 */
61856705Selan 		mvcur(top, 0, bot - n + 1, 0);
61958019Selan 		if (AL)
62057475Sbostic 			tputs(__tscroll(AL, n), 0, __cputchar);
62156652Selan 		else
62256652Selan 			for(i = 0; i < n; i++)
62356652Selan 				tputs(al, 0, __cputchar);
62456705Selan 		mvcur(bot - n + 1, 0, oy, ox);
62556302Selan 	} else {
62656651Selan 		/* Preserve the bottom lines */
62756705Selan 		mvcur(oy, ox, bot + n + 1, 0);	/* n < 0 */
62856652Selan 		if (DL)
62957475Sbostic 			tputs(__tscroll(DL, -n), 0, __cputchar);
63056652Selan 		else
63156652Selan 		       	for(i = n; i < 0; i++)
63256652Selan 				tputs(dl, 0, __cputchar);
63356705Selan 		mvcur(bot + n + 1, 0, top, 0);
63456302Selan 
63556302Selan 		/* Scroll the block down */
63658019Selan 		if (AL)
63757475Sbostic 			tputs(__tscroll(AL, -n), 0, __cputchar);
63856302Selan 		else
63956302Selan 			for(i = n; i < 0; i++)
64056302Selan 				tputs(al, 0, __cputchar);
64156705Selan 		mvcur(top, 0, oy, ox);
64256378Selan 	}
64356302Selan }
64456378Selan 
64556378Selan 
646