-
Notifications
You must be signed in to change notification settings - Fork 53
/
buffer.c
1987 lines (1761 loc) · 61.1 KB
/
buffer.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
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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <utlist.h>
#include <inttypes.h>
#include <wchar.h>
#include "mlbuf.h"
static int _buffer_open_mmap(buffer_t *self, int fd, size_t size);
static int _buffer_open_read(buffer_t *self, int fd, size_t size);
static int _buffer_bline_unslab(bline_t *self);
static void _buffer_stat(buffer_t *self);
static int _buffer_baction_do(buffer_t *self, bline_t *bline, baction_t *action, int is_redo, bint_t *opt_repeat_offset);
static int _buffer_update(buffer_t *self, baction_t *action);
static int _buffer_undo(buffer_t *self, int by_group);
static int _buffer_redo(buffer_t *self, int by_group);
static int _buffer_truncate_undo_stack(buffer_t *self, baction_t *action_from);
static int _buffer_add_to_undo_stack(buffer_t *self, baction_t *action);
static int _buffer_apply_styles_all(bline_t *bline, bint_t min_nlines);
static int _buffer_match_srule(bline_t *bline, bint_t look_offset, srule_t *srule, int end_rule, bint_t *ret_start, bint_t *ret_stop);
static void _buffer_bline_reset_styles(bline_t *bline);
static void _buffer_bline_style(bline_t *bline, bint_t start, bint_t stop, sblock_t *style);
static bline_t *_buffer_bline_new(buffer_t *self);
static int _buffer_bline_free(bline_t *bline, bline_t *maybe_mark_line, bint_t col_delta);
static bline_t *_buffer_bline_break(bline_t *bline, bint_t col);
static void _buffer_find_end_pos(bline_t *start_line, bint_t start_col, bint_t num_chars, bline_t **ret_end_line, bint_t *ret_end_col, bint_t *ret_safe_num_chars);
static void _buffer_bline_replace(bline_t *bline, bint_t start_col, char *data, bint_t data_len, str_t *del_data);
static bint_t _buffer_bline_insert(bline_t *bline, bint_t col, char *data, bint_t data_len, int move_marks);
static bint_t _buffer_bline_delete(bline_t *bline, bint_t col, bint_t num_chars);
static bint_t _buffer_bline_col_to_index(bline_t *bline, bint_t col);
static bint_t _buffer_bline_index_to_col(bline_t *bline, bint_t index);
static int _buffer_munmap(buffer_t *self);
static int _baction_destroy(baction_t *action);
static void _bline_advance_col(bline_t **self, bint_t *col);
// Make a new buffer and return it
buffer_t *buffer_new(void) {
buffer_t *buffer;
bline_t *bline;
buffer = calloc(1, sizeof(buffer_t));
buffer->tab_width = 4;
bline = _buffer_bline_new(buffer);
buffer->first_line = bline;
buffer->last_line = bline;
buffer->line_count = 1;
buffer->mmap_fd = -1;
return buffer;
}
// Wrapper for buffer_new + buffer_open
buffer_t *buffer_new_open(char *path) {
buffer_t *self;
self = buffer_new();
if (buffer_open(self, path) != MLBUF_OK) {
buffer_destroy(self);
return NULL;
}
return self;
}
// Read buffer from path
int buffer_open(buffer_t *self, char *path) {
int rc;
struct stat st;
int fd;
fd = -1;
rc = MLBUF_OK;
do {
// Exit early if path is empty
if (!path || strlen(path) < 1) {
rc = MLBUF_ERR;
break;
}
// Open file for reading
if ((fd = open(path, O_RDONLY)) < 0) {
rc = MLBUF_ERR;
break;
}
// Stat file
if (fstat(fd, &st) < 0) {
rc = MLBUF_ERR;
break;
}
// Read or mmap file into buffer
self->is_in_open = 1;
if (st.st_size >= MLBUF_LARGE_FILE_SIZE) {
if (_buffer_open_mmap(self, fd, st.st_size) != MLBUF_OK) {
// mmap failed so fallback to normal read
if (_buffer_open_read(self, fd, st.st_size) != MLBUF_OK) {
rc = MLBUF_ERR;
break;
}
}
} else {
if (_buffer_open_read(self, fd, st.st_size) != MLBUF_OK) {
rc = MLBUF_ERR;
break;
}
}
self->is_in_open = 0;
} while(0);
if (fd >= 0) close(fd);
if (rc == MLBUF_ERR) return rc;
// Set path
if (self->path) free(self->path);
self->path = strdup(path);
self->is_unsaved = 0;
// Remember stat
_buffer_stat(self);
return MLBUF_OK;
}
// Write buffer to path
int buffer_save(buffer_t *self) {
return buffer_save_as(self, self->path, NULL);
}
// Write buffer to specified path
int buffer_save_as(buffer_t *self, char *path, bint_t *optret_nbytes) {
FILE *fp;
size_t nbytes;
if (optret_nbytes) *optret_nbytes = 0;
// Exit early if path is empty
if (!path || strlen(path) < 1) {
return MLBUF_ERR;
}
// Open file for writing
if (!(fp = fopen(path, "wb"))) {
return MLBUF_ERR;
}
// Write data to file
nbytes = 0;
buffer_write_to_file(self, fp, &nbytes);
fclose(fp);
if (optret_nbytes) *optret_nbytes = (bint_t)nbytes;
if ((bint_t)nbytes != self->byte_count) return MLBUF_ERR;
// Set path
if (self->path != path) {
if (self->path) free(self->path);
self->path = strdup(path);
}
self->is_unsaved = 0;
// Remember stat
_buffer_stat(self);
return MLBUF_OK;
}
// Write buffer data to FILE*
int buffer_write_to_file(buffer_t *self, FILE *fp, size_t *optret_nbytes) {
return buffer_write_to_fd(self, fileno(fp), optret_nbytes);
}
// Write buffer data to file descriptor
int buffer_write_to_fd(buffer_t *self, int fd, size_t *optret_nbytes) {
bline_t *bline;
size_t nbytes;
ssize_t write_rc;
nbytes = 0;
#define MLBUF_BUFFER_WRITE_CHECK(pd, pl) do { \
write_rc = write(fd, (pd), (pl)); \
if (write_rc < (pl)) { \
return MLBUF_ERR; \
} \
nbytes += write_rc; \
} while(0)
for (bline = self->first_line; bline; bline = bline->next) {
if (bline->data_len > 0) MLBUF_BUFFER_WRITE_CHECK(bline->data, bline->data_len);
if (bline->next) MLBUF_BUFFER_WRITE_CHECK("\n", 1);
}
if (optret_nbytes) *optret_nbytes = nbytes;
return MLBUF_OK;
}
// Free a buffer
int buffer_destroy(buffer_t *self) {
bline_t *line;
bline_t *line_tmp;
baction_t *action;
baction_t *action_tmp;
char c;
for (line = self->last_line; line; ) {
line_tmp = line->prev;
_buffer_bline_free(line, NULL, 0);
line = line_tmp;
}
if (self->data) free(self->data);
if (self->path) free(self->path);
DL_FOREACH_SAFE(self->actions, action, action_tmp) {
DL_DELETE(self->actions, action);
_baction_destroy(action);
}
for (c = 'a'; c <= 'z'; c++) buffer_register_clear(self, c);
_buffer_munmap(self);
if (self->slabbed_blines) free(self->slabbed_blines);
if (self->slabbed_chars) free(self->slabbed_chars);
free(self);
return MLBUF_OK;
}
// Add a mark to this buffer and return it
mark_t *buffer_add_mark(buffer_t *self, bline_t *maybe_line, bint_t maybe_col) {
return buffer_add_mark_ex(self, '\0', maybe_line, maybe_col);
}
// If letter is [a-z], add a lettered mark and return it.
// If letter is \0, add a non-lettered mark and return it.
// Otherwise do nothing and return NULL.
mark_t *buffer_add_mark_ex(buffer_t *self, char letter, bline_t *maybe_line, bint_t maybe_col) {
mark_t *mark;
mark_t *mark_tmp;
if (!((letter >= 'a' && letter <= 'z') || letter == '\0')) {
return NULL;
}
mark = calloc(1, sizeof(mark_t));
mark->letter = letter;
MLBUF_MAKE_GT_EQ0(maybe_col);
if (maybe_line != NULL) {
MLBUF_BLINE_ENSURE_CHARS(maybe_line);
mark->bline = maybe_line;
mark->col = maybe_col < 0 ? 0 : MLBUF_MIN(maybe_line->char_count, maybe_col);
} else {
mark->bline = self->first_line;
mark->col = 0;
}
DL_APPEND(mark->bline->marks, mark);
if (mark->letter) {
if ((mark_tmp = MLBUF_LETT_MARK(self, mark->letter)) != NULL) {
buffer_destroy_mark(self, mark_tmp);
}
MLBUF_LETT_MARK(self, mark->letter) = mark;
}
return mark;
}
// Return lettered mark or NULL if it does not exist
int buffer_get_lettered_mark(buffer_t *self, char letter, mark_t **ret_mark) {
MLBUF_ENSURE_AZ(letter);
*ret_mark = MLBUF_LETT_MARK(self, letter);
return MLBUF_OK;
}
// Remove mark from buffer and free it, removing any range srules that use it
int buffer_destroy_mark(buffer_t *self, mark_t *mark) {
srule_node_t *node;
srule_node_t *node_tmp;
DL_DELETE(mark->bline->marks, mark);
if (mark->letter) MLBUF_LETT_MARK(self, mark->letter) = NULL;
DL_FOREACH_SAFE(self->range_srules, node, node_tmp) {
if (node->srule->range_a == mark
|| node->srule->range_b == mark
) {
buffer_remove_srule(self, node->srule);
}
}
free(mark);
return MLBUF_OK;
}
// Get buffer contents and length
int buffer_get(buffer_t *self, char **ret_data, bint_t *ret_data_len) {
bline_t *bline;
char *data_cursor;
bint_t alloc_size;
if (self->is_data_dirty) {
// Refresh self->data
alloc_size = self->byte_count + 2;
self->data = self->data != NULL
? realloc(self->data, alloc_size)
: malloc(alloc_size);
data_cursor = self->data;
for (bline = self->first_line; bline != NULL; bline = bline->next) {
if (bline->data_len > 0) {
memcpy(data_cursor, bline->data, bline->data_len);
data_cursor += bline->data_len;
self->data_len += bline->data_len;
}
if (bline->next) {
*data_cursor = '\n';
data_cursor += 1;
}
}
*data_cursor = '\0';
self->data_len = (bint_t)(data_cursor - self->data);
self->is_data_dirty = 0;
}
*ret_data = self->data;
*ret_data_len = self->data_len;
return MLBUF_OK;
}
int buffer_clear(buffer_t *self) {
return buffer_delete(self, 0, self->byte_count);
}
// Set buffer contents
int buffer_set(buffer_t *self, char *data, bint_t data_len) {
int rc;
MLBUF_MAKE_GT_EQ0(data_len);
if ((rc = buffer_clear(self)) != MLBUF_OK) {
return rc;
}
rc = buffer_insert(self, 0, data, data_len, NULL);
if (self->actions) _buffer_truncate_undo_stack(self, self->actions);
return rc;
}
// Set buffer contents more efficiently
int buffer_set_mmapped(buffer_t *self, char *data, bint_t data_len) {
bint_t nlines;
bint_t line_num;
bline_t *blines;
bint_t data_remaining_len;
char *data_cursor;
char *data_newline;
bint_t line_len;
if (buffer_clear(self) != MLBUF_OK) {
return MLBUF_ERR;
}
// Count number of lines
nlines = 1;
data_cursor = data;
data_remaining_len = data_len;
while (data_remaining_len > 0 && (data_newline = memchr(data_cursor, '\n', data_remaining_len)) != NULL) {
data_remaining_len -= (bint_t)(data_newline - data_cursor) + 1;
data_cursor = data_newline + 1;
nlines += 1;
}
// Allocate blines and chars. These are freed in buffer_destroy.
self->slabbed_chars = calloc(data_len, sizeof(bline_char_t));
self->slabbed_blines = malloc(nlines * sizeof(bline_t));
blines = self->slabbed_blines;
// Populate blines
line_num = 0;
data_cursor = data;
data_remaining_len = data_len;
while (1) {
data_newline = data_remaining_len > 0
? memchr(data_cursor, '\n', data_remaining_len)
: NULL;
line_len = data_newline ?
(bint_t)(data_newline - data_cursor)
: data_remaining_len;
blines[line_num] = (bline_t){
.buffer = self,
.data = data_cursor,
.data_len = line_len,
.data_cap = line_len,
.line_index = line_num,
.char_count = line_len,
.char_vwidth = line_len,
.chars = (self->slabbed_chars + (data_len - data_remaining_len)),
.chars_cap = line_len,
.marks = NULL,
.eol_rule = NULL,
.is_chars_dirty = 1,
.is_slabbed = 1,
.is_data_slabbed = 1,
.next = NULL,
.prev = NULL
};
if (line_num > 0) {
blines[line_num-1].next = blines + line_num;
blines[line_num].prev = blines + (line_num-1);
}
if (data_newline) {
data_remaining_len -= line_len + 1;
data_cursor = data_newline + 1;
line_num += 1;
} else {
break;
}
}
if (self->first_line) _buffer_bline_free(self->first_line, NULL, 0);
self->first_line = blines;
self->last_line = blines + line_num;
self->byte_count = data_len;
self->line_count = line_num + 1;
self->is_data_dirty = 1;
return MLBUF_OK;
}
// Insert data into buffer given a buffer offset
int buffer_insert(buffer_t *self, bint_t offset, char *data, bint_t data_len, bint_t *optret_num_chars) {
int rc;
bline_t *start_line;
bint_t start_col;
MLBUF_MAKE_GT_EQ0(offset);
// Find start line and col
if ((rc = buffer_get_bline_col(self, offset, &start_line, &start_col)) != MLBUF_OK) {
return rc;
}
return buffer_insert_w_bline(self, start_line, start_col, data, data_len, optret_num_chars);
}
// Insert data into buffer given a bline/col
int buffer_insert_w_bline(buffer_t *self, bline_t *start_line, bint_t start_col, char *data, bint_t data_len, bint_t *optret_num_chars) {
bline_t *cur_line;
bint_t cur_col;
bline_t *new_line;
char *data_cursor;
char *data_newline;
bint_t data_remaining_len;
bint_t insert_len;
bint_t num_lines_added;
char *ins_data;
bint_t ins_data_len;
bint_t ins_data_nchars;
baction_t *action;
MLBUF_MAKE_GT_EQ0(data_len);
// Exit early if no data
if (data_len < 1) {
return MLBUF_OK;
}
// Insert lines
data_cursor = data;
data_remaining_len = data_len;
cur_line = start_line;
cur_col = start_col;
num_lines_added = 0;
while (data_remaining_len > 0 && (data_newline = memchr(data_cursor, '\n', data_remaining_len)) != NULL) {
insert_len = (bint_t)(data_newline - data_cursor);
new_line = _buffer_bline_break(cur_line, cur_col);
num_lines_added += 1;
if (insert_len > 0) {
_buffer_bline_insert(cur_line, cur_col, data_cursor, insert_len, 1);
}
data_remaining_len -= (data_newline - data_cursor) + 1;
data_cursor = data_newline + 1;
cur_line = new_line;
cur_col = 0;
}
if (data_remaining_len > 0) {
cur_col += _buffer_bline_insert(cur_line, cur_col, data_cursor, data_remaining_len, 1);
}
// Get inserted data
buffer_substr(self, start_line, start_col, cur_line, cur_col, &ins_data, &ins_data_len, &ins_data_nchars);
// Add baction
action = calloc(1, sizeof(baction_t));
action->type = MLBUF_BACTION_TYPE_INSERT;
action->buffer = self;
action->start_line = start_line;
action->start_line_index = start_line->line_index;
action->start_col = start_col;
action->maybe_end_line = cur_line;
action->maybe_end_line_index = action->start_line_index + num_lines_added;
action->maybe_end_col = cur_col;
action->byte_delta = (bint_t)ins_data_len;
action->char_delta = (bint_t)ins_data_nchars;
action->line_delta = (bint_t)num_lines_added;
action->data = ins_data;
action->data_len = ins_data_len;
_buffer_update(self, action);
if (optret_num_chars) *optret_num_chars = ins_data_nchars;
return MLBUF_OK;
}
// Delete data from buffer given an offset
int buffer_delete(buffer_t *self, bint_t offset, bint_t num_chars) {
bline_t *start_line;
bint_t start_col;
MLBUF_MAKE_GT_EQ0(offset);
buffer_get_bline_col(self, offset, &start_line, &start_col);
return buffer_delete_w_bline(self, start_line, start_col, num_chars);
}
// Delete data from buffer given a bline/col
int buffer_delete_w_bline(buffer_t *self, bline_t *start_line, bint_t start_col, bint_t num_chars) {
bline_t *end_line;
bint_t end_col;
bline_t *tmp_line;
bline_t *swap_line;
bline_t *next_line;
bint_t tmp_len;
char *del_data;
bint_t del_data_len;
bint_t del_data_nchars;
bint_t num_lines_removed;
bint_t safe_num_chars;
bint_t orig_char_count;
baction_t *action;
MLBUF_MAKE_GT_EQ0(num_chars);
// Find end line and col
_buffer_find_end_pos(start_line, start_col, num_chars, &end_line, &end_col, &num_chars);
// Exit early if there is nothing to delete
MLBUF_BLINE_ENSURE_CHARS(self->last_line);
if (start_line == end_line && start_col >= end_col) {
return MLBUF_OK;
} else if (start_line == self->last_line && start_col == self->last_line->char_count) {
return MLBUF_OK;
}
// Get deleted data
buffer_substr(self, start_line, start_col, end_line, end_col, &del_data, &del_data_len, &del_data_nchars);
// Delete suffix starting at start_line:start_col
MLBUF_BLINE_ENSURE_CHARS(start_line);
safe_num_chars = MLBUF_MIN(num_chars, start_line->char_count - start_col);
if (safe_num_chars > 0) {
_buffer_bline_delete(start_line, start_col, safe_num_chars);
}
// Copy remaining portion of end_line to start_line:start_col
MLBUF_BLINE_ENSURE_CHARS(start_line);
orig_char_count = start_line->char_count;
if (start_line != end_line && (tmp_len = end_line->data_len - _buffer_bline_col_to_index(end_line, end_col)) > 0) {
_buffer_bline_insert(
start_line,
start_col,
end_line->data + (end_line->data_len - tmp_len),
tmp_len,
0
);
}
// Remove lines after start_line thru end_line
// Relocate marks to end of start_line
num_lines_removed = 0;
swap_line = end_line->next;
next_line = NULL;
tmp_line = start_line->next;
while (tmp_line != NULL && tmp_line != swap_line) {
next_line = tmp_line->next;
_buffer_bline_free(tmp_line, start_line, orig_char_count - end_col);
num_lines_removed += 1;
tmp_line = next_line;
}
start_line->next = swap_line;
if (swap_line) swap_line->prev = start_line;
// Add baction
action = calloc(1, sizeof(baction_t));
action->type = MLBUF_BACTION_TYPE_DELETE;
action->buffer = self;
action->start_line = start_line;
action->start_line_index = start_line->line_index;
action->start_col = start_col;
action->byte_delta = -1 * (bint_t)del_data_len;
action->char_delta = -1 * (bint_t)del_data_nchars;
action->line_delta = -1 * (bint_t)num_lines_removed;
action->data = del_data;
action->data_len = del_data_len;
_buffer_update(self, action);
return MLBUF_OK;
}
// Replace num_chars in buffer at offset with data
int buffer_replace(buffer_t *self, bint_t offset, bint_t num_chars, char *data, bint_t data_len) {
int rc;
bline_t *start_line;
bint_t start_col;
MLBUF_MAKE_GT_EQ0(offset);
// Find start line and col
if ((rc = buffer_get_bline_col(self, offset, &start_line, &start_col)) != MLBUF_OK) {
return rc;
}
return buffer_replace_w_bline(self, start_line, start_col, num_chars, data, data_len);
}
// Replace num_chars from start_line:start_col with data
int buffer_replace_w_bline(buffer_t *self, bline_t *start_line, bint_t start_col, bint_t del_chars, char *data, bint_t data_len) {
bline_t *cur_line;
bint_t cur_col;
bint_t insert_rem;
bint_t delete_rem;
bint_t data_linelen;
bint_t nchars_ins;
bint_t nlines;
char *data_cursor;
char *data_newline;
baction_t *action;
str_t del_data = {0};
// Replace data on common lines
insert_rem = data_len;
delete_rem = del_chars;
cur_line = start_line;
cur_col = start_col;
data_cursor = data;
nchars_ins = 0;
nlines = 0;
MLBUF_BLINE_ENSURE_CHARS(cur_line);
while (insert_rem > 0 && delete_rem > (cur_line->char_count - cur_col)) {
data_newline = memchr(data_cursor, '\n', insert_rem);
data_linelen = data_newline ? (bint_t)(data_newline - data_cursor) : insert_rem;
delete_rem -= cur_line->char_count - cur_col;
_buffer_bline_replace(cur_line, cur_col, data_cursor, data_linelen, &del_data);
nchars_ins += cur_line->char_count - cur_col;
insert_rem -= data_linelen;
data_cursor += data_linelen;
cur_col = cur_line->char_count;
if (data_newline && insert_rem >= 2 && delete_rem >= 2 && cur_line->next) {
data_cursor += 1;
insert_rem -= 1;
delete_rem -= 1;
cur_line = cur_line->next;
cur_col = 0;
nchars_ins += 1;
str_append_len(&del_data, "\n", 1);
} else {
break;
}
nlines += 1;
MLBUF_BLINE_ENSURE_CHARS(cur_line);
}
// Add delete baction
if (del_data.len > 0) {
action = calloc(1, sizeof(baction_t));
action->type = MLBUF_BACTION_TYPE_DELETE;
action->buffer = self;
action->start_line = start_line;
action->start_line_index = start_line->line_index;
action->start_col = start_col;
action->byte_delta = -1 * (bint_t)del_data.len;
action->char_delta = -1 * (del_chars - delete_rem);
action->line_delta = -1 * nlines;
action->data = del_data.data;
action->data_len = (bint_t)del_data.len;
_buffer_update(self, action);
}
// Add insert baction
if (data_len - insert_rem > 0) {
action = calloc(1, sizeof(baction_t));
action->type = MLBUF_BACTION_TYPE_INSERT;
action->buffer = self;
action->start_line = start_line;
action->start_line_index = start_line->line_index;
action->start_col = start_col;
action->maybe_end_line = cur_line;
action->maybe_end_line_index = action->start_line_index + nlines;
action->maybe_end_col = cur_col;
action->byte_delta = data_len - insert_rem;
action->char_delta = nchars_ins;
action->line_delta = nlines;
action->data = strndup(data, action->byte_delta);
action->data_len = action->byte_delta;
_buffer_update(self, action);
}
// Delete left over data
if (delete_rem > 0) {
buffer_delete_w_bline(self, cur_line, cur_col, delete_rem);
}
// Insert left over data
if (insert_rem > 0) {
buffer_insert_w_bline(self, cur_line, cur_col, data_cursor, insert_rem, NULL);
}
return MLBUF_OK;
}
// Return a line given a line_index
int buffer_get_bline(buffer_t *self, bint_t line_index, bline_t **ret_bline) {
return buffer_get_bline_w_hint(self, line_index, self->first_line, ret_bline);
}
// Return a line given a line_index, starting at hint and iterating outward
int buffer_get_bline_w_hint(buffer_t *self, bint_t line_index, bline_t *opt_hint, bline_t **ret_bline) {
bline_t *fwd, *rev, *found;
MLBUF_MAKE_GT_EQ0(line_index);
if (!opt_hint) {
opt_hint = self->first_line;
if (!opt_hint) { // Satisfy scan-build lint
*ret_bline = NULL;
return MLBUF_ERR;
}
}
fwd = opt_hint;
rev = opt_hint->prev;
found = NULL;
while (fwd || rev) {
if (fwd) {
if (fwd->line_index == line_index) {
found = fwd;
break;
}
fwd = fwd != fwd->next ? fwd->next : NULL;
}
if (rev) {
if (rev->line_index == line_index) {
found = rev;
break;
}
rev = rev != rev->prev ? rev->prev : NULL;
}
}
if (found) {
*ret_bline = found;
return MLBUF_OK;
}
*ret_bline = self->last_line;
return MLBUF_ERR;
}
// Return a line and col for the given offset
int buffer_get_bline_col(buffer_t *self, bint_t offset, bline_t **ret_bline, bint_t *ret_col) {
bline_t *tmp_line;
bline_t *good_line = NULL;
bint_t remaining_chars;
MLBUF_MAKE_GT_EQ0(offset);
// TODO convert to binary search for perf win
remaining_chars = offset;
for (tmp_line = self->first_line; tmp_line != NULL; tmp_line = tmp_line->next) {
MLBUF_BLINE_ENSURE_CHARS(tmp_line);
if (tmp_line->char_count >= remaining_chars) {
*ret_bline = tmp_line;
*ret_col = remaining_chars;
return MLBUF_OK;
} else {
remaining_chars -= (tmp_line->char_count + 1); // Plus 1 for newline
}
good_line = tmp_line;
}
if (!good_line) good_line = self->first_line;
*ret_bline = good_line;
*ret_col = good_line->char_count;
return MLBUF_OK;
}
// Return an offset given a line and col
int buffer_get_offset(buffer_t *self, bline_t *bline, bint_t col, bint_t *ret_offset) {
bline_t *tmp_line;
bint_t offset;
MLBUF_MAKE_GT_EQ0(col);
// TODO cache for perf win
offset = 0;
for (tmp_line = self->first_line; tmp_line != bline->next; tmp_line = tmp_line->next) {
MLBUF_BLINE_ENSURE_CHARS(tmp_line);
if (tmp_line == bline) {
offset += MLBUF_MIN(tmp_line->char_count, col);
break;
} else {
offset += tmp_line->char_count + 1; // Plus 1 for newline
}
}
*ret_offset = offset;
return MLBUF_OK;
}
// Add a style rule to the buffer
int buffer_add_srule(buffer_t *self, srule_t *srule) {
srule_node_t *node;
node = calloc(1, sizeof(srule_node_t));
node->srule = srule;
if (srule->type == MLBUF_SRULE_TYPE_SINGLE || srule->type == MLBUF_SRULE_TYPE_MULTI) {
DL_APPEND(self->srules, node);
} else if (srule->type == MLBUF_SRULE_TYPE_RANGE) {
srule->range_a->range_srule = srule;
srule->range_b->range_srule = srule;
DL_APPEND(self->range_srules, node);
return MLBUF_OK; // range styles get applied in bview_draw
} else {
free(node);
return MLBUF_ERR;
}
return buffer_apply_styles(self, self->first_line, self->line_count - 1);
}
// Remove a style rule from the buffer
int buffer_remove_srule(buffer_t *self, srule_t *srule) {
int found;
srule_node_t **head;
srule_node_t *node;
srule_node_t *node_tmp;
if (srule->type == MLBUF_SRULE_TYPE_SINGLE || srule->type == MLBUF_SRULE_TYPE_MULTI) {
head = &self->srules;
} else if (srule->type == MLBUF_SRULE_TYPE_RANGE) {
head = &self->range_srules;
} else {
return MLBUF_ERR;
}
found = 0;
DL_FOREACH_SAFE(*head, node, node_tmp) {
if (node->srule != srule) continue;
if (srule->type == MLBUF_SRULE_TYPE_RANGE) {
srule->range_a->range_srule = NULL;
srule->range_b->range_srule = NULL;
}
DL_DELETE(*head, node);
free(node);
found = 1;
break;
}
if (!found) return MLBUF_ERR;
if (srule->type == MLBUF_SRULE_TYPE_RANGE) {
return MLBUF_OK; // range styles get applied in bview_draw
}
return buffer_apply_styles(self, self->first_line, self->line_count - 1);
}
// Set callback to cb. Pass in NULL to unset callback.
int buffer_set_callback(buffer_t *self, buffer_callback_t cb, void *udata) {
if (cb) {
self->callback = cb;
self->callback_udata = udata;
} else {
self->callback = NULL;
self->callback_udata = NULL;
}
return MLBUF_OK;
}
// Set tab_width and recalculate all line char vwidths
int buffer_set_tab_width(buffer_t *self, int tab_width) {
bline_t *tmp_line;
if (tab_width < 1) {
return MLBUF_ERR;
}
// TODO invalidate by incrementing a version scalar or something
self->tab_width = tab_width;
for (tmp_line = self->first_line; tmp_line; tmp_line = tmp_line->next) {
bline_count_chars(tmp_line);
}
return MLBUF_OK;
}
// Return data from start_line:start_col thru end_line:end_col
int buffer_substr(buffer_t *self, bline_t *start_line, bint_t start_col, bline_t *end_line, bint_t end_col, char **ret_data, bint_t *ret_data_len, bint_t *ret_data_nchars) {
char *data;
bint_t data_len;
bint_t data_size;
bline_t *tmp_line;
bint_t copy_len;
bint_t copy_index;
bint_t add_len;
bint_t nchars;
MLBUF_MAKE_GT_EQ0(start_col);
MLBUF_MAKE_GT_EQ0(end_col);
data = calloc(2, sizeof(char));
data_len = 0;
data_size = 2;
nchars = 0;
for (tmp_line = start_line; tmp_line != end_line->next; tmp_line = tmp_line->next) {
// Get copy_index + copy_len
// Also increment nchars
if (start_line == end_line) {
copy_index = _buffer_bline_col_to_index(start_line, start_col);
copy_len = _buffer_bline_col_to_index(start_line, end_col) - copy_index;
nchars += end_col - start_col;
} else if (tmp_line == start_line) {
copy_index = _buffer_bline_col_to_index(start_line, start_col);
copy_len = tmp_line->data_len - copy_index;
MLBUF_BLINE_ENSURE_CHARS(start_line);
nchars += start_line->char_count - start_col;
} else if (tmp_line == end_line) {
copy_index = 0;
copy_len = _buffer_bline_col_to_index(end_line, end_col);
nchars += end_col;
} else {
copy_index = 0;
copy_len = tmp_line->data_len;
MLBUF_BLINE_ENSURE_CHARS(tmp_line);
nchars += tmp_line->char_count;
}
// Add 1 for newline if not on end_line
add_len = copy_len + (tmp_line != end_line ? 1 : 0); // Plus 1 for newline
nchars += (tmp_line != end_line ? 1 : 0);
// Copy add_len bytes from copy_index into data
if (add_len > 0) {
if (data_len + add_len + 1 > data_size) {
data_size = data_len + add_len + 1; // Plus 1 for nullchar
data = realloc(data, data_size);
}
if (copy_len > 0) {
memcpy(data + data_len, tmp_line->data + copy_index, copy_len);
data_len += copy_len;
}
if (tmp_line != end_line) {
*(data + data_len) = '\n';
data_len += 1;
}
}
}
*(data + data_len) = '\0';
*ret_data = data;
*ret_data_len = data_len;
*ret_data_nchars = nchars;
return MLBUF_OK;
}
// Undo an action
int buffer_undo(buffer_t *self) {
return _buffer_undo(self, 0);
}
// Undo actions in last action_group
int buffer_undo_action_group(buffer_t *self) {
return _buffer_undo(self, 1);
}
// Redo an action
int buffer_redo(buffer_t *self) {
return _buffer_redo(self, 0);
}
// Redo actions in last action_group
int buffer_redo_action_group(buffer_t *self) {
return _buffer_redo(self, 1);
}
// Toggle is_style_disabled
int buffer_set_styles_enabled(buffer_t *self, int is_enabled) {
if (!self->is_style_disabled && !is_enabled) {
self->is_style_disabled = 1;
} else if (self->is_style_disabled && is_enabled) {
self->is_style_disabled = 0;
buffer_apply_styles(self, self->first_line, self->line_count);
}
return MLBUF_OK;
}
// Set buffer action_group pointer
int buffer_set_action_group_ptr(buffer_t *self, int *action_group) {
self->action_group = action_group;
return MLBUF_OK;
}
// Apply styles from start_line
int buffer_apply_styles(buffer_t *self, bline_t *start_line, bint_t line_delta) {
bint_t min_nlines;
srule_node_t *srule_node;
int count_tmp;
int srule_count;
if (self->is_style_disabled) {
return MLBUF_OK;
}
// min_nlines, minimum number of lines to style
// line_delta < 0: 2 (start_line + 1)
// line_delta == 0: 1 (start_line)
// line_delta > 0: 1 + line_delta (start_line + added lines)
min_nlines = 1 + (line_delta < 0 ? 1 : line_delta);
// Count current srules
srule_count = 0;
DL_COUNT(self->srules, srule_node, count_tmp);
srule_count += count_tmp;
// Apply rules if there are any, or if the number of rules changed
if (srule_count > 0 || self->num_applied_srules != srule_count) {
_buffer_apply_styles_all(start_line, min_nlines);
self->num_applied_srules = srule_count;