fixed up ellipse, circle and RoundEnd routines for speed
[unix-history] / usr / src / usr.bin / window / wwupdate.c
CommitLineData
bbb8329f 1#ifndef lint
6e3b31ff 2static char *sccsid = "@(#)wwupdate.c 3.11 84/01/06";
bbb8329f
EW
3#endif
4
5#include "ww.h"
e908bfac 6#include "tt.h"
bbb8329f
EW
7
8wwupdate()
9{
b7f39bc5
EW
10 int i;
11 register j;
bbb8329f 12 register union ww_char *ns, *os;
b7f39bc5 13 char *touched;
6e3b31ff 14 char didit;
bbb8329f 15
861cd1ed 16 wwnupdate++;
861cd1ed
EW
17 for (i = 0, touched = wwtouched; i < wwnrow; i++, touched++) {
18 if (!*touched)
19 continue;
6e3b31ff
EW
20 if (*touched & WWU_MAJOR && tt.tt_clreol != 0) {
21 register gain = 0;
22 register best_gain = 0;
23 register best;
b7f39bc5 24
04d70db4
EW
25 wwnmajline++;
26 j = wwncol;
6e3b31ff
EW
27 ns = &wwns[i][j];
28 os = &wwos[i][j];
04d70db4 29 while (--j >= 0) {
6e3b31ff
EW
30 /*
31 * The cost of clearing is:
32 * ncol - nblank + X
33 * The cost of straight update is:
34 * ncol - nsame
35 * We clear if: nblank - nsame > X
36 * X is the clreol overhead.
37 * So we make gain = nblank - nsame.
38 */
39 if ((--ns)->c_w == (--os)->c_w)
40 gain--;
41 else
42 best_gain--;
43 if (ns->c_w == ' ')
44 gain++;
45 if (gain >= best_gain) {
46 best = j;
47 best_gain = gain;
48 }
04d70db4 49 }
6e3b31ff
EW
50 if (best_gain > 4) {
51 (*tt.tt_move)(i, best);
04d70db4 52 (*tt.tt_clreol)();
6e3b31ff
EW
53 for (j = wwncol - best, os = &wwos[i][best];
54 --j >= 0;)
04d70db4
EW
55 os++->c_w = ' ';
56 } else
57 wwnmajmiss++;
58 }
59 wwnupdline++;
60 didit = 0;
bbb8329f
EW
61 ns = wwns[i];
62 os = wwos[i];
b7f39bc5 63 for (j = 0; j < wwncol;) {
6e3b31ff
EW
64 register char *p, *q;
65 char m;
66 int c;
67 register n;
68 char buf[512]; /* > wwncol */
69 union ww_char lastc;
70
b7f39bc5
EW
71 for (; j++ < wwncol && ns++->c_w == os++->c_w;)
72 ;
73 if (j > wwncol)
74 break;
75 p = buf;
3130283e 76 m = ns[-1].c_m & tt.tt_availmodes;
b7f39bc5
EW
77 c = j - 1;
78 os[-1] = ns[-1];
79 *p++ = ns[-1].c_c;
6e3b31ff 80 n = 5;
b7f39bc5 81 q = p;
3130283e 82 while (j < wwncol && (ns->c_m&tt.tt_availmodes) == m) {
b7f39bc5
EW
83 *p++ = ns->c_c;
84 if (ns->c_w == os->c_w) {
6e3b31ff 85 if (--n <= 0)
b7f39bc5
EW
86 break;
87 os++;
88 ns++;
89 } else {
6e3b31ff 90 n = 5;
b7f39bc5 91 q = p;
5e785082 92 lastc = *os;
b7f39bc5
EW
93 *os++ = *ns++;
94 }
95 j++;
bbb8329f 96 }
3130283e 97 tt.tt_nmodes = m;
5e785082
EW
98 if (wwwrap
99 && i == wwnrow - 1 && q - buf + c == wwncol) {
3130283e 100 if (tt.tt_hasinsert) {
594208de
EW
101 if (q - buf != 1) {
102 (*tt.tt_move)(i, c);
103 (*tt.tt_write)(buf + 1,
104 q - buf - 1);
105 (*tt.tt_move)(i, c);
106 tt.tt_ninsert = 1;
107 (*tt.tt_write)(buf, 1);
108 tt.tt_ninsert = 0;
109 } else {
110 (*tt.tt_move)(i, c - 1);
111 (*tt.tt_write)(buf, 1);
112 tt.tt_nmodes = ns[-2].c_m;
113 (*tt.tt_move)(i, c - 1);
114 tt.tt_ninsert = 1;
115 (*tt.tt_write)(&ns[-2].c_c, 1);
116 tt.tt_ninsert = 0;
117 }
5e785082 118 } else {
594208de 119 (*tt.tt_move)(i, c);
5e785082 120 os[-1] = lastc;
3130283e 121 (*tt.tt_write)(buf, q - buf - 1);
5e785082 122 }
594208de
EW
123 } else {
124 (*tt.tt_move)(i, c);
3130283e 125 (*tt.tt_write)(buf, q - buf);
594208de 126 }
b7f39bc5 127 didit++;
bbb8329f 128 }
861cd1ed 129 if (!didit)
04d70db4
EW
130 wwnupdmiss++;
131 *touched = 0;
bbb8329f
EW
132 }
133}