home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / Editor / DVD!FX17.LHA / FrexxEd / fpl / CIndent.FPL < prev    next >
Encoding:
Text File  |  1994-11-18  |  3.8 KB  |  190 lines

  1. void MatchIt(string paren)
  2. {
  3.    Output(paren);
  4.    CursorLeft(1);
  5.    if (MatchParen()>=0)
  6.    {
  7.       Delay(4);
  8.       MatchParen();
  9.    }
  10.    else
  11.    {
  12.       ReturnStatus("No match!");
  13.    }
  14.    CursorRight(1);
  15. }
  16.  
  17. int GetIndent(int linenum, string inline)
  18. {
  19.    int x;
  20.    int linelen = strlen(inline);
  21.  
  22.    for (x = 0; x < linelen && ((inline[x] == '\t') || (inline[x] == ' ')); x++);
  23.    return(GetCursor(x, linenum) - 1);
  24. }
  25.  
  26. string InsertIndent(string output, int indentation)
  27. {
  28.    int x, tabs, spaces;
  29.    int tabsize = ReadInfo("tab_size");
  30.  
  31.    if (ReadInfo("c_usetabs"))
  32.    {
  33.       tabs = indentation / tabsize;
  34.       spaces = indentation - tabs * tabsize;
  35.       for (x = 0; x < tabs; x++)
  36.       {
  37.      output = joinstr(output, "\t");
  38.       }
  39.       for (x = 0; x < spaces; x++)
  40.       {
  41.      output = joinstr(output, " ");
  42.       }
  43.    }
  44.    else
  45.    {
  46.       for (x = 0; x < indentation; x++)
  47.       {
  48.      output = joinstr(output, " ");
  49.       }
  50.    }
  51.    return (output);
  52. }
  53.  
  54. export void CIndent()
  55. {
  56.    int indentation;
  57.    int line = ReadInfo("line");
  58.    int linelen = ReadInfo("line_length") - 1;
  59.    int column = ReadInfo("column") - 1;
  60.    int keywordpos;
  61.    string input = GetLine();
  62.    string output = "\n";
  63.  
  64.    indentation = GetIndent(line, input);
  65.    if(indentation == column && column == linelen)
  66.    {
  67.       GotoLine(line, 0);
  68.       DeleteEol();
  69.    }
  70.  
  71.    if (strstr(input, "}") < 0)
  72.    {
  73.       if ((keywordpos = strstr(input, "{")) >= 0)
  74.       {
  75.      indentation += ReadInfo("c_indent");
  76.       }
  77.       else if ((keywordpos = strstr(input, "case")) >= 0)
  78.       {
  79.      indentation += ReadInfo("c_indent");
  80.       }
  81.       else if ((keywordpos = strstr(input, "default")) >= 0)
  82.       {
  83.      indentation += ReadInfo("c_indent");
  84.       }
  85.       else if (indentation > column)
  86.       {
  87.      indentation -= indentation - column;
  88.       }
  89.    }
  90.  
  91.    if (keywordpos < column)
  92.    {
  93.       output = InsertIndent("\n", indentation);
  94.    }
  95.    Output(output);
  96. }
  97.  
  98. export void CAutoMatch()
  99. {
  100.    int line = ReadInfo("line");
  101.    int col = ReadInfo("byte_position");
  102.    int newline;
  103.    string input = GetLine();
  104.    string output;
  105.    int len = strlen(input);
  106.    int column, maxcol;
  107.    int x = 0;
  108.  
  109.    /* If we aren't on the last line, we subtract one from len */
  110.    if (line != ReadInfo("lines"))
  111.    {
  112.       len -= 1;
  113.    }
  114.  
  115.    if (col == len)    /* Only act if we are at the end of the line */
  116.    {
  117.       x = GetIndent(line, input);
  118.       if(x == GetCursor(col) - 1) /* Act if there is no text on the line */
  119.       {
  120.      Output("}");
  121.      CursorLeft(1);
  122.      if (MatchParen() >= 0)
  123.      {
  124.         newline = ReadInfo("line");
  125.         if (newline != line)
  126.         {
  127.            column = GetIndent(newline, GetLine());
  128.            output = InsertIndent("", column);
  129.         }
  130.         output = joinstr(output, "}");
  131.         Delay(10);
  132.         GotoLine(line);
  133.         DeleteEol();
  134.         Output(output);
  135.      }
  136.      else
  137.      {
  138.         ReturnStatus("No match!");
  139.      }
  140.       }
  141.       else
  142.       {
  143.      MatchIt("}");
  144.       }
  145.    }
  146.    else
  147.    {
  148.       MatchIt("}");
  149.    }
  150. }
  151.  
  152. export int CIndent_LoadHook(string path, string filename)
  153. {
  154.    int place;
  155.    string extension;
  156.    int ret = 0;
  157.  
  158.    place = strstr(filename, ".");
  159.    if (place > 0)
  160.    {
  161.       extension = substr(filename, place + 1, -1);
  162.       if (0 == Stricmp(extension, "c"))
  163.       {
  164.      SetInfo(-1, "c_mode", 1);
  165.       }
  166.       else if (0 == Stricmp(extension, "h"))
  167.       {
  168.      SetInfo(-1, "c_mode", 1);
  169.       }
  170.       else if (0 == Stricmp(extension, "fpl"))
  171.       {
  172.      SetInfo(-1, "c_mode", 1);
  173.       }
  174.       else
  175.       {
  176.      SetInfo(-1, "c_mode", 0);
  177.       }
  178.    }
  179.    return (ret);
  180. }
  181.  
  182. Hook("GetFile", "CIndent_LoadHook");
  183. ConstructInfo("c_mode", "", "", "LB", "", 0, 1, 0);
  184. ConstructInfo("c_indent", "", "", "LIW", "", 0, 50, 3);
  185. ConstructInfo("c_usetabs", "", "", "LBW", "", 0, 1, 1);
  186. AssignKey("CAutoMatch();", "}", "c_mode");
  187. AssignKey("Output(\"\n\");", "Shift 'Enter'", "c_mode");
  188. AssignKey("CIndent();", "'Enter'", "c_mode");
  189.  
  190.