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