Implement SpawnMultiCursorSelect (#1046)

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.
This commit is contained in:
dwwmmn 2018-03-30 16:40:45 -04:00 committed by Zachary Yedidia
parent 3293160dcb
commit ac0b89366b
3 changed files with 51 additions and 0 deletions

View 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 {

View 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",

View file

@ -464,6 +464,7 @@ MouseWheelRight
// Multiple cursors bindings
"Alt-n": "SpawnMultiCursor",
"CtrlM": "SpawnMultiCursorSelect",
"Alt-p": "RemoveMultiCursor",
"Alt-c": "RemoveAllMultiCursors",
"Alt-x": "SkipMultiCursor",