forked from B-Y-P/4coder-community
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4ed_api_implementation.cpp
3211 lines (2944 loc) · 108 KB
/
4ed_api_implementation.cpp
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
/*
* Mr. 4th Dimention - Allen Webster
*
* ??.??.????
*
* Implementation of the API functions.
*
*/
// TOP
function void
output_file_append(Thread_Context *tctx, Models *models, Editing_File *file, String_Const_u8 value){
i64 end = buffer_size(&file->state.buffer);
Edit_Behaviors behaviors = {};
behaviors.pos_before_edit = end;
edit_single(tctx, models, file, Ii64(end), value, behaviors);
}
function void
file_cursor_to_end(Thread_Context *tctx, Models *models, Editing_File *file){
Assert(file != 0);
i64 pos = buffer_size(&file->state.buffer);
Layout *layout = &models->layout;
for (Panel *panel = layout_get_first_open_panel(layout);
panel != 0;
panel = layout_get_next_open_panel(layout, panel)){
View *view = panel->view;
if (view->file != file){
continue;
}
view_set_cursor(tctx, models, view, pos);
view->mark = pos;
}
}
////////////////////////////////
function b32
access_test(Access_Flag object_flags, Access_Flag access_flags){
return((object_flags & access_flags) == access_flags);
}
function b32
api_check_panel(Panel *panel){
b32 result = false;
if (panel != 0 && panel->kind != PanelKind_Unused){
result = true;
}
return(result);
}
function b32
api_check_buffer(Editing_File *file){
return(file != 0);
}
function b32
api_check_buffer(Editing_File *file, Access_Flag access){
return(api_check_buffer(file) && access_test(file_get_access_flags(file), access));
}
function b32
api_check_view(View *view){
return(view != 0 && view->in_use);
}
function b32
api_check_view(View *view, Access_Flag access){
return(api_check_view(view) && access_test(view_get_access_flags(view), access));
}
function b32
is_running_coroutine(Application_Links *app){
Thread_Context *tctx = app->tctx;
Thread_Context_Extra_Info *info = (Thread_Context_Extra_Info*)tctx->user_data;
return(info->coroutine != 0);
}
api(custom) function b32
global_set_setting(Application_Links *app, Global_Setting_ID setting, i64 value){
Models *models = (Models*)app->cmd_context;
b32 result = true;
switch (setting){
case GlobalSetting_LAltLCtrlIsAltGr:
{
models->settings.lctrl_lalt_is_altgr = (b32)(value != 0);
}break;
default:
{
result = false;
}break;
}
return(result);
}
api(custom) function Rect_f32
global_get_screen_rectangle(Application_Links *app){
Models *models = (Models*)app->cmd_context;
return(Rf32(V2f32(0, 0), V2f32(layout_get_root_size(&models->layout))));
}
api(custom) function Thread_Context*
get_thread_context(Application_Links *app){
return(app->tctx);
}
api(custom) function Child_Process_ID
create_child_process(Application_Links *app, String_Const_u8 path, String_Const_u8 command){
Models *models = (Models*)app->cmd_context;
Child_Process_ID result = 0;
if (!child_process_call(app->tctx, models, path, command, &result)){
result = 0;
}
return(result);
}
api(custom) function b32
child_process_set_target_buffer(Application_Links *app, Child_Process_ID child_process_id, Buffer_ID buffer_id, Child_Process_Set_Target_Flags flags){
Models *models = (Models*)app->cmd_context;
Child_Process *child_process = child_process_from_id(&models->child_processes, child_process_id);
Editing_File *file = imp_get_file(models, buffer_id);
b32 result = false;
if (api_check_buffer(file) && child_process != 0){
result = child_process_set_target_buffer(models, child_process, file, flags);
}
return(result);
}
api(custom) function Child_Process_ID
buffer_get_attached_child_process(Application_Links *app, Buffer_ID buffer_id){
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
Child_Process_ID result = 0;
if (api_check_buffer(file)){
result = file->state.attached_child_process;
}
return(result);
}
api(custom) function Buffer_ID
child_process_get_attached_buffer(Application_Links *app, Child_Process_ID child_process_id){
Models *models = (Models*)app->cmd_context;
Child_Process *child_process = child_process_from_id(&models->child_processes, child_process_id);
Buffer_ID result = 0;
if (child_process != 0 && child_process->out_file != 0){
result = child_process->out_file->id;
}
return(result);
}
api(custom) function Process_State
child_process_get_state(Application_Links *app, Child_Process_ID child_process_id){
Models *models = (Models*)app->cmd_context;
return(child_process_get_state(&models->child_processes, child_process_id));
}
api(custom) function b32
enqueue_virtual_event(Application_Links *app, Input_Event *event){
Models *models = (Models*)app->cmd_context;
b32 result = false;
if (InputEventKind_None < event->kind && event->kind < InputEventKind_COUNT){
models_push_virtual_event(models, event);
}
return(result);
}
api(custom) function i32
get_buffer_count(Application_Links *app)
{
Models *models = (Models*)app->cmd_context;
Working_Set *working_set = &models->working_set;
return(working_set->active_file_count);
}
api(custom) function Buffer_ID
get_buffer_next(Application_Links *app, Buffer_ID buffer_id, Access_Flag access)
{
Models *models = (Models*)app->cmd_context;
Working_Set *working_set = &models->working_set;
Editing_File *file = working_set_get_file(working_set, buffer_id);
file = file_get_next(working_set, file);
for (;file != 0 && !access_test(file_get_access_flags(file), access);){
file = file_get_next(working_set, file);
}
Buffer_ID result = 0;
if (file != 0){
result = file->id;
}
return(result);
}
api(custom) function Buffer_ID
get_buffer_by_name(Application_Links *app, String_Const_u8 name, Access_Flag access)
{
Models *models = (Models*)app->cmd_context;
Working_Set *working_set = &models->working_set;
Editing_File *file = working_set_contains_name(working_set, name);
Buffer_ID result = 0;
if (api_check_buffer(file, access)){
result = file->id;
}
return(result);
}
api(custom) function Buffer_ID
get_buffer_by_file_name(Application_Links *app, String_Const_u8 file_name, Access_Flag access)
{
Models *models = (Models*)app->cmd_context;
Editing_File_Name canon = {};
Buffer_ID result = false;
Scratch_Block scratch(app);
if (get_canon_name(scratch, file_name, &canon)){
Working_Set *working_set = &models->working_set;
Editing_File *file = working_set_contains_canon(working_set, string_from_file_name(&canon));
if (api_check_buffer(file, access)){
result = file->id;
}
}
return(result);
}
api(custom) function b32
buffer_read_range(Application_Links *app, Buffer_ID buffer_id, Range_i64 range, u8 *out)
{
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
b32 result = false;
if (api_check_buffer(file)){
i64 size = buffer_size(&file->state.buffer);
if (0 <= range.min && range.min <= range.max && range.max <= size){
Scratch_Block scratch(app);
String_Const_u8 string = buffer_stringify(scratch, &file->state.buffer, range);
block_copy(out, string.str, string.size);
result = true;
}
}
return(result);
}
function Edit_Behaviors
get_active_edit_behaviors(Models *models, Editing_File *file){
Panel *panel = layout_get_active_panel(&models->layout);
Assert(panel != 0);
View *view = panel->view;
Assert(view != 0);
Edit_Behaviors behaviors = {};
if (view->file == file){
behaviors.pos_before_edit = view->edit_pos_.cursor_pos;
}
else{
behaviors.pos_before_edit = -1;
}
return(behaviors);
}
api(custom) function b32
buffer_replace_range(Application_Links *app, Buffer_ID buffer_id, Range_i64 range, String_Const_u8 string)
{
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
b32 result = false;
if (api_check_buffer(file)){
i64 size = buffer_size(&file->state.buffer);
if (0 <= range.first && range.first <= range.one_past_last && range.one_past_last <= size){
Edit_Behaviors behaviors = get_active_edit_behaviors(models, file);
edit_single(app->tctx, models, file, range, string, behaviors);
result = true;
}
}
return(result);
}
api(custom) function b32
buffer_batch_edit(Application_Links *app, Buffer_ID buffer_id, Batch_Edit *batch)
{
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
b32 result = false;
if (api_check_buffer(file)){
Edit_Behaviors behaviors = get_active_edit_behaviors(models, file);
result = edit_batch(app->tctx, models, file, batch, behaviors);
}
return(result);
}
api(custom) function String_Match
buffer_seek_string(Application_Links *app, Buffer_ID buffer, String_Const_u8 needle, Scan_Direction direction, i64 start_pos){
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer);
String_Match result = {};
if (api_check_buffer(file)){
if (needle.size == 0){
result.flags = StringMatch_CaseSensitive;
result.range = Ii64(start_pos);
}
else{
Scratch_Block scratch(app);
Gap_Buffer *gap_buffer = &file->state.buffer;
i64 size = buffer_size(gap_buffer);
List_String_Const_u8 chunks = buffer_get_chunks(scratch, gap_buffer);
Range_i64 range = {};
if (direction == Scan_Forward){
i64 adjusted_pos = start_pos + 1;
start_pos = clamp_top(adjusted_pos, size);
range = Ii64(adjusted_pos, size);
}
else{
i64 adjusted_pos = start_pos - 1 + needle.size;
start_pos = clamp_bot(0, adjusted_pos);
range = Ii64(0, adjusted_pos);
}
buffer_chunks_clamp(&chunks, range);
if (chunks.first != 0){
u64_Array jump_table = string_compute_needle_jump_table(scratch, needle, direction);
Character_Predicate dummy = {};
String_Match_List list = find_all_matches(scratch, 1,
chunks, needle, jump_table, &dummy, direction,
range.min, buffer, 0);
if (list.count == 1){
result = *list.first;
}
}
else{
result.range = Ii64(start_pos);
}
}
}
return(result);
}
api(custom) function String_Match
buffer_seek_character_class(Application_Links *app, Buffer_ID buffer, Character_Predicate *predicate, Scan_Direction direction, i64 start_pos){
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer);
String_Match result = {};
if (api_check_buffer(file)){
Scratch_Block scratch(app);
Gap_Buffer *gap_buffer = &file->state.buffer;
List_String_Const_u8 chunks_list = buffer_get_chunks(scratch, gap_buffer);
if (chunks_list.node_count > 0){
// TODO(allen): If you are reading this comment, then I haven't revisited this to tighten it up yet.
// buffer_seek_character_class was originally implemented using the chunk indexer helper
// Buffer_Chunk_Position, and it was written when buffer chunks were in an array instead
// of the new method of listing strings in a linked list.
// This should probably be implemented as a direct iteration-in-an-iteration that avoids the
// extra function calls and branches to achieve the iteration. However, this is a very easy API to
// get wrong. There are _a lot_ of opportunities for off by one errors and necessary code duplication,
// really tedious stuff. Anyway, this is all just to say, cleaning this up would be really nice, but
// there are almost certainly lower hanging fruit with higher payoffs elsewhere... unless need to change
// this anyway or whatever.
String_Const_u8 chunk_mem[3] = {};
String_Const_u8_Array chunks = {chunk_mem};
for (Node_String_Const_u8 *node = chunks_list.first;
node != 0;
node = node->next){
chunks.vals[chunks.count] = node->string;
chunks.count += 1;
}
i64 size = buffer_size(gap_buffer);
start_pos = clamp(-1, start_pos, size);
Buffer_Chunk_Position pos = buffer_get_chunk_position(chunks, size, start_pos);
for (;;){
i32 past_end = buffer_chunk_position_iterate(chunks, &pos, direction);
if (past_end == -1){
break;
}
else if (past_end == 1){
result.range = Ii64(size);
break;
}
u8 v = chunks.vals[pos.chunk_index].str[pos.chunk_pos];
if (character_predicate_check_character(*predicate, v)){
result.buffer = buffer;
result.range = Ii64(pos.real_pos, pos.real_pos + 1);
break;
}
}
}
}
return(result);
}
api(custom) function f32
buffer_line_y_difference(Application_Links *app, Buffer_ID buffer_id,
f32 width, Face_ID face_id,
i64 line_a, i64 line_b){
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
f32 result = 0.0f;
if (api_check_buffer(file)){
Face *face = font_set_face_from_id(&models->font_set, face_id);
if (face != 0){
Layout_Function *layout_func = file_get_layout_func(file);
result = file_line_y_difference(app->tctx, models, file,
layout_func, width, face,
line_a, line_b);
}
}
return(result);
}
api(custom) function Line_Shift_Vertical
buffer_line_shift_y(Application_Links *app, Buffer_ID buffer_id,
f32 width, Face_ID face_id,
i64 line, f32 y_shift){
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
Line_Shift_Vertical result = {};
if (api_check_buffer(file)){
Face *face = font_set_face_from_id(&models->font_set, face_id);
if (face != 0){
Layout_Function *layout_func = file_get_layout_func(file);
result = file_line_shift_y(app->tctx, models, file,
layout_func, width, face,
line, y_shift);
}
}
return(result);
}
api(custom) function i64
buffer_pos_at_relative_xy(Application_Links *app, Buffer_ID buffer_id,
f32 width, Face_ID face_id,
i64 base_line, Vec2_f32 relative_xy){
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
i64 result = -1;
if (api_check_buffer(file)){
Face *face = font_set_face_from_id(&models->font_set, face_id);
if (face != 0){
Layout_Function *layout_func = file_get_layout_func(file);
result = file_pos_at_relative_xy(app->tctx, models, file,
layout_func, width, face,
base_line, relative_xy);
}
}
return(result);
}
api(custom) function Rect_f32
buffer_relative_box_of_pos(Application_Links *app, Buffer_ID buffer_id, f32 width, Face_ID face_id, i64 base_line, i64 pos){
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
Rect_f32 result = {};
if (api_check_buffer(file)){
Face *face = font_set_face_from_id(&models->font_set, face_id);
if (face != 0){
Layout_Function *layout_func = file_get_layout_func(file);
result = file_relative_box_of_pos(app->tctx, models, file,
layout_func, width, face,
base_line, pos);
}
}
return(result);
}
api(custom) function Rect_f32
buffer_padded_box_of_pos(Application_Links *app, Buffer_ID buffer_id, f32 width, Face_ID face_id, i64 base_line, i64 pos){
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
Rect_f32 result = {};
if (api_check_buffer(file)){
Face *face = font_set_face_from_id(&models->font_set, face_id);
if (face != 0){
Layout_Function *layout_func = file_get_layout_func(file);
result = file_padded_box_of_pos(app->tctx, models, file,
layout_func, width, face,
base_line, pos);
}
}
return(result);
}
api(custom) function i64
buffer_relative_character_from_pos(Application_Links *app, Buffer_ID buffer_id,
f32 width, Face_ID face_id, i64 base_line, i64 pos)
{
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
i64 result = 0;
if (api_check_buffer(file)){
Face *face = font_set_face_from_id(&models->font_set, face_id);
if (face != 0){
Layout_Function *layout_func = file_get_layout_func(file);
result = file_relative_character_from_pos(app->tctx, models, file,
layout_func, width, face,
base_line, pos);
}
}
return(result);
}
api(custom) function i64
buffer_pos_from_relative_character(Application_Links *app, Buffer_ID buffer_id, f32 width, Face_ID face_id, i64 base_line, i64 relative_character)
{
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
i64 result = -1;
if (api_check_buffer(file)){
Face *face = font_set_face_from_id(&models->font_set, face_id);
if (face != 0){
Layout_Function *layout_func = file_get_layout_func(file);
result = file_pos_from_relative_character(app->tctx, models, file,
layout_func, width, face,
base_line, relative_character);
}
}
return(result);
}
api(custom) function f32
view_line_y_difference(Application_Links *app, View_ID view_id, i64 line_a, i64 line_b){
Models *models = (Models*)app->cmd_context;
View *view = imp_get_view(models, view_id);
f32 result = 0.0f;
if (api_check_view(view)){
result = view_line_y_difference(app->tctx, models, view, line_a, line_b);
}
return(result);
}
api(custom) function Line_Shift_Vertical
view_line_shift_y(Application_Links *app, View_ID view_id, i64 line, f32 y_shift){
Models *models = (Models*)app->cmd_context;
View *view = imp_get_view(models, view_id);
Line_Shift_Vertical result = {};
if (api_check_view(view)){
result = view_line_shift_y(app->tctx, models, view, line, y_shift);
}
return(result);
}
api(custom) function i64
view_pos_at_relative_xy(Application_Links *app, View_ID view_id, i64 base_line, Vec2_f32 relative_xy){
Models *models = (Models*)app->cmd_context;
View *view = imp_get_view(models, view_id);
i64 result = -1;
if (api_check_view(view)){
result = view_pos_at_relative_xy(app->tctx, models, view, base_line, relative_xy);
}
return(result);
}
api(custom) function Rect_f32
view_relative_box_of_pos(Application_Links *app, View_ID view_id, i64 base_line, i64 pos){
Models *models = (Models*)app->cmd_context;
View *view = imp_get_view(models, view_id);
Rect_f32 result = {};
if (api_check_view(view)){
result = view_relative_box_of_pos(app->tctx, models, view, base_line, pos);
}
return(result);
}
api(custom) function Rect_f32
view_padded_box_of_pos(Application_Links *app, View_ID view_id, i64 base_line, i64 pos){
Models *models = (Models*)app->cmd_context;
View *view = imp_get_view(models, view_id);
Rect_f32 result = {};
if (api_check_view(view)){
result = view_padded_box_of_pos(app->tctx, models, view, base_line, pos);
}
return(result);
}
api(custom) function i64
view_relative_character_from_pos(Application_Links *app, View_ID view_id, i64 base_line, i64 pos){
Models *models = (Models*)app->cmd_context;
View *view = imp_get_view(models, view_id);
i64 result = 0;
if (api_check_view(view)){
result = view_relative_character_from_pos(app->tctx, models, view, base_line, pos);
}
return(result);
}
api(custom) function i64
view_pos_from_relative_character(Application_Links *app, View_ID view_id, i64 base_line, i64 character){
Models *models = (Models*)app->cmd_context;
View *view = imp_get_view(models, view_id);
i64 result = 0;
if (api_check_view(view)){
result = view_pos_from_relative_character(app->tctx, models, view, base_line, character);
}
return(result);
}
api(custom) function b32
buffer_exists(Application_Links *app, Buffer_ID buffer_id){
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
return(api_check_buffer(file));
}
api(custom) function Access_Flag
buffer_get_access_flags(Application_Links *app, Buffer_ID buffer_id){
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
Access_Flag result = 0;
if (api_check_buffer(file)){
result = file_get_access_flags(file);
}
return(result);
}
api(custom) function i64
buffer_get_size(Application_Links *app, Buffer_ID buffer_id){
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
i64 result = 0;
if (api_check_buffer(file)){
result = buffer_size(&file->state.buffer);
}
return(result);
}
api(custom) function i64
buffer_get_line_count(Application_Links *app, Buffer_ID buffer_id){
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
u64 result = 0;
if (api_check_buffer(file)){
result = buffer_line_count(&file->state.buffer);
}
return(result);
}
api(custom) function String_Const_u8
push_buffer_base_name(Application_Links *app, Arena *arena, Buffer_ID buffer_id){
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
String_Const_u8 result = {};
if (api_check_buffer(file)){
result = push_string_copy(arena, string_from_file_name(&file->base_name));
}
return(result);
}
api(custom) function String_Const_u8
push_buffer_unique_name(Application_Links *app, Arena *out, Buffer_ID buffer_id){
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
String_Const_u8 result = {};
if (api_check_buffer(file)){
result = push_string_copy(out, string_from_file_name(&file->unique_name));
}
return(result);
}
api(custom) function String_Const_u8
push_buffer_file_name(Application_Links *app, Arena *arena, Buffer_ID buffer_id){
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
String_Const_u8 result = {};
if (api_check_buffer(file)){
result = push_string_copy(arena, string_from_file_name(&file->canon));
}
return(result);
}
api(custom) function Dirty_State
buffer_get_dirty_state(Application_Links *app, Buffer_ID buffer_id){
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
Dirty_State result = 0;
if (api_check_buffer(file)){
result = file->state.dirty;
}
return(result);
}
api(custom) function b32
buffer_set_dirty_state(Application_Links *app, Buffer_ID buffer_id, Dirty_State dirty_state){
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
b32 result = false;
if (api_check_buffer(file)){
result = true;
file->state.dirty = dirty_state;
}
return(result);
}
api(custom) function b32
buffer_set_layout(Application_Links *app, Buffer_ID buffer_id, Layout_Function *layout_func){
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
b32 result = false;
if (api_check_buffer(file)){
result = true;
file->settings.layout_func = layout_func;
file_clear_layout_cache(file);
}
return(result);
}
api(custom) function b32
buffer_clear_layout_cache(Application_Links *app, Buffer_ID buffer_id){
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
b32 result = false;
if (api_check_buffer(file)){
result = true;
file_clear_layout_cache(file);
}
return(result);
}
api(custom) function Layout_Function*
buffer_get_layout(Application_Links *app, Buffer_ID buffer_id){
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
Layout_Function *result = 0;
if (api_check_buffer(file)){
result = file->settings.layout_func;
}
return(result);
}
api(custom) function b32
buffer_get_setting(Application_Links *app, Buffer_ID buffer_id, Buffer_Setting_ID setting, i64 *value_out)
{
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
b32 result = false;
if (api_check_buffer(file)){
result = true;
switch (setting){
case BufferSetting_Unimportant:
{
*value_out = file->settings.unimportant;
}break;
case BufferSetting_Unkillable:
{
*value_out = (file->settings.never_kill || file->settings.unkillable);
}break;
case BufferSetting_ReadOnly:
{
*value_out = file->settings.read_only;
}break;
case BufferSetting_RecordsHistory:
{
*value_out = history_is_activated(&file->state.history);
}break;
default:
{
result = false;
}break;
}
}
return(result);
}
api(custom) function b32
buffer_set_setting(Application_Links *app, Buffer_ID buffer_id, Buffer_Setting_ID setting, i64 value)
{
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
b32 result = false;
if (api_check_buffer(file)){
result = true;
switch (setting){
case BufferSetting_Unimportant:
{
if (value != 0){
file_set_unimportant(file, true);
}
else{
file_set_unimportant(file, false);
}
}break;
case BufferSetting_Unkillable:
{
file->settings.unkillable = (value != 0);
}break;
case BufferSetting_ReadOnly:
{
file->settings.read_only = (value != 0);
}break;
case BufferSetting_RecordsHistory:
{
if (value){
if (!history_is_activated(&file->state.history)){
history_init(app->tctx, models, &file->state.history);
}
}
else{
if (history_is_activated(&file->state.history)){
history_free(app->tctx, &file->state.history);
}
}
}break;
default:
{
result = 0;
}break;
}
}
return(result);
}
api(custom) function Managed_Scope
buffer_get_managed_scope(Application_Links *app, Buffer_ID buffer_id)
{
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
Managed_Scope result = 0;
if (api_check_buffer(file)){
result = file_get_managed_scope(file);
}
return(result);
}
api(custom) function b32
buffer_send_end_signal(Application_Links *app, Buffer_ID buffer_id)
{
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
b32 result = false;
if (api_check_buffer(file)){
file_end_file(app->tctx, models, file);
result = true;
}
return(result);
}
api(custom) function Buffer_ID
create_buffer(Application_Links *app, String_Const_u8 file_name, Buffer_Create_Flag flags)
{
Models *models = (Models*)app->cmd_context;
Editing_File *new_file = create_file(app->tctx, models, file_name, flags);
Buffer_ID result = 0;
if (new_file != 0){
result = new_file->id;
}
return(result);
}
api(custom) function b32
buffer_save(Application_Links *app, Buffer_ID buffer_id, String_Const_u8 file_name, Buffer_Save_Flag flags)
{
Models *models = (Models*)app->cmd_context;
Editing_File *file = imp_get_file(models, buffer_id);
b32 result = false;
if (api_check_buffer(file)){
b32 skip_save = false;
if (!HasFlag(flags, BufferSave_IgnoreDirtyFlag)){
if (file->state.dirty == DirtyState_UpToDate){
skip_save = true;
}
}
if (!skip_save){
Thread_Context *tctx = app->tctx;
Scratch_Block scratch(tctx);
String_Const_u8 name = push_string_copy(scratch, file_name);
save_file_to_name(tctx, models, file, name.str);
result = true;
}
}
return(result);
}
api(custom) function Buffer_Kill_Result
buffer_kill(Application_Links *app, Buffer_ID buffer_id, Buffer_Kill_Flag flags)
{
Models *models = (Models*)app->cmd_context;
Working_Set *working_set = &models->working_set;
Editing_File *file = imp_get_file(models, buffer_id);
Buffer_Kill_Result result = BufferKillResult_DoesNotExist;
if (api_check_buffer(file)){
if (!file->settings.never_kill && !file->settings.unkillable){
b32 needs_to_save = file_needs_save(file);
if (!needs_to_save || (flags & BufferKill_AlwaysKill) != 0){
Thread_Context *tctx = app->tctx;
if (models->end_buffer != 0){
models->end_buffer(app, file->id);
}
buffer_unbind_name_low_level(working_set, file);
if (file->canon.name_size != 0){
buffer_unbind_file(working_set, file);
}
file_free(tctx, models, file);
working_set_free_file(&models->heap, working_set, file);
Layout *layout = &models->layout;
Node *order = &working_set->touch_order_sentinel;
Node *file_node = order->next;
for (Panel *panel = layout_get_first_open_panel(layout);
panel != 0;
panel = layout_get_next_open_panel(layout, panel)){
View *view = panel->view;
if (view->file == file){
Assert(file_node != order);
view->file = 0;
Editing_File *new_file = CastFromMember(Editing_File, touch_node, file_node);
view_set_file(tctx, models, view, new_file);
file_node = file_node->next;
if (file_node == order){
file_node = file_node->next;
}
Assert(file_node != order);
}
}
Child_Process_Container *child_processes = &models->child_processes;
for (Node *node = child_processes->child_process_active_list.next;
node != &child_processes->child_process_active_list;
node = node->next){
Child_Process *child_process = CastFromMember(Child_Process, node, node);
if (child_process->out_file == file){
child_process->out_file = 0;
}
}
result = BufferKillResult_Killed;
}
else{
result = BufferKillResult_Dirty;
}
}
else{
result = BufferKillResult_Unkillable;
}
}
return(result);
}
api(custom) function Buffer_Reopen_Result
buffer_reopen(Application_Links *app, Buffer_ID buffer_id, Buffer_Reopen_Flag flags)
{
Models *models = (Models*)app->cmd_context;
Thread_Context *tctx = app->tctx;
Scratch_Block scratch(tctx);
Editing_File *file = imp_get_file(models, buffer_id);
Buffer_Reopen_Result result = BufferReopenResult_Failed;
if (api_check_buffer(file)){
if (file->canon.name_size > 0){
Plat_Handle handle = {};
if (system_load_handle(scratch, (char*)file->canon.name_space, &handle)){
File_Attributes attributes = system_load_attributes(handle);
char *file_memory = push_array(scratch, char, (i32)attributes.size);
if (file_memory != 0){
if (system_load_file(handle, file_memory, (i32)attributes.size)){
system_load_close(handle);
// TODO(allen): try(perform a diff maybe apply edits in reopen)
i32 line_numbers[16];
i32 column_numbers[16];
View *vptrs[16];
i32 vptr_count = 0;
Layout *layout = &models->layout;
for (Panel *panel = layout_get_first_open_panel(layout);
panel != 0;
panel = layout_get_next_open_panel(layout, panel)){
View *view_it = panel->view;
if (view_it->file == file){
vptrs[vptr_count] = view_it;
File_Edit_Positions edit_pos = view_get_edit_pos(view_it);
Buffer_Cursor cursor = file_compute_cursor(view_it->file, seek_pos(edit_pos.cursor_pos));
line_numbers[vptr_count] = (i32)cursor.line;
column_numbers[vptr_count] = (i32)cursor.col;
view_it->file = models->scratch_buffer;
++vptr_count;
}
}
Working_Set *working_set = &models->working_set;
file_free(tctx, models, file);
working_set_file_default_settings(working_set, file);
file_create_from_string(tctx, models, file, SCu8(file_memory, attributes.size), attributes);
for (i32 i = 0; i < vptr_count; ++i){
view_set_file(tctx, models, vptrs[i], file);
vptrs[i]->file = file;
i64 line = line_numbers[i];
i64 col = column_numbers[i];
Buffer_Cursor cursor = file_compute_cursor(file, seek_line_col(line, col));