diff --git a/kilo.c b/kilo.c index 8f5cba9..26c453b 100644 --- a/kilo.c +++ b/kilo.c @@ -20,7 +20,9 @@ enum editorKey { ARROW_LEFT = 1000, ARROW_RIGHT, ARROW_UP, - ARROW_DOWN + ARROW_DOWN, + PAGE_UP, + PAGE_DOWN }; /*** data ***/ @@ -90,11 +92,27 @@ int editorReadKey() { return '\x1b'; if (seq[0] == '[') { - switch (seq[1]) { - case 'A': return ARROW_UP; - case 'B': return ARROW_DOWN; - case 'C': return ARROW_RIGHT; - case 'D': return ARROW_LEFT; + if (seq[1] >= '0' && seq[1] <= '9') { + // Parse a single digit escape seq arg: + if (read(STDIN_FILENO, &seq[2], 1) != 1) + return '\x1b'; + // `~` is the universal terminator for extended numeric key codes. + if (seq[2] == '~') { + // NOTE: On MacOS and Ghostty specifically, fn+arrow[up/down] is + // not sent as a page up/dow. Because it can mess up system keybindings. + switch (seq[1]) { + case '5': return PAGE_UP; + case '6': return PAGE_DOWN; + } + } + } else { + // Parse no arg seq otherwise: + switch (seq[1]) { + case 'A': return ARROW_UP; + case 'B': return ARROW_DOWN; + case 'C': return ARROW_RIGHT; + case 'D': return ARROW_LEFT; + } } } @@ -270,6 +288,14 @@ void editorProcessKeypress() { exit(0); break; + + case PAGE_UP: + case PAGE_DOWN: { + int times = E.screenrows; + while (times--) { + editorMoveCursor(c == PAGE_UP ? ARROW_UP : ARROW_DOWN); + } + } break; case ARROW_UP: case ARROW_LEFT: case ARROW_DOWN: