From 9797bc9316c4baa6556b4329b1f97303b915cf2b Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Tue, 2 Jan 2024 14:15:57 +0000 Subject: [PATCH] Editor: support page-up and page-down. Not very useful just yet while we're not able to scroll to keep the cursor in view, but even so, it's nice to be able to get to the bottom quickly! --- src/editor.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/editor.rs b/src/editor.rs index a178af3..f53da1d 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -722,6 +722,7 @@ struct Composer { scanner: &'static Scan, conf: InstanceStatusConfig, last_size: Option<(usize, usize)>, + page_len: usize, keystate: ComposerKeyState, goal_column: Option, header: FileHeader, @@ -744,6 +745,7 @@ impl Composer { scanner: Scan::get(), conf, last_size: None, + page_len: 0, keystate: ComposerKeyState::Start, goal_column: None, header, @@ -1030,6 +1032,20 @@ impl Composer { self.goal_column = None; } } + + fn page_down(&mut self) { + let (_x, y) = self.cursor_pos.expect("post_update should have run"); + self.goto_xy(0, y + self.page_len); + } + + fn page_up(&mut self) { + let (_x, y) = self.cursor_pos.expect("post_update should have run"); + if let Some(newy) = y.checked_sub(self.page_len) { + self.goto_xy(0, newy); + } else { + self.core.beginning_of_buffer(); + } + } } #[test] @@ -1221,6 +1237,9 @@ impl ActivityState for Composer { fn resize(&mut self, w: usize, h: usize) { if self.last_size != Some((w, h)) { self.last_size = Some((w, h)); + self.page_len = h.saturating_sub( + self.header.render(w).len() + self.headersep.render(w).len() + ); self.post_update(); } } @@ -1277,6 +1296,9 @@ impl ActivityState for Composer { (Start, Ctrl('N')) | (Start, Down) => self.next_line(), (Start, Ctrl('P')) | (Start, Up) => self.prev_line(), + (Start, Ctrl('V')) | (Start, PgDn) => self.page_down(), + (Start, Ctrl('Z')) | (Start, PgUp) => self.page_up(), + (Start, Return) => { self.core.insert("\n"); self.post_update(); -- 2.30.2