From 0a8cb732901531a9a88d9e5567477101c804628b Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Mon, 1 Oct 2018 17:03:40 +0200 Subject: [PATCH] Add tabs --- kilo.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/kilo.c b/kilo.c index bf0bbca..8666576 100644 --- a/kilo.c +++ b/kilo.c @@ -17,6 +17,7 @@ /*** defines ***/ #define KILO_VERSION "0.0.1" +#define KILO_TAB_STOP 8 #define CTRL_KEY(k) ((k) & 0x1f) @@ -37,12 +38,15 @@ enum editorKey { typedef struct erow { int size; + int rsize; char *chars; + char *render; } erow; struct editorConfig { int cx, cy; int rowoff; + int coloff; int screenrows; int screencols; int numrows; @@ -171,6 +175,29 @@ int getWindowSize(int *rows, int *cols) { /*** row operations ***/ +void editorUpdateRow(erow *row) { + int tabs = 0; + int j; + for (j=0; jsize; j++) { + if (row->chars[j] == '\t') tabs++; + } + + free(row->render); + 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++] = ' '; + } else { + row->render[idx++] = row->chars[j]; + } + } + row->render[idx] = '\0'; + row->rsize = idx; +} + void editorAppendRow(char *s, size_t len) { E.row = realloc(E.row, sizeof(erow) * (E.numrows +1)); @@ -179,6 +206,11 @@ void editorAppendRow(char *s, size_t len) { E.row[at].chars = malloc(len + 1); memcpy(E.row[at].chars, s, len); E.row[at].chars[len] = '\0'; + + E.row[at].rsize = 0; + E.row[at].render = NULL; + editorUpdateRow(&E.row[at]); + E.numrows++; } @@ -230,6 +262,12 @@ void editorScroll() { if (E.cy >= E.rowoff + E.screenrows) { E.rowoff = E.cy - E.screenrows + 1; } + if (E.cx < E.coloff) { + E.coloff = E.cx; + } + if (E.cx >= E.coloff + E.screencols) { + E.coloff = E.cx - E.screencols + 1; + } } void editorDrawRows(struct abuf *ab) { @@ -253,9 +291,10 @@ void editorDrawRows(struct abuf *ab) { abAppend(ab, "~", 1); } } else { - int len = E.row[filerow].size; + int len = E.row[filerow].rsize - E.coloff; + if (len < 0) len = 0; if (len > E.screencols) len = E.screencols; - abAppend(ab, E.row[filerow].chars, len); + abAppend(ab, &E.row[filerow].render[E.coloff], len); } abAppend(ab, "\x1b[K", 3); // Erase one line @@ -277,7 +316,7 @@ void editorRefreshScreen() { // Update cursor position char buf[32]; - snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1, E.cx + 1); + snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1, (E.cx - E.coloff) + 1); abAppend(&ab, buf, strlen(buf)); abAppend(&ab, "\x1b[?25h", 6); // Show cursor @@ -289,15 +328,23 @@ void editorRefreshScreen() { /*** input ***/ void editorMoveCursor(int key) { + erow *row = (E.cy >= E.numrows) ? NULL : &E.row[E.cy]; + switch (key) { case ARROW_LEFT: if (E.cx != 0) { E.cx--; + } else if (E.cy > 0) { + E.cy--; + E.cx = E.row[E.cy].size; } break; case ARROW_RIGHT: - if (E.cx != E.screencols -1) { + if (row && E.cx < row->size) { E.cx++; + } else if (row && E.cx == row->size) { + E.cy++; + E.cx = 0; } break; case ARROW_UP: @@ -311,6 +358,12 @@ void editorMoveCursor(int key) { } break; } + + row = (E.cy >= E.numrows) ? NULL : &E.row[E.cy]; + int rowlen = row ? row->size : 0; + if (E.cx > rowlen) { + E.cx = rowlen; + } } @@ -356,6 +409,7 @@ void initEditor() { E.cx = 0; E.cy = 0; E.rowoff = 0; + E.coloff = 0; E.numrows = 0; E.row = NULL;