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