chiark / gitweb /
Editor: make the cursor position persist.
authorSimon Tatham <anakin@pobox.com>
Tue, 2 Jan 2024 13:56:46 +0000 (13:56 +0000)
committerSimon Tatham <anakin@pobox.com>
Tue, 2 Jan 2024 17:30:18 +0000 (17:30 +0000)
Then we'll still have it available to refer to when processing
keystrokes.

src/editor.rs

index 343db2653a3ba4b05f741fa4db2b917e943b1655..5ed64cb8cfb26228711023a53be57bfa7b1976d4 100644 (file)
@@ -718,6 +718,7 @@ struct Composer {
     core: EditorCore,
     regions: Vec<ComposeBufferRegion>,
     layout: Vec<ComposeLayoutCell>,
+    cursor_pos: Option<(usize, usize)>,
     scanner: &'static Scan,
     conf: InstanceStatusConfig,
     last_size: Option<(usize, usize)>,
@@ -738,6 +739,7 @@ impl Composer {
             },
             regions: Vec::new(),
             layout: Vec::new(),
+            cursor_pos: None,
             scanner: Scan::get(),
             conf,
             last_size: None,
@@ -965,11 +967,23 @@ impl Composer {
         Some(cs)
     }
 
+    fn determine_cursor_pos(&self) -> Option<(usize, usize)> {
+        let cursor_cell_index = self.layout
+            .partition_point(|cell| cell.pos < self.core.point);
+        if let Some(cell) = self.layout.get(cursor_cell_index) {
+            if cell.pos == self.core.point {
+                return Some((cell.x, cell.y));
+            }
+        }
+        None // _probably_ shouldn't happen unless something is confused
+    }
+
     fn post_update(&mut self) {
         self.regions = self.make_regions(&self.core);
         if let Some((w, _h)) = self.last_size {
             self.layout = self.layout(w);
         }
+        self.cursor_pos = self.determine_cursor_pos();
     }
 }
 
@@ -1184,15 +1198,11 @@ impl ActivityState for Composer {
         }
 
         let mut cursor_pos = CursorPosition::None;
-        let cursor_cell_index = self.layout
-            .partition_point(|cell| cell.pos < self.core.point);
-        if let Some(cell) = self.layout.get(cursor_cell_index) {
-            if cell.pos == self.core.point {
-                if let Some(y) = cell.y.checked_sub(ytop) {
-                    let y = y + ystart;
-                    if y < h {
-                        cursor_pos = CursorPosition::At(cell.x, y);
-                    }
+        if let Some((cx, cy)) = self.cursor_pos {
+            if let Some(y) = cy.checked_sub(ytop) {
+                let cy = y + ystart;
+                if cy < h {
+                    cursor_pos = CursorPosition::At(cx, cy);
                 }
             }
         }