Skip to content
75 changes: 55 additions & 20 deletions internal/action/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func (h *BufPane) ScrollDown(n int) {
func (h *BufPane) ScrollAdjust() {
v := h.GetView()
end := h.SLocFromLoc(h.Buf.End())
if h.Diff(v.StartLine, end) < v.Height-1 {
v.StartLine = h.Scroll(end, -v.Height+1)
if h.Diff(v.StartLine, end) < h.BufView().Height-1 {
v.StartLine = h.Scroll(end, -h.BufView().Height+1)
}
h.SetView(v)
}
Expand Down Expand Up @@ -117,24 +117,63 @@ func (h *BufPane) ScrollDownAction() bool {
// Center centers the view on the cursor
func (h *BufPane) Center() bool {
v := h.GetView()
v.StartLine = h.Scroll(h.SLocFromLoc(h.Cursor.Loc), -v.Height/2)
v.StartLine = h.Scroll(h.SLocFromLoc(h.Cursor.Loc), -h.BufView().Height/2)
h.SetView(v)
h.ScrollAdjust()
return true
}

// MoveCursorUp is not an action
func (h *BufPane) MoveCursorUp(n int) {
if !h.Buf.Settings["softwrap"].(bool) {
h.Cursor.UpN(n)
} else {
vloc := h.VLocFromLoc(h.Cursor.Loc)
sloc := h.Scroll(vloc.SLoc, -n)
if sloc == vloc.SLoc {
// we are at the beginning of buffer
h.Cursor.Loc = h.Buf.Start()
h.Cursor.LastVisualX = 0
} else {
vloc.SLoc = sloc
vloc.VisualX = h.Cursor.LastVisualX
h.Cursor.Loc = h.LocFromVLoc(vloc)
}
}
}

// MoveCursorDown is not an action
func (h *BufPane) MoveCursorDown(n int) {
if !h.Buf.Settings["softwrap"].(bool) {
h.Cursor.DownN(n)
} else {
vloc := h.VLocFromLoc(h.Cursor.Loc)
sloc := h.Scroll(vloc.SLoc, n)
if sloc == vloc.SLoc {
// we are at the end of buffer
h.Cursor.Loc = h.Buf.End()
vloc = h.VLocFromLoc(h.Cursor.Loc)
h.Cursor.LastVisualX = vloc.VisualX
} else {
vloc.SLoc = sloc
vloc.VisualX = h.Cursor.LastVisualX
h.Cursor.Loc = h.LocFromVLoc(vloc)
}
}
}

// CursorUp moves the cursor up
func (h *BufPane) CursorUp() bool {
h.Cursor.Deselect(true)
h.Cursor.Up()
h.MoveCursorUp(1)
h.Relocate()
return true
}

// CursorDown moves the cursor down
func (h *BufPane) CursorDown() bool {
h.Cursor.Deselect(true)
h.Cursor.Down()
h.MoveCursorDown(1)
h.Relocate()
return true
}
Expand Down Expand Up @@ -212,7 +251,7 @@ func (h *BufPane) SelectUp() bool {
if !h.Cursor.HasSelection() {
h.Cursor.OrigSelection[0] = h.Cursor.Loc
}
h.Cursor.Up()
h.MoveCursorUp(1)
h.Cursor.SelectTo(h.Cursor.Loc)
h.Relocate()
return true
Expand All @@ -223,7 +262,7 @@ func (h *BufPane) SelectDown() bool {
if !h.Cursor.HasSelection() {
h.Cursor.OrigSelection[0] = h.Cursor.Loc
}
h.Cursor.Down()
h.MoveCursorDown(1)
h.Cursor.SelectTo(h.Cursor.Loc)
h.Relocate()
return true
Expand Down Expand Up @@ -1251,22 +1290,20 @@ func (h *BufPane) Start() bool {
// End moves the viewport to the end of the buffer
func (h *BufPane) End() bool {
v := h.GetView()
v.StartLine = h.Scroll(h.SLocFromLoc(h.Buf.End()), -v.Height+1)
v.StartLine = h.Scroll(h.SLocFromLoc(h.Buf.End()), -h.BufView().Height+1)
h.SetView(v)
return true
}

// PageUp scrolls the view up a page
func (h *BufPane) PageUp() bool {
v := h.GetView()
h.ScrollUp(v.Height)
h.ScrollUp(h.BufView().Height)
return true
}

// PageDown scrolls the view down a page
func (h *BufPane) PageDown() bool {
v := h.GetView()
h.ScrollDown(v.Height)
h.ScrollDown(h.BufView().Height)
h.ScrollAdjust()
return true
}
Expand All @@ -1276,7 +1313,7 @@ func (h *BufPane) SelectPageUp() bool {
if !h.Cursor.HasSelection() {
h.Cursor.OrigSelection[0] = h.Cursor.Loc
}
h.Cursor.UpN(h.GetView().Height)
h.MoveCursorUp(h.BufView().Height)
h.Cursor.SelectTo(h.Cursor.Loc)
h.Relocate()
return true
Expand All @@ -1287,7 +1324,7 @@ func (h *BufPane) SelectPageDown() bool {
if !h.Cursor.HasSelection() {
h.Cursor.OrigSelection[0] = h.Cursor.Loc
}
h.Cursor.DownN(h.GetView().Height)
h.MoveCursorDown(h.BufView().Height)
h.Cursor.SelectTo(h.Cursor.Loc)
h.Relocate()
return true
Expand All @@ -1302,7 +1339,7 @@ func (h *BufPane) CursorPageUp() bool {
h.Cursor.ResetSelection()
h.Cursor.StoreVisualX()
}
h.Cursor.UpN(h.GetView().Height)
h.MoveCursorUp(h.BufView().Height)
h.Relocate()
return true
}
Expand All @@ -1316,22 +1353,20 @@ func (h *BufPane) CursorPageDown() bool {
h.Cursor.ResetSelection()
h.Cursor.StoreVisualX()
}
h.Cursor.DownN(h.GetView().Height)
h.MoveCursorDown(h.BufView().Height)
h.Relocate()
return true
}

// HalfPageUp scrolls the view up half a page
func (h *BufPane) HalfPageUp() bool {
v := h.GetView()
h.ScrollUp(v.Height / 2)
h.ScrollUp(h.BufView().Height / 2)
return true
}

// HalfPageDown scrolls the view down half a page
func (h *BufPane) HalfPageDown() bool {
v := h.GetView()
h.ScrollDown(v.Height / 2)
h.ScrollDown(h.BufView().Height / 2)
h.ScrollAdjust()
return true
}
Expand Down
13 changes: 13 additions & 0 deletions internal/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,19 @@ type Buffer struct {
cursors []*Cursor
curCursor int
StartCursor Loc

// OptionCallback is called after a buffer option value is changed.
// The display module registers its OptionCallback to ensure the buffer window
// is properly updated when needed. This is a workaround for the fact that
// the buffer module cannot directly call the display's API (it would mean
// a circular dependency between packages).
OptionCallback func(option string, nativeValue interface{})

// The display module registers its own GetVisualX function for getting
// the correct visual x location of a cursor when softwrap is used.
// This is hacky. Maybe it would be better to move all the visual x logic
// from buffer to display, but it would require rewriting a lot of code.
GetVisualX func(loc Loc) int
}

// NewBufferFromFileAtLoc opens a new buffer with a given cursor location
Expand Down
4 changes: 4 additions & 0 deletions internal/buffer/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (c *Cursor) GotoLoc(l Loc) {

// GetVisualX returns the x value of the cursor in visual spaces
func (c *Cursor) GetVisualX() int {
if c.buf.GetVisualX != nil {
return c.buf.GetVisualX(c.Loc)
}

if c.X <= 0 {
c.X = 0
return 0
Expand Down
4 changes: 4 additions & 0 deletions internal/buffer/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error {
b.Type.Readonly = nativeValue.(bool)
}

if b.OptionCallback != nil {
b.OptionCallback(option, nativeValue)
}

return nil
}

Expand Down
1 change: 1 addition & 0 deletions internal/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ var defaultCommonSettings = map[string]interface{}{
"tabsize": float64(4),
"tabstospaces": false,
"useprimary": true,
"wordwrap": false,
}

func GetInfoBarOffset() int {
Expand Down
Loading