From 00718f99cf3c916aa6d01ad43c49e40b71f3c96e Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Mon, 12 Jun 2017 16:58:39 -0400 Subject: [PATCH] Add ability to add cursors with Ctrl-MouseLeft With the new code that allows binding mouse buttons this was remarkably easy to add. The new binding is: "Ctrl-MouseLeft": "MouseMultiCursor" Note: A number of terminals don't support Ctrl-MouseLeft (macOS especially) so you might want to rebind to MouseRight or MouseMiddle. --- cmd/micro/actions.go | 27 +++++++++++++++++++++++++++ cmd/micro/bindings.go | 4 +++- cmd/micro/view.go | 4 ++-- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/cmd/micro/actions.go b/cmd/micro/actions.go index dd33621a..60d95268 100644 --- a/cmd/micro/actions.go +++ b/cmd/micro/actions.go @@ -1869,6 +1869,33 @@ func (v *View) SpawnMultiCursor(usePlugin bool) bool { 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) + + 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] diff --git a/cmd/micro/bindings.go b/cmd/micro/bindings.go index cb289d98..1c232da8 100644 --- a/cmd/micro/bindings.go +++ b/cmd/micro/bindings.go @@ -14,7 +14,8 @@ var mouseBindings map[Key][]func(*View, bool, *tcell.EventMouse) bool var helpBinding string var mouseBindingActions = map[string]func(*View, bool, *tcell.EventMouse) bool{ - "MousePress": (*View).MousePress, + "MousePress": (*View).MousePress, + "MouseMultiCursor": (*View).MouseMultiCursor, } var bindingActions = map[string]func(*View, bool) bool{ @@ -517,6 +518,7 @@ func DefaultBindings() map[string]string { "MouseWheelDown": "ScrollDown", "MouseLeft": "MousePress", "MouseMiddle": "PastePrimary", + "Ctrl-MouseLeft": "MouseMultiCursor", "Alt-n": "SpawnMultiCursor", "Alt-p": "RemoveMultiCursor", diff --git a/cmd/micro/view.go b/cmd/micro/view.go index b9c901bc..18b6d468 100644 --- a/cmd/micro/view.go +++ b/cmd/micro/view.go @@ -561,7 +561,7 @@ func (v *View) HandleEvent(event tcell.Event) { button := e.Buttons() for key, actions := range bindings { - if button == key.buttons { + if button == key.buttons && e.Modifiers() == key.modifiers { for _, c := range v.Buf.cursors { v.Cursor = c relocate = v.ExecuteActions(actions) || relocate @@ -571,7 +571,7 @@ func (v *View) HandleEvent(event tcell.Event) { } for key, actions := range mouseBindings { - if button == key.buttons { + if button == key.buttons && e.Modifiers() == key.modifiers { for _, action := range actions { action(v, true, e) }