[texworks] a script question

Henrik Skov Midtiby henrikmidtiby at gmail.com
Wed Mar 30 14:08:26 CEST 2011


In the autocomplete script [1] I use the cursor location to extract
the word in front of the cursor. You might want to take a look at the
code there.

Best regards,
Henrik

[1] http://code.google.com/p/texworks/issues/detail?id=261#c42

On 30 March 2011 14:03, Pavol Severa <pavol.severa at gmail.com> wrote:
> Thanks a lot for your answer
> I have still one question
> if the text is
> ...something something someword<here-is-cursor>
> how can I get the position of the first character of "someword"?
> Should I simply take the big string TW.target.text and let the script
> detect the word in front of the cursor (by whatever means available in
> the scripting language)? Or is there an API command doing it?
> (I know a bit of lua scripting from scite, and there similar things
> are done through API - that's why I'm asking)
> Best regards, P.
>
> On Wed, Mar 30, 2011 at 12:56 AM, Paul A Norman <paul.a.norman at gmail.com> wrote:
>> Hi Pavol,
>>
>> Ok you've got a few options. I'll focus on TeXworks here and its
>> scripting api and not underlying Qt framework options that might also
>> work.
>>
>> As the user has to do something anyway ...
>>
>> If you tell the user to write their word and then double click it, you
>> have it selected already.
>>
>> Following your approach ...
>>
>> When you say the in front of the cursor that is describing the
>> situation where the user has just written a word and the cursor is now
>> at the end of that word?
>>
>>  envname<cursor>
>>
>> If you are guaranteeing that there is a space before  envname  then
>> have a look at some rough documentation for the Tw scripting that we
>> use, its very much a draft and  incomplete as yet, but may help
>> getting you and anyone else going  ... (note for any references to
>> TW.app.system see the section
>> http://twscript.paulanorman.com/docs/html/files/systemQString.html)
>>
>> Looking at http://twscript.paulanorman.com/docs/index.html
>> (downloadable copies) and
>> in particularly on line web version at
>> http://twscript.paulanorman.com/docs/html/TexWorksQtScripting.htm
>>
>> "Check out script basics" and subsections, and "Script Authoring" and
>> subsections
>>
>>  And here is a script for setting up  new scripts that might help ...
>>
>>    http://code.google.com/p/texworks/issues/detail?id=261#c41
>>
>>    Download:--
>>    http://texworks.googlecode.com/issues/attachment?aid=-852821199693380206&name=makeScript_031.zip&token=1ce8d47ca3d174d36c17d9d7071ac160
>>
>> For your project ...
>>
>> In the script information on line web version at
>> http://twscript.paulanorman.com/docs/html/TexWorksQtScripting.htm open
>> up the section  "Tw Api functions and  properties" (click + symbol -
>> sorry this should be obvious)
>>
>> Open up the section TW
>>
>> Text stuff is mostly handled by "target" which represents the current
>> Tw document.
>>
>> Open up the section "target"
>> Open up the section "text"
>>
>> Tw itself provides direct methods (distinct form the Qt framework) for
>> reporting and handling the cursor position and making selections.
>>
>> Then look at the entries with "select" as part of their name.
>>
>> In QtScript (JavaScript) store the current cursor position (at the end
>> of the just typed envname).
>>
>>  var endOfEnvName = TW.target.selectionStart;
>>
>> Use the selection functions to move the selection point back setting a
>> range of 1
>>
>> TW.target.selectRange(int, int)  where the first integer is the
>> absolute position form the start of the document, and the second
>> integer is the length you want here 1 , then you can obtain that from
>> script as TW.target.selection and see if it is a space " ".
>>
>> Then select your envanme text using TW.target.selectRange(int, int) again.
>>
>> I am sure you can work out the rest .. using
>> TW.target.insertText(AString) once you have selected and processed in
>> script  the envname.
>>
>> Positioning the cursor in the middle? ... just in script reckon the
>> number of characters back from where the inserted text ends that you
>> want the cursor to end up and use TW.target.selectRange(int) notice
>> only the absolute position is required here.
>>
>> However there might be an easier way ...
>>
>> Using a short-cut key combo, start the script asking for some input
>> form the user getting the environment name they want.
>>
>> This can be done by using a prompt type box...
>>
>> Open up the section  "Tw Api functions and  properties" / TW / get User Input
>> then click on Tw.getText(QWidget*,QString,QString)
>>
>> This can obtain the environment name directly from the user, and then
>> you can construct your string and use  TW.target.insertText(AString)
>> to insert it where the cursor is (it would replace any selected text
>> as well), then the calculated use of TW.target.selectionStart; and
>> TW.target.selectRange(int) to get into the middle of the environement.
>>
>> You could also look at using the TwApi file read and write functions
>> to store the unique environment names the user uses over time and
>> present them in an editable drop-down box for selection or writing a
>> new one (and so storing it as well)
>>
>> Open up the section  "Tw Api functions and  properties" / TW / File Functions
>> Open up the section  "Tw Api functions and  properties" / TW / get User Input
>> then click on   Tw.getItem get List Choice
>>
>> Any way please do get back to the list if you have any other questions
>> and please do post any script you make on
>> http://code.google.com/p/texworks/issues/detail?id=261 for other to
>> use if you want to.
>>
>> Paul
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> On 29 March 2011 22:16, Pavol Severa <pavol.severa at gmail.com> wrote:
>>> sorry
>>>
>>>> if xcvb is the word right between the cursor
>>>
>>> I wanted to say "in front of the cursor"
>>>
>>
>
>



More information about the texworks mailing list