]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/actions.go
Merge cursors properly
[micro.git] / cmd / micro / actions.go
index a4aab0165bfa15e971f6a9d12a1f97f5c949898d..5b29e1e91a1047c773cd43c5e42c4b6e2a2f38f0 100644 (file)
@@ -8,13 +8,14 @@ import (
 
        "github.com/yuin/gopher-lua"
        "github.com/zyedidia/clipboard"
+       "github.com/zyedidia/tcell"
 )
 
 // PreActionCall executes the lua pre callback if possible
-func PreActionCall(funcName string, view *View) bool {
+func PreActionCall(funcName string, view *View, args ...interface{}) bool {
        executeAction := true
        for pl := range loadedPlugins {
-               ret, err := Call(pl+".pre"+funcName, view)
+               ret, err := Call(pl+".pre"+funcName, append([]interface{}{view}, args...)...)
                if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
                        TermMessage(err)
                        continue
@@ -27,10 +28,10 @@ func PreActionCall(funcName string, view *View) bool {
 }
 
 // PostActionCall executes the lua plugin callback if possible
-func PostActionCall(funcName string, view *View) bool {
+func PostActionCall(funcName string, view *View, args ...interface{}) bool {
        relocate := true
        for pl := range loadedPlugins {
-               ret, err := Call(pl+".on"+funcName, view)
+               ret, err := Call(pl+".on"+funcName, append([]interface{}{view}, args...)...)
                if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
                        TermMessage(err)
                        continue
@@ -51,6 +52,98 @@ func (v *View) deselect(index int) bool {
        return false
 }
 
+// MousePress is the event that should happen when a normal click happens
+// This is almost always bound to left click
+func (v *View) MousePress(usePlugin bool, e *tcell.EventMouse) bool {
+       if usePlugin && !PreActionCall("MousePress", v, e) {
+               return false
+       }
+
+       x, y := e.Position()
+       x -= v.lineNumOffset - v.leftCol + v.x
+       y += v.Topline - v.y
+
+       // This is usually bound to left click
+       if v.mouseReleased {
+               v.MoveToMouseClick(x, y)
+               if time.Since(v.lastClickTime)/time.Millisecond < doubleClickThreshold {
+                       if v.doubleClick {
+                               // Triple click
+                               v.lastClickTime = time.Now()
+
+                               v.tripleClick = true
+                               v.doubleClick = false
+
+                               v.Cursor.SelectLine()
+                               v.Cursor.CopySelection("primary")
+                       } else {
+                               // Double click
+                               v.lastClickTime = time.Now()
+
+                               v.doubleClick = true
+                               v.tripleClick = false
+
+                               v.Cursor.SelectWord()
+                               v.Cursor.CopySelection("primary")
+                       }
+               } else {
+                       v.doubleClick = false
+                       v.tripleClick = false
+                       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.mouseReleased = false
+       } else if !v.mouseReleased {
+               v.MoveToMouseClick(x, y)
+               if v.tripleClick {
+                       v.Cursor.AddLineToSelection()
+               } else if v.doubleClick {
+                       v.Cursor.AddWordToSelection()
+               } else {
+                       v.Cursor.SetSelectionEnd(v.Cursor.Loc)
+                       v.Cursor.CopySelection("primary")
+               }
+       }
+
+       if usePlugin {
+               PostActionCall("MousePress", v, e)
+       }
+       return false
+}
+
+// ScrollUpAction scrolls the view up
+func (v *View) ScrollUpAction(usePlugin bool) bool {
+       if usePlugin && !PreActionCall("ScrollUp", v) {
+               return false
+       }
+
+       scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
+       v.ScrollUp(scrollspeed)
+
+       if usePlugin {
+               PostActionCall("ScrollUp", v)
+       }
+       return false
+}
+
+// ScrollDownAction scrolls the view up
+func (v *View) ScrollDownAction(usePlugin bool) bool {
+       if usePlugin && !PreActionCall("ScrollDown", v) {
+               return false
+       }
+
+       scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
+       v.ScrollDown(scrollspeed)
+
+       if usePlugin {
+               PostActionCall("ScrollDown", v)
+       }
+       return false
+}
+
 // Center centers the view on the cursor
 func (v *View) Center(usePlugin bool) bool {
        if usePlugin && !PreActionCall("Center", v) {
@@ -461,7 +554,7 @@ func (v *View) InsertSpace(usePlugin bool) bool {
                v.Cursor.ResetSelection()
        }
        v.Buf.Insert(v.Cursor.Loc, " ")
-       v.Cursor.Right()
+       // v.Cursor.Right()
 
        if usePlugin {
                return PostActionCall("InsertSpace", v)
@@ -481,15 +574,15 @@ func (v *View) InsertNewline(usePlugin bool) bool {
                v.Cursor.ResetSelection()
        }
 
-       v.Buf.Insert(v.Cursor.Loc, "\n")
        ws := GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))
-       v.Cursor.Right()
+       v.Buf.Insert(v.Cursor.Loc, "\n")
+       // v.Cursor.Right()
 
        if v.Buf.Settings["autoindent"].(bool) {
                v.Buf.Insert(v.Cursor.Loc, ws)
-               for i := 0; i < len(ws); i++ {
-                       v.Cursor.Right()
-               }
+               // for i := 0; i < len(ws); i++ {
+               //      v.Cursor.Right()
+               // }
 
                // Remove the whitespaces if keepautoindent setting is off
                if IsSpacesOrTabs(v.Buf.Line(v.Cursor.Y-1)) && !v.Buf.Settings["keepautoindent"].(bool) {
@@ -529,18 +622,10 @@ func (v *View) Backspace(usePlugin bool) bool {
                tabSize := int(v.Buf.Settings["tabsize"].(float64))
                if v.Buf.Settings["tabstospaces"].(bool) && IsSpaces(lineStart) && len(lineStart) != 0 && len(lineStart)%tabSize == 0 {
                        loc := v.Cursor.Loc
-                       v.Cursor.Loc = loc.Move(-tabSize, v.Buf)
-                       cx, cy := v.Cursor.X, v.Cursor.Y
-                       v.Cursor.Loc = loc
                        v.Buf.Remove(loc.Move(-tabSize, v.Buf), loc)
-                       v.Cursor.X, v.Cursor.Y = cx, cy
                } else {
-                       v.Cursor.Left()
-                       cx, cy := v.Cursor.X, v.Cursor.Y
-                       v.Cursor.Right()
                        loc := v.Cursor.Loc
                        v.Buf.Remove(loc.Move(-1, v.Buf), loc)
-                       v.Cursor.X, v.Cursor.Y = cx, cy
                }
        }
        v.Cursor.LastVisualX = v.Cursor.GetVisualX()
@@ -660,7 +745,6 @@ func (v *View) OutdentLine(usePlugin bool) bool {
                        break
                }
                v.Buf.Remove(Loc{0, v.Cursor.Y}, Loc{1, v.Cursor.Y})
-               v.Cursor.X -= 1
        }
        v.Cursor.Relocate()
 
@@ -723,9 +807,9 @@ func (v *View) InsertTab(usePlugin bool) bool {
        tabBytes := len(v.Buf.IndentString())
        bytesUntilIndent := tabBytes - (v.Cursor.GetVisualX() % tabBytes)
        v.Buf.Insert(v.Cursor.Loc, v.Buf.IndentString()[:bytesUntilIndent])
-       for i := 0; i < bytesUntilIndent; i++ {
-               v.Cursor.Right()
-       }
+       // for i := 0; i < bytesUntilIndent; i++ {
+       //      v.Cursor.Right()
+       // }
 
        if usePlugin {
                return PostActionCall("InsertTab", v)
@@ -995,7 +1079,7 @@ func (v *View) DuplicateLine(usePlugin bool) bool {
        } else {
                v.Cursor.End()
                v.Buf.Insert(v.Cursor.Loc, "\n"+v.Buf.Line(v.Cursor.Y))
-               v.Cursor.Right()
+               // v.Cursor.Right()
        }
 
        messenger.Message("Duplicated line")
@@ -1717,7 +1801,7 @@ func (v *View) PlayMacro(usePlugin bool) bool {
                                v.Cursor.ResetSelection()
                        }
                        v.Buf.Insert(v.Cursor.Loc, string(t))
-                       v.Cursor.Right()
+                       // v.Cursor.Right()
 
                        for pl := range loadedPlugins {
                                _, err := Call(pl+".onRune", string(t), v)
@@ -1736,7 +1820,143 @@ func (v *View) PlayMacro(usePlugin bool) bool {
        return true
 }
 
-// None is no action
-func None() bool {
+// SpawnMultiCursor creates a new multiple cursor at the next occurence of the current selection or current word
+func (v *View) SpawnMultiCursor(usePlugin bool) bool {
+       spawner := v.Buf.cursors[len(v.Buf.cursors)-1]
+       // You can only spawn a cursor from the main cursor
+       if v.Cursor == spawner {
+               if usePlugin && !PreActionCall("SpawnMultiCursor", v) {
+                       return false
+               }
+
+               if !spawner.HasSelection() {
+                       spawner.SelectWord()
+               } else {
+                       c := &Cursor{
+                               buf: v.Buf,
+                       }
+
+                       sel := spawner.GetSelection()
+
+                       searchStart = ToCharPos(spawner.CurSelection[1], v.Buf)
+                       v.Cursor = c
+                       Search(sel, v, true)
+
+                       for _, cur := range v.Buf.cursors {
+                               if c.Loc == cur.Loc {
+                                       return false
+                               }
+                       }
+                       v.Buf.cursors = append(v.Buf.cursors, c)
+                       v.Buf.UpdateCursors()
+                       v.Relocate()
+                       v.Cursor = spawner
+               }
+
+               if usePlugin {
+                       PostActionCall("SpawnMultiCursor", v)
+               }
+       }
+
+       return false
+}
+
+// MouseMultiCursor is a mouse action which puts a new cursor at the mouse position
+func (v *View) MouseMultiCursor(usePlugin bool, e *tcell.EventMouse) bool {
+       if v.Cursor == &v.Buf.Cursor {
+               if usePlugin && !PreActionCall("SpawnMultiCursorAtMouse", v, e) {
+                       return false
+               }
+               x, y := e.Position()
+               x -= v.lineNumOffset - v.leftCol + v.x
+               y += v.Topline - v.y
+
+               c := &Cursor{
+                       buf: v.Buf,
+               }
+               v.Cursor = c
+               v.MoveToMouseClick(x, y)
+               v.Relocate()
+               v.Cursor = &v.Buf.Cursor
+
+               v.Buf.cursors = append(v.Buf.cursors, c)
+               v.Buf.UpdateCursors()
+
+               if usePlugin {
+                       PostActionCall("SpawnMultiCursorAtMouse", v)
+               }
+       }
+       return false
+}
+
+// SkipMultiCursor moves the current multiple cursor to the next available position
+func (v *View) SkipMultiCursor(usePlugin bool) bool {
+       cursor := v.Buf.cursors[len(v.Buf.cursors)-1]
+
+       if v.Cursor == cursor {
+               if usePlugin && !PreActionCall("SkipMultiCursor", v) {
+                       return false
+               }
+               sel := cursor.GetSelection()
+
+               searchStart = ToCharPos(cursor.CurSelection[1], v.Buf)
+               v.Cursor = cursor
+               Search(sel, v, true)
+               v.Relocate()
+               v.Cursor = cursor
+
+               if usePlugin {
+                       PostActionCall("SkipMultiCursor", v)
+               }
+       }
+       return false
+}
+
+// RemoveMultiCursor removes the latest multiple cursor
+func (v *View) RemoveMultiCursor(usePlugin bool) bool {
+       end := len(v.Buf.cursors)
+       if end > 1 {
+               lastOne := v.Buf.cursors[end-1]
+               if v.Cursor == lastOne {
+                       if usePlugin && !PreActionCall("RemoveMultiCursor", v) {
+                               return false
+                       }
+
+                       v.Buf.cursors[end-1] = nil
+                       v.Buf.cursors = v.Buf.cursors[:end-1]
+                       v.Buf.UpdateCursors()
+                       v.Relocate()
+
+                       if usePlugin {
+                               return PostActionCall("RemoveMultiCursor", v)
+                       }
+                       return true
+               }
+       } else {
+               v.RemoveAllMultiCursors(usePlugin)
+       }
+       return false
+}
+
+// RemoveAllMultiCursors removes all cursors except the base cursor
+func (v *View) RemoveAllMultiCursors(usePlugin bool) bool {
+       if v.Cursor == v.Buf.cursors[len(v.Buf.cursors)-1] {
+               if usePlugin && !PreActionCall("RemoveAllMultiCursors", v) {
+                       return false
+               }
+
+               for i := 1; i < len(v.Buf.cursors); i++ {
+                       v.Buf.cursors[i] = nil
+               }
+               v.Buf.cursors = v.Buf.cursors[:1]
+               v.Buf.UpdateCursors()
+               v.Cursor.ResetSelection()
+               v.Relocate()
+
+               if usePlugin {
+                       return PostActionCall("RemoveAllMultiCursors", v)
+               }
+               return true
+       }
        return false
 }