Do not move the cursor beyond EOF

This commit is contained in:
Roi Martin 2017-08-19 00:06:54 +02:00
parent a4d0b30476
commit 75a7ad2750
1 changed files with 19 additions and 13 deletions

16
edit.go
View File

@ -148,11 +148,13 @@ func (v *View) MoveCursor(dx, dy int, writeMode bool) {
// adjust cursor's x position and view's x origin
if x > curLineWidth { // move to next line
if dx > 0 { // horizontal movement
cy++
if writeMode || v.oy+cy < len(v.viewLines) {
if !v.Wrap {
v.ox = 0
}
v.cx = 0
cy++
}
} else { // vertical movement
if curLineWidth > 0 { // move cursor to the EOL
if v.Wrap {
@ -170,17 +172,20 @@ func (v *View) MoveCursor(dx, dy int, writeMode bool) {
}
}
} else {
if writeMode || v.oy+cy < len(v.viewLines) {
if !v.Wrap {
v.ox = 0
}
v.cx = 0
}
}
}
} else if cx < 0 {
if !v.Wrap && v.ox > 0 { // move origin to the left
v.ox += cx
v.cx = 0
} else { // move to previous line
cy--
if prevLineWidth > 0 {
if !v.Wrap { // set origin so the EOL is visible
nox := prevLineWidth - maxX + 1
@ -197,7 +202,6 @@ func (v *View) MoveCursor(dx, dy int, writeMode bool) {
}
v.cx = 0
}
cy--
}
} else { // stay on the same line
if v.Wrap {
@ -213,15 +217,17 @@ func (v *View) MoveCursor(dx, dy int, writeMode bool) {
}
// adjust cursor's y position and view's y origin
if cy >= maxY {
v.oy++
} else if cy < 0 {
if cy < 0 {
if v.oy > 0 {
v.oy--
}
} else if writeMode || v.oy+cy < len(v.viewLines) {
if cy >= maxY {
v.oy++
} else {
v.cy = cy
}
}
}
// writeRune writes a rune into the view's internal buffer, at the