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