home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / Editor / FREDV19A.LHA / FrexxEd / fpl / CIndent.FPL < prev    next >
Encoding:
Text File  |  1995-08-30  |  12.7 KB  |  534 lines

  1. // Various search strings
  2. string whitespace = "^[ \t]*";
  3. string any_character = "(\\sW|\\s!)";
  4. string left_brace_at_eol = "{[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*$";
  5. string left_or_right_brace = "(({[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*$)|(^[ \t]*}))";
  6. string case_or_default = "^[ \t]*((case[ \t]+.+)|(default[ \t]*)):";
  7. string if_or_while = "(if|while)[ \t]*\\(.*\\)[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*$";
  8. string _else = "else[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*$";
  9. string _switch = "switch[ \t]*\\(.*\\)[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*$";
  10. string backward = "=w+f-";
  11. string forward = "=w+f+";
  12.  
  13. //»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» CModePrefs() ««
  14. void export CModePrefs()
  15. {
  16.    int intelligence = ReadInfo("c_intelligence");
  17.    string indentkey = ReadInfo("c_indentkey");
  18.    int usetabs = ReadInfo("c_usetabs");
  19.    int indent_level = ReadInfo("c_indent_level");
  20.    int case_level = ReadInfo("c_case_level");
  21.    int brace_offset = ReadInfo("c_brace_offset");
  22.    int cont_offset = ReadInfo("c_cont_offset");
  23.    int delay = ReadInfo("c_delay");
  24.  
  25.    DeleteKey(indentkey);
  26.    
  27.    RequestWindow("C Indent Preferences", 15,
  28.       "Intelligence level", "c", &intelligence, "Stupid|Intelligent|Ninja",
  29.       "Force Indent key", "s", &indentkey,
  30.       "Use TAB character", "b", &usetabs,
  31.       "General indent level", "i", &indent_level,
  32.       "'case' indent level", "i", &case_level,
  33.       "Brace offset", "i", &brace_offset,
  34.       "Continuation offset", "i", &cont_offset,
  35.       "Parenthesis match delay", "i", &delay
  36.    );
  37.  
  38.    AssignKey("ForceIndent();", indentkey, "c_indent_mode");
  39.  
  40.    SetInfo(-1, "c_intelligence", intelligence);
  41.    SetInfo(-1, "c_indentkey", indentkey);
  42.    SetInfo(-1, "c_usetabs", usetabs);
  43.    SetInfo(-1, "c_indent_level", indent_level);
  44.    SetInfo(-1, "c_case_level", case_level);
  45.    SetInfo(-1, "c_brace_offset", brace_offset);
  46.    SetInfo(-1, "c_cont_offset", cont_offset);
  47.    SetInfo(-1, "c_delay", delay);
  48. }
  49.  
  50. int PrevLine()
  51. {
  52.    CursorStack(-1);
  53.    if(0 == Search(any_character, backward))
  54.    {
  55.       int line;
  56.  
  57.       line = ReadInfo("line");
  58.       CursorStack(1);
  59.       return(line);
  60.    }
  61.    else
  62.    {
  63.       CursorStack(1);
  64.       return(1);
  65.    }
  66. }
  67.  
  68. export void MatchIt(string paren)
  69. {
  70.    Output(paren);
  71.    CursorLeft(1);
  72.    if (MatchParen()>=0)
  73.    {
  74.       Delay(ReadInfo("c_delay"));
  75.       MatchParen();
  76.    }
  77.    else
  78.    {
  79.       ReturnStatus("No match!");
  80.    }
  81.    CursorRight(1);
  82. }
  83.  
  84. string InsertIndent(string output, int indentation)
  85. {
  86.    int x;
  87.    int tabsize = ReadInfo("tab_size");
  88.    int tabs = indentation / tabsize;
  89.    int spaces= indentation % tabsize;
  90.  
  91.    if (ReadInfo("c_usetabs"))
  92.    {
  93.       while(x < tabs)
  94.       {
  95.      output += "\t";
  96.      x++;
  97.       }
  98.       for (x = 0; x < spaces; x++)
  99.       {
  100.      output += " ";
  101.       }
  102.    }
  103.    else
  104.    {
  105.       for (x = 0; x < indentation; x++)
  106.       {
  107.      output += " ";
  108.       }
  109.    }
  110.    return (output);
  111. }
  112.  
  113. export void HashMark()
  114. {
  115.    int v = Visible(0);
  116.    int dum = Output("#");
  117.    int line = ReadInfo("line");
  118.    string thisline = GetLine();
  119.    int thislinelen = strlen(thisline) - (line != ReadInfo("lines"));
  120.    
  121.    CursorStack(-1);
  122.    GotoLine(line, 0);
  123.    CursorLeft();    /* Fix! The search cannot find if we are at
  124.                   the beginning of the line */
  125.    
  126.    if(0 <= Search("^[ \t]*\\#$",forward, thislinelen))
  127.    {
  128.       GotoLine(line, 0);
  129.       DeleteEol();
  130.       Output("#");
  131.    }
  132.    else
  133.    {
  134.       CursorStack(1);
  135.    }
  136.    Visible(v);
  137. }
  138.  
  139. export void Colon()
  140. {
  141.    int v = Visible(0);
  142.    int dum = Output(":");
  143.    int line = ReadInfo("line");
  144.    string thisline = GetLine();
  145.    int thislinelen = strlen(thisline) - (line != ReadInfo("lines"));
  146.    
  147.    CursorStack(-1);
  148.    GotoLine(line, 0);
  149.    CursorLeft();    /* Fix! The search cannot find if we are at
  150.                   the beginning of the line */
  151.    
  152.    if(0 <= Search(case_or_default, forward, thislinelen))
  153.    {
  154.       if (0 <= Search(_switch, backward))
  155.       {
  156.      int x;
  157.      int prevline = ReadInfo("line");
  158.      int linelen = ReadInfo("line_length") - (prevline != ReadInfo("lines"));
  159.      int indentation;
  160.      
  161.      GotoLine(prevline, 0);
  162.  
  163.      indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1 + ReadInfo("c_case_level");
  164.  
  165.      GotoLine(line, 0);
  166.      Replace(2, whitespace, "", forward, linelen);
  167.  
  168.      Output(InsertIndent("", indentation));
  169.      Search(":", forward);
  170.      CursorRight();
  171.       }
  172.       else
  173.       {
  174.      CursorStack(1);
  175.       }
  176.    }
  177.    else
  178.    {
  179.       CursorStack(1);
  180.    }
  181.    Visible(v);
  182. }
  183.  
  184. export void LeftBrace()
  185. {
  186.    int v = Visible(0);
  187.    int line = ReadInfo("line");
  188.    int thislinelen = ReadInfo("line_length") - (line != ReadInfo("lines"));
  189.  
  190.    Output("{");
  191.   
  192.    CursorStack(-1);
  193.    GotoLine(line, 0);
  194.    if (GetChar() != '{' && (0 <= Search("^[ \t]*{", forward, thislinelen)))
  195.    {
  196.       int x;
  197.       int prevline = PrevLine();
  198.       string input = GetLine(prevline);
  199.       int linelen = strlen(input) - (line != ReadInfo("lines"));
  200.       int indentation;
  201.       
  202.       GotoLine(prevline, 0);
  203.       indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1;
  204.  
  205.       if(0 == Search(left_brace_at_eol, forward, linelen))
  206.       {
  207.      indentation += ReadInfo("c_indent_level");
  208.       }
  209.  
  210.       GotoLine(line, 0);
  211.  
  212.       Replace(2, whitespace, "", forward, thislinelen);
  213.  
  214.       Output(InsertIndent("", indentation));
  215.       CursorRight();
  216.    }
  217.    else
  218.    {
  219.       CursorStack(1);
  220.    }
  221.    Visible(v);
  222. }
  223.  
  224. export void ForceIndent()
  225. {
  226.    int v = Visible(0);
  227.    int thisline = ReadInfo("line");
  228.    int thislinelen = ReadInfo("line_length") - (thisline != ReadInfo("lines"));
  229.    int linelen, prevline;
  230.    int indentation, intelligence;
  231.  
  232.    CursorStack(-1);
  233.    GotoLine(ReadInfo("line"), 0);
  234.    prevline = PrevLine();
  235.  
  236.    GotoLine(prevline, 0);
  237.    linelen = ReadInfo("line_length") - (ReadInfo("line") != ReadInfo("lines"));
  238.    indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1;
  239.  
  240.    if (prevline < thisline)
  241.    {
  242.       CursorStack(1);
  243.       CursorStack(-1);
  244.  
  245.       GotoLine(thisline, 0);
  246.  
  247.       if (0 <= Search(case_or_default, forward, thislinelen))
  248.       {
  249.      if (0 <= Search(_switch, backward))
  250.      {
  251.         int x;
  252.         int prevline = ReadInfo("line");
  253.         int linelen = ReadInfo("line_length") - (prevline != ReadInfo("lines"));
  254.  
  255.         GotoLine(prevline, 0);
  256.  
  257.         indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1 + ReadInfo("c_case_level");
  258.      }
  259.       }
  260.       else if (0 <= Search("^[ \t]*\\#", forward, thislinelen))
  261.       {
  262.      indentation = 0;
  263.       }
  264.       else if (GetChar() != '}' && 0 > Search("^[ \t]*}", forward, thislinelen))
  265.       {
  266.      GotoLine(prevline, 0);
  267.      if (0 == Search(left_brace_at_eol, forward, linelen))
  268.      {
  269.         indentation += ReadInfo("c_indent_level");
  270.      }
  271.      else if (intelligence = ReadInfo("c_intelligence"))
  272.      {
  273.         if (0 == Search(case_or_default, forward, linelen))
  274.         {
  275.            indentation += ReadInfo("c_indent_level");
  276.         }
  277.         else if (0 == Search(if_or_while, forward, linelen))
  278.         {
  279.            indentation += ReadInfo("c_cont_offset");
  280.         }
  281.         else if (0 == Search(_else, forward, linelen))
  282.         {
  283.            indentation += ReadInfo("c_cont_offset");
  284.         }
  285.         else if (intelligence > 1)
  286.         {
  287.            prevline = PrevLine();
  288.            GotoLine(prevline, 0);
  289.            linelen = ReadInfo("line_length");
  290.            if (0 == Search(if_or_while, forward, linelen))
  291.            {
  292.           indentation -= ReadInfo("c_cont_offset");
  293.            }
  294.            else if (0 == Search(_else, forward, linelen))
  295.            {
  296.           indentation -= ReadInfo("c_cont_offset");
  297.            }
  298.         }
  299.      }
  300.       }
  301.       else
  302.       {
  303.      int prevline;
  304.      int prevlen;
  305.      string input;
  306.  
  307.      if (ReadInfo("c_intelligence"))
  308.      {
  309.         int end, left, right;
  310.  
  311.         right = 1;
  312.         do
  313.         {
  314.            if (0 == (end = Search(left_or_right_brace, backward)))
  315.            {
  316.           if (GetChar() == '{')
  317.              left++;
  318.           else
  319.              right++;
  320.            }
  321.         }
  322.         while (!end && (left < right));
  323.  
  324.         if (left >= right)
  325.         {
  326.            prevline = ReadInfo("line");
  327.            GotoLine(prevline, 0);
  328.            indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1;
  329.         }
  330.      }
  331.       }
  332.    }
  333.  
  334.    CursorStack(1);
  335.    GotoLine(ReadInfo("line"), 0);
  336.    Replace(2, whitespace, "", forward, linelen);
  337.  
  338.    Output(InsertIndent("", indentation));
  339.    GotoLine(thisline, ReadInfo("line_length"));
  340.    Visible(v);
  341. }
  342.  
  343. export void CIndent()
  344. {
  345.    int v = Visible(0);
  346.    int linelen = Output("\n");
  347.    int prevline = PrevLine();
  348.    int indentation, intelligence;
  349.  
  350.    CursorStack(-1);
  351.    GotoLine(prevline, 0);
  352.    linelen = ReadInfo("line_length") - (ReadInfo("line") != ReadInfo("lines"));
  353.    indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1;
  354.  
  355.    CursorStack(1);
  356.    CursorStack(-1);
  357.    if (GetChar() != '}' || 0 > Search("^[ \t]*}", forward, linelen))
  358.    {
  359.       GotoLine(prevline, 0);
  360.       if (0 == Search(left_brace_at_eol, forward, linelen))
  361.       {
  362.      indentation += ReadInfo("c_indent_level");
  363.       }
  364.       else if (intelligence = ReadInfo("c_intelligence"))
  365.       {
  366.      if (0 == Search(case_or_default, forward, linelen))
  367.      {
  368.         indentation += ReadInfo("c_indent_level");
  369.      }
  370.      else if (0 == Search(if_or_while, forward, linelen))
  371.      {
  372.         indentation += ReadInfo("c_cont_offset");
  373.      }
  374.      else if (0 == Search(_else, forward, linelen))
  375.      {
  376.         indentation += ReadInfo("c_cont_offset");
  377.      }
  378.      else if (intelligence > 1)
  379.      {
  380.         prevline = PrevLine();
  381.         GotoLine(prevline, 0);
  382.         linelen = ReadInfo("line_length");
  383.         if (0 == Search(if_or_while, forward, linelen))
  384.         {
  385.            indentation -= ReadInfo("c_cont_offset");
  386.         }
  387.         else if (0 == Search(_else, forward, linelen))
  388.         {
  389.            indentation -= ReadInfo("c_cont_offset");
  390.         }
  391.      }
  392.       }
  393.    }
  394.    CursorStack(1);
  395.  
  396.    Replace(2, whitespace, "", forward, linelen);
  397.  
  398.    Output(InsertIndent("", indentation));
  399.    Visible(v);
  400. }
  401.  
  402. export void RightBrace()
  403. {
  404.    int line = ReadInfo("line");
  405.    int dum = Output("}");
  406.    int v = Visible(0);
  407.    string thisline = GetLine();
  408.    int thislinelen = strlen(thisline) - (line != ReadInfo("lines"));
  409.    string output;
  410.    int x;
  411.  
  412.    CursorStack(-1);
  413.    GotoLine(line, 0);
  414.    CursorLeft();
  415.    if (0 == Search("^[ \t]*}", forward, thislinelen))
  416.    {
  417.       int prevline;
  418.       int prevlen;
  419.       string input;
  420.  
  421.       if(ReadInfo("c_intelligence"))
  422.       {
  423.      int end, left, right;
  424.  
  425.      CursorStack(1);
  426.      CursorStack(-1);
  427.      do
  428.      {
  429.         if (0 == (end = Search(left_or_right_brace, backward)))
  430.         {
  431.            if (GetChar() == '{')
  432.           left++;
  433.            else
  434.           right++;
  435.         }
  436.      } while (!end && (left != right));
  437.      
  438.      if (end)
  439.      {
  440.         CursorStack(-1);
  441.         Visible(v);
  442.      }
  443.      else
  444.      {
  445.         int indentation;
  446.  
  447.         prevline = ReadInfo("line");
  448.         GotoLine(prevline, 0);
  449.         indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1;
  450.  
  451.         Visible(v);
  452.         Delay(ReadInfo("c_delay"));
  453.         MatchParen();
  454.         v = Visible(0);
  455.         GotoLine(line, 0);
  456.  
  457.         Replace(2, whitespace, "", forward, thislinelen);
  458.  
  459.         Output(InsertIndent("", indentation));
  460.         CursorRight();
  461.         Visible(v);
  462.      }
  463.       }
  464.       else
  465.       {
  466.      CursorStack(1);
  467.      Visible(v);
  468.       }
  469.    }
  470.    else
  471.    {
  472.       CursorStack(1);
  473.       Visible(v);
  474.    }
  475. }
  476.  
  477. export void Slash()
  478. {
  479.    int line = ReadInfo("line");
  480.    int dum = Output("/");
  481.    int pos = ReadInfo("byte_position");
  482.    int v = Visible(0);
  483.    string thisline = GetLine();
  484.    int thislinelen = strlen(thisline) - (line != ReadInfo("lines"));
  485.    string output;
  486.    int x;
  487.  
  488.    CursorStack(-1);
  489.    if(pos > 1)
  490.    {
  491.       if(thisline[pos - 2] == '*')
  492.       {
  493.      if(0 == Search("//(\*)", backward))
  494.      {
  495.         Visible(v);
  496.         Delay(ReadInfo("c_delay"));
  497.         CursorStack(1);
  498.      }
  499.       }
  500.    }
  501.    else
  502.    {
  503.       Visible(v);
  504.    }
  505. }
  506.  
  507. ConstructInfo("c_indent_mode", "", "", "LB", "", 0, 1, 0);
  508. ConstructInfo("c_indent_level", "", "", "LIW", "", 0, 0, 3);
  509. ConstructInfo("c_brace_offset", "", "", "LIW", "", 0, 0, 0);
  510. ConstructInfo("c_cont_offset", "", "", "LIW", "", 0, 0, 3);
  511. ConstructInfo("c_case_level", "", "", "LIW", "", 0, 0, 3);
  512. ConstructInfo("c_usetabs", "", "", "LBW", "", 0, 1, 1);
  513. ConstructInfo("c_delay", "", "", "GIW", "", 0, 0, 5);
  514. ConstructInfo("c_intelligence", "", "", "GCW", "Stupid|Intelligent|Ninja", 0, 0, 2);
  515. ConstructInfo("c_indentkey", "", "", "GSW", "", 0, 0, "Alt i");
  516.  
  517. AssignKey("RightBrace();", "}", "c_indent_mode");
  518. AssignKey("Output(\"\n\");", "Shift 'Return'", "c_indent_mode");
  519. AssignKey("CIndent();", "'Return'", "c_indent_mode");
  520. AssignKey("LeftBrace();", "{", "c_indent_mode");
  521. AssignKey("HashMark();", "#", "c_indent_mode");
  522. AssignKey("Colon();", ":", "c_indent_mode&c_intelligence");
  523. AssignKey("Slash();", "/", "c_indent_mode");
  524. AssignKey("ForceIndent();", ReadInfo("c_indentkey"), "c_indent_mode");
  525.  
  526. AssignKey("MatchIt(\")\");", ")", "c_indent_mode");
  527. AssignKey("MatchIt(\"]\");", "]", "c_indent_mode");
  528.  
  529. AddMode(0,"c_indent_mode", "", "");            // Add as minor mode
  530.  
  531. //»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» CIndent menu ««
  532. MenuAdd("s", "C Indent...", "CModePrefs();", "", 6,6,-1); // Add to PackageSettings
  533. MenuBuild();
  534.