From 01dbed5e6c15875d15eb7e9bcd58da207e0d1d4a Mon Sep 17 00:00:00 2001 From: Hadeed Ahmad Date: Mon, 4 Sep 2023 16:59:33 +0500 Subject: [PATCH] Almost finish editing --- src/buffer.c | 2 +- src/commands.c | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/buffer.c b/src/buffer.c index 5055047..7965d11 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -96,7 +96,7 @@ END: void buffer_insert_row(struct buffer *buffer, const char *chars, int n_chars, int at) { // TODO: Make this take an erow buffer->rows = realloc(buffer->rows, sizeof(struct erow) * (buffer->n_rows + 1)); - memmove(buffer->rows + at, buffer->rows + at + 1, sizeof(struct erow) * (buffer->n_rows - at)); + memmove(buffer->rows + at + 1, buffer->rows + at, sizeof(struct erow) * (buffer->n_rows - at)); struct erow *erow = buffer->rows + at; erow->chars = malloc(n_chars); diff --git a/src/commands.c b/src/commands.c index 031b602..d1c24ef 100644 --- a/src/commands.c +++ b/src/commands.c @@ -70,11 +70,27 @@ void command_move_cursor(KEY key) { cursor_adjust_viewport(); } +// TODO: Improve this void command_insert_line(void) { - buffer_insert_row(E.current_buf, NULL, 0, ++E.cy); + if (E.cy == E.current_buf->n_rows) { + buffer_insert_row(E.current_buf, NULL, 0, E.cy); + E.cy++; + } else { + buffer_insert_row(E.current_buf, NULL, 0, E.cy + 1); - E.cx = 0; - E.rx = 0; + struct erow *c_row = E.current_buf->rows + E.cy; + struct erow *n_row = E.current_buf->rows + E.cy + 1; + + erow_append_string(n_row, c_row->chars + E.cx, c_row->n_chars - E.cx); + + c_row->chars = realloc(c_row->chars, E.cx); + c_row->n_chars = E.cx; + erow_update_rendering(c_row); + + E.cx = 0; + E.rx = 0; + E.cy++; + } } void command_insert_char(char c) {