diff --git a/kilo.c b/kilo.c index 5c37762..bee0c25 100644 --- a/kilo.c +++ b/kilo.c @@ -224,10 +224,12 @@ void editorUpdateRow(erow *row) { row->rsize = idx; } -void editorAppendRow(char *s, size_t len) { - E.row = realloc(E.row, sizeof(erow) * (E.numrows +1)); +void editorInsertRow(int at, char *s, size_t len) { + if (at < 0 || at > E.numrows) return; + + E.row = realloc(E.row, sizeof(erow) * (E.numrows +1)); + memmove(&E.row[at + 1], &E.row[at], sizeof(erow) * (E.numrows - at)); - int at = E.numrows; E.row[at].size = len; E.row[at].chars = malloc(len + 1); memcpy(E.row[at].chars, s, len); @@ -241,6 +243,19 @@ 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) at = row->size; row->chars = realloc(row->chars, row->size + 2); @@ -251,17 +266,66 @@ 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) return; + memmove(&row->chars[at], &row->chars[at + 1], row->size - at); + row->size--; + editorUpdateRow(row); + E.dirty++; +} + /*** editor operations ***/ void editorInsertChar(int c) { if (E.cy == E.numrows) { - editorAppendRow("", 0); + editorInsertRow(E.numrows, "", 0); } editorRowInsertChar(&E.row[E.cy], E.cx, c); E.cx++; } +void editorInsertNewLine() { + if (E.cx == 0) { + editorInsertRow(E.cy, "", 0); + } else { + erow *row = &E.row[E.cy]; + editorInsertRow(E.cy + 1, &row->chars[E.cx], row->size - E.cx); + row = &E.row[E.cy]; + row->size = E.cx; + row->chars[row->size] = '\0'; + editorUpdateRow(row); + } + E.cy++; + E.cx = 0; +} + + +void editorDelChar() { + if (E.cy == E.numrows) 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 ***/ char *editorRowsToString(int *buflen) { @@ -298,7 +362,7 @@ void editorOpen(char *filename) { while (linelen > 0 && (line[linelen - 1] == '\n' || line[linelen - 1] == '\r')) linelen--; - editorAppendRow(line, linelen); + editorInsertRow(E.numrows, line, linelen); } free(line); fclose(fp); @@ -514,7 +578,7 @@ void editorProcessKeypress() { switch (c) { case '\r': - /* TODO */ + editorInsertNewLine(); break; case CTRL_KEY('q'): @@ -546,7 +610,8 @@ void editorProcessKeypress() { case BACKSPACE: case CTRL_KEY('h'): case DEL_KEY: - /* TODO */ + if (c == DEL_KEY) editorMoveCursor(ARROW_RIGHT); + editorDelChar(); break; case PAGE_UP: