public
Last active

Sublime Text 2 better mouse handling

  • Download Gist
Default (OSX).sublime-mousemap
JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
[
// Basic drag select
{
"button": "button1", "count": 1,
"press_command": "drag_select_callback"
},
{
// Select between selection and click location
"button": "button1", "modifiers": ["shift"],
"press_command": "drag_select_callback",
"press_args": {"extend": true}
},
{
"button": "button1", "count": 1, "modifiers": ["super"],
"press_command": "drag_select_callback",
"press_args": {"additive": true}
},
{
"button": "button1", "count": 1, "modifiers": ["shift", "super"],
"press_command": "drag_select_callback",
"press_args": {"subtractive": true}
},
 
// Drag select by words
{
"button": "button1", "count": 2,
"press_command": "drag_select_callback",
"press_args": {"by": "words"}
},
{
"button": "button1", "count": 2, "modifiers": ["super"],
"press_command": "drag_select_callback",
"press_args": {"by": "words", "additive": true}
},
{
"button": "button1", "count": 2, "modifiers": ["shift", "super"],
"press_command": "drag_select_callback",
"press_args": {"by": "words", "subtractive": true}
},
 
// Drag select by lines
{
"button": "button1", "count": 3,
"press_command": "drag_select_callback",
"press_args": {"by": "lines"}
},
{
"button": "button1", "count": 3, "modifiers": ["super"],
"press_command": "drag_select_callback",
"press_args": {"by": "lines", "additive": true}
},
{
"button": "button1", "count": 3, "modifiers": ["shift", "super"],
"press_command": "drag_select_callback",
"press_args": {"by": "lines", "subtractive": true}
},
 
// Alt + Mouse 1 Column select
{
"button": "button1", "modifiers": ["alt"],
"press_command": "drag_select_callback",
"press_args": {"by": "columns"}
},
{
"button": "button1", "modifiers": ["alt", "super"],
"press_command": "drag_select_callback",
"press_args": {"by": "columns", "additive": true}
},
{
"button": "button1", "modifiers": ["alt", "shift", "super"],
"press_command": "drag_select_callback",
"press_args": {"by": "columns", "subtractive": true}
},
 
// Mouse 3 column select
{
"button": "button3",
"press_command": "drag_select_callback",
"press_args": {"by": "columns"}
},
{
"button": "button3", "modifiers": ["super"],
"press_command": "drag_select_callback",
"press_args": {"by": "columns", "additive": true}
},
{
"button": "button3", "modifiers": ["shift", "super"],
"press_command": "drag_select_callback",
"press_args": {"by": "columns", "subtractive": true}
}
]
mousetest.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
import sublime, sublime_plugin
 
class DragSelectCallbackCommand(sublime_plugin.TextCommand):
def run_(self, args):
for c in sublime_plugin.all_callbacks.setdefault('on_pre_click',[]):
c.on_pre_click(args)
#We have to make a copy of the selection, otherwise we'll just have
#a *reference* to the selection which is useless if we're trying to
#roll back to a previous one.
old_sel = [r for r in self.view.sel()]
#Only send the event so we don't do an extend or subtract or
#whatever. We want the only selection to be where they clicked.
self.view.run_command("drag_select", {'event': args['event']})
new_sel = self.view.sel()
click_point = new_sel[0].a
new_sel.clear()
map(new_sel.add, old_sel)
self.view.run_command("drag_select", args)
for c in sublime_plugin.all_callbacks.setdefault('on_post_click',[]):
c.on_post_click(click_point)
 
class MouseEventListener(sublime_plugin.EventListener):
#If we add the callback names to the list of all callbacks, Sublime
#Text will automatically search for them in future imported classes.
#You don't actually *need* to inherit from MouseEventListener, but
#doing so forces you to import this file and therefore forces Sublime
#to add these to its callback list.
sublime_plugin.all_callbacks.setdefault('on_pre_click', [])
sublime_plugin.all_callbacks.setdefault('on_post_click', [])
 
class MouseEventProcessor(MouseEventListener):
def on_pre_click(self, args):
print "on pre click!", args
def on_post_click(self, point):
print "on post click!", point

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.