2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Edward Wang at The University of California, Berkeley.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * @(#)wwupdate.c 8.1 (Berkeley) 6/6/93
37 * $FreeBSD: src/usr.bin/window/wwupdate.c,v 1.1.1.1.14.1 2001/05/17 09:45:02 obrien Exp $
38 * $DragonFly: src/usr.bin/window/wwupdate.c,v 1.2 2003/06/17 04:29:34 dillon Exp $
49 struct ww_update
*upd
;
50 char check_clreos
= 0;
51 int scan_top
, scan_bot
;
55 register char *t1
= wwtouched
+ top
, *t2
= wwtouched
+ bot
;
63 scan_top
= top
= t1
- wwtouched
- 1;
64 scan_bot
= bot
= t2
- wwtouched
+ 1;
65 if (scan_bot
- scan_top
> 1 &&
66 (tt
.tt_clreos
!= 0 || tt
.tt_clear
!= 0)) {
67 int st
= tt
.tt_clreos
!= 0 ? scan_top
: 0;
70 * t1 is one past the first touched row,
71 * t2 is on the last touched row.
73 for (t1
--, n
= 1; t1
< t2
;)
77 * If we can't clreos then we try for clearing
80 if (check_clreos
= n
* 10 > (wwnrow
- st
) * 9) {
86 if (tt
.tt_clreol
== 0 && !check_clreos
)
88 for (i
= scan_top
, touched
= &wwtouched
[i
], upd
= &wwupd
[i
];
90 i
++, touched
++, upd
++) {
92 register best_gain
= 0;
94 register union ww_char
*ns
, *os
;
98 if (!check_clreos
&& !*touched
)
106 * The cost of clearing is:
108 * The cost of straight update is, more or less:
110 * We clear if nblank - nsame > X
111 * X is the clreol overhead.
112 * So we make gain = nblank - nsame.
114 if ((--ns
)->c_w
== (--os
)->c_w
)
120 if (gain
> best_gain
) {
125 upd
->best_gain
= best_gain
;
126 upd
->best_col
= best_col
;
130 register struct ww_update
*u
;
132 register best_gain
= 0;
134 register simple_gain
= 0;
138 * gain is the advantage of clearing all the lines.
139 * best_gain is the advantage of clearing to eos
140 * at best_row and u->best_col.
141 * simple_gain is the advantage of using only clreol.
142 * We use g > best_gain because u->best_col can be
143 * undefined when u->best_gain is 0 so we can't use it.
145 for (j
= scan_bot
- 1, u
= wwupd
+ j
; j
>= top
; j
--, u
--) {
146 register g
= gain
+ u
->best_gain
;
153 if (tt
.tt_clreol
!= 0 && u
->best_gain
> 4)
154 simple_gain
+= u
->best_gain
- 4;
156 if (tt
.tt_clreos
== 0) {
157 if (gain
> simple_gain
&& gain
> 4) {
165 if (best_gain
> simple_gain
&& best_gain
> 4) {
167 xxclreos(i
, j
= wwupd
[i
].best_col
);
173 wwnupdclreosline
+= wwnrow
- i
;
175 while (i
< scan_bot
) {
176 register union ww_char
*os
= &wwos
[i
][j
];
178 for (j
= wwncol
- j
; --j
>= 0;)
180 wwtouched
[i
++] |= WWU_TOUCHED
;
188 for (i
= top
, touched
= &wwtouched
[i
], upd
= &wwupd
[i
]; i
< bot
;
189 i
++, touched
++, upd
++) {
190 register union ww_char
*os
, *ns
;
198 if (tt
.tt_clreol
!= 0 && upd
->best_gain
> 4) {
200 xxclreol(i
, j
= upd
->best_col
);
201 for (os
= &wwos
[i
][j
], j
= wwncol
- j
; --j
>= 0;)
207 for (j
= 0; j
< wwncol
;) {
208 register char *p
, *q
;
212 char buf
[512]; /* > wwncol */
215 for (; j
++ < wwncol
&& ns
++->c_w
== os
++->c_w
;)
226 while (j
< wwncol
&& ns
->c_m
== m
) {
228 if (ns
->c_w
== os
->c_w
) {
242 if (!wwwrap
|| i
!= wwnrow
- 1 || c
+ n
!= wwncol
)
243 xxwrite(i
, c
, buf
, n
, m
);
244 else if (tt
.tt_inschar
|| tt
.tt_insspace
) {
250 xxwrite(i
, c
, buf
, n
, m
);
253 xxinschar(i
, c
, ns
[-2].c_c
,
257 xxwrite(i
, c
, &ns
[-2].c_c
, 1,
262 xxwrite(i
, c
, buf
, n
, m
);
264 *touched
= WWU_TOUCHED
;