chiark / gitweb /
Editor: support page-up and page-down.
authorSimon Tatham <anakin@pobox.com>
Tue, 2 Jan 2024 14:15:57 +0000 (14:15 +0000)
committerSimon Tatham <anakin@pobox.com>
Tue, 2 Jan 2024 17:30:18 +0000 (17:30 +0000)
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

index a178af3e3f17af6dbcad41e921e70d5304a15261..f53da1d4825f767290422f713649c8b97d82f3cd 100644 (file)
@@ -722,6 +722,7 @@ struct Composer {
     scanner: &'static Scan,
     conf: InstanceStatusConfig,
     last_size: Option<(usize, usize)>,
+    page_len: usize,
     keystate: ComposerKeyState,
     goal_column: Option<usize>,
     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();