From 8bd23a575f2d28a34b0af7a20edaf11f63a8060f Mon Sep 17 00:00:00 2001 From: JT Olds Date: Tue, 31 May 2016 16:02:42 -0600 Subject: [PATCH] Add CursorPageUp and CursorPageDown actions --- cmd/micro/bindings.go | 22 +++++++++++++++++++++ cmd/micro/cursor.go | 45 ++++++++++++++++++++++++++----------------- 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/cmd/micro/bindings.go b/cmd/micro/bindings.go index c450ce19..7e6f7081 100644 --- a/cmd/micro/bindings.go +++ b/cmd/micro/bindings.go @@ -19,6 +19,8 @@ var helpBinding string var bindingActions = map[string]func(*View) bool{ "CursorUp": (*View).CursorUp, "CursorDown": (*View).CursorDown, + "CursorPageUp": (*View).CursorPageUp, + "CursorPageDown": (*View).CursorPageDown, "CursorLeft": (*View).CursorLeft, "CursorRight": (*View).CursorRight, "CursorStart": (*View).CursorStart, @@ -884,6 +886,26 @@ func (v *View) PageDown() bool { return false } +// CursorPageUp places the cursor a page up +func (v *View) CursorPageUp() bool { + if v.Cursor.HasSelection() { + v.Cursor.SetLoc(v.Cursor.CurSelection[0]) + v.Cursor.ResetSelection() + } + v.Cursor.UpN(v.height) + return true +} + +// CursorPageDown places the cursor a page up +func (v *View) CursorPageDown() bool { + if v.Cursor.HasSelection() { + v.Cursor.SetLoc(v.Cursor.CurSelection[1]) + v.Cursor.ResetSelection() + } + v.Cursor.DownN(v.height) + return true +} + // HalfPageUp scrolls the view up half a page func (v *View) HalfPageUp() bool { if v.Topline > v.height/2 { diff --git a/cmd/micro/cursor.go b/cmd/micro/cursor.go index d3a6176c..2284501a 100644 --- a/cmd/micro/cursor.go +++ b/cmd/micro/cursor.go @@ -276,30 +276,39 @@ func (c *Cursor) RuneUnder(x int) rune { return line[x] } +// UpN moves the cursor up N lines (if possible) +func (c *Cursor) UpN(amount int) { + proposedY := c.Y - amount + if proposedY < 0 { + proposedY = 0 + } else if proposedY >= c.buf.NumLines { + proposedY = c.buf.NumLines - 1 + } + if proposedY == c.Y { + return + } + + c.Y = proposedY + runes := []rune(c.buf.Lines[c.Y]) + c.X = c.GetCharPosInLine(c.Y, c.LastVisualX) + if c.X > len(runes) { + c.X = len(runes) + } +} + +// DownN moves the cursor down N lines (if possible) +func (c *Cursor) DownN(amount int) { + c.UpN(-amount) +} + // Up moves the cursor up one line (if possible) func (c *Cursor) Up() { - if c.Y > 0 { - c.Y-- - - runes := []rune(c.buf.Lines[c.Y]) - c.X = c.GetCharPosInLine(c.Y, c.LastVisualX) - if c.X > len(runes) { - c.X = len(runes) - } - } + c.UpN(1) } // Down moves the cursor down one line (if possible) func (c *Cursor) Down() { - if c.Y < c.buf.NumLines-1 { - c.Y++ - - runes := []rune(c.buf.Lines[c.Y]) - c.X = c.GetCharPosInLine(c.Y, c.LastVisualX) - if c.X > len(runes) { - c.X = len(runes) - } - } + c.DownN(1) } // Left moves the cursor left one cell (if possible) or to the last line if it is at the beginning