From 94dc8ca3313e14792058d3d16f8778248f43538d Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 20 Aug 2019 11:55:11 -0400 Subject: [PATCH] Backspace to previous line --- kilo.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/kilo.c b/kilo.c index e77bb73..9fedc36 100644 --- a/kilo.c +++ b/kilo.c @@ -321,6 +321,24 @@ void editorAppendRow(char *s, size_t len) E.dirty++; } +void editorFreeRow(erow *row) +{ + free(row->render); + free(row->chars); +} + +void editorDelRow(int at) +{ + if (at < 0 || at >= E.numrows) + { + return; + } + editorFreeRow(&E.row[at]); + memmove(&E.row[at], &E.row[at + 1], sizeof(erow) * (E.numrows - at - 1)); + E.numrows--; + E.dirty++; +} + void editorRowInsertChar(erow *row, int at, int c) { if (at < 0 || at > row->size) @@ -336,6 +354,16 @@ void editorRowInsertChar(erow *row, int at, int c) E.dirty++; } +void editorRowAppendString(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'; + editorUpdateRow(row); + E.dirty++; +} + void editorRowDelChar(erow *row, int at) { if (at < 0 || at >= row->size) @@ -368,12 +396,24 @@ void editorDelChar() return; } + if (E.cx == 0 && E.cy == 0) + { + return; + } + erow *row = &E.row[E.cy]; if (E.cx > 0) { editorRowDelChar(row, E.cx - 1); E.cx--; } + else + { + E.cx = E.row[E.cy - 1].size; + editorRowAppendString(&E.row[E.cy - 1], row->chars, row->size); + editorDelRow(E.cy); + E.cy--; + } } /*** file i/o ***/