-
Notifications
You must be signed in to change notification settings - Fork 2
/
editor.c
3085 lines (2570 loc) · 60.7 KB
/
editor.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
/*
* Copyright (c) 2020 Joris Vink <joris@coders.se>
*
* Permission to use, copy, modify, and 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.
*/
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <ctype.h>
#include <dirent.h>
#include <pwd.h>
#include <libgen.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "ce.h"
#define CE_SPLASH_TEXT_1 "Coma Editor"
#define CE_SPLASH_TEXT_2 "Peace Of Mind"
#define CE_UTF8_U2015 "\xe2\x80\x95"
/* Show messages for 5 seconds. */
#define EDITOR_MESSAGE_DELAY 5
#define EDITOR_CMD_BUFLIST 0x12
#define EDITOR_CMD_PASTE 0x16
#define EDITOR_CMD_HIST_PREV 0x10
#define EDITOR_CMD_HIST_NEXT 0x0e
#define EDITOR_INTERRUPT 0x03
#define EDITOR_WORD_ERASE 0x17
#define EDITOR_KEY_ESC 0x1b
#define EDITOR_KEY_UP 0xfa
#define EDITOR_KEY_DOWN 0xfb
#define EDITOR_KEY_LEFT 0xfc
#define EDITOR_KEY_RIGHT 0xfd
/* UTF-8 § (U+00A7). */
#define EDITOR_KEY_UTF8_PREFIX 0xc2
#define EDITOR_KEY_UTF8_CONSOLE 0xa7
#define EDITOR_COMMAND_DELETE 1
#define EDITOR_COMMAND_YANK 2
#define EDITOR_COMMAND_WORD_NEXT 3
#define EDITOR_COMMAND_WORD_PREV 4
#define EDITOR_COMMAND_MARK_SET 5
#define EDITOR_COMMAND_MARK_JMP 6
#define EDITOR_COMMAND_ALTER 7
#define EDITOR_COMMAND_PROCESS 8
#define EDITOR_COMMAND_ALIGN 9
#define EDITOR_COMMAND_JUMP_DOWN 10
#define EDITOR_COMMAND_JUMP_UP 11
#define EDITOR_COMMAND_BUFFER 12
#define KEY_MAP_LEN(x) ((sizeof(x) / sizeof(x[0])))
struct keymap {
u_int8_t key;
void (*command)(void);
};
struct inq {
size_t sz;
size_t off;
u_int8_t data[512];
};
static void editor_signal(int);
static void editor_resume(void);
static void editor_event_wait(void);
static void editor_read_input(void);
static void editor_signal_setup(void);
static void editor_consume_input(void);
static u_int8_t editor_process_escape(void);
static void editor_preset_cmd(const char *);
static int editor_allowed_command_key(char);
static int editor_cmd_can_autocomplete(void);
static void editor_shellbuf_close(struct cebuf *);
static int editor_get_input(u_int8_t *, int,int);
static void editor_autocomplete_path(struct cebuf *);
static void editor_splash_text(u_int16_t, const char *, ...);
static void editor_shellbuf_new(const char *, struct cebuf **);
static void editor_yank_lines(struct cebuf *, size_t, size_t, int);
static void editor_draw_status(void);
static void editor_draw_cmdbuf(void);
static void editor_draw_suggestions(int);
static void editor_draw_message(struct timespec *);
static void editor_cmd_quit(int);
static void editor_cmd_grep(void);
static void editor_cmd_find(void);
static void editor_cmd_exec(void);
static void editor_cmd_reset(void);
static void editor_cmd_paste(void);
static void editor_cmd_record(void);
static void editor_cmd_replay(void);
static void editor_cmd_suspend(void);
static void editor_cmd_word_erase(void);
static void editor_cmd_search_next(void);
static void editor_cmd_search_prev(void);
static void editor_cmd_search_word(void);
static void editor_cmd_buffer_list(void);
static void editor_cmd_buffer_next(void);
static void editor_cmd_buffer_prev(void);
static void editor_cmd_history_prev(void);
static void editor_cmd_history_next(void);
static void editor_cmd_history_cycle(int, int);
static void editor_cmd_suggestions(struct cehist *, size_t);
static void editor_cmd_directory_list(void);
static void editor_directory_from_buffer(void);
static void editor_directory_list(const char *);
static void editor_directory_change(const char *);
static void editor_cmd_execute(char *);
static void editor_cmd_open_file(const char *);
static void editor_cmd_command_mode(void);
static void editor_cmd_search_mode(void);
static void editor_cmd_normal_mode(void);
static void editor_cmd_change_string(struct cebuf *);
static void editor_cmd_word_prev(struct cebuf *, long);
static void editor_cmd_word_next(struct cebuf *, long);
static void editor_cmd_yank_lines(struct cebuf *, long);
static void editor_cmd_delete_words(struct cebuf *, long);
static void editor_cmd_delete_lines(struct cebuf *, long);
static void editor_cmd_select_mode(void);
static void editor_cmd_select_execute(void);
static void editor_cmd_select_yank_delete(int);
static void editor_cmd_insert_mode(void);
static void editor_cmd_insert_mode_append(void);
static void editor_cmd_insert_mode_prepend(void);
static void editor_select_mode_command(u_int8_t);
static void editor_normal_mode_command(u_int8_t);
static void editor_dirlist_mode_command(u_int8_t);
static void editor_no_input(struct cebuf *, u_int8_t);
static void editor_cmdbuf_input(struct cebuf *, u_int8_t);
static void editor_cmdbuf_search(struct cebuf *, u_int8_t);
static void editor_buflist_input(struct cebuf *, u_int8_t);
static struct keymap normal_map[] = {
{ 'k', ce_buffer_move_up },
{ 'j', ce_buffer_move_down },
{ 'l', ce_buffer_move_right },
{ 'h', ce_buffer_move_left },
{ 'C', ce_buffer_center },
{ 'T', ce_buffer_top },
{ '$', ce_buffer_jump_right },
{ '0', ce_buffer_jump_left },
{ 'G', ce_buffer_jump_down },
{ 'J', ce_buffer_join_line },
{ 0x06, ce_buffer_page_down },
{ 0x02, ce_buffer_page_up },
{ 'x', ce_buffer_delete_character},
{ 'p', editor_cmd_paste },
{ 'n', editor_cmd_search_next },
{ 'N', editor_cmd_search_prev },
{ 0x23, editor_cmd_search_word },
{ 'i', editor_cmd_insert_mode },
{ 'o', editor_cmd_insert_mode_append },
{ 'O', editor_cmd_insert_mode_prepend },
{ 's', editor_cmd_select_mode },
{ 'g', editor_cmd_grep },
{ 'f', editor_cmd_find },
{ 'e', editor_cmd_exec },
{ ':', editor_cmd_command_mode },
{ '/', editor_cmd_search_mode },
{ 'R', editor_cmd_replay },
{ '@', editor_cmd_record },
{ 0x04, editor_cmd_directory_list },
{ EDITOR_CMD_BUFLIST, editor_cmd_buffer_list },
{ EDITOR_CMD_HIST_PREV, editor_cmd_buffer_prev },
{ EDITOR_CMD_HIST_NEXT, editor_cmd_buffer_next },
{ 0x1a, editor_cmd_suspend },
};
static struct keymap insert_map[] = {
{ EDITOR_CMD_PASTE, editor_cmd_paste },
{ EDITOR_KEY_UP, ce_buffer_move_up },
{ EDITOR_KEY_DOWN, ce_buffer_move_down },
{ EDITOR_KEY_RIGHT, ce_buffer_move_right },
{ EDITOR_KEY_LEFT, ce_buffer_move_left },
{ EDITOR_WORD_ERASE, editor_cmd_word_erase },
{ EDITOR_CMD_HIST_NEXT, editor_cmd_history_next },
{ EDITOR_CMD_HIST_PREV, editor_cmd_history_prev },
{ EDITOR_KEY_ESC, editor_cmd_normal_mode },
};
static struct keymap command_map[] = {
{ 0, NULL },
};
static struct keymap buflist_map[] = {
{ 'k', ce_buffer_move_up },
{ 'j', ce_buffer_move_down },
{ 'G', ce_buffer_jump_down },
{ 'n', editor_cmd_search_next },
{ 'N', editor_cmd_search_prev },
{ 0x06, ce_buffer_page_down },
{ 0x02, ce_buffer_page_up },
{ '/', editor_cmd_search_mode },
{ EDITOR_KEY_ESC, editor_cmd_normal_mode },
};
static struct keymap select_map[] = {
{ 'k', ce_buffer_move_up },
{ 'j', ce_buffer_move_down },
{ 'l', ce_buffer_move_right },
{ 'h', ce_buffer_move_left },
{ 'C', ce_buffer_center },
{ '$', ce_buffer_jump_right },
{ '0', ce_buffer_jump_left },
{ 'G', ce_buffer_jump_down },
{ 'J', ce_buffer_join_line },
{ 0x06, ce_buffer_page_down },
{ 0x02, ce_buffer_page_up },
{ '/', editor_cmd_search_mode },
{ 'n', editor_cmd_search_next },
{ 'N', editor_cmd_search_prev },
{ EDITOR_KEY_ESC, editor_cmd_normal_mode },
};
static struct {
const struct keymap *map;
size_t maplen;
} keymaps[] = {
{ normal_map, KEY_MAP_LEN(normal_map) },
{ insert_map, KEY_MAP_LEN(insert_map) },
{ command_map, KEY_MAP_LEN(command_map) },
{ buflist_map, KEY_MAP_LEN(buflist_map) },
{ command_map, KEY_MAP_LEN(command_map) },
{ select_map, KEY_MAP_LEN(select_map) },
};
static struct {
const char *cmd;
void (*run)(const char *);
} cmdtab[] = {
{ "cd", editor_directory_change },
{ NULL, NULL },
};
static struct {
char *message;
time_t when;
} msg;
static struct inq inq;
static struct inq rec;
static int quit = 0;
static int dirty = 1;
static int splash = 0;
static int pasting = 0;
static int award_xp = 0;
static volatile sig_atomic_t sig_recv = -1;
static int recording = 0;
static int normalcmd = -1;
static char *home = NULL;
static char *curdir = NULL;
static char *search = NULL;
static struct cebuf *cmdbuf = NULL;
static struct cebuf *buflist = NULL;
static struct cebuf *pbuffer = NULL;
static struct cebuf *suggestions = NULL;
static int suggestions_wipe = 0;
static int mode = CE_EDITOR_MODE_NORMAL;
static int lastmode = CE_EDITOR_MODE_NORMAL;
void
ce_editor_init(void)
{
struct passwd *pw;
char *login, pwd[PATH_MAX];
if ((home = getenv("HOME")) == NULL) {
if ((login = getlogin()) == NULL)
fatal("%s: getlogin: %s", __func__, errno_s);
if ((pw = getpwnam(login)) == NULL)
fatal("%s: getpwnam: %s", __func__, errno_s);
home = pw->pw_dir;
}
home = ce_strdup(home);
if (getcwd(pwd, sizeof(pwd)) == NULL)
fatal("%s: getpwd: %s", __func__, errno_s);
memset(&msg, 0, sizeof(msg));
memset(&inq, 0, sizeof(inq));
memset(&rec, 0, sizeof(rec));
editor_directory_change(pwd);
free(msg.message);
msg.message = NULL;
ce_debug("homedir is '%s'", home);
ce_debug("curdir is '%s'", curdir);
editor_signal_setup();
}
void
ce_editor_loop(void)
{
struct timespec ts;
struct cemark tmp;
struct cebuf *buf;
u_int32_t level;
pbuffer = ce_buffer_internal("<pb>");
ce_buffer_reset(pbuffer);
cmdbuf = ce_buffer_internal("<cmd>");
ce_buffer_reset(cmdbuf);
cmdbuf->cb = editor_cmdbuf_input;
cmdbuf->line = ce_term_height();
cmdbuf->orig_line = ce_term_height();
buflist = ce_buffer_internal("<buflist>");
buflist->cb = editor_buflist_input;
suggestions = ce_buffer_internal("<suggestions>");
while (!quit) {
(void)clock_gettime(CLOCK_MONOTONIC, &ts);
if (sig_recv != -1) {
switch (sig_recv) {
case SIGQUIT:
case SIGTERM:
quit = 1;
return;
case SIGCONT:
dirty = 1;
editor_resume();
break;
case SIGWINCH:
dirty = 1;
ce_term_restore();
ce_term_setup();
ce_buffer_resize();
cmdbuf->line = ce_term_height();
cmdbuf->orig_line = ce_term_height();
break;
}
sig_recv = -1;
}
buf = ce_buffer_active();
if (mode == CE_EDITOR_MODE_SELECT) {
tmp.line = ce_buffer_line_index(buf);
tmp.col = buf->column;
tmp.off = buf->loff;
tmp.set = 1;
if (tmp.line < buf->selmark.line) {
buf->selend = buf->selmark;
buf->selstart = tmp;
} else if (tmp.line == buf->selmark.line) {
if (tmp.col <= buf->selmark.col) {
buf->selstart.col = tmp.col;
buf->selstart.off = tmp.off;
buf->selend = buf->selmark;
} else {
buf->selend.col = tmp.col;
buf->selend.off = tmp.off;
buf->selstart = buf->selmark;
}
} else {
if (buf->selstart.line != buf->selmark.line &&
buf->selstart.col != buf->selmark.col) {
buf->selstart = buf->selmark;
}
buf->selend = tmp;
}
/* for now XXX */
dirty = 1;
}
if (dirty) {
if (mode == CE_EDITOR_MODE_SEARCH &&
buf->prev->buftype == CE_BUF_TYPE_DIRLIST) {
ce_term_writestr(TERM_SEQUENCE_CLEAR_ONLY);
ce_buffer_map(buf->prev);
} else if (buf != cmdbuf) {
ce_term_writestr(TERM_SEQUENCE_CLEAR_ONLY);
ce_buffer_map(buf);
} else if (suggestions_wipe) {
ce_term_writestr(TERM_SEQUENCE_CLEAR_ONLY);
ce_buffer_map(buf->prev);
}
dirty = 0;
suggestions_wipe = 0;
}
if (buf == cmdbuf)
editor_draw_cmdbuf();
if (splash) {
level = ce_game_level();
ce_term_foreground_rgb(128, 128, 128);
ce_term_writestr(TERM_SEQUENCE_CURSOR_SAVE);
editor_splash_text(0, CE_SPLASH_TEXT_1);
editor_splash_text(1, CE_SPLASH_TEXT_2);
editor_splash_text(3, "You are a %s",
ce_game_level_name());
editor_splash_text(4,
"%u xp required until level %u",
ce_game_xp_required(level + 1) -
ce_game_xp(), level + 1);
editor_splash_text(6,
"You have opened ce %u times in total",
ce_game_open_count());
ce_term_writestr(TERM_SEQUENCE_CURSOR_RESTORE);
ce_term_attr_off();
}
editor_draw_status();
editor_draw_message(&ts);
ce_term_flush();
editor_event_wait();
while (inq.off != inq.sz)
editor_consume_input();
if (splash) {
dirty = 1;
splash = 0;
}
}
free(search);
free(msg.message);
}
void
ce_editor_dirty(void)
{
dirty = 1;
}
void
ce_editor_show_splash(void)
{
splash = 1;
}
int
ce_editor_mode(void)
{
return (mode);
}
void
ce_editor_message(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
ce_editor_messagev(fmt, args);
va_end(args);
}
void
ce_editor_messagev(const char *fmt, va_list args)
{
struct timespec ts;
int len;
size_t off, width;
free(msg.message);
if ((len = vasprintf(&msg.message, fmt, args)) == -1)
fatal("%s: vasprintf: %s", __func__, errno_s);
width = ce_term_width() - 8;
if ((size_t)len > width) {
off = len - width;
memmove(&msg.message[0], &msg.message[off], (len - off) + 1);
msg.message[0] = '>';
}
(void)clock_gettime(CLOCK_MONOTONIC, &ts);
msg.when = ts.tv_sec;
}
int
ce_editor_yesno(void (*cb)(const void *), const void *arg, const char *fmt, ...)
{
u_int8_t key;
va_list args;
va_start(args, fmt);
ce_editor_messagev(fmt, args);
va_end(args);
for (;;) {
editor_draw_message(NULL);
ce_term_flush();
if (editor_get_input(&key, 1, 0) == 0)
return (-1);
switch (key) {
case 'y':
cb(arg);
return (0);
case 'n':
case EDITOR_KEY_ESC:
free(msg.message);
msg.message = NULL;
return (-1);
}
}
return (-1);
}
void
ce_editor_pbuffer_reset(void)
{
ce_buffer_reset(pbuffer);
}
void
ce_editor_pbuffer_append(const void *data, size_t len)
{
ce_buffer_append(pbuffer, data, len);
}
void
ce_editor_pbuffer_sync(void)
{
#if defined(__APPLE__)
ce_macos_set_pasteboard_contents(pbuffer->data, pbuffer->length);
#endif
}
int
ce_editor_word_byte(u_int8_t byte)
{
if (isspace(byte))
return (0);
if (ce_editor_word_separator(byte))
return (0);
return (1);
}
int
ce_editor_word_separator(u_int8_t byte)
{
if (isspace(byte))
return (1);
switch (byte) {
case '(':
case ')':
case '{':
case '}':
case '\t':
case '\n':
case '[':
case ']':
case ':':
case ';':
case ',':
case '-':
case '=':
case '*':
case '.':
case '@':
case '<':
case '>':
case '\'':
case '"':
case '&':
case '/':
return (1);
default:
break;
}
return (0);
}
void
ce_editor_set_pasting(int val)
{
pasting = val;
}
int
ce_editor_pasting(void)
{
return (pasting);
}
const char *
ce_editor_fullpath(const char *path)
{
int len;
static char tmp[PATH_MAX];
if (path[0] == '~') {
if (strlen(path) > 1) {
len = snprintf(tmp, sizeof(tmp), "%s%s%s",
home, path[1] == '/' ? "" : "/", &path[1]);
} else {
len = snprintf(tmp, sizeof(tmp), "%s", home);
}
} else {
len = snprintf(tmp, sizeof(tmp), "%s", path);
}
if (len == -1 || (size_t)len >= sizeof(tmp))
fatal("%s: failed to construct '%s'", __func__, path);
return (tmp);
}
const char *
ce_editor_shortpath(const char *path)
{
int slen;
size_t len, plen;
static char tmp[PATH_MAX];
plen = strlen(path);
len = strlen(curdir);
if (!strncmp(curdir, path, len)) {
if (len == plen)
return (path);
return (&path[len + 1]);
}
len = strlen(home);
if (!strncmp(home, path, len)) {
slen = snprintf(tmp, sizeof(tmp), "~%s", &path[len]);
if (slen == -1 || (size_t)slen >= sizeof(tmp))
fatal("%s: snprintf '~%s'", __func__, &path[len]);
return (tmp);
}
return (path);
}
const char *
ce_editor_pwd(void)
{
return (curdir);
}
const char *
ce_editor_home(void)
{
return (home);
}
void
ce_editor_settings(struct cebuf *buf)
{
if (buf == NULL) {
config.tab_width = CE_TAB_WIDTH_DEFAULT;
config.tab_expand = CE_TAB_EXPAND_DEFAULT;
return;
}
ce_syntax_guess(buf);
if (ce_lame_mode()) {
config.tab_width = 4;
config.tab_expand = 1;
return;
}
switch (buf->type) {
case CE_FILE_TYPE_CSS:
case CE_FILE_TYPE_HTML:
case CE_FILE_TYPE_YAML:
case CE_FILE_TYPE_JSON:
case CE_FILE_TYPE_PYTHON:
config.tab_width = 4;
config.tab_expand = 1;
break;
default:
config.tab_width = CE_TAB_WIDTH_DEFAULT;
config.tab_expand = CE_TAB_EXPAND_DEFAULT;
break;
}
}
static void
editor_signal(int sig)
{
sig_recv = sig;
}
static void
editor_signal_setup(void)
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = editor_signal;
if (sigfillset(&sa.sa_mask) == -1)
fatal("sigfillset: %s", errno_s);
if (sigaction(SIGCONT, &sa, NULL) == -1)
fatal("sigaction: %s", errno_s);
if (sigaction(SIGQUIT, &sa, NULL) == -1)
fatal("sigaction: %s", errno_s);
if (sigaction(SIGTERM, &sa, NULL) == -1)
fatal("sigaction: %s", errno_s);
if (sigaction(SIGCHLD, &sa, NULL) == -1)
fatal("sigaction: %s", errno_s);
if (sigaction(SIGWINCH, &sa, NULL) == -1)
fatal("sigaction: %s", errno_s);
(void)signal(SIGINT, SIG_IGN);
(void)signal(SIGHUP, SIG_IGN);
(void)signal(SIGPIPE, SIG_IGN);
}
static void
editor_event_wait(void)
{
int nfd;
struct pollfd pfd[CE_MAX_POLL];
pfd[0].events = POLLIN;
pfd[0].fd = STDIN_FILENO;
nfd = 1 + ce_buffer_proc_gather(&pfd[1], CE_MAX_POLL - 1);
if ((nfd = poll(pfd, nfd, -1)) == -1) {
if (errno == EINTR)
return;
fatal("%s: poll %s", __func__, errno_s);
}
if (nfd == 0)
return;
if (pfd[0].revents & (POLLHUP | POLLERR))
fatal("%s: stdin error", __func__);
if (pfd[0].revents & POLLIN)
editor_read_input();
ce_buffer_proc_dispatch();
}
static void
editor_read_input(void)
{
ssize_t sz;
size_t idx;
for (;;) {
sz = read(STDIN_FILENO, inq.data, sizeof(inq.data));
if (sz == -1) {
if (errno == EINTR)
continue;
fatal("%s: read: %s", __func__, errno_s);
}
if (sz == 0)
fatal("%s: stdin eof", __func__);
inq.sz = sz;
inq.off = 0;
if (recording) {
idx = 0;
while (rec.sz != sizeof(rec.data) && idx < inq.sz)
rec.data[rec.sz++] = inq.data[idx++];
}
award_xp += sz;
if (award_xp >= 50) {
ce_game_add_xp();
award_xp = 0;
}
break;
}
}
static void
editor_consume_input(void)
{
size_t idx;
u_int8_t key;
struct cebuf *curbuf = ce_buffer_active();
if (mode >= CE_EDITOR_MODE_MAX)
fatal("%s: mode %d invalid", __func__, mode);
if (editor_get_input(&key, 0, 0) == 0)
return;
if (key == EDITOR_KEY_ESC)
key = editor_process_escape();
if (mode == CE_EDITOR_MODE_NORMAL_CMD) {
editor_normal_mode_command(key);
return;
}
for (idx = 0; idx < keymaps[mode].maplen; idx++) {
if (key == keymaps[mode].map[idx].key &&
keymaps[mode].map[idx].command != NULL) {
keymaps[mode].map[idx].command();
return;
}
}
switch (mode) {
case CE_EDITOR_MODE_NORMAL:
switch (curbuf->buftype) {
case CE_BUF_TYPE_DEFAULT:
case CE_BUF_TYPE_SHELLCMD:
editor_normal_mode_command(key);
return;
case CE_BUF_TYPE_DIRLIST:
editor_dirlist_mode_command(key);
return;
}
break;
case CE_EDITOR_MODE_SELECT:
editor_select_mode_command(key);
return;
}
if (mode != CE_EDITOR_MODE_COMMAND && mode != CE_EDITOR_MODE_SEARCH)
ce_hist_autocomplete_reset(NULL);
ce_buffer_input(curbuf, key);
}
static u_int8_t
editor_process_escape(void)
{
u_int8_t key, ret;
ret = EDITOR_KEY_ESC;
if (editor_get_input(&key, 0, 1) == 0)
goto cleanup;
if (key != 0x5b)
goto cleanup;
inq.off++;
if (editor_get_input(&key, 0, 1) == 0)
goto cleanup;
switch (key) {
case 0x41:
inq.off++;
ret = EDITOR_KEY_UP;
break;
case 0x42:
inq.off++;
ret = EDITOR_KEY_DOWN;
break;
case 0x43:
inq.off++;
ret = EDITOR_KEY_RIGHT;
break;
case 0x44:
inq.off++;
ret = EDITOR_KEY_LEFT;
break;
}
cleanup:
return (ret);
}
static int
editor_get_input(u_int8_t *out, int fetch, int peek)
{
*out = 0;
if ((inq.sz == 0 || inq.off == inq.sz) && fetch)
editor_read_input();
if (inq.sz == 0 || inq.off == inq.sz)
return (0);
*out = inq.data[inq.off];
if (peek == 0)
inq.off++;
return (1);
}
static void
editor_draw_status(void)
{
const u_int8_t *ptr;
struct cebuf *curbuf;
size_t cmdoff, width, pc, dlen;
int flen, slen, llen, procfd;
char fline[1024], sline[128], lline[128];
const char *isdirty, *filemode, *modestr, *dname, *recmode;
isdirty = "";
recmode = "";
filemode = "";
modestr = NULL;
curbuf = ce_buffer_active();
switch (mode) {
case CE_EDITOR_MODE_SEARCH:
modestr = "- SEARCH -";
break;
case CE_EDITOR_MODE_NORMAL:
case CE_EDITOR_MODE_COMMAND:
case CE_EDITOR_MODE_BUFLIST:
case CE_EDITOR_MODE_NORMAL_CMD:
modestr = "";
break;
case CE_EDITOR_MODE_SELECT:
modestr = "- SELECT -";
break;
case CE_EDITOR_MODE_INSERT:
modestr = "- INSERT -";
break;
default:
fatal("%s: unknown mode %d", __func__, mode);
}
if (curbuf->internal == 0 && (curbuf->flags & CE_BUFFER_DIRTY))
isdirty = " [+]";
if (curbuf->flags & CE_BUFFER_RO)
filemode = "r-";
else
filemode = "rw";
if (recording)
recmode = "R";
procfd = (curbuf->proc != NULL) ? curbuf->proc->ofd : -1;
if (curbuf->top == 0) {
llen = snprintf(lline, sizeof(lline), "%zuL [Top]%s ",
curbuf->lcnt, procfd != -1 ? " *" : "");
} else if (curbuf->lcnt - (curbuf->top + curbuf->line) <
((ce_term_height() - 2) / 2)) {