Repository navigation
Expand file tree
/
Copy pathview_edit.c
More file actions
683 lines (581 loc) · 25.1 KB
/
Copy pathview_edit.c
File metadata and controls
683 lines (581 loc) · 25.1 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
#include <ncurses.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/utsname.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <sys/time.h>
#include <ctype.h>
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.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;
}
}
int write_file_lines(const char *filename, file_lines *lines) {
char temp_filename[strlen(filename) + 10]; // Space for ".tmpN\0"
int counter = 0;
do {
snprintf(temp_filename, sizeof(temp_filename), "%s.tmp%d", filename, counter++);
} while (access(temp_filename, F_OK) != -1);
int fd = open(temp_filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1) return -1;
file_lines *current = lines;
while (current) {
write(fd, current->line, current->line_length);
if (current->next) write(fd, "\n", 1);
current = current->next;
}
close(fd);
return rename(temp_filename, filename) == 0 ? 0 : -1;
}
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(0);
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);
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);
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 display_line(WINDOW *win, file_lines *line, int max_x, int current_col, int editor_mode) {
char *ptr = line->line;
int offset = 0;
while (offset < line->line_length && offset < current_col) {
ptr++;
offset++;
}
if (offset < current_col) {
// If the line is shorter than current_col, just clear the line
wclrtoeol(win);
} else {
for (int x = 0; x < max_x && offset < line->line_length; x++, ptr++, offset++) {
if (isprint((unsigned char)*ptr)) { // Check if the character is printable
waddch(win, *ptr);
} else if (*ptr != '\n') {
if (*ptr == 9) {
wattron(win, COLOR_PAIR(COLOR_CYAN_ON_BLUE));
waddch(win, '>');
wattron(win, COLOR_PAIR(COLOR_WHITE_ON_BLUE));
} else {
if (editor_mode) {
wattron(win, COLOR_PAIR(COLOR_WHITE_ON_RED));
waddch(win, *ptr >= 0 && *ptr < 32 ? '@' + *ptr : '.');
wattron(win, COLOR_PAIR(COLOR_WHITE_ON_BLUE));
} else {
waddch(win, '.'); // If not, print a "." in viewer mode
}
}
}
}
}
wrefresh(win);
}
int view_edit_file(char *filename, int editor_mode) {
int input;
int max_y, max_x;
int screen_start_line = 0;
int screen_start_col = 0; // Add a variable to keep track of the current column offset
int cursor_row = 0;
int cursor_col = 0;
int skip_refresh = 0;
int is_modified = 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);
werase(content_win); // Clear the window
wbkgd(content_win, COLOR_PAIR(COLOR_WHITE_ON_BLUE)); // Set the background color
wrefresh(content_win); // Refresh the window to apply the changes
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);
// Initial display
file_lines *current = lines;
for (int i = 0; i < max_y - 2 && current != NULL; i++) {
wmove(content_win, i, 0);
display_line(content_win, current, max_x, screen_start_col, editor_mode); // Pass screen_start_col to display_line
current = current->next;
}
current = lines;
// Handle user input for scrolling
while(1) {
int seek = 0;
// globally get current line, since it may be used on many places later
file_lines *current_line = lines;
for (int i = 0; i < screen_start_line + cursor_row; i++) {
seek += current_line->line_length + 1;
current_line = current_line->next;
}
int shown_line_max = screen_start_line + max_y - 2;
if (shown_line_max > num_lines) shown_line_max = num_lines;
int absolute_cursor_col = cursor_col + screen_start_col;
int absolute_cursor_row = cursor_row + screen_start_line;
char charcode[10] = {0};
// Initial top row stats
if (editor_mode) {
if (absolute_cursor_col < current_line->line_length) {
unsigned char current_char = current_line->line[absolute_cursor_col];
sprintf(charcode, "#%d", (int)current_char);
} else if (seek + absolute_cursor_col >= num_bytes) {
sprintf(charcode, "<EOF>");
} else sprintf(charcode, "#10");
mvwprintw(toprow_win, 0, 0, "%s [-%s--] %3d L:[%3d+%3d %3d/%3d] *(%4d/%db) %s ", filename, is_modified ? "M" : "-", absolute_cursor_col, screen_start_line + 1, cursor_row, absolute_cursor_row + 1, num_lines, seek + absolute_cursor_col, num_bytes, charcode);
} else {
mvwprintw(toprow_win, 0, 0, "%s", filename);
int num_width = snprintf(NULL, 0, " %d/%d %d%%", shown_line_max, num_lines, num_lines > 0 ? 100 * shown_line_max / num_lines : 100);
mvwprintw(toprow_win, 0, max_x - num_width, " %d/%d %d%%", shown_line_max, num_lines, num_lines > 0 ? 100 * shown_line_max / num_lines : 100);
}
wrefresh(toprow_win);
if (editor_mode) {
curs_set(1); // Make cursor visible
} else {
curs_set(0); // Hide cursor
}
move(cursor_row + 1, cursor_col);
skip_refresh = 0;
input = noesc(getch());
switch (input) {
case KEY_F(3):
if (editor_mode) {
// TODO: perhaps some Mark function
} else {
delwin(content_win);
free_file_lines(lines);
curs_set(1);
return 0;
}
break;
case KEY_F(10):
case 27:
{
if (is_modified) {
int btn = show_dialog(SPRINTF("File %s was modified.\nSave before close?", filename), (char *[]) {"Yes", "No", "Cancel", NULL}, 2, NULL, 0, 0);
if (btn == 1) {
write_file_lines(filename, lines);
}
if (btn != 1 && btn != 2) { // continue editing
break;
}
}
delwin(content_win);
free_file_lines(lines);
curs_set(1);
return 0;
<
10BC8
div>
}
case KEY_F(2):
{
int btn = show_dialog(SPRINTF("Confirm save file:\n%s", filename), (char *[]) {"Save", "Cancel", NULL}, 0, NULL, 0, 0);
if (btn == 1) {
write_file_lines(filename, lines);
is_modified = 0;
}
break;
}
case KEY_UP:
if (editor_mode && cursor_row > 0) {
cursor_row--;
// TODO fix cursor_col position
skip_refresh = 1;
} else {
if (screen_start_line > 0) {
screen_start_line--;
}
}
break;
case KEY_DOWN:
if (editor_mode && cursor_row < max_y - 3 && absolute_cursor_row < num_lines - 1) {
cursor_row++;
skip_refresh = 1;
} else {
if (screen_start_line < num_lines - (max_y - 2)) {
screen_start_line++;
}
}
break;
case KEY_LEFT:
if (editor_mode) {
if (cursor_col > 0) {
cursor_col--;
skip_refresh = 1;
} else if (screen_start_col > 0) {
screen_start_col--;
} else if (cursor_row > 0) {
cursor_row--;
file_lines *prev_line = lines;
for (int i = 0; i < screen_start_line + cursor_row; i++) {
prev_line = prev_line->next;
}
cursor_col = prev_line->line_length;
if (prev_line->line[prev_line->line_length - 1] == '\n') cursor_col--;
if (cursor_col < 0) cursor_col = 0;
if (cursor_col >= max_x) {
screen_start_col = cursor_col - max_x + 1;
cursor_col = max_x - 1;
} else {
screen_start_col = 0;
}
} else if (screen_start_line > 0) {
screen_start_line--;
file_lines *prev_line = lines;
for (int i = 0; i < screen_start_line; i++) {
prev_line = prev_line->next;
}
cursor_col = prev_line->line_length;
if (prev_line->line[prev_line->line_length - 1] == '\n') cursor_col--;
if (cursor_col < 0) cursor_col = 0;
if (cursor_col >= max_x) {
screen_start_col = cursor_col - max_x + 1;
cursor_col = max_x - 1;
} else {
screen_start_col = 0;
}
}
} else {
if (screen_start_col > 0) screen_start_col -= 10;
if (screen_start_col < 0) screen_start_col = 0;
}
break;
case KEY_RIGHT:
if (editor_mode) {
if (absolute_cursor_col < current_line->line_length) {
if (cursor_col < max_x - 1) {
cursor_col++;
} else {
screen_start_col++;
}
} else if (current_line->next) {
cursor_col = 0;
if (cursor_row < max_y - 3) {
cursor_row++;
} else {
screen_start_line++;
}
screen_start_col = 0; // Reset horizontal scrolling when moving to a new line
}
} else {
screen_start_col += 10;
}
break;
case KEY_PPAGE: // PgUp
if (editor_mode && screen_start_line == 0) {
cursor_row = 0; // Move cursor to the first line
}
screen_start_line -= max_y - 2;
if (screen_start_line < 0) screen_start_line = 0;
break;
case KEY_NPAGE: // PgDn
if (editor_mode && screen_start_line + max_y > num_lines - 2) {
cursor_row = num_lines - screen_start_line - 1; // Move cursor to the last line
if (cursor_row > max_y - 3) cursor_row = max_y - 3;
}
screen_start_line += max_y - 2;
if (screen_start_line > num_lines - (max_y - 2)) {
screen_start_line = num_lines - (max_y - 2);
}
if (screen_start_line < 0) screen_start_line = 0;
break;
case KEY_HOME: // Handle Home key
if (editor_mode) {
cursor_col = 0;
screen_start_col = 0;
} else {
screen_start_line = 0;
}
break;
case KEY_END: // Handle End key
if (editor_mode) {
cursor_col = current_line->line_length;
if (cursor_col >= max_x) {
screen_start_col = cursor_col - max_x + 1;
cursor_col = max_x - 1;
}
} else {
screen_start_line = num_lines - (max_y - 2);
if (screen_start_line < 0) screen_start_line = 0;
}
break;
case KEY_RESIZE: // Handle screen resize
getmaxyx(stdscr, max_y, max_x); // Update max_y and max_x
wclear(content_win); // Clear the old window
wrefresh(content_win);
delwin(content_win); // Delete the old window
content_win = newwin(max_y - 2, max_x, 1, 0); // Create a new window with the new dimensions
wbkgd(content_win, COLOR_PAIR(COLOR_WHITE_ON_BLUE));
wattron(content_win, COLOR_PAIR(COLOR_WHITE_ON_BLUE));
break;
case '\n': // Enter key
{
if (editor_mode) {
is_modified = 1;
// Split the current line into two at the cursor's position
char *first_half = malloc(absolute_cursor_col + 1);
char *second_half = malloc(current_line->line_length - absolute_cursor_col + 1);
memcpy(first_half, current_line->line, absolute_cursor_col);
first_half[absolute_cursor_col] = '\0';
memcpy(second_half, ¤t_line->line[absolute_cursor_col], current_line->line_length - absolute_cursor_col);
second_half[current_line->line_length - absolute_cursor_col] = '\0';
// Free the original line memory
free(current_line->line);
// Adjust the linked list to accommodate the new line
file_lines *new_line = malloc(sizeof(file_lines));
new_line->line = second_half;
new_line->line_length = current_line->line_length - absolute_cursor_col;
new_line->next = current_line->next;
current_line->next = new_line;
current_line->line = first_half;
current_line->line_length = absolute_cursor_col;
num_lines++;
num_bytes++;
// Move the cursor to the beginning of the next line
if (cursor_row < max_y - 3) {
cursor_row++;
cursor_col = 0;
screen_start_col = 0; // Reset horizontal scrolling when moving to a new line
} else {
screen_start_line++;
cursor_col = 0;
screen_start_col = 0; // Reset horizontal scrolling when moving to a new line
}
}
}
break;
case KEY_BACKSPACE: // Handle Backspace key
{
if (editor_mode) {
is_modified = 1;
if (absolute_cursor_col > 0) {
// Remove the character to the left of the cursor
memmove(¤t_line->line[absolute_cursor_col - 1], ¤t_line->line[absolute_cursor_col], current_line->line_length - absolute_cursor_col);
current_line->line_length--;
char *new_line = realloc(current_line->line, current_line->line_length);
current_line->line = new_line;
num_bytes--;
// Move the cursor to the left
if (cursor_col > 0) {
cursor_col--;
} else {
screen_start_col--;
}
} else if (cursor_row > 0) {
// Merge the current line with the previous line
file_lines *prev_line = lines;
for (int i = 0; i < screen_start_line + cursor_row - 1; i++) {
prev_line = prev_line->next;
}
int original_prev_line_length = prev_line->line_length; // Store the original length before the merge
int new_length = prev_line->line_length + current_line->line_length;
char *merged_line = realloc(prev_line->line, new_length);
memcpy(&merged_line[prev_line->line_length], current_line->line, current_line->line_length);
prev_line->line = merged_line;
prev_line->line_length = new_length;
prev_line->next = current_line->next;
free(current_line->line);
free(current_line);
current_line = prev_line;
num_lines--;
num_bytes--;
cursor_row--;
cursor_col = original_prev_line_length - screen_start_col;
} else {
is_modified = 0;
}
}
}
break;
case KEY_DC: // Handle Delete key
{
if (editor_mode) {
is_modified = 1;
if (absolute_cursor_col < current_line->line_length) {
// Remove the character at the cursor position
memmove(¤t_line->line[absolute_cursor_col], ¤t_line->line[absolute_cursor_col + 1], current_line->line_length - absolute_cursor_col - 1);
current_line->line_length--;
char *new_line = realloc(current_line->line, current_line->line_length);
current_line->line = new_line;
num_bytes--;
} else if (current_line->next) {
// Merge the current line with the next line
int new_length = current_line->line_length + current_line->next->line_length;
char *merged_line = realloc(current_line->line, new_length);
memcpy(&merged_line[current_line->line_length], current_line->next->line, current_line->next->line_length);
current_line->line = merged_line;
current_line->line_length = new_length;
file_lines *temp = current_line->next;
current_line->next = temp->next;
free(temp->line);
free(temp);
num_lines--;
num_bytes--;
} else {
is_modified = 0;
}
}
}
break;
}
if (input == KEY_MOUSE && editor_mode) {
MEVENT event;
if (getmouse(&event) == OK) {
// Mouse clicked at event.y and event.x
// Adjust based on screen start lines and columns due to scrolling
int target_line = event.y - 1 + screen_start_line; // -1 to account for top row
int target_col = event.x + screen_start_col;
// Update cursor_row and cursor_col with the target values.
cursor_row = event.y - 1;
cursor_col = event.x;
// Don't move cursor beyond the last line or beyond the line length
if (target_line >= num_lines) {
cursor_row = num_lines - screen_start_line - 1;
}
}
}
// insert character where it belongs
if (editor_mode && (isprint(input) || input == 9)) {
is_modified = 1; num_bytes++;
// Reallocate memory for the new character
char *new_line = realloc(current_line->line, current_line->line_length + 1);
current_line->line = new_line;
memmove(¤t_line->line[absolute_cursor_col + 1], ¤t_line->line[absolute_cursor_col], current_line->line_length - absolute_cursor_col);
current_line->line[absolute_cursor_col] = input;
current_line->line_length++;
// Move the cursor to the right after inserting the character
if (cursor_col < max_x - 1) {
cursor_col++;
} else {
screen_start_col++;
}
}
if (editor_mode) {
// re-get current line, since it may have changed
file_lines *current_line = lines;
for (int i = 0; i < screen_start_line + cursor_row; i++) {
current_line = current_line->next;
}
// recalculate absolutes
absolute_cursor_col = cursor_col + screen_start_col;
// Don't move cursor beyond the end of the line
if (absolute_cursor_col > current_line->line_length) {
cursor_col = current_line->line_length - screen_start_col;
if (cursor_col < 0) {
screen_start_col = current_line->line_length;
cursor_col = 0;
}
absolute_cursor_col = cursor_col + screen_start_col;
skip_refresh = 0; // Force a refresh to update cursor position
}
}
if (!skip_refresh) {
curs_set(0); // Hide cursor
// Update the current pointer based on screen_start_line
current = lines;
for (int i = 0; i < screen_start_line; i++) {
current = current->next;
}
// Redisplay the window content
file_lines *temp = current;
int i;
for (i = 0; i < max_y - 2 && temp != NULL; i++) {
wmove(content_win, i, 0);
wclrtoeol(content_win);
display_line(content_win, temp, max_x, screen_start_col, editor_mode);
temp = temp->next;
}
// Clear any remaining lines on the screen
for (; i < max_y - 2; i++) {
wmove(content_win, i, 0);
wclrtoeol(content_win);
}
}
if (editor_mode) {
wmove(content_win, cursor_row, cursor_col);
}
}
return 0;
}
int view_file(char *filename) {
return view_edit_file(filename, 0);
}
int edit_file(char *filename) {
return view_edit_file(filename, 1);
}