forked from bloovis/microemacs.mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathundo.c
More file actions
884 lines (780 loc) · 18.4 KB
/
undo.c
File metadata and controls
884 lines (780 loc) · 18.4 KB
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
/*
Copyright (C) 2018 Mark Alexander
This file is part of MicroEMACS, a small text editor.
MicroEMACS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "def.h"
/* Maximum number of undo operations saved. */
#define N_UNDO 100
/* A single undo step, as part of a larger group.
* Each step is like a journal entry for the editor.
* It consists of a:
* - UDELETE entry when the editor deletes a string
* - UINSERT entry when the editor inserts a string
* - UMOVE entry when the editor moves the dot
*/
typedef struct UNDO
{
UKIND kind; /* Kind of information */
int l; /* Line number */
int o; /* Offset into line */
union
{
/* UDELETE record */
struct
{
int chars; /* # of characters deleted */
int bytes; /* # of bytes deleted */
uchar *s; /* string that was deleted */
} del;
/* UINSERT record */
struct
{
int copies; /* # of copies of string */
int chars; /* # of characters inserted */
int bytes; /* # of bytes inserted */
uchar *s; /* string that was inserted */
} ins;
} u;
}
UNDO;
typedef struct LINKS
{
struct LINKS *next;
struct LINKS *prev;
}
LINKS;
/* Group of UNDO steps, treated as one undo operation. */
typedef struct UNDOGROUP
{
LINKS links; /* head = prev group, tail = next group */
UNDO *undos; /* array of undo steps */
int next; /* next free entry in group */
int avail; /* size of group array */
int b_flag; /* copy of curbp->b_flag before any changes */
}
UNDOGROUP;
/* Linked list of undo groups, treated as a stack of maximum size N_UNDO */
typedef struct UNDOSTACK
{
LINKS undolist; /* head and tail of undo group list */
LINKS redolist; /* head and tail of redo group list */
int ngroups; /* size of group stack */
}
UNDOSTACK;
#define ukind(up) (up->kind & 0xff)
#define ustart(up) ((up->kind & USTART) != 0)
#define uend(up) ((up->kind & UEND) != 0)
#define NOLINE -1 /* UNDO.{l,o} value meaning "not used" */
static int startl = NOLINE; /* lineno saved by startundo */
static int starto; /* offset saved by startundo */
static int undoing = FALSE; /* currently undoing an operation? */
static int b_flag; /* copy of curbp->b_flag */
/* Initialize group links */
static void
initlinks (LINKS *links)
{
links->next = links->prev = links;
}
/*
* Remove group from list.
*/
static void
unlinkgroup (UNDOGROUP *g)
{
g->links.prev->next = g->links.next;
g->links.next->prev = g->links.prev;
}
/*
* Append a group to the end of a list.
*/
static void
appendgroup (UNDOGROUP *g, LINKS *list)
{
g->links.prev = list->prev;
g->links.next = list;
list->prev->next = &g->links;
list->prev = &g->links;
}
/*
* Is a group list empty?
*/
static int
emptylist (LINKS *list)
{
return list->next == list;
}
/*
* Allocate a new undo group structure.
*/
static UNDOGROUP *
newgroup (void)
{
UNDOGROUP *g = malloc (sizeof (*g));
UNDO *u = malloc (sizeof (*u));
g->undos = u;
g->next = 0;
g->avail = 1;
g->b_flag = b_flag;
return g;
}
/*
* Free up any resources associated with an undo step.
*/
static void
freeundo (UNDO *up)
{
switch (ukind (up))
{
case UDELETE:
free (up->u.del.s);
break;
case UINSERT:
free (up->u.ins.s);
break;
}
up->kind = UUNUSED;
}
/*
* Calculate the zero-based line number for a given line pointer.
*/
int
lineno (const LINE *lp)
{
LINE *clp;
int nline;
clp = firstline (curbp);
nline = 0;
for (;;)
{
if (clp == lastline (curbp) || clp == lp)
break;
clp = lforw (clp);
++nline;
}
return nline;
}
/*
* Allocate a new undo stack structure, but don't
* add any undo groups to it yet.
*/
static UNDOSTACK *
newstack (void)
{
UNDOSTACK *st = malloc (sizeof (*st));
initlinks (&st->undolist);
initlinks (&st->redolist);
st->ngroups = 0;
return st;
}
/*
* Call this at the start of an undo save sequence,
* i.e. before the first saveundo. It saves
* some context about the current location: the
* line number and offset, and the buffer changed flag.
*/
void
startsaveundo (void)
{
UNDOSTACK *st;
st = curwp->w_bufp->b_undo;
if (st == NULL)
{
st = newstack ();
curwp->w_bufp->b_undo = st;
}
startl = lineno (curwp->w_dot.p);
starto = curwp->w_dot.o;
b_flag = curbp->b_flag;
undoing = FALSE;
}
/*
* Call this at the end of an undo save sequence,
* i.e. after the last saveundo. Currently it does
* nothing, but conceivably it could free up any
* resources that might have been allocated by
* startsaveundo and that are not longer needed.
*/
void
endsaveundo (void)
{
}
/*
* Remove an undo group from its list, then free up
* its undo records, and finally free the group record itself.
*/
static void
freegroup (UNDOGROUP *g)
{
UNDO *up, *end;
/* Remove group from its list.
*/
unlinkgroup (g);
/* Free up any resources used by the undo steps in the group.
*/
end = &g->undos[g->next];
for (up = &g->undos[0]; up != end; up++)
freeundo (up);
/* Free up the undo array.
*/
free (g->undos);
/* Finally, free up the group record.
*/
free (g);
}
/*
* Free up all undo groups on a list.
*/
static void
freegrouplist (UNDOSTACK *st, LINKS *list)
{
UNDOGROUP *g;
for (g = (UNDOGROUP *) list->next;
&g->links != list;
g = (UNDOGROUP *) list->next)
{
freegroup (g);
st->ngroups--;
}
}
/*
* Return a pointer to the most recently saved undo record,
* or NULL if there is none.
*/
static UNDO *
lastundo (UNDOSTACK *st)
{
UNDOGROUP *g;
/* Is group list empty?
*/
if (emptylist (&st->undolist))
return NULL;
/* Is group itself empty?
*/
g = (UNDOGROUP *) st->undolist.prev;
if (g->next == 0)
return NULL;
/* Return last undo record in group.
*/
return &g->undos[g->next - 1];
}
/*
* Allocate a new undo record and return a pointer to it.
* Initialize its kind, line number, and offset from the
* passed-in values. Also create a new undo group for
* this undostack if this its first undo record.
*/
static UNDO *
newundo (UNDOSTACK *st, UKIND kind, int line, int offset)
{
UNDO *up;
UNDOGROUP *g;
/* If startl has been set by startundo, this is the first
* undo record for the current command, so allocate a new
* undo group.
*/
if (startl != NOLINE)
{
/* This is the start of a new undo group. Create a group
* and place it at the end of list of groups.
*/
g = newgroup ();
appendgroup (g, &st->undolist);
/* If we've reached the maximum number of undo groups, recycle the
* first one in the list.
*/
if (st->ngroups >= N_UNDO)
freegroup ((UNDOGROUP *) st->undolist.next);
else
st->ngroups++;
}
else
/* This is not the first undo record in a group. Get
* the last group in the list
*/
g = (UNDOGROUP *) st->undolist.prev;
/* Do we need to expand the array of undo records in this group?
*/
if (g->next >= g->avail)
{
g->avail = g->avail << 1;
g->undos = (UNDO *) realloc (g->undos, g->avail * sizeof (UNDO));
}
up = &g->undos[g->next];
g->next++;
/* Initialize the undo record with its kind, and the specified
* line number and offset (which may be NOLINE if unknown).
*/
up->kind = kind;
up->l = line;
up->o = offset;
return up;
}
/*
* Prevent subsequent saveundo calls from storing data. Currently
* not used, but conceivably could be used for code sections
* that should not be saving undo records.
*/
void
disablesaveundo (void)
{
undoing = TRUE;
}
/* Allow subsequent saveundo calls to store data. See disablesaveundo.
*/
void
enablesaveundo (void)
{
undoing = FALSE;
}
/*
* Make one or more copies of byte sequence s whose length in bytes is n,
* and store the result in dest, which must be at least (copies * n) bytes.
*/
static uchar *
memdup (uchar *dest, int copies, const uchar *s, int n)
{
int i;
for (i = 0; i < copies; i++)
memcpy (dest + (i * n), s, n);
return dest;
}
/*
* Save a single undo record, which is a record of a move, deletion, or insertion.
* The first two parameters are fixed:
* - the kind of undo record
* - a pointer to a line/offset pair to be recorded in
* the record, or NULL if no line/offset pair is to be recorded.
* Following these two parameters there may be other parameters,
* depending on the kind:
* - UMOVE:
* - takes no extra parameters
* - UDELETE:
* - size of deleted string in characters
* - size of deleted string in bytes
* - pointer to deleted string (may not be null-terminated)
* - UINSERT:
* - # of copies of inserted string
* - size of inserted string in characters
* - size of inserted string in bytes
* - pointer to inserted string (may not be null-terminated)
*/
int
saveundo (UKIND kind, POS *pos, ...)
{
va_list ap;
UNDO *up;
UNDOSTACK *st;
int line, offset;
if (undoing)
return TRUE;
va_start (ap, pos);
/* Figure out what line number and offset to use for this undo record.
* If POS was passed in, calculate the corresponding line number and offset.
* Otherwise, if this is the first record after a startundo, use the line
* number and offset saved by startundo. Otherwise don't use any line
* number or offset.
*/
if (pos != NULL)
{
line = lineno (pos->p); /* Line number */
offset = pos->o; /* Offset */
}
else if (startl != NOLINE)
{
line = startl;
offset = starto;
}
else
{
line = NOLINE;
offset = NOLINE;
}
/* Free up any redo records that have accumulated.
*/
st = curwp->w_bufp->b_undo;
freegrouplist (st, &st->redolist);
switch (kind)
{
case UMOVE: /* Move to (line #, offset) */
up = newundo (st, kind, line, offset);
break;
case UDELETE: /* Delete string */
{
int chars = va_arg (ap, int);
int bytes = va_arg (ap, int);
const uchar *s = va_arg (ap, const uchar *);
up = newundo (st, kind, line, offset);
up->u.del.s = (uchar *) malloc (bytes);
if (up->u.del.s == NULL)
{
eprintf ("Out of memory in undo!");
return FALSE;
}
memcpy (up->u.del.s, s, bytes);
up->u.del.chars = chars;
up->u.del.bytes = bytes;
break;
}
case UINSERT: /* Insert N copies of a string */
{
int copies = va_arg (ap, int);
int chars = va_arg (ap, int);
int bytes = va_arg (ap, int);
const uchar *s = va_arg (ap, const uchar *);
UNDO *prev = lastundo (st);
int totalbytes = bytes * copies;
if (prev != NULL &&
ukind (prev) == UINSERT &&
prev->l == line &&
prev->o + prev->u.ins.chars == offset)
{
prev->u.ins.chars += chars;
prev->u.ins.s = (uchar *) realloc (prev->u.ins.s, prev->u.ins.bytes + totalbytes);
if (prev->u.ins.s == NULL)
{
eprintf ("Out of memory in undo!");
return FALSE;
}
memdup (prev->u.ins.s + prev->u.ins.bytes, copies, s, bytes);
prev->u.ins.bytes += totalbytes;
}
else
{
up = newundo (st, kind, line, offset);
up->u.ins.chars = chars * copies;
up->u.ins.bytes = totalbytes;
/* Make copy of string. */
up->u.ins.s = (uchar *) malloc (totalbytes);
if (up->u.ins.s == NULL)
{
eprintf ("Out of memory in undo!");
return FALSE;
}
memdup (up->u.ins.s, copies, s, bytes);
}
break;
}
default:
eprintf ("Unimplemented undo type %d", kind);
break;
}
va_end (ap);
startl = NOLINE;
return TRUE;
}
/*
* Undo a single step in a possibly larger sequence of undo records.
*/
static int
undostep (UNDO *up)
{
int status = TRUE;
if (up->l != NOLINE)
{
status = gotoline (TRUE, up->l + 1, KRANDOM);
if (up->o > wllength (curwp->w_dot.p))
eprintf ("Offset too large");
else
{
curwp->w_dot.o = up->o;
curwp->w_flag |= WFMOVE;
}
}
if (status == TRUE)
{
switch (ukind (up))
{
case UMOVE:
break;
case UDELETE:
{
const uchar *s = up->u.del.s;
status = insertwithnl ((const char *) s, up->u.del.bytes);
break;
}
case UINSERT:
status = ldelete (up->u.ins.chars, FALSE);
break;
default:
eprintf ("Unknown undo kind 0x%x", up->kind);
status = FALSE;
break;
}
}
return status;
}
/*
* Undo the topmost undo group on the undo stack.
* Each group consists of a linear sequence of undo steps.
* This sequence is split into subsequences; the
* start of each subsequence is any undo record that
* moves the dot. These subsequences are processed
* in reverse order, but within each subsequence,
* the undo records are processed in forward order.
* This ordering is necessary to account for any
* undo sequences that move the dot.
*/
int
undo (int f, int n, int k)
{
UNDO *up;
UNDO *start;
UNDO *end;
UNDOSTACK *st;
UNDOGROUP *g;
int status = TRUE;
/* Get the last undo group on the list, or error out
* if the list is empty.
*/
undoing = TRUE;
st = curwp->w_bufp->b_undo;
if (emptylist (&st->undolist))
{
eprintf ("undo stack is empty");
undoing = FALSE;
return FALSE;
}
g = (UNDOGROUP *) st->undolist.prev;
/* Replay all steps of the most recently saved undo. Break up
* the steps into subsequences that start with moves. Play these
* subsequences in reverse order, but play the individual steps
* within a subsequence in forward order.
*/
end = &g->undos[g->next];
start = end - 1;
while (start >= g->undos)
{
while (start > g->undos && start->l == NOLINE)
--start;
for (up = start; up != end; up++)
{
int s = undostep (up);
if (s != TRUE)
status = s;
}
end = start;
--start;
}
/* Set the buffer change flag.
*/
if (g->b_flag & BFCHG)
curbp->b_flag |= BFCHG;
else
curbp->b_flag &= ~BFCHG;
updatemode ();
/* Pop this undo group from the undo list and move it
* to the redo list.
*/
unlinkgroup (g);
appendgroup (g, &st->redolist);
undoing = FALSE;
return status;
}
/*
* Redo a single step in a possibly larger sequence of undo records.
*/
static int
redostep (UNDO *up)
{
int status = TRUE;
if (up->l != NOLINE)
{
status = gotoline (TRUE, up->l + 1, KRANDOM);
if (up->o > wllength (curwp->w_dot.p))
eprintf ("Offset too large");
else
{
curwp->w_dot.o = up->o;
curwp->w_flag |= WFMOVE;
}
curwp->w_flag |= WFMOVE;
}
if (status == TRUE)
{
switch (ukind (up))
{
case UMOVE:
break;
case UDELETE:
{
status = ldelete (up->u.del.chars, FALSE);
break;
}
case UINSERT:
status = insertwithnl ((const char *) up->u.ins.s, up->u.ins.bytes);
break;
default:
eprintf ("Unknown undo kind 0x%x", up->kind);
status = FALSE;
break;
}
}
return status;
}
/*
* Redo the topmost undo group on the redo stack.
*/
int
redo (int f, int n, int k)
{
UNDO *up;
UNDO *start;
UNDO *end;
UNDOSTACK *st;
UNDOGROUP *g;
int status = TRUE;
/* Get the top undo group on the redo stack, or error out
* if the stack is empty.
*/
undoing = TRUE;
st = curwp->w_bufp->b_undo;
if (emptylist (&st->redolist))
{
eprintf ("redo stack is empty");
undoing = FALSE;
return FALSE;
}
g = (UNDOGROUP *) st->redolist.prev;
/* Undo all steps of this redo group.
*/
end = &g->undos[g->next];
start = &g->undos[0];
for (up = start; up != end; up++)
{
int s = redostep (up);
if (s != TRUE)
status = s;
}
/* Set the buffer change flag.
*/
curbp->b_flag |= BFCHG;
updatemode ();
/* Move this undo group from the redo stack back to the top of the
* the undo stack.
*/
unlinkgroup (g);
appendgroup (g, &st->undolist);
undoing = FALSE;
return status;
}
#if DEBUG
/*
* Print a non-null-terminated string.
*/
static void
printstring (const uchar *s, int bytes)
{
printf ("'");
while (bytes > 0)
{
uchar c = *s;
if (c == '\n')
printf ("\\n");
else
printf ("%c", c);
--bytes;
++s;
}
printf ("'");
}
/*
* Print a single undo record. The \r characters are necessary
* because this function is called from gdb, and
* at this point the editor has tweaked the tty so that
* newline doesn't generate a carriage return.
*/
static void
printone (UNDO *up)
{
printf (" ");
switch (ukind (up))
{
case UDELETE:
{
printf ("Delete string: ");
printstring (up->u.del.s, up->u.del.bytes);
break;
}
case UMOVE:
printf ("Move");
break;
case UINSERT:
printf ("Insert string: ");
printstring (up->u.ins.s, up->u.ins.bytes);
break;
default:
printf ("Unexpected kind 0x%x", up->kind);
break;
}
if (up->l != NOLINE)
printf (", line %d, offset %d",
up->l, up->o);
printf ("\r\n");
}
/*
* Print the current window's undo stack. This is intended
* to be called from gdb for debugging purposes only.
*/
void
printundo (void)
{
int level;
UNDOSTACK *st;
UNDOGROUP *g;
UNDO *up;
UNDO *end;
level = 1;
st = curwp->w_bufp->b_undo;
for (g = (UNDOGROUP *) st->undolist.next;
&g->links != &st->undolist;
g = (UNDOGROUP *) g->links.next)
{
printf ("%d:\r\n", level);
end = &g->undos[g->next];
for (up = &g->undos[0]; up != end; up++)
printone (up);
++level;
}
}
#endif /* DEBUG */
/*
* Free up the undo records associated with a buffer.
*/
void
killundo (BUFFER *bp)
{
UNDOSTACK *st = bp->b_undo;
freegrouplist (st, &st->undolist);
freegrouplist (st, &st->redolist);
free (st);
bp->b_undo = NULL;
}
/*
* Set the "buffer changed" flag in all undo records for
* the buffer buffer. This should be called after a buffer
* is saved to disk, so that any subsequent undo operations will
* set the "buffer changed" flag for that buffer.
*/
void
setundochanged (void)
{
UNDOSTACK *st;
UNDOGROUP *g;
st = curbp->b_undo;
for (g = (UNDOGROUP *) st->undolist.next;
&g->links != &st->undolist;
g = (UNDOGROUP *) g->links.next)
{
g->b_flag = BFCHG;
}
}