tAllow deletion across lines - ve - a minimal text editor (work in progress) (HTM) git clone git://src.adamsgaard.dk/ve (DIR) Log (DIR) Files (DIR) Refs (DIR) README (DIR) LICENSE --- (DIR) commit 5a34030990e5e1ea2cd8c30403c22c85c4ea088c (DIR) parent 2485a4471857acd56602b5069e6f08f388bed7aa (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk> Date: Tue, 6 Aug 2019 18:54:34 +0200 Allow deletion across lines Diffstat: M edit.c | 8 +++++++- M input.c | 3 +++ M row.c | 30 ++++++++++++++++++++++++++++++ M row.h | 2 ++ 4 files changed, 42 insertions(+), 1 deletion(-) --- (DIR) diff --git a/edit.c b/edit.c t@@ -13,12 +13,18 @@ editor_insert_char(int c) void editor_delete_char_left() { - if (E.cursor_y == E.num_rows) + if (E.cursor_y == E.num_rows || (E.cursor_x == 0 && E.cursor_y == 0)) return; eRow *row = &E.row[E.cursor_y]; if (E.cursor_x > 0) { editor_row_delete_char(row, E.cursor_x - 1); E.cursor_x--; + } else { + E.cursor_x = E.row[E.cursor_y - 1].size; + editor_row_append_string(&E.row[E.cursor_y - 1], + row->chars, row->size); + editor_row_delete(E.cursor_y); + E.cursor_y--; } } (DIR) diff --git a/input.c b/input.c t@@ -133,6 +133,9 @@ editor_process_keypress() case 'x': editor_delete_char_right(); break; + case 'd': + editor_row_delete(E.cursor_y); + break; case 'I': E.cursor_x = 0; (DIR) diff --git a/row.c b/row.c t@@ -82,6 +82,18 @@ editor_row_insert_char(eRow *row, int i, int c) E.file_changed = 1; } +/* append a string to the end of a row */ +void +editor_row_append_string(eRow *row, char *s, size_t len) +{ + row->chars = realloc(row->chars, row->size + len + 1); + memcpy(&row->chars[row->size], s, len); + row->size += len; + row->chars[row->size] = '\0'; + editor_update_row(row); + E.file_changed = 1; +} + void editor_row_delete_char(eRow *row, int i) { t@@ -92,3 +104,21 @@ editor_row_delete_char(eRow *row, int i) editor_update_row(row); E.file_changed = 1; } + +void +editor_row_free(eRow *row) +{ + free(row->rchars); + free(row->chars); +} + +void +editor_row_delete(int i) +{ + if (i<0 || i >= E.num_rows) + return; + editor_row_free(&E.row[i]); + memmove(&E.row[i], &E.row[i+1], sizeof(eRow)*(E.num_rows-i-1)); + E.num_rows--; + E.file_changed = 1; +} (DIR) diff --git a/row.h b/row.h t@@ -5,7 +5,9 @@ int editor_row_cursor_x_to_rx(eRow *row, int cursor_x); void editor_append_row(char *s, size_t len); +void editor_row_append_string(eRow *row, char *s, size_t len); void editor_row_insert_char(eRow *row, int i, int c); void editor_row_delete_char(eRow *row, int i); +void editor_row_delete(int i); #endif