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