From 0de0b4c1aecea10c90856a278dfbd76bd2df2188 Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Sun, 8 Mar 2026 11:55:09 +0100 Subject: [PATCH] CE edit KB_DELETE the following line --- ce/edit.c | 19 +++++++++++++++++-- ce/gapbuffer.c | 7 +++++++ ce/gapbuffer.h | 2 ++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/ce/edit.c b/ce/edit.c index b4272f4..55d90fe 100644 --- a/ce/edit.c +++ b/ce/edit.c @@ -272,11 +272,26 @@ void edit_start (const char* file_path, const char* text) { case KB_DELETE: if (editor.mode == EDIT_MODE_NORMAL) { size_t len = gapbuffer_length (&editor.current_line->gb); - if (editor.cursor.col < len) { - gapbuffer_move (&editor.current_line->gb, editor.cursor.col); + gapbuffer_move (&editor.current_line->gb, editor.cursor.col); + if (editor.cursor.col < len) { if (editor.current_line->gb.gap_end < editor.current_line->gb.size) editor.current_line->gb.gap_end++; + } else { + if (editor.cursor.line > 0) { + struct list_node_link* next = editor.current_line->lines_link.next; + struct edit_line* next_line = list_entry (next, struct edit_line, lines_link); + size_t next_len = gapbuffer_length (&next_line->gb); + + for (size_t i = 0; i < next_len; i++) { + char chr = gapbuffer_at (&next_line->gb, i); + gapbuffer_insert (&wrealloc, NULL, &editor.current_line->gb, chr); + } + + list_remove (editor.lines, &next_line->lines_link); + free (next_line->gb.buffer); + free (next_line); + } } } break; diff --git a/ce/gapbuffer.c b/ce/gapbuffer.c index 783c215..3537bc3 100644 --- a/ce/gapbuffer.c +++ b/ce/gapbuffer.c @@ -112,3 +112,10 @@ char* gapbuffer_string_at (gb_malloc_func_t mallocfn, void* ctx, struct gapbuffe res[written] = '\0'; return res; } + +char gapbuffer_at (struct gapbuffer* gb, size_t index) { + if (index < gb->gap_start) + return gb->buffer[index]; + else + return gb->buffer[index + (gb->gap_end - gb->gap_start)]; +} diff --git a/ce/gapbuffer.h b/ce/gapbuffer.h index 0e1c058..1401f8c 100644 --- a/ce/gapbuffer.h +++ b/ce/gapbuffer.h @@ -33,4 +33,6 @@ size_t gapbuffer_length (struct gapbuffer* gb); char* gapbuffer_string_at (gb_malloc_func_t mallocfn, void* ctx, struct gapbuffer* gb, size_t pos); +char gapbuffer_at (struct gapbuffer* gb, size_t index); + #endif // _GAPBUFFER_H