[texworks] SCRIPTING: Get content of specified directory

Henrik Skov Midtiby henrikmidtiby at gmail.com
Fri May 20 15:40:04 CEST 2011


Hi Paul and others

I have tried to use your code for filename completion for windows. It
it still not tested ...

In addition the script can now complete open environments (which is
determined by looking at the current file from the beginning and to
the cursor location).

The script can be downloaded from
https://github.com/henrikmidtiby/autocompleteForTexworks/blob/master/scripts/autocomplete.js

Any comments and ideas for improvements are appreciated.

Best regards,
Henrik

On 17 May 2011 02:12, Paul A Norman <paul.a.norman at gmail.com> wrote:
> Looking really awesome Henrik!
>
> Haven't tried all scenarios yet, but really like the precision of
> operation, and the clarity of coding - made it really easy for me to
> try and get it running under Windows for your new feature - input of
> filenames.
>
> Often people have asked here whether there could be an automatic
> completion of environments
>
> \begin{something}   sa a s a sad sad  fd f
> <contorl-key M>
>
> \begin{ofsomething}   sa a s a sad sad  fd f
> \end{ofsomething}
>
> Is that something that you would see possibly logically fitting in
> with your code at all?
>
> With your new code, under Windows - I've tried these alterations, I
> don't think they'd break the Linux version (?)
>
> Quotes for possible spaces in file path (platform tested), and I've
> found that for *some* Dos commands and utilities we need to absolutely
> reverse the path delimiter to a backslash - fickle problem, and safer
> to actually sample the output to determine whether \n or \r\n is doing
> the line breaks and not assume it on platform.
>
> Regards,
> Paul
> line 304(?)
> function getListOfFilesInDir(directory)
> {
>
>  if (TW.platform() == 'Windows')
>   {
>   var retVal = TW.system("cmd /c dir /b \"" +
> directory.replace(/\//g,"\\") +"\"", true);
>   }
>   else
>        {
>        var retVal = TW.system("ls " + directory, true);
>        }
>
>        return retVal.output;
> }
>
> function typeOfLineBreak(sample) // Win OS with utilities called can
> return either
>  {
>   if (sample.indexOf("\r\n") > -1)
>     {
>       return "\r\n";
>       }
>
>      return "\n";
>
>  }
>
>
> function getMatchingFilenames(filenamesInDirectory, localPath, inputWord, words)
> {
>        while(filenamesInDirectory.indexOf(typeOfLineBreak(filenamesInDirectory)) > -1)
>        {
> ...
>
> On 17 May 2011 03:53, Henrik Skov Midtiby <henrikmidtiby at gmail.com> wrote:
>> Hi Paul
>>
>> Thanks for your input.
>>
>> I now have autocompletion working for the macros \includegraphics,
>> \input and \include based on the files present in the directory of the
>> file open for editing.
>> This part works on systems where the "ls" command is available and
>> where the file path does not contain any spaces. The code have only
>> been tested on linux at the moment.
>>
>> The result is uploaded at
>> http://code.google.com/p/texworks/issues/detail?id=261#c60
>> With the description
>> ====================
>> Context based autocompleter for completing words used in the open
>> document, arguments to label and ref and as a new feature filename
>> autocompletion for include, input and includegraphics commands.
>>
>> Guide to use.
>> 1. start typing the long word that should be autocompleted
>> 2. press "<crtl> + m" and the autocompleter will complete the word or
>> at least make a suggestion for a completion.
>> 3. if not satisfied with the suggestion press "<crtl> + m" until the
>> correct word is found.
>> ====================
>>
>> Best regards,
>> Henrik Skov Midtiby
>>
>>
>> On 15 May 2011 12:22, Paul A Norman <paul.a.norman at gmail.com> wrote:
>>> Dear Henrik,
>>>
>>> Not so far as far as I know.
>>>
>>> As you have, I've been using system calls to things like á"cmd /c
>>> mkdir \""+ documentPath +"images\""
>>> Can switch case on
>>> http://twscript.paulanorman.com/docs/html/files/platform.html and
>>> prepare differnt statements for the TW.system call.
>>> http://twscript.paulanorman.com/docs/html/files/systemQString.html
>>>
>>> (see subheadngs in and under main headings "User 'Library' Modules and
>>> helper objects" áat
>>> http://twscript.paulanorman.com/docs/html/TexWorksQtScripting.htm for
>>> some explanation on twPan - recomend downloading from
>>> http://twscript.paulanorman.com/docs/index.html and the .chm there.)
>>>
>>> and áhave been using things like ...
>>>
>>> áfunction áchooseFromDirectoryListing(Description, pathDir, fileExtension)
>>> á{
>>> á// pathDir (no trailing slash), fileExtension with . E.g. á.txt
>>>
>>> á // á á TW.app.clipboard = "cmd /c dir /b á\""
>>> á á á á á á á á á á á á á+ ápathDir + "\\*" + fileExtension + "\"";
>>>
>>> á á ávar fileList = twPan.osCmd("cmd /c dir /b á\""
>>> á á á á á á á á á á á á á+ ápathDir + "\\*" + fileExtension + "\""
>>> á á á á á á á á á á á á á á , true)
>>>
>>> á á á á áremoveExtnsn = new RegExp(fileExtension,"ig");
>>>
>>> á á á áfileList = fileList.replace(removeExtnsn,"").split(lineBreak);
>>>
>>> á// á ávar chosenAnswer =
>>> á á á á á á// á twPan.select(Description, fileList);
>>>
>>> ávar chosenAnswer = á áTW.getItem(null, Description, Description, fileList);
>>>
>>> á á á áif (twPan.emptyString(chosenAnswer) == false)
>>> á á á á á{
>>> á á á á áchosenAnswer = chosenAnswer + fileExtension;
>>> á á á á á }
>>>
>>> á á áreturn chosenAnswer;
>>> á á}
>>>
>>> You can use
>>>
>>> http://twscript.paulanorman.com/docs/html/files/getOpenFileNames.html
>>>
>>> See also notes in
>>> http://twscript.paulanorman.com/docs/html/files/getOpenFileNameQString.html
>>>
>>> Can get one or multi-select more file names though returns an array in
>>> that case, requires User interaction though.
>>>
>>> Paul
>>>
>>>
>>>
>>> On 15 May 2011 21:26, Henrik Skov Midtiby <henrikmidtiby at gmail.com> wrote:
>>>> Hi
>>>>
>>>> >From at script would I like to get access to the contents of the
>>>> current directory.
>>>> This information should be used for autocompleting filenames for
>>>> includegraphics and input macros in latex.
>>>>
>>>> I know it is possible to access the commandline and issue an 'ls -all'
>>>> or something similar, but is this information also available directly
>>>> in QtScript?
>>>>
>>>> Best regards,
>>>> Henrik Skov Midtiby
>>>>
>>>
>>
>>
>
>



More information about the texworks mailing list