]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/cursor.go
Merge pull request #570 from yursan9/yaml
[micro.git] / cmd / micro / cursor.go
index c6eb92aec192c8bbe8f93055739b6b4503560c60..b864974af927c4632667162c48534e935a7cf4cf 100644 (file)
@@ -29,6 +29,15 @@ func (c *Cursor) Goto(b Cursor) {
        c.OrigSelection, c.CurSelection = b.OrigSelection, b.CurSelection
 }
 
+// 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()
@@ -38,19 +47,11 @@ func (c *Cursor) ResetSelection() {
 // SetSelectionStart sets the start of the selection
 func (c *Cursor) SetSelectionStart(pos Loc) {
        c.CurSelection[0] = pos
-       // Copy to primary clipboard for linux
-       if c.HasSelection() {
-               clipboard.WriteAll(c.GetSelection(), "primary")
-       }
 }
 
 // SetSelectionEnd sets the end of the selection
 func (c *Cursor) SetSelectionEnd(pos Loc) {
        c.CurSelection[1] = pos
-       // Copy to primary clipboard for linux
-       if c.HasSelection() {
-               clipboard.WriteAll(c.GetSelection(), "primary")
-       }
 }
 
 // HasSelection returns whether or not the user has selected anything
@@ -73,10 +74,13 @@ 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