16 static char Wsequence[] = "warning: changes out of sequence\n";
17 static int warned = FALSE;
20 * Log of changes made by editing commands. Three reasons for this:
21 * 1) We want addresses in commands to apply to old file, not file-in-change.
22 * 2) It's difficult to track changes correctly as things move, e.g. ,x m$
23 * 3) This gives an opportunity to optimize by merging adjacent changes.
24 * It's a little bit like the Undo/Redo log in Files, but Point 3) argues for a
25 * separate implementation. To do this well, we use Replace as well as
29 typedef struct Buflog Buflog;
32 short type; /* Replace, Filename */
33 uint q0; /* location of change (unused in f) */
34 uint nd; /* # runes to delete */
35 uint nr; /* # runes in string or file name */
40 Buflogsize = sizeof(Buflog)/sizeof(Rune)
44 * Minstring shouldn't be very big or we will do lots of I/O for small changes.
45 * Maxstring is RBUFSIZE so we can fbufalloc() once and not realloc elog.r.
49 Minstring = 16, /* distance beneath which we merge changes */
50 Maxstring = RBUFSIZE /* maximum length of change we will merge into one */
56 if(f->elog.type != Empty)
60 f->elogbuf = emalloc(sizeof(Buffer));
62 f->elog.r = fbufalloc();
101 b.type = f->elog.type;
105 switch(f->elog.type){
107 warning(nil, "unknown elog type 0x%ux\n", f->elog.type);
114 bufinsert(f->elogbuf, f->elogbuf->nc, f->elog.r, f->elog.nr);
117 bufinsert(f->elogbuf, f->elogbuf->nc, (Rune*)&b, Buflogsize);
124 elogreplace(File *f, int q0, int q1, Rune *r, int nr)
131 if(f->elog.type!=Null && q0<f->elog.q0){
133 warning(nil, Wsequence);
136 /* try to merge with previous */
137 gap = q0 - (f->elog.q0+f->elog.nd); /* gap between previous and this */
138 if(f->elog.type==Replace && f->elog.nr+gap+nr<Maxstring){
141 bufread(&f->b, f->elog.q0+f->elog.nd, f->elog.r+f->elog.nr, gap);
144 f->elog.nd += gap + q1-q0;
145 runemove(f->elog.r+f->elog.nr, r, nr);
151 f->elog.type = Replace;
156 editerror("internal error: replacement string too large(%d)", nr);
157 runemove(f->elog.r, r, nr);
161 eloginsert(File *f, int q0, Rune *r, int nr)
168 if(f->elog.type!=Null && q0<f->elog.q0){
170 warning(nil, Wsequence);
173 /* try to merge with previous */
174 if(f->elog.type==Insert && q0==f->elog.q0 && f->elog.nr+nr<Maxstring){
175 runemove(f->elog.r+f->elog.nr, r, nr);
181 f->elog.type = Insert;
187 runemove(f->elog.r, r, n);
194 elogdelete(File *f, int q0, int q1)
199 if(f->elog.type!=Null && q0<f->elog.q0+f->elog.nd){
201 warning(nil, Wsequence);
204 /* try to merge with previous */
205 if(f->elog.type==Delete && f->elog.q0+f->elog.nd==q0){
210 f->elog.type = Delete;
242 * The edit commands have already updated the selection in t->q0, t->q1,
243 * but using coordinates relative to the unmodified buffer. As we apply the log,
244 * we have to update the coordinates to be relative to the modified buffer.
245 * Textinsert and textdelete will do this for us; our only work is to apply the
246 * convention that an insertion at t->q0==t->q1 is intended to select the
251 * We constrain the addresses in here (with textconstrain()) because
252 * overlapping changes will generate bogus addresses. We will warn
253 * about changes out of sequence but proceed anyway; here we must
254 * keep things in range.
258 up = log->nc-Buflogsize;
259 bufread(log, up, (Rune*)&b, Buflogsize);
262 fprint(2, "elogapply: 0x%ux\n", b.type);
268 warning(nil, "elog replace %d %d (%d %d)\n",
269 b.q0, b.q0+b.nd, t->q0, t->q1);
274 textconstrain(t, b.q0, b.q0+b.nd, &tq0, &tq1);
275 textdelete(t, tq0, tq1, TRUE);
277 for(i=0; i<b.nr; i+=n){
281 bufread(log, up+i, buf, n);
282 textinsert(t, tq0+i, buf, n, TRUE);
284 if(t->q0 == b.q0 && t->q1 == b.q0)
290 warning(nil, "elog delete %d %d (%d %d)\n",
291 b.q0, b.q0+b.nd, t->q0, t->q1);
296 textconstrain(t, b.q0, b.q0+b.nd, &tq0, &tq1);
297 textdelete(t, tq0, tq1, TRUE);
302 warning(nil, "elog insert %d %d (%d %d)\n",
303 b.q0, b.q0+b.nr, t->q0, t->q1);
308 textconstrain(t, b.q0, b.q0, &tq0, &tq1);
310 for(i=0; i<b.nr; i+=n){
314 bufread(log, up+i, buf, n);
315 textinsert(t, tq0+i, buf, n, TRUE);
317 if(t->q0 == b.q0 && t->q1 == b.q0)
323 fileunsetname(f, epsilon);
330 f->name = runemalloc(u.n);
331 bufread(delta, up, f->name, u.n);
336 bufdelete(log, up, log->nc);
342 * Bad addresses will cause bufload to crash, so double check.
343 * If changes were out of order, we expect problems so don't complain further.
345 if(t->q0 > f->b.nc || t->q1 > f->b.nc || t->q0 > t->q1){
347 warning(nil, "elogapply: can't happen %d %d %d\n", t->q0, t->q1, f->b.nc);
348 t->q1 = min(t->q1, f->b.nc);
349 t->q0 = min(t->q0, t->q1);