1 /*	$OpenBSD: wwupdate.c,v 1.5 2003/06/03 02:56:23 millert Exp $	*/
2 /*	$NetBSD: wwupdate.c,v 1.3 1995/09/28 10:36:00 tls 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 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)wwupdate.c	8.1 (Berkeley) 6/6/93";
39 #else
40 static char rcsid[] = "$OpenBSD: wwupdate.c,v 1.5 2003/06/03 02:56:23 millert Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #include "ww.h"
45 #include "tt.h"
46 
wwupdate1(top,bot)47 wwupdate1(top, bot)
48 {
49 	int i;
50 	int j;
51 	char *touched;
52 	struct ww_update *upd;
53 	char check_clreos = 0;
54 	int scan_top, scan_bot;
55 
56 	wwnupdate++;
57 	{
58 		char *t1 = wwtouched + top, *t2 = wwtouched + bot;
59 		int n;
60 
61 		while (!*t1++)
62 			if (t1 == t2)
63 				return;
64 		while (!*--t2)
65 			;
66 		scan_top = top = t1 - wwtouched - 1;
67 		scan_bot = bot = t2 - wwtouched + 1;
68 		if (scan_bot - scan_top > 1 &&
69 		    (tt.tt_clreos != 0 || tt.tt_clear != 0)) {
70 			int st = tt.tt_clreos != 0 ? scan_top : 0;
71 
72 			/*
73 			 * t1 is one past the first touched row,
74 			 * t2 is on the last touched row.
75 			 */
76 			for (t1--, n = 1; t1 < t2;)
77 				if (*t1++)
78 					n++;
79 			/*
80 			 * If we can't clreos then we try for clearing
81 			 * the whole screen.
82 			 */
83 			if (check_clreos = n * 10 > (wwnrow - st) * 9) {
84 				scan_top = st;
85 				scan_bot = wwnrow;
86 			}
87 		}
88 	}
89 	if (tt.tt_clreol == 0 && !check_clreos)
90 		goto simple;
91 	for (i = scan_top, touched = &wwtouched[i], upd = &wwupd[i];
92 	     i < scan_bot;
93 	     i++, touched++, upd++) {
94 		int gain = 0;
95 		int best_gain = 0;
96 		int best_col;
97 		union ww_char *ns, *os;
98 
99 		if (wwinterrupt())
100 			return;
101 		if (!check_clreos && !*touched)
102 			continue;
103 		wwnupdscan++;
104 		j = wwncol;
105 		ns = &wwns[i][j];
106 		os = &wwos[i][j];
107 		while (--j >= 0) {
108 			/*
109 			 * The cost of clearing is:
110 			 *	ncol - nblank + X
111 			 * The cost of straight update is, more or less:
112 			 *	ncol - nsame
113 			 * We clear if  nblank - nsame > X
114 			 * X is the clreol overhead.
115 			 * So we make gain = nblank - nsame.
116 			 */
117 			if ((--ns)->c_w == (--os)->c_w)
118 				gain--;
119 			else
120 				best_gain--;
121 			if (ns->c_w == ' ')
122 				gain++;
123 			if (gain > best_gain) {
124 				best_col = j;
125 				best_gain = gain;
126 			}
127 		}
128 		upd->best_gain = best_gain;
129 		upd->best_col = best_col;
130 		upd->gain = gain;
131 	}
132 	if (check_clreos) {
133 		struct ww_update *u;
134 		int gain = 0;
135 		int best_gain = 0;
136 		int best_row;
137 		int simple_gain = 0;
138 		char didit = 0;
139 
140 		/*
141 		 * gain is the advantage of clearing all the lines.
142 		 * best_gain is the advantage of clearing to eos
143 		 * at best_row and u->best_col.
144 		 * simple_gain is the advantage of using only clreol.
145 		 * We use g > best_gain because u->best_col can be
146 		 * undefined when u->best_gain is 0 so we can't use it.
147 		 */
148 		for (j = scan_bot - 1, u = wwupd + j; j >= top; j--, u--) {
149 			int g = gain + u->best_gain;
150 
151 			if (g > best_gain) {
152 				best_gain = g;
153 				best_row = j;
154 			}
155 			gain += u->gain;
156 			if (tt.tt_clreol != 0 && u->best_gain > 4)
157 				simple_gain += u->best_gain - 4;
158 		}
159 		if (tt.tt_clreos == 0) {
160 			if (gain > simple_gain && gain > 4) {
161 				xxclear();
162 				i = top = scan_top;
163 				bot = scan_bot;
164 				j = 0;
165 				didit = 1;
166 			}
167 		} else
168 			if (best_gain > simple_gain && best_gain > 4) {
169 				i = best_row;
170 				xxclreos(i, j = wwupd[i].best_col);
171 				bot = scan_bot;
172 				didit = 1;
173 			}
174 		if (didit) {
175 			wwnupdclreos++;
176 			wwnupdclreosline += wwnrow - i;
177 			u = wwupd + i;
178 			while (i < scan_bot) {
179 				union ww_char *os = &wwos[i][j];
180 
181 				for (j = wwncol - j; --j >= 0;)
182 					os++->c_w = ' ';
183 				wwtouched[i++] |= WWU_TOUCHED;
184 				u++->best_gain = 0;
185 				j = 0;
186 			}
187 		} else
188 			wwnupdclreosmiss++;
189 	}
190 simple:
191 	for (i = top, touched = &wwtouched[i], upd = &wwupd[i]; i < bot;
192 	     i++, touched++, upd++) {
193 		union ww_char *os, *ns;
194 		char didit;
195 
196 		if (!*touched)
197 			continue;
198 		*touched = 0;
199 		wwnupdline++;
200 		didit = 0;
201 		if (tt.tt_clreol != 0 && upd->best_gain > 4) {
202 			wwnupdclreol++;
203 			xxclreol(i, j = upd->best_col);
204 			for (os = &wwos[i][j], j = wwncol - j; --j >= 0;)
205 				os++->c_w = ' ';
206 			didit = 1;
207 		}
208 		ns = wwns[i];
209 		os = wwos[i];
210 		for (j = 0; j < wwncol;) {
211 			char *p, *q;
212 			char m;
213 			int c;
214 			int n;
215 			char buf[512];			/* > wwncol */
216 			union ww_char lastc;
217 
218 			for (; j++ < wwncol && ns++->c_w == os++->c_w;)
219 				;
220 			if (j > wwncol)
221 				break;
222 			p = buf;
223 			m = ns[-1].c_m;
224 			c = j - 1;
225 			os[-1] = ns[-1];
226 			*p++ = ns[-1].c_c;
227 			n = 5;
228 			q = p;
229 			while (j < wwncol && ns->c_m == m) {
230 				*p++ = ns->c_c;
231 				if (ns->c_w == os->c_w) {
232 					if (--n <= 0)
233 						break;
234 					os++;
235 					ns++;
236 				} else {
237 					n = 5;
238 					q = p;
239 					lastc = *os;
240 					*os++ = *ns++;
241 				}
242 				j++;
243 			}
244 			n = q - buf;
245 			if (!wwwrap || i != wwnrow - 1 || c + n != wwncol)
246 				xxwrite(i, c, buf, n, m);
247 			else if (tt.tt_inschar || tt.tt_insspace) {
248 				if (n > 1) {
249 					q[-2] = q[-1];
250 					n--;
251 				} else
252 					c--;
253 				xxwrite(i, c, buf, n, m);
254 				c += n - 1;
255 				if (tt.tt_inschar)
256 					xxinschar(i, c, ns[-2].c_c,
257 						ns[-2].c_m);
258 				else {
259 					xxinsspace(i, c);
260 					xxwrite(i, c, &ns[-2].c_c, 1,
261 						ns[-2].c_m);
262 				}
263 			} else {
264 				if (--n)
265 					xxwrite(i, c, buf, n, m);
266 				os[-1] = lastc;
267 				*touched = WWU_TOUCHED;
268 			}
269 			didit = 1;
270 		}
271 		if (!didit)
272 			wwnupdmiss++;
273 	}
274 }
275