From c113787dfeecede9212a4ee92e04bd02a3f06ee3 Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Thu, 8 Feb 2024 14:46:21 +1100 Subject: [PATCH] Chapter 5: Start --- kilo.c | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 121 insertions(+), 14 deletions(-) diff --git a/kilo.c b/kilo.c index fc8c6b0..9a5e676 100644 --- a/kilo.c +++ b/kilo.c @@ -7,16 +7,19 @@ #include #include #include +#include #include #include #include #include #include +#include #include /*** defines ***/ #define KILO_VERSION "0.0.1" +#define KILO_TAB_STOP 4 #define CTRL_KEY(k) ((k) & 0x1f) @@ -37,18 +40,24 @@ enum editorKey typedef struct erow { int size; + int rsize; char *chars; + char *render; } erow; struct editorConfig { int cx, cy; + int rx; int rowoff; // Row offset int coloff; // Col offset int screenrows; int screencols; int numrows; erow *row; + char *filename; + char statusmsg[80]; + time_t statusmsg_time; struct termios orig_termios; }; @@ -207,6 +216,41 @@ int getWindowSize(int *rows, int *cols) /*** row operations ***/ +int editorRowCxToRx(erow *row, int cx) { + int rx = 0; + int j; + for (j = 0; j < cx; j++) { + if (row->chars[j] == '\t') + rx =+ (KILO_TAB_STOP - 1) - (rx % KILO_TAB_STOP); + rx++; + } + return rx; +} + +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)); @@ -215,12 +259,20 @@ 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++; } /*** file i/o ***/ void editorOpen(char *filename) { + free(E.filename); + E.filename = strdup(filename); + FILE *fp = fopen(filename, "r"); if (!fp) die("fopen"); @@ -268,17 +320,24 @@ void abFree(struct abuf *ab) /*** output ***/ void editorScroll() { + E.rx = 0; + + if (E.cy < E.numrows) { + E.rx = editorRowCxToRx(&E.row[E.cy], E.cx); + } + + if (E.cy < E.rowoff) { E.rowoff = E.cy; } 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.rx < E.coloff) { + E.coloff = E.rx; } - if (E.cx >= E.coloff + E.screencols) { - E.coloff = E.cx - E.screencols + 1; + if (E.rx >= E.coloff + E.screencols) { + E.coloff = E.rx - E.screencols + 1; } } @@ -312,17 +371,44 @@ void editorDrawRows(struct abuf *ab) } } else { - int len = E.row[filerow].size - E.coloff; + 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[E.coloff], len); + abAppend(ab, &E.row[filerow].render[E.coloff], len); } abAppend(ab, "\x1b[K", 3); - if (y < E.screenrows - 1) - { - abAppend(ab, "\r\n", 2); + abAppend(ab, "\r\n", 2); + } +} + +void editorDrawStatusBar(struct abuf *ab) { + abAppend(ab, "\x1b[7m", 4); + char status[80], rstatus[80]; + int len = snprintf(status, sizeof(status), "%.20s - %d lines", + E.filename ? E.filename : "[No Name]", E.numrows); + int rlen = snprintf(rstatus, sizeof(rstatus), "%d/%d", + E.cy + 1, E.numrows); + if (len > E.screencols) len = E.screencols; + abAppend(ab, status, len); + while (len < E.screencols) { + if (E.screencols - len == rlen) { + abAppend(ab, rstatus, rlen); + break; + } else { + abAppend(ab, " ", 1); + len++; } } + abAppend(ab, "\x1b[m", 3); + abAppend(ab, "\r\n", 2); +} + +void editorDrawMessageBar(struct abuf *ab) { + abAppend(ab, "\x1b[K", 3); + int msglen = strlen(E.statusmsg); + if (msglen > E.screencols) msglen = E.screencols; + if (msglen && time(NULL) - E.statusmsg_time < 5) + abAppend(ab, E.statusmsg, msglen); } void editorRefreshScreen() @@ -334,9 +420,11 @@ void editorRefreshScreen() abAppend(&ab, "\x1b[H", 3); editorDrawRows(&ab); + editorDrawStatusBar(&ab); + editorDrawMessageBar(&ab); char buf[32]; - snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1, (E.cx - E.coloff) + 1); + snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1, (E.rx - E.coloff) + 1); abAppend(&ab, buf, strlen(buf)); abAppend(&ab, "\x1b[?25h", 6); @@ -344,6 +432,14 @@ void editorRefreshScreen() abFree(&ab); } +void editorSetStatusMessage(const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + vsnprintf(E.statusmsg, sizeof(E.statusmsg), fmt, ap); + va_end(ap); + E.statusmsg_time = time(NULL); +} + /*** input ***/ void editorMoveCursor(int key) @@ -408,14 +504,18 @@ void editorProcessKeypress() E.cx = 0; break; case END_KEY: - E.cx = E.screencols - 1; + if (E.cy < E.numrows) + E.cx = E.row[E.cy].size; break; case PAGE_UP: case PAGE_DOWN: { - int times = E.screenrows; - while (times--) - editorMoveCursor(c == PAGE_UP ? ARROW_UP : ARROW_DOWN); + if (c == PAGE_UP) { + E.cy = E.rowoff; + } else { + E.cy = E.rowoff + E.screenrows - 1; + if (E.cy > E.numrows) E.cy = E.numrows; + } } break; @@ -434,13 +534,18 @@ void initEditor() { E.cx = 0; E.cy = 0; + E.rx = 0; E.rowoff = 0; E.coloff = 0; E.numrows = 0; E.row = NULL; + E.filename = NULL; + E.statusmsg[0] = '\0'; + E.statusmsg_time = 0; if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize"); + E.screenrows -= 2; } int main(int argc, char *argv[]) @@ -450,6 +555,8 @@ int main(int argc, char *argv[]) if (argc >= 2) { editorOpen(argv[1]); } + + editorSetStatusMessage("HELP Ctrl-Q = Quit"); while (1) {