-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomp_editor.h
More file actions
1790 lines (1447 loc) · 52.4 KB
/
comp_editor.h
File metadata and controls
1790 lines (1447 loc) · 52.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
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
#ifndef COMP_EDITOR_HEADER_
#define COMP_EDITOR_HEADER_
#ifndef NAMESPACE
#define NAMESPACE
#endif
#ifndef TYPENAME
#define TYPENAME editor_t
#endif
#define CONCAT(a,b) a##b
#define EXPAND_AND_CONCAT(a,b) CONCAT(a,b)
#define NAME(name) EXPAND_AND_CONCAT(NAMESPACE, name)
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#ifndef cursor_t
#define cursor_t NAME(cursor_t)
#endif
typedef struct {
int x_start,y_start;
int x_end, y_end;
} cursor_t;
#ifndef word_coloring_t
#define word_coloring_t NAME(word_coloring_t)
#endif
typedef struct {
int start, end;
int rendering_hint;
} word_coloring_t;
#ifndef line_coloring_t
#define line_coloring_t NAME(line_coloring_t)
#endif
typedef struct {
word_coloring_t *word_colorings;
} line_coloring_t;
#ifndef line_buffer_t
#define line_buffer_t NAME(line_buffer_t)
#endif
typedef struct {
char* text;
line_coloring_t coloring;
} line_buffer_t;
#ifndef editor_timer_t
#define timer_t NAME(timer_t)
#endif
typedef struct {
double length;
double current;
bool running;
bool effect;
} editor_timer_t;
typedef enum {
ACTION_INSERT,
ACTION_MOVE,
ACTION_DELETE,
ACTION_COMPOUND,
ACTION_DELETE_LINE,
ACTION_NEWLINE,
ACTION_MOVE_LINE
} editor_action_type_t;
typedef struct {
int x_start, y_start;
int x_end, y_end;
} action_insert_t;
typedef enum {
DELETE_MULTIPLE,
DELETE_SINGLE,
} delete_type_t;
typedef struct {
delete_type_t type;
char* data;
char c;
} action_delete_t;
typedef struct {
int index;
line_buffer_t* lines;
} action_delete_line_t;
typedef struct editor_action_t editor_action_t;
typedef struct {
editor_action_type_t compound_type;
editor_action_t *actions;
} action_compound_t;
typedef struct {
int line;
bool splitline;
} action_newline_t;
typedef struct {
int direction;
int amount;
} action_move_line_t;
typedef union {
action_insert_t insert;
action_compound_t compound;
action_delete_t delete;
action_delete_line_t delete_line;
action_newline_t newline;
action_move_line_t move_line;
} action_data_t;
struct editor_action_t {
editor_action_type_t type;
action_data_t as;
cursor_t before;
cursor_t after;
};
#ifndef editor_t
#define editor_t TYPENAME
#endif
typedef struct {
line_buffer_t *lines;
cursor_t *cursors;
char** printable_lines;
void *font;
bool selecting;
bool (*word_seperator)(uint32_t);
int last_size;
int scroll_start;
line_coloring_t (*calculate_color)(char *, int, line_coloring_t);
editor_action_t *actions;
editor_timer_t action_timer;
} editor_t;
#ifndef render_type_t
#define render_type_t NAME(render_type_t)
#endif
typedef enum {
TEXT,
RECT,
} render_type_t;
#ifndef render_text_t
#define render_text_t NAME(render_text_t)
#endif
typedef struct {
int x, y;
char* text;
void* font;
int font_size;
} render_text_t;
#ifndef render_rect_t
#define render_rect_t NAME(render_rect_t)
#endif
typedef struct {
int x, y;
int width, height;
int color[4];
} render_rect_t;
#ifndef render_data_t
#define render_data_t NAME(render_data_t)
#endif
typedef union {
render_text_t text;
render_rect_t rect;
} render_data_t;
#ifndef render_hint_t
#define render_hint_t NAME(render_hint_t)
#endif
typedef enum {
HINT_CURSOR,
HINT_TEXT,
HINT_LINE_NUMBER,
HINT_SELECTION,
HINT_HIGHLIGHT_LINE,
HINT_END
} render_hint_t;
#ifndef render_command_t
#define render_command_t NAME(render_command_t)
#endif
typedef struct {
render_type_t type;
render_data_t as;
int render_hint;
} render_command_t;
#ifndef init_options_t
#define init_options_t NAME(init_options_t)
#endif
typedef struct {
char* text;
} init_options_t;
#ifndef render_options_t
#define render_options_t NAME(render_options_t)
#endif
typedef struct {
int area_x, area_y;
int area_width, area_height;
int font_size;
int line_margin;
int (*text_width)(char* text, int index, void* font, int font_size);
bool line_numbers;
int line_number_margin;
} render_options_t;
#ifndef editor_cursor_mright
#define editor_cursor_mright NAME(cursor_mright)
#endif
void editor_cursor_mright(editor_t *editor);
#ifndef editor_cursor_mleft
#define editor_cursor_mleft NAME(cursor_mleft)
#endif
void editor_cursor_mleft(editor_t *editor);
#ifndef editor_cursor_mdown
#define editor_cursor_mdown NAME(cursor_mdown)
#endif
void editor_cursor_mdown(editor_t *editor);
#ifndef editor_cursor_mup
#define editor_cursor_mup NAME(cursor_mup)
#endif
void editor_cursor_mup(editor_t *editor);
#ifndef editor_create
#define editor_create NAME(create)
#endif
editor_t editor_create(
init_options_t options,
line_coloring_t (*calculate_color)(char *, int, line_coloring_t),
bool (*word_seperator)(uint32_t c)
);
#ifndef editor_acursor_insert
#define editor_acursor_insert NAME(acursor_insert)
#endif
void editor_acursor_insert(editor_t *editor, int c);
#ifndef editor_acursor_delete
#define editor_acursor_delete NAME(acursor_delete)
#endif
void editor_acursor_delete(editor_t *editor, int direction, bool unindent);
#ifndef editor_acursor_word_delete
#define editor_acursor_word_delete NAME(acursor_word_delete)
#endif
void editor_acursor_word_delete(editor_t *editor, int direction);
#ifndef editor_acursor_newline
#define editor_acursor_newline NAME(acursor_newline)
#endif
void editor_acursor_newline(editor_t *editor, bool splitline, int direction);
#ifndef editor_start_select
#define editor_start_select NAME(start_select)
#endif
void editor_start_select(editor_t *editor);
#ifndef editor_stop_select
#define editor_stop_select NAME(stop_select)
#endif
void editor_stop_select(editor_t *editor);
#ifndef editor_start_render
#define editor_start_render NAME(start_render)
#endif
render_command_t* editor_start_render(editor_t *editor, const render_options_t* options);
#ifndef editor_acursor_insert_block
#define editor_acursor_insert_block NAME(acursor_insert_block)
#endif
void editor_acursor_insert_block(editor_t *editor, const char* text, bool ignore_newlines, bool select);
#ifndef editor_get_selection
#define editor_get_selection NAME(get_selection)
#endif
char *editor_get_selection(editor_t *editor, int cursor_index);
#ifndef editor_cursor_mendline
#define editor_cursor_mendline NAME(cursor_mendline)
#endif
void editor_cursor_mendline(editor_t *editor);
#ifndef editor_cursor_mstartline
#define editor_cursor_mstartline NAME(cursor_mstartline)
#endif
void editor_cursor_mstartline(editor_t *editor);
#ifndef editor_acursor_line_dublicate
#define editor_acursor_line_dublicate NAME(acursor_line_dublicate)
#endif
void editor_acursor_line_dublicate(editor_t *editor, int direction);
#ifndef editor_acursor_line_move
#define editor_acursor_line_move NAME(acursor_line_move)
#endif
void editor_acursor_line_move(editor_t *editor, int direction);
#ifndef editor_acursor_line_delete
#define editor_acursor_line_delete NAME(acursor_line_delete)
#endif
void editor_acursor_line_delete(editor_t *editor);
#ifndef editor_cursor_mword
#define editor_cursor_mword NAME(cursor_mword)
#endif
void editor_cursor_mword(editor_t *editor, int direction);
#ifndef editor_mouse_to_cursor
#define editor_mouse_to_cursor NAME(mouse_to_cursor)
#endif
void editor_mouse_to_cursor(editor_t *editor, render_options_t *options, float x, float y, int *out_x, int* out_y);
#ifndef editor_rollback
#define editor_rollback NAME(rollback)
#endif
void editor_rollback(editor_t *editor);
#ifndef editor_update_timers
#define editor_update_timers NAME(update_timers)
#endif
void editor_update_timers(editor_t *editor, double delta);
#ifndef editor_select_all
#define editor_select_all NAME(select_all)
#endif
void editor_select_all(editor_t *editor);
#ifndef editor_acursor_indent
#define editor_acursor_indent NAME(acursor_indent)
#endif
void editor_acursor_indent(editor_t *editor);
#ifndef editor_acursor_unindent
#define editor_acursor_unindent NAME(acursor_unindent)
#endif
void editor_acursor_unindent(editor_t *editor);
#ifdef EDITOR_IMPLEMENTATION
#ifndef VECTOR_PATH
#error "Please define VECTOR_PATH"
#endif
#include VECTOR_PATH
#include <string.h>
// --Utility
// --Misc
int edutil_min(int a, int b){
return a < b ? a : b;
}
char** edutil_split_lines(const char* text) {
if(text == NULL) return NULL;
size_t len = strlen(text);
char **lines = NULL;
char *cur_line = NULL;
for(int i = 0;i < len;i++) {
if(text[i] == '\n') {
vec_add(lines, cur_line);
cur_line = NULL;
continue;
}
vec_add(cur_line, text[i]);
}
vec_add(lines, cur_line);
return lines;
}
line_buffer_t *edutil_line_get(editor_t *editor, int index) {
return &editor->lines[index];
}
int edutil_find_first_char(line_buffer_t *line) {
int first_char = -1;
for(int i = 0;i < vec_length(line->text);i++){
if(line->text[i] != ' ') {
first_char = i;
break;
}
}
return first_char;
}
int coloring_sort(const word_coloring_t *a, const word_coloring_t *b) {
if(a->start == b->start) {
return 0;
}
return a->start < b->start ? -1 : 1;
}
void edutil_update_coloring(editor_t* editor, line_buffer_t *line) {
line_coloring_t color = editor->calculate_color(line->text, vec_length(line->text), (line_coloring_t){0});
qsort(color.word_colorings, vec_length(color.word_colorings), sizeof(*color.word_colorings), (int (*)(const void *, const void *))coloring_sort);
line->coloring = color;
}
void edutil_check_view(editor_t *editor, cursor_t *cursor) {
if(cursor->y_start < editor->scroll_start) {
editor->scroll_start = cursor->y_start;
}
if(cursor->y_start >= editor->scroll_start + editor->last_size) {
editor->scroll_start = cursor->y_start - editor->last_size+1;
}
}
char *edutil_copy_text(char* text, int start, int end) {
char* new_text = NULL;
for(int i = start;i < edutil_min(end, vec_length(text));i++){
vec_add(new_text, text[i]);
}
return new_text;
}
int edutil_line_number_width(editor_t *editor, const render_options_t *options) {
int line_number_width = 0;
if(options->line_numbers) {
int biggest_number = vec_length(editor->lines);
char line_number[21];
snprintf(line_number, 21, "%d", biggest_number);
line_number_width = options->text_width(line_number, strlen(line_number), editor->font, options->font_size);
line_number_width += options->line_number_margin;
}
return line_number_width;
}
int edutil_line_index_from_width(editor_t *editor, render_options_t *options, line_buffer_t *line, int width) {
if(options->text_width(line->text, vec_length(line->text), editor->font, options->font_size) < width) {
return vec_length(line->text);
}
int index = 0;
while(index < vec_length(line->text)
&& options->text_width(line->text, index, editor->font, options->font_size) < width){
index++;
}
return index-1;
}
// --Timer
void edutil_timer_start(editor_timer_t *timer, double length) {
timer->running = true;
timer->effect = false;
timer->length = length;
timer->current = 0;
}
void edutil_timer_stop(editor_timer_t *timer) {
timer->running = false;
timer->effect = false;
timer->current = 0;
}
void edutil_timer_advance(editor_timer_t *timer, double delta) {
if(!timer->running) return;
timer->current += delta;
if(timer->current > timer->length) {
timer->running = false;
timer->effect = true;
}
}
bool edutil_timer_poll(editor_timer_t *timer) {
if(timer->effect) {
timer->effect = false;
return true;
}
return false;
}
// --Action
void edutil_add_action_compound(editor_t *editor, editor_action_t *actions) {
editor_action_t compound = {
.type = ACTION_COMPOUND,
.as.compound = {
.compound_type = actions[0].type,
.actions = actions
}
};
vec_add(editor->actions, compound);
}
void edutil_add_action_impl(editor_t *editor, editor_action_t action) {
editor_action_t *last_action = &vec_peek(editor->actions);
printf("Adding action: running: %d \n", editor->action_timer.running);
if(editor->action_timer.running
&& last_action->type == ACTION_COMPOUND
&& last_action->as.compound.compound_type == action.type) {
vec_add(last_action->as.compound.actions, action);
return;
}
if(editor->action_timer.running
&& last_action->type == action.type) {
editor_action_t *actions = NULL;
vec_add(actions, *last_action);
vec_pop(editor->actions);
vec_add(actions, action);
edutil_add_action_compound(editor, actions);
return;
}
vec_add(editor->actions, action);
}
void edutil_add_action(editor_t *editor, editor_action_t action) {
if(vec_length(editor->actions) > 0) {
edutil_add_action_impl(editor, action);
} else {
vec_add(editor->actions, action);
}
edutil_timer_start(&editor->action_timer, 0.5);
}
void edutil_add_action_insert(editor_t *editor, cursor_t before, cursor_t after) {
editor_action_t action = {
.type = ACTION_INSERT,
.as.insert = {.x_start = before.x_start, .y_start = before.y_start, .y_end = after.y_start, .x_end = after.x_start},
.before = before,
.after = after
};
edutil_add_action(editor, action);
}
void edutil_add_action_delete_single(editor_t *editor, cursor_t before, cursor_t after, uint32_t c) {
editor_action_t delete = {.type = ACTION_DELETE, .before = before, .after = after, .as.delete = {.type = DELETE_SINGLE, .c = c}};
edutil_add_action(editor, delete);
}
void edutil_add_action_delete_multiple(editor_t *editor, cursor_t before, cursor_t after, char *cs) {
editor_action_t delete = {.type = ACTION_DELETE, .before = before, .after = after, .as.delete = {.type = DELETE_MULTIPLE, .data = cs}};
edutil_add_action(editor, delete);
}
void edutil_add_action_delete_lines(editor_t *editor, cursor_t before, cursor_t after, int index, line_buffer_t *lines) {
editor_action_t action = {
.type = ACTION_DELETE_LINE,
.before = before,
.after = after,
.as.delete_line = {
.index = index,
.lines = lines
}
};
edutil_add_action(editor, action);
}
void edutil_add_action_newline(editor_t *editor, cursor_t before, cursor_t after, int line, bool splitline) {
editor_action_t action = {
.type = ACTION_NEWLINE,
.before = before,
.after = after,
.as.newline = {
.line = line,
.splitline = splitline
}
};
edutil_add_action(editor, action);
}
void edutil_add_action_move_line(editor_t *editor, cursor_t before, cursor_t after, int direction, int amount) {
editor_action_t action = {
.type = ACTION_MOVE_LINE,
.before = before,
.after = after,
.as.move_line = {
.direction = direction,
.amount = amount
}
};
edutil_add_action(editor, action);
}
// --Cursor
void edutil_pmove(editor_t * editor, cursor_t *cursor, bool selecting) {
if(!selecting) {
cursor->x_end = cursor->x_start;
cursor->y_end = cursor->y_start;
}
edutil_check_view(editor, cursor);
}
bool edutil_cursor_aedge(editor_t *editor, cursor_t* cursor, int direction) {
return (direction == -1 && cursor->x_start == 0 && cursor->y_start == 0)
|| (direction == 0 && cursor->y_start >= vec_length(editor->lines)-1 && cursor->x_start == vec_length(editor->lines[cursor->y_start].text));
}
void edutil_cursor_mright(editor_t *editor, cursor_t *cursor, bool selecting) {
if(edutil_cursor_aedge(editor, cursor, 0)) return;
if(cursor->x_start >= vec_length(editor->lines[cursor->y_start].text)) {
cursor->x_start = 0;
cursor->y_start++;
} else {
cursor->x_start++;
}
edutil_pmove(editor, cursor, selecting);
}
void edutil_cursor_mleft(editor_t *editor, cursor_t *cursor, bool selecting) {
if(edutil_cursor_aedge(editor, cursor, -1)) return;
if(cursor->x_start <= 0) {
cursor->y_start--;
cursor->x_start = vec_length(editor->lines[cursor->y_start].text);
} else {
cursor->x_start--;
}
edutil_pmove(editor, cursor, selecting);
}
void edutil_cursor_mdown(editor_t *editor, cursor_t *cursor, bool selecting) {
if(cursor->y_start >= vec_length(editor->lines)-1) return;
cursor->y_start++;
if(cursor->x_start >= vec_length(editor->lines[cursor->y_start].text)) {
cursor->x_start = vec_length(editor->lines[cursor->y_start].text);
}
edutil_pmove(editor, cursor, selecting);
}
void edutil_cursor_mup(editor_t *editor, cursor_t *cursor, bool selecting) {
if(cursor->y_start <= 0) return;
cursor->y_start--;
if(cursor->x_start >= vec_length(editor->lines[cursor->y_start].text)) {
cursor->x_start = vec_length(editor->lines[cursor->y_start].text);
}
edutil_pmove(editor, cursor, selecting);
}
bool edutil_cursor_hselection(cursor_t *cursor) {
return cursor->x_start != cursor->x_end || cursor->y_start != cursor->y_end;
}
void edutil_cursor_set(cursor_t *cursor, int x_start, int y_start) {
cursor->x_start = x_start;
cursor->x_end = x_start;
cursor->y_start = y_start;
cursor->y_end = y_start;
}
void edutil_cursor_sorted(cursor_t *cursor, int *out_x_start, int *out_y_start, int *out_x_end, int *out_y_end) {
int x_start = cursor->x_start;
int x_end = cursor->x_end;
int y_start = cursor->y_start;
int y_end = cursor->y_end;
if(y_start == y_end && x_start > x_end) {
x_start = cursor->x_end;
x_end = cursor->x_start;
} else if(y_start > y_end) {
x_start = cursor->x_end;
y_start = cursor->y_end;
x_end = cursor->x_start;
y_end = cursor->y_start;
}
*out_x_start = x_start;
*out_y_start = y_start;
*out_x_end = x_end;
*out_y_end = y_end;
}
bool edutil_cursor_in_intendation(cursor_t *cursor, line_buffer_t *line) {
int first_char = edutil_find_first_char(line);
return cursor->x_start != 0 && (first_char == -1 || first_char >= cursor->x_start);
}
// --Lines
void edutil_insert_line(editor_t *editor, line_buffer_t line, int index) {
edutil_update_coloring(editor, &line);
vec_insert(editor->lines, index, line);
}
void edutil_insert_in_line(line_buffer_t *line, int index, int c) {
vec_insert(line->text, index, (char)c);
}
char edutil_remove_in_line(line_buffer_t *line, int index) {
char c = line->text[index];
vec_remove(line->text, index);
return c;
}
char* edutil_remove_in_lineEx(editor_t *editor, line_buffer_t *line, int start, int end) {
char *cs = NULL;
for(int i = start;i < end;i++) {
int c = edutil_remove_in_line(line, start);
vec_add(cs, c);
}
edutil_update_coloring(editor, line);
return cs;
}
void edutil_insert_line_in_line(line_buffer_t *line1, line_buffer_t *line2, int index) {
for(int i = 0;i < vec_length(line2->text);i++) {
vec_insert(line1->text, index+i, line2->text[i]);
}
}
void edutil_concat_lines(editor_t *editor, line_buffer_t *line1, line_buffer_t *line2) {
vec_for_each_cpy(char c, line2->text) {
vec_add(line1->text, c);
}
edutil_update_coloring(editor, line1);
}
line_buffer_t edutil_split_line_at(editor_t *editor, line_buffer_t *line1, int index) {
char *text = NULL;
size_t length = vec_length(line1->text);
for(int j = index;j < length;j++) {
vec_add(text, line1->text[index]);
vec_remove(line1->text, index);
}
line_buffer_t newline = { text };
edutil_update_coloring(editor, line1);
edutil_update_coloring(editor, &newline);
return newline;
}
char* edutil_get_block(editor_t *editor, int x_from, int y_from, int x_to, int y_to) {
char *text = NULL;
if(y_from == y_to) {
for(int i = x_from;i < x_to;i++) {
vec_add(text, editor->lines[y_from].text[i]);
}
} else {
for(int i = x_from;i < vec_length(editor->lines[y_from].text);i++) {
vec_add(text, editor->lines[y_from].text[i]);
}
vec_add(text,'\n');
for(int i = y_from+1;i < y_to;i++) {
for(int j = 0;j < vec_length(editor->lines[i].text);j++) {
vec_add(text, editor->lines[i].text[j]);
}
vec_add(text, '\n');
}
for(int i = 0;i < x_to;i++) {
vec_add(text, editor->lines[y_to].text[i]);
}
}
vec_add(text, '\0');
return text;
}
char* edutil_remove_block(editor_t *editor, int x_from, int y_from, int x_to, int y_to) {
char *cs = NULL;
if(y_from == y_to) {
line_buffer_t *line = edutil_line_get(editor, y_from);
cs = edutil_remove_in_lineEx(editor, line, x_from, x_to);
vec_add(cs, '\0');
} else {
cs = edutil_get_block(editor, x_from, y_from, x_to, y_to);
line_buffer_t *start = edutil_line_get(editor, y_from);
line_buffer_t other = edutil_split_line_at(editor, start, x_from);
// TODO(Adrian): Line free
vec_free(other.text);
line_buffer_t *end = edutil_line_get(editor, y_to);
other = edutil_split_line_at(editor, end, x_to);
edutil_concat_lines(editor, start, &other);
for(int i = y_from+1;i <= y_to;i++){
vec_remove(editor->lines, y_from+1);
}
}
return cs;
}
void edutil_remove_selection(editor_t *editor, cursor_t *cursor) {
int x_start, y_start, x_end, y_end;
edutil_cursor_sorted(cursor, &x_start, &y_start, &x_end, &y_end);
cursor_t before = *cursor;
char *cs = edutil_remove_block(editor, x_start, y_start, x_end, y_end);
edutil_cursor_set(cursor, x_start, y_start);
cursor_t after = *cursor;
edutil_add_action_delete_multiple(editor, before, after, cs);
}
void edutil_insert_block(editor_t *editor, cursor_t *cursor, const char* text, int x_from, int y_from, int x_to, int y_to, bool ignore_newlines, bool select) {
if(ignore_newlines) {
line_buffer_t *line_buffer = &editor->lines[y_from];
char* text_copy = NULL;
for(int i = 0;i < strlen(text);i++){
vec_add(text_copy, text[i]);
}
line_buffer_t new_line = (line_buffer_t){text_copy};
edutil_insert_line_in_line(line_buffer, &new_line, x_from);
edutil_update_coloring(editor, line_buffer);
if(select) {
cursor->x_start += vec_length(text_copy);
}
return;
}
char **lines = edutil_split_lines(text);
line_buffer_t rest = edutil_split_line_at(editor, &editor->lines[y_from], x_from);
line_buffer_t first = {lines[0]};
edutil_concat_lines(editor, &editor->lines[y_from], &first);
if(vec_length(lines) == 1) {
edutil_concat_lines(editor, &editor->lines[y_from], &rest);
x_from += vec_length(first.text);
} else {
for(int i = 1;i < vec_length(lines)-1;i++) {
line_buffer_t line = {lines[i]};
edutil_update_coloring(editor, &line);
y_from++;
vec_insert(editor->lines, y_from, line);
}
line_buffer_t line = {lines[vec_length(lines)-1]};
x_from = vec_length(line.text);
edutil_concat_lines(editor, &line, &rest);
y_from++;
vec_insert(editor->lines, y_from, line);
}
if(select) {
cursor->x_start = x_from;
cursor->y_start = y_from;
}
}
void edutil_move_block(editor_t *editor, int y_from, int y_to, int direction, int amount) {
int abs_amount = amount < 0 ? -amount : amount;
int move_start = direction < 0 ? 0: (y_to - y_from);
for(int i = 0;i < abs_amount;i++){
line_buffer_t line = editor->lines[direction < 0 ? y_from-1 : y_to+1];
for(int j = 0;j <= (y_to - y_from);j++){
editor->lines[y_from + abs(move_start - j) + direction] = editor->lines[y_from + abs(move_start - j)];
}
editor->lines[direction < 0 ? y_to : y_from] = line;
y_from += direction;
y_to += direction;
}
}
// ++Editor functions
// ++Cursor
void editor_cursor_mright(editor_t *editor) {
vec_for_each_ptr(cursor_t *cursor, editor->cursors) {
edutil_cursor_mright(editor, cursor, editor->selecting);
}
}
void editor_cursor_mleft(editor_t *editor) {
vec_for_each_ptr(cursor_t *cursor, editor->cursors) {
edutil_cursor_mleft(editor, cursor, editor->selecting);
}
}
void editor_cursor_mdown(editor_t *editor) {
vec_for_each_ptr(cursor_t *cursor, editor->cursors) {
edutil_cursor_mdown(editor, cursor, editor->selecting);
}
}
void editor_cursor_mup(editor_t *editor) {
vec_for_each_ptr(cursor_t *cursor, editor->cursors) {
edutil_cursor_mup(editor, cursor, editor->selecting);
}
}
void editor_cursor_mto(editor_t* editor, int x, int y) {
cursor_t *cursor = &editor->cursors[0];
cursor->x_start = x;
cursor->y_start = y;
if(!editor->selecting) {
cursor->x_end = cursor->x_start;
cursor->y_end = cursor->y_start;
}
}
void editor_cursor_mendline(editor_t *editor) {
for(int i = 0;i < vec_length(editor->cursors);i++) {
cursor_t *cursor = &editor->cursors[i];
line_buffer_t *line = &editor->lines[cursor->y_start];
cursor->x_start = vec_length(line->text);
if(!editor->selecting) {
cursor->x_end = cursor->x_start;
cursor->y_end = cursor->y_start;
}
}
}
void editor_cursor_mstartline(editor_t *editor) {
for(int i = 0;i < vec_length(editor->cursors);i++) {
cursor_t *cursor = &editor->cursors[i];
cursor->x_start = 0;
if(!editor->selecting) {
cursor->x_end = cursor->x_start;
cursor->y_end = cursor->y_start;
}
}
}
// TODO(Adrian): When multiple white space only jump whitespace?
#define dir_cond(DIR) ((cursor->x_start == vec_length(editor->lines[cursor->y_start].text) && DIR == 0) || (cursor->x_start == 0 && DIR == -1))
#define sep_cond(DIR) (editor->word_seperator(dir_cond(DIR) ? '\n' : editor->lines[cursor->y_start].text[cursor->x_start + DIR]))
void editor_cursor_mword(editor_t *editor, int direction) {
cursor_t *cursor = &editor->cursors[0];
bool whitespace_before = sep_cond(direction);
if(edutil_cursor_aedge(editor, cursor, direction)) return;
if(whitespace_before) {
while(!edutil_cursor_aedge(editor, cursor, direction) && sep_cond(direction)) {
if(direction == -1) {
edutil_cursor_mleft(editor,cursor, editor->selecting);
} else {
edutil_cursor_mright(editor,cursor, editor->selecting);
}
}
}
while(!edutil_cursor_aedge(editor, cursor, direction) && !sep_cond(direction)) {
if(direction == -1) {
edutil_cursor_mleft(editor,cursor, editor->selecting);
} else {
edutil_cursor_mright(editor,cursor, editor->selecting);
}
}
}
void editor_select_all(editor_t *editor) {
cursor_t *cursor = &editor->cursors[0];
int last_line = vec_length(editor->lines)-1;
int last_line_end = vec_length(editor->lines[last_line].text);