# HG changeset patch # User Michael Davis # Date 1668963417 21600 # Node ID 0cde36878b49229eeb240ebee329b060355b2075 # Parent 01d5c96e4e85cb09174e4f63c5d5c1c70c1792cf 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. diff -r 01d5c96e4e85 -r 0cde36878b49 helix-term/src/commands.rs --- 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); }