]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/view.go
Copy to primary clipboard for any change in selection
[micro.git] / cmd / micro / view.go
index 28d59d120b607876284c9e380bb40bd16d4f4ff3..996ac8bdd39cde4e67e443c5fbc07f2f34453485 100644 (file)
@@ -145,6 +145,20 @@ func (v *View) ToggleTabbar() {
        }
 }
 
+func (v *View) paste(clip string) {
+       leadingWS := GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))
+
+       if v.Cursor.HasSelection() {
+               v.Cursor.DeleteSelection()
+               v.Cursor.ResetSelection()
+       }
+       clip = strings.Replace(clip, "\n", "\n"+leadingWS, -1)
+       v.Buf.Insert(v.Cursor.Loc, clip)
+       v.Cursor.Loc = v.Cursor.Loc.Move(Count(clip), v.Buf)
+       v.freshClip = false
+       messenger.Message("Pasted clipboard")
+}
+
 // ScrollUp scrolls the view up n lines (if possible)
 func (v *View) ScrollUp(n int) {
        // Try to scroll by n but if it would overflow, scroll by 1
@@ -400,8 +414,8 @@ func (v *View) HandleEvent(event tcell.Event) {
                                        v.lastClickTime = time.Now()
 
                                        v.Cursor.OrigSelection[0] = v.Cursor.Loc
-                                       v.Cursor.CurSelection[0] = v.Cursor.Loc
-                                       v.Cursor.CurSelection[1] = v.Cursor.Loc
+                                       v.Cursor.SetSelectionStart(v.Cursor.Loc)
+                                       v.Cursor.SetSelectionEnd(v.Cursor.Loc)
                                }
                                v.mouseReleased = false
                        } else if !v.mouseReleased {
@@ -411,9 +425,13 @@ func (v *View) HandleEvent(event tcell.Event) {
                                } else if v.doubleClick {
                                        v.Cursor.AddWordToSelection()
                                } else {
-                                       v.Cursor.CurSelection[1] = v.Cursor.Loc
+                                       v.Cursor.SetSelectionEnd(v.Cursor.Loc)
                                }
                        }
+               case tcell.Button2:
+                       // Middle mouse button was clicked,
+                       // We should paste primary
+                       v.PastePrimary(true)
                case tcell.ButtonNone:
                        // Mouse event with no click
                        if !v.mouseReleased {
@@ -427,7 +445,7 @@ func (v *View) HandleEvent(event tcell.Event) {
 
                                if !v.doubleClick && !v.tripleClick {
                                        v.MoveToMouseClick(x, y)
-                                       v.Cursor.CurSelection[1] = v.Cursor.Loc
+                                       v.Cursor.SetSelectionEnd(v.Cursor.Loc)
                                }
                                v.mouseReleased = true
                        }