/********************************************************************
                          SynHglKeywords

   Description: Create syntax highlighting keywords from keyword lists
   Syntax:      SynHglKeywords

********************************************************************/

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <editor/doc.h>
#include <editor/macros.h>
#include <editor/options.h>
#include <editor/dlg.h>
//#include <editor/app.h>


// function prototypes
void ScriptMain(HDOC);


/*-------------------------------------------------------------------
                    main function
-------------------------------------------------------------------*/

int main(int argc, char *argv[])
{
   HDOC hDoc;

   hDoc = Gte_GetActiveDocument();
   if(hDoc == NULL) {
      MsgBox("Cannot get handle of the active document!", MB_OK);
      return 1;
   }
   // check if document is read only
   if(Gte_IsReadOnly(hDoc)) {
      MsgBox("Document is read only!", MB_OK);
      return 1;
   }
   // turn off line drawing and/or selecting mode
   GM_EndLineDraw(hDoc);
   GM_EndSelect(hDoc);

   {
      BOOL bKeepTrailingBlanks;

      // save original editor options
      bKeepTrailingBlanks = Gte_IsKeepTrailingBlanks();

      // set new editor options
      Gte_SetKeepTrailingBlanks(FALSE);

      // call main script function
      ScriptMain(hDoc);

      // restore original editor options
      Gte_SetKeepTrailingBlanks(bKeepTrailingBlanks);
   }

   return 0;
}


/*-------------------------------------------------------------------
                         ScriptMain

  Main script function.

  PARAMETERS

     hDoc - Handle of the active document.
-------------------------------------------------------------------*/

void ScriptMain(HDOC hDoc)
{
   HDOC hKeywordsDoc;
   char szKeywordRegExp[64] = "[A-Za-z0-9_]+";
   long nLine;
   int  nKeywLen;

   Gte_ReadMeStringDlg("Word List", NULL, 
                       "This document must contain one keyword per line.\r\n"
                       "Keywords must be sorted in acessing order.\r\n\r\n"
                       "Document can contain empty lines and lines which\r\n"
                       "does not contains keyword characters", MB_OK);
   if(Gte_GetStringDlg("Keyword characters", "&Keyword regular expression:",
                    szKeywordRegExp, sizeof(szKeywordRegExp)) == FALSE)
   {
      return;
   }
   hKeywordsDoc = Gte_NewDocument();
   if(hKeywordsDoc == NULL)
      return;

   for(nKeywLen = 1; nKeywLen <= 24; nKeywLen++)
   {
      char szKeywX[20];

      sprintf(szKeywX, "Keyword%d=", nKeywLen);
      Gte_InsertText(hKeywordsDoc, szKeywX);

      for(nLine = 1; nLine <= Gte_GetNumberOfLines(hDoc); nLine++)
      {
         Gte_SetCursorPos(hDoc, 1, nLine, FALSE);
         Gte_FindInLine(hDoc, szKeywordRegExp, 1, nLine, FALSE, TRUE, TRUE, TRUE);
         // if selection exists, we found an keyword
         if(Gte_GetSelType(hDoc) != 0)
         {
            int nSelBegPos, nSelEndPos;

            // get keyword len
            Gte_GetSelBeginPos(hDoc, &nSelBegPos, NULL);
            Gte_GetSelEndPos(hDoc, &nSelEndPos, NULL);

            // if this is keyword with nKeywLen
            if(nSelEndPos - nSelBegPos == nKeywLen)
            {
               char *pszKeyword = Gte_GetSelection(hDoc);
               if(pszKeyword)
               {
                  Gte_InsertText(hKeywordsDoc, pszKeyword);
                  free(pszKeyword);
               }
            }
         }
      }
      Gte_InsertText(hKeywordsDoc, "\r\n");
   }
}
