home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / ME22-OS2.ZIP / MACROS.ZIP / CINDENT.M < prev    next >
Text File  |  1989-01-20  |  4KB  |  193 lines

  1. /*
  2.   This indenting package has been set up for the style :
  3.     if (xxx)
  4.     {
  5.       statements
  6.     }
  7. */
  8.      
  9. #define FALSE  0
  10. #define TRUE   1
  11.  
  12.  
  13. string C_Keywords;
  14. int    CIndent;
  15.  
  16. init()
  17. {
  18.   assign_key("C_SmartIndent", '\r');
  19.   C_Keywords = "if|else|for|while|switch|case";
  20.   CIndent = TRUE;
  21. }
  22.  
  23. CIndentOn()
  24. {
  25.   CIndent = TRUE;
  26. }
  27.  
  28. CIndentOff()
  29. {
  30.   CIndent = FALSE;
  31. }
  32.  
  33.  
  34. C_SmartIndent()
  35. {
  36.   /*
  37.     We use the following rules :
  38.     1) if the current line has a C statement like "if", "while", etc, 
  39.        we will indent to the level of that line.
  40.     2) if the current line ends with a semicolon, then we want to look at
  41.        the previous line. If the previous line also ends in a semicolon, then
  42.        the indentation level should be the same as the current line. If the
  43.        previous line has a C statement on it (and doesn't end in '{'), then
  44.        we will indent to the level of that C statement.
  45.     3) If the current line is blank, then start the line at column 1.
  46.     4) If the current line consists of '}', then indent at the level of the
  47.        current line.
  48.   */
  49.   
  50.  
  51.   /*
  52.     If we are splitting a line in the middle, just insert a '\r'
  53.   */
  54.   if (!CIndent || !IsCFile() || !is_eol())
  55.   {
  56.     insert("\r");
  57.     return;
  58.   }
  59.  
  60.   /* Take a look at the last character on this line */
  61.   if (currcol() > 1)
  62.   {
  63.     left();
  64.     lastchar = currchar();
  65.   }
  66.   else
  67.     lastchar = 0;
  68.  
  69.   gobol();
  70.   CurrlineIndent = 0;
  71.  
  72.   /*
  73.     If the first char in the line is not whitespace, just append '\r'
  74.   */
  75.   if (currchar() != ' ' && currchar() != '\t')
  76.   {
  77. ins_cr:
  78.     autoindent(CurrlineIndent);
  79.     return;
  80.   }
  81.  
  82.  
  83.   cl = currline();
  84.   if ((CurrlineIndent = search_string(cl, "[^ \t]")) < 0)
  85.   {
  86.     /* The line was totally blank. Just perform the usual autoindent. */
  87.     CurrlineIndent = 0;
  88.     goto ins_cr;
  89.   }
  90.   
  91.  
  92.   /*
  93.     Examine the first char on the line
  94.   */
  95.   firstchar = tonum(substr(cl, CurrlineIndent+1, 1));
  96.   CurrlineIndent = NormalizeColumn(cl, CurrlineIndent+1);
  97.   setcol(CurrlineIndent);
  98.   
  99.   /*
  100.     If the only char on the line is a left brace, indent one more level.
  101.     If the only char was '}' (or anything else), indent at the same level.
  102.   */
  103.   if (firstchar == lastchar)
  104.   {
  105.     autoindent(CurrlineIndent);
  106.     /*
  107.         {_  =>  {          }_  =>  }
  108.                   _                _
  109.                  
  110.     */
  111.     if (lastchar == '{')
  112.       instab();
  113.   }
  114.  
  115.   else if (search_string(substr(cl, CurrlineIndent, 5), C_Keywords) >= 0)
  116.   {
  117.     autoindent(CurrlineIndent);
  118.   }
  119.  
  120.   else if (lastchar == ';')
  121.   {
  122.     if (up())
  123.     {
  124.       cl = currline();
  125.       down();  goeol();
  126.       if ((prevIndent = search_string(cl, "[^ \t]")) >= 0)
  127.         if (search_string(substr(cl, prevIndent+1, 5), C_Keywords) >= 0)
  128.           /*
  129.                 FOUND KEYWORD                      KEYWORD NOT FOUND
  130.              if (xxx)    =>  if (xxx)            c = d;    =>    c = d;
  131.                a = b;_         a = b;            a = b;_         a = b;
  132.                              _                                   _
  133.           */
  134.           CurrlineIndent = NormalizeColumn(cl, prevIndent + 1);
  135.     }
  136.     autoindent(CurrlineIndent);
  137.   }
  138.  
  139.   else
  140.     autoindent(CurrlineIndent);
  141. }
  142.  
  143.  
  144. autoindent(CurrlineIndent)
  145. {
  146.   goeol();
  147.   insert("\r");
  148.   if (CurrlineIndent)
  149.     setcol(CurrlineIndent);
  150. }
  151.  
  152. instab()
  153. {
  154.   insert("\t");
  155.   goeol();
  156. }
  157.  
  158. NormalizeColumn(cl, endcol)
  159.   string cl;
  160.   int    endcol;
  161. {
  162.   col = 1;
  163.   tab_distance = get_option("ta");
  164.  
  165.   for (i = 1;  i < endcol;  i++)
  166.   {
  167.       if (substr(cl, i, 1) == "\t")
  168.     {
  169.         col = (col + tab_distance) / tab_distance * tab_distance + 1;
  170.     }
  171.     else
  172.       col++;
  173.   }
  174.   return col;
  175. }
  176.  
  177.  
  178. IsCFile()
  179. {
  180.   currfile = filename();
  181.  
  182.   if ((i = index(currfile, ".")) > 0)           /* get the extension */
  183.   {
  184.     extension = substr(currfile, i+1, 3);
  185.     if (extension != "c" && extension != "C" && /* is it a C file?   */
  186.         extension != "h" && extension != "H")
  187.       return 0;
  188.     return 1;
  189.   }
  190.   return 0;
  191. }
  192.  
  193.