1 /* $NetBSD: wwupdate.c,v 1.6 2003/08/07 11:17:46 agc Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Edward Wang at The University of California, Berkeley.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
38 static char sccsid[] = "@(#)wwupdate.c 8.1 (Berkeley) 6/6/93";
40 __RCSID("$NetBSD: wwupdate.c,v 1.6 2003/08/07 11:17:46 agc Exp $");
49 wwupdate1(int top, int bot)
54 struct ww_update *upd;
55 char check_clreos = 0;
56 int scan_top, scan_bot;
60 char *t1 = wwtouched + top, *t2 = wwtouched + bot;
68 scan_top = top = t1 - wwtouched - 1;
69 scan_bot = bot = t2 - wwtouched + 1;
70 if (scan_bot - scan_top > 1 &&
71 (tt.tt_clreos != 0 || tt.tt_clear != 0)) {
72 int st = tt.tt_clreos != 0 ? scan_top : 0;
75 * t1 is one past the first touched row,
76 * t2 is on the last touched row.
78 for (t1--, n = 1; t1 < t2;)
82 * If we can't clreos then we try for clearing
85 if ((check_clreos = n * 10 > (wwnrow - st) * 9)) {
91 if (tt.tt_clreol == 0 && !check_clreos)
93 for (i = scan_top, touched = &wwtouched[i], upd = &wwupd[i];
95 i++, touched++, upd++) {
99 union ww_char *ns, *os;
103 if (!check_clreos && !*touched)
111 * The cost of clearing is:
113 * The cost of straight update is, more or less:
115 * We clear if nblank - nsame > X
116 * X is the clreol overhead.
117 * So we make gain = nblank - nsame.
119 if ((--ns)->c_w == (--os)->c_w)
125 if (gain > best_gain) {
130 upd->best_gain = best_gain;
131 upd->best_col = best_col;
143 * gain is the advantage of clearing all the lines.
144 * best_gain is the advantage of clearing to eos
145 * at best_row and u->best_col.
146 * simple_gain is the advantage of using only clreol.
147 * We use g > best_gain because u->best_col can be
148 * undefined when u->best_gain is 0 so we can't use it.
150 for (j = scan_bot - 1, u = wwupd + j; j >= top; j--, u--) {
151 int g = gain + u->best_gain;
158 if (tt.tt_clreol != 0 && u->best_gain > 4)
159 simple_gain += u->best_gain - 4;
161 if (tt.tt_clreos == 0) {
162 if (gain > simple_gain && gain > 4) {
170 if (best_gain > simple_gain && best_gain > 4) {
172 xxclreos(i, j = wwupd[i].best_col);
178 wwnupdclreosline += wwnrow - i;
180 while (i < scan_bot) {
181 union ww_char *os = &wwos[i][j];
183 for (j = wwncol - j; --j >= 0;)
185 wwtouched[i++] |= WWU_TOUCHED;
193 for (i = top, touched = &wwtouched[i], upd = &wwupd[i]; i < bot;
194 i++, touched++, upd++) {
195 union ww_char *os, *ns;
203 if (tt.tt_clreol != 0 && upd->best_gain > 4) {
205 xxclreol(i, j = upd->best_col);
206 for (os = &wwos[i][j], j = wwncol - j; --j >= 0;)
212 for (j = 0; j < wwncol;) {
217 char buf[512]; /* > wwncol */
220 for (; j++ < wwncol && ns++->c_w == os++->c_w;)
231 while (j < wwncol && ns->c_m == m) {
233 if (ns->c_w == os->c_w) {
247 if (!wwwrap || i != wwnrow - 1 || c + n != wwncol)
248 xxwrite(i, c, buf, n, m);
249 else if (tt.tt_inschar || tt.tt_insspace) {
255 xxwrite(i, c, buf, n, m);
258 xxinschar(i, c, ns[-2].c_c,
262 xxwrite(i, c, &ns[-2].c_c, 1,
267 xxwrite(i, c, buf, n, m);
269 *touched = WWU_TOUCHED;