-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathted.c
556 lines (452 loc) · 11.5 KB
/
ted.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
#include "tui.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>
#define BUFINC 128
int logi10(int a) {
for (int i = -1;; ++i) {
a /= 10;
if (a == 0)
return i;
}
}
int visual_str_len(wchar_t * str, unsigned max_len) {
int l = 0;
for (int i = 0; str[i] && i < max_len; ++i) {
l += 1 + (str[i] == '\t') * (TABSIZ - 1);
}
return l;
}
typedef struct {
wchar_t * content;
unsigned nwchar_t;
} Line;
struct File {
Line * line;
unsigned nline;
} file;
void move_cursor_to_line(unsigned y);
void move_cursor_to_column(unsigned x);
int get_current_line_length();
int get_num_lines();
void init_file();
void draw_file_to_window(Window * win, Window * numwin);
void update_window_sizes();
void update_cursor_position_scroll();
void correct_cursor_column();
void correct_cursor_line();
void correct_cursor();
void set_multiplicator(wchar_t);
void insert_wchar_at_position();
void check_line_length(Line * line, unsigned needed);
void check_number_of_lines(unsigned needed);
void insert_line_at_position(unsigned index, const wchar_t * content, unsigned size);
void remove_wchar_at_position(Line * line, unsigned index);
void remove_line_at_position(unsigned index);
void move_cursor_up();
void move_cursor_down();
void move_cursor_left();
void move_cursor_right();
void go_bottom();
void goto_call();
void write();
void quit();
void set_insert();
void set_command();
void append();
void insert_at_start();
void append_at_end();
void delete_character();
void delete();
void z_combo();
void open_line_after();
void open_line_before();
void normal();
void insert();
void command();
const int active_line_number_color[3] = ACTIVE_LINE_NUMBER_COL;
const int line_number_color[3] = LINE_NUMBER_COL;
enum MODES {
NORMAL,
INSERT,
COMMAND
};
const wchar_t * mode_desc[] = {
L"Normal",
L"Insert",
L"COMMAND"
};
void (*mode[])(void) = {
[NORMAL] = normal,
[INSERT] = insert,
[COMMAND] = command,
};
void (*key_cmd[256])(void) = {
['j'] = move_cursor_down,
['k'] = move_cursor_up,
['h'] = move_cursor_left,
['l'] = move_cursor_right,
['G'] = go_bottom,
['g'] = goto_call,
['i'] = set_insert,
['a'] = append,
['I'] = insert_at_start,
['A'] = append_at_end,
['d'] = delete,
['x'] = delete_character,
['o'] = open_line_after,
['O'] = open_line_before,
[':'] = set_command,
['Z'] = z_combo,
};
struct Cursor {
int line;
int column;
int x;
int y;
} cursor;
Window main_window;
Window num_window;
Window status_bar;
int running = 1;
unsigned scroll = 0;
unsigned multiplicator = 1;
unsigned mulset = 0;
unsigned active_mode = 0;
char * path;
int get_num_lines() {
int i = 0;
for (i = 0; file.line[i].content; ++i);
return i;
}
void quit() {
running = 0;
}
void init_file() {
FILE * f = fopen(path, "r");
file.line = malloc(sizeof(Line) * BUFINC);
file.nline = BUFINC;
if (f) {
wchar_t buf[BUFSIZ];
int i = 0;
while (fgetws(buf, BUFSIZ, f) != NULL) {
buf[wcslen(buf) - 1] = L'\0';
if (i == file.nline - 1) {
file.nline += BUFINC;
file.line = realloc(file.line, sizeof(Line) * file.nline);
}
file.line[i].nwchar_t = wcslen(buf) + 1;
file.line[i].content = malloc(file.line[i].nwchar_t * sizeof(wchar_t));
wcsncpy(file.line[i].content, buf, file.line[i].nwchar_t);
++i;
}
fclose(f);
}
if (get_num_lines() == 0) {
file.line[0].nwchar_t = BUFINC;
file.line[0].content = malloc(file.line[0].nwchar_t * sizeof(wchar_t));
}
}
void write() {
FILE * f = fopen(path, "w");
if (!f) {
perror("ted");
quit();
}
for (int i = 0; i < get_num_lines(); ++i) {
fputws(file.line[i].content, f);
fputwc('\n', f);
}
fclose(f);
}
void draw_file_to_window(Window * win, Window * numwin) {
int y = -scroll;
for (int i = 0; i < file.nline && file.line[i].content && y < (int)main_window.height; ++i) {
printfxy_to_window(win, 0, y, L"%ls" RETURN_SIGN, file.line[i].content);
const int * color = (cursor.line == i ? line_number_color : active_line_number_color);
SET_COLOR(color[0], color[1], color[2]);
printfxy_to_window(numwin, 0, y + 1 + win->has_borders, L"%d", i + 1);
RESET_COLOR();
y += visual_str_len(file.line[i].content, file.line[i].nwchar_t) / win->width + 1;
}
}
void move_cursor_up() {
cursor.line -= multiplicator;
correct_cursor();
}
void move_cursor_down() {
cursor.line += multiplicator;
correct_cursor();
}
void move_cursor_left() {
cursor.column -= multiplicator;
correct_cursor_column();
}
void move_cursor_right() {
cursor.column += multiplicator;
correct_cursor_column();
}
void go_bottom() {
cursor.line = get_num_lines() - 1;
cursor.column = 0;
}
void goto_call() {
wchar_t c = getch();
switch(c) {
case 'g': cursor.line = multiplicator < get_num_lines() ? multiplicator - 1 : get_num_lines() - 1;
cursor.column = 0;
break;
}
}
void delete() {
wchar_t c = getch();
switch(c) {
case 'd':
if (get_num_lines() > 1) {
remove_line_at_position(cursor.line);
} else {
memset(file.line[0].content, 0, sizeof(wchar_t) * file.line[0].nwchar_t);
}
break;
}
correct_cursor();
}
void delete_character() {
remove_wchar_at_position(&file.line[cursor.line], cursor.column);
correct_cursor_column();
}
void z_combo() {
wchar_t c = getch();
switch(c) {
case 'Q': quit();
break;
case 'Z': write();
quit();
break;
}
}
void open_line_after() {
insert_line_at_position(++cursor.line, L"", BUFINC);
cursor.column = 0;
}
void open_line_before() {
insert_line_at_position(cursor.line, L"", BUFINC);
cursor.column = 0;
}
void update_window_sizes() {
num_window.x = 0;
num_window.y = 0;
num_window.width = logi10(get_num_lines()) + 5;
num_window.height = get_term_height();
num_window.has_borders = 0;
main_window.width = get_term_width() - num_window.width;
main_window.height = num_window.height - 1;
main_window.x = 1 + num_window.width;
main_window.y = 1;
main_window.has_borders = 0;
status_bar.y = main_window.height + 1;
status_bar.height = 1;
status_bar.width = main_window.width + num_window.width;
status_bar.x = 1;
status_bar.has_borders = 0;
}
void update_cursor_position_and_scroll() {
cursor.x = main_window.has_borders + main_window.x;
cursor.y = main_window.has_borders + main_window.y;
cursor.x += visual_str_len(file.line[cursor.line].content, cursor.column) % main_window.width;
for (int i = 0; i < cursor.line; ++i) {
cursor.y += visual_str_len(file.line[i].content, file.line[i].nwchar_t) / main_window.width + 1;
}
cursor.y += cursor.column / main_window.width;
if (cursor.y - (int)scroll > (int)main_window.height) {
scroll = cursor.y - main_window.height;
} else if (cursor.y - (int)scroll < 1) {
scroll = cursor.y - 1;
}
cursor.y -= scroll;
}
void set_multiplicator(wchar_t c) {
if (mulset) {
if (multiplicator * 10 + c - 48 < 1000)
multiplicator = multiplicator * 10 + c - 48;
} else {
multiplicator = c - 48;
}
}
void correct_cursor_column() {
Line * line = &file.line[cursor.line];
if (cursor.column + 1 > wcslen(line->content)) {
if (wcslen(line->content) == 0) {
cursor.column = 0;
} else {
cursor.column = wcslen(line->content) - 1;
}
} else if (cursor.column < 0) {
cursor.column = 0;
}
}
void correct_cursor_line() {
if (cursor.line >= get_num_lines()) {
cursor.line = get_num_lines() - 1;
} else if (cursor.line < 0) {
cursor.line = 0;
}
}
void correct_cursor() {
correct_cursor_line();
correct_cursor_column();
}
void set_insert() {
active_mode = INSERT;
}
void set_command() {
active_mode = COMMAND;
}
void append() {
active_mode = INSERT;
if (wcslen(file.line[cursor.line].content) > 0)
++cursor.column;
}
void append_at_end() {
active_mode = INSERT;
cursor.column = wcslen(file.line[cursor.line].content);
}
void insert_at_start() {
active_mode = INSERT;
cursor.column = 0;
}
void normal() {
wchar_t c = getch();
if (*key_cmd[c]) {
(*key_cmd[c])();
mulset = 0;
multiplicator = 1;
} else if (c > 47 && c < 58) {
set_multiplicator(c);
mulset = 1;
}
}
void command() {
wchar_t buf[BUFSIZ] = {0};
int command_input = 1;
while (command_input) {
printfxy_to_window(&status_bar, 0, 0, L"-- COMMAND -- :%ls", buf);
wchar_t c = getch();
switch (c) {
case '\n':
command_input = 0;
if (!wcscmp(buf, L"q")) {
quit();
} else if (!wcscmp(buf, L"wq")) {
write();
quit();
} else if (!wcscmp(buf, L"w")) {
write();
}
break;
case 127:
buf[wcslen(buf) - 1] = 0;
case 27:
command_input = 0;
default:
buf[wcslen(buf)] = c;
break;
}
}
active_mode = NORMAL;
}
void insert() {
wchar_t c = getch();
Line * line = &file.line[cursor.line];
if (c == 27) {
active_mode = NORMAL;
} else if (c == 127) {
if (cursor.column > 0) {
remove_wchar_at_position(line, --cursor.column);
} else if (cursor.line > 0) {
unsigned n = wcslen(file.line[cursor.line - 1].content);
check_line_length(&file.line[cursor.line - 1], file.line[cursor.line - 1].nwchar_t);
wcsncat(file.line[cursor.line - 1].content, file.line[cursor.line].content, file.line[cursor.line].nwchar_t);
remove_line_at_position(cursor.line--);
cursor.column = n;
}
} else if (c == '\n') {
check_number_of_lines(get_num_lines() + 1);
insert_line_at_position(cursor.line + 1, line->content + cursor.column, line->nwchar_t);
file.line[cursor.line].content[cursor.column] = 0;
++cursor.line;
cursor.column = 0;
} else {
insert_wchar_at_position(line, cursor.column, c);
++cursor.column;
}
}
void remove_wchar_at_position(Line * line, unsigned index) {
for (int i = index; i < wcslen(line->content) + 1; ++i)
line->content[i] = line->content[i + 1];
}
void remove_line_at_position(unsigned index) {
for (int i = index; i < get_num_lines() + 1; ++i)
file.line[i] = file.line[i + 1];
}
void insert_line_at_position(unsigned index, const wchar_t * content, unsigned size) {
for (int i = get_num_lines(); i + 1 > index; --i) {
file.line[i] = file.line[i - 1];
}
file.line[index].content = malloc(sizeof(wchar_t) * size);
check_line_length(&file.line[index], size);
wmemset(file.line[index].content, 0, file.line[index].nwchar_t);
wcsncpy(file.line[index].content, content, size);
}
void insert_wchar_at_position(Line * line, unsigned index, wchar_t c) {
check_line_length(line, wcslen(line->content));
unsigned s = wcslen(line->content) + 1 - index;
wchar_t * end = malloc(sizeof(wchar_t) * s);
wcsncpy(end, line->content + index, s);
line->content[index] = c;
line->content[index + 1] = 0;
wcsncat(line->content, end, s);
}
void check_line_length(Line * line, unsigned needed) {
if (wcslen(line->content) + 1 == line->nwchar_t) {
line->nwchar_t += BUFINC;
line->content = realloc(line->content, sizeof(wchar_t) * line->nwchar_t);
}
}
void check_number_of_lines(unsigned needed) {
if (needed > file.nline) {
file.nline += BUFINC;
file.line = realloc(file.line, sizeof(Line) * file.nline);
}
}
int main(int argc, char ** argv) {
setlocale(LC_ALL, "");
if (argc != 2) {
fwprintf(stderr, L"%s [file]\n", argv[0]);
return -1;
}
path = argv[1];
init_file();
inittui();
showecho(0);
cursor.line = 0;
cursor.column = 0;
while (running) {
update_window_sizes();
update_cursor_position_and_scroll();
draw_window(&main_window);
draw_window(&status_bar);
draw_window(&num_window);
draw_file_to_window(&main_window, &num_window);
printfxy_to_window(&status_bar, 0, 0, L"-- %ls -- ", mode_desc[active_mode]);
if (mulset) {
printfxy_to_window(&status_bar, status_bar.width - logi10(multiplicator) - 2, 0, L"%d", multiplicator);
}
set_cursor_position(cursor.x, cursor.y);
mode[active_mode]();
}
showcursor(1);
endtui();
return 0;
}