From 66b97546e27ab481608595b308c8607998f799a5 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Fri, 5 Oct 2018 11:29:30 +0200 Subject: [PATCH] Make the editor insert text! --- kilo.c | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/kilo.c b/kilo.c index a3f872c..20cb239 100644 --- a/kilo.c +++ b/kilo.c @@ -24,6 +24,7 @@ #define CTRL_KEY(k) ((k) & 0x1f) enum editorKey { + BACKSPACE = 127, ARROW_LEFT = 1000, ARROW_RIGHT, ARROW_UP, @@ -201,13 +202,13 @@ void editorUpdateRow(erow *row) { } free(row->render); - row->render = malloc(row->size + tabs*(KILO_TAB_STOP-1) + 1); + row->render = malloc(row->size + tabs*(KILO_TAB_STOP - 1) + 1); int idx = 0; for (j=0; jsize; j++) { if (row->chars[j] == '\t') { row->render[idx++] = ' '; - while (idx%KILO_TAB_STOP != 0) row->render[idx++] = ' '; + while (idx % KILO_TAB_STOP != 0) row->render[idx++] = ' '; } else { row->render[idx++] = row->chars[j]; } @@ -232,6 +233,26 @@ void editorAppendRow(char *s, size_t len) { E.numrows++; } +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); + memmove(&row->chars[at + 1], &row->chars[at], row->size - at + 1); + row->size++; + row->chars[at] = c; + editorUpdateRow(row); +} + + +/*** editor operations ***/ + +void editorInsertChar(int c) { + if (E.cy == E.numrows) { + editorAppendRow("", 0); + } + editorRowInsertChar(&E.row[E.cy], E.cx, c); + E.cx++; +} + /*** file i/o ***/ void editorOpen(char *filename) { @@ -277,7 +298,7 @@ void abFree(struct abuf *ab) { /*** output ***/ void editorScroll() { - E.rx = E.cx; + E.rx = 0; if (E.cy < E.numrows) { E.rx = editorRowCxToRx(&E.row[E.cy], E.cx); } @@ -435,6 +456,10 @@ void editorProcessKeypress() { int c = editorReadKey(); // Blocking! switch (c) { + case '\r': + /* TODO */ + break; + case CTRL_KEY('q'): write(STDOUT_FILENO, "\x1b[2J", 4); write(STDOUT_FILENO, "\x1b[H", 3); @@ -451,6 +476,12 @@ void editorProcessKeypress() { } break; + case BACKSPACE: + case CTRL_KEY('h'): + case DEL_KEY: + /* TODO */ + break; + case PAGE_UP: case PAGE_DOWN: { @@ -472,6 +503,13 @@ void editorProcessKeypress() { editorMoveCursor(c); break; + case CTRL_KEY('l'): + case '\x1b': + break; + + default: + editorInsertChar(c); + break; } }