Commit | Line | Data |
---|---|---|
60de5df9 | 1 | /* |
46e9ea25 KB |
2 | * Copyright (c) 1983 Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
6 | * provided that this notice is preserved and that due credit is given | |
7 | * to the University of California at Berkeley. The name of the University | |
8 | * may not be used to endorse or promote products derived from this | |
9 | * software without specific prior written permission. This software | |
10 | * is provided ``as is'' without express or implied warranty. | |
60de5df9 EW |
11 | */ |
12 | ||
46e9ea25 KB |
13 | #ifndef lint |
14 | static char sccsid[] = "@(#)wwupdate.c 3.18 (Berkeley) %G%"; | |
15 | #endif /* not lint */ | |
16 | ||
bbb8329f | 17 | #include "ww.h" |
e908bfac | 18 | #include "tt.h" |
bbb8329f | 19 | |
b1189050 | 20 | wwupdate1(top, bot) |
bbb8329f | 21 | { |
b7f39bc5 EW |
22 | int i; |
23 | register j; | |
bbb8329f | 24 | register union ww_char *ns, *os; |
b7f39bc5 | 25 | char *touched; |
6e3b31ff | 26 | char didit; |
bbb8329f | 27 | |
861cd1ed | 28 | wwnupdate++; |
b1189050 EW |
29 | for (i = top, touched = &wwtouched[i]; i < bot && !wwinterrupt(); |
30 | i++, touched++) { | |
861cd1ed EW |
31 | if (!*touched) |
32 | continue; | |
6e3b31ff EW |
33 | if (*touched & WWU_MAJOR && tt.tt_clreol != 0) { |
34 | register gain = 0; | |
35 | register best_gain = 0; | |
36 | register best; | |
b7f39bc5 | 37 | |
04d70db4 EW |
38 | wwnmajline++; |
39 | j = wwncol; | |
6e3b31ff EW |
40 | ns = &wwns[i][j]; |
41 | os = &wwos[i][j]; | |
04d70db4 | 42 | while (--j >= 0) { |
6e3b31ff EW |
43 | /* |
44 | * The cost of clearing is: | |
45 | * ncol - nblank + X | |
46 | * The cost of straight update is: | |
47 | * ncol - nsame | |
48 | * We clear if: nblank - nsame > X | |
49 | * X is the clreol overhead. | |
50 | * So we make gain = nblank - nsame. | |
51 | */ | |
52 | if ((--ns)->c_w == (--os)->c_w) | |
53 | gain--; | |
54 | else | |
55 | best_gain--; | |
56 | if (ns->c_w == ' ') | |
57 | gain++; | |
58 | if (gain >= best_gain) { | |
59 | best = j; | |
60 | best_gain = gain; | |
61 | } | |
04d70db4 | 62 | } |
6e3b31ff EW |
63 | if (best_gain > 4) { |
64 | (*tt.tt_move)(i, best); | |
04d70db4 | 65 | (*tt.tt_clreol)(); |
6e3b31ff EW |
66 | for (j = wwncol - best, os = &wwos[i][best]; |
67 | --j >= 0;) | |
04d70db4 EW |
68 | os++->c_w = ' '; |
69 | } else | |
70 | wwnmajmiss++; | |
71 | } | |
2b19684f | 72 | *touched = 0; |
04d70db4 EW |
73 | wwnupdline++; |
74 | didit = 0; | |
bbb8329f EW |
75 | ns = wwns[i]; |
76 | os = wwos[i]; | |
b7f39bc5 | 77 | for (j = 0; j < wwncol;) { |
6e3b31ff EW |
78 | register char *p, *q; |
79 | char m; | |
80 | int c; | |
81 | register n; | |
82 | char buf[512]; /* > wwncol */ | |
83 | union ww_char lastc; | |
84 | ||
b7f39bc5 EW |
85 | for (; j++ < wwncol && ns++->c_w == os++->c_w;) |
86 | ; | |
87 | if (j > wwncol) | |
88 | break; | |
89 | p = buf; | |
f4f5703a | 90 | m = ns[-1].c_m; |
b7f39bc5 EW |
91 | c = j - 1; |
92 | os[-1] = ns[-1]; | |
93 | *p++ = ns[-1].c_c; | |
6e3b31ff | 94 | n = 5; |
b7f39bc5 | 95 | q = p; |
f4f5703a | 96 | while (j < wwncol && ns->c_m == m) { |
b7f39bc5 EW |
97 | *p++ = ns->c_c; |
98 | if (ns->c_w == os->c_w) { | |
6e3b31ff | 99 | if (--n <= 0) |
b7f39bc5 EW |
100 | break; |
101 | os++; | |
102 | ns++; | |
103 | } else { | |
6e3b31ff | 104 | n = 5; |
b7f39bc5 | 105 | q = p; |
5e785082 | 106 | lastc = *os; |
b7f39bc5 EW |
107 | *os++ = *ns++; |
108 | } | |
109 | j++; | |
bbb8329f | 110 | } |
3130283e | 111 | tt.tt_nmodes = m; |
5e785082 EW |
112 | if (wwwrap |
113 | && i == wwnrow - 1 && q - buf + c == wwncol) { | |
3130283e | 114 | if (tt.tt_hasinsert) { |
594208de EW |
115 | if (q - buf != 1) { |
116 | (*tt.tt_move)(i, c); | |
117 | (*tt.tt_write)(buf + 1, | |
118 | q - buf - 1); | |
119 | (*tt.tt_move)(i, c); | |
120 | tt.tt_ninsert = 1; | |
121 | (*tt.tt_write)(buf, 1); | |
122 | tt.tt_ninsert = 0; | |
123 | } else { | |
124 | (*tt.tt_move)(i, c - 1); | |
125 | (*tt.tt_write)(buf, 1); | |
126 | tt.tt_nmodes = ns[-2].c_m; | |
127 | (*tt.tt_move)(i, c - 1); | |
128 | tt.tt_ninsert = 1; | |
129 | (*tt.tt_write)(&ns[-2].c_c, 1); | |
130 | tt.tt_ninsert = 0; | |
131 | } | |
5e785082 | 132 | } else { |
2bb24b34 EW |
133 | if (q - buf > 1) { |
134 | (*tt.tt_move)(i, c); | |
135 | (*tt.tt_write)(buf, q-buf-1); | |
136 | } | |
2b19684f EW |
137 | os[-1] = lastc; |
138 | *touched = WWU_TOUCHED; | |
5e785082 | 139 | } |
594208de EW |
140 | } else { |
141 | (*tt.tt_move)(i, c); | |
3130283e | 142 | (*tt.tt_write)(buf, q - buf); |
594208de | 143 | } |
b7f39bc5 | 144 | didit++; |
bbb8329f | 145 | } |
861cd1ed | 146 | if (!didit) |
04d70db4 | 147 | wwnupdmiss++; |
bbb8329f EW |
148 | } |
149 | } |