-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.c
executable file
·2406 lines (2226 loc) · 75.4 KB
/
edit.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
// See LICENSE file for copyright and license details.
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TB_IMPL
#include "termbox2.h"
#include "config.h"
#include "languages.h"
// macros
#define ERR_BUFFER_TOO_SMALL "A buffer was too small."
#define ERR_FILE_IO "File connection error occurred."
#define ERR_MALLOC "Memory allocation error occurred."
#define ERR_TERM_TOO_SMALL "Terminal was too small."
#define ERR_UTF8_ENCODING "UTF-8 error occurred."
#define ACC_LETTER ((char) 0xc3)
#define DEFAULT_BUF_SIZE INTERFACE_WIDTH
#define INTERFACE_MEM_LENGTH (4*INTERFACE_WIDTH + 1)
#if LINE_NUMBERS_WIDTH < 2
#define LINE_NUMBERS_MODULUS 1
#elif LINE_NUMBERS_WIDTH == 2
#define LINE_NUMBERS_MODULUS 10
#elif LINE_NUMBERS_WIDTH == 3
#define LINE_NUMBERS_MODULUS 100
#else
#define LINE_NUMBERS_MODULUS 1000
#endif // LINE_NUMBERS_WIDTH
#define MIN_HEIGHT 2
#define ABS(A) (((A) < 0) ? -(A) : (A))
#define CONSTRAIN(A, X, B) (((X) < (A)) ? (A) : (((X) > (B)) ? (B) : (X)))
#define DECLARE_BRACKETS(A, B) \
case A: associated_bracket = B; e = 1; break; \
case B: associated_bracket = A; e = -1; break;
#define DECLARE_PARAMETER(PARAMETER, NAME, TYPE, VAR) \
else if (sscanf(assign, NAME"=%"TYPE, &VAR) == 1) settings.PARAMETER = VAR;
#define MAX(A, B) (((A) > (B)) ? (A) : (B))
#define MIN(A, B) (((A) < (B)) ? (A) : (B))
#define MOVE_SEL_LIST(A, B) {forget_sel_list(B); (B) = (A); (A) = NULL;}
#define PARSE_LINE_IDENTIFIER(S, DEFAULT, OPERATOR, VAR) \
if (!strcmp(S, "")) *VAR = DEFAULT; \
else if (!strcmp(S, ".")) *VAR = first_line_nb + y; \
else if (sscanf(S, "%d", VAR) == 1) *VAR = (DEFAULT) ? OPERATOR(*VAR, DEFAULT) : 0; \
else return EXIT_FAILURE;
#define FILE_IO(A, E) if ((A) == (E)) die(EXIT_FAILURE, ERR_FILE_IO);
#define SET_SEL_LIST(A, B) {forget_sel_list(A); (A) = (B);}
#define SET_SUBSTRING(S, ST, MST, N, MN) \
{(S).st = (ST); (S).mst = (MST); (S).n = (N); (S).mn = (MN);}
#define SET_TO_ADD(S) {to_add = (S); (S) = (S)->next;}
#define SET_X(X) {x = (X); attribute_x = 1;}
#define echo(MESSAGE) strcpy(message, MESSAGE)
#define echof(PATTERN, INTEGER) sprintf(message, PATTERN, INTEGER)
#define first_line_nb (first_line_on_screen->line_nb)
#define is_type(TYPE, L) is_in(lang->TYPE, l->chars + k, L)
#define pos_of_cursor() pos_of(first_line_nb + y, x)
#define pos_of_sel(S) pos_of((S).l, (S).x)
#define way(DIRECT_CONDITION) ((DIRECT_CONDITION) ? m : -m)
// types
typedef char Interface[INTERFACE_MEM_LENGTH]; // interface for dialog mode
typedef struct Line { // doubly linked list of lines
struct Line *prev, *next;
int line_nb; // between 1 and nb_lines
int ml, dl; // length in memory, on screen
char *chars; // content (UTF-8, NULL-ended string)
} Line;
typedef struct { // position in file
int l, x; // line number, column
} Pos;
typedef struct Selection { // sorted list of non-overlapping selections
struct Selection *next;
int l, x, n; // line number, column, number of characters
} Selection;
typedef struct { // marks a substring in an original string
int st, mst; // starting position (characters, bytes)
int n, mn; // length (characters, bytes)
} Substring;
// functions declarations
static void act(void (*process)(Line *, Selection *), int line_op);
static void autocomplete(Line *l, Selection *s);
static void break_line(Line *l, Selection *s, int start);
static int column_sel(int m);
static void comment(Line *l, Selection *s);
static int compare_chars(const char *s1, int k1, const char *s2, int k2);
static Selection *compute_running_sel(void);
static void concatenate_line(Line *l, Selection *s);
static void copy_to_clip(int starting_line_nb, int nb);
static Line *create_line(int line_nb, int ml, int dl);
static Selection *create_sel(int l, int x, int n, Selection *next);
static void decrement(const char *chars, int *i, int *k, int goal);
static int dialog(const char *prompt, Interface interf, int refresh_sel);
static void die(int exit_status, const char *msg);
static int eat_pattern_atom(const char *sp, int *j, int *l);
static int eat_pattern_block(const char *sp, int *j, int *l, int *nb_subpatterns);
static int eat_pattern_character(const char *sp, int *j, int *l);
static void *emalloc(size_t size);
static int find_block_delim(int starting_line_nb, int nb);
static int find_first_non_blank(const Line *l);
static Pos find_matching_bracket(void);
static Pos find_next_selection(int delta);
static Pos find_start_of_word(int n);
static void forget_lines(Line *start);
static void forget_sel_list(Selection *a);
static Line *get_line(int delta_from_first_line_on_screen);
static Pos get_pos_of_sel(Selection *a, int index);
static int get_str_index(const char *chars, int x);
static void indent(Line *l, Selection *s);
static int index_closest_after_cursor(Selection *a);
static void init_termbox(void);
static void insert(Line *l, Selection *s);
static void insert_clip(int below);
static void insert_line(int line_nb, int ml, int dl);
static void insert_lines(Line *to_insert, int nb, int below);
static int is_in(const char *list, const char *chars, int length);
static int is_inf(Pos p1, Pos p2);
static int is_word_boundary(const char *chars, int k);
static int is_word_char(char c);
static void link_lines(Line *l1, Line *l2);
static void load_file(int first_line_on_screen_nb);
static void lower(Line *l, Selection *s);
static int mark_fields(const char *chars, int sx, int n);
static int mark_subpatterns(const char *sp, const char *chars, int dl, int ss, int sx, int n);
static Selection *merge_sel(Selection *a, Selection *b);
static int move(Line **l, int *dx, int e);
static void move_line(int delta);
static void move_sel_end_of_line(Selection *a, int l, int dl, int e);
static void move_to_clip(int starting_line_nb, int nb);
static void move_to_cursor(void);
static int nb_sel(Selection *a);
static int parse_assign(const char *assign);
static int parse_file(Line **dest, Line *next, const char *file_name, int min, int max, int starting);
static int parse_lang(const char *file_name);
static int parse_range(const char *range, int *l1, int *l2, int min, int max);
static int parse_repeater(const char *sp, int *j, int *l, int *min, int *max);
static Pos pos_of(int l, int x);
static void print_all(void);
static void print_dialog_ruler(void);
static Selection *print_line(const Line *l, Selection *s, int screen_line);
static Selection *range_lines_sel(int start, int end, Selection *next);
static void remove_sel_line_range(int min, int max);
static void reorder_sel(int l, int nb, int new_l);
static void replace(Line *l, Selection *s);
static int replace_chars(Line *l, Selection *a, int start, int n, int new_n, int nb_bytes);
static void reset_selections(void);
static int resize(int width, int height);
static void run_command(const char *command, int background_process, int wait_for_confirmation, int potential_changes);
static Selection *search(Selection *a, const char *pattern);
static int search_word_under_cursor(void);
static void set_attr_buffer(uintattr_t *buf, int start, int nb, uintattr_t value);
static void shift_line_nb(Line *l, int min, int max, int delta);
static void shift_sel_line_nb(Selection *a, int min, int max, int delta);
static void split(Line *l, Selection *s);
static void suppress(Line *l, Selection *s);
static void unwrap_pos(Pos p);
static void upper(Line *l, Selection *s);
static void write_file(const char *file_name);
// variables
static Interface message, file_name_int, replace_pattern, search_pattern;
static Line *first_line, *first_line_on_screen;
static Pos anchor, matching_bracket;
static Selection *saved, *running, *displayed;
static Substring fields[10], subpatterns[10];
static int nb_lines, y, x;
static int anchored, in_insert_mode, attribute_x, is_bracket;
static int has_been_changes, has_been_invalid_resizing;
static int asked_indent, asked_remove;
static int screen_height, screen_width, horizontal_offset, vertical_padding;
static uint32_t *ch, to_insert;
static uintattr_t *fg, *bg;
static struct {
Line *start;
int nb_lines;
} clipboard;
static const struct lang *lang;
static struct {
int case_sensitive;
char field_separator;
int highlight_selections;
int syntax_highlight;
int tab_width;
} settings = {
CASE_SENSITIVE,
FIELD_SEPARATOR,
HIGHLIGHT_SELECTIONS,
SYNTAX_HIGHLIGHT,
TAB_WIDTH,
};
// function implementations
void
act(void (*process)(Line *, Selection *), int line_op)
{
Line *l;
Selection *s;
int old_line_nb;
s = (saved) ? saved : running;
l = get_line(s->l - first_line_nb);
old_line_nb = 0;
has_been_changes = 1;
for (; s; s = s->next) {
for (; l->line_nb < s->l; l = l->next);
if (!line_op || s->l > old_line_nb)
process(l, s);
old_line_nb = s->l;
}
}
#ifdef ENABLE_AUTOCOMPLETE
void
autocomplete(Line *l, Selection *s)
{
Line *sl;
char *match, *tmp;
int k, k1, k2, it, kt, ms, dl, ml, max_ml;
k2 = kt = get_str_index(l->chars, it = s->x + s->n);
while ((kt == k2 || is_word_char(l->chars[kt])) && kt > 0)
decrement(l->chars, &it, &kt, it - 1);
k1 = (kt == k2 || is_word_char(l->chars[kt])) ? 0 : (kt + tb_utf8_char_length(l->chars[kt]));
if (k1 == k2)
return;
match = NULL;
ms = dl = ml = max_ml = 0;
for (sl = first_line, k = 0; sl;) {
if (is_word_char(sl->chars[k]) && is_word_boundary(sl->chars, k) &&
(k + k2 - k1) < sl->ml && !strncmp(l->chars + k1, sl->chars + k, k2 - k1) &&
is_word_char(sl->chars[k + k2 - k1])) {
k += k2 - k1;
for (ml = dl = 0; (!match) ? is_word_char(sl->chars[k + ml]) :
(ml < max_ml && !compare_chars(match, ms + ml, sl->chars, k + ml));
dl++, ml += tb_utf8_char_length(sl->chars[k + ml]));
max_ml = ml;
if (!match) {
match = sl->chars;
ms = k;
}
}
if (sl->chars[k]) {
k += tb_utf8_char_length(sl->chars[k]);
} else {
sl = sl->next;
k = 0;
}
}
if (match) {
tmp = (char *) emalloc(ml);
strncpy(tmp, match + ms, ml);
replace_chars(l, s, s->x + s->n, 0, dl, ml);
strncpy(l->chars + k2, tmp, ml);
free(tmp);
}
}
#endif // ENABLE_AUTOCOMPLETE
void
break_line(Line *l, Selection *s, int start)
{
Line *new;
char *new_chars;
int k;
shift_sel_line_nb(s, l->line_nb + 1, 0, 1);
move_sel_end_of_line(s, l->line_nb, start, -1);
shift_line_nb(l, l->line_nb + 1, 0, 1);
k = get_str_index(l->chars, start);
new = create_line(l->line_nb + 1, l->ml - k, l->dl - start);
strncpy(new->chars, l->chars + k, l->ml - k);
new_chars = (char *) emalloc(k + 1);
new_chars[k] = '\0';
strncpy(new_chars, l->chars, k);
free(l->chars);
l->chars = new_chars;
l->ml = k + 1;
l->dl = start;
link_lines(new, l->next);
link_lines(l, new);
nb_lines++;
}
int
column_sel(int n)
{
Line *l;
Selection *last, *tmp;
Pos cursor;
int i, wx, wn;
cursor = pos_of_cursor();
if (anchored && anchor.l != cursor.l)
return 0;
n = MIN(n, nb_lines - cursor.l + 1);
wx = (anchored) ? MIN(cursor.x, anchor.x) : x;
wn = (anchored) ? ABS(cursor.x - anchor.x) : 0;
last = NULL;
for (i = 0, l = get_line(y + n - 1); i < n; i++, l = l->prev)
if (l->dl >= wx)
last = create_sel(l->line_nb, wx, MIN(wn, l->dl - wx), last);
tmp = merge_sel(saved, last);
SET_SEL_LIST(saved, tmp);
if (anchored) {
if (cursor.l + n > nb_lines || anchor.x > get_line(y + n)->dl)
anchored = 0;
else
anchor.l = cursor.l + n;
}
return n;
}
void
comment(Line *l, Selection *s)
{
int k, syntax_length = strlen(lang->comment);
if (!(l->chars[k = find_first_non_blank(l)]))
return;
if (is_type(comment, syntax_length - 1)) {
replace_chars(l, s, k, syntax_length, 0, 0);
} else {
k = replace_chars(l, s, k, 0, syntax_length, syntax_length);
strncpy(l->chars + k, lang->comment, syntax_length);
}
}
int
compare_chars(const char *s1, int k1, const char *s2, int k2)
{
int k, l1, l2, delta;
if ((l1 = tb_utf8_char_length(s1[k1])) != (l2 = tb_utf8_char_length(s2[k2])))
return l2 - l1;
else
for (k = 0; k < l1; k++)
if ((delta = s2[k2 + k] - s1[k1 + k])) {
if (!(settings.case_sensitive) && k == l1 - 1 && ABS(delta) == (1 << 5))
return 0;
return delta;
}
return 0;
}
Selection *
compute_running_sel(void)
{
Selection *medium_sel, *end_sel;
Pos cursor, begin, end;
cursor = pos_of_cursor();
if (!anchored)
return create_sel(cursor.l, cursor.x, 0, NULL);
if (cursor.l == anchor.l)
return create_sel(cursor.l, MIN(cursor.x, anchor.x), ABS(cursor.x - anchor.x), NULL);
begin = (cursor.l > anchor.l) ? anchor : cursor;
end = (cursor.l > anchor.l) ? cursor : anchor;
end_sel = create_sel(end.l, 0, end.x, NULL);
medium_sel = (begin.l + 1 > end.l - 1) ? end_sel : range_lines_sel(begin.l + 1, end.l - 1, end_sel);
return create_sel(begin.l, begin.x, get_line(begin.l - first_line_nb)->dl - begin.x, medium_sel);
}
void
concatenate_line(Line *l, Selection *s)
{
Line *next;
char *new_chars;
move_sel_end_of_line(s, l->line_nb + 1, l->dl, 1);
shift_sel_line_nb(s, l->line_nb + 1, 0, -1);
shift_line_nb(l, l->line_nb + 1, 0, -1);
next = l->next;
new_chars = (char *) emalloc(l->ml + next->ml - 1);
strncpy(new_chars, l->chars, l->ml - 1);
free(l->chars);
strncpy(new_chars + l->ml - 1, next->chars, next->ml);
free(next->chars);
l->chars = new_chars;
l->ml += next->ml - 1;
l->dl += next->dl;
link_lines(l, next->next);
free(next);
nb_lines--;
}
void
copy_to_clip(int starting_line_nb, int nb)
{
Line *l, *cb_l, *old_cb_l;
int i;
nb = MIN(nb, nb_lines - starting_line_nb + 1);
forget_lines(clipboard.start);
clipboard.nb_lines = nb;
cb_l = old_cb_l = NULL;
for (i = 0, l = get_line(starting_line_nb - first_line_nb); i < nb; i++, l = l->next) {
cb_l = create_line(i, l->ml, l->dl);
strncpy(cb_l->chars, l->chars, l->ml);
link_lines(old_cb_l, cb_l);
if (i == 0)
clipboard.start = cb_l;
old_cb_l = cb_l;
}
link_lines(cb_l, NULL);
}
Line *
create_line(int line_nb, int ml, int dl)
{
Line *res;
res = (Line *) emalloc(sizeof(Line));
res->prev = res->next = NULL;
res->line_nb = line_nb;
res->ml = ml;
res->dl = dl;
res->chars = (char *) emalloc(ml);
res->chars[ml - 1] = '\0';
return res;
}
Selection *
create_sel(int l, int x, int n, Selection *next)
{
Selection *res;
res = (Selection *) emalloc(sizeof(Selection));
res->l = l;
res->x = x;
res->n = n;
res->next = next;
return res;
}
void
decrement(const char *chars, int *i, int *k, int goal)
{
while (*i > goal)
for ((*i)--, (*k)--; (chars[*k] & 0xc0) == 0x80; (*k)--);
}
int
dialog(const char *prompt, Interface interf, int refresh_sel)
{
Interface previous;
char unicode_buffer[6];
int dpl, dx, i, k, n, len;
struct tb_event ev;
strcpy(previous, interf);
strcpy(interf, "");
for (k = i = 0; prompt[k]; i++, k += tb_utf8_char_length(prompt[k]));
dpl = i;
dx = n = 0;
while (1) {
if (refresh_sel) {
SET_SEL_LIST(displayed, search(saved, search_pattern));
print_all();
}
strcpy(message, prompt);
strcat(message, interf);
print_dialog_ruler();
tb_set_cursor(dx + dpl, screen_height - 1);
tb_present();
tb_poll_event(&ev);
switch (ev.type) {
case TB_EVENT_KEY:
if (ev.ch && dpl + n + 1 < MIN(INTERFACE_WIDTH, screen_width - RULER_WIDTH)) {
k = get_str_index(interf, i = dx);
len = tb_utf8_unicode_to_char(unicode_buffer, ev.ch);
memmove(interf + k + len, interf + k, strlen(interf + k) + 1);
strncpy(interf + k, unicode_buffer, len);
dx++;
n++;
} else
switch (ev.key) {
case TB_KEY_ARROW_LEFT:
case TB_KEY_ARROW_RIGHT:
dx = CONSTRAIN(0, dx + ((ev.key == TB_KEY_ARROW_LEFT) ? -1 : 1), n);
break;
case TB_KEY_CTRL_A:
case TB_KEY_CTRL_E:
dx = (ev.key == TB_KEY_CTRL_A) ? 0 : n;
break;
case TB_KEY_ARROW_UP:
case TB_KEY_ARROW_DOWN:
strcpy(interf, (ev.key == TB_KEY_ARROW_UP) ? previous : "");
for (k = i = 0; interf[k]; i++, k += tb_utf8_char_length(interf[k]));
dx = n = i;
break;
case TB_KEY_DELETE:
if (dx == n)
break;
dx++;
// fall-through
case TB_KEY_BACKSPACE:
case TB_KEY_BACKSPACE2:
if (dx == 0)
break;
k = get_str_index(interf, dx - 1);
len = tb_utf8_char_length(interf[k]);
memmove(interf + k, interf + k + len, strlen(interf + k + len) + 1);
dx--;
n--;
break;
case TB_KEY_ENTER:
return 1;
case TB_KEY_ESC:
strcpy(interf, previous);
echo("");
return 0;
}
break;
#ifdef MOUSE_SUPPORT
case TB_EVENT_MOUSE:
switch (ev.key) {
case TB_KEY_MOUSE_LEFT:
dx = CONSTRAIN(0, ev.x - dpl, n);
break;
}
break;
#endif // MOUSE_SUPPORT
case TB_EVENT_RESIZE:
if ((has_been_invalid_resizing = resize(ev.w, ev.h)))
return 0;
print_all();
break;
}
}
}
void
die(int exit_status, const char *msg)
{
tb_shutdown();
if (msg)
fprintf((exit_status) ? stderr : stdout, "%s\n", msg);
exit(exit_status);
}
int
eat_pattern_atom(const char *sp, int *j, int *l)
{
int min, max;
if (sp[*l] == '^' || sp[*l] == '$') {
(*j)++; (*l)++;
} else if (sp[*l] == '\\' && strchr("AZbB", sp[*l + 1])) {
(*j) += 2; (*l) += 2;
} else {
if (eat_pattern_character(sp, j, l))
return EXIT_FAILURE;
if (parse_repeater(sp, j, l, &min, &max))
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int
eat_pattern_block(const char *sp, int *j, int *l, int *nb_subpatterns)
{
int min, max;
if (sp[*l] == '(') {
(*nb_subpatterns)++;
while (sp[*l] != ')') {
if (!(sp[*l]) || eat_pattern_atom(sp, j, l))
return EXIT_FAILURE;
while (sp[*l] == '|') {
(*j)++; (*l)++;
if (!(sp[*l]) || eat_pattern_atom(sp, j, l))
return EXIT_FAILURE;
}
}
(*j)++; (*l)++;
return parse_repeater(sp, j, l, &min, &max);
} else
return eat_pattern_atom(sp, j, l);
}
int
eat_pattern_character(const char *sp, int *j, int *l)
{
if (sp[*l] == '\\' && strchr("\\^$|()*+?{[.dDwW", sp[*l + 1])) {
(*j) += 2; (*l) += 2;
} else if (sp[*l] == '[') {
for (; sp[*l] && sp[*l] != ']'; (*j)++, (*l) += tb_utf8_char_length(sp[*l]));
if (!(sp[*l]))
return EXIT_FAILURE;
(*j)++; (*l)++;
} else {
(*j)++; (*l) += tb_utf8_char_length(sp[*l]);
}
return EXIT_SUCCESS;
}
void *
emalloc(size_t size)
{
void *p;
if (!(p = malloc(size)))
die(EXIT_FAILURE, ERR_MALLOC);
return p;
}
int
find_block_delim(int starting_line_nb, int nb)
{
Line *l;
l = get_line(starting_line_nb - first_line_nb);
if (nb < 0)
while (nb++) {
for (; l->prev && !(l->dl); l = l->prev);
for (; l->prev && l->dl; l = l->prev);
}
else
while (nb--) {
for (; l->next && !(l->dl); l = l->next);
for (; l->next && l->dl; l = l->next);
}
return l->line_nb + ((nb == 1 && (l->prev || !(l->dl))) ? 1 : 0);
}
int
find_first_non_blank(const Line *l)
{
int i;
for (i = 0; l->chars[i] == ' '; i++);
return i;
}
Pos
find_matching_bracket(void)
{
Line *l;
char bracket, associated_bracket, c;
int dx, e, nb;
l = get_line(y);
dx = x;
nb = 1;
switch (bracket = l->chars[get_str_index(l->chars, dx)]) {
DECLARE_BRACKETS('(', ')')
DECLARE_BRACKETS('{', '}')
DECLARE_BRACKETS('[', ']')
DECLARE_BRACKETS('<', '>')
default:
return pos_of(l->line_nb, x);
}
while (nb && !move(&l, &dx, e)) {
c = l->chars[get_str_index(l->chars, dx)];
if (c == bracket)
nb++;
else if (c == associated_bracket)
nb--;
}
if (nb)
is_bracket = 0;
return pos_of(l->line_nb, dx);
}
Pos
find_next_selection(int delta)
{
int nb, closest, asked_number, last_strictly_before;
if (!(nb = nb_sel(saved)))
return pos_of(0, 0);
closest = index_closest_after_cursor(saved);
if (delta > 0) {
if (closest == -1)
return pos_of(0, 0);
asked_number = MIN(closest + delta - 1, nb - 1);
} else {
if (closest == 0)
return pos_of(0, 0);
last_strictly_before = ((closest == -1) ? nb : closest) - 1;
if (!is_inf(get_pos_of_sel(saved, last_strictly_before), pos_of_cursor()))
last_strictly_before--;
if (last_strictly_before < 0)
return pos_of(0, 0);
asked_number = MAX(last_strictly_before + delta + 1, 0);
}
return get_pos_of_sel(saved, asked_number);
}
Pos
find_start_of_word(int n)
{
Line *l;
int dx, e;
l = get_line(y);
dx = x;
e = (n > 0) ? 1 : -1;
n *= e;
while (n--) {
while (!move(&l, &dx, e) && is_word_char(l->chars[get_str_index(l->chars, dx)]));
while (!move(&l, &dx, e) && !is_word_char(l->chars[get_str_index(l->chars, dx)]));
}
if (e == -1)
while (!move(&l, &dx, e))
if (!is_word_char(l->chars[get_str_index(l->chars, dx)])) {
move(&l, &dx, -e);
break;
}
return pos_of(l->line_nb, dx);
}
void
forget_lines(Line *start)
{
Line *l, *next;
for (l = start; l; next = l->next, free(l->chars), free(l), l = next);
}
void
forget_sel_list(Selection *a)
{
Selection *next;
for (; a; next = a->next, free(a), a = next);
}
Line *
get_line(int delta_from_first_line_on_screen)
{
Line *res;
res = first_line_on_screen;
if (delta_from_first_line_on_screen > 0)
for (; res->next && delta_from_first_line_on_screen--; res = res->next);
else
for (; res->prev && delta_from_first_line_on_screen++; res = res->prev);
return res;
}
Pos
get_pos_of_sel(Selection *a, int index)
{
int i;
for (i = 0; a && i < index; i++, a = a->next);
return (a) ? pos_of(a->l, a->x) : pos_of(0, 0);
}
int
get_str_index(const char *chars, int x)
{
int i, k;
for (i = k = 0; i < x; i++, k += tb_utf8_char_length(chars[k]));
return k;
}
void
indent(Line *l, Selection *s)
{
int k, start, n;
if (!in_insert_mode && l->dl == 0)
return;
start = (in_insert_mode) ? s->x : find_first_non_blank(l);
if (asked_indent > 0) {
n = asked_indent*(settings.tab_width) - start%(settings.tab_width);
k = replace_chars(l, s, start, 0, n, n);
memset(l->chars + k, ' ', n);
} else {
k = get_str_index(l->chars, start);
for (n = 0; n < k && n < (-asked_indent - 1)*(settings.tab_width) + 1 +
(start - 1)%(settings.tab_width) && l->chars[k - n - 1] == ' '; n++);
replace_chars(l, s, start - n, n, 0, 0);
}
}
int
index_closest_after_cursor(Selection *a)
{
Pos cursor;
int res;
cursor = pos_of_cursor();
for (res = 0; a; res++, a = a->next)
if (is_inf(cursor, pos_of_sel(*a)))
return res;
return -1;
}
void
init_termbox(void)
{
tb_init();
tb_set_clear_attrs(COLOR_DEFAULT, COLOR_BG_DEFAULT);
#ifdef MOUSE_SUPPORT
tb_set_input_mode(TB_INPUT_ESC | TB_INPUT_MOUSE);
#else
tb_set_input_mode(TB_INPUT_ESC);
#endif // MOUSE_SUPPORT
#ifdef TERM_256_COLORS_SUPPORT
tb_set_output_mode(TB_OUTPUT_256);
#else
tb_set_output_mode(TB_OUTPUT_NORMAL);
#endif // TERM_256_COLORS_SUPPORT
has_been_invalid_resizing = resize(tb_width(), tb_height());
}
void
insert(Line *l, Selection *s)
{
char unicode_buffer[6];
int len, k;
len = tb_utf8_unicode_to_char(unicode_buffer, to_insert);
k = replace_chars(l, s, s->x, 0, 1, len);
strncpy(l->chars + k, unicode_buffer, len);
}
void
insert_clip(int below)
{
int first_inserted_line_nb;
first_inserted_line_nb = first_line_nb + y + ((below) ? 1 : 0);
insert_lines(clipboard.start, clipboard.nb_lines, below);
clipboard.start = NULL;
copy_to_clip(first_inserted_line_nb, clipboard.nb_lines);
}
void
insert_line(int line_nb, int ml, int dl)
{
Line *replaced_line, *new;
new = create_line(line_nb, ml, dl);
if (line_nb == nb_lines + 1) {
link_lines(get_line(nb_lines - first_line_nb), new);
link_lines(new, NULL);
} else {
replaced_line = get_line(line_nb - first_line_nb);
shift_line_nb(replaced_line, line_nb, 0, 1);
shift_sel_line_nb(saved, line_nb, 0, 1);
link_lines(replaced_line->prev, new);
link_lines(new, replaced_line);
if (!(new->prev))
first_line = new;
if (replaced_line == first_line_on_screen)
first_line_on_screen = new;
}
nb_lines++;
has_been_changes = 1;
}
void
insert_lines(Line *to_insert, int nb, int below)
{
Line *starting_line, *l, *before, *after;
int i, first_inserted_line_nb;
if (!to_insert)
return;
starting_line = get_line(y);
first_inserted_line_nb = starting_line->line_nb + ((below) ? 1 : 0);
shift_line_nb(to_insert, 0, 0, first_inserted_line_nb - to_insert->line_nb);
shift_line_nb(starting_line, first_inserted_line_nb, 0, nb);
shift_sel_line_nb(saved, first_inserted_line_nb, 0, nb);
for (i = 1, l = to_insert; i < nb; i++, l = l->next);
before = (below) ? starting_line : starting_line->prev;
after = (below) ? starting_line->next : starting_line;
link_lines(before, to_insert);
link_lines(l, after);
if (!before)
first_line = to_insert;
nb_lines += nb;
has_been_changes = 1;
if (!below) {
if (y == 0)
first_line_on_screen = get_line(-clipboard.nb_lines);
y += clipboard.nb_lines;
}
}
int
is_in(const char *list, const char *chars, int length)
{
int len;
for (; *list; list += len + 1) {
if ((len = strchr(list, ' ') - list) != length)
continue;
if (!strncmp(list, chars, len))
return 1;
}
return 0;
}
int
is_inf(Pos p1, Pos p2)
{
return (p1.l < p2.l || (p1.l == p2.l && p1.x < p2.x));
}
int
is_word_boundary(const char *chars, int k)
{
int i, is_word;
is_word = is_word_char(chars[k]);
if (k == 0)
return is_word;
i = 1;
decrement(chars, &i, &k, i - 1);
return (is_word != is_word_char(chars[k]));
}
int
is_word_char(char c)
{
return (isalpha(c) || c == '_' || c == ACC_LETTER);
}
void
link_lines(Line *l1, Line *l2)
{
if (l1)
l1->next = l2;
if (l2)
l2->prev = l1;
}
void
load_file(int first_line_on_screen_nb)
{
int nb;
forget_lines(first_line);
reset_selections();
if ((nb = parse_file(&first_line, NULL, file_name_int, 1, 0, 1))) {
first_line_on_screen = first_line;
first_line_on_screen = get_line(first_line_on_screen_nb - 1);
} else
first_line = first_line_on_screen = create_line(1, 1, 0);
nb_lines = (nb) ? nb : 1;
has_been_changes = (nb) ? 0 : 1;
parse_lang(file_name_int);
}
void
lower(Line *l, Selection *s)
{
char c;
int i, k;
for (k = get_str_index(l->chars, i = s->x); i < s->x + s->n; i++, k += tb_utf8_char_length(c))
if (isupper(c = l->chars[k]) || c == ACC_LETTER)
l->chars[k + ((c == ACC_LETTER) ? 1 : 0)] |= (1 << 5);
}
int
mark_fields(const char *chars, int sx, int n)
{
int f, a, st, mst, i, k;
k = get_str_index(chars, i = sx);
SET_SUBSTRING(fields[0], st = i, mst = k, n, get_str_index(chars + k, n));
for (a = 1; a < 10; a++)
SET_SUBSTRING(fields[a], 0, 0, 0, 0);
for (f = 1; f < 10 && i < sx + n; i++, k += tb_utf8_char_length(chars[k]))
if ((chars[k] == settings.field_separator) && (k == 0 || chars[k - 1] != '\\')) {
SET_SUBSTRING(fields[f], st, mst, i - st, k - mst);
f++;
st = i + 1;
mst = k + 1;
} else if (chars[k] == '\\') {
i++; k++;
}
if (f < 10) {
SET_SUBSTRING(fields[f], st, mst, i - st, k - mst);
f++;
}
return f - 1;
}
int
mark_subpatterns(const char *sp, const char *chars, int dl, int ss, int sx, int n)
{
// try to read searched pattern sp in chars, store identified subpatterns
// dl must be the visual length of chars, and ss the real selection start
// return length of read pattern if found at sx, of length < n, else 0