Change formatting rules
All checks were successful
Build ISO image / build-and-deploy (push) Successful in 2m7s
Build documentation / build-and-deploy (push) Successful in 39s

This commit is contained in:
2026-04-24 01:54:48 +02:00
parent 34f7809a2d
commit c8fb575bdd
208 changed files with 6310 additions and 6339 deletions

198
ce/edit.c
View File

@@ -22,9 +22,9 @@
#include <tgraphics.h>
#include <tscreen.h>
#define LINE_NUMBERS_STYLE ANSIQ_GR_SEQ (ANSIQ_BG_RGB (17, 115, 176) ANSIQ_FG_WHITE)
#define LINE_NUMBERS_STYLE ANSIQ_GR_SEQ(ANSIQ_BG_RGB(17, 115, 176) ANSIQ_FG_WHITE)
#define STATUS_LINE_STYLE ANSIQ_GR_SEQ (ANSIQ_BG_RGB (240, 191, 88) ANSIQ_FG_BLACK)
#define STATUS_LINE_STYLE ANSIQ_GR_SEQ(ANSIQ_BG_RGB(240, 191, 88) ANSIQ_FG_BLACK)
#define TAB_INSERT " "
@@ -69,22 +69,22 @@ struct editor {
static struct editor editor;
static bool prepare_lines_cb (void* ctx, const char* start, size_t len) {
static bool prepare_lines_cb(void* ctx, const char* start, size_t len) {
(void)ctx;
struct edit_line* line = malloc (sizeof (*line));
struct edit_line* line = malloc(sizeof(*line));
if (line == NULL)
return false;
memset (line, 0, sizeof (*line));
memset(line, 0, sizeof(*line));
size_t init_len = len > 0 ? len : 32;
in_gb_init (&wmalloc, NULL, &line->gb, init_len);
in_gb_init(&wmalloc, NULL, &line->gb, init_len);
for (size_t chr = 0; chr < len; chr++)
in_gb_insert (&wrealloc, NULL, &line->gb, start[chr]);
in_gb_insert(&wrealloc, NULL, &line->gb, start[chr]);
list_append (editor.lines, &line->lines_link);
list_append(editor.lines, &line->lines_link);
if (editor.current_line == NULL)
editor.current_line = line;
@@ -94,14 +94,14 @@ static bool prepare_lines_cb (void* ctx, const char* start, size_t len) {
return true;
}
static void prepare_lines (const char* text, size_t len) {
static void prepare_lines(const char* text, size_t len) {
if (len == 0)
str_split_lines ("\n", 1, NULL, &prepare_lines_cb);
str_split_lines("\n", 1, NULL, &prepare_lines_cb);
else
str_split_lines (text, len, NULL, &prepare_lines_cb);
str_split_lines(text, len, NULL, &prepare_lines_cb);
}
static void update_horz_scroll (size_t screen_cols) {
static void update_horz_scroll(size_t screen_cols) {
if (editor.cursor.col < editor.col_offset)
editor.col_offset = editor.cursor.col;
@@ -109,7 +109,7 @@ static void update_horz_scroll (size_t screen_cols) {
editor.col_offset = editor.cursor.col - screen_cols + 1;
}
static void update_vert_scroll (size_t screen_rows) {
static void update_vert_scroll(size_t screen_rows) {
if (editor.cursor.line < editor.row_offset)
editor.row_offset = editor.cursor.line;
@@ -117,7 +117,7 @@ static void update_vert_scroll (size_t screen_rows) {
editor.row_offset = editor.cursor.line - screen_rows + 1;
}
static size_t count_digits (size_t n) {
static size_t count_digits(size_t n) {
if (n == 0)
return 1;
@@ -130,43 +130,43 @@ static size_t count_digits (size_t n) {
return count;
}
void edit_start (const char* volume, const char* file_path, const char* text, size_t text_len) {
void edit_start(const char* volume, const char* file_path, const char* text, size_t text_len) {
editor.volume = volume;
editor.path = file_path;
mprintf (ANSIQ_SCR_SAVE);
mprintf(ANSIQ_SCR_SAVE);
prepare_lines (text, text_len);
prepare_lines(text, text_len);
struct arena temp_arena;
memset (&temp_arena, 0, sizeof (temp_arena));
memset(&temp_arena, 0, sizeof(temp_arena));
size_t cols, rows;
terminal_dimensions (&cols, &rows);
terminal_dimensions(&cols, &rows);
mprintf (ANSIQ_CUR_INVISIBLE);
mprintf(ANSIQ_CUR_INVISIBLE);
bool edit_run = true;
while (edit_run) {
size_t gutter_width = count_digits (editor.total_lines);
size_t gutter_width = count_digits(editor.total_lines);
size_t effective_cols = (cols > gutter_width) ? cols - gutter_width : cols;
update_horz_scroll (effective_cols);
update_vert_scroll (rows - 1);
update_horz_scroll(effective_cols);
update_vert_scroll(rows - 1);
const size_t backbuffer_max = 256 * 1024;
char* backbuffer = arena_malloc (&temp_arena, backbuffer_max);
char* backbuffer = arena_malloc(&temp_arena, backbuffer_max);
char* bbptr = backbuffer;
memcpy (bbptr, ANSIQ_CUR_HOME, sizeof (ANSIQ_CUR_HOME) - 1);
bbptr += sizeof (ANSIQ_CUR_HOME) - 1;
memcpy(bbptr, ANSIQ_CUR_HOME, sizeof(ANSIQ_CUR_HOME) - 1);
bbptr += sizeof(ANSIQ_CUR_HOME) - 1;
size_t lines_drawn = 0;
size_t current_idx = 0;
struct list_node_link *line_link, *tmp_line_link;
list_foreach (editor.lines, line_link, tmp_line_link) {
list_foreach(editor.lines, line_link, tmp_line_link) {
if (current_idx < editor.row_offset) {
current_idx++;
continue;
@@ -175,11 +175,11 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
if (lines_drawn >= rows - 1)
break;
struct edit_line* line = list_entry (line_link, struct edit_line, lines_link);
struct edit_line* line = list_entry(line_link, struct edit_line, lines_link);
bbptr += snprintf (bbptr, (backbuffer_max - (bbptr - backbuffer)),
ANSIQ_SCR_CLR_LINE LINE_NUMBERS_STYLE "%*zu" ANSIQ_GR_RESET,
(int)gutter_width, current_idx + 1);
bbptr += snprintf(bbptr, (backbuffer_max - (bbptr - backbuffer)),
ANSIQ_SCR_CLR_LINE LINE_NUMBERS_STYLE "%*zu" ANSIQ_GR_RESET,
(int)gutter_width, current_idx + 1);
size_t part1_len = line->gb.gap_start;
size_t part2_len = line->gb.size - line->gb.gap_end;
@@ -193,7 +193,7 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
if (len > effective_cols)
len = effective_cols;
memcpy (bbptr, &line->gb.buffer[start], len);
memcpy(bbptr, &line->gb.buffer[start], len);
bbptr += len;
current_col += len;
}
@@ -209,12 +209,12 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
if (current_col + len > effective_cols)
len = effective_cols - current_col;
memcpy (bbptr, &line->gb.buffer[line->gb.gap_end + skip], len);
memcpy(bbptr, &line->gb.buffer[line->gb.gap_end + skip], len);
bbptr += len;
}
}
bbptr += snprintf (bbptr, (backbuffer_max - (bbptr - backbuffer)), ANSIQ_SCR_CLR2END "\n");
bbptr += snprintf(bbptr, (backbuffer_max - (bbptr - backbuffer)), ANSIQ_SCR_CLR2END "\n");
lines_drawn++;
current_idx++;
@@ -222,7 +222,7 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
char* command_str = (editor.mode == EDIT_MODE_COMAMND) ? editor.cmdbuf.buffer : "";
bbptr += snprintf (
bbptr += snprintf(
bbptr, (backbuffer_max - (bbptr - backbuffer)),
ANSIQ_DYN_CUR_SET ANSIQ_SCR_CLR_LINE STATUS_LINE_STYLE
"%*s" ANSIQ_DYN_CUR_SET
@@ -232,35 +232,35 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
(int)(editor.cursor.line - editor.row_offset) + 1,
(int)(editor.cursor.col - editor.col_offset) + 1 + (int)gutter_width);
stream_write (process_get_pgid (), STREAM_OUT, backbuffer, bbptr - backbuffer);
stream_write(process_get_pgid(), STREAM_OUT, backbuffer, bbptr - backbuffer);
uint8_t ch = 0;
for (;;) {
if (stream_read (process_get_pgid (), STREAM_IN, &ch, 1) > 0)
if (stream_read(process_get_pgid(), STREAM_IN, &ch, 1) > 0)
break;
else
sched ();
sched();
}
switch (ch) {
case '\b':
if (editor.mode == EDIT_MODE_NORMAL) {
if (editor.cursor.col > 0) {
in_gb_move (&editor.current_line->gb, editor.cursor.col);
in_gb_move(&editor.current_line->gb, editor.cursor.col);
editor.cursor.col--;
in_gb_backspace (&editor.current_line->gb);
in_gb_backspace(&editor.current_line->gb);
} else {
if (editor.cursor.line > 0) {
struct list_node_link* prev = editor.current_line->lines_link.prev;
struct edit_line* prev_line = list_entry (prev, struct edit_line, lines_link);
struct edit_line* prev_line = list_entry(prev, struct edit_line, lines_link);
size_t prev_len = in_gb_length (&prev_line->gb);
size_t prev_len = in_gb_length(&prev_line->gb);
in_gb_concat (&wrealloc, NULL, &prev_line->gb, &editor.current_line->gb);
in_gb_concat(&wrealloc, NULL, &prev_line->gb, &editor.current_line->gb);
list_remove (editor.lines, &editor.current_line->lines_link);
free (editor.current_line->gb.buffer);
free (editor.current_line);
list_remove(editor.lines, &editor.current_line->lines_link);
free(editor.current_line->gb.buffer);
free(editor.current_line);
editor.current_line = prev_line;
editor.cursor.col = prev_len;
@@ -272,61 +272,61 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
break;
case '\t':
if (editor.mode == EDIT_MODE_NORMAL) {
in_gb_move (&editor.current_line->gb, editor.cursor.col);
for (size_t i = 0; i < sizeof (TAB_INSERT) - 1; i++)
in_gb_insert (&wrealloc, NULL, &editor.current_line->gb, TAB_INSERT[i]);
in_gb_move(&editor.current_line->gb, editor.cursor.col);
for (size_t i = 0; i < sizeof(TAB_INSERT) - 1; i++)
in_gb_insert(&wrealloc, NULL, &editor.current_line->gb, TAB_INSERT[i]);
editor.cursor.col += sizeof (TAB_INSERT) - 1;
editor.cursor.col += sizeof(TAB_INSERT) - 1;
}
break;
case '\n': {
if (editor.mode == EDIT_MODE_NORMAL) {
in_gb_move (&editor.current_line->gb, editor.cursor.col);
in_gb_move(&editor.current_line->gb, editor.cursor.col);
struct edit_line* new_line = malloc (sizeof (*new_line));
memset (new_line, 0, sizeof (*new_line));
struct edit_line* new_line = malloc(sizeof(*new_line));
memset(new_line, 0, sizeof(*new_line));
char* tail = in_gb_string_at (&warena_malloc, &temp_arena, &editor.current_line->gb,
editor.cursor.col);
size_t tail_size = strlen (tail);
char* tail = in_gb_string_at(&warena_malloc, &temp_arena, &editor.current_line->gb,
editor.cursor.col);
size_t tail_size = strlen(tail);
size_t init_cap = tail_size > 0 ? tail_size : 32;
in_gb_init (&wmalloc, NULL, &new_line->gb, init_cap);
in_gb_init(&wmalloc, NULL, &new_line->gb, init_cap);
if (tail_size > 0) {
memcpy (new_line->gb.buffer,
editor.current_line->gb.buffer + editor.current_line->gb.gap_end, tail_size);
memcpy(new_line->gb.buffer,
editor.current_line->gb.buffer + editor.current_line->gb.gap_end, tail_size);
new_line->gb.gap_start = tail_size;
}
editor.current_line->gb.gap_end = editor.current_line->gb.size;
list_insert_after (editor.lines, &editor.current_line->lines_link, &new_line->lines_link);
list_insert_after(editor.lines, &editor.current_line->lines_link, &new_line->lines_link);
editor.cursor.col = 0;
editor.cursor.line++;
editor.current_line = new_line;
editor.total_lines++;
} else if (editor.mode == EDIT_MODE_COMAMND) {
if (strcmp (editor.cmdbuf.buffer, "q") == 0) {
if (strcmp(editor.cmdbuf.buffer, "q") == 0) {
edit_run = false;
editor.mode = EDIT_MODE_NORMAL;
} else if (strcmp (editor.cmdbuf.buffer, "w") == 0) {
} else if (strcmp(editor.cmdbuf.buffer, "w") == 0) {
struct filewriter fw;
if (!(filewriter_init (&fw, editor.volume, editor.path, FW_CREATE_FILE) < 0)) {
if (!(filewriter_init(&fw, editor.volume, editor.path, FW_CREATE_FILE) < 0)) {
struct list_node_link *line_link, *tmp_line_link;
list_foreach (editor.lines, line_link, tmp_line_link) {
struct edit_line* line = list_entry (line_link, struct edit_line, lines_link);
list_foreach(editor.lines, line_link, tmp_line_link) {
struct edit_line* line = list_entry(line_link, struct edit_line, lines_link);
filewriter_write (&fw, (uint8_t*)line->gb.buffer, line->gb.gap_start);
filewriter_write (&fw, (uint8_t*)line->gb.buffer + line->gb.gap_end,
line->gb.size - line->gb.gap_end);
filewriter_write(&fw, (uint8_t*)line->gb.buffer, line->gb.gap_start);
filewriter_write(&fw, (uint8_t*)line->gb.buffer + line->gb.gap_end,
line->gb.size - line->gb.gap_end);
if (line->lines_link.next != NULL)
filewriter_write (&fw, (uint8_t*)"\n", 1);
filewriter_write(&fw, (uint8_t*)"\n", 1);
}
filewriter_fini (&fw);
filewriter_fini(&fw);
}
editor.mode = EDIT_MODE_NORMAL;
} else {
@@ -336,22 +336,22 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
} break;
case KB_DELETE:
if (editor.mode == EDIT_MODE_NORMAL) {
size_t len = in_gb_length (&editor.current_line->gb);
size_t len = in_gb_length(&editor.current_line->gb);
if (editor.cursor.col < len) {
in_gb_move (&editor.current_line->gb, editor.cursor.col);
in_gb_move(&editor.current_line->gb, editor.cursor.col);
if (editor.current_line->gb.gap_end < editor.current_line->gb.size)
editor.current_line->gb.gap_end++;
} else {
if (editor.current_line->lines_link.next != NULL) {
struct list_node_link* next = editor.current_line->lines_link.next;
struct edit_line* next_line = list_entry (next, struct edit_line, lines_link);
struct edit_line* next_line = list_entry(next, struct edit_line, lines_link);
in_gb_concat (&wrealloc, NULL, &editor.current_line->gb, &next_line->gb);
in_gb_concat(&wrealloc, NULL, &editor.current_line->gb, &next_line->gb);
list_remove (editor.lines, &next_line->lines_link);
free (next_line->gb.buffer);
free (next_line);
list_remove(editor.lines, &next_line->lines_link);
free(next_line->gb.buffer);
free(next_line);
}
}
}
@@ -361,15 +361,15 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
editor.cursor.col--;
break;
case KB_RIGHT:
if (editor.cursor.col < in_gb_length (&editor.current_line->gb))
if (editor.cursor.col < in_gb_length(&editor.current_line->gb))
editor.cursor.col++;
break;
case KB_UP: {
if (editor.cursor.line > 0) {
editor.cursor.line--;
editor.current_line =
list_entry (editor.current_line->lines_link.prev, struct edit_line, lines_link);
size_t len = in_gb_length (&editor.current_line->gb);
list_entry(editor.current_line->lines_link.prev, struct edit_line, lines_link);
size_t len = in_gb_length(&editor.current_line->gb);
if (editor.cursor.col > len)
editor.cursor.col = len;
}
@@ -378,8 +378,8 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
if (editor.current_line->lines_link.next != NULL) {
editor.cursor.line++;
editor.current_line =
list_entry (editor.current_line->lines_link.next, struct edit_line, lines_link);
size_t len = in_gb_length (&editor.current_line->gb);
list_entry(editor.current_line->lines_link.next, struct edit_line, lines_link);
size_t len = in_gb_length(&editor.current_line->gb);
if (editor.cursor.col > len)
editor.cursor.col = len;
}
@@ -388,50 +388,50 @@ void edit_start (const char* volume, const char* file_path, const char* text, si
editor.cursor.col = 0;
break;
case KB_END:
editor.cursor.col = in_gb_length (&editor.current_line->gb);
editor.cursor.col = in_gb_length(&editor.current_line->gb);
break;
case KB_ESCAPE:
if (editor.mode == EDIT_MODE_COMAMND) {
editor.mode = EDIT_MODE_NORMAL;
editor.cmdbuf.len = 0;
memset (editor.cmdbuf.buffer, 0, sizeof (editor.cmdbuf.buffer));
memset(editor.cmdbuf.buffer, 0, sizeof(editor.cmdbuf.buffer));
}
break;
case KB_CTRL ('X'):
case KB_CTRL('X'):
editor.mode = EDIT_MODE_COMAMND;
editor.cmdbuf.len = 0;
memset (editor.cmdbuf.buffer, 0, sizeof (editor.cmdbuf.buffer));
memset(editor.cmdbuf.buffer, 0, sizeof(editor.cmdbuf.buffer));
break;
default:
if (isprint (ch)) {
if (isprint(ch)) {
if (editor.mode == EDIT_MODE_COMAMND) {
if (editor.cmdbuf.len < sizeof (editor.cmdbuf.buffer) - 1) {
if (editor.cmdbuf.len < sizeof(editor.cmdbuf.buffer) - 1) {
editor.cmdbuf.buffer[editor.cmdbuf.len++] = ch;
editor.cmdbuf.buffer[editor.cmdbuf.len] = '\0';
}
} else if (editor.mode == EDIT_MODE_NORMAL) {
in_gb_move (&editor.current_line->gb, editor.cursor.col);
in_gb_insert (&wrealloc, NULL, &editor.current_line->gb, ch);
in_gb_move(&editor.current_line->gb, editor.cursor.col);
in_gb_insert(&wrealloc, NULL, &editor.current_line->gb, ch);
editor.cursor.col++;
}
}
break;
}
arena_reset (&temp_arena);
arena_reset(&temp_arena);
}
mprintf (ANSIQ_SCR_CLR_ALL ANSIQ_CUR_VISIBLE);
mprintf(ANSIQ_SCR_CLR_ALL ANSIQ_CUR_VISIBLE);
arena_destroy (&temp_arena);
arena_destroy(&temp_arena);
struct list_node_link *line_link, *tmp_line_link;
list_foreach (editor.lines, line_link, tmp_line_link) {
struct edit_line* line = list_entry (line_link, struct edit_line, lines_link);
free (line->gb.buffer);
free (line);
list_foreach(editor.lines, line_link, tmp_line_link) {
struct edit_line* line = list_entry(line_link, struct edit_line, lines_link);
free(line->gb.buffer);
free(line);
}
memset (&editor, 0, sizeof (editor));
mprintf (ANSIQ_SCR_RESTORE);
memset(&editor, 0, sizeof(editor));
mprintf(ANSIQ_SCR_RESTORE);
}