From 779b0808e0ca16bec3ae80514656df4142a66110 Mon Sep 17 00:00:00 2001 From: jmug Date: Sun, 8 Nov 2026 11:07:27 -0800 Subject: [PATCH] feat: Limit horizontal scrolling. --- kilo.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/kilo.c b/kilo.c index a6ecaa1..3cf390b 100644 --- a/kilo.c +++ b/kilo.c @@ -342,6 +342,9 @@ void editorRefreshScreen() { /*** input ***/ void editorMoveCursor(int key) { + // Cursor can be one past the end of the file. + erow *row= (E.cy >= E.numrows) ? NULL : &E.row[E.cy]; + switch (key) { case ARROW_LEFT: if (E.cx != 0) { @@ -349,7 +352,9 @@ void editorMoveCursor(int key) { } break; case ARROW_RIGHT: - E.cx++; + if (row && E.cx < row->size) { + E.cx++; + } break; case ARROW_UP: if (E.cy != 0) { @@ -362,6 +367,13 @@ void editorMoveCursor(int key) { } break; } + + // Cursor could have moved past the end as a result of the scroll action. + 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() {