From 061ddbc5b57ef38de6e49efb5e855ca57ab44cf6 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 20 Aug 2019 09:03:04 -0400 Subject: [PATCH] Enable entering some characters --- kilo.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/kilo.c b/kilo.c index a818810..1c0ec8b 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, @@ -312,6 +313,32 @@ 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) { @@ -603,6 +630,10 @@ void editorProcessKeypress() switch (c) { + case '\r': + /* TODO */ + break; + case CTRL_KEY('q'): write(STDOUT_FILENO, "\x1b[2J", 4); write(STDOUT_FILENO, "\x1b[H", 3); @@ -620,6 +651,12 @@ void editorProcessKeypress() } break; + case BACKSPACE: + case CTRL_KEY('h'): + case DEL_KEY: + /* TODO */ + break; + case PAGE_UP: case PAGE_DOWN: { if (c == PAGE_UP) @@ -649,6 +686,14 @@ void editorProcessKeypress() case ARROW_RIGHT: editorMoveCursor(c); break; + + case CTRL_KEY('l'): + case '\x1b': + break; + + default: + editorInsertChar(c); + break; } }