Home Download Buy Blog Forum Support

Customizing ctrl+click

Customizing ctrl+click

Postby xeno.by on Fri Jun 22, 2012 8:22 am

I've found how to bind my own command to ctrl+click, but now I'm struggling with obtaining current position of the cursor. Doing view.sel()[0] is obviously unreliable, so I'm looking for a better way.

A great solution would be to get mouse coordinates of the click, convert them to text coordinates via view.layout_to_text and do my stuff. After enabling sublime.log_commands I've noticed that Sublime passes mouse coordinates to drag_select handlers (e.g. "command: drag_select {"event": {"button": 1, "x": 582.5, "y": 274.5}}"). However, I've been unable to get hold of them - neither bound by press_command, nor by command in sublime-mousemap entry, my command doesn't receive the "event" argument.

Any ideas?
xeno.by
 
Posts: 104
Joined: Mon Jan 30, 2012 10:03 am
Location: Lausanne, Switzerland

Re: Customizing ctrl+click

Postby adzenith on Fri Jun 22, 2012 6:23 pm

Let me know if you figure out how to do this—I tried briefly a while ago but I don't think I found anything.
adzenith
 
Posts: 1213
Joined: Mon Oct 19, 2009 9:12 pm

Re: Customizing ctrl+click

Postby Clams on Sat Jun 23, 2012 6:05 am

Normally it's simply part of the args variable:
Code: Select all
import sublime, sublime_plugin

class TestPluginCommand(sublime_plugin.TextCommand):
    def run_(self, args):
        print args['event']


I tried to play with this but i had issues converting reliably (x,y) to test position (it was some times ago but maybe things are better now ...). So in the end what i did was pass the arg to the drag_select command, and then use view.sel()[0].
This was for a plugin to emulate a kind of drag&drop (more a sel&click) before it was implemented: you can find the very simple source here: https://bitbucket.org/Clams/pasteselonclick/src
Clams
 
Posts: 46
Joined: Fri Dec 30, 2011 7:44 am

Re: Customizing ctrl+click

Postby xeno.by on Sat Jun 23, 2012 10:38 am

Didn't work for me. The second argument is just a plain Edit object, which cannot be indexed. Could you, please, post your mousemap?
xeno.by
 
Posts: 104
Joined: Mon Jan 30, 2012 10:03 am
Location: Lausanne, Switzerland

Re: Customizing ctrl+click

Postby Clams on Sat Jun 23, 2012 12:13 pm

Code: Select all
[
        {
                "button": "button2",
                "modifiers": ["alt"],
                "press_command": "paste_sel_on_click",
                "press_args"  : {"del" : true}
        },
        {
                "button": "button2",
                "modifiers": ["alt","ctrl"],
                "press_command": "paste_sel_on_click",
                "press_args"  : {"del" : false}
        }
]
Clams
 
Posts: 46
Joined: Fri Dec 30, 2011 7:44 am

Re: Customizing ctrl+click

Postby adzenith on Sat Jun 23, 2012 4:53 pm

Ah I see! run_ (with an underscore) strips the event from the arguments list before calling run (no underscore). So you have to override run_ instead of overriding run.
Awesome.
adzenith
 
Posts: 1213
Joined: Mon Oct 19, 2009 9:12 pm

Re: Customizing ctrl+click

Postby adzenith on Sat Jun 23, 2012 7:45 pm

I don't think wbond's package manager does anything with dependencies, but I just wrote a cool mouse-callback thing:
https://gist.github.com/2979613
It lets you inherit from MouseEventListener and then you get an on_pre_click with the args and an on_post_click with the position in the file where the user clicked.
I think I might upload it to github and make it a real repo, and then I'll make my ScrollOffset plugin ignore mouse input. Now that would rock!
adzenith
 
Posts: 1213
Joined: Mon Oct 19, 2009 9:12 pm

Re: Customizing ctrl+click

Postby xeno.by on Sat Jun 23, 2012 9:19 pm

adzenith wrote:Ah I see! run_ (with an underscore) strips the event from the arguments list before calling run (no underscore). So you have to override run_ instead of overriding run.
Awesome.

Woot! What's the difference between run and run_?
xeno.by
 
Posts: 104
Joined: Mon Jan 30, 2012 10:03 am
Location: Lausanne, Switzerland

Re: Customizing ctrl+click

Postby quodlibet on Sat Jun 23, 2012 9:45 pm

/quick thread hijack:

@adzenith I was checking out your ScrollOffset plugin earlier today. I use a similar plugin that always keeps the cursor centered, so I thought your "mouse-callback thing" may be solving similar problems as the ones I am encountering. Honestly, I can't figure out what it's supposed to do. What I was looking for is disabling the click+hold for selection so that I could only use mouse button 1 to change the cursor's location. (I would then use shift+click for selection). With the aforementioned plugin, my thumb has a bad habit of tapping the touchpad, selecting chunks of text which I then gleefully proceed to overwrite without noticing... Hope this makes, sense
quodlibet
 
Posts: 499
Joined: Sat Jun 18, 2011 5:53 pm

Re: Customizing ctrl+click

Postby adzenith on Mon Jun 25, 2012 6:34 pm

The mouse-callback thing is not yet integrated into my ScrollOff plugin, which may have explained what you were seeing.
My plan was to capture on_pre_click, disable ScrollOff for the next on_selection_modified, then capture on_post_click and re-enable. This would make it so that clicking wouldn't scroll the display, allowing normal selections with the mouse. Does that make sense?
adzenith
 
Posts: 1213
Joined: Mon Oct 19, 2009 9:12 pm

Next

Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 9 guests