/********************************************************************
                           BackTab

  Description: Autouindent backspace on tab stop
  Syntax:      BACKTAB

  To map this script to a shortcut key:

  1. Save this script in the GwdTextEditor\script directory.
  2. From the Macro menu, choose Macro command.
  3. Choose the New button.
  4. In the New Macro Name input box, type a name for the macro. For
     example Script_BackTab. In this dialog you can also define
     Backspace as shortcut key for this macro.
  5. Choose the OK button.
  6. In the Edit Macro dialog box edit box enter the following two
     lines:
        Param1 "backtab"
        ExecScript
  7. Choose the Close button.
  8. You can now define shortcut key for Script_BackTab macro
     in the Key Mapping dialog box.

  Author: Vedran Gaco
********************************************************************/

#include <stdio.h>
#include <malloc.h>
#include <editor/doc.h>
#include <editor/macros.h>
#include <editor/string.h>

int main(void)
{
   HDOC hdoc = Gte_GetActiveDocument();
   if(hdoc != NULL)
   {
      // if we have selected text, just call backspace
      if(Gte_GetSelType(hdoc) != 0)
         GM_Backspace(hdoc);
      else
      {
         // if we have some characters from the beginnig of the line
         // to the cursor position we will also just call backspace
         char *pszLine;
         char *psz;
         BOOL fFoundNonspace;
         long xpos, ypos;

         pszLine = (char *)malloc(Gte_GetMaxLineLen() + 1);

         Gte_GetCursorPos(hdoc, &xpos, &ypos);
         Gte_GetLine(hdoc, ypos, pszLine, Gte_GetMaxLineLen() + 1);
         psz = Gte_GetCharPtr(pszLine, xpos);
         if(psz != pszLine)
            psz--;
         fFoundNonspace = FALSE;
         while(psz != pszLine) {
            if(*psz != ' ' && *psz != '\t')
            {
                fFoundNonspace = TRUE;
                break;
            }
            psz--;
         }
         if(pszLine[0] != 0 && pszLine[0] != ' ' && pszLine[0] != '\t')
            fFoundNonspace = TRUE;

         free(pszLine);

         if(fFoundNonspace || xpos == 0)
         {
            GM_Backspace(hdoc);
         }
         else
         {
            long nOldX, nOldY;
            int  nOldSelMode;

            // remember cursor position
            Gte_GetCursorPos(hdoc, &nOldX, &nOldY);
            // remember selection mode
            nOldSelMode = Gte_GetSelMode(hdoc);
            // move to stream selection mode
            Gte_SetSelMode(hdoc, 1);

            // to speed up macro disable screen update
            Gte_LockUpdate(hdoc);

            GM_PrevTabPos(hdoc);
            Gte_SetCursorPos(hdoc, nOldX, nOldY, TRUE);
            if(Gte_GetSelType(hdoc) != 0)
               GM_Backspace(hdoc);
            else
               GM_PrevTabPos(hdoc);

            // Unlock screen updates
            Gte_UnlockUpdate(hdoc);

            // return selection mode
            Gte_SetSelMode(hdoc, nOldSelMode);
         }
      }
   }
   return 0;
}



