Skip to content

Instantly share code, notes, and snippets.

@adzenith
Created June 23, 2012 19:28
Show Gist options
  • Save adzenith/2979613 to your computer and use it in GitHub Desktop.
Save adzenith/2979613 to your computer and use it in GitHub Desktop.
Sublime Text 2 better mouse handling
[
// 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}
}
]
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment