-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathundo.c
610 lines (456 loc) · 11.1 KB
/
undo.c
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
#include "def.h"
#include "macro.h"
/*
* A linear undo.
*/
INT undo_limit = 1000000; // Max bytes stored in undo list per buffer
int u_boundary = 1; // An undo boundary will be made
INT undo_enabled = 1; // Undo enabled
INT buf_modified = 0; // True if the buffer was modified before the commmand
static int u_panic();
static int u_trim();
/*
* Fake the general string insert into inserting raw.
*/
static INT
linsert_str_nl(char *str, INT len)
{
return linsert_general_string(curbp->charset, 1, str, len);
}
/*
* Insert an undo record after another, and update size
*/
static int
u_insertafter(undo_rec *p1, undo_rec *p2)
{
if (!buf_modified) p2->flag |= UFL_CLEARMOD;
buf_modified = BFCHG; // Record clear modify only once per u_modify()
p2->suc = p1->suc;
p2->pre = p1;
p1->suc->pre = p2;
p1->suc = p2;
// Include a reasonable estimate of the overhead in the size
if (curbp->undo_size > MAXINT - (INT)offsetof(undo_rec, content) - p2->size) {
ewprintf("Integer overflow in undo size");
return u_panic();
}
curbp->undo_size += offsetof(undo_rec, content) + p2->size;
return u_trim();
}
/*
* Remove an undo record and update size
*/
static void
u_unlink(undo_rec *p1)
{
p1->pre->suc = p1->suc;
p1->suc->pre = p1->pre;
curbp->undo_size -= offsetof(undo_rec, content) + p1->size;
free(p1);
}
/*
* Create a new undo record
*/
static undo_rec *
u_new(int flag, size_t pos, INT len)
{
undo_rec *p1;
INT size;
if (len < UNDO_MIN) size = UNDO_MIN;
else if (len > MAXINT - 8) size = MAXINT;
else size = len + 8;
p1 = malloc(offsetof(undo_rec, content) + size);
if (p1 == NULL) {
ewprintf("Error allocating undo entry");
return NULL;
}
p1->pos = pos;
p1->flag = flag;
p1->size = size;
p1->len = len;
p1->suc = p1->pre = p1;
return p1;
}
/*
* Trim the undo list to 'undo-limit'
*/
static int
u_trim()
{
undo_rec *undo_list = curbp->undo_list;
if (undo_list && curbp->undo_size > undo_limit) {
while (curbp->undo_size > undo_limit && undo_list->suc != undo_list) {
u_unlink(undo_list->suc);
}
while (undo_list->suc != undo_list && !(undo_list->suc->flag & UFL_BOUNDARY)) {
u_unlink(undo_list->suc);
}
}
curbp->undo_current = NULL;
return 1;
}
/*
* Clean out the undo list in a given buffer. Deallocate everything.
*/
int
u_clear(BUFFER *bp)
{
undo_rec *undo_list = bp->undo_list, *p1;
// ewprintf("Clearing undo.");
if (undo_list) {
while (undo_list->suc != undo_list) {
p1 = undo_list->suc;
undo_list->suc = p1->suc;
free(p1);
}
free(undo_list);
bp->undo_list = NULL;
}
bp->undo_size = 0;
bp->undo_current = NULL;
return 1;
}
/*
* Error. Clear the undo list to keep sync with the buffer. Return
* error status. A message should already have been shown.
*/
static int
u_panic()
{
u_clear(curbp);
return 0;
}
/*
* Kill the undo-entries beyond the current point and clear the
* current pointer.
*/
void
u_kill_later_entries()
{
undo_rec *list = curbp->undo_list;
undo_rec *p1 = curbp->undo_current;
if (p1) {
while (p1 != list) {
p1 = p1->suc;
u_unlink(p1->pre);
}
curbp->undo_current = NULL;
}
if (list) list->flag = 0;
}
/*
* Insert an undo entry
*/
int
u_entry(int flag, size_t pos, char *str, INT len)
{
undo_rec *last, *p1, *undo_list;
if (!undo_enabled || len <= 0 || (curbp->b_flag & (BFSYS|BFDIRED))) {
return 1; // Not an error
} else if (len > undo_limit) {
return u_clear(curbp); // Not an error
}
u_kill_later_entries();
if (!curbp->undo_list && (curbp->undo_list = u_new(0, 0, 0)) == NULL) return u_panic();
undo_list = curbp->undo_list;
last = undo_list->pre;
// Try to merge with the last entry to save space. If merging backward,
// only do it if the entry is not too big.
if (last != undo_list && last->len <= last->size - len && !u_boundary &&
(flag & UFL_INSERT) == (last->flag & UFL_INSERT))
{
if (flag & UFL_INSERT) { // Merge insert after
if (last->pos + last->len == pos) {
memcpy(&last->content[last->len], str, len);
last->len += len;
return u_trim();
}
} else if (last->pos == pos) { // Merge delete after
memcpy(&last->content[last->len], str, len);
last->len += len;
return u_trim();
} else if (last->pos == pos + len && last->len < (INT)(sizeof(undo_rec) << 2)) {
// Merge delete before
memmove(&last->content[len], last->content, last->len);
memcpy(last->content, str, len);
last->pos = pos;
last->len += len;
return u_trim();
}
}
if ((p1 = u_new(flag, pos, len)) == NULL) return u_panic();
memcpy(p1->content, str, len);
if (u_boundary) p1->flag |= UFL_BOUNDARY;
u_boundary = 0;
return u_insertafter(last, p1);
}
/*
* Insert an undo entry for a range, starting at a given point.
*/
int
u_entry_range(int flag, LINE *lp, INT offset, INT len, size_t pos)
{
undo_rec *p1 = NULL;
char *str, *startbuf, buf[UNDO_MIN];
INT chunk;
LINE *blp = curbp->b_linep; // Must check this.
if (!undo_enabled || len <= 0 || lp == blp) return 1; // Not an error
// Within same line, potential merge
if (len <= llength(lp) - offset) {
return u_entry(flag, pos, <ext(lp)[offset], len);
}
if (curbp->b_flag & (BFSYS|BFDIRED)) {
return 1; // Not an error
} else if (len > undo_limit) {
return u_clear(curbp); // Not an error
}
u_kill_later_entries();
if (!curbp->undo_list && (curbp->undo_list = u_new(0, 0, 0)) == NULL) return u_panic();
if (len < UNDO_MIN) {
// Small entry, potential merge
startbuf = buf;
} else {
if ((p1 = u_new(flag, pos, len)) == NULL) return u_panic();
startbuf = p1->content;
}
str = startbuf;
chunk = llength(lp) - offset;
if (chunk > len) chunk = len;
memcpy(str, <ext(lp)[offset], chunk);
str += chunk;
len -= chunk;
while (len > 0) {
if (lforw(lp) == blp) break;
*str++ = '\n';
len--;
if (len <= 0) break;
lp = lforw(lp);
chunk = llength(lp);
if (chunk > len) chunk = len;
memcpy(str, ltext(lp), chunk);
str += chunk;
len -= chunk;
}
if (!p1) {
return u_entry(flag, pos, buf, str - startbuf);
}
p1->len = str - startbuf;
if (u_boundary) p1->flag |= UFL_BOUNDARY;
u_boundary = 0;
return u_insertafter(curbp->undo_list->pre, p1);
}
/*
* Return true if undo list is empty
*/
int
u_empty()
{
undo_rec *undo_list = curbp->undo_list;
return (!undo_list || undo_list->suc == undo_list);
}
/*
* Return true if we can't undo
*/
static int
u_njet()
{
if (curbp->b_flag & (BFSYS|BFDIRED)) {
u_clear(curbp);
ewprintf("Undo is off in system or Dired buffers.");
return 1;
} else if (!undo_enabled) {
u_clear(curbp);
ewprintf("Undo is disabled.");
return 1;
} else if (u_empty()) {
ewprintf("Undo list is empty.");
return 1;
} else if (curbp->b_flag & BFREADONLY) {
readonly();
return 1;
}
return 0;
}
/*
* Undo command that goes only backward
*/
INT
undo_only(INT f, INT n)
{
undo_rec *p1, *undo_list;
INT s, save_undo = undo_enabled;
if (n < 0) return FALSE;
if (u_njet()) return FALSE;
thisflag |= CFUNDO;
curbp->undo_forward = 0;
if (curbp->undo_current == NULL) p1 = curbp->undo_list->pre;
else p1 = curbp->undo_current->pre;
undo_list = curbp->undo_list;
if (p1 == undo_list) {
ewprintf("Nothing more to undo.");
return FALSE;
}
ewprintf("Undo!");
if (p1 == undo_list->pre && !(curbp->b_flag & BFCHG)) {
undo_list->flag |= UFL_CLEARMOD;
}
while (1) {
if (!position(p1->pos)) return FALSE;
undo_enabled = 0;
if (p1->flag & UFL_INSERT) {
s = ldeleteraw(p1->len, KNONE);
} else {
s = linsert_str_nl(p1->content, p1->len);
}
undo_enabled = save_undo;
if (s == FALSE) {
u_clear(curbp);
return s;
}
if (p1->flag & UFL_CLEARMOD) {
curbp->b_flag &= ~BFCHG;
upmodes(curbp);
}
curbp->undo_current = p1;
if (p1->pre == undo_list || ((p1->flag & UFL_BOUNDARY) && n && !--n)) return TRUE;
p1 = p1->pre;
}
}
/*
* Undo command that goes only forward
*/
INT
redo(INT f, INT n)
{
undo_rec *p1, *undo_list;
INT s, save_undo = undo_enabled;
if (n < 0) return FALSE;
if (u_njet()) return FALSE;
thisflag |= CFUNDO;
curbp->undo_forward = 1;
p1 = curbp->undo_current;
undo_list = curbp->undo_list;
if (p1 == NULL) {
ewprintf("Nothing to redo.");
return FALSE;
}
if (p1 == undo_list) {
ewprintf("Nothing more to redo.");
return FALSE;
}
ewprintf("Redo!");
while (1) {
if (!position(p1->pos)) return FALSE;
undo_enabled = 0;
if (p1->flag & UFL_INSERT) {
s = linsert_str_nl(p1->content, p1->len);
} else {
s = ldeleteraw(p1->len, KNONE);
}
undo_enabled = save_undo;
if (s == FALSE) {
u_clear(curbp);
return s;
}
p1 = p1->suc;
curbp->undo_current = p1;
if (p1->flag & UFL_CLEARMOD) {
curbp->b_flag &= ~BFCHG;
upmodes(curbp);
}
if (p1 == undo_list || ((p1->flag & UFL_BOUNDARY) && n && !--n)) return TRUE;
}
}
/*
* Undo command that switches direction if not consecutive
*/
INT
undo(INT f, INT n)
{
if (curbp->undo_current == NULL) {
return undo_only(f, n);
} else if (lastflag & CFUNDO) {
return curbp->undo_forward ? redo(f, n) : undo_only(f, n);
} else {
return curbp->undo_forward ? undo_only(f, n) : redo(f, n);
}
}
/*
* Command to introduce an undo boundary
*/
INT
undo_boundary(INT f, INT n)
{
u_boundary = 1;
return TRUE;
}
/*
* Signal that the buffer gets modified, and save modified flag
*/
void
u_modify()
{
if (undo_enabled) buf_modified = (curbp->b_flag & BFCHG);
if (!toplevel_undo_enabled) u_clear(curbp);
}
/*
* Selectively inhibit undo boundary
*/
void
inhibit_undo_boundary(INT f, PF pf)
{
if (!inmacro && pf == prevfunc && !(f & FFRAND)) u_boundary = 0;
}
#ifdef LIST_UNDO
/*
* List the undo data
*/
INT
list_undo(INT f, INT n)
{
BUFFER *bp;
LINE *lp;
undo_rec *undo_list = curbp->undo_list, *p;
char str[200];
INT len, plen;
size_t count = 0;
const INT maxlen = (MAXINT > (1 << 25)) ? (1 << 24) : MAXINT/2;
if (undo_list == NULL) {
ewprintf("No undo data.");
return TRUE;
}
bp = emptyhelpbuffer();
if (bp == NULL) return FALSE;
snprintf(str, sizeof(str), "Undo list size = " INTFMT, curbp->undo_size);
if (addline(bp, str) == FALSE) return FALSE;
if (addline(bp, "") == FALSE) return FALSE;
for (p = undo_list->suc; p != undo_list; p = p->suc) {
snprintf(str, sizeof(str), "%s%s, pos = %zu, size = %zu, allocated = %zu, flag = %d",
(p->flag & UFL_INSERT) ? "Insert" : "Delete",
(p->flag & UFL_BOUNDARY) ? ", boundary" : "",
p->pos, (size_t)p->len, (size_t)p->size, p->flag);
if (addline(bp, str) == FALSE) return FALSE;
// Truncate large record, also solves the problem of a
// MAXINT undo record with overhead in a MAXINT line
if (p->len > maxlen) {
len = stringcopy(str, "data (truncated) = \"", sizeof(str));
plen = maxlen;
} else {
len = stringcopy(str, "data = \"", sizeof(str));
plen = p->len;
}
lp = lalloc(len + plen + 1);
if (lp == NULL) return FALSE;
memcpy(ltext(lp), str, len);
memcpy(<ext(lp)[len], p->content, plen);
ltext(lp)[len + plen] = '"';
addlast(bp, lp);
if (addline(bp, "") == FALSE) return FALSE;
count++;
}
snprintf(str, sizeof(str), "%zu record%s, %.2f bytes per record",
count, count == 1 ? "" : "s", count ? (double)curbp->undo_size/count : 0.0);
if (addline(bp, str) == FALSE) return FALSE;
return popbuftop(bp);
}
#endif