-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4coder_sloth_commands.cpp
More file actions
1012 lines (879 loc) · 37.5 KB
/
4coder_sloth_commands.cpp
File metadata and controls
1012 lines (879 loc) · 37.5 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
CUSTOM_COMMAND_SIG(lily_view_toggle_passive_lock)
CUSTOM_DOC("Toggles a view's passive setting; use this to pin/lock a view to the screen forever as a reference window.")
{
View_ID view = get_active_view(app, Access_ReadWriteVisible);
view_set_passive(app, view, !view_get_is_passive(app, view));
}
CUSTOM_COMMAND_SIG(lily_arrow)
CUSTOM_DOC("Inserts an arrow \"->\" string")
{
write_string(app, string_u8_litexpr("->"));
}
CUSTOM_COMMAND_SIG(lily_toggle_hex_colors)
CUSTOM_DOC("Toggles hex colors for all files.")
{
String_ID key = vars_save_string_lit("show_hex_colors");
b32 show_hex_colors = def_get_config_b32(key);
def_set_config_b32(key, !show_hex_colors);
}
CUSTOM_COMMAND_SIG(lily_query_replace_selection)
CUSTOM_DOC("Queries the user for a string, and incrementally replace every occurence of the string found in the selected range with the specified string.")
{
View_ID view = get_active_view(app, Access_ReadWriteVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
if (buffer != 0) {
Scratch_Block scratch(app);
Range_i64 range = get_view_range(app, view);
String_Const_u8 replace = push_buffer_range(app, scratch, buffer, range);
if (replace.size != 0) {
query_replace_parameter(app, replace, range.min, true);
} else {
qol_bot_text_set(string_u8_litexpr("Zero range; select a range and invoke again"));
}
}
}
function void
sloth_close_files(Application_Links *app)
{
Scratch_Block scratch(app);
i32 buffers_to_close_max = Thousand(100);
Buffer_ID *buffers_to_close = push_array(scratch, Buffer_ID, buffers_to_close_max);
b32 do_repeat = false;
do{
i32 buffers_to_close_count = 0;
do_repeat = false;
for (Buffer_ID buffer = get_buffer_next(app, 0, Access_Always);
buffer != 0;
buffer = get_buffer_next(app, buffer, Access_Always)) {
if (buffers_to_close_count >= buffers_to_close_max) {
do_repeat = true;
break;
}
buffers_to_close[buffers_to_close_count++] = buffer;
}
for (i32 i = 0; i < buffers_to_close_count; ++i) {
buffer_kill(app, buffers_to_close[i], BufferKill_AlwaysKill);
}
} while(do_repeat);
}
CUSTOM_COMMAND_SIG(lily_close_all_open_buffers)
CUSTOM_DOC("Closes all the buffers.")
{
sloth_close_files(app);
}
CUSTOM_COMMAND_SIG(lily_move_up_11)
CUSTOM_DOC("Moves down to the next line of actual text, regardless of line wrapping.")
{
// move_vertical_lines(app, -11); // NOTE(sloth): simple version
View_ID view = get_active_view(app, Access_ReadWriteVisible);
i64 pos = view_get_cursor_pos(app, view);
Buffer_Cursor cursor = view_compute_cursor(app, view, seek_pos(pos));
i64 next_line = cursor.line - 11;
view_set_cursor_and_preferred_x(app, view, seek_line_col(next_line, 1));
}
CUSTOM_COMMAND_SIG(lily_move_down_11)
CUSTOM_DOC("Moves down to the next line of actual text, regardless of line wrapping.")
{
// move_vertical_lines(app, 11); // NOTE(sloth): simple version
View_ID view = get_active_view(app, Access_ReadWriteVisible);
i64 pos = view_get_cursor_pos(app, view);
Buffer_Cursor cursor = view_compute_cursor(app, view, seek_pos(pos));
i64 next_line = cursor.line + 11;
view_set_cursor_and_preferred_x(app, view, seek_line_col(next_line, 1));
}
CUSTOM_COMMAND_SIG(lily_delete_to_end_of_line)
CUSTOM_DOC("Deletes everything from the cursor to the end of the line")
{
View_ID view = get_active_view(app, Access_ReadWriteVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
i64 pos = view_get_cursor_pos(app, view);
i64 line = get_line_number_from_pos(app, buffer, pos);
Range_i64 range = get_line_pos_range(app, buffer, line);
if (pos == range.end) {
range.end = pos + 1;
range.start = pos;
} else {
range.start = pos + 1;
}
i32 size = (i32)buffer_get_size(app, buffer);
range.end = clamp_top(range.end, size);
if (range_size(range) == 0 || buffer_get_char(app, buffer, range.end - 1) != '\n') {
range.start -= 1;
range.first = clamp_bot(0, range.first);
}
buffer_replace_range(app, buffer, range, string_u8_litexpr(""));
}
CUSTOM_COMMAND_SIG(lily_backspace_to_front_of_line)
CUSTOM_DOC("Deletes everything from the cursor to the beginning of the line")
{
View_ID view = get_active_view(app, Access_ReadWriteVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
i64 pos = view_get_cursor_pos(app, view);
i64 line = get_line_number_from_pos(app, buffer, pos);
Range_i64 range = get_line_pos_range(app, buffer, line);
range.max = pos;
buffer_replace_range(app, buffer, range, string_u8_litexpr(""));
}
CUSTOM_COMMAND_SIG(lily_new_line_below)
CUSTOM_DOC("Creates a new line below the cursor without moving the cursor down a line")
{
// TODO(sloth): undo is a little goofy
View_ID view = get_active_view(app, Access_ReadWriteVisible);
i64 oldpos = view_get_cursor_pos(app, view);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
move_down(app);
i64 editpos = view_get_cursor_pos(app, view);
Scratch_Block scratch(app);
i64 line = get_line_number_from_pos(app, buffer, editpos);
i64 pos = get_line_side_pos(app, buffer, line, Side_Min);
buffer_replace_range(app, buffer, Ii64(pos), string_u8_litexpr("\n"));
view_set_cursor_and_preferred_x(app, view, seek_pos(oldpos));
}
CUSTOM_COMMAND_SIG(lily_new_line_below_and_go)
CUSTOM_DOC("Creates a new line below the cursor without modifying the current line following the cursor")
{
// TODO(sloth): undo is a little goofy
lily_new_line_below(app);
move_down(app);
}
CUSTOM_COMMAND_SIG(lily_close_panel)
CUSTOM_DOC("Closes the currently active panel if it is not the only panel open.")
{
View_ID view = get_active_view(app, Access_Always);
if (view != get_next_view_looped_primary_panels(app, view, Access_Always)){
view_close(app, view);
} else {
qol_bot_text_set(str8_lit("Cannot close last view"));
}
}
CUSTOM_COMMAND_SIG(lily_count_views)
CUSTOM_DOC("Count number of views.")
{
Scratch_Block scratch(app);
View_ID view = get_active_view(app, Access_Always);
View_ID start, current;
start = view;
current = start;
i32 count = 0;
do {
current = get_next_view_looped_all_panels(app, current, Access_Visible);
++count;
} while (current != start);
String_Const_u8 message = push_stringf(scratch, "%s:%d", "Views found\n", count);
qol_bot_text_set(message);
print_message(app, message);
// return(count);
}
void
sloth_font_change(Application_Links *app, i32 vector, Buffer_ID buffer) {
Face_ID face_id = get_face_id(app, buffer);
Face_Description description = get_face_description(app, face_id);
description.parameters.pt_size += vector;
try_modify_face(app, face_id, &description);
description = get_face_description(app, qol_small_face);
description.parameters.pt_size += vector;
try_modify_face(app, qol_small_face, &description);
}
CUSTOM_COMMAND_SIG(lily_increase_face_size)
CUSTOM_DOC("Increase the size of the face used by the current buffer.")
{
View_ID view = get_active_view(app, Access_Always);
Buffer_ID buffer = view_get_buffer(app, view, Access_Always);
sloth_font_change(app, 1, buffer);
}
CUSTOM_COMMAND_SIG(lily_decrease_face_size)
CUSTOM_DOC("Decrease the size of the face used by the current buffer.")
{
View_ID view = get_active_view(app, Access_Always);
Buffer_ID buffer = view_get_buffer(app, view, Access_Always);
sloth_font_change(app, -1, buffer);
}
CUSTOM_COMMAND_SIG(lily_mouse_wheel_change_face_size)
CUSTOM_DOC("Reads the state of the mouse wheel and uses it to either increase or decrease the face size.")
{
local_persist u64 next_resize_time = 0;
u64 now = system_now_time();
if (now >= next_resize_time){
next_resize_time = now + 30*1000; // NOTE(sloth): slows it down for trackpads cuz they zoom
Mouse_State mouse = get_mouse_state(app);
if (mouse.wheel > 0){
lily_decrease_face_size(app);
}
else if (mouse.wheel < 0){
lily_increase_face_size(app);
}
}
}
function Buffer_ID
sloth_buffer_identifier_to_id_create_out_buffer(Application_Links *app, Buffer_Identifier buffer_id){
// NOTE(sloth): all this to get a read-write command buffer
Buffer_ID result = 0;
if (buffer_id.name != 0 && buffer_id.name_len > 0){
String_Const_u8 buffer_name = SCu8(buffer_id.name, buffer_id.name_len);
Buffer_ID buffer_attach_id = get_buffer_by_name(app, buffer_name, Access_Always);
if (buffer_attach_id != 0){
result = buffer_attach_id;
}
else{
buffer_attach_id = create_buffer(app, buffer_name, BufferCreate_AlwaysNew|BufferCreate_NeverAttachToFile);
if (buffer_attach_id != 0){
buffer_set_setting(app, buffer_attach_id, BufferSetting_ReadOnly, false);
buffer_set_setting(app, buffer_attach_id, BufferSetting_Unimportant, true);
result = buffer_attach_id;
}
}
}
else{
result = buffer_id.id;
}
return(result);
}
function b32
sloth_exec_system_command(Application_Links *app, View_ID view, Buffer_Identifier buffer_id,
String_Const_u8 path, String_Const_u8 command, Command_Line_Interface_Flag flags){
b32 result = false;
Child_Process_ID child_process_id = create_child_process(app, path, command);
if (child_process_id != 0){
result = true;
Buffer_ID buffer_attach_id = sloth_buffer_identifier_to_id_create_out_buffer(app, buffer_id);
if (buffer_attach_id != 0){
if (set_buffer_system_command(app, child_process_id, buffer_attach_id, flags)){
if (view != 0){
view_set_buffer(app, view, buffer_attach_id, 0);
view_set_cursor(app, view, seek_pos(0));
}
}
}
}
return(result);
}
CUSTOM_COMMAND_SIG(lily_execute_previous_cli)
CUSTOM_DOC("If the command execute_any_cli has already been used, this will execute a CLI reusing the most recent buffer name and command.")
{
// TODO(sloth): @todotracked this should really go into a command table that we can re-execute from the active view
local_persist int command_count = 0;
Scratch_Block scratch(app);
String_Const_u8 out_buffer_name = push_stringf(scratch, "cmd%d", ++command_count);
String_Const_u8 cmd = SCu8(command_space);
String_Const_u8 hot_directory = SCu8(hot_directory_space);
if (out_buffer_name.size > 0 && cmd.size > 0 && hot_directory.size > 0){
View_ID view = get_active_view(app, Access_Always);
Buffer_Identifier id = buffer_identifier(out_buffer_name);
sloth_exec_system_command(app, view, id, hot_directory, cmd, CLI_OverlapWithConflict|CLI_CursorAtEnd|CLI_SendEndSignal);
lock_jump_buffer(app, out_buffer_name);
}
}
CUSTOM_COMMAND_SIG(lily_execute_any_cli)
CUSTOM_DOC("Queries for an output buffer name and system command, runs the system command as a CLI and prints the output to the specified buffer."){
Scratch_Block scratch(app);
Query_Bar_Group group(app);
Query_Bar bar_cmd = {};
bar_cmd.prompt = string_u8_litexpr("Command: ");
bar_cmd.string = SCu8(command_space, (u64)0);
bar_cmd.string_capacity = sizeof(command_space);
if (!query_user_string(app, &bar_cmd)) return;
bar_cmd.string.size = clamp_top(bar_cmd.string.size, sizeof(command_space) - 1);
command_space[bar_cmd.string.size] = 0;
String_Const_u8 hot = push_hot_directory(app, scratch);
{
u64 size = clamp_top(hot.size, sizeof(hot_directory_space));
block_copy(hot_directory_space, hot.str, size);
hot_directory_space[hot.size] = 0;
}
// NOTE(sloth): meat and potatoes
lily_execute_previous_cli(app);
}
CUSTOM_COMMAND_SIG(lily_toggler)
CUSTOM_DOC("Replaces the character under the cursor with a complementory character, for example replaces 0 with 1, replaces . with ->, replaces lowercase with uppercase and vice versa etc."){
// NOTE(sloth): lifted from flyingsolomon
View_ID view = get_active_view(app, Access_ReadWrite);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWrite);
i64 pos = view_get_cursor_pos(app, view);
#define TOGGLER_GET_CHAR(offset) buffer_get_char(app, buffer, pos+(offset))
#define TOGGLER_REPLACE_RANGE(min, max, string) buffer_replace_range(app, buffer, Ii64(pos+(min), pos+(max)), string_u8_litexpr((string)))
char c = TOGGLER_GET_CHAR(0);
switch(c){
case '-': {
if(TOGGLER_GET_CHAR(1) == '-') TOGGLER_REPLACE_RANGE(0, 2, "++");
else if(TOGGLER_GET_CHAR(1) == '-') TOGGLER_REPLACE_RANGE(-1, 1, "++");
else if(TOGGLER_GET_CHAR(1) == '>') TOGGLER_REPLACE_RANGE(0, 2, ".");
else TOGGLER_REPLACE_RANGE(0, 1, "+");
} break;
case '+': {
if(TOGGLER_GET_CHAR(1) == '+') TOGGLER_REPLACE_RANGE(0, 2, "--");
else if(TOGGLER_GET_CHAR(-1) == '+') TOGGLER_REPLACE_RANGE(-1, 1, "--");
else TOGGLER_REPLACE_RANGE(0, 1, "-");
} break;
case '=': {
if(TOGGLER_GET_CHAR(1) == '=') TOGGLER_REPLACE_RANGE(0, 2, "!=");
else if(TOGGLER_GET_CHAR(-1) == '=') TOGGLER_REPLACE_RANGE(-1, 1, "!=");
else if(TOGGLER_GET_CHAR(-1) == '!') TOGGLER_REPLACE_RANGE(-1, 1, "==");
else if(TOGGLER_GET_CHAR(-1) == '-') TOGGLER_REPLACE_RANGE(-1, 1, "+=");
else if(TOGGLER_GET_CHAR(-1) == '+') TOGGLER_REPLACE_RANGE(-1, 1, "-=");
else if(TOGGLER_GET_CHAR(-1) == '<') {
if(TOGGLER_GET_CHAR(-2) == '<') TOGGLER_REPLACE_RANGE(-2, 1, ">>=");
else TOGGLER_REPLACE_RANGE(-1, 1, ">=");
} else if(TOGGLER_GET_CHAR(-1) == '>') {
if(TOGGLER_GET_CHAR(-2) == '>') TOGGLER_REPLACE_RANGE(-2, 1, "<<=");
else TOGGLER_REPLACE_RANGE(-1, 1, "<=");
}
} break;
case '!': {
if(TOGGLER_GET_CHAR(1) == '=') TOGGLER_REPLACE_RANGE(0, 2, "==");
} break;
case '<': {
if(TOGGLER_GET_CHAR(-1) == '<') TOGGLER_REPLACE_RANGE(-1, 1, ">>");
else if(TOGGLER_GET_CHAR(1) == '<') TOGGLER_REPLACE_RANGE(0, 2, ">>");
else if(TOGGLER_GET_CHAR(1) == '=') TOGGLER_REPLACE_RANGE(0, 2, ">=");
else TOGGLER_REPLACE_RANGE(0, 1, ">");
} break;
case '>': {
if(TOGGLER_GET_CHAR(-1) == '-') TOGGLER_REPLACE_RANGE(-1, 1, ".");
else if(TOGGLER_GET_CHAR(-1) == '>') TOGGLER_REPLACE_RANGE(-1, 1, "<<");
else if(TOGGLER_GET_CHAR(1) == '>') TOGGLER_REPLACE_RANGE(0, 2, "<<");
else if(TOGGLER_GET_CHAR(1) == '=') TOGGLER_REPLACE_RANGE(0, 2, "<=");
else TOGGLER_REPLACE_RANGE(0, 1, "<");
} break;
case '|': {
if(TOGGLER_GET_CHAR(-1) == '|') TOGGLER_REPLACE_RANGE(-1, 1, "&&");
else if(TOGGLER_GET_CHAR(1) == '|') TOGGLER_REPLACE_RANGE(0, 2, "&&");
else TOGGLER_REPLACE_RANGE(0, 1, "&");
} break;
case '&': {
if(TOGGLER_GET_CHAR(-1) == '&') TOGGLER_REPLACE_RANGE(-1, 1, "||");
else if(TOGGLER_GET_CHAR(1) == '&') TOGGLER_REPLACE_RANGE(0, 2, "||");
else TOGGLER_REPLACE_RANGE(0, 1, "|");
} break;
case '.': { TOGGLER_REPLACE_RANGE(0, 1, "->"); } break;
case '0': { TOGGLER_REPLACE_RANGE(0, 1, "1"); } break;
case '1': { TOGGLER_REPLACE_RANGE(0, 1, "0"); } break;
case '\\': { TOGGLER_REPLACE_RANGE(0, 1, "/"); } break;
case '/': { TOGGLER_REPLACE_RANGE(0, 1, "\\"); } break;
case ' ': { TOGGLER_REPLACE_RANGE(0, 1, "_"); } break;
case '_': { TOGGLER_REPLACE_RANGE(0, 1, " "); } break;
case '"': { TOGGLER_REPLACE_RANGE(0, 1, "'"); } break;
case '\'': { TOGGLER_REPLACE_RANGE(0, 1, "\""); } break;
default: {
if(character_is_upper(c)) c = character_to_lower(c);
else if(character_is_lower(c)) c = character_to_upper(c);
buffer_replace_range(app, buffer, Ii64(pos, pos + 1), SCu8(&c, 1));
} break;
}
#undef TOGGLER_REPLACE_RANGE
#undef TOGGLER_GET_CHAR
}
function void mod_describe_meta(Application_Links *app, Command_Metadata *meta) {
Scratch_Block scratch(app);
String_Const_u8 command_name = SCu8(meta->name, meta->name_len);
Doc_Cluster *docs = doc_commands(scratch);
Doc_Page *page = doc_get_page(docs, command_name);
if(page) {
Buffer_ID doc_buffer = render_doc_page(app, page);
String_Const_u8 source_name = SCu8(meta->source_name, meta->source_name_len);
String_Const_u8 command_location =
push_u8_stringf(scratch, "%.*s:%d:1: %.*s\n", string_expand(source_name), meta->line_number, meta->name_len, meta->name);
buffer_replace_range(app, doc_buffer, Ii64(), command_location);
View_ID doc_view = get_or_open_build_panel(app);
view_set_buffer(app, doc_view, doc_buffer, Access_Read);
}
}
CUSTOM_COMMAND_SIG(lily_describe_binding)
CUSTOM_DOC("Shows a given key binding descriptions.") {
// NOTE(sloth): from flyingsolomon
View_ID view = get_this_ctx_view(app, Access_Always);
if(!view_exists(app, view)) return;
Query_Bar_Group group(app);
Query_Bar bar = {};
bar.prompt = string_u8_litexpr("Key combo");
start_query_bar(app, &bar, 0);
Command_Metadata *meta = 0;
for(;;) {
User_Input input = get_next_input(app, EventPropertyGroup_AnyUserInput, EventProperty_Escape);
if(input.abort) break;
View_Context ctx = view_current_context(app, view);
// Mapping *mapping = ctx.mapping;
// Command_Map *map = mapping_get_map(mapping, ctx.map_id);
Command_Binding binding = map_get_binding_recursive(&framework_mapping, ctx.mapping->last_map->id, &input.event);
meta = get_command_metadata(binding.custom);
if(meta) break;
}
if(meta) {
mod_describe_meta(app, meta);
}
}
CUSTOM_COMMAND_SIG(lily_scroll_right)
CUSTOM_DOC("Scroll the buffer right")
{
View_ID view = get_active_view(app, Access_ReadVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadVisible);
Buffer_Scroll scroll = view_get_buffer_scroll(app, view);
Face_ID face = get_face_id(app, buffer);
f32 space_advance = get_face_metrics(app, face).space_advance;
scroll.target = view_move_buffer_point(app, view, scroll.target, V2f32(10.f*space_advance, 0.f));
view_set_buffer_scroll(app, view, scroll, SetBufferScroll_SnapCursorIntoView);
}
CUSTOM_COMMAND_SIG(lily_scroll_left)
CUSTOM_DOC("Scroll the buffer left")
{
View_ID view = get_active_view(app, Access_ReadVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadVisible);
Buffer_Scroll scroll = view_get_buffer_scroll(app, view);
Face_ID face = get_face_id(app, buffer);
f32 space_advance = get_face_metrics(app, face).space_advance;
scroll.target = view_move_buffer_point(app, view, scroll.target, V2f32(-10.f*space_advance, 0));
view_set_buffer_scroll(app, view, scroll, SetBufferScroll_SnapCursorIntoView);
}
CUSTOM_COMMAND_SIG(lily_scroll_down)
CUSTOM_DOC("Scroll the buffer up")
{
View_ID view = get_active_view(app, Access_ReadVisible);
Buffer_Scroll scroll = view_get_buffer_scroll(app, view);
f32 line_height = get_view_line_height(app, view);
scroll.target = view_move_buffer_point(app, view, scroll.target, V2f32(0.f, 2.f*line_height));
view_set_buffer_scroll(app, view, scroll, SetBufferScroll_SnapCursorIntoView);
}
CUSTOM_COMMAND_SIG(lily_scroll_up)
CUSTOM_DOC("Scroll the buffer up")
{
View_ID view = get_active_view(app, Access_ReadVisible);
Buffer_Scroll scroll = view_get_buffer_scroll(app, view);
f32 line_height = get_view_line_height(app, view);
scroll.target = view_move_buffer_point(app, view, scroll.target, V2f32(0.f, -2.f*line_height));
view_set_buffer_scroll(app, view, scroll, SetBufferScroll_SnapCursorIntoView);
}
CUSTOM_COMMAND_SIG(lily_comment_line_toggle)
CUSTOM_DOC("Turns uncommented lines into commented lines using '// ' and vice versa.")
{
View_ID view = get_active_view(app, Access_ReadWriteVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
i64 pos = get_start_of_line_at_cursor(app, view, buffer);
i64 buffer_size = buffer_get_size(app, buffer);
b32 already_has_comment = c_line_comment_starts_at_position(app, buffer, pos);
if (already_has_comment) {
i64 space_check_pos = pos + 2;
b32 has_space_after_comment = false;
if (space_check_pos < buffer_size) {
char after_comment = buffer_get_char(app, buffer, space_check_pos);
has_space_after_comment = (after_comment == ' ');
}
if (has_space_after_comment) {
buffer_replace_range(app, buffer, Ii64(pos, pos + 3), string_u8_empty);
} else {
buffer_replace_range(app, buffer, Ii64(pos, pos + 2), string_u8_empty);
}
} else {
buffer_replace_range(app, buffer, Ii64(pos), string_u8_litexpr("// "));
}
move_vertical_lines(app, 1);
}
internal void
sloth_list_all_locations__generic_query(Application_Links *app, List_All_Locations_Flag flags){
Scratch_Block scratch(app);
String_Const_u8 query = push_token_or_word_under_active_cursor(app, scratch);
list_all_locations__generic(app, query, flags);
}
CUSTOM_COMMAND_SIG(lily_list_all_locations_at_cursor)
CUSTOM_DOC("Reads the string under the cursor and lists all exact case-sensitive mathces in all open buffers.")
{
sloth_list_all_locations__generic_query(app, ListAllLocationsFlag_CaseSensitive);
// this one below kinda is like an of_selection kind, which we have already in ctrl-shift-g
// list_all_locations__generic_view_range(app, ListAllLocationsFlag_CaseSensitive);
}
// NOTE(sloth): @do i identify as a definition?
CUSTOM_COMMAND_SIG(lily_jump_to_definition_at_cursor_in_other)
CUSTOM_DOC("Jumps to a definition under the cursor in the other panel.")
{
View_ID view = get_active_view(app, Access_Always);
Scratch_Block scratch(app);
String_Const_u8 query = push_token_or_word_under_active_cursor(app, scratch);
view = get_next_view_looped_primary_panels(app, view, Access_Always);
if (view != 0){
code_index_lock();
for (Buffer_ID buffer = get_buffer_next(app, 0, Access_Always);
buffer != 0;
buffer = get_buffer_next(app, buffer, Access_Always)){
Code_Index_File *file = code_index_get_file(buffer);
if (file != 0){
for (i32 i = 0; i < file->note_array.count; i += 1){
Code_Index_Note *note = file->note_array.ptrs[i];
if (string_match(note->text, query)){
view_set_active(app, view);
point_stack_push_view_cursor(app, view);
jump_to_location(app, view, buffer, note->pos.first);
goto done; // double break
}
}
}
}
done:
code_index_unlock();
}
}
CUSTOM_COMMAND_SIG(lily_cut)
CUSTOM_DOC("Cut the text in the range from the cursor to the mark onto the clipboard. Cut the whole line when cursor and mark are the same.")
{
View_ID view = get_active_view(app, Access_ReadWriteVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
Range_i64 range = get_view_range(app, view);
if (range.min == range.max) {
i64 buffer_size = buffer_get_size(app, buffer);
// NOTE(sloth): to enclose text when cursor is at the end, we have
// to walk backward to keep looking for spaces
range = enclose_whole_lines(app, buffer, range);
if (range.min == range.max) { // we're at the end of the line
char first_char = buffer_get_char(app, buffer, range.min);
while (range.min > 0 && character_is_whitespace(first_char)) {
--range.min;
first_char = buffer_get_char(app, buffer, range.min);
if (first_char == '\n') {
++range.min;
break;
}
}
range = enclose_whole_lines(app, buffer, range);
}
// NOTE(sloth): enclose whole lines including newline
if (range.max < buffer_size) {
char next_char = buffer_get_char(app, buffer, range.max);
if (next_char == '\n') {
range.max += 1;
} else if (next_char == '\r') {
range.max += 1;
if (range.max < buffer_size) {
next_char = buffer_get_char(app, buffer, range.max);
if (next_char == '\n') {
range.max += 1;
}
}
}
}
}
if (clipboard_post_buffer_range(app, 0, buffer, range)) {
buffer_replace_range(app, buffer, range, string_u8_empty);
}
}
CUSTOM_COMMAND_SIG(lily_copy)
CUSTOM_DOC("Copy the text in the range from the cursor to the mark onto the clipboard. Copy the whole line when cursor and mark are the same.")
{
View_ID view = get_active_view(app, Access_ReadVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadVisible);
Range_i64 range = get_view_range(app, view);
if (range.min == range.max) {
i64 buffer_size = buffer_get_size(app, buffer);
// NOTE(sloth): to enclose text when cursor is at the end, we have
// to walk backward to keep looking for spaces
range = enclose_whole_lines(app, buffer, range);
if (range.min == range.max) { // we're at the end of the line
char first_char = buffer_get_char(app, buffer, range.min);
while (range.min > 0 && character_is_whitespace(first_char)) {
--range.min;
first_char = buffer_get_char(app, buffer, range.min);
if (first_char == '\n') {
++range.min;
break;
}
}
range = enclose_whole_lines(app, buffer, range);
}
// NOTE(sloth): enclose whole lines including newline
if (range.max < buffer_size) {
char next_char = buffer_get_char(app, buffer, range.max);
if (next_char == '\n') {
range.max += 1;
} else if (next_char == '\r') {
range.max += 1;
if (range.max < buffer_size) {
next_char = buffer_get_char(app, buffer, range.max);
if (next_char == '\n') {
range.max += 1;
}
}
}
}
}
clipboard_post_buffer_range(app, 0, buffer, range);
}
function void
sloth_lister_fill_buffers(Application_Links *app, Lister *lister) {
for (Buffer_ID buffer = get_buffer_next(app, 0, Access_Always);
buffer != 0;
buffer = get_buffer_next(app, buffer, Access_Always)){
if (!buffer_has_name_with_star(app, buffer)){
Dirty_State dirty = buffer_get_dirty_state(app, buffer);
if (dirty == DirtyState_UnsavedChanges ||
dirty == DirtyState_UnloadedChanges ||
dirty == DirtyState_UnsavedChangesAndUnloadedChanges) {
generate_all_buffers_list__output_buffer(app, lister, buffer);
}
}
}
}
function void
sloth_lister_refill_buffers(Application_Links *app, Lister *lister){
lister_begin_new_item_set(app, lister);
sloth_lister_fill_buffers(app, lister);
}
function Buffer_ID
sloth_get_buffer_from_user(Application_Links *app, String_Const_u8 query){
Lister_Handlers handlers = lister_get_default_handlers();
handlers.refresh = sloth_lister_refill_buffers;
Lister_Result l_result = run_lister_with_refresh_handler(app, query, handlers);
Buffer_ID result = 0;
if (!l_result.canceled){
result = (Buffer_ID)(PtrAsInt(l_result.user_data));
} else {
Scratch_Block scratch(app);
String_Const_u8 message = push_stringf(scratch, "%s", "No lister options found\n");
qol_bot_text_set(message);
}
return(result);
}
function Buffer_ID
sloth_get_buffer_from_user(Application_Links *app, char *query){
return(sloth_get_buffer_from_user(app, SCu8(query)));
}
CUSTOM_UI_COMMAND_SIG(lily_interactive_switch_modified_buffer)
CUSTOM_DOC("Interactively switch to a modified buffer.")
{
Buffer_ID buffer = sloth_get_buffer_from_user(app, "Switch:");
if (buffer != 0){
View_ID view = get_this_ctx_view(app, Access_Always);
view_set_buffer(app, view, buffer, 0);
}
}
CUSTOM_COMMAND_SIG(lily_full_path_to_clipboard)
CUSTOM_DOC("Copies the full path of this buffer to the clipboard; prints full file-path to qol bot text")
{
Scratch_Block scratch(app);
View_ID view = get_active_view(app, Access_ReadVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadVisible);
String_Const_u8 path = push_buffer_file_name(app, scratch, buffer);
if (path.size > 0) {
qol_bot_text_set(path);
i32 clip_idx = 0;
clipboard_post(app, clip_idx, path);
printf_message(app, "Copied file path to clipboard: %S\n", path);
} else {
printf_message(app, "No file path found for this buffer\n");
}
}
CUSTOM_COMMAND_SIG(lily_create_scratch_code_buffer)
CUSTOM_DOC("Creates a scratch code buffer with syntax highlighting")
{
Scratch_Block scratch(app);
b32 treat_as_code = true;
b32 use_lexer = true;
Buffer_ID buffer_id = create_buffer(app, string_u8_litexpr("*scratch.c*"),
BufferCreate_NeverAttachToFile | BufferCreate_AlwaysNew);
buffer_set_setting(app, buffer_id, BufferSetting_Unimportant, true);
buffer_set_setting(app, buffer_id, BufferSetting_Unkillable, true);
buffer_set_setting(app, buffer_id, BufferSetting_ReadOnly, false);
//String_ID file_map_id = vars_save_string_lit("keys_file");
String_ID code_map_id = vars_save_string_lit("keys_code");
// create a buffer and set the command map attachement
Command_Map_ID map_id = code_map_id; // (treat_as_code)?(code_map_id):(file_map_id);
Managed_Scope scope = buffer_get_managed_scope(app, buffer_id);
Command_Map_ID *map_id_ptr = scope_attachment(app, scope, buffer_map_id, Command_Map_ID);
*map_id_ptr = map_id;
bool wrap_lines = def_get_config_b32(vars_save_string_lit("enable_code_wrapping"));
String_Const_u8 buffer_name = push_buffer_base_name(app, scratch, buffer_id);
if (buffer_name.size > 0 && buffer_name.str[0] == '*' && buffer_name.str[buffer_name.size - 1] == '*'){
wrap_lines = def_get_config_b32(vars_save_string_lit("enable_output_wrapping"));
}
if (use_lexer){
Async_Task *lex_task_ptr = scope_attachment(app, scope, buffer_lex_task, Async_Task);
*lex_task_ptr = async_task_no_dep(&global_async_system, do_full_lex_async, make_data_struct(&buffer_id));
}
b32 *wrap_lines_ptr = scope_attachment(app, scope, buffer_wrap_lines, b32);
*wrap_lines_ptr = wrap_lines;
if (use_lexer) {
buffer_set_layout(app, buffer_id, layout_virt_indent_index_generic);
} else {
if (treat_as_code){
buffer_set_layout(app, buffer_id, layout_virt_indent_literal_generic);
} else {
buffer_set_layout(app, buffer_id, layout_generic);
}
}
// set the other view to show the buffer
View_ID view = get_active_view(app, Access_Always);
i64 pos = view_get_cursor_pos(app, view);
change_active_panel(app);
view = get_active_view(app, Access_Always);
view_set_buffer(app, view, buffer_id, SetBuffer_KeepOriginalGUI);
view_set_cursor_and_preferred_x(app, view, seek_pos(pos));
}
CUSTOM_COMMAND_SIG(f4_remedy_open_cursor)
CUSTOM_DOC("Opens the active panel's file in an actively-running RemedyBG instance, and moves to the cursor's line position.")
{
Scratch_Block scratch(app);
View_ID view = get_active_view(app, Access_Read);
Buffer_ID buffer = view_get_buffer(app, view, Access_Read);
String8 buffer_name = push_buffer_file_name(app, scratch, buffer);
i64 pos = view_get_cursor_pos(app, view);
i64 line = get_line_number_from_pos(app, buffer, pos);
String8 hot_directory = push_hot_directory(app, scratch);
Child_Process_ID child_id = create_child_process(app, hot_directory, push_stringf(scratch, "remedybg.exe open-file %.*s %i", string_expand(buffer_name), (int)line));
(void)child_id;
}
function void
sloth_command_trigger_stringize_mods(Arena *arena, List_String_Const_u8 *list, Input_Modifier_Set *modifiers){
for (i32 i = 0; i < modifiers->count; ++i){
if (i > 0) string_list_pushf(arena, list, ", ");
string_list_pushf(arena, list, "KeyCode_%s", key_mod_name(modifiers->mods[i]));
}
}
function void
sloth_command_trigger_stringize(Arena *arena, List_String_Const_u8 *list, Command_Trigger *trigger){
switch (trigger->kind){
case InputEventKind_TextInsert:
{
string_list_push(arena, list, string_u8_litexpr("TextInsert"));
}break;
case InputEventKind_KeyStroke:
{
string_list_pushf(arena, list, "KeyCode_%S", SCu8(ArraySafe(key_code_name, trigger->sub_code)));
if (trigger->mods.count) string_list_push(arena, list, str8_lit(", "));
sloth_command_trigger_stringize_mods(arena, list, &trigger->mods);
string_list_push(arena, list, str8_lit(""));
}break;
case InputEventKind_KeyRelease:
{
sloth_command_trigger_stringize_mods(arena, list, &trigger->mods);
string_list_pushf(arena, list, "Release %s", ArraySafe(key_code_name, trigger->sub_code));
}break;
case InputEventKind_MouseButton:
{
sloth_command_trigger_stringize_mods(arena, list, &trigger->mods);
string_list_pushf(arena, list, "Mouse %s", ArraySafe(mouse_code_name, trigger->sub_code));
}break;
case InputEventKind_MouseButtonRelease:
{
sloth_command_trigger_stringize_mods(arena, list, &trigger->mods);
string_list_pushf(arena, list, "Release Mouse %s", ArraySafe(mouse_code_name, trigger->sub_code));
}break;
case InputEventKind_MouseWheel:
{
sloth_command_trigger_stringize_mods(arena, list, &trigger->mods);
string_list_push(arena, list, string_u8_litexpr("MouseWheel"));
}break;
case InputEventKind_MouseMove:
{
sloth_command_trigger_stringize_mods(arena, list, &trigger->mods);
string_list_push(arena, list, string_u8_litexpr("MouseMove"));
}break;
case InputEventKind_Core:
{
string_list_pushf(arena, list, "Core %s", ArraySafe(core_code_name, trigger->sub_code));
}break;
default:
{
string_list_push(arena, list, string_u8_litexpr("ERROR unexpected trigger kind"));
}break;
}
}
function void
sloth_lister_fill_maybe_bound_commands(Application_Links *app, Lister *lister, Command_Lister_Status_Rule *status_rule, bool bound) {
for (i32 i = 0; i < command_one_past_last_id; i += 1) {
Custom_Command_Function *proc = fcoder_metacmd_table[i].proc;
String_Const_u8 status = {0};
switch (status_rule->mode) {
case CommandLister_Descriptions: {
status = SCu8(fcoder_metacmd_table[i].description);
break;
}
case CommandLister_Bindings: {
Command_Trigger_List triggers = map_get_triggers_recursive(lister->arena, status_rule->mapping, status_rule->map_id, proc);
List_String_Const_u8 list = {};
for (Command_Trigger *node = triggers.first; node != 0; node = node->next) {
command_trigger_stringize(lister->arena, &list, node);
if (node->next != 0){
string_list_push(lister->arena, &list, string_u8_litexpr(" "));
}
}
status = string_list_flatten(lister->arena, list);
break;
}
}
if (bound && status.size != 0) {
lister_add_item(lister, SCu8(fcoder_metacmd_table[i].name), status, (void*)proc, 0);
// NOTE(sloth): spam the logs
// String_Const_u8 message = push_stringf(lister->arena, "// %S\n{ \"%S\", %S },\n", SCu8(fcoder_metacmd_table[i].description), SCu8(fcoder_metacmd_table[i].name), status);
String_Const_u8 message = push_stringf(lister->arena, "Bind(%S, %S);\n",SCu8(fcoder_metacmd_table[i].name), status);
print_message(app, message);
}
if (!bound && status.size == 0) {
lister_add_item(lister, SCu8(fcoder_metacmd_table[i].name), status, (void*)proc, 0);
// NOTE(sloth): spam the logs
String_Const_u8 message = push_stringf(lister->arena, "// %S\n{ \"%S\", },\n", SCu8(fcoder_metacmd_table[i].description), SCu8(fcoder_metacmd_table[i].name));
print_message(app, message);
}
}
}
function Custom_Command_Function*
sloth_get_command_from_user(Application_Links *app, String_Const_u8 query, Command_Lister_Status_Rule *status_rule, bool bound){
Scratch_Block scratch(app);
Lister_Block lister(app, scratch);
lister_set_query(lister, query);
lister_set_default_handlers(lister);
sloth_lister_fill_maybe_bound_commands(app, lister, status_rule, bound);
Custom_Command_Function *result = 0;
if (lister.lister.current->options.count) {
Lister_Result l_result = run_lister(app, lister);
if (!l_result.canceled){
result = (Custom_Command_Function*)l_result.user_data;
}
} else {
String_Const_u8 message = push_stringf(scratch, "%s", "No commands found\n");
qol_bot_text_set(message);
print_message(app, message);
}
return(result);
}
CUSTOM_UI_COMMAND_SIG(lily_unbound_command_lister)
CUSTOM_DOC("Opens an interactive list of all unbound, registered commands.")
{
View_ID view = get_this_ctx_view(app, Access_Always);
if (view != 0){
Command_Lister_Status_Rule rule = {};
Buffer_ID buffer = view_get_buffer(app, view, Access_Visible);
Managed_Scope buffer_scope = buffer_get_managed_scope(app, buffer);
Command_Map_ID *map_id_ptr = scope_attachment(app, buffer_scope, buffer_map_id, Command_Map_ID);
if (map_id_ptr != 0) {
rule = command_lister_status_bindings(&framework_mapping, *map_id_ptr);
} else {
rule = command_lister_status_descriptions();
}
Custom_Command_Function *func = sloth_get_command_from_user(app, string_u8_litexpr("Command:"), &rule, false);
if (func != 0) {
view_enqueue_command_function(app, view, func);
}
}
}
CUSTOM_UI_COMMAND_SIG(lily_bound_command_lister)
CUSTOM_DOC("Opens an interactive list of all bound, registered commands.")
{
View_ID view = get_this_ctx_view(app, Access_Always);
if (view != 0){
Command_Lister_Status_Rule rule = {};
Buffer_ID buffer = view_get_buffer(app, view, Access_Visible);
Managed_Scope buffer_scope = buffer_get_managed_scope(app, buffer);
Command_Map_ID *map_id_ptr = scope_attachment(app, buffer_scope, buffer_map_id, Command_Map_ID);
if (map_id_ptr != 0) {
rule = command_lister_status_bindings(&framework_mapping, *map_id_ptr);
} else {
rule = command_lister_status_descriptions();
}
Custom_Command_Function *func = sloth_get_command_from_user(app, string_u8_litexpr("Command:"), &rule, true);
if (func != 0) {