[texworks] Spell Checking - user list - personal dictionary

Paul A Norman paul.a.norman at gmail.com
Sun Feb 13 09:59:58 CET 2011


Cool,

I'm stating to think that TeXworks C++ has to tell Hunspell to try and
use the User list.

I've got a basic script for saving highlighted word(s) to the user
list but am having trouble identifying where Tw is looking for it.

I am relying on the user deliberately highlighting text to add to the
user list, and if they high light nothing  but run the script, then
the user list is opened for editing.

It seems from what I've read from other projects that Tw may need to
turn on, or seek this feature to work with Hunspell. Otherwise the

I'll keep looking - very close to a resolution if Tw/Hunspell is
trying to look for the  .hunspell_default  file by default.

What I've put below here is a WIndows only experiment at the moment,
looking into the %HomePath% which is one of the locations that
HunSpell looks for  .hunspell_default
Paul

// TeXworksScript
// Title:  Add to Hunn Spell User List
// Description: Adds highlighted word to user list (work arround)
// Author:  Paul Norman
// Version: 0.1
// Date: 2011-02-13
// Script-Type: standalone
// Context: TeXDocument
// Shortcut: Alt+H, Alt+S

// Usage: Selected text (User selects/double clicks - first) is added
to the HunSpell user's list, if no text is selected in editor, the
user's list is opened for editing.

// This is a temporary workaround in scripting for saving accepted
words from spell checking marks.

const twConst = {
                             SystemAccess_OK : 0,

                         SystemAccess_Failed : 1,

        SystemAccess_PermissionDenied : 2
              }; // end twConst


 function checkForWord(text) // do long hand
      {
        for (word in userList)
           {
             if (userList[word] == text)
               {
                return false;
                 }
            }
            return true;
        }


//  eval(TW.app.getGlobal("helper_twPan")); //  Comment if NOT Needed
- This includes PhpJs ($P), twConst, msgBox, twPan ($tw),
string.toTitleCase()

 var addWord = TW.target.selection;
 var hunnSpellUserFilePath = "";
 var   writeResult;

var hunnSpellUserFile = __FILE__;

       buildPath = hunnSpellUserFile.split("/");

       for (var dir = 0; dir < buildPath.length -3; dir++)
          {
           hunnSpellUserFilePath += buildPath[dir] + "/";
           }

   // if it is in where Tw looks for its dictionaries then it is here

   //    hunnSpellUserFile = hunnSpellUserFilePath +
"dictionaries/.hunspell_default";

       // other options

    //  hunnSpellUserFile = hunnSpellUserFilePath +
"dictionaries/hunspell_default.cdic";

   // Qt does not seem to expand environment variables so ...

   hunnSpellUserFile = TW.system("cmd /c echo
%HOMEDRIVE%%HOMEPATH%\\.hunspell_default", true).output;

    hunnSpellUserFile = hunnSpellUserFile.replace(/\\/g,"/");
     hunnSpellUserFile = hunnSpellUserFile.replace(/\r\n/g,""); //
remove new line created by echo

 // TW.information(null, "Tw Message", ">" + hunnSpellUserFile + "<");

   if(addWord != undefined && addWord != "")
    {

      readResult =  TW.readFile(hunnSpellUserFile);

      if (readResult.status == twConst.SystemAccess_Failed)	
        {
         // First usage

         var   writeResult =  TW.writeFile(hunnSpellUserFile,addWord);

          writeStatus  =   writeResult.status;

         }
       else
       {

            userList = readResult.result;
            userList = userList.replace(/\r/g,"");
            userList = userList.split("\n");

        if (checkForWord(addWord) == true)
         {
          userList.push(addWord);
          userList.sort(); // make any later manual editing easier
          userList = userList.join("\n");
          writeResult =  TW.writeFile(hunnSpellUserFile,userList);
          writeStatus  =   writeResult.status;
          }
        else
          {
            writeStatus  = 0;

           } // End. if (checkForWord(addWord) == true)


         }  // End. if (readResult.status ==
twConst.SystemAccess_Failed)


    } // End.  if(addWord != undefined && addWord != "")
    else
    {
          writeStatus  = -1;
    }

       switch (writeStatus)
    {
      case twConst.SystemAccess_OK:
        TW.target.statusTip = addWord + " Added to User List";
           break;

     case  twConst.SystemAccess_Failed:
       TW.target.statusTip = addWord + " Not Added to User List";
           break;

      case twConst.SystemAccess_PermissionDenied:
           TW.information(null, "Tw Message", "Please Check
Edit/Permissions Scripting for Write");
           break;

        case -1:
        whatHappened = TW.app.openFileFromScript(hunnSpellUserFile,
TW, -1, true);

        if (whatHappened.status == twConst.SystemAccess_Failed)
          {
            TW.information(null, "Tw Message", "Please Save at least
one word First\n\n" +
                                               "(Highlight a word and
then ALt+H & Alt+S)");
             }

       break;

      }
null;

On 13 February 2011 20:40, Stefan Löffler <st.loeffler at gmail.com> wrote:
> Hi,
>
> On 2011-02-13 04:02, Paul A Norman wrote:
>> Does Tw look for a user added list called .hunspell_default  ?
>
> Not deliberately, AFAIK. However, hunspell has support for some user
> word list, IIRC. Where this is stored by default is probably
> implementation-specific, but it may be the file you mention. I wouldn't
> rely on it on all platform/builds ATM, though.
>
> HTH
> Stefan
>



More information about the texworks mailing list