Repository navigation
Expand file tree
/
Copy pathview_edit.c
More file actions
646 lines (596 loc) · 25.7 KB
/
Copy pathview_edit.c
File metadata and controls
646 lines (596 loc) · 25.7 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
#include "includes.h"
#include "types.h"
#include "globals.h"
char *find_newline(char *buffer, size_t length) {
char *pos_r = memchr(buffer, '\r', length);
char *pos_n = memchr(buffer, '\n', length);
if (pos_r && pos_n) {
if (pos_r + 1 == pos_n) {
return pos_n; // if \r\n is encountered, break on the later
}
return pos_r < pos_n ? pos_r : pos_n;
} else if (pos_r) {
return pos_r;
} else {
return pos_n;
}
}
// Complete partial writes and retry interrupted writes before reporting a failure.
static int write_all(int fd, const char *data, size_t length)
{
while (length)
{
ssize_t written=write(fd, data, length);
if (written < 0 && errno == EINTR) continue;
if (written <= 0)
{
if (written == 0) errno=EIO;
return -1;
}
data+=written;
length-=written;
}
return 0;
}
// Save through symlinks and preserve ownership and permissions when replacing the target.
int write_file_lines(const char *filename, file_lines *lines)
{
char *target=realpath(filename, NULL);
if (!target) return -1;
struct stat original;
if (stat(target, &original) != 0)
{
free(target);
return -1;
}
char temp_filename[strlen(target)+11];
snprintf(temp_filename, sizeof(temp_filename), "%s.tmpXXXXXX", target);
int fd=mkstemp(temp_filename);
if (fd == -1)
{
free(target);
return -1;
}
int error=0;
for (file_lines *current=lines; current; current=current->next)
{
if (write_all(fd, current->line, current->line_length) != 0 ||
(current->next && write_all(fd, "\n", 1) != 0))
{
error=errno;
break;
}
}
// Apply the mode after ownership and data changes, which can clear special mode bits.
if (!error && fchown(fd, original.st_uid, original.st_gid) != 0) error=errno;
if (!error && fchmod(fd, original.st_mode & 07777) != 0) error=errno;
if (!error && fsync(fd) != 0) error=errno;
if (close(fd) != 0 && !error) error=errno;
if (!error && rename(temp_filename, target) != 0) error=errno;
free(target);
if (error)
{
unlink(temp_filename);
errno=error;
return -1;
}
return 0;
}
file_lines* read_file_lines(const char *filename, off_t *num_lines, off_t *num_bytes) {
// Initialize linked list and counters
file_lines *head = NULL, *tail = NULL;
*num_lines = 0;
// Open the file
int fd = open(filename, O_RDONLY);
if (fd == -1) {
return NULL;
}
// Get the file size
struct stat sb;
if (fstat(fd, &sb) == -1) {
close(fd);
return NULL;
}
*num_bytes = sb.st_size;
// Handle empty file scenario separately
if (sb.st_size == 0) {
head = malloc(sizeof(file_lines));
head->line = malloc(1);
close(fd);
head->line_length = 0;
head->next = NULL;
*num_lines = 1;
return head;
}
// Memory map the file
char *file_in_memory = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (file_in_memory == MAP_FAILED)
{
close(fd);
return NULL;
}
char *line_start = file_in_memory;
// Iterate through mapped memory
// Iterate through mapped memory
for (char *current = file_in_memory; current <= file_in_memory + sb.st_size; ++current) {
// Check for end of file or newline character
if (current == file_in_memory + sb.st_size || *current == '\n') {
file_lines *new_node = malloc(sizeof(file_lines));
// Allocate memory for the line data and copy it from the mapped memory
int line_length = current - line_start;
char *line_copy = malloc(line_length+1);
memcpy(line_copy, line_start, line_length);
// Fill in the new node
new_node->line = line_copy;
new_node->line_length = line_length;
new_node->next = NULL;
// Append to the linked list
if (!head) {
head = new_node;
} else {
tail->next = new_node;
}
tail = new_node;
// Prepare for next block
line_start = current + 1;
(*num_lines)++;
}
}
// Clean up
munmap(file_in_memory, sb.st_size);
close(fd);
return head;
}
void free_file_lines(file_lines *head) {
while (head != NULL) {
file_lines *temp = head;
head = head->next;
free(temp->line);
free(temp);
}
}
void free_pattern_regexes(PatternColorPair* patterns, int num_patterns) {
for (int i = 0; i < num_patterns; i++) {
regfree(&patterns[i].regex); // Free each compiled regex
}
}
// Render complete UTF-8 cells and clip by columns without wrapping into the next row.
void display_line(WINDOW *win, file_lines *line, int max_x, int current_col, int editor_mode, PatternColorPair* patterns, int num_patterns)
{
int row=getcury(win), column=0, match_end=0, color=COLOR_PAIR(COLOR_WHITE_ON_BLUE), bold=0;
char *text=malloc(line->line_length+1);
memcpy(text, line->line, line->line_length);
text[line->line_length]='\0';
for (int offset=0; offset < line->line_length && column < current_col+max_x;)
{
wchar_t chars[CCHARW_MAX];
int width, bytes=text_cell(text+offset, line->line_length-offset, chars, &width);
if (offset >= match_end)
{
color=COLOR_PAIR(COLOR_WHITE_ON_BLUE);
bold=0;
regmatch_t match;
for (int i=0; i < num_patterns; i++)
if (regexec(&patterns[i].regex, text+offset, 1, &match, 0) == 0 && match.rm_so == 0 && match.rm_eo > 0)
{
match_end=offset+match.rm_eo;
color=patterns[i].color_pair;
bold=patterns[i].is_bold ? A_BOLD : 0;
break;
}
}
if (column >= current_col && column+width <= current_col+max_x)
{
int attributes=color|bold;
if (chars[0] == L'\t')
{
chars[0]=L'>';
attributes=COLOR_PAIR(COLOR_CYAN_ON_BLUE);
}
else if (!iswprint(chars[0]))
{
chars[0]=editor_mode && chars[0] < 32 ? L'@'+chars[0] : L'.';
if (editor_mode) attributes=COLOR_PAIR(COLOR_WHITE_ON_RED);
}
cchar_t cell;
setcchar(&cell, chars, attributes & ~A_COLOR, PAIR_NUMBER(attributes), NULL);
mvwadd_wchnstr(win, row, column-current_col, &cell, 1);
}
column+=width;
offset+=bytes;
}
free(text);
}
// Compare decoded characters so case-insensitive search also works with non-ASCII text.
static int text_matches(const char *text, int length, const char *pattern)
{
int remaining=strlen(pattern);
while (remaining)
{
wchar_t left, right;
mbstate_t left_state={0}, right_state={0};
size_t left_bytes=mbrtowc(&left, text, length, &left_state);
size_t right_bytes=mbrtowc(&right, pattern, remaining, &right_state);
if (!left_bytes || left_bytes > (size_t)length || right_bytes > (size_t)remaining) return 0;
if (towlower(left) != towlower(right)) return 0;
text+=left_bytes;
length-=left_bytes;
pattern+=right_bytes;
remaining-=right_bytes;
}
return 1;
}
int view_edit_file(char *filename, int editor_mode) {
int max_y, max_x;
int screen_start_line = 0;
int screen_start_col = 0;
int cursor_row = 0;
int is_modified = 0;
PatternColorPair patterns[100] = {0};
int num_patterns = 0;
char find_str[CMD_MAX] = {0};
// Get the screen dimensions
getmaxyx(stdscr, max_y, max_x);
// Top line on screen
WINDOW *toprow_win = newwin(1, max_x, 0, 0);
wbkgd(toprow_win, COLOR_PAIR(COLOR_BLACK_ON_CYAN));
wattron(toprow_win, COLOR_PAIR(COLOR_BLACK_ON_CYAN));
// Create a new window for displaying the file content
WINDOW *content_win = newwin(max_y - 2, max_x, 1, 0);
keypad(content_win, TRUE);
werase(content_win); // Clear the window
wbkgd(content_win, COLOR_PAIR(COLOR_WHITE_ON_BLUE)); // Set the background color
wattron(content_win, COLOR_PAIR(COLOR_WHITE_ON_BLUE));
// Build the linked list of line pointers
off_t num_lines, num_bytes;
file_lines *lines = read_file_lines(filename, &num_lines, &num_bytes);
if (!lines)
{
delwin(toprow_win);
delwin(content_win);
show_errormsg(SPRINTF("Cannot open file:\n%s\n%s", filename, strerror(errno)));
return -1;
}
// Extract file extension
char *file_type = strrchr(filename, '.'); // find last '.' in filename
if (editor_mode)
{
// Syntax highlighting for editor mode
if (file_type && (strcmp(file_type, ".c") == 0 || strcmp(file_type, ".h") == 0)) {
patterns[num_patterns++] = (PatternColorPair) {"\".*\"", COLOR_PAIR(COLOR_GREEN_ON_BLUE), 0, {0}};
patterns[num_patterns++] = (PatternColorPair) {"^(#include|#define).*$", COLOR_PAIR(COLOR_RED_ON_BLUE), 1, {0}};
patterns[num_patterns++] = (PatternColorPair) {"//.*$", COLOR_PAIR(COLOR_YELLOW_ON_BLUE), 0, {0}};
patterns[num_patterns++] = (PatternColorPair) {"\\b(auto|break|case|char|const|continue|default|do|double|else|enum|extern|float|for|goto|if|int|long|register|return|short|signed|sizeof|static|struct|switch|typedef|union|unsigned|void|volatile|while|asm|inline|wchar_t|[.][.][.])\\b", COLOR_PAIR(COLOR_YELLOW_ON_BLUE), 1, {0}};
patterns[num_patterns++] = (PatternColorPair) {"!|%|==|!=|&&|[*]|->|[+]|-|[|][|]|=|>|<|/", COLOR_PAIR(COLOR_YELLOW_ON_BLUE), 1, {0}};
patterns[num_patterns++] = (PatternColorPair) {"[(){},:?]|\\[|\\]", COLOR_PAIR(COLOR_CYAN_ON_BLUE), 0, {0}};
patterns[num_patterns++] = (PatternColorPair) {"[;&^~|]", COLOR_PAIR(COLOR_MAGENTA_ON_BLUE), 1, {0}};
}
if ((file_type && (strcmp(file_type, ".sh") == 0)) || (lines != NULL && lines->line_length > 3 && strncmp(lines->line, "#!/", 3) == 0)) {
patterns[num_patterns++] = (PatternColorPair) {"^#!/.*", COLOR_PAIR(COLOR_CYAN_ON_BLACK), 0, {0}};
patterns[num_patterns++] = (PatternColorPair) {"#.*$", COLOR_PAIR(COLOR_YELLOW_ON_BLUE), 0, {0}};
patterns[num_patterns++] = (PatternColorPair) {"[;{}]", COLOR_PAIR(COLOR_CYAN_ON_BLUE), 1, {0}};
patterns[num_patterns++] = (PatternColorPair) {"\\$[(].*[)]|\\$[{].*[}]", COLOR_PAIR(COLOR_GREEN_ON_BLUE), 0, {0}};
patterns[num_patterns++] = (PatternColorPair) {"\\$[*]|\\$@|\\$#|\\$[?]|\\$-|\\$\\$|\\$!|\\$_", COLOR_PAIR(COLOR_RED_ON_BLUE), 1, {0}};
patterns[num_patterns++] = (PatternColorPair) {"2>&1|1>&2|2>|1>", COLOR_PAIR(COLOR_RED_ON_BLUE), 1, {0}};
patterns[num_patterns++] = (PatternColorPair) {"\\$[0123456789]", COLOR_PAIR(COLOR_RED_ON_BLUE), 1, {0}};
patterns[num_patterns++] = (PatternColorPair) {"\\$[a-zA-Z0-9_]+", COLOR_PAIR(COLOR_GREEN_ON_BLUE), 1, {0}};
patterns[num_patterns++] = (PatternColorPair) {"\\$", COLOR_PAIR(COLOR_GREEN_ON_BLUE), 1, {0}};
patterns[num_patterns++] = (PatternColorPair) {"\\bfunction\\b.*[(][)]", COLOR_PAIR(COLOR_MAGENTA_ON_BLUE), 1, {0}};
patterns[num_patterns++] = (PatternColorPair) {"[a-zA-Z0-9_]+[(][)]", COLOR_PAIR(COLOR_MAGENTA_ON_BLUE), 1, {0}};
patterns[num_patterns++] = (PatternColorPair) {"\\b(break|case|clear|continue|declare|done|do|echo|elif|else|esac|exit|export|fi|for|getopts|if|in|read|return|select|set|shift|source|then|trap|until|unset|wait|while)\\b", COLOR_PAIR(COLOR_YELLOW_ON_BLUE), 1, {0}};
}
for (int i = 0; i < num_patterns; i++) {
regcomp(&patterns[i].regex, patterns[i].pattern, REG_EXTENDED);
}
}
// Byte positions identify edits; terminal columns are derived only for display/navigation.
int cursor_byte=0;
while (1)
{
file_lines *current_line=lines;
off_t seek=0;
for (int i=0; i < cursor_row; i++)
{
seek+=current_line->line_length+1;
current_line=current_line->next;
}
int column=text_column(current_line->line, cursor_byte);
// Insertions and line joins can attach existing combining marks to the preceding character.
cursor_byte=text_offset(current_line->line, current_line->line_length, column);
if (editor_mode)
{
if (cursor_row < screen_start_line) screen_start_line=cursor_row;
if (cursor_row >= screen_start_line+max_y-2) screen_start_line=cursor_row-max_y+3;
if (column < screen_start_col) screen_start_col=column;
wchar_t chars[CCHARW_MAX];
int cursor_width=1;
if (cursor_byte < current_line->line_length)
text_cell(current_line->line+cursor_byte, current_line->line_length-cursor_byte, chars, &cursor_width);
if (column+cursor_width > screen_start_col+max_x) screen_start_col=column+cursor_width-max_x;
}
werase(content_win);
file_lines *shown=lines;
for (int i=0; i < screen_start_line; i++) shown=shown->next;
int shown_rows=0;
for (; shown && shown_rows < max_y-2; shown_rows++, shown=shown->next)
{
wmove(content_win, shown_rows, 0);
display_line(content_win, shown, max_x, screen_start_col, editor_mode, patterns, num_patterns);
}
werase(toprow_win);
if (editor_mode)
{
char charcode[16];
if (cursor_byte < current_line->line_length)
{
wchar_t chars[CCHARW_MAX];
int width;
text_cell(current_line->line+cursor_byte, current_line->line_length-cursor_byte, chars, &width);
snprintf(charcode, sizeof(charcode), "U+%04X", (unsigned int)chars[0]);
}
else snprintf(charcode, sizeof(charcode), "%s", current_line->next ? "#10" : "<EOF>");
mvwprintw(toprow_win, 0, 0, "%s [-%s--] %3d L:[%3d+%3d %3d/%3lld] *(%4lld/%lldb) %s",
filename, is_modified ? "M" : "-", column, screen_start_line+1, cursor_row-screen_start_line,
cursor_row+1, (long long)num_lines, (long long)(seek+cursor_byte), (long long)num_bytes, charcode);
}
else
{
int last=screen_start_line+shown_rows;
mvwprintw(toprow_win, 0, 0, "%s", filename);
int width=snprintf(NULL, 0, " %d/%lld %lld%%", last, (long long)num_lines, (long long)(100*last/num_lines));
mvwprintw(toprow_win, 0, max_x-width, " %d/%lld %lld%%", last, (long long)num_lines, (long long)(100*last/num_lines));
}
wnoutrefresh(toprow_win);
if (editor_mode) wmove(content_win, cursor_row-screen_start_line, column-screen_start_col);
wnoutrefresh(content_win);
refresh_screen(editor_mode);
char input_text[MB_LEN_MAX+1];
int input=read_text_key(content_win, input_text);
int target_column=-1;
switch (input)
{
case KEY_F(3):
if (editor_mode) break;
goto close_editor;
case KEY_F(10):
case 27:
if (is_modified)
{
int button=show_dialog(SPRINTF("File %s was modified.\nSave before close?", filename), (char *[]) {"Yes", "No", "Cancel", NULL}, 2, NULL, 0, 0, 0);
if (button != 1 && button != 2) break;
if (button == 1 && write_file_lines(filename, lines) != 0)
{
show_errormsg(SPRINTF("Cannot save file:\n%s\n%s", filename, strerror(errno)));
break;
}
}
goto close_editor;
case KEY_F(2):
if (editor_mode && show_dialog(SPRINTF("Confirm save file:\n%s", filename), (char *[]) {"Save", "Cancel", NULL}, 0, NULL, 0, 0, 0) == 1)
{
if (write_file_lines(filename, lines) != 0)
show_errormsg(SPRINTF("Cannot save file:\n%s\n%s", filename, strerror(errno)));
else is_modified=0;
}
break;
case KEY_F(7):
case KEY_SHIFT_F7:
{
if ((!find_str[0] || input == KEY_F(7)) && show_dialog("Enter search string:", (char *[]) {"Find", "Cancel", NULL}, 0, find_str, 0, 0, 0) != 1) break;
if (!find_str[0]) break;
file_lines *search_line=current_line;
int found_row=cursor_row, offset=cursor_byte, found=-1;
wchar_t chars[CCHARW_MAX];
int width;
if (offset < search_line->line_length)
offset+=text_cell(search_line->line+offset, search_line->line_length-offset, chars, &width);
else offset++;
while (search_line)
{
for (; offset < search_line->line_length; offset+=text_cell(search_line->line+offset, search_line->line_length-offset, chars, &width))
if (text_matches(search_line->line+offset, search_line->line_length-offset, find_str))
{
found=offset;
break;
}
if (found >= 0) break;
search_line=search_line->next;
found_row++;
offset=0;
}
if (found < 0) show_dialog("Search string not found", (char *[]) {"OK", NULL}, 0, NULL, 0, 0, 0);
else
{
cursor_row=found_row;
cursor_byte=found;
screen_start_line=found_row;
screen_start_col=text_column(search_line->line, found);
}
break;
}
case KEY_UP:
case KEY_DOWN:
case KEY_PPAGE:
case KEY_NPAGE:
{
int step=(input == KEY_PPAGE || input == KEY_NPAGE) ? max_y-2 : 1;
if (input == KEY_UP || input == KEY_PPAGE) step=-step;
if (editor_mode)
{
cursor_row+=step;
if (cursor_row < 0) cursor_row=0;
if (cursor_row >= num_lines) cursor_row=num_lines-1;
target_column=column;
}
else
{
screen_start_line+=step;
int last=num_lines > max_y-2 ? num_lines-max_y+2 : 0;
if (screen_start_line > last) screen_start_line=last;
if (screen_start_line < 0) screen_start_line=0;
cursor_row=screen_start_line;
cursor_byte=0;
}
break;
}
case KEY_LEFT:
if (!editor_mode)
{
screen_start_col=screen_start_col > 10 ? screen_start_col-10 : 0;
break;
}
if (cursor_byte) cursor_byte=text_previous(current_line->line, cursor_byte);
else if (cursor_row > 0)
{
cursor_row--;
target_column=INT_MAX;
}
break;
case KEY_RIGHT:
if (!editor_mode) screen_start_col+=10;
else if (cursor_byte < current_line->line_length)
{
wchar_t chars[CCHARW_MAX];
int width;
cursor_byte+=text_cell(current_line->line+cursor_byte, current_line->line_length-cursor_byte, chars, &width);
}
else if (current_line->next)
{
cursor_row++;
cursor_byte=0;
}
break;
case KEY_HOME:
if (editor_mode) cursor_byte=0;
else screen_start_line=cursor_row=cursor_byte=0;
break;
case KEY_END:
if (editor_mode) cursor_byte=current_line->line_length;
else
{
screen_start_line=num_lines > max_y-2 ? num_lines-max_y+2 : 0;
cursor_row=screen_start_line;
cursor_byte=0;
}
break;
case KEY_RESIZE:
getmaxyx(stdscr, max_y, max_x);
wresize(toprow_win, 1, max_x);
wresize(content_win, max_y-2, max_x);
break;
case '\n':
case '\r':
if (editor_mode)
{
file_lines *next=malloc(sizeof(*next));
next->line_length=current_line->line_length-cursor_byte;
next->line=malloc(next->line_length+1);
memcpy(next->line, current_line->line+cursor_byte, next->line_length);
next->next=current_line->next;
current_line->next=next;
current_line->line_length=cursor_byte;
cursor_row++;
cursor_byte=0;
num_lines++;
num_bytes++;
is_modified=1;
}
break;
case KEY_BACKSPACE:
if (!editor_mode) break;
if (cursor_byte > 0)
{
int previous=text_previous(current_line->line, cursor_byte);
memmove(current_line->line+previous, current_line->line+cursor_byte, current_line->line_length-cursor_byte);
current_line->line_length-=cursor_byte-previous;
num_bytes-=cursor_byte-previous;
cursor_byte=previous;
is_modified=1;
}
else if (cursor_row > 0)
{
file_lines *previous=lines;
for (int i=0; i < cursor_row-1; i++) previous=previous->next;
cursor_byte=previous->line_length;
previous->line=realloc(previous->line, cursor_byte+current_line->line_length+1);
memcpy(previous->line+cursor_byte, current_line->line, current_line->line_length);
previous->line_length+=current_line->line_length;
previous->next=current_line->next;
free(current_line->line);
free(current_line);
cursor_row--;
num_lines--;
num_bytes--;
is_modified=1;
}
break;
case KEY_DC:
if (!editor_mode) break;
if (cursor_byte < current_line->line_length)
{
wchar_t chars[CCHARW_MAX];
int width, bytes=text_cell(current_line->line+cursor_byte, current_line->line_length-cursor_byte, chars, &width);
memmove(current_line->line+cursor_byte, current_line->line+cursor_byte+bytes, current_line->line_length-cursor_byte-bytes);
current_line->line_length-=bytes;
num_bytes-=bytes;
is_modified=1;
}
else if (current_line->next)
{
file_lines *next=current_line->next;
current_line->line=realloc(current_line->line, current_line->line_length+next->line_length+1);
memcpy(current_line->line+current_line->line_length, next->line, next->line_length);
current_line->line_length+=next->line_length;
current_line->next=next->next;
free(next->line);
free(next);
num_lines--;
num_bytes--;
is_modified=1;
}
break;
case KEY_MOUSE:
{
MEVENT event;
if (!editor_mode || getmouse(&event) != OK) break;
if (event.y < 1 || event.y >= max_y-1 || event.x < 0 || event.x >= max_x) break;
cursor_row=screen_start_line+event.y-1;
if (cursor_row >= num_lines) cursor_row=num_lines-1;
target_column=screen_start_col+event.x;
break;
}
default:
if (input == '\t') strcpy(input_text, "\t");
if (editor_mode && input_text[0])
{
int bytes=strlen(input_text);
current_line->line=realloc(current_line->line, current_line->line_length+bytes+1);
memmove(current_line->line+cursor_byte+bytes, current_line->line+cursor_byte, current_line->line_length-cursor_byte);
memcpy(current_line->line+cursor_byte, input_text, bytes);
current_line->line_length+=bytes;
cursor_byte+=bytes;
num_bytes+=bytes;
is_modified=1;
}
break;
}
if (target_column >= 0)
{
current_line=lines;
for (int i=0; i < cursor_row; i++) current_line=current_line->next;
cursor_byte=text_offset(current_line->line, current_line->line_length, target_column);
}
}
close_editor:
delwin(toprow_win);
delwin(content_win);
free_file_lines(lines);
free_pattern_regexes(patterns, num_patterns);
return 0;
}
int view_file(char *filename) {
return view_edit_file(filename, 0);
}
int edit_file(char *filename) {
return view_edit_file(filename, 1);
}