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
|
#define _BSD_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include "editor.h"
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define BUFFER_SIZE (1 << 20)
/* Buffer holding the file content, either readonly mmap(2)-ed from the original
* file or heap allocated to store the modifications.
*/
typedef struct Buffer Buffer;
struct Buffer {
size_t size; /* maximal capacity */
size_t len; /* current used length / insertion position */
char *data; /* actual data */
Buffer *next; /* next junk */
};
/* A piece holds a reference (but doesn't itself store) a certain amount of data.
* All active pieces chained together form the whole content of the document.
* At the beginning there exists only one piece, spanning the whole document.
* Upon insertion/delition new pieces will be created to represent the changes.
* Generally pieces are never destroyed, but kept around to peform undo/redo operations.
*/
struct Piece {
Editor *editor; /* editor to which this piece belongs */
Piece *prev, *next; /* pointers to the logical predecessor/successor */
Piece *global_prev; /* double linked list in order of allocation, */
Piece *global_next; /* used to free individual pieces */
const char *data; /* pointer into a Buffer holding the data */
size_t len; /* the lenght in number of bytes starting from content */
int index; /* unique index identifiying the piece */
// size_t line_count;
};
/* used to transform a global position (byte offset starting from the begining
* of the text) into an offset relative to piece.
*/
typedef struct {
Piece *piece; /* piece holding the location */
size_t off; /* offset into the piece in bytes */
} Location;
/* A Span holds a certain range of pieces. Changes to the document are allways
* performed by swapping out an existing span with a new one.
*/
typedef struct {
Piece *start, *end; /* start/end of the span */
size_t len; /* the sum of the lenghts of the pieces which form this span */
// size_t line_count;
} Span;
/* A Change keeps all needed information to redo/undo an insertion/deletion. */
typedef struct Change Change;
struct Change {
Span old; /* all pieces which are being modified/swapped out by the change */
Span new; /* all pieces which are introduced/swapped in by the change */
Change *next;
};
/* An Action is a list of Changes which are used to undo/redo all modifications
* since the last snapshot operation. Actions are kept in an undo and a redo stack.
*/
typedef struct Action Action;
struct Action {
Change *change; /* the most recent change */
Action *next; /* next action in the undo/redo stack */
time_t time; /* when the first change of this action was performed */
};
/* The main struct holding all information of a given file */
struct Editor {
Buffer buf; /* original mmap(2)-ed file content at the time of load operation */
Buffer *buffers; /* all buffers which have been allocated to hold insertion data */
Piece *pieces; /* all pieces which have been allocated, used to free them */
Piece *cache; /* most recently modified piece */
int piece_count; /* number of pieces allocated, only used for debuging purposes */
Piece begin, end; /* sentinel nodes which always exists but don't hold any data */
Action *redo, *undo; /* two stacks holding all actions performed to the file */
Action *current_action; /* action holding all file changes until a snapshot is performed */
Action *saved_action; /* the last action at the time of the save operation */
size_t size; /* current file content size in bytes */
const char *filename; /* filename of which data was loaded */
struct stat info; /* stat as proped on load time */
int fd; /* the file descriptor of the original mmap-ed data */
};
/* buffer management */
static Buffer *buffer_alloc(Editor *ed, size_t size);
static void buffer_free(Buffer *buf);
static bool buffer_capacity(Buffer *buf, size_t len);
static const char *buffer_append(Buffer *buf, const char *data, size_t len);
static bool buffer_insert(Buffer *buf, size_t pos, const char *data, size_t len);
static bool buffer_delete(Buffer *buf, size_t pos, size_t len);
static const char *buffer_store(Editor *ed, const char *data, size_t len);
/* cache layer */
static void cache_piece(Editor *ed, Piece *p);
static bool cache_contains(Editor *ed, Piece *p);
static bool cache_insert(Editor *ed, Piece *p, size_t off, const char *data, size_t len);
static bool cache_delete(Editor *ed, Piece *p, size_t off, size_t len);
/* piece management */
static Piece *piece_alloc(Editor *ed);
static void piece_free(Piece *p);
static void piece_init(Piece *p, Piece *prev, Piece *next, const char *data, size_t len);
static Location piece_get(Editor *ed, size_t pos);
/* span management */
static void span_init(Span *span, Piece *start, Piece *end);
static void span_swap(Editor *ed, Span *old, Span *new);
/* change management */
static Change *change_alloc(Editor *ed);
static void change_free(Change *c);
/* action management */
static Action *action_alloc(Editor *ed);
static void action_free(Action *a);
static void action_push(Action **stack, Action *action);
static Action *action_pop(Action **stack);
/* allocate a new buffer of MAX(size, BUFFER_SIZE) bytes */
static Buffer *buffer_alloc(Editor *ed, size_t size) {
Buffer *buf = calloc(1, sizeof(Buffer));
if (!buf)
return NULL;
if (BUFFER_SIZE > size)
size = BUFFER_SIZE;
if (!(buf->data = malloc(size))) {
free(buf);
return NULL;
}
buf->size = size;
buf->next = ed->buffers;
ed->buffers = buf;
return buf;
}
static void buffer_free(Buffer *buf) {
if (!buf)
return;
free(buf->data);
free(buf);
}
/* check whether buffer has enough free space to store len bytes */
static bool buffer_capacity(Buffer *buf, size_t len) {
return buf->size - buf->len >= len;
}
/* append data to buffer, assumes there is enough space available */
static const char *buffer_append(Buffer *buf, const char *data, size_t len) {
char *dest = memcpy(buf->data + buf->len, data, len);
buf->len += len;
return dest;
}
/* stores the given data in a buffer, allocates a new one if necessary. returns
* a pointer to the storage location or NULL if allocation failed. */
static const char *buffer_store(Editor *ed, const char *data, size_t len) {
Buffer *buf = ed->buffers;
if ((!buf || !buffer_capacity(buf, len)) && !(buf = buffer_alloc(ed, len)))
return NULL;
return buffer_append(buf, data, len);
}
/* insert data into buffer at an arbitrary position, this should only be used with
* data of the most recently created piece. */
static bool buffer_insert(Buffer *buf, size_t pos, const char *data, size_t len) {
if (pos > buf->len || !buffer_capacity(buf, len))
return false;
if (buf->len == pos)
return buffer_append(buf, data, len);
char *insert = buf->data + pos;
memmove(insert + len, insert, buf->len - pos);
memcpy(insert, data, len);
buf->len += len;
return true;
}
/* delete data from a buffer at an arbitrary position, this should only be used with
* data of the most recently created piece. */
static bool buffer_delete(Buffer *buf, size_t pos, size_t len) {
if (pos + len > buf->len)
return false;
if (buf->len == pos) {
buf->len -= len;
return true;
}
char *delete = buf->data + pos;
memmove(delete, delete + len, buf->len - pos - len);
buf->len -= len;
return true;
}
/* cache the given piece if it is the most recently changed one */
static void cache_piece(Editor *ed, Piece *p) {
Buffer *buf = ed->buffers;
if (!buf || p->data < buf->data || p->data + p->len != buf->data + buf->len)
return;
ed->cache = p;
}
/* check whether the given piece was the most recently modified one */
static bool cache_contains(Editor *ed, Piece *p) {
Buffer *buf = ed->buffers;
Action *a = ed->current_action;
if (!buf || !ed->cache || ed->cache != p || !a || !a->change)
return false;
Piece *start = a->change->new.start;
Piece *end = a->change->new.start;
bool found = false;
for (Piece *cur = start; !found; cur = cur->next) {
if (cur == p)
found = true;
if (cur == end)
break;
}
return found && p->data + p->len == buf->data + buf->len;
}
/* try to insert a junk of data at a given piece offset. the insertion is only
* performed if the piece is the most recenetly changed one. the legnth of the
* piece, the span containing it and the whole text is adjusted accordingly */
static bool cache_insert(Editor *ed, Piece *p, size_t off, const char *data, size_t len) {
if (!cache_contains(ed, p))
return false;
Buffer *buf = ed->buffers;
size_t bufpos = p->data + off - buf->data;
if (!buffer_insert(buf, bufpos, data, len))
return false;
p->len += len;
ed->current_action->change->new.len += len;
ed->size += len;
return true;
}
/* try to delete a junk of data at a given piece offset. the deletion is only
* performed if the piece is the most recenetly changed one and the whole
* affected range lies within it. the legnth of the piece, the span containing it
* and the whole text is adjusted accordingly */
static bool cache_delete(Editor *ed, Piece *p, size_t off, size_t len) {
if (!cache_contains(ed, p))
return false;
Buffer *buf = ed->buffers;
size_t bufpos = p->data + off - buf->data;
if (off + len > p->len || !buffer_delete(buf, bufpos, len))
return false;
p->len -= len;
ed->current_action->change->new.len -= len;
ed->size -= len;
return true;
}
/* initialize a span and calculate its length */
static void span_init(Span *span, Piece *start, Piece *end) {
size_t len = 0;
span->start = start;
span->end = end;
for (Piece *p = start; p; p = p->next) {
len += p->len;
if (p == end)
break;
}
span->len = len;
}
/* swap out an old span and replace it with a new one.
*
* - if old is an empty span do not remove anything, just insert the new one
* - if new is an empty span do not insert anything, just remove the old one
*
* adjusts the document size accordingly.
*/
static void span_swap(Editor *ed, Span *old, Span *new) {
/* TODO use a balanced search tree to keep the pieces
instead of a doubly linked list.
*/
if (old->len == 0 && new->len == 0) {
return;
} else if (old->len == 0) {
/* insert new span */
new->start->prev->next = new->start;
new->end->next->prev = new->end;
} else if (new->len == 0) {
/* delete old span */
old->start->prev->next = old->end->next;
old->end->next->prev = old->start->prev;
} else {
/* replace old with new */
old->start->prev->next = new->start;
old->end->next->prev = new->end;
}
ed->size -= old->len;
ed->size += new->len;
}
static void action_push(Action **stack, Action *action) {
action->next = *stack;
*stack = action;
}
static Action *action_pop(Action **stack) {
Action *action = *stack;
if (action)
*stack = action->next;
return action;
}
/* allocate a new action, empty the redo stack and push the new action onto
* the undo stack. all further changes will be associated with this action. */
static Action *action_alloc(Editor *ed) {
Action *old, *new = calloc(1, sizeof(Action));
if (!new)
return NULL;
new->time = time(NULL);
/* throw a away all old redo operations */
while ((old = action_pop(&ed->redo)))
action_free(old);
ed->current_action = new;
action_push(&ed->undo, new);
return new;
}
static void action_free(Action *a) {
if (!a)
return;
for (Change *next, *c = a->change; c; c = next) {
next = c->next;
change_free(c);
}
free(a);
}
static Piece *piece_alloc(Editor *ed) {
Piece *p = calloc(1, sizeof(Piece));
if (!p)
return NULL;
p->editor = ed;
p->index = ++ed->piece_count;
p->global_next = ed->pieces;
if (ed->pieces)
ed->pieces->global_prev = p;
ed->pieces = p;
return p;
}
static void piece_free(Piece *p) {
if (!p)
return;
if (p->global_prev)
p->global_prev->global_next = p->global_next;
if (p->global_next)
p->global_next->global_prev = p->global_prev;
if (p->editor->pieces == p)
p->editor->pieces = p->global_next;
if (p->editor->cache == p)
p->editor->cache = NULL;
free(p);
}
static void piece_init(Piece *p, Piece *prev, Piece *next, const char *data, size_t len) {
p->prev = prev;
p->next = next;
p->data = data;
p->len = len;
}
/* returns the piece holding the text at byte offset pos.
* if pos is zero, then the begin sentinel piece is returned. */
static Location piece_get(Editor *ed, size_t pos) {
Location loc = {};
size_t cur = 0;
for (Piece *p = &ed->begin; p->next; p = p->next) {
if (cur <= pos && pos <= cur + p->len) {
loc.piece = p;
loc.off = pos - cur;
return loc;
}
cur += p->len;
}
return loc;
}
/* allocate a new change, associate it with current action or a newly
* allocated one if none exists. */
static Change *change_alloc(Editor *ed) {
Action *a = ed->current_action;
if (!a) {
a = action_alloc(ed);
if (!a)
return NULL;
}
Change *c = calloc(1, sizeof(Change));
if (!c)
return NULL;
c->next = a->change;
a->change = c;
return c;
}
static void change_free(Change *c) {
/* only free the new part of the span, the old one is still in use */
piece_free(c->new.start);
if (c->new.start != c->new.end)
piece_free(c->new.end);
free(c);
}
/* When inserting new data there are 2 cases to consider.
*
* - in the first the insertion point falls into the middle of an exisiting
* piece which is replaced by three new pieces:
*
* /-+ --> +---------------+ --> +-\
* | | | existing text | | |
* \-+ <-- +---------------+ <-- +-/
* ^
* Insertion point for "demo "
*
* /-+ --> +---------+ --> +-----+ --> +-----+ --> +-\
* | | | existing| |demo | |text | | |
* \-+ <-- +---------+ <-- +-----+ <-- +-----+ <-- +-/
*
* - the second case deals with an insertion point at a piece boundry:
*
* /-+ --> +---------------+ --> +-\
* | | | existing text | | |
* \-+ <-- +---------------+ <-- +-/
* ^
* Insertion point for "short"
*
* /-+ --> +-----+ --> +---------------+ --> +-\
* | | |short| | existing text | | |
* \-+ <-- +-----+ <-- +---------------+ <-- +-/
*/
bool editor_insert_raw(Editor *ed, size_t pos, const char *data, size_t len) {
if (pos > ed->size)
return false;
Location loc = piece_get(ed, pos);
Piece *p = loc.piece;
size_t off = loc.off;
if (cache_insert(ed, p, off, data, len))
return true;
Change *c = change_alloc(ed);
if (!c)
return false;
if (!(data = buffer_store(ed, data, len)))
return false;
Piece *new = NULL;
if (off == p->len) {
/* insert between two existing pieces, hence there is nothing to
* remove, just add a new piece holding the extra text */
if (!(new = piece_alloc(ed)))
return false;
piece_init(new, p, p->next, data, len);
span_init(&c->new, new, new);
span_init(&c->old, NULL, NULL);
} else {
/* insert into middle of an existing piece, therfore split the old
* piece. that is we have 3 new pieces one containing the content
* before the insertion point then one holding the newly inserted
* text and one holding the content after the insertion point.
*/
Piece *before = piece_alloc(ed);
new = piece_alloc(ed);
Piece *after = piece_alloc(ed);
if (!before || !new || !after)
return false;
piece_init(before, p->prev, new, p->data, off);
piece_init(new, before, after, data, len);
piece_init(after, new, p->next, p->data + off, p->len - off);
span_init(&c->new, before, after);
span_init(&c->old, p, p);
}
cache_piece(ed, new);
span_swap(ed, &c->old, &c->new);
return true;
}
bool editor_insert(Editor *ed, size_t pos, const char *data) {
return editor_insert_raw(ed, pos, data, strlen(data));
}
/* undo all changes of the last action, return whether changes existed */
bool editor_undo(Editor *ed) {
Action *a = action_pop(&ed->undo);
if (!a)
return false;
for (Change *c = a->change; c; c = c->next) {
span_swap(ed, &c->new, &c->old);
}
action_push(&ed->redo, a);
return true;
}
/* redo all changes of the last action, return whether changes existed */
bool editor_redo(Editor *ed) {
Action *a = action_pop(&ed->redo);
if (!a)
return false;
for (Change *c = a->change; c; c = c->next) {
span_swap(ed, &c->old, &c->new);
}
action_push(&ed->undo, a);
return true;
}
/* save current content to given filename. the data is first saved to
* a file called `.filename.tmp` and then atomically moved to its final
* (possibly alredy existing) destination using rename(2).
*/
int editor_save(Editor *ed, const char *filename) {
size_t len = strlen(filename) + 10;
char tmpname[len];
snprintf(tmpname, len, ".%s.tmp", filename);
// TODO file ownership, permissions etc
/* O_RDWR is needed because otherwise we can't map with MAP_SHARED */
int fd = open(tmpname, O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR);
if (fd == -1)
return -1;
if (ftruncate(fd, ed->size) == -1)
goto err;
if (ed->size > 0) {
void *buf = mmap(NULL, ed->size, PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED)
goto err;
char *cur = buf;
editor_iterate(ed, it, 0) {
size_t len = it.end - it.start;
memcpy(cur, it.start, len);
cur += len;
}
if (munmap(buf, ed->size) == -1)
goto err;
}
if (close(fd) == -1)
return -1;
if (rename(tmpname, filename) == -1)
return -1;
ed->saved_action = ed->undo;
editor_snapshot(ed);
return 0;
err:
close(fd);
return -1;
}
/* load the given file as starting point for further editing operations.
* to start with an empty document, pass NULL as filename. */
Editor *editor_load(const char *filename) {
Editor *ed = calloc(1, sizeof(Editor));
if (!ed)
return NULL;
ed->begin.index = 1;
ed->end.index = 2;
ed->piece_count = 2;
piece_init(&ed->begin, NULL, &ed->end, NULL, 0);
piece_init(&ed->end, &ed->begin, NULL, NULL, 0);
if (filename) {
ed->filename = filename;
ed->fd = open(filename, O_RDONLY);
if (ed->fd == -1)
goto out;
if (fstat(ed->fd, &ed->info) == -1)
goto out;
if (!S_ISREG(ed->info.st_mode))
goto out;
// XXX: use lseek(fd, 0, SEEK_END); instead?
ed->buf.size = ed->info.st_size;
ed->buf.data = mmap(NULL, ed->info.st_size, PROT_READ, MAP_SHARED, ed->fd, 0);
if (ed->buf.data == MAP_FAILED)
goto out;
Piece *p = piece_alloc(ed);
if (!p)
goto out;
piece_init(&ed->begin, NULL, p, NULL, 0);
piece_init(p, &ed->begin, &ed->end, ed->buf.data, ed->buf.size);
piece_init(&ed->end, p, NULL, NULL, 0);
ed->size = ed->buf.size;
}
return ed;
out:
if (ed->fd > 2)
close(ed->fd);
editor_free(ed);
return NULL;
}
static void print_piece(Piece *p) {
fprintf(stderr, "index: %d\tnext: %d\tprev: %d\t len: %d\t data: %p\n", p->index,
p->next ? p->next->index : -1,
p->prev ? p->prev->index : -1,
p->len, p->data);
fflush(stderr);
write(1, p->data, p->len);
write(1, "\n", 1);
}
void editor_debug(Editor *ed) {
for (Piece *p = &ed->begin; p; p = p->next) {
print_piece(p);
}
}
/* A delete operation can either start/stop midway through a piece or at
* a boundry. In the former case a new piece is created to represent the
* remaining text before/after the modification point.
*
* /-+ --> +---------+ --> +-----+ --> +-----+ --> +-\
* | | | existing| |demo | |text | | |
* \-+ <-- +---------+ <-- +-----+ <-- +-----+ <-- +-/
* ^ ^
* |------ delete range -----|
*
* /-+ --> +----+ --> +--+ --> +-\
* | | | exi| |t | | |
* \-+ <-- +----+ <-- +--+ <-- +-/
*/
bool editor_delete(Editor *ed, size_t pos, size_t len) {
if (len == 0)
return true;
if (pos + len > ed->size)
return false;
Location loc = piece_get(ed, pos);
Piece *p = loc.piece;
size_t off = loc.off;
if (cache_delete(ed, p, off, len))
return true;
size_t cur; // how much has already been deleted
bool midway_start = false, midway_end = false;
Change *c = change_alloc(ed);
if (!c)
return false;
Piece *before, *after; // unmodified pieces before / after deletion point
Piece *start, *end; // span which is removed
if (off == p->len) {
/* deletion starts at a piece boundry */
cur = 0;
before = p;
start = p->next;
} else {
/* deletion starts midway through a piece */
midway_start = true;
cur = p->len - off;
start = p;
before = piece_alloc(ed);
}
/* skip all pieces which fall into deletion range */
while (cur < len) {
p = p->next;
cur += p->len;
}
if (cur == len) {
/* deletion stops at a piece boundry */
end = p;
after = p->next;
} else { // cur > len
/* deletion stops midway through a piece */
midway_end = true;
end = p;
after = piece_alloc(ed);
piece_init(after, before, p->next, p->data + p->len - (cur - len), cur - len);
}
if (midway_start) {
/* we finally know which piece follows our newly allocated before piece */
piece_init(before, start->prev, after, start->data, off);
}
Piece *new_start = NULL, *new_end = NULL;
if (midway_start) {
new_start = before;
if (!midway_end)
new_end = before;
}
if (midway_end) {
if (!midway_start)
new_start = after;
new_end = after;
}
span_init(&c->new, new_start, new_end);
span_init(&c->old, start, end);
span_swap(ed, &c->old, &c->new);
return true;
}
bool editor_replace_raw(Editor *ed, size_t pos, const char *data, size_t len) {
if (!editor_delete(ed, pos, len))
return false;
return editor_insert_raw(ed, pos, data, len);
}
bool editor_replace(Editor *ed, size_t pos, const char *data) {
return editor_replace_raw(ed, pos, data, strlen(data));
}
/* preserve the current text content such that it can be restored by
* means of undo/redo operations */
void editor_snapshot(Editor *ed) {
ed->current_action = NULL;
ed->cache = NULL;
}
void editor_free(Editor *ed) {
if (!ed)
return;
Action *a;
while ((a = action_pop(&ed->undo)))
action_free(a);
while ((a = action_pop(&ed->redo)))
action_free(a);
for (Piece *next, *p = ed->pieces; p; p = next) {
next = p->global_next;
piece_free(p);
}
for (Buffer *next, *buf = ed->buffers; buf; buf = next) {
next = buf->next;
buffer_free(buf);
}
if (ed->buf.data)
munmap(ed->buf.data, ed->buf.size);
free(ed);
}
bool editor_modified(Editor *ed) {
return ed->saved_action != ed->undo;
}
static bool editor_iterator_init(Iterator *it, Piece *p, size_t off) {
*it = (Iterator){
.piece = p,
.start = p ? p->data : NULL,
.end = p ? p->data + p->len : NULL,
.text = p ? p->data + off : NULL,
};
return editor_iterator_valid(it);
}
Iterator editor_iterator_get(Editor *ed, size_t pos) {
Iterator it;
Location loc = piece_get(ed, pos);
Piece *p = loc.piece;
editor_iterator_init(&it, p == &ed->begin ? p->next : p, loc.off);
return it;
}
Iterator editor_iterator_byte_get(Editor *ed, size_t pos, char *b) {
Iterator it = editor_iterator_get(ed, pos);
editor_iterator_byte_peek(&it, b);
return it;
}
bool editor_iterator_next(Iterator *it) {
return editor_iterator_init(it, it->piece ? it->piece->next : NULL, 0);
}
bool editor_iterator_prev(Iterator *it) {
return editor_iterator_init(it, it->piece ? it->piece->prev : NULL, 0);
}
bool editor_iterator_valid(const Iterator *it) {
/* filter out sentinel nodes */
return it->piece && it->piece->editor;
}
bool editor_iterator_byte_peek(Iterator *it, char *b) {
if (editor_iterator_valid(it) && it->start <= it->text && it->text < it->end) {
*b = *it->text;
return true;
}
return false;
}
bool editor_iterator_byte_next(Iterator *it, char *b) {
if (!editor_iterator_valid(it))
return false;
it->text++;
while (it->text == it->end) {
if (!editor_iterator_next(it))
return false;
it->text = it->start;
}
if (b)
*b = *it->text;
return true;
}
bool editor_iterator_byte_prev(Iterator *it, char *b) {
if (!editor_iterator_valid(it))
return false;
while (it->text == it->start) {
if (!editor_iterator_prev(it))
return false;
it->text = it->end;
}
--it->text;
if (b)
*b = *it->text;
return true;
}
size_t editor_bytes_get(Editor *ed, size_t pos, size_t len, char *buf) {
if (!buf)
return 0;
char *cur = buf;
size_t rem = len;
editor_iterate(ed, it, pos) {
if (rem == 0)
break;
size_t piece_len = it.end - it.text;
if (piece_len > rem)
piece_len = rem;
memcpy(cur, it.text, piece_len);
cur += piece_len;
rem -= piece_len;
}
return len - rem;
}
size_t editor_size(Editor *ed) {
return ed->size;
}
|