Sublime Forum

Select word under cursor for further processing

#1

Hi.

I am writing my first plug-in. It’s a contextual plug-in that lets me do stuff with selected words.

So far, this is how it works: I select a word, control-click it to call the contextual menu and then I choose the appropriate action. The code looks something like this:

class ProcessWordCommand(sublime_plugin.TextCommand):
def run(self, edit):
        for region in self.view.sel():
            if not region.empty():
                word = self.view.substr(region)

#then process word

But what I actually need is to simply control-click the word (without selecting it first) and choose the contextual command for it. How can this be achieved? In other words, how can I get the python code to pick up the whole word based on where I clicked in the text?

Thanks a lot in advance.
Psych

0 Likes

#2

Sounds like you want the view.word(Region) method.

0 Likes

#3

Thanks, skuroda, that was a good tip.

I have implemented a version of my code with your suggestion only to realize in the end that it’s not quite what I want. Let me explain. With this code:

def run(self, edit): for region in self.view.sel(): if region.begin() == region.end(): word = self.view.word(region) else: word = region if not word.empty(): keyword = self.view.substr(word) print (keyword)

I can catch either a selection or a word where the text cursor (insertion point) is. That means, I actually have to click the word (to get the cursor there), then right-click to run my code from the contextual menu. That’s two steps.

What I actually need is to control-click the word and run the code without placing the cursor in it first. What I am looking for is not word under or next to the text cursor, but word under the pointer or mouse cursor. (Think of how we usually interact with underlined misspelt words – it’s enough to control-click them and choose a different word from the contextual menu without having to click the word itself first).

So… is it possible to rewrite the code above for the mouse cursor?

All best,
Psych

1 Like

#4

As far as I know, what you want to do isn’t possible. A look over the API also suggests that. But I haven’t written many plugins that do stuff with mouse bindings so I could be wrong. There could be some undocumented functionality I’m not aware of.

0 Likes

#5

Short answer: install github.com/SublimeText/MouseEventListener, and then create plugins normally using on_pre_mouse_down and on_post_mouse_down - you’ll get passed a text point of where the click happened. Note that it’s a huge hack, so it doesn’t work 100% successfully.

Longer answer:
There’s no real way to do this. The only thing you can do is override run_ instead of run so that you get the mouse args, call drag_select, check the selection, call soft_undo to reset the selection, etc…
Check out github.com/SublimeText/MouseEve … istener.py if you’re curious as to how the plugin works, but again, note that it doesn’t always work very well.

0 Likes

#6

thanks a lot guys!

0 Likes