Skip to content

Commit

Permalink
Add ability to add cursors with Ctrl-MouseLeft
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zyedidia committed Jun 12, 2017
1 parent 572198c commit 340cfb8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
27 changes: 27 additions & 0 deletions cmd/micro/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 3 additions & 1 deletion cmd/micro/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -517,6 +518,7 @@ func DefaultBindings() map[string]string {
"MouseWheelDown": "ScrollDown",
"MouseLeft": "MousePress",
"MouseMiddle": "PastePrimary",
"Ctrl-MouseLeft": "MouseMultiCursor",

"Alt-n": "SpawnMultiCursor",
"Alt-p": "RemoveMultiCursor",
Expand Down
4 changes: 2 additions & 2 deletions cmd/micro/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down

0 comments on commit 340cfb8

Please sign in to comment.