]> git.lizzy.rs Git - micro.git/commitdiff
Implement SpawnMultiCursorSelect (#1046)
authordwwmmn <dwwmmn@users.noreply.github.com>
Fri, 30 Mar 2018 20:40:45 +0000 (16:40 -0400)
committerZachary Yedidia <zyedidia@gmail.com>
Fri, 30 Mar 2018 20:40:45 +0000 (16:40 -0400)
Add function to actions.go which adds a new cursor to the beginning of each line of a selection. Bind to Ctrl-M by default.

cmd/micro/actions.go
cmd/micro/bindings.go
runtime/help/keybindings.md

index b9af7f6dc946285149ed99777b30dbb4b06c2ceb..21179e3bb14de2f25490f40f9d5fa6bb5efeb53e 100644 (file)
@@ -2144,6 +2144,54 @@ func (v *View) SpawnMultiCursor(usePlugin bool) bool {
        return false
 }
 
+// SpawnMultiCursorSelect adds a cursor at the beginning of each line of a selection
+func (v *View) SpawnMultiCursorSelect(usePlugin bool) bool {
+       if v.Cursor == &v.Buf.Cursor {
+               if usePlugin && !PreActionCall("SpawnMultiCursorSelect", v) {
+                       return false
+               }
+
+               // Avoid cases where multiple cursors already exist, that would create problems
+               if len(v.Buf.cursors) > 1 {
+                       return false
+               }
+
+               var startLine int
+               var endLine int
+
+               a, b := v.Cursor.CurSelection[0].Y, v.Cursor.CurSelection[1].Y
+               if a > b {
+                       startLine, endLine = b, a
+               } else {
+                       startLine, endLine = a, b
+               }
+
+               if v.Cursor.HasSelection() {
+                       v.Cursor.ResetSelection()
+                       v.Cursor.GotoLoc(Loc{0, startLine})
+
+                       for i := startLine; i <= endLine; i++ {
+                               c := &Cursor{
+                                       buf: v.Buf,
+                               }
+                               c.GotoLoc(Loc{0, i})
+                               v.Buf.cursors = append(v.Buf.cursors, c)
+                       }
+                       v.Buf.MergeCursors()
+                       v.Buf.UpdateCursors()
+               } else {
+                       return false
+               }
+
+               if usePlugin {
+                       PostActionCall("SpawnMultiCursorSelect", v)
+               }
+
+               messenger.Message("Added cursors from selection")
+       }
+       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 {
index f70e58230d81bf63836afff12fd8f3e517cef701..cc24a2b3ffa1e97fcb8b76798d3f8035dfe73d7b 100644 (file)
@@ -109,6 +109,7 @@ var bindingActions = map[string]func(*View, bool) bool{
        "ScrollUp":              (*View).ScrollUpAction,
        "ScrollDown":            (*View).ScrollDownAction,
        "SpawnMultiCursor":      (*View).SpawnMultiCursor,
+       "SpawnMultiCursorSelect":(*View).SpawnMultiCursorSelect,
        "RemoveMultiCursor":     (*View).RemoveMultiCursor,
        "RemoveAllMultiCursors": (*View).RemoveAllMultiCursors,
        "SkipMultiCursor":       (*View).SkipMultiCursor,
@@ -600,6 +601,7 @@ func DefaultBindings() map[string]string {
                "Ctrl-MouseLeft": "MouseMultiCursor",
 
                "Alt-n": "SpawnMultiCursor",
+               "CtrlM": "SpawnMultiCursorSelect",
                "Alt-p": "RemoveMultiCursor",
                "Alt-c": "RemoveAllMultiCursors",
                "Alt-x": "SkipMultiCursor",
index dfee40b623757ed65c8ac105e214348026ace54d..4290cd1ee28159aba7ebb6dfeee00c2d7559431d 100644 (file)
@@ -464,6 +464,7 @@ MouseWheelRight
 
     // Multiple cursors bindings
     "Alt-n": "SpawnMultiCursor",
+    "CtrlM": "SpawnMultiCursorSelect",
     "Alt-p": "RemoveMultiCursor",
     "Alt-c": "RemoveAllMultiCursors",
     "Alt-x": "SkipMultiCursor",