feat: Parse page up/down.

This commit is contained in:
jmug
2026-10-27 00:43:34 -07:00
parent 6b59986fca
commit b85d656a74
+32 -6
View File
@@ -20,7 +20,9 @@ enum editorKey {
ARROW_LEFT = 1000, ARROW_LEFT = 1000,
ARROW_RIGHT, ARROW_RIGHT,
ARROW_UP, ARROW_UP,
ARROW_DOWN ARROW_DOWN,
PAGE_UP,
PAGE_DOWN
}; };
/*** data ***/ /*** data ***/
@@ -90,11 +92,27 @@ int editorReadKey() {
return '\x1b'; return '\x1b';
if (seq[0] == '[') { if (seq[0] == '[') {
switch (seq[1]) { if (seq[1] >= '0' && seq[1] <= '9') {
case 'A': return ARROW_UP; // Parse a single digit escape seq arg:
case 'B': return ARROW_DOWN; if (read(STDIN_FILENO, &seq[2], 1) != 1)
case 'C': return ARROW_RIGHT; return '\x1b';
case 'D': return ARROW_LEFT; // `~` 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); exit(0);
break; 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_UP:
case ARROW_LEFT: case ARROW_LEFT:
case ARROW_DOWN: case ARROW_DOWN: