From b3227d60498938a06d16d9ea405417ce70a52158 Mon Sep 17 00:00:00 2001 From: Nimish Jha Date: Wed, 23 Oct 2024 16:25:33 +1100 Subject: [PATCH] add actions: CursorToViewTop, CursorToViewCenter, CursorToViewBottom (#3506) --- internal/action/actions.go | 46 +++++++++++++++++++++++++++++++++++++ internal/action/bufpane.go | 3 +++ runtime/help/keybindings.md | 3 +++ 3 files changed, 52 insertions(+) diff --git a/internal/action/actions.go b/internal/action/actions.go index 1fe8b7f0..2c7157c4 100644 --- a/internal/action/actions.go +++ b/internal/action/actions.go @@ -160,6 +160,52 @@ func (h *BufPane) Center() bool { return true } +// CursorToViewTop moves the cursor to the top of the view, +// offset by scrollmargin unless at the beginning or end of the file +func (h *BufPane) CursorToViewTop() bool { + v := h.GetView() + h.Buf.ClearCursors() + scrollmargin := int(h.Buf.Settings["scrollmargin"].(float64)) + bStart := display.SLoc{0, 0} + if v.StartLine == bStart { + scrollmargin = 0 + } + h.Cursor.GotoLoc(h.LocFromVLoc(display.VLoc{ + SLoc: h.Scroll(v.StartLine, scrollmargin), + VisualX: 0, + })) + return true +} + +// CursorToViewCenter moves the cursor to the center of the view +func (h *BufPane) CursorToViewCenter() bool { + v := h.GetView() + h.Buf.ClearCursors() + h.Cursor.GotoLoc(h.LocFromVLoc(display.VLoc{ + SLoc: h.Scroll(v.StartLine, h.BufView().Height/2), + VisualX: 0, + })) + return true +} + +// CursorToViewBottom moves the cursor to the bottom of the view, +// offset by scrollmargin unless at the beginning or end of the file +func (h *BufPane) CursorToViewBottom() bool { + v := h.GetView() + h.Buf.ClearCursors() + scrollmargin := int(h.Buf.Settings["scrollmargin"].(float64)) + bEnd := h.SLocFromLoc(h.Buf.End()) + lastLine := h.Scroll(v.StartLine, h.BufView().Height-1) + if lastLine == bEnd { + scrollmargin = 0 + } + h.Cursor.GotoLoc(h.LocFromVLoc(display.VLoc{ + SLoc: h.Scroll(lastLine, -scrollmargin), + VisualX: 0, + })) + return true +} + // MoveCursorUp is not an action func (h *BufPane) MoveCursorUp(n int) { if !h.Buf.Settings["softwrap"].(bool) { diff --git a/internal/action/bufpane.go b/internal/action/bufpane.go index 34cf0f30..139d3b66 100644 --- a/internal/action/bufpane.go +++ b/internal/action/bufpane.go @@ -738,6 +738,9 @@ var BufKeyActions = map[string]BufKeyAction{ "CursorRight": (*BufPane).CursorRight, "CursorStart": (*BufPane).CursorStart, "CursorEnd": (*BufPane).CursorEnd, + "CursorToViewTop": (*BufPane).CursorToViewTop, + "CursorToViewCenter": (*BufPane).CursorToViewCenter, + "CursorToViewBottom": (*BufPane).CursorToViewBottom, "SelectToStart": (*BufPane).SelectToStart, "SelectToEnd": (*BufPane).SelectToEnd, "SelectUp": (*BufPane).SelectUp, diff --git a/runtime/help/keybindings.md b/runtime/help/keybindings.md index a77d6a7b..ecac2957 100644 --- a/runtime/help/keybindings.md +++ b/runtime/help/keybindings.md @@ -168,6 +168,9 @@ CursorLeft CursorRight CursorStart CursorEnd +CursorToViewTop +CursorToViewCenter +CursorToViewBottom SelectToStart SelectToEnd SelectUp