home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / AP / JED / JED097-1.TAR / jed / lib / indent.sl < prev    next >
Encoding:
Text File  |  1994-12-12  |  1.9 KB  |  119 lines

  1. % These routines perform a better job of indenting C code.
  2.  
  3. !if (is_defined ("C_No_Brace_Offset"))
  4. {
  5.    variable C_No_Brace_Offset = 0;
  6. }
  7.  
  8. define c_looking_at (token)
  9. {
  10.    variable cse = CASE_SEARCH, ret = 0;
  11.    CASE_SEARCH = 0;
  12.    
  13.    push_spot ();
  14.    if (looking_at(token))
  15.      {
  16.     go_right(strlen(token));
  17.     POINT;
  18.     skip_chars ("\t :({");
  19.     ret = (POINT - ()) or eolp();
  20.      }
  21.    pop_spot ();
  22.    CASE_SEARCH = cse;
  23.    ret;
  24. }
  25.  
  26.  
  27. define c_indent_line_1 ()
  28. {
  29.    variable ch;
  30.  
  31.    push_spot ();
  32.    EXIT_BLOCK {pop_spot ();}
  33.    
  34.    bol (); bskip_chars ("\n \t");
  35.    go_left (1);
  36.    ch = what_char ();
  37.    if ((ch == ';') or (ch == '/') or (ch == '{')) return;
  38.    
  39.    if (ch == ')') call ("goto_match");
  40.    bskip_chars (" \t");
  41.    bskip_chars ("^ \t");
  42.    
  43.    if (orelse 
  44.      {c_looking_at("if");}
  45.      {c_looking_at("else");}
  46.      {c_looking_at("for");}
  47.      {c_looking_at("while");}
  48.        )
  49.      {
  50.     pop_spot ();
  51.     bol ();
  52.     skip_white ();
  53.     !if (looking_at_char ('{'))
  54.       {
  55.          %
  56.          %  increase indentation level
  57.          %
  58.          whitespace (C_BRACE + C_No_Brace_Offset);
  59.       }
  60.     push_spot ();  % for exit block
  61.      }
  62. }
  63.  
  64. variable C_Indent_In_Indent = 0;
  65. define c_colon_indent_line ()
  66. {
  67.    if (C_Indent_In_Indent) return 0;
  68.    
  69.    push_spot ();
  70.    bol ();
  71.    skip_white ();
  72.    if (looking_at_char ('#')) 
  73.      {
  74.     bol (); 
  75.     trim ();
  76.     return 1;
  77.      }
  78.  
  79.    C_Indent_In_Indent++;
  80.    
  81.    !if (orelse
  82.       { c_looking_at("case"); }
  83.       { c_looking_at("default"); }
  84.       { c_looking_at("protected");}
  85.       { c_looking_at("private");}
  86.       { c_looking_at("public");}
  87.     )
  88.      {
  89.     pop_spot ();
  90.     C_Indent_In_Indent--;
  91.     return 0;
  92.      }
  93.    
  94.    indent_line ();
  95.    bol (); skip_white ();
  96.    what_column() - C_INDENT;
  97.    bol(); trim();
  98.    whitespace(());
  99.    pop_spot ();
  100.    if (bolp()) skip_white ();
  101.    C_Indent_In_Indent--;
  102.    1;
  103.  
  104. % indent_line_cmd will automatically call this.
  105. define c_indent_line ()
  106. {
  107.    if (c_colon_indent_line ())
  108.      {
  109.     return;
  110.      }
  111.    
  112.    c_indent_line_1 ();
  113. }
  114.  
  115.  
  116.          
  117.    
  118.