]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/cursor.go
Code optimisation (#1117)
[micro.git] / cmd / micro / cursor.go
index cd1a165700a6752c3143352ae0edfb7d8014ac20..452268ba12b1f3802623be7fca8c5d33097e81e0 100644 (file)
@@ -1,5 +1,9 @@
 package main
 
+import (
+       "github.com/zyedidia/clipboard"
+)
+
 // The Cursor struct stores the location of the cursor in the view
 // The complicated part about the cursor is storing its location.
 // The cursor must be displayed at an x, y location, but since the buffer
@@ -19,20 +23,51 @@ type Cursor struct {
        // This is used for line and word selection where it is necessary
        // to know what the original selection was
        OrigSelection [2]Loc
+
+       // Which cursor index is this (for multiple cursors)
+       Num int
 }
 
-// Goto puts the cursor at the given cursor's location and gives the current cursor its selection too
+// Goto puts the cursor at the given cursor's location and gives
+// the current cursor its selection too
 func (c *Cursor) Goto(b Cursor) {
        c.X, c.Y, c.LastVisualX = b.X, b.Y, b.LastVisualX
        c.OrigSelection, c.CurSelection = b.OrigSelection, b.CurSelection
 }
 
+// GotoLoc puts the cursor at the given cursor's location and gives
+// the current cursor its selection too
+func (c *Cursor) GotoLoc(l Loc) {
+       c.X, c.Y = l.X, l.Y
+       c.LastVisualX = c.GetVisualX()
+}
+
+// CopySelection copies the user's selection to either "primary"
+// or "clipboard"
+func (c *Cursor) CopySelection(target string) {
+       if c.HasSelection() {
+               if target != "primary" || c.buf.Settings["useprimary"].(bool) {
+                       clipboard.WriteAll(c.GetSelection(), target)
+               }
+       }
+}
+
 // ResetSelection resets the user's selection
 func (c *Cursor) ResetSelection() {
        c.CurSelection[0] = c.buf.Start()
        c.CurSelection[1] = c.buf.Start()
 }
 
+// SetSelectionStart sets the start of the selection
+func (c *Cursor) SetSelectionStart(pos Loc) {
+       c.CurSelection[0] = pos
+}
+
+// SetSelectionEnd sets the end of the selection
+func (c *Cursor) SetSelectionEnd(pos Loc) {
+       c.CurSelection[1] = pos
+}
+
 // HasSelection returns whether or not the user has selected anything
 func (c *Cursor) HasSelection() bool {
        return c.CurSelection[0] != c.CurSelection[1]
@@ -43,7 +78,7 @@ func (c *Cursor) DeleteSelection() {
        if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
                c.buf.Remove(c.CurSelection[1], c.CurSelection[0])
                c.Loc = c.CurSelection[1]
-       } else if c.GetSelection() == "" {
+       } else if !c.HasSelection() {
                return
        } else {
                c.buf.Remove(c.CurSelection[0], c.CurSelection[1])
@@ -53,21 +88,24 @@ func (c *Cursor) DeleteSelection() {
 
 // GetSelection returns the cursor's selection
 func (c *Cursor) GetSelection() string {
-       if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
-               return c.buf.Substr(c.CurSelection[1], c.CurSelection[0])
+       if InBounds(c.CurSelection[0], c.buf) && InBounds(c.CurSelection[1], c.buf) {
+               if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
+                       return c.buf.Substr(c.CurSelection[1], c.CurSelection[0])
+               }
+               return c.buf.Substr(c.CurSelection[0], c.CurSelection[1])
        }
-       return c.buf.Substr(c.CurSelection[0], c.CurSelection[1])
+       return ""
 }
 
 // SelectLine selects the current line
 func (c *Cursor) SelectLine() {
        c.Start()
-       c.CurSelection[0] = c.Loc
+       c.SetSelectionStart(c.Loc)
        c.End()
        if c.buf.NumLines-1 > c.Y {
-               c.CurSelection[1] = c.Loc.Move(1, c.buf)
+               c.SetSelectionEnd(c.Loc.Move(1, c.buf))
        } else {
-               c.CurSelection[1] = c.Loc
+               c.SetSelectionEnd(c.Loc)
        }
 
        c.OrigSelection = c.CurSelection
@@ -77,13 +115,13 @@ func (c *Cursor) SelectLine() {
 func (c *Cursor) AddLineToSelection() {
        if c.Loc.LessThan(c.OrigSelection[0]) {
                c.Start()
-               c.CurSelection[0] = c.Loc
-               c.CurSelection[1] = c.OrigSelection[1]
+               c.SetSelectionStart(c.Loc)
+               c.SetSelectionEnd(c.OrigSelection[1])
        }
        if c.Loc.GreaterThan(c.OrigSelection[1]) {
                c.End()
-               c.CurSelection[1] = c.Loc.Move(1, c.buf)
-               c.CurSelection[0] = c.OrigSelection[0]
+               c.SetSelectionEnd(c.Loc.Move(1, c.buf))
+               c.SetSelectionStart(c.OrigSelection[0])
        }
 
        if c.Loc.LessThan(c.OrigSelection[1]) && c.Loc.GreaterThan(c.OrigSelection[0]) {
@@ -98,8 +136,8 @@ func (c *Cursor) SelectWord() {
        }
 
        if !IsWordChar(string(c.RuneUnder(c.X))) {
-               c.CurSelection[0] = c.Loc
-               c.CurSelection[1] = c.Loc.Move(1, c.buf)
+               c.SetSelectionStart(c.Loc)
+               c.SetSelectionEnd(c.Loc.Move(1, c.buf))
                c.OrigSelection = c.CurSelection
                return
        }
@@ -110,19 +148,20 @@ func (c *Cursor) SelectWord() {
                backward--
        }
 
-       c.CurSelection[0] = Loc{backward, c.Y}
+       c.SetSelectionStart(Loc{backward, c.Y})
        c.OrigSelection[0] = c.CurSelection[0]
 
        for forward < Count(c.buf.Line(c.Y))-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
                forward++
        }
 
-       c.CurSelection[1] = Loc{forward, c.Y}.Move(1, c.buf)
+       c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
        c.OrigSelection[1] = c.CurSelection[1]
        c.Loc = c.CurSelection[1]
 }
 
-// AddWordToSelection adds the word the cursor is currently on to the selection
+// AddWordToSelection adds the word the cursor is currently on
+// to the selection
 func (c *Cursor) AddWordToSelection() {
        if c.Loc.GreaterThan(c.OrigSelection[0]) && c.Loc.LessThan(c.OrigSelection[1]) {
                c.CurSelection = c.OrigSelection
@@ -136,8 +175,8 @@ func (c *Cursor) AddWordToSelection() {
                        backward--
                }
 
-               c.CurSelection[0] = Loc{backward, c.Y}
-               c.CurSelection[1] = c.OrigSelection[1]
+               c.SetSelectionStart(Loc{backward, c.Y})
+               c.SetSelectionEnd(c.OrigSelection[1])
        }
 
        if c.Loc.GreaterThan(c.OrigSelection[1]) {
@@ -147,21 +186,22 @@ func (c *Cursor) AddWordToSelection() {
                        forward++
                }
 
-               c.CurSelection[1] = Loc{forward, c.Y}.Move(1, c.buf)
-               c.CurSelection[0] = c.OrigSelection[0]
+               c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
+               c.SetSelectionStart(c.OrigSelection[0])
        }
 
        c.Loc = c.CurSelection[1]
 }
 
-// SelectTo selects from the current cursor location to the given location
+// SelectTo selects from the current cursor location to the given
+// location
 func (c *Cursor) SelectTo(loc Loc) {
        if loc.GreaterThan(c.OrigSelection[0]) {
-               c.CurSelection[0] = c.OrigSelection[0]
-               c.CurSelection[1] = loc
+               c.SetSelectionStart(c.OrigSelection[0])
+               c.SetSelectionEnd(loc)
        } else {
-               c.CurSelection[0] = loc
-               c.CurSelection[1] = c.OrigSelection[0]
+               c.SetSelectionStart(loc)
+               c.SetSelectionEnd(c.OrigSelection[0])
        }
 }
 
@@ -221,19 +261,19 @@ func (c *Cursor) UpN(amount int) {
        proposedY := c.Y - amount
        if proposedY < 0 {
                proposedY = 0
+               c.LastVisualX = 0
        } else if proposedY >= c.buf.NumLines {
                proposedY = c.buf.NumLines - 1
        }
-       if proposedY == c.Y {
-               return
-       }
 
-       c.Y = proposedY
        runes := []rune(c.buf.Line(c.Y))
-       c.X = c.GetCharPosInLine(c.Y, c.LastVisualX)
-       if c.X > len(runes) {
+       c.X = c.GetCharPosInLine(proposedY, c.LastVisualX)
+
+       if c.X > len(runes) || (amount < 0 && proposedY == c.Y) {
                c.X = len(runes)
        }
+
+       c.Y = proposedY
 }
 
 // DownN moves the cursor down N lines (if possible)
@@ -251,7 +291,8 @@ func (c *Cursor) Down() {
        c.DownN(1)
 }
 
-// Left moves the cursor left one cell (if possible) or to the last line if it is at the beginning
+// Left moves the cursor left one cell (if possible) or to
+// the previous line if it is at the beginning
 func (c *Cursor) Left() {
        if c.Loc == c.buf.Start() {
                return
@@ -265,7 +306,8 @@ func (c *Cursor) Left() {
        c.LastVisualX = c.GetVisualX()
 }
 
-// Right moves the cursor right one cell (if possible) or to the next line if it is at the end
+// Right moves the cursor right one cell (if possible) or
+// to the next line if it is at the end
 func (c *Cursor) Right() {
        if c.Loc == c.buf.End() {
                return
@@ -291,7 +333,21 @@ func (c *Cursor) Start() {
        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)
+// StartOfText moves the cursor to the first non-whitespace rune of 
+// the line it is on
+func (c *Cursor) StartOfText() {
+       c.Start()
+       for IsWhitespace(c.RuneUnder(c.X)) {
+               if c.X == Count(c.buf.Line(c.Y)) {
+                       break
+               }
+               c.Right()
+       }
+}
+
+// GetCharPosInLine gets the char position of a visual x y
+// coordinate (this is necessary because tabs are 1 char but
+// 4 visual spaces)
 func (c *Cursor) GetCharPosInLine(lineNum, visualPos int) int {
        // Get the tab size
        tabSize := int(c.buf.Settings["tabsize"].(float64))
@@ -310,11 +366,25 @@ func (c *Cursor) GetCharPosInLine(lineNum, visualPos int) int {
 func (c *Cursor) GetVisualX() int {
        runes := []rune(c.buf.Line(c.Y))
        tabSize := int(c.buf.Settings["tabsize"].(float64))
+       if c.X > len(runes) {
+               c.X = len(runes) - 1
+       }
+
+       if c.X < 0 {
+               c.X = 0
+       }
+
        return StringWidth(string(runes[:c.X]), tabSize)
 }
 
-// Relocate makes sure that the cursor is inside the bounds of the buffer
-// If it isn't, it moves it to be within the buffer's lines
+// StoreVisualX stores the current visual x value in the cursor
+func (c *Cursor) StoreVisualX() {
+       c.LastVisualX = c.GetVisualX()
+}
+
+// Relocate makes sure that the cursor is inside the bounds
+// of the buffer If it isn't, it moves it to be within the
+// buffer's lines
 func (c *Cursor) Relocate() {
        if c.Y < 0 {
                c.Y = 0