-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
687 lines (632 loc) · 14.3 KB
/
main.c
File metadata and controls
687 lines (632 loc) · 14.3 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
#include "main.h"
char *mode_names[] = {"NORMAL", "INSERT", "VISUAL"};
String buf; // the editor's buffer
String buf_backup; // undo backup buffer
String inpbuf; // command input buffer
String outbuf; // command output buffer
String pipebuf; // pipes buffer
String clip; // clipboard
Mode mode = NORMAL; // current editor mode
char *filename = NULL; // current filename
int is_saved = 1; // is the current buffer saved?
int total_lines = 1; // total lines of the current buffer
int top_line = 1; // number of the top visible line
int bottom_line = 1; // number of the bottom visible line
int lineno_width; // the width of the line numbers
int cursor_line = 1; // cursor line number
int cursor_lpos = 0; // cursor position in line on screen
int cursor_idx = 0; // cursor index in buffer
int cursor_backup_idx; // undo backup cursor index
int saved_cursor; // saved cursor index for visual mode
FindAns *find_matches; // matches for find that should be highlighted
void clearline(int num)
{
move(num, 0);
for (int i = 0; i < SCR_WIDTH; i++) addch(' ');
}
int saveas_file(char *path)
{
if (address_error(path)) return 0;
save_buffer(&buf, path);
if (filename == NULL) filename = strdup(path);
is_saved = 1;
return 1;
}
void save_file(void)
{
if (filename != NULL)
saveas_file(filename);
while (filename == NULL)
{
clearline(SCR_HEIGHT - 1);
move(SCR_HEIGHT - 1, 0);
printw("Enter a name for the current file: ");
char name[MAX_FILENAME];
echo();
getstr(name);
noecho();
char *path = convert_path(name);
saveas_file(path);
free(path);
}
}
char get_prompt(char *msg)
{
clearline(SCR_HEIGHT - 1);
move(SCR_HEIGHT - 1, 0);
printw("%s", msg);
refresh();
char ch = getch();
clearline(SCR_HEIGHT - 1);
refresh();
return ch;
}
// close the current file in the editor
void close_file(void)
{
if (!is_saved)
{
char ch = get_prompt("Do you want to save the current file? (y/n)");
if (ch != 'N' && ch != 'n') save_file();
}
string_free(&buf);
string_free(&buf_backup);
free(filename);
filename = NULL;
}
// ensure that the buffer ends with '\n'
void terminate_with_LF(String *buf)
{
if (buf->len == 0)
{
string_resize(buf, 1);
buf->len = 1;
buf->arr[0] = '\n';
}
else if (buf->arr[buf->len - 1] != '\n')
{
string_pushc(buf, '\n');
}
}
// initialize global vars based a newly opened buffer
void init_new_buf(String *buf)
{
terminate_with_LF(buf);
is_saved = (filename != NULL);
total_lines = 0;
for (size_t i = 0; i < buf->len; i++)
total_lines += (buf->arr[i] == '\n');
top_line = 1;
bottom_line = SCR_HEIGHT - 2;
if (total_lines < bottom_line) bottom_line = total_lines;
cursor_line = 1;
cursor_lpos = 0;
cursor_idx = 0;
cursor_backup_idx = -1;
}
// refresh buffer global vars based on cursor_idx and previous top_line/bottom_line
void refresh_buffer_vars(String *buf)
{
terminate_with_LF(buf);
total_lines = 0;
for (size_t i = 0; i < buf->len; i++)
{
if (i == cursor_idx) cursor_line = total_lines + 1;
total_lines += (buf->arr[i] == '\n');
}
cursor_calc_lpos();
// cut adjustments
if (bottom_line > total_lines)
{
bottom_line = total_lines;
}
if (cursor_line < top_line)
{
top_line = cursor_line;
bottom_line = top_line + SCR_HEIGHT - 3;
if (bottom_line > total_lines) bottom_line = total_lines;
}
// paste adjustments
if (bottom_line - top_line < SCR_HEIGHT - 3 && total_lines > bottom_line)
{
bottom_line = total_lines;
if (bottom_line - top_line > SCR_HEIGHT - 3)
top_line = bottom_line - (SCR_HEIGHT - 3);
}
if (cursor_line > bottom_line)
{
bottom_line = cursor_line;
top_line = bottom_line - (SCR_HEIGHT - 3);
if (top_line < 1) top_line = 1;
}
}
void open_file(char *path)
{
if (address_error(path)) return;
if (file_not_found_error(path)) return;
close_file();
filename = strdup(path);
load_buffer(&buf, path);
init_new_buf(&buf);
}
void print_msg(char *msg)
{
clearline(SCR_HEIGHT - 1);
move(SCR_HEIGHT - 1, 0);
attron(COLOR_PAIR(ERROR_COLOR));
printw("%s", msg);
attroff(COLOR_PAIR(ERROR_COLOR));
refresh();
getch();
clearline(SCR_HEIGHT - 1);
refresh();
}
void print_status_bar(void)
{
move(SCR_HEIGHT - 2, 0);
attron(COLOR_PAIR(MODE_COLOR));
printw(" %s ", mode_names[mode]);
attroff(COLOR_PAIR(MODE_COLOR));
attron(COLOR_PAIR(FILENAME_COLOR));
if (filename != NULL)
printw(" %s ", strrchr(filename, '/') + 1);
else
printw(" untitled ");
if (!is_saved) addch('+');
int y, x;
getyx(stdscr, y, x);
for (; x < SCR_WIDTH - 30; x++)
addch(' ');
printw("%d:%d - %d", cursor_line, cursor_lpos, cursor_idx);
getyx(stdscr, y, x);
for (; x < SCR_WIDTH; x++)
addch(' ');
attroff(COLOR_PAIR(FILENAME_COLOR));
}
void print_buffer(String *buf)
{
lineno_width = count_digits(total_lines) + 1;
char *ptr = buf->arr + pos2idx(buf, top_line, 0);
for (int i = 0; i < SCR_HEIGHT - 2 && i <= bottom_line - top_line; i++)
{
attron(COLOR_PAIR(LINENO_COLOR));
move(i, 0);
printw("%*d ", lineno_width - 1, top_line + i);
attroff(COLOR_PAIR(LINENO_COLOR));
while (*ptr != '\n')
{
// selection highlighting
int highlight = 0;
int L = cursor_idx;
int R = saved_cursor;
if (R < L)
{
int tmp = R;
R = L;
L = tmp;
}
if (mode == VISUAL && ptr - buf->arr >= L && ptr - buf->arr <= R) highlight = 1;
if (mode == NORMAL && find_matches != NULL) // find highlighting
{
for (int i = 0; find_matches[i].L != -1; i++)
{
if (ptr - buf->arr >= find_matches[i].L && ptr - buf->arr <= find_matches[i].R)
{
highlight = 1;
break;
}
}
}
if (highlight) attron(COLOR_PAIR(SELECTION_COLOR));
if (*ptr == '\t')
{
int y, x;
getyx(stdscr, y, x);
x -= lineno_width;
for (int i = 0; i < TABSIZE - x % TABSIZE; i++)
addch(' ');
}
else if (*ptr == '{' || *ptr == '}')
{
if (!highlight) attron(COLOR_PAIR(CRBRACE_COLOR));
addch(*ptr);
if (!highlight) attroff(COLOR_PAIR(CRBRACE_COLOR));
}
else if (*ptr == '(' || *ptr == ')')
{
if (!highlight) attron(COLOR_PAIR(BRACE_COLOR));
addch(*ptr);
if (!highlight) attroff(COLOR_PAIR(BRACE_COLOR));
}
else if (*ptr == '[' || *ptr == ']')
{
if (!highlight) attron(COLOR_PAIR(SQBRACE_COLOR));
addch(*ptr);
if (!highlight) attroff(COLOR_PAIR(SQBRACE_COLOR));
}
else
addch(*ptr);
ptr++;
if (highlight) attroff(COLOR_PAIR(SELECTION_COLOR));
}
addch(*ptr++);
}
}
// move the cursor to its correct location on screen
void position_cursor(void)
{
move(cursor_line - top_line, cursor_lpos + lineno_width);
}
void draw_window(void)
{
clear();
print_status_bar();
print_buffer(&buf);
position_cursor();
refresh();
}
void command_mode(char start_char)
{
move(SCR_HEIGHT - 1, 0);
addch(start_char);
char cmd[MAX_CMD_LEN];
echo();
getstr(cmd);
noecho();
if (start_char == ':')
{
int clen = strlen(cmd);
cmd[clen++] = '\n';
cmd[clen] = '\0';
int ret = parse_line(cmd);
if (ret == 0 && outbuf.len)
{
// move command output into editor buffer
close_file();
buf = outbuf;
outbuf.arr = NULL;
outbuf.len = outbuf.cap = 0;
init_new_buf(&buf);
}
else if (ret == 1) // exit
{
close_file();
sigint_handler(0);
}
}
else
{
graphical_find(cmd);
}
}
void copy_selection(void)
{
size_t L = saved_cursor;
size_t R = cursor_idx;
if (R < L)
{
size_t tmp = R;
R = L;
L = tmp;
}
string_free(&clip);
string_init(&clip, R - L + 1);
memcpy(clip.arr, buf.arr + L, R - L + 1);
}
void remove_selection(void)
{
size_t L = saved_cursor;
size_t R = cursor_idx;
if (R < L)
{
size_t tmp = R;
R = L;
L = tmp;
}
if (L == buf.len - 1) return;
if (R == buf.len - 1) R--;
string_remove(&buf, L, R + 1);
cursor_idx = L;
refresh_buffer_vars(&buf);
}
int cursor_right(void)
{
if (buf.arr[cursor_idx] == '\n') return 0;
cursor_lpos += (buf.arr[cursor_idx] == '\t' ? TABSIZE - cursor_lpos % TABSIZE : 1);
cursor_idx++;
return 1;
}
// go to the beginning og the current line
void cursor_home(void)
{
cursor_lpos = 0;
while (cursor_idx > 0 && buf.arr[cursor_idx - 1] != '\n') cursor_idx--;
}
void cursor_left(void)
{
int dist = cursor_idx;
cursor_home();
dist -= cursor_idx;
for (int i = 0; i < dist - 1; i++) cursor_right();
}
// use this to calculate cursor_lpos correctly when cursor_idx is correct
void cursor_calc_lpos(void)
{
if (cursor_idx > 0 && buf.arr[cursor_idx - 1] != '\n')
{
cursor_left();
cursor_right();
}
else
cursor_left();
}
void create_backup_buf(void)
{
string_free(&buf_backup);
string_init(&buf_backup, buf.len);
memcpy(buf_backup.arr, buf.arr, buf.len);
cursor_backup_idx = cursor_idx;
}
void undo_buf(void)
{
if (cursor_backup_idx == -1)
{
print_msg("Error: No change to undo");
return;
}
String tmp = buf;
buf = buf_backup;
buf_backup = tmp;
int ctmp = cursor_idx;
cursor_idx = cursor_backup_idx;
cursor_backup_idx = ctmp;
refresh_buffer_vars(&buf);
is_saved = 0;
}
void graphical_find(char *pat)
{
char *tmp = strchr(pat, '\n');
if (tmp) *tmp = 0;
if (pat[0] == '\0')
{
print_msg("Error: empty pattern");
return;
}
parsestr_wildcard(pat);
string_null_terminate(&buf);
find_matches = findall_buf(buf.arr, pat);
if (find_matches[0].L == -1)
{
print_msg("Pattern not found");
free(find_matches);
find_matches = NULL;
return;
}
int cur_match = 0;
cursor_idx = find_matches[0].L;
refresh_buffer_vars(&buf);
draw_window();
while (1)
{
int ch = getch();
if (ch == 'n')
{
cur_match++;
if (find_matches[cur_match].L == -1) cur_match = 0;
cursor_idx = find_matches[cur_match].L;
refresh_buffer_vars(&buf);
draw_window();
}
else
{
free(find_matches);
find_matches = NULL;
break;
}
}
}
int input_loop(void)
{
int key = getch();
if (mode == NORMAL && (key == ':' || key == '/'))
{
command_mode(key);
}
// cursor navigation
else if (key == KEY_UP || (mode != INSERT && key == 'k'))
{
if (cursor_line == 1) return 0;
cursor_line--;
if (cursor_line < top_line + 3 && top_line != 1) // shift lines
{
top_line--;
if (bottom_line - top_line > SCR_HEIGHT - 3)
bottom_line--;
}
int tmp = cursor_lpos;
cursor_home();
cursor_idx--;
cursor_home();
while (cursor_lpos < tmp)
if (!cursor_right())
break;
}
else if (key == KEY_DOWN || (mode != INSERT && key == 'j'))
{
if (cursor_line == total_lines) return 0;
cursor_line++;
if (cursor_line > bottom_line - 3 && bottom_line != total_lines) // shift lines
{
top_line++;
bottom_line++;
}
int tmp = cursor_lpos;
cursor_lpos = 0;
while (buf.arr[cursor_idx] != '\n') cursor_idx++;
cursor_idx++;
while (cursor_lpos < tmp)
if (!cursor_right())
break;
}
else if (key == KEY_RIGHT || (mode != INSERT && key == 'l'))
{
cursor_right();
}
else if (key == KEY_LEFT || (mode != INSERT && key == 'h'))
{
cursor_left();
}
// insert mode
else if (mode == NORMAL && key == 'i')
{
mode = INSERT;
create_backup_buf();
}
else if (mode == INSERT && key == KEY_ESC)
{
mode = NORMAL;
}
else if (mode == INSERT && key == KEY_BACKSPACE)
{
if (cursor_idx == 0) return 0;
is_saved = 0;
if (buf.arr[cursor_idx - 1] != '\n')
{
string_remove(&buf, cursor_idx - 1, cursor_idx);
cursor_idx--;
cursor_calc_lpos();
}
else
{
string_remove(&buf, cursor_idx - 1, cursor_idx);
cursor_line--;
total_lines--;
cursor_idx--;
cursor_calc_lpos();
if (cursor_line < top_line) // backspace in the first line
{
top_line--;
bottom_line--;
}
else if (bottom_line > total_lines)
bottom_line--;
}
}
//else if (mode == INSERT && key == KEY_DC) {} // possible implementation of DEL key
else if (mode == INSERT && (isprint(key) || key == '\t'))
{
is_saved = 0;
string_insert(&buf, (char *)&key, 1, cursor_idx);
cursor_right();
}
else if (mode == INSERT && key == '\n')
{
is_saved = 0;
string_insert(&buf, (char *)&key, 1, cursor_idx);
cursor_idx++;
cursor_line++;
cursor_lpos = 0;
total_lines++;
if (cursor_line > bottom_line) // enter in the last line
{
bottom_line++;
if (bottom_line - top_line > SCR_HEIGHT - 3)
top_line++;
}
else if (bottom_line - top_line < SCR_HEIGHT - 3)
bottom_line++;
}
// visual mode
else if (mode == NORMAL && key == 'v')
{
mode = VISUAL;
saved_cursor = cursor_idx;
}
else if (mode == VISUAL && (key == KEY_ESC || key == 'v'))
{
mode = NORMAL;
}
else if (mode == VISUAL && key == 'd') // cut
{
mode = NORMAL;
create_backup_buf();
copy_selection();
remove_selection();
is_saved = 0;
}
else if (mode == VISUAL && key == 'y') // copy
{
mode = NORMAL;
copy_selection();
}
else if (mode == NORMAL && key == '=') // auto-indent
{
create_backup_buf();
auto_indent_buf(&buf);
cursor_idx = 0;
refresh_buffer_vars(&buf);
is_saved = 0;
}
else if (mode == NORMAL && key == 'p') // paste
{
create_backup_buf();
string_insert(&buf, clip.arr, clip.len, cursor_idx);
cursor_idx += clip.len;
refresh_buffer_vars(&buf);
is_saved = 0;
}
else if (mode == NORMAL && key == 'u') // undo
{
undo_buf();
}
return 0;
}
void sigint_handler(int signum)
{
string_free(&buf);
string_free(&buf_backup);
string_free(&inpbuf);
string_free(&outbuf);
string_free(&pipebuf);
string_free(&clip);
endwin();
exit(0);
}
int main(void)
{
initscr(); // initialize ncurses
cbreak(); // disable line buffering
keypad(stdscr, TRUE); // enable getting input from arrow keys, F1, F2, etc.
noecho(); // disable echo when getting keyboard input
start_color(); // start using ncurses colors
set_escdelay(0); // cancel the delay for ESC key
struct sigaction sigint_action = {{sigint_handler}};
sigaction(SIGINT, &sigint_action, NULL);
// init colors
init_pair(MODE_COLOR, COLOR_BLACK, COLOR_GREEN);
init_pair(FILENAME_COLOR, COLOR_WHITE, COLOR_BLUE);
init_pair(LINENO_COLOR, COLOR_WHITE, COLOR_BLUE);
init_pair(ERROR_COLOR, COLOR_RED, COLOR_BLACK);
init_pair(BRACE_COLOR, COLOR_GREEN, COLOR_BLACK);
init_pair(SQBRACE_COLOR, COLOR_RED, COLOR_BLACK);
init_pair(CRBRACE_COLOR, COLOR_MAGENTA, COLOR_BLACK);
init_pair(SELECTION_COLOR, COLOR_BLACK, COLOR_CYAN);
// init editor
string_init(&buf, 0);
init_new_buf(&buf);
is_saved = 1;
// move(10, 10);
// char str[] = "this is so cool this is so cool this is so cool";
// char pat[] = "h\x80";
// FindAns *res = findall_buf(str, pat);
// printw("%d %d", res[1].L, res[1].R);
// refresh();
// getch();
while (1)
{
draw_window();
input_loop();
}
sigint_handler(0);
return 0;
}