This repository has been archived by the owner on Oct 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbim.c
5999 lines (5365 loc) · 146 KB
/
bim.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vim: tabstop=4 shiftwidth=4 noexpandtab
*
* Copyright (C) 2012-2018 K. Lange
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* bim - Text editor
*
* Bim is inspired by vim, and its name is short for "Bad IMitation".
*
* Bim supports syntax highlighting, extensive editing, line selection
* and copy-paste, undo/redo stack, forward and backward search, and can
* be built for ToaruOS, Sortix, Linux, macOS, and BSDs.
*/
#define _XOPEN_SOURCE
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <unistd.h>
#include <termios.h>
#include <signal.h>
#include <locale.h>
#include <wchar.h>
#include <ctype.h>
#include <dirent.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#define BIM_VERSION "1.0.2"
#define BIM_COPYRIGHT "Copyright 2013-2018 K. Lange <\033[[email protected]\033[23m>"
#define BLOCK_SIZE 4096
#define ENTER_KEY '\n'
#define BACKSPACE_KEY 0x08
#define DELETE_KEY 0x7F
/**
* Theming data
*
* This is all overridden by a load_colorscheme_ method.
* The default is to load_colorscheme_ansi, but config
* files can be used to set a different default theme.
*/
const char * COLOR_FG = "@17";
const char * COLOR_BG = "@0";
const char * COLOR_ALT_FG = "@17";
const char * COLOR_ALT_BG = "@0";
const char * COLOR_NUMBER_FG = "@17";
const char * COLOR_NUMBER_BG = "@0";
const char * COLOR_STATUS_FG = "@17";
const char * COLOR_STATUS_BG = "@0";
const char * COLOR_TABBAR_BG = "@0";
const char * COLOR_TAB_BG = "@0";
const char * COLOR_ERROR_FG = "@17";
const char * COLOR_ERROR_BG = "@0";
const char * COLOR_SEARCH_FG = "@17";
const char * COLOR_SEARCH_BG = "@0";
const char * COLOR_KEYWORD = "@17";
const char * COLOR_STRING = "@17";
const char * COLOR_COMMENT = "@17";
const char * COLOR_TYPE = "@17";
const char * COLOR_PRAGMA = "@17";
const char * COLOR_NUMERAL = "@17";
const char * COLOR_SELECTFG = "@0";
const char * COLOR_SELECTBG = "@17";
const char * COLOR_RED = "@1";
const char * COLOR_GREEN = "@2";
const char * current_theme = "none";
/**
* Syntax highlighting flags.
*/
#define FLAG_NONE 0
#define FLAG_KEYWORD 1
#define FLAG_STRING 2
#define FLAG_COMMENT 3
#define FLAG_TYPE 4
#define FLAG_PRAGMA 5
#define FLAG_NUMERAL 6
#define FLAG_SELECT 7
#define FLAG_STRING2 8
#define FLAG_DIFFPLUS 9
#define FLAG_DIFFMINUS 10
#define FLAG_CONTINUES (1 << 6)
/**
* Convert syntax hilighting flag to color code
*/
const char * flag_to_color(int _flag) {
int flag = _flag & 0x3F;
switch (flag) {
case FLAG_KEYWORD:
return COLOR_KEYWORD;
case FLAG_STRING:
case FLAG_STRING2: /* allows python to differentiate " and ' */
return COLOR_STRING;
case FLAG_COMMENT:
return COLOR_COMMENT;
case FLAG_TYPE:
return COLOR_TYPE;
case FLAG_NUMERAL:
return COLOR_NUMERAL;
case FLAG_PRAGMA:
return COLOR_PRAGMA;
case FLAG_DIFFPLUS:
return COLOR_GREEN;
case FLAG_DIFFMINUS:
return COLOR_RED;
case FLAG_SELECT:
return COLOR_FG;
default:
return COLOR_FG;
}
}
/**
* Line buffer definitions
*
* Lines are essentially resizable vectors of char_t structs,
* which represent single codepoints in the file.
*/
typedef struct {
uint32_t display_width:4;
uint32_t flags:7;
uint32_t codepoint:21;
} __attribute__((packed)) char_t;
/**
* Lines have available and actual lengths, describing
* how much space was allocated vs. how much is being
* used at the moment.
*/
typedef struct {
int available;
int actual;
int istate;
char_t text[0];
} line_t;
/**
* Global configuration state
*/
struct {
/* Terminal size */
int term_width, term_height;
int bottom_size;
/* Command-line parameters */
int hilight_on_open;
int initial_file_is_read_only;
line_t ** yanks;
size_t yank_count;
int yank_is_full_lines;
int tty_in;
const char * bimrc_path;
int can_scroll;
int can_hideshow;
int can_altscreen;
int can_mouse;
int can_unicode;
int can_bright;
int can_title;
int can_bce;
int history_enabled;
int cursor_padding;
} global_config = {
0, /* term_width */
0, /* term_height */
2, /* bottom_size */
1, /* hilight_on_open */
0, /* initial_file_is_read_only */
NULL, /* yanks */
0, /* yank_count */
0,
STDIN_FILENO, /* tty_in */
"~/.bimrc", /* bimrc_path */
1,
1,
1,
1,
1,
1,
1,
1,
1,
4, /* cursor padding */
};
void redraw_line(int j, int x);
/**
* Special implementation of getch with a timeout
*/
int _bim_unget = -1;
void bim_unget(int c) {
_bim_unget = c;
}
#define bim_getch() bim_getch_timeout(200)
int bim_getch_timeout(int timeout) {
if (_bim_unget != -1) {
int out = _bim_unget;
_bim_unget = -1;
return out;
}
struct pollfd fds[1];
fds[0].fd = global_config.tty_in;
fds[0].events = POLLIN;
int ret = poll(fds,1,timeout);
if (ret > 0 && fds[0].revents & POLLIN) {
unsigned char buf[1];
read(global_config.tty_in, buf, 1);
return buf[0];
} else {
return -1;
}
}
#define HISTORY_SENTINEL 0
#define HISTORY_INSERT 1
#define HISTORY_DELETE 2
#define HISTORY_REPLACE 3
#define HISTORY_REMOVE_LINE 4
#define HISTORY_ADD_LINE 5
#define HISTORY_REPLACE_LINE 6
#define HISTORY_MERGE_LINES 7
#define HISTORY_SPLIT_LINE 8
#define HISTORY_BREAK 10
typedef struct history {
struct history * previous;
struct history * next;
int type;
union {
struct {
int lineno;
int offset;
int codepoint;
int old_codepoint;
} insert_delete_replace;
struct {
int lineno;
line_t * contents;
line_t * old_contents;
} remove_replace_line;
struct {
int lineno;
int split;
} add_merge_split_lines;
};
} history_t;
/**
* Buffer data
*
* A buffer describes a file, and stores
* its name as well as the editor state
* (cursor offsets, etc.) and the actual
* line buffers.
*/
typedef struct _env {
short loading:1;
short tabs:1;
short modified:1;
short readonly:1;
short indent:1;
short mode;
short tabstop;
char * file_name;
int offset;
int coffset;
int line_no;
int line_count;
int line_avail;
int col_no;
uint32_t * search;
struct syntax_definition * syntax;
line_t ** lines;
history_t * history;
history_t * last_save_history;
} buffer_t;
/**
* Pointer to current active buffer
*/
buffer_t * env;
/**
* Editor modes (like in vim)
*/
#define MODE_NORMAL 0
#define MODE_INSERT 1
#define MODE_LINE_SELECTION 2
#define MODE_REPLACE 3
#define MODE_CHAR_SELECTION 4
/**
* Available buffers
*/
int buffers_len;
int buffers_avail;
buffer_t ** buffers;
/**
* Create a new buffer
*/
buffer_t * buffer_new(void) {
if (buffers_len == buffers_avail) {
/* If we are out of buffer space, expand the buffers vector */
buffers_avail *= 2;
buffers = realloc(buffers, sizeof(buffer_t *) * buffers_avail);
}
/* Allocate a new buffer */
buffers[buffers_len] = malloc(sizeof(buffer_t));
memset(buffers[buffers_len], 0x00, sizeof(buffer_t));
buffers_len++;
return buffers[buffers_len-1];
}
/**
* Close a buffer
*/
buffer_t * buffer_close(buffer_t * buf) {
int i;
/* Locate the buffer in the buffer pointer vector */
for (i = 0; i < buffers_len; i++) {
if (buf == buffers[i])
break;
}
/* Invalid buffer? */
if (i == buffers_len) {
return env; /* wtf */
}
/* Remove the buffer from the vector, moving others up */
if (i != buffers_len - 1) {
memmove(&buffers[i], &buffers[i+1], sizeof(*buffers) * (buffers_len - i));
}
/* There is one less buffer */
buffers_len--;
if (!buffers_len) {
/* There are no more buffers. */
return NULL;
}
/* If this was the last buffer, return the previous last buffer */
if (i == buffers_len) {
return buffers[buffers_len-1];
}
/* Otherwise return the new last buffer */
return buffers[i];
}
/**
* Themes
*/
/* Based on the wombat256 theme for vim */
void load_colorscheme_wombat(void) {
COLOR_FG = "5;230";
COLOR_BG = "5;235";
COLOR_ALT_FG = "5;244";
COLOR_ALT_BG = "5;236";
COLOR_NUMBER_BG = "5;232";
COLOR_NUMBER_FG = "5;101";
COLOR_STATUS_FG = "5;230";
COLOR_STATUS_BG = "5;238";
COLOR_TABBAR_BG = "5;230";
COLOR_TAB_BG = "5;248";
COLOR_KEYWORD = "5;117";
COLOR_STRING = "5;113";
COLOR_COMMENT = "5;102;3";
COLOR_TYPE = "5;185";
COLOR_PRAGMA = "5;173";
COLOR_NUMERAL = COLOR_PRAGMA;
COLOR_ERROR_FG = "5;15";
COLOR_ERROR_BG = "5;196";
COLOR_SEARCH_FG = "5;234";
COLOR_SEARCH_BG = "5;226";
COLOR_SELECTFG = "5;235";
COLOR_SELECTBG = "5;230";
COLOR_RED = "@1";
COLOR_GREEN = "@2";
current_theme = "wombat";
}
/* "City Lights" based on citylights.xyz */
void load_colorscheme_citylights(void) {
COLOR_FG = "2;151;178;198";
COLOR_BG = "2;29;37;44";
COLOR_ALT_FG = "2;45;55;65";
COLOR_ALT_BG = "2;33;42;50";
COLOR_NUMBER_FG = "2;71;89;103";
COLOR_NUMBER_BG = "2;37;47;56";
COLOR_STATUS_FG = "2;116;144;166";
COLOR_STATUS_BG = "2;53;67;78";
COLOR_TABBAR_BG = "2;37;47;56";
COLOR_TAB_BG = "2;29;37;44";
COLOR_KEYWORD = "2;94;196;255";
COLOR_STRING = "2;83;154;252";
COLOR_COMMENT = "2;107;133;153;3";
COLOR_TYPE = "2;139;212;156";
COLOR_PRAGMA = "2;0;139;148";
COLOR_NUMERAL = "2;207;118;132";
COLOR_ERROR_FG = "5;15";
COLOR_ERROR_BG = "5;196";
COLOR_SEARCH_FG = "5;234";
COLOR_SEARCH_BG = "5;226";
COLOR_SELECTFG = "2;29;37;44";
COLOR_SELECTBG = "2;151;178;198";
COLOR_RED = "2;222;53;53";
COLOR_GREEN = "2;55;167;0";
current_theme = "citylights";
}
/* Solarized Dark, popular theme */
void load_colorscheme_solarized_dark(void) {
COLOR_FG = "2;147;161;161";
COLOR_BG = "2;0;43;54";
COLOR_ALT_FG = "2;147;161;161";
COLOR_ALT_BG = "2;7;54;66";
COLOR_NUMBER_FG = "2;131;148;149";
COLOR_NUMBER_BG = "2;7;54;66";
COLOR_STATUS_FG = "2;131;148;150";
COLOR_STATUS_BG = "2;7;54;66";
COLOR_TABBAR_BG = "2;7;54;66";
COLOR_TAB_BG = "2;131;148;150";
COLOR_KEYWORD = "2;133;153;0";
COLOR_STRING = "2;42;161;152";
COLOR_COMMENT = "2;101;123;131";
COLOR_TYPE = "2;181;137;0";
COLOR_PRAGMA = "2;203;75;22";
COLOR_NUMERAL = "2;220;50;47";
COLOR_ERROR_FG = "5;15";
COLOR_ERROR_BG = "5;196";
COLOR_SEARCH_FG = "5;234";
COLOR_SEARCH_BG = "5;226";
COLOR_SELECTFG = "2;0;43;54";
COLOR_SELECTBG = "2;147;161;161";
COLOR_RED = "2;222;53;53";
COLOR_GREEN = "2;55;167;0";
current_theme = "solarized-dark";
}
/* Custom theme */
void load_colorscheme_sunsmoke(void) {
COLOR_FG = "2;230;230;230";
COLOR_BG = "2;31;31;31";
COLOR_ALT_FG = "2;122;122;122";
COLOR_ALT_BG = "2;46;43;46";
COLOR_NUMBER_FG = "2;150;139;57";
COLOR_NUMBER_BG = "2;0;0;0";
COLOR_STATUS_FG = "2;230;230;230";
COLOR_STATUS_BG = "2;71;64;58";
COLOR_TABBAR_BG = "2;71;64;58";
COLOR_TAB_BG = "2;71;64;58";
COLOR_KEYWORD = "2;51;162;230";
COLOR_STRING = "2;72;176;72";
COLOR_COMMENT = "2;158;153;129;3";
COLOR_TYPE = "2;230;206;110";
COLOR_PRAGMA = "2;194;70;54";
COLOR_NUMERAL = "2;230;43;127";
COLOR_ERROR_FG = "5;15";
COLOR_ERROR_BG = "5;196";
COLOR_SEARCH_FG = "5;234";
COLOR_SEARCH_BG = "5;226";
COLOR_SELECTFG = "2;0;43;54";
COLOR_SELECTBG = "2;147;161;161";
COLOR_RED = "2;222;53;53";
COLOR_GREEN = "2;55;167;0";
current_theme = "sunsmoke";
}
/* 16-color theme, default */
void load_colorscheme_ansi(void) {
COLOR_FG = global_config.can_bright ? "@17" : "@7";
COLOR_BG = global_config.can_bright ? "@9" : "@0";
COLOR_ALT_FG = global_config.can_bright ? "@10" : "@5";
COLOR_ALT_BG = "@9";
COLOR_NUMBER_FG = "@3";
COLOR_NUMBER_BG = "@9";
COLOR_STATUS_FG = global_config.can_bright ? "@17" : "@7";
COLOR_STATUS_BG = "@4";
COLOR_TABBAR_BG = "@4";
COLOR_TAB_BG = "@4";
COLOR_KEYWORD = global_config.can_bright ? "@14" : "@4";
COLOR_STRING = "@2";
COLOR_COMMENT = global_config.can_bright ? "@10" : "@5";
COLOR_TYPE = "@3";
COLOR_PRAGMA = "@1";
COLOR_NUMERAL = "@1";
COLOR_ERROR_FG = global_config.can_bright ? "@17" : "@7";
COLOR_ERROR_BG = "@1";
COLOR_SEARCH_FG = "@0";
COLOR_SEARCH_BG = global_config.can_bright ? "@13" : "@3";
COLOR_SELECTBG = global_config.can_bright ? "@17" : "@7";
COLOR_SELECTFG = "@0";
COLOR_RED = "@1";
COLOR_GREEN = "@2";
current_theme = "ansi";
}
struct theme_def {
const char * name;
void (*load)(void);
} themes[] = {
{"wombat", load_colorscheme_wombat},
{"citylights", load_colorscheme_citylights},
{"solarized-dark", load_colorscheme_solarized_dark},
{"ansi", load_colorscheme_ansi},
{"sunsmoke", load_colorscheme_sunsmoke},
{NULL, NULL}
};
/**
* Syntax definition for C
*/
int syn_c_iskeywordchar(int c) {
if (isalnum(c)) return 1;
if (c == '_') return 1;
return 0;
}
static char * syn_c_keywords[] = {
"while","if","for","continue","return","break","switch","case","sizeof",
"struct","union","typedef","do","default","else","goto",
"alignas","alignof","offsetof",
/* C++ stuff */
"public","private","class","using","namespace",
NULL
};
static char * syn_c_types[] = {
"static","int","char","short","float","double","void","unsigned","volatile","const",
"register","long","inline","restrict","enum","auto","extern","bool","complex",
"uint8_t","uint16_t","uint32_t","uint64_t",
"int8_t","int16_t","int32_t","int64_t","FILE",
NULL
};
static char * syn_c_special[] = {
"NULL",
"stdin","stdout","stderr",
"STDIN_FILENO","STDOUT_FILENO","STDERR_FILENO",
NULL
};
static int syn_c_extended(line_t * line, int i, int c, int last, int * out_left) {
if (i == 0 && c == '#') {
*out_left = line->actual+1;
if (line->text[line->actual-1].codepoint == '\\') {
return FLAG_PRAGMA | FLAG_CONTINUES;
}
return FLAG_PRAGMA;
}
if ((!last || !syn_c_iskeywordchar(last)) && syn_c_iskeywordchar(c)) {
int j = i;
for (int s = 0; syn_c_special[s]; ++s) {
int d = 0;
while (j + d < line->actual && line->text[j+d].codepoint == syn_c_special[s][d]) d++;
if (syn_c_special[s][d] == '\0' && (j+d > line->actual || !syn_c_iskeywordchar(line->text[j+d].codepoint))) {
*out_left = d-1;
return FLAG_NUMERAL;
}
}
}
if ((!last || !syn_c_iskeywordchar(last)) && isdigit(c)) {
if (c == '0' && i < line->actual - 1 && line->text[i+1].codepoint == 'x') {
int j = 2;
for (; i + j < line->actual && isxdigit(line->text[i+j].codepoint); ++j);
if (i + j < line->actual && syn_c_iskeywordchar(line->text[i+j].codepoint)) {
return FLAG_NONE;
}
*out_left = j - 1;
return FLAG_NUMERAL;
} else {
int j = 1;
while (i + j < line->actual && isdigit(line->text[i+j].codepoint)) {
j++;
}
if (i + j < line->actual && syn_c_iskeywordchar(line->text[i+j].codepoint)) {
return FLAG_NONE;
}
*out_left = j - 1;
return FLAG_NUMERAL;
}
}
if (c == '/') {
if (i < line->actual - 1 && line->text[i+1].codepoint == '/') {
*out_left = (line->actual + 1) - i;
return FLAG_COMMENT;
}
if (i < line->actual - 1 && line->text[i+1].codepoint == '*') {
int last = 0;
for (int j = i + 2; j < line->actual; ++j) {
int c = line->text[j].codepoint;
if (c == '/' && last == '*') {
*out_left = j - i;
return FLAG_COMMENT;
}
last = c;
}
/* TODO multiline - update next */
*out_left = (line->actual + 1) - i;
return FLAG_COMMENT | FLAG_CONTINUES;
}
}
if (c == '\'') {
if (i < line->actual - 3 && line->text[i+1].codepoint == '\\' &&
line->text[i+3].codepoint == '\'') {
*out_left = 3;
return FLAG_NUMERAL;
}
if (i < line->actual - 2 && line->text[i+2].codepoint == '\'') {
*out_left = 2;
return FLAG_NUMERAL;
}
}
if (c == '"') {
int last = 0;
for (int j = i+1; j < line->actual; ++j) {
int c = line->text[j].codepoint;
if (last != '\\' && c == '"') {
*out_left = j - i;
return FLAG_STRING;
}
if (last == '\\' && c == '\\') {
last = 0;
}
last = c;
}
*out_left = (line->actual + 1) - i; /* unterminated string */
return FLAG_STRING;
}
return 0;
}
char * syn_c_ext[] = {".c",".h",".cpp",".hpp",".c++",".h++",NULL};
static int syn_c_finish(line_t * line, int * left, int state) {
if (state == (FLAG_COMMENT | FLAG_CONTINUES)) {
int last = 0;
for (int i = 0; i < line->actual; ++i) {
if (line->text[i].codepoint == '/' && last == '*') {
*left = i+2;
return FLAG_COMMENT;
}
last = line->text[i].codepoint;
}
return FLAG_COMMENT | FLAG_CONTINUES;
}
if (state == (FLAG_PRAGMA | FLAG_CONTINUES)) {
*left = line->actual + 1;
if (line->text[line->actual-1].codepoint == '\\') {
return FLAG_PRAGMA | FLAG_CONTINUES;
}
return FLAG_PRAGMA;
}
return 0;
}
/**
* Syntax definition for Python
*/
static char * syn_py_keywords[] = {
"class","def","return","del","if","else","elif",
"for","while","continue","break","assert",
"as","and","or","except","finally","from",
"global","import","in","is","lambda","with",
"nonlocal","not","pass","raise","try","yield",
NULL
};
static char * syn_py_types[] = {
"True","False","None",
"object","set","dict","int","str","bytes",
NULL
};
static int syn_py_extended(line_t * line, int i, int c, int last, int * out_left) {
if (i == 0 && c == 'i') {
/* Check for import */
char * import = "import ";
for (int j = 0; j < line->actual + 1; ++j) {
if (import[j] == '\0') {
*out_left = j - 2;
return FLAG_PRAGMA;
}
if (line->text[j].codepoint != import[j]) break;
}
}
if (c == '#') {
*out_left = (line->actual + 1) - i;
return FLAG_COMMENT;
}
if (c == '@') {
for (int j = i+1; j < line->actual + 1; ++j) {
if (!syn_c_iskeywordchar(line->text[j].codepoint)) {
*out_left = j - i - 1;
return FLAG_PRAGMA;
}
*out_left = (line->actual + 1) - i;
return FLAG_PRAGMA;
}
}
if ((!last || !syn_c_iskeywordchar(last)) && isdigit(c)) {
if (c == '0' && i < line->actual - 1 && line->text[i+1].codepoint == 'x') {
int j = 2;
for (; i + j < line->actual && isxdigit(line->text[i+j].codepoint); ++j);
if (i + j < line->actual && syn_c_iskeywordchar(line->text[i+j].codepoint)) {
return FLAG_NONE;
}
*out_left = j - 1;
return FLAG_NUMERAL;
} else {
int j = 1;
while (i + j < line->actual && isdigit(line->text[i+j].codepoint)) {
j++;
}
if (i + j < line->actual && syn_c_iskeywordchar(line->text[i+j].codepoint)) {
return FLAG_NONE;
}
*out_left = j - 1;
return FLAG_NUMERAL;
}
}
if (line->text[i].codepoint == '\'') {
if (i + 2 < line->actual && line->text[i+1].codepoint == '\'' && line->text[i+2].codepoint == '\'') {
/* Begin multiline */
for (int j = i + 3; j < line->actual - 2; ++j) {
if (line->text[j].codepoint == '\'' &&
line->text[j+1].codepoint == '\'' &&
line->text[j+2].codepoint == '\'') {
*out_left = (j+2) - i;
return FLAG_STRING;
}
}
return FLAG_STRING | FLAG_CONTINUES;
}
int last = 0;
for (int j = i+1; j < line->actual; ++j) {
int c = line->text[j].codepoint;
if (last != '\\' && c == '\'') {
*out_left = j - i;
return FLAG_STRING;
}
if (last == '\\' && c == '\\') {
last = 0;
}
last = c;
}
*out_left = (line->actual + 1) - i; /* unterminated string */
return FLAG_STRING;
}
if (line->text[i].codepoint == '"') {
if (i + 2 < line->actual && line->text[i+1].codepoint == '"' && line->text[i+2].codepoint == '"') {
/* Begin multiline */
for (int j = i + 3; j < line->actual - 2; ++j) {
if (line->text[j].codepoint == '"' &&
line->text[j+1].codepoint == '"' &&
line->text[j+2].codepoint == '"') {
*out_left = (j+2) - i;
return FLAG_STRING;
}
}
return FLAG_STRING2 | FLAG_CONTINUES;
}
int last = 0;
for (int j = i+1; j < line->actual; ++j) {
int c = line->text[j].codepoint;
if (last != '\\' && c == '"') {
*out_left = j - i;
return FLAG_STRING;
}
if (last == '\\' && c == '\\') {
last = 0;
}
last = c;
}
*out_left = (line->actual + 1) - i; /* unterminated string */
return FLAG_STRING;
}
return 0;
}
static int syn_py_finish(line_t * line, int * left, int state) {
/* TODO support multiline quotes */
if (state == (FLAG_STRING | FLAG_CONTINUES)) {
for (int j = 0; j < line->actual - 2; ++j) {
if (line->text[j].codepoint == '\'' &&
line->text[j+1].codepoint == '\'' &&
line->text[j+2].codepoint == '\'') {
*left = (j+3);
return FLAG_STRING;
}
}
return FLAG_STRING | FLAG_CONTINUES;
}
if (state == (FLAG_STRING2 | FLAG_CONTINUES)) {
for (int j = 0; j < line->actual - 2; ++j) {
if (line->text[j].codepoint == '"' &&
line->text[j+1].codepoint == '"' &&
line->text[j+2].codepoint == '"') {
*left = (j+3);
return FLAG_STRING2;
}
}
return FLAG_STRING2 | FLAG_CONTINUES;
}
return 0;
}
char * syn_py_ext[] = {".py",NULL};
/**
* Syntax definition for ToaruOS shell
*/
static char * syn_sh_keywords[] = {
"cd","exit","export","help","history","if",
"empty?","equals?","return","export-cmd",
"source","exec","not","while","then","else",
NULL,
};
static int variable_char(uint8_t c) {
if (c >= 'A' && c <= 'Z') return 1;
if (c >= 'a' && c <= 'z') return 1;
if (c >= '0' && c <= '9') return 1;
if (c == '_') return 1;
if (c == '?') return 1;
return 0;
}
static int syn_sh_extended(line_t * line, int i, int c, int last, int * out_left) {
(void)last;
if (c == '#' && last != '\\') {
*out_left = (line->actual + 1) - i;
return FLAG_COMMENT;
}
if (line->text[i].codepoint == '\'' && last != '\\') {
int last = 0;
for (int j = i+1; j < line->actual + 1; ++j) {
int c = line->text[j].codepoint;
if (last != '\\' && c == '\'') {
*out_left = j - i;
return FLAG_STRING;
}
if (last == '\\' && c == '\\') {
last = 0;
}
last = c;
}
*out_left = (line->actual + 1) - i; /* unterminated string */
return FLAG_STRING;
}
if (line->text[i].codepoint == '$' && last != '\\') {
if (i < line->actual - 1 && line->text[i+1].codepoint == '{') {
int j = i + 2;
for (; j < line->actual+1; ++j) {
if (line->text[j].codepoint == '}') break;
}
*out_left = (j - i);
return FLAG_NUMERAL;
}
int j = i + 1;
for (; j < line->actual + 1; ++j) {
if (!variable_char(line->text[j].codepoint)) break;
}
*out_left = (j - i) - 1;
return FLAG_NUMERAL;
}
if (line->text[i].codepoint == '"' && last != '\\') {
int last = 0;
for (int j = i+1; j < line->actual + 1; ++j) {
int c = line->text[j].codepoint;
if (last != '\\' && c == '"') {
*out_left = j - i;
return FLAG_STRING;
}
if (last == '\\' && c == '\\') {
last = 0;
}
last = c;
}
*out_left = (line->actual + 1) - i; /* unterminated string */
return FLAG_STRING;
}
return 0;
}
static int syn_sh_iskeywordchar(int c) {
if (isalnum(c)) return 1;
if (c == '-') return 1;
if (c == '_') return 1;
if (c == '?') return 1;
return 0;
}
static char * syn_sh_ext[] = {".sh",".eshrc",".esh",NULL};
static char * syn_make_ext[] = {"Makefile","makefile","GNUmakefile",".mak",NULL};
static char * syn_make_commands[] = {
"define","endef","undefine","ifdef","ifndef","ifeq","ifneq","else","endif",
"include","sinclude","override","export","unexport","private","vpath",
"-include",
NULL
};
static char * syn_make_functions[] = {
"subst","patsubst","findstring","filter","filter-out",
"sort","word","words","wordlist","firstword","lastword",
"dir","notdir","suffix","basename","addsuffix","addprefix",
"join","wildcard","realpath","abspath","error","warning",
"shell","origin","flavor","foreach","if","or","and",
"call","eval","file","value",
NULL
};
static int syn_make_extended(line_t * line, int i, int c, int last, int * out_left) {
(void)last;
if (c == '#') {
*out_left = (line->actual + 1) - i;