Skip to content

Commit b2dbcb3

Browse files
committed
implement nano-like page up/page down functionality
1 parent eb880d8 commit b2dbcb3

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

internal/action/actions.go

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,63 +1669,77 @@ func (h *BufPane) End() bool {
16691669

16701670
// PageUp scrolls the view up a page
16711671
func (h *BufPane) PageUp() bool {
1672-
h.ScrollUp(h.BufView().Height)
1672+
pageOverlap := int(h.Buf.Settings["pageoverlap"].(float64))
1673+
h.ScrollUp(h.BufView().Height - pageOverlap)
16731674
return true
16741675
}
16751676

16761677
// PageDown scrolls the view down a page
16771678
func (h *BufPane) PageDown() bool {
1678-
h.ScrollDown(h.BufView().Height)
1679+
pageOverlap := int(h.Buf.Settings["pageoverlap"].(float64))
1680+
h.ScrollDown(h.BufView().Height - pageOverlap)
16791681
h.ScrollAdjust()
16801682
return true
16811683
}
16821684

16831685
// SelectPageUp selects up one page
16841686
func (h *BufPane) SelectPageUp() bool {
1687+
pageOverlap := int(h.Buf.Settings["pageoverlap"].(float64))
1688+
scrollAmount := h.BufView().Height - pageOverlap
16851689
if !h.Cursor.HasSelection() {
16861690
h.Cursor.OrigSelection[0] = h.Cursor.Loc
16871691
}
1688-
h.MoveCursorUp(h.BufView().Height)
1692+
h.MoveCursorUp(scrollAmount)
16891693
h.Cursor.SelectTo(h.Cursor.Loc)
1694+
if h.Cursor.Num == 0 {
1695+
h.ScrollUp(scrollAmount)
1696+
}
16901697
h.Relocate()
16911698
return true
16921699
}
16931700

16941701
// SelectPageDown selects down one page
16951702
func (h *BufPane) SelectPageDown() bool {
1703+
pageOverlap := int(h.Buf.Settings["pageoverlap"].(float64))
1704+
scrollAmount := h.BufView().Height - pageOverlap
16961705
if !h.Cursor.HasSelection() {
16971706
h.Cursor.OrigSelection[0] = h.Cursor.Loc
16981707
}
1699-
h.MoveCursorDown(h.BufView().Height)
1708+
h.MoveCursorDown(scrollAmount)
17001709
h.Cursor.SelectTo(h.Cursor.Loc)
1710+
if h.Cursor.Num == 0 {
1711+
h.ScrollDown(scrollAmount)
1712+
h.ScrollAdjust()
1713+
}
17011714
h.Relocate()
17021715
return true
17031716
}
17041717

1705-
// CursorPageUp places the cursor a page up
1718+
// CursorPageUp places the cursor a page up,
1719+
// moving the view to keep cursor at the same relative position in the view
17061720
func (h *BufPane) CursorPageUp() bool {
17071721
h.Cursor.Deselect(true)
1708-
1709-
if h.Cursor.HasSelection() {
1710-
h.Cursor.Loc = h.Cursor.CurSelection[0]
1711-
h.Cursor.ResetSelection()
1712-
h.Cursor.StoreVisualX()
1722+
pageOverlap := int(h.Buf.Settings["pageoverlap"].(float64))
1723+
scrollAmount := h.BufView().Height - pageOverlap
1724+
h.MoveCursorUp(scrollAmount)
1725+
if h.Cursor.Num == 0 {
1726+
h.ScrollUp(scrollAmount)
17131727
}
1714-
h.MoveCursorUp(h.BufView().Height)
17151728
h.Relocate()
17161729
return true
17171730
}
17181731

1719-
// CursorPageDown places the cursor a page up
1732+
// CursorPageDown places the cursor a page down,
1733+
// moving the view to keep cursor at the same relative position in the view
17201734
func (h *BufPane) CursorPageDown() bool {
17211735
h.Cursor.Deselect(false)
1722-
1723-
if h.Cursor.HasSelection() {
1724-
h.Cursor.Loc = h.Cursor.CurSelection[1]
1725-
h.Cursor.ResetSelection()
1726-
h.Cursor.StoreVisualX()
1736+
pageOverlap := int(h.Buf.Settings["pageoverlap"].(float64))
1737+
scrollAmount := h.BufView().Height - pageOverlap
1738+
h.MoveCursorDown(scrollAmount)
1739+
if h.Cursor.Num == 0 {
1740+
h.ScrollDown(scrollAmount)
1741+
h.ScrollAdjust()
17271742
}
1728-
h.MoveCursorDown(h.BufView().Height)
17291743
h.Relocate()
17301744
return true
17311745
}

internal/config/settings.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var optionValidators = map[string]optionValidator{
3232
"helpsplit": validateChoice,
3333
"matchbracestyle": validateChoice,
3434
"multiopen": validateChoice,
35+
"pageoverlap": validateNonNegativeValue,
3536
"reload": validateChoice,
3637
"scrollmargin": validateNonNegativeValue,
3738
"scrollspeed": validateNonNegativeValue,
@@ -76,6 +77,7 @@ var defaultCommonSettings = map[string]interface{}{
7677
"matchbraceleft": true,
7778
"matchbracestyle": "underline",
7879
"mkparents": false,
80+
"pageoverlap": float64(2),
7981
"permbackup": false,
8082
"readonly": false,
8183
"reload": "prompt",

runtime/help/options.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,13 @@ Here are the available options:
285285

286286
default value: `tab`
287287

288+
* `pageoverlap`: the number of lines from the current view to keep in view
289+
when paging up or down. If this is set to 2, for instance, and you page
290+
down, the last two lines of the previous page will be the first two lines
291+
of the next page.
292+
293+
default value: `2`
294+
288295
* `paste`: treat characters sent from the terminal in a single chunk as a paste
289296
event rather than a series of manual key presses. If you are pasting using
290297
the terminal keybinding (not `Ctrl-v`, which is micro's default paste

0 commit comments

Comments
 (0)