2 * Copyright (c) 1981, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)refresh.c 8.4 (Berkeley) 8/4/94";
45 static void domvcur __P((int, int, int, int));
46 static int makech __P((WINDOW *, int));
47 static void quickch __P((WINDOW *));
48 static void scrolln __P((int, int, int, int, int));
52 * Make the current screen look like "win" over the area coverd by
64 /* Initialize loop parameters. */
68 curwin = (win == curscr);
71 for (wy = 0; wy < win->maxy; wy++) {
73 if (wlp->flags & __ISDIRTY)
75 __hash((char *) wlp->line, win->maxx * __LDATASIZE);
78 if (win->flags & __CLEAROK || curscr->flags & __CLEAROK || curwin) {
79 if ((win->flags & __FULLWIN) || curscr->flags & __CLEAROK) {
80 if (curscr->flags & __WSTANDOUT) {
81 tputs(SE, 0, __cputchar);
82 curscr->flags &= ~__WSTANDOUT;
84 tputs(CL, win->maxy, __cputchar);
88 curscr->flags &= ~__CLEAROK;
95 win->flags &= ~__CLEAROK;
104 __CTRACE("wrefresh: (%0.2o): curwin = %d\n", win, curwin);
105 __CTRACE("wrefresh: \tfirstch\tlastch\n");
109 if ((win->flags & __FULLLINE) && !curwin) {
111 * Invoke quickch() only if more than a quarter of the lines
112 * in the window are dirty.
114 for (wy = 0, dnum = 0; wy < win->maxy; wy++)
115 if (win->lines[wy]->flags & (__ISDIRTY | __FORCEPAINT))
117 /* __noqch already == 0 when __FULLLINE */
118 if (dnum > (int) win->maxy / 4)
125 __CTRACE("#####################################\n");
126 for (i = 0; i < curscr->maxy; i++) {
127 __CTRACE("C: %d:", i);
128 __CTRACE(" 0x%x \n", curscr->lines[i]->hash);
129 for (j = 0; j < curscr->maxx; j++)
131 curscr->lines[i]->line[j].ch);
133 for (j = 0; j < curscr->maxx; j++)
135 curscr->lines[i]->line[j].attr);
137 if (i < win->begy || i > win->begy + win->maxy - 1)
139 __CTRACE("W: %d:", i - win->begy);
140 __CTRACE(" 0x%x \n", win->lines[i - win->begy]->hash);
141 __CTRACE(" 0x%x ", win->lines[i - win->begy]->flags);
142 for (j = 0; j < win->maxx; j++)
144 win->lines[i - win->begy]->line[j].ch);
146 for (j = 0; j < win->maxx; j++)
148 win->lines[i - win->begy]->line[j].attr);
154 for (wy = 0; wy < win->maxy; wy++) {
156 __CTRACE("%d\t%d\t%d\n",
157 wy, *win->lines[wy]->firstchp, *win->lines[wy]->lastchp);
160 curscr->lines[win->begy + wy]->hash = win->lines[wy]->hash;
161 if (win->lines[wy]->flags & (__ISDIRTY | __FORCEPAINT)) {
162 if (makech(win, wy) == ERR)
165 if (*win->lines[wy]->firstchp >= win->ch_off)
166 *win->lines[wy]->firstchp = win->maxx +
168 if (*win->lines[wy]->lastchp < win->maxx +
170 *win->lines[wy]->lastchp = win->ch_off;
171 if (*win->lines[wy]->lastchp <
172 *win->lines[wy]->firstchp) {
174 __CTRACE("wrefresh: line %d notdirty \n", wy);
176 win->lines[wy]->flags &= ~__ISDIRTY;
182 __CTRACE("\t%d\t%d\n", *win->lines[wy]->firstchp,
183 *win->lines[wy]->lastchp);
188 __CTRACE("refresh: ly=%d, lx=%d\n", ly, lx);
192 domvcur(ly, lx, win->cury, win->curx);
194 if (win->flags & __LEAVEOK) {
199 if (ly >= 0 && ly < win->maxy && lx >= 0 &&
204 win->cury = win->curx = 0;
206 domvcur(ly, lx, win->cury + win->begy,
207 win->curx + win->begx);
208 curscr->cury = win->cury + win->begy;
209 curscr->curx = win->curx + win->begx;
214 (void)fflush(stdout);
220 * Make a change on the screen.
224 register WINDOW *win;
227 static __LDATA blank = {' ', 0};
228 register int nlsp, clsp; /* Last space in lines. */
229 register int wx, lch, y;
230 register __LDATA *nsp, *csp, *cp, *cep;
234 /* Is the cursor still on the end of the last line? */
235 if (wy > 0 && win->lines[wy - 1]->flags & __ISPASTEOL) {
236 domvcur(ly, lx, ly + 1, 0);
240 wx = *win->lines[wy]->firstchp - win->ch_off;
243 else if (wx >= win->maxx)
245 lch = *win->lines[wy]->lastchp - win->ch_off;
248 else if (lch >= (int) win->maxx)
255 csp = &curscr->lines[wy + win->begy]->line[wx + win->begx];
257 nsp = &win->lines[wy]->line[wx];
258 force = win->lines[wy]->flags & __FORCEPAINT;
259 win->lines[wy]->flags &= ~__FORCEPAINT;
261 for (cp = &win->lines[wy]->line[win->maxx - 1];
262 cp->ch == ' ' && cp->attr == 0; cp--)
263 if (cp <= win->lines[wy]->line)
265 nlsp = cp - win->lines[wy]->line;
274 tputs(tgoto(CM, lx, ly), 1, __cputchar);
276 tputs(HO, 1, __cputchar);
277 __mvcur(0, 0, ly, lx, 1);
281 if (!force && memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
284 memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
294 domvcur(ly, lx, y, wx + win->begx);
297 __CTRACE("makech: 1: wx = %d, ly= %d, lx = %d, newy = %d, newx = %d, force =%d\n",
298 wx, ly, lx, y, wx + win->begx, force);
302 while ((force || memcmp(nsp, csp, sizeof(__LDATA)) != 0)
305 if (ce != NULL && win->maxx + win->begx ==
306 curscr->maxx && wx >= nlsp && nsp->ch == ' ' && nsp->attr == 0) {
307 /* Check for clear to end-of-line. */
308 cep = &curscr->lines[win->begy + wy]->line[win->begx + win->maxx - 1];
309 while (cep->ch == ' ' && cep->attr == 0)
312 clsp = cep - curscr->lines[win->begy + wy]->line -
315 __CTRACE("makech: clsp = %d, nlsp = %d\n", clsp, nlsp);
317 if ((clsp - nlsp >= strlen(CE)
318 && clsp < win->maxx) ||
319 wy == win->maxy - 1) {
321 __CTRACE("makech: using CE\n");
323 if (curscr->flags & __WSTANDOUT) {
324 tputs(SE, 0, __cputchar);
325 curscr->flags &= ~__WSTANDOUT;
327 tputs(CE, 1, __cputchar);
329 while (wx++ <= clsp) {
339 /* Enter/exit standout mode as appropriate. */
340 /* don't use simple ! here due to gcc -O bug */
341 if (SO && !!(nsp->attr & __STANDOUT) !=
342 !!(curscr->flags & __WSTANDOUT)
344 if (nsp->attr & __STANDOUT) {
345 tputs(SO, 0, __cputchar);
346 curscr->flags |= __WSTANDOUT;
348 tputs(SE, 0, __cputchar);
349 curscr->flags &= ~__WSTANDOUT;
354 if (wx >= win->maxx && wy == win->maxy - 1 && !curwin)
355 if (win->flags & __SCROLLOK) {
356 if (curscr->flags & __WSTANDOUT
357 && win->flags & __ENDLINE)
364 if (!(win->flags & __SCROLLWIN)) {
366 csp->attr = nsp->attr;
367 putchar(csp->ch = nsp->ch);
371 if (wx + win->begx < curscr->maxx) {
372 domvcur(ly, wx + win->begx,
373 win->begy + win->maxy - 1,
374 win->begx + win->maxx - 1);
376 ly = win->begy + win->maxy - 1;
377 lx = win->begx + win->maxx - 1;
380 if (wx < win->maxx || wy < win->maxy - 1 ||
381 !(win->flags & __SCROLLWIN)) {
383 csp->attr = nsp->attr;
384 putchar(csp->ch = nsp->ch);
390 __CTRACE("makech: putchar(%c)\n", nsp->ch & 0177);
392 if (UC && (nsp->attr & __STANDOUT)) {
394 tputs(UC, 0, __cputchar);
398 __CTRACE("makech: 2: wx = %d, lx = %d\n", wx, lx);
401 if (lx == wx + win->begx) /* If no change. */
404 if (lx >= COLS && AM)
406 else if (wx >= win->maxx) {
407 domvcur(ly, lx, ly, win->maxx + win->begx - 1);
408 lx = win->maxx + win->begx - 1;
412 __CTRACE("makech: 3: wx = %d, lx = %d\n", wx, lx);
420 * Do a mvcur, leaving standout mode if necessary.
423 domvcur(oy, ox, ny, nx)
426 if (curscr->flags & __WSTANDOUT && !MS) {
427 tputs(SE, 0, __cputchar);
428 curscr->flags &= ~__WSTANDOUT;
431 __mvcur(oy, ox, ny, nx, 1);
435 * Quickch() attempts to detect a pattern in the change of the window
436 * in order to optimize the change, e.g., scroll n lines as opposed to
437 * repainting the screen line by line.
444 #define THRESH (int) win->maxy / 4
446 register __LINE *clp, *tmp1, *tmp2;
447 register int bsize, curs, curw, starts, startw, i, j;
448 int n, target, cur_period, bot, top, sc_region;
453 * Find how many lines from the top of the screen are unchanged.
455 for (top = win->begy; top < win->begy + win->maxy; top++)
456 if (win->lines[top - win->begy]->flags & __FORCEPAINT ||
457 win->lines[top - win->begy]->hash != curscr->lines[top]->hash
458 || memcmp(win->lines[top - win->begy]->line,
459 curscr->lines[top]->line,
460 win->maxx * __LDATASIZE) != 0)
463 win->lines[top - win->begy]->flags &= ~__ISDIRTY;
465 * Find how many lines from bottom of screen are unchanged.
467 for (bot = win->begy + win->maxy - 1; bot >= (int) win->begy; bot--)
468 if (win->lines[bot - win->begy]->flags & __FORCEPAINT ||
469 win->lines[bot - win->begy]->hash != curscr->lines[bot]->hash
470 || memcmp(win->lines[bot - win->begy]->line,
471 curscr->lines[bot]->line,
472 win->maxx * __LDATASIZE) != 0)
475 win->lines[bot - win->begy]->flags &= ~__ISDIRTY;
479 * If we have a bottom unchanged region return. Scrolling the
480 * bottom region up and then back down causes a screen jitter.
481 * This will increase the number of characters sent to the screen
482 * but it looks better.
484 if (bot < (int) win->begy + win->maxy - 1)
486 #endif /* NO_JERKINESS */
489 * Search for the largest block of text not changed.
490 * Invariants of the loop:
491 * - Startw is the index of the beginning of the examined block in win.
492 * - Starts is the index of the beginning of the examined block in
494 * - Curw is the index of one past the end of the exmined block in win.
495 * - Curs is the index of one past the end of the exmined block in
497 * - bsize is the current size of the examined block.
499 for (bsize = bot - top; bsize >= THRESH; bsize--) {
500 for (startw = top; startw <= bot - bsize; startw++)
501 for (starts = top; starts <= bot - bsize;
503 for (curw = startw, curs = starts;
504 curs < starts + bsize; curw++, curs++)
505 if (win->lines[curw - win->begy]->flags &
507 (win->lines[curw - win->begy]->hash !=
508 curscr->lines[curs]->hash ||
509 memcmp(win->lines[curw - win->begy]->line,
510 curscr->lines[curs]->line,
511 win->maxx * __LDATASIZE) != 0))
513 if (curs == starts + bsize)
518 /* Did not find anything */
523 __CTRACE("quickch:bsize=%d,starts=%d,startw=%d,curw=%d,curs=%d,top=%d,bot=%d\n",
524 bsize, starts, startw, curw, curs, top, bot);
528 * Make sure that there is no overlap between the bottom and top
529 * regions and the middle scrolled block.
539 __CTRACE("#####################################\n");
540 for (i = 0; i < curscr->maxy; i++) {
541 __CTRACE("C: %d:", i);
542 __CTRACE(" 0x%x \n", curscr->lines[i]->hash);
543 for (j = 0; j < curscr->maxx; j++)
545 curscr->lines[i]->line[j].ch);
547 for (j = 0; j < curscr->maxx; j++)
549 curscr->lines[i]->line[j].attr);
551 if (i < win->begy || i > win->begy + win->maxy - 1)
553 __CTRACE("W: %d:", i - win->begy);
554 __CTRACE(" 0x%x \n", win->lines[i - win->begy]->hash);
555 __CTRACE(" 0x%x ", win->lines[i - win->begy]->flags);
556 for (j = 0; j < win->maxx; j++)
558 win->lines[i - win->begy]->line[j].ch);
560 for (j = 0; j < win->maxx; j++)
562 win->lines[i - win->begy]->line[j].attr);
567 /* So we don't have to call __hash() each time */
568 for (i = 0; i < win->maxx; i++) {
572 blank_hash = __hash((char *) buf, win->maxx * __LDATASIZE);
575 * Perform the rotation to maintain the consistency of curscr.
576 * This is hairy since we are doing an *in place* rotation.
577 * Invariants of the loop:
578 * - I is the index of the current line.
579 * - Target is the index of the target of line i.
580 * - Tmp1 points to current line (i).
581 * - Tmp2 and points to target line (target);
582 * - Cur_period is the index of the end of the current period.
585 * There are 2 major issues here that make this rotation non-trivial:
586 * 1. Scrolling in a scrolling region bounded by the top
587 * and bottom regions determined (whose size is sc_region).
588 * 2. As a result of the use of the mod function, there may be a
589 * period introduced, i.e., 2 maps to 4, 4 to 6, n-2 to 0, and
590 * 0 to 2, which then causes all odd lines not to be rotated.
591 * To remedy this, an index of the end ( = beginning) of the
592 * current 'period' is kept, cur_period, and when it is reached,
593 * the next period is started from cur_period + 1 which is
594 * guaranteed not to have been reached since that would mean that
595 * all records would have been reached. (think about it...).
597 * Lines in the rotation can have 3 attributes which are marked on the
598 * line so that curscr is consistent with the visual screen.
599 * 1. Not dirty -- lines inside the scrolled block, top region or
601 * 2. Blank lines -- lines in the differential of the scrolling
602 * region adjacent to top and bot regions
603 * depending on scrolling direction.
604 * 3. Dirty line -- all other lines are marked dirty.
606 sc_region = bot - top + 1;
608 tmp1 = curscr->lines[top];
610 for (j = top; j <= bot; j++) {
611 target = (i - top + n + sc_region) % sc_region + top;
612 tmp2 = curscr->lines[target];
613 curscr->lines[target] = tmp1;
614 /* Mark block as clean and blank out scrolled lines. */
615 clp = curscr->lines[target];
617 __CTRACE("quickch: n=%d startw=%d curw=%d i = %d target=%d ",
618 n, startw, curw, i, target);
620 if ((target >= startw && target < curw) || target < top
623 __CTRACE("-- notdirty");
625 win->lines[target - win->begy]->flags &= ~__ISDIRTY;
626 } else if ((n > 0 && target >= top && target < top + n) ||
627 (n < 0 && target <= bot && target > bot + n)) {
628 if (clp->hash != blank_hash || memcmp(clp->line,
629 buf, win->maxx * __LDATASIZE) !=0) {
630 (void)memcpy(clp->line, buf,
631 win->maxx * __LDATASIZE);
633 __CTRACE("-- blanked out: dirty");
635 clp->hash = blank_hash;
636 __touchline(win, target - win->begy, 0, win->maxx - 1, 0);
638 __touchline(win, target - win->begy, 0, win->maxx - 1, 0);
640 __CTRACE(" -- blank line already: dirty");
645 __CTRACE(" -- dirty");
647 __touchline(win, target - win->begy, 0, win->maxx - 1, 0);
652 if (target == cur_period) {
654 tmp1 = curscr->lines[i];
662 __CTRACE("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
663 for (i = 0; i < curscr->maxy; i++) {
664 __CTRACE("C: %d:", i);
665 for (j = 0; j < curscr->maxx; j++)
667 curscr->lines[i]->line[j].ch);
669 if (i < win->begy || i > win->begy + win->maxy - 1)
671 __CTRACE("W: %d:", i - win->begy);
672 for (j = 0; j < win->maxx; j++)
674 win->lines[i - win->begy]->line[j].ch);
680 scrolln(starts, startw, curs, bot, top);
682 * Need to repoint any subwindow lines to the rotated
685 for (wp = curscr->nextp; wp != curscr; wp = wp->nextp)
686 __set_subwin(wp->orig, wp);
692 * Scroll n lines, where n is starts - startw.
695 scrolln(starts, startw, curs, bot, top)
696 int starts, startw, curs, bot, top;
705 * When the scrolling region has been set, the cursor has to be at the
706 * last line of the region to make the scroll happen.
708 * Doing SF/SR or AL/DL appears faster on the screen than either sf/sr
709 * or al/dl, and, some terminals have AL/DL, sf/sr, and CS, but not
710 * SF/SR. So, if we're scrolling almost all of the screen, try and use
711 * AL/DL, otherwise use the scrolling region. The "almost all" is a
712 * shameless hack for vi.
715 if (__usecs) { /* Use change scroll region */
716 if (bot != curscr->maxy - 1 || top != 0)
717 __set_scroll_region(top, bot, ox, oy);
719 __mvcur(oy, ox, bot, 0, 1);
720 /* Scroll up the block */
721 if (SF != NULL && n > 1)
722 tputs(__tscroll(SF, n, 0), n, __cputchar);
724 /* newline seems faster than sf */
725 for(i = 0; i < n; i++)
727 tputs(NL, 1, __cputchar);
730 __mvcur(bot, 0, oy, ox, 1);
732 __mvcur(oy, ox, top, 0, 1);
733 /* Scroll the block down */
734 if (SR != NULL && (sr == NULL || -n > 1))
735 tputs(__tscroll(SR, -n, 0), -n, __cputchar);
737 for(i = n; i < 0; i++)
738 tputs(sr, 1, __cputchar);
739 __mvcur(top, 0, oy, ox, 1);
741 if (bot != curscr->maxy - 1 || top != 0)
742 __set_scroll_region(0, curscr->maxy - 1, ox, oy);
747 /* Scroll up the screen. */
748 if (((!DB && SF != NULL) || n == 1) && (bot == curscr->maxy - 1) && top == 0) {
749 __mvcur(oy, ox, curscr->maxy - 1, 0, 1);
752 tputs(__tscroll(SF, n, 0), n, __cputchar);
753 __mvcur(curscr->maxy - 1, 0, oy, ox, 1);
756 else if (DL != NULL && top != 0) {
757 __mvcur(oy, ox, top, 0, 1);
758 if (dl != NULL && n == 1)
760 tputs(__tscroll(DL, n, 0), n, __cputchar);
761 __mvcur(top, 0, bot - n + 1, 0, 1);
763 else if (dl != NULL && top != 0) {
764 __mvcur(oy, ox, top, 0, 1);
766 for (i = 0; i < n; i++)
767 tputs(dl, 1, __cputchar);
768 __mvcur(top, 0, bot - n + 1, 0, 1);
771 __mvcur(oy, ox, curscr->maxy - 1, 0, 1);
773 /* newline seems faster than sf */
774 for(i = 0; i < n; i++)
776 tputs(NL, 1, __cputchar);
779 if (bot == curscr->maxy - 1) {
780 __mvcur(curscr->maxy - 1, 0, oy, ox, 1);
783 __mvcur(curscr->maxy - 1, 0, bot - n + 1, 0, 1);
788 /* Push down the bottom region. */
790 if (al != NULL && n == 1)
792 tputs(__tscroll(AL, n, 0), n, __cputchar);
794 else if (al != NULL) {
796 for (i = 0; i < n; i++)
797 tputs(al, 1, __cputchar);
801 __mvcur(bot - n + 1, 0, oy, ox, 1);
807 /* Scroll down the screen. */
808 if (!DA && (SR != NULL || sr != NULL) && bot == curscr->maxy - 1 && top == 0) {
809 __mvcur(oy, ox, 0, 0, 1);
810 if (SR == NULL || (sr != NULL && -n == 1)) {
811 for (i = n; i < 0; i++)
812 tputs(sr, 1, __cputchar);
814 tputs(__tscroll(SR, -n, 0), -n, __cputchar);
815 __mvcur(0, 0, oy, ox, 1);
818 else if (DL != NULL) {
819 __mvcur(oy, ox, bot + n + 1, 0, 1);
820 if (dl != NULL && -n == 1)
822 tputs(__tscroll(DL, -n, 0), -n, __cputchar);
823 __mvcur(bot + n + 1, 0, top, 0, 1);
825 else if (dl != NULL) {
826 __mvcur(oy, ox, bot + n + 1, 0, 1);
828 for (i = n; i < 0; i++)
829 tputs(dl, 1, __cputchar);
830 __mvcur(bot + n + 1, 0, top, 0, 1);
835 /* Scroll the block down. */
837 if (al != NULL && -n == 1)
839 tputs(__tscroll(AL, -n, 0), -n, __cputchar);
841 else if (al != NULL) {
843 for (i = n; i < 0; i++)
844 tputs(al, 1, __cputchar);
848 __mvcur(top, 0, oy, ox, 1);