changeset 6827:0cde36878b49 draft

Use a scrolloff-based margin for page scrolling I'm not totally sure about the numbers here: Is the +1 doing what I think it is? What about the division by 2 in the half variants: should the +1 be done after the division? ~Is this all dependent on whether the height of the viewport is even or odd?~ Tested and this is not the case.
author Michael Davis <mcarsondavis@gmail.com>
date Sun, 20 Nov 2022 10:56:57 -0600
parents 01d5c96e4e85
children aed55b52115e
files helix-term/src/commands.rs
diffstat 1 files changed, 20 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/helix-term/src/commands.rs	Tue Apr 09 08:13:22 2024 +0200
+++ b/helix-term/src/commands.rs	Sun Nov 20 10:56:57 2022 -0600
@@ -1915,51 +1915,63 @@
     doc.set_selection(view.id, sel);
 }
 
+// The `margin` in the next eight commands allows you to "undo" the view change
+// even if you have scrolloff set: (half_)page_down and then (half_)page_up should
+// keep the cursor in the same place. The `+ 1` accounts for the current line.
+
 fn page_up(cx: &mut Context) {
     let view = view!(cx.editor);
-    let offset = view.inner_height();
+    let margin = cx.editor.config().scrolloff * 2 + 1;
+    let offset = view.inner_height().saturating_sub(margin);
     scroll(cx, offset, Direction::Backward, false);
 }
 
 fn page_down(cx: &mut Context) {
     let view = view!(cx.editor);
-    let offset = view.inner_height();
+    let margin = cx.editor.config().scrolloff * 2 + 1;
+    let offset = view.inner_height().saturating_sub(margin);
     scroll(cx, offset, Direction::Forward, false);
 }
 
 fn half_page_up(cx: &mut Context) {
     let view = view!(cx.editor);
-    let offset = view.inner_height() / 2;
+    let margin = cx.editor.config().scrolloff * 2 + 1;
+    let offset = view.inner_height().saturating_sub(margin) / 2;
     scroll(cx, offset, Direction::Backward, false);
 }
 
 fn half_page_down(cx: &mut Context) {
     let view = view!(cx.editor);
-    let offset = view.inner_height() / 2;
+    let margin = cx.editor.config().scrolloff * 2 + 1;
+    let offset = view.inner_height().saturating_sub(margin) / 2;
     scroll(cx, offset, Direction::Forward, false);
 }
 
 fn page_cursor_up(cx: &mut Context) {
     let view = view!(cx.editor);
-    let offset = view.inner_height();
+    let margin = cx.editor.config().scrolloff * 2 + 1;
+    let offset = view.inner_height().saturating_sub(margin);
     scroll(cx, offset, Direction::Backward, true);
 }
 
 fn page_cursor_down(cx: &mut Context) {
     let view = view!(cx.editor);
-    let offset = view.inner_height();
+    let margin = cx.editor.config().scrolloff * 2 + 1;
+    let offset = view.inner_height().saturating_sub(margin);
     scroll(cx, offset, Direction::Forward, true);
 }
 
 fn page_cursor_half_up(cx: &mut Context) {
     let view = view!(cx.editor);
-    let offset = view.inner_height() / 2;
+    let margin = cx.editor.config().scrolloff * 2 + 1;
+    let offset = view.inner_height().saturating_sub(margin) / 2;
     scroll(cx, offset, Direction::Backward, true);
 }
 
 fn page_cursor_half_down(cx: &mut Context) {
     let view = view!(cx.editor);
-    let offset = view.inner_height() / 2;
+    let margin = cx.editor.config().scrolloff * 2 + 1;
+    let offset = view.inner_height().saturating_sub(margin) / 2;
     scroll(cx, offset, Direction::Forward, true);
 }