Annotation of src/usr.bin/window/wwupdate.c, Revision 1.1
1.1 ! deraadt 1: /* $NetBSD: wwupdate.c,v 1.3 1995/09/28 10:36:00 tls Exp $ */
! 2:
! 3: /*
! 4: * Copyright (c) 1983, 1993
! 5: * The Regents of the University of California. All rights reserved.
! 6: *
! 7: * This code is derived from software contributed to Berkeley by
! 8: * Edward Wang at The University of California, Berkeley.
! 9: *
! 10: * Redistribution and use in source and binary forms, with or without
! 11: * modification, are permitted provided that the following conditions
! 12: * are met:
! 13: * 1. Redistributions of source code must retain the above copyright
! 14: * notice, this list of conditions and the following disclaimer.
! 15: * 2. Redistributions in binary form must reproduce the above copyright
! 16: * notice, this list of conditions and the following disclaimer in the
! 17: * documentation and/or other materials provided with the distribution.
! 18: * 3. All advertising materials mentioning features or use of this software
! 19: * must display the following acknowledgement:
! 20: * This product includes software developed by the University of
! 21: * California, Berkeley and its contributors.
! 22: * 4. Neither the name of the University nor the names of its contributors
! 23: * may be used to endorse or promote products derived from this software
! 24: * without specific prior written permission.
! 25: *
! 26: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
! 27: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 28: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 29: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
! 30: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 31: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 32: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 33: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 34: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 35: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 36: * SUCH DAMAGE.
! 37: */
! 38:
! 39: #ifndef lint
! 40: #if 0
! 41: static char sccsid[] = "@(#)wwupdate.c 8.1 (Berkeley) 6/6/93";
! 42: #else
! 43: static char rcsid[] = "$NetBSD: wwupdate.c,v 1.3 1995/09/28 10:36:00 tls Exp $";
! 44: #endif
! 45: #endif /* not lint */
! 46:
! 47: #include "ww.h"
! 48: #include "tt.h"
! 49:
! 50: wwupdate1(top, bot)
! 51: {
! 52: int i;
! 53: register j;
! 54: char *touched;
! 55: struct ww_update *upd;
! 56: char check_clreos = 0;
! 57: int scan_top, scan_bot;
! 58:
! 59: wwnupdate++;
! 60: {
! 61: register char *t1 = wwtouched + top, *t2 = wwtouched + bot;
! 62: register n;
! 63:
! 64: while (!*t1++)
! 65: if (t1 == t2)
! 66: return;
! 67: while (!*--t2)
! 68: ;
! 69: scan_top = top = t1 - wwtouched - 1;
! 70: scan_bot = bot = t2 - wwtouched + 1;
! 71: if (scan_bot - scan_top > 1 &&
! 72: (tt.tt_clreos != 0 || tt.tt_clear != 0)) {
! 73: int st = tt.tt_clreos != 0 ? scan_top : 0;
! 74:
! 75: /*
! 76: * t1 is one past the first touched row,
! 77: * t2 is on the last touched row.
! 78: */
! 79: for (t1--, n = 1; t1 < t2;)
! 80: if (*t1++)
! 81: n++;
! 82: /*
! 83: * If we can't clreos then we try for clearing
! 84: * the whole screen.
! 85: */
! 86: if (check_clreos = n * 10 > (wwnrow - st) * 9) {
! 87: scan_top = st;
! 88: scan_bot = wwnrow;
! 89: }
! 90: }
! 91: }
! 92: if (tt.tt_clreol == 0 && !check_clreos)
! 93: goto simple;
! 94: for (i = scan_top, touched = &wwtouched[i], upd = &wwupd[i];
! 95: i < scan_bot;
! 96: i++, touched++, upd++) {
! 97: register gain = 0;
! 98: register best_gain = 0;
! 99: register best_col;
! 100: register union ww_char *ns, *os;
! 101:
! 102: if (wwinterrupt())
! 103: return;
! 104: if (!check_clreos && !*touched)
! 105: continue;
! 106: wwnupdscan++;
! 107: j = wwncol;
! 108: ns = &wwns[i][j];
! 109: os = &wwos[i][j];
! 110: while (--j >= 0) {
! 111: /*
! 112: * The cost of clearing is:
! 113: * ncol - nblank + X
! 114: * The cost of straight update is, more or less:
! 115: * ncol - nsame
! 116: * We clear if nblank - nsame > X
! 117: * X is the clreol overhead.
! 118: * So we make gain = nblank - nsame.
! 119: */
! 120: if ((--ns)->c_w == (--os)->c_w)
! 121: gain--;
! 122: else
! 123: best_gain--;
! 124: if (ns->c_w == ' ')
! 125: gain++;
! 126: if (gain > best_gain) {
! 127: best_col = j;
! 128: best_gain = gain;
! 129: }
! 130: }
! 131: upd->best_gain = best_gain;
! 132: upd->best_col = best_col;
! 133: upd->gain = gain;
! 134: }
! 135: if (check_clreos) {
! 136: register struct ww_update *u;
! 137: register gain = 0;
! 138: register best_gain = 0;
! 139: int best_row;
! 140: register simple_gain = 0;
! 141: char didit = 0;
! 142:
! 143: /*
! 144: * gain is the advantage of clearing all the lines.
! 145: * best_gain is the advantage of clearing to eos
! 146: * at best_row and u->best_col.
! 147: * simple_gain is the advantage of using only clreol.
! 148: * We use g > best_gain because u->best_col can be
! 149: * undefined when u->best_gain is 0 so we can't use it.
! 150: */
! 151: for (j = scan_bot - 1, u = wwupd + j; j >= top; j--, u--) {
! 152: register g = gain + u->best_gain;
! 153:
! 154: if (g > best_gain) {
! 155: best_gain = g;
! 156: best_row = j;
! 157: }
! 158: gain += u->gain;
! 159: if (tt.tt_clreol != 0 && u->best_gain > 4)
! 160: simple_gain += u->best_gain - 4;
! 161: }
! 162: if (tt.tt_clreos == 0) {
! 163: if (gain > simple_gain && gain > 4) {
! 164: xxclear();
! 165: i = top = scan_top;
! 166: bot = scan_bot;
! 167: j = 0;
! 168: didit = 1;
! 169: }
! 170: } else
! 171: if (best_gain > simple_gain && best_gain > 4) {
! 172: i = best_row;
! 173: xxclreos(i, j = wwupd[i].best_col);
! 174: bot = scan_bot;
! 175: didit = 1;
! 176: }
! 177: if (didit) {
! 178: wwnupdclreos++;
! 179: wwnupdclreosline += wwnrow - i;
! 180: u = wwupd + i;
! 181: while (i < scan_bot) {
! 182: register union ww_char *os = &wwos[i][j];
! 183:
! 184: for (j = wwncol - j; --j >= 0;)
! 185: os++->c_w = ' ';
! 186: wwtouched[i++] |= WWU_TOUCHED;
! 187: u++->best_gain = 0;
! 188: j = 0;
! 189: }
! 190: } else
! 191: wwnupdclreosmiss++;
! 192: }
! 193: simple:
! 194: for (i = top, touched = &wwtouched[i], upd = &wwupd[i]; i < bot;
! 195: i++, touched++, upd++) {
! 196: register union ww_char *os, *ns;
! 197: char didit;
! 198:
! 199: if (!*touched)
! 200: continue;
! 201: *touched = 0;
! 202: wwnupdline++;
! 203: didit = 0;
! 204: if (tt.tt_clreol != 0 && upd->best_gain > 4) {
! 205: wwnupdclreol++;
! 206: xxclreol(i, j = upd->best_col);
! 207: for (os = &wwos[i][j], j = wwncol - j; --j >= 0;)
! 208: os++->c_w = ' ';
! 209: didit = 1;
! 210: }
! 211: ns = wwns[i];
! 212: os = wwos[i];
! 213: for (j = 0; j < wwncol;) {
! 214: register char *p, *q;
! 215: char m;
! 216: int c;
! 217: register n;
! 218: char buf[512]; /* > wwncol */
! 219: union ww_char lastc;
! 220:
! 221: for (; j++ < wwncol && ns++->c_w == os++->c_w;)
! 222: ;
! 223: if (j > wwncol)
! 224: break;
! 225: p = buf;
! 226: m = ns[-1].c_m;
! 227: c = j - 1;
! 228: os[-1] = ns[-1];
! 229: *p++ = ns[-1].c_c;
! 230: n = 5;
! 231: q = p;
! 232: while (j < wwncol && ns->c_m == m) {
! 233: *p++ = ns->c_c;
! 234: if (ns->c_w == os->c_w) {
! 235: if (--n <= 0)
! 236: break;
! 237: os++;
! 238: ns++;
! 239: } else {
! 240: n = 5;
! 241: q = p;
! 242: lastc = *os;
! 243: *os++ = *ns++;
! 244: }
! 245: j++;
! 246: }
! 247: n = q - buf;
! 248: if (!wwwrap || i != wwnrow - 1 || c + n != wwncol)
! 249: xxwrite(i, c, buf, n, m);
! 250: else if (tt.tt_inschar || tt.tt_insspace) {
! 251: if (n > 1) {
! 252: q[-2] = q[-1];
! 253: n--;
! 254: } else
! 255: c--;
! 256: xxwrite(i, c, buf, n, m);
! 257: c += n - 1;
! 258: if (tt.tt_inschar)
! 259: xxinschar(i, c, ns[-2].c_c,
! 260: ns[-2].c_m);
! 261: else {
! 262: xxinsspace(i, c);
! 263: xxwrite(i, c, &ns[-2].c_c, 1,
! 264: ns[-2].c_m);
! 265: }
! 266: } else {
! 267: if (--n)
! 268: xxwrite(i, c, buf, n, m);
! 269: os[-1] = lastc;
! 270: *touched = WWU_TOUCHED;
! 271: }
! 272: didit = 1;
! 273: }
! 274: if (!didit)
! 275: wwnupdmiss++;
! 276: }
! 277: }