home *** CD-ROM | disk | FTP | other *** search
/ Aminet 10 / aminetcdnumber101996.iso / Aminet / text / edit / FrexxEdA.lha / FrexxEd / fpl / CIndentMode.FPL < prev    next >
Text File  |  1995-10-09  |  6KB  |  193 lines

  1. // $Id: CIndentMode.FPL 1.4 1995/07/21 10:30:50 jskov Exp jskov $
  2. // $VER: CIndentMode.FPL Version 0.2 (07.03.95) © Jesper Skov
  3.  
  4. //»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» CIndentModePrefs() ««
  5. void export CIndentModePrefs()
  6. {
  7.   PromptInfo(-1,"CIndentMode preferences",-1,-1,
  8.    "c_usetabs",
  9.    "c-indent-level",
  10.    "c-cont-offset",
  11.    "c-brace-offset",
  12.    "c-label-offset"
  13.    );
  14. }
  15.  
  16. //»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» CIndentBlock() ««
  17. void export CIndentBlock()
  18. {
  19.   if (!ReadInfo("block_exist"))
  20.     ReturnStatus("No block marked!");        // Complaint no block marked
  21.   else {
  22.     int start=ReadInfo("block_begin_y");
  23.     int end=ReadInfo("block_end_y");
  24.  
  25.     BlockMark(0);                            // Remove block mark
  26.     GotoLine(start);
  27.     while (ReadInfo("line")=<end){
  28.       CIndent();
  29.       CursorDown();
  30.     }
  31.   }
  32. }
  33.  
  34. //»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» CIndent() ««
  35. int export CIndent()
  36. {
  37.   int    thisLine = ReadInfo("line");
  38.   int    prevLine;
  39.  
  40.   if ((prevLine=findPrevLine(thisLine)) > 0){
  41.     int indentVal = GetIndent(prevLine, GetLine(prevLine));
  42.     int lineLength, thisBuffer;
  43.     int searchBuffer = DuplicateEntry();
  44.  
  45.     thisBuffer = CurrentBuffer(searchBuffer);
  46.  
  47.     //// Checks on current line
  48.     // c-brace-offset - add if { is first letter on line (not col 0 though)
  49.     GotoLine(thisLine);                        // beg of line
  50.     lineLength = ReadInfo("line_length");
  51.     if ('{'!=GetChar()){                    // check not { on col 0
  52.       CursorLeft(2);
  53.       if (0==Search("\\sN[ \t]*\{","=wf+",lineLength))
  54.         indentVal += ReadInfo("c-brace-offset");
  55.     }
  56.  
  57.     // c-brace-offset - sub if } starts line
  58.     GotoLine(thisLine); // beg of line
  59.     CursorLeft(2);
  60.     if (0==Search("\\sN[ \t]*\}","=wf+",lineLength))
  61.       indentVal -= ReadInfo("c-brace-offset");
  62.  
  63.     // c-label-offset - add if case/default is first on line
  64.     GotoLine(thisLine);
  65.     CursorLeft(2);
  66.     if (0==Search("\\sN[ \t]*((case)|(default))","=wf+", lineLength))
  67.       indentVal += ReadInfo("c-label-offset");
  68.  
  69.  
  70.     //// Checks on previous line
  71.  
  72.     // c-cont-offset - add if prev "if/while" line had no {/;
  73.     GotoLine(prevLine);
  74.     lineLength = ReadInfo("line_length");    // length of prev line + a bit for \n etc
  75.     if (0==Search("(if|while)[ \t]*\\(.*\\)[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*\\sN","=wf+", lineLength))
  76.       indentVal += ReadInfo("c-cont-offset");
  77.     else {                                    // de-indent if prev line was indented
  78.       GotoLine(findPrevLine(prevLine));
  79.       if (0==Search("(if|while)[ \t]*\\(.*\\)[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*\\sN","=wf+", lineLength))
  80.         indentVal -= ReadInfo("c-cont-offset");
  81.     }
  82.  
  83.     // c-indent-level - add if prev line end in {.
  84.     GotoLine(prevLine);
  85.     lineLength -= CursorLeft();
  86.     if (0==Search("\{[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*\\sN","=wf+",lineLength))
  87.       indentVal += ReadInfo("c-indent-level");
  88.  
  89.     // c-indent-level - sub if prev line end in }.
  90.     GotoLine(prevLine);
  91.     if (Isspace(GetChar())){
  92.       CursorRightWord();
  93.       if (GetChar()!='\}'){
  94.         GotoLine(prevLine);
  95.         CursorLeft();
  96.         if (0==Search("\}[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*\\sN","=wf+",lineLength))
  97.           indentVal -= ReadInfo("c-indent-level");
  98.       }
  99.     }
  100.  
  101.     CurrentBuffer(thisBuffer);
  102.     Kill(searchBuffer);
  103.  
  104.     if (GetIndent(thisLine, GetLine(thisLine)) != indentVal){
  105.       GotoLine(thisLine);
  106.       while(Isspace(GetChar())&&!Isnewline(GetChar()))
  107.         Delete();
  108.       if (indentVal>0)
  109.         InsertIndent(indentVal);
  110.     }
  111.   }
  112. }
  113.  
  114.  
  115. int findPrevLine(int thisLine)
  116. {
  117.   int i;
  118.   string s;
  119.   int line=ReadInfo("line");
  120.   int byte=ReadInfo("byte_position");
  121.  
  122.   while(thisLine>0){
  123.     s=GetLine(--thisLine);
  124.  
  125.     for(i=0;(!Isnewline(s[i])&&(Isspace(s[i])));i++);
  126.  
  127.     if (Isnewline(s[i])){
  128.       GotoLine(thisLine);
  129.       DeleteEol();
  130.     } else {
  131.       GotoLine(line, byte);
  132.       return(thisLine);
  133.     }
  134.   }
  135.   GotoLine(line, byte);
  136.   return(0);
  137. }
  138.  
  139. void InsertIndent(int indentation)
  140. {
  141.   int x, tabs, spaces;
  142.   int tabsize = ReadInfo("tab_size");
  143.   string output;
  144.  
  145.   if (ReadInfo("c_usetabs")){
  146.     tabs = indentation / tabsize;
  147.     spaces = indentation - tabs * tabsize;
  148.     for (x = 0; x < tabs; x++){
  149.       output = joinstr(output, "\t");
  150.     }
  151.     for (x = 0; x < spaces; x++){
  152.       output = joinstr(output, " ");
  153.     }
  154.   }
  155.   else {
  156.     for (x = 0; x < indentation; x++){
  157.       output = joinstr(output, " ");
  158.     }
  159.   }
  160.   Output(output);
  161. }
  162.  
  163.  
  164. int GetIndent(int linenum, string inline)
  165. {   
  166.   int linelen = strlen(inline);
  167.   int x;
  168.  
  169.   for (x = 0; x < linelen && ((inline[x] == '\t') || (inline[x] == ' ')); x++);
  170.   return(GetCursor(x, linenum) - 1);
  171. }
  172.  
  173. //»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» Key bindings ««
  174. AssignKey("CIndent();", "'tab'", "c_indent_mode");
  175. AssignKey("Output(\"\t\");", "shift 'tab'", "c_indent_mode");
  176. AssignKey("CIndent();GotoLine(ReadInfo(\"line\"),ReadInfo(\"line_length\"));Output(\"\n\");CIndent();", "shift 'Enter'", "c_indent_mode");
  177. AssignKey("Output(\"\n\");", "'enter'", "c_indent_mode");
  178.  
  179. //»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» CIndentMode settings ««
  180. ConstructInfo("c_usetabs", "", "", "GBWH", "", 0, 1, 1);
  181. ConstructInfo("c-indent-level","","","GIWH","",-32,32,2);
  182. ConstructInfo("c-cont-offset","","","GIWH","",-32,32,2);
  183. ConstructInfo("c-brace-offset","","","GIWH","",-32,32,2);
  184. ConstructInfo("c-label-offset","","","GIWH","",-32,32,-2);
  185.  
  186. ConstructInfo("c_indent_mode", "", "", "LBH", "", 0, 1, 0);
  187.  
  188. AddMode(0,"c_indent_mode", "", "");            // Add as minor mode
  189.  
  190. //»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» CIndent menu ««
  191. MenuAdd("s", "CIndentMode...", "CIndentModePrefs();", "", 6,6,-1); // Add to PackageSettings
  192. MenuBuild();
  193.