1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
#include "sam.h"
/*
* GROWDATASIZE must be big enough that all errors go out as Hgrowdata's,
* so they will be scrolled into visibility in the ~~sam~~ window (yuck!).
*/
#define GROWDATASIZE 50 /* if size is <= this, send data with grow */
void rcut(List*, Posn, Posn);
int rterm(List*, Posn);
void rgrow(List*, Posn, Posn);
static Posn growpos;
static Posn grown;
static Posn shrinkpos;
static Posn shrunk;
/*
* rasp routines inform the terminal of changes to the file.
*
* a rasp is a list of spans within the file, and an indication
* of whether the terminal knows about the span.
*
* optimize by coalescing multiple updates to the same span
* if it is not known by the terminal.
*
* other possible optimizations: flush terminal's rasp by cut everything,
* insert everything if rasp gets too large.
*/
/*
* only called for initial load of file
*/
void
raspload(File *f)
{
if(f->rasp == nil)
return;
grown = f->b.nc;
growpos = 0;
if(f->b.nc)
rgrow(f->rasp, 0, f->b.nc);
raspdone(f, 1);
}
void
raspstart(File *f)
{
if(f->rasp == nil)
return;
grown = 0;
shrunk = 0;
outbuffered = 1;
}
void
raspdone(File *f, int toterm)
{
if(f->dot.r.p1 > f->b.nc)
f->dot.r.p1 = f->b.nc;
if(f->dot.r.p2 > f->b.nc)
f->dot.r.p2 = f->b.nc;
if(f->mark.p1 > f->b.nc)
f->mark.p1 = f->b.nc;
if(f->mark.p2 > f->b.nc)
f->mark.p2 = f->b.nc;
if(f->rasp == nil)
return;
if(grown)
outTsll(Hgrow, f->tag, growpos, grown);
else if(shrunk)
outTsll(Hcut, f->tag, shrinkpos, shrunk);
if(toterm)
outTs(Hcheck0, f->tag);
outflush();
outbuffered = 0;
if(f == cmd){
cmdpt += cmdptadv;
cmdptadv = 0;
}
}
void
raspflush(File *f)
{
if(grown){
outTsll(Hgrow, f->tag, growpos, grown);
grown = 0;
}
else if(shrunk){
outTsll(Hcut, f->tag, shrinkpos, shrunk);
shrunk = 0;
}
outflush();
}
void
raspdelete(File *f, uint p1, uint p2, int toterm)
{
long n;
n = p2 - p1;
if(n == 0)
return;
if(p2 <= f->dot.r.p1){
f->dot.r.p1 -= n;
f->dot.r.p2 -= n;
}
if(p2 <= f->mark.p1){
f->mark.p1 -= n;
f->mark.p2 -= n;
}
if(f->rasp == nil)
return;
if(f==cmd && p1<cmdpt){
if(p2 <= cmdpt)
cmdpt -= n;
else
cmdpt = p1;
}
if(toterm){
if(grown){
outTsll(Hgrow, f->tag, growpos, grown);
grown = 0;
}else if(shrunk && shrinkpos!=p1 && shrinkpos!=p2){
outTsll(Hcut, f->tag, shrinkpos, shrunk);
shrunk = 0;
}
if(!shrunk || shrinkpos==p2)
shrinkpos = p1;
shrunk += n;
}
rcut(f->rasp, p1, p2);
}
void
raspinsert(File *f, uint p1, Rune *buf, uint n, int toterm)
{
Range r;
if(n == 0)
return;
if(p1 < f->dot.r.p1){
f->dot.r.p1 += n;
f->dot.r.p2 += n;
}
if(p1 < f->mark.p1){
f->mark.p1 += n;
f->mark.p2 += n;
}
if(f->rasp == nil)
return;
if(f==cmd && p1<cmdpt)
cmdpt += n;
if(toterm){
if(shrunk){
outTsll(Hcut, f->tag, shrinkpos, shrunk);
shrunk = 0;
}
if(n>GROWDATASIZE || !rterm(f->rasp, p1)){
rgrow(f->rasp, p1, n);
if(grown && growpos+grown!=p1 && growpos!=p1){
outTsll(Hgrow, f->tag, growpos, grown);
grown = 0;
}
if(!grown)
growpos = p1;
grown += n;
}else{
if(grown){
outTsll(Hgrow, f->tag, growpos, grown);
grown = 0;
}
rgrow(f->rasp, p1, n);
r = rdata(f->rasp, p1, n);
if(r.p1!=p1 || r.p2!=p1+n)
panic("rdata in toterminal");
outTsllS(Hgrowdata, f->tag, p1, n, tmprstr(buf, n));
}
}else{
rgrow(f->rasp, p1, n);
r = rdata(f->rasp, p1, n);
if(r.p1!=p1 || r.p2!=p1+n)
panic("rdata in toterminal");
}
}
#define M 0x80000000L
#define P(i) r->posnptr[i]
#define T(i) (P(i)&M) /* in terminal */
#define L(i) (P(i)&~M) /* length of this piece */
void
rcut(List *r, Posn p1, Posn p2)
{
Posn p, x;
int i;
if(p1 == p2)
panic("rcut 0");
for(p=0,i=0; i<r->nused && p+L(i)<=p1; p+=L(i++))
;
if(i == r->nused)
panic("rcut 1");
if(p < p1){ /* chop this piece */
if(p+L(i) < p2){
x = p1-p;
p += L(i);
}else{
x = L(i)-(p2-p1);
p = p2;
}
if(T(i))
P(i) = x|M;
else
P(i) = x;
i++;
}
while(i<r->nused && p+L(i)<=p2){
p += L(i);
dellist(r, i);
}
if(p < p2){
if(i == r->nused)
panic("rcut 2");
x = L(i)-(p2-p);
if(T(i))
P(i) = x|M;
else
P(i) = x;
}
/* can we merge i and i-1 ? */
if(i>0 && i<r->nused && T(i-1)==T(i)){
x = L(i-1)+L(i);
dellist(r, i--);
if(T(i))
P(i)=x|M;
else
P(i)=x;
}
}
void
rgrow(List *r, Posn p1, Posn n)
{
Posn p;
int i;
if(n == 0)
panic("rgrow 0");
for(p=0,i=0; i<r->nused && p+L(i)<=p1; p+=L(i++))
;
if(i == r->nused){ /* stick on end of file */
if(p!=p1)
panic("rgrow 1");
if(i>0 && !T(i-1))
P(i-1)+=n;
else
inslist(r, i, n);
}else if(!T(i)) /* goes in this empty piece */
P(i)+=n;
else if(p==p1 && i>0 && !T(i-1)) /* special case; simplifies life */
P(i-1)+=n;
else if(p==p1)
inslist(r, i, n);
else{ /* must break piece in terminal */
inslist(r, i+1, (L(i)-(p1-p))|M);
inslist(r, i+1, n);
P(i) = (p1-p)|M;
}
}
int
rterm(List *r, Posn p1)
{
Posn p;
int i;
for(p = 0,i = 0; i<r->nused && p+L(i)<=p1; p+=L(i++))
;
if(i==r->nused && (i==0 || !T(i-1)))
return 0;
return T(i);
}
Range
rdata(List *r, Posn p1, Posn n)
{
Posn p;
int i;
Range rg;
if(n==0)
panic("rdata 0");
for(p = 0,i = 0; i<r->nused && p+L(i)<=p1; p+=L(i++))
;
if(i==r->nused)
panic("rdata 1");
if(T(i)){
n-=L(i)-(p1-p);
if(n<=0){
rg.p1 = rg.p2 = p1;
return rg;
}
p+=L(i++);
p1 = p;
}
if(T(i) || i==r->nused)
panic("rdata 2");
if(p+L(i)<p1+n)
n = L(i)-(p1-p);
rg.p1 = p1;
rg.p2 = p1+n;
if(p!=p1){
inslist(r, i+1, L(i)-(p1-p));
P(i)=p1-p;
i++;
}
if(L(i)!=n){
inslist(r, i+1, L(i)-n);
P(i)=n;
}
P(i)|=M;
/* now i is set; can we merge? */
if(i<r->nused-1 && T(i+1)){
P(i)=(n+=L(i+1))|M;
dellist(r, i+1);
}
if(i>0 && T(i-1)){
P(i)=(n+L(i-1))|M;
dellist(r, i-1);
}
return rg;
}
|