From b9f9aec88b8a3a3b478f34932a834a94e889f769 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Fri, 21 Sep 2018 17:05:22 +0200 Subject: [PATCH] Add vertical scolling --- kilo.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/kilo.c b/kilo.c index 60bfd7a..bf0bbca 100644 --- a/kilo.c +++ b/kilo.c @@ -42,6 +42,7 @@ typedef struct erow { struct editorConfig { int cx, cy; + int rowoff; int screenrows; int screencols; int numrows; @@ -222,10 +223,20 @@ void abFree(struct abuf *ab) { /*** output ***/ +void editorScroll() { + if (E.cy < E.rowoff) { + E.rowoff = E.cy; + } + if (E.cy >= E.rowoff + E.screenrows) { + E.rowoff = E.cy - E.screenrows + 1; + } +} + void editorDrawRows(struct abuf *ab) { int y; for (y = 0; y < E.screenrows; y++) { - if (y >= E.numrows) { + int filerow = y + E.rowoff; + if (filerow >= E.numrows) { if (E.numrows == 0 && y == E.screenrows / 3) { char welcome[80]; int welcomelen = snprintf(welcome, sizeof(welcome), @@ -242,9 +253,9 @@ void editorDrawRows(struct abuf *ab) { abAppend(ab, "~", 1); } } else { - int len = E.row[y].size; + int len = E.row[filerow].size; if (len > E.screencols) len = E.screencols; - abAppend(ab, E.row[y].chars, len); + abAppend(ab, E.row[filerow].chars, len); } abAppend(ab, "\x1b[K", 3); // Erase one line @@ -255,6 +266,8 @@ void editorDrawRows(struct abuf *ab) { } void editorRefreshScreen() { + editorScroll(); + struct abuf ab = ABUF_INIT; abAppend(&ab, "\x1b[?25l", 6); // Hide cursor @@ -264,7 +277,7 @@ void editorRefreshScreen() { // Update cursor position 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 + 1); abAppend(&ab, buf, strlen(buf)); abAppend(&ab, "\x1b[?25h", 6); // Show cursor @@ -293,7 +306,7 @@ void editorMoveCursor(int key) { } break; case ARROW_DOWN: - if (E.cy != E.screenrows -1) { + if (E.cy < E.numrows) { E.cy++; } break; @@ -342,6 +355,7 @@ void editorProcessKeypress() { void initEditor() { E.cx = 0; E.cy = 0; + E.rowoff = 0; E.numrows = 0; E.row = NULL;