Sublime Forum

Get caret position from external program

#1

I am trying to write an AutoHotKey script that uses the caret position.

AHK exposes A_CaretX and A_CaretY variables to get the caret position. However they do not work for sublime.

I think the issue is that sublime draws it’s own carrot and does not use the windows calls CreateCaret to create it and SetCaretPos to position it.

Is there any way around it ? some call to get the caret position or some way to get sublime to write them to a place where they can be picked up ?

Thx!

0 Likes

#2

I think that Sublime does all of it’s own rendering, so it most likely does indeed have it’s own concept of where the caret (or carets, since there can be many at once) currently is; I’m not a Sublime developer, though.

The sublime API allows you to obtain the current location of the caret (or carets) in the current view in a variety of ways (row and column, offset in characters, etc) and you could get it out by writing it to a file, copying it to the clipboard or something else.

What are you trying to do with AutoHotKey? Perhaps there’s another way to do what you want.

0 Likes

#3

thx !

well I wrote a script that binds the numpad ‘*’ to do either copy the currently selected text to the clipboard (^c) or if nothing is selected then select and copy the current word under the cursor.

selecting the current word is tricky … simply doing ctrl+left and then shift+ctrl+right often selects spaces at the end or have other issues whereas I want the exact equivalent of double clicking the word. So the easiest way is to move the cursor to the caret position and issuing a double click - this is why I need the caret position (see https://autohotkey.com/boards/viewtopic.php?f=5&t=17242&p=104195#p104195)

0 Likes

#4

If you are having a special behavior for Sublime Text anyway I would just directly write the command as a plugin (I think you would also need a plugin to get the caret position). Just select Tools >> Developer >> New Plugin... and paste

import sublime
import sublime_plugin


class AhkCopyWordCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        view = self.view
        sel = view.sel()[0]
        region = view.word(sel) if sel.empty() else sel
        sublime.set_clipboard(view.substr(region))

You can call it using subl --command "ahk_copy_word". If Sublime Text is not in the path you may need to adapt the command to "C:\Program Files\Sublime Text 3\subl.exe" --command "ahk_copy_word".

0 Likes

#5

that’s great r-stein, thank you !

0 Likes