]> git.lizzy.rs Git - micro.git/commitdiff
Implement lastX for cursor movement
authorZachary Yedidia <zyedidia@gmail.com>
Mon, 4 Apr 2016 18:09:24 +0000 (14:09 -0400)
committerZachary Yedidia <zyedidia@gmail.com>
Mon, 4 Apr 2016 18:09:24 +0000 (14:09 -0400)
src/cursor.go
src/view.go

index f54ae6bf9170c2318dc9b280ac2bbebd79f2bee7..ffbedf0ab7e4bf795485f61d8c49f676e225e20b 100644 (file)
@@ -42,6 +42,9 @@ type Cursor struct {
        x int
        y int
 
+       // Last cursor x position
+       lastVisualX int
+
        // The current selection as a range of character numbers (inclusive)
        curSelection [2]int
        // The original selection as a range of character numbers
@@ -200,6 +203,7 @@ func (c *Cursor) Up() {
                c.y--
 
                runes := []rune(c.v.buf.lines[c.y])
+               c.x = c.GetCharPosInLine(c.y, c.lastVisualX)
                if c.x > len(runes) {
                        c.x = len(runes)
                }
@@ -212,6 +216,7 @@ func (c *Cursor) Down() {
                c.y++
 
                runes := []rune(c.v.buf.lines[c.y])
+               c.x = c.GetCharPosInLine(c.y, c.lastVisualX)
                if c.x > len(runes) {
                        c.x = len(runes)
                }
@@ -229,6 +234,7 @@ func (c *Cursor) Left() {
                c.Up()
                c.End()
        }
+       c.lastVisualX = c.GetVisualX()
 }
 
 // Right moves the cursor right one cell (if possible) or to the next line if it is at the end
@@ -242,16 +248,19 @@ func (c *Cursor) Right() {
                c.Down()
                c.Start()
        }
+       c.lastVisualX = c.GetVisualX()
 }
 
 // End moves the cursor to the end of the line it is on
 func (c *Cursor) End() {
        c.x = Count(c.v.buf.lines[c.y])
+       c.lastVisualX = c.GetVisualX()
 }
 
 // Start moves the cursor to the start of the line it is on
 func (c *Cursor) Start() {
        c.x = 0
+       c.lastVisualX = c.GetVisualX()
 }
 
 // GetCharPosInLine gets the char position of a visual x y coordinate (this is necessary because tabs are 1 char but 4 visual spaces)
index 2fce237ff70067588ff940ff3d5024fb06218fe9..beef181feb84e96a9b934192e1efd3462176283f 100644 (file)
@@ -330,6 +330,7 @@ func (v *View) MoveToMouseClick(x, y int) {
        }
        v.cursor.x = x
        v.cursor.y = y
+       v.cursor.lastVisualX = v.cursor.GetVisualX()
 }
 
 // HandleEvent handles an event passed by the main loop