From c1f46771d7c9f39e776a2c20945251ff4b9c92f7 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 19 Aug 2019 15:19:16 -0400 Subject: [PATCH] Render tabs as spaces --- kilo.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 86 insertions(+), 4 deletions(-) diff --git a/kilo.c b/kilo.c index 4012d9f..1772c48 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) @@ -36,12 +37,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; @@ -231,6 +235,43 @@ int getWindowSize(int *rows, int *cols) } /*** row operations ***/ + +void editorUpdateRow(erow *row) +{ + int tabs = 0; + int j; + + for (j = 0; j < row->size; 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; j < row->size; 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)); @@ -240,6 +281,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++; } @@ -311,6 +357,16 @@ void editorScroll() { 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) @@ -353,13 +409,18 @@ void editorDrawRows(struct abuf *ab) } 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); @@ -382,7 +443,8 @@ void editorRefreshScreen() editorDrawRows(&ab); char buf[32]; - snprintf(buf, sizeof(buf), "\x1b[%d;%dH", E.cy + 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); @@ -395,6 +457,8 @@ void editorRefreshScreen() void editorMoveCursor(int key) { + erow *row = (E.cy >= E.numrows) ? NULL : &E.row[E.cy]; + switch (key) { case ARROW_LEFT: @@ -402,13 +466,23 @@ void editorMoveCursor(int key) { 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: @@ -425,6 +499,13 @@ 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; + } } void editorProcessKeypress() @@ -473,6 +554,7 @@ void initEditor() E.cx = 0; E.cy = 0; E.rowoff = 0; + E.coloff = 0; E.numrows = 0; E.row = NULL;