Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -1327,23 +1327,48 @@ func (g *Gui) onKey(ev *GocuiEvent) error {
}
}

// newCx and newCy are relative to the view port, i.e. to the visible area of the view
newCx := mx - v.x0 - 1
newCy := my - v.y0 - 1
// if view is editable don't go further than the furthest character for that line
if v.Editable && newCy >= 0 && newCy <= len(v.lines)-1 {
lastCharForLine := len(v.lines[newCy])
if lastCharForLine < newCx {
newCx = lastCharForLine
// newX and newY are relative to the view's content, independent of its scroll position
newX := newCx + v.ox
newY := newCy + v.oy
// if view is editable don't go further than the furthest character for that line
if v.Editable {
if newY < 0 {
newY = 0
newCy = -v.oy
} else if newY >= len(v.lines) {
newY = len(v.lines) - 1
newCy = newY - v.oy
}

lastCharForLine := len(v.lines[newY])
for lastCharForLine > 0 && v.lines[newY][lastCharForLine-1].chr == 0 {
lastCharForLine--
}
if lastCharForLine < newX {
newX = lastCharForLine
newCx = lastCharForLine - v.ox
}
}
if !IsMouseScrollKey(ev.Key) {
if err := v.SetCursor(newCx, newCy); err != nil {
return err
}
if v.Editable {
v.TextArea.SetCursor2D(newX, newY)

// SetCursor2D might have adjusted the text area's cursor to the
// left to move left from a soft line break, so we need to
// update the view's cursor to match the text area's cursor.
cX, _ := v.TextArea.GetCursorXY()
v.SetCursorX(cX)
}
}

if IsMouseKey(ev.Key) {
opts := ViewMouseBindingOpts{X: newCx + v.ox, Y: newCy + v.oy}
opts := ViewMouseBindingOpts{X: newX, Y: newY}
matched, err := g.execMouseKeybindings(v, ev, opts)
if err != nil {
return err
Expand Down
22 changes: 15 additions & 7 deletions text_area.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,7 @@ func (self *TextArea) GoToEndOfLine() {

self.cursor = self.closestNewlineOnRight()

// If the end of line is a soft line break, we need to move left by one so
// that we end up at the last whitespace before the line break. Otherwise
// we'd be at the start of the next line, since the newline character
// doesn't really exist in the real content.
if self.cursor < len(self.content) && self.content[self.cursor] != '\n' {
self.cursor--
}
self.moveLeftFromSoftLineBreak()
}

func (self *TextArea) closestNewlineOnRight() int {
Expand All @@ -303,6 +297,16 @@ func (self *TextArea) closestNewlineOnRight() int {
return len(self.content)
}

func (self *TextArea) moveLeftFromSoftLineBreak() {
// If the end of line is a soft line break, we need to move left by one so
// that we end up at the last whitespace before the line break. Otherwise
// we'd be at the start of the next line, since the newline character
// doesn't really exist in the real content.
if self.cursor < len(self.content) && self.content[self.cursor] != '\n' {
self.cursor--
}
}

func (self *TextArea) atLineStart() bool {
return self.cursor == 0 ||
(len(self.content) > self.cursor-1 && self.content[self.cursor-1] == '\n')
Expand Down Expand Up @@ -420,12 +424,16 @@ func (self *TextArea) SetCursor2D(x int, y int) {
for _, r := range self.wrappedContent {
if x <= 0 && y == 0 {
self.cursor = self.wrappedCursorToOrigCursor(newCursor)
if self.wrappedContent[newCursor] == '\n' {
self.moveLeftFromSoftLineBreak()
}
return
}

if r == '\n' {
if y == 0 {
self.cursor = self.wrappedCursorToOrigCursor(newCursor)
self.moveLeftFromSoftLineBreak()
return
}
y--
Expand Down