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