[comp.sources.d] v18i049: Indent, C reformatting program, Part01/03

jef@ace.ee.lbl.gov (Jef Poskanzer) (03/23/89)

I use a somewhat unusual style in C, so I wasn't surprised that indent
couldn't be configured to do quite what I wanted.  But I was surprised
that I only had to add three flags; and I was also surprised at how easy
the flags were to add.  Appended are diffs that add:

    -brr (brace right right), which goes along with -bl and -br, and does
    brace indentation like this:

	if ( foo )
	    {
	    bar
	    }

    -ccin (case code indent), which specifies the indentation of case code
    relative to the case label.  The default is -cci1, but I like cci0,
    like this:

	switch ( foo )
	    {
	    case bar:
	    bletch;
	    }

    -prs (paren spaces), which adds spaces after '(' and before ')'.

The default behaviour should be completely unmodified.  Enjoy.
---
Jef

            Jef Poskanzer   jef@helios.ee.lbl.gov   ...well!pokey

*** /tmp/,RCSt1a04112	Wed Mar 22 22:52:33 1989
--- indent.1	Wed Mar 22 22:51:55 1989
***************
*** 31,36 ****
--- 31,37 ----
  [\ \fB\-bc\fR\ |\ \fB\-nbc\fR\ ]
  [\ \fB\-bl\fR\ ]
  [\ \fB\-br\fR\ ]
+ [\ \fB\-brr\fR\ ]
  [\ \fB\-c\fIn\fR\ ]
  [\ \fB\-cd\fIn\fR\ ]
  [\ \fB\-cdb\fR\ |\ \fB\-ncdb\fR\ ]
***************
*** 37,42 ****
--- 38,44 ----
  [\ \fB\-ce\fR\ |\ \fB\-nce\fR\ ]
  [\ \fB\-ci\fIn\fR\ ]
  [\ \fB\-cli\fIn\fR\ ]
+ [\ \fB\-cci\fIn\fR\ ]
  [\ \fB\-d\fIn\fR\ ]
  [\ \fB\-di\fIn\fR\ ]
  [\ \fB\-fc1\fR\ |\ \fB\-nfc1\fR\ ]
***************
*** 47,52 ****
--- 49,55 ----
  [\ \fB\-lp\fR\ |\ \fB\-nlp\fR\ ]
  [\ \fB\-pcs\fR\ |\ \fB\-npcs\fR\ ]
  [\ \fB\-npro\fR\ ]
+ [\ \fB\-prs\fR\ |\ \fB\-nprs\fR\ ]
  [\ \fB\-psl\fR\ |\ \fB\-npsl\fR\ ]
  [\ \fB\-sc\fR\ |\ \fB\-nsc\fR\ ]
  [\ \fB\-sob\fR\ |\ \fB\-nsob\fR\ ]
***************
*** 108,114 ****
  turns off this option.  The default is
  .BR \-bc .
  .TP 15
! .BR \-br , \-bl
  Specifying
  .B \-bl
  lines up compound statements like this:
--- 111,117 ----
  turns off this option.  The default is
  .BR \-bc .
  .TP 15
! .BR \-br , \-bl , \-brr
  Specifying
  .B \-bl
  lines up compound statements like this:
***************
*** 132,137 ****
--- 135,152 ----
      }
  .ft R
  .fi
+ And specifying
+ .B \-brr
+ makes them look like this:
+ .ne 3
+ .nf
+ .ft L
+     if (...)
+ 	{
+         code
+         }
+ .ft R
+ .fi
  .LP
  .TP 15
  .BI \-c n
***************
*** 181,186 ****
--- 196,209 ----
  default is
  .B \-cli0 .
  .TP 15
+ .BI \-cci n
+ Causes case code to be indented
+ .I n
+ tab stops to the right of the corresponding case label.
+ \fB-cci0.5\fR causes case code to be indented half a tab stop.  The
+ default is
+ .B \-cci1 .
+ .TP 15
  .BI \-d n
  Controls the placement of comments which are not to the
  right of code.  The default
***************
*** 270,275 ****
--- 293,303 ----
  If true (\fB-pcs\fR) all procedure calls will have a space inserted between
  the name and the '('.  The default is 
  .B \-npcs
+ .TP 15
+ .B \-prs , \-nprs
+ If true (\fB-prs\fR) all parentheses will have a space inserted
+ after the '(' and before the ')'.  The default is 
+ .B \-nprs
  .TP 15
  .B \-psl , \-npsl
  If true (\fB-psl\fR) the names of procedures being defined are placed in
*** /tmp/,RCSt1a04112	Wed Mar 22 22:52:35 1989
--- indent_globs.h	Wed Mar 22 22:52:02 1989
***************
*** 94,99 ****
--- 94,101 ----
  				 * comma */
  int         btype_2;		/* when true, brace should be on same line as
  				 * if, while, etc */
+ int         btype_3;		/* when true, braces are not only on the next
+ 				 * line but indented with the enclosed code */
  float       case_ind;		/* indentation level to be used for a "case
  				 * n:" */
  int         code_lines;		/* count of lines with code */
***************
*** 114,119 ****
--- 116,122 ----
  					 * name) */
  int         proc_calls_space;	/* If true, procedure calls look like:
  				 * foo(bar) rather than foo (bar) */
+ int         parens_space;	/* If true, parens gets spaces inside them */
  int         format_col1_comments;	/* If comments which start in column 1
  					 * are to be magically reformatted
  					 * (just like comments that begin in
***************
*** 256,261 ****
--- 259,266 ----
      int         dumped_decl_indent;
      float       case_indent;	/* The distance to indent case labels from the
  				 * switch statement */
+     float       case_code_indent;	/* The distance to indent case code
+ 					 * from the case label */
      int         in_parameter_declaration;
      int         indent_parameters;
      int         tos;		/* pointer to top of stack */
*** /tmp/,RCSt1a04112	Wed Mar 22 22:52:37 1989
--- args.c	Wed Mar 22 22:51:53 1989
***************
*** 48,53 ****
--- 48,54 ----
  #define	CLI		2	/* case label indent (float) */
  #define	STDIN		3	/* use stdin */
  #define	KEY		4	/* type (keyword) */
+ #define	CCI		5	/* case code indent (float) */
  
  /*
   * N.B.: because of the way the table here is scanned, options whose names are
***************
*** 72,77 ****
--- 73,79 ----
      "bc", PRO_BOOL, true, OFF, &ps.leave_comma,
      "bl", PRO_BOOL, true, OFF, &btype_2,
      "br", PRO_BOOL, true, ON, &btype_2,
+     "brr", PRO_BOOL, true, ON, &btype_3,
      "bs", PRO_BOOL, false, ON, &Bill_Shannon,
      "cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline,
      "cd", PRO_INT, 0, 0, &ps.decl_com_ind,
***************
*** 78,83 ****
--- 80,86 ----
      "ce", PRO_BOOL, true, ON, &cuddle_else,
      "ci", PRO_INT, 0, 0, &continuation_indent,
      "cli", PRO_SPECIAL, 0, CLI, 0,
+     "cci", PRO_SPECIAL, 0, CCI, 0,
      "c", PRO_INT, 33, 0, &ps.com_ind,
      "di", PRO_INT, 16, 0, &ps.decl_indent,
      "dj", PRO_BOOL, false, ON, &ps.ljust_decl,
***************
*** 113,118 ****
--- 116,122 ----
      "nlp", PRO_BOOL, true, OFF, &lineup_to_parens,
      "npcs", PRO_BOOL, false, OFF, &proc_calls_space,
      "npro", PRO_SPECIAL, 0, IGN, 0,
+     "nprs", PRO_BOOL, false, OFF, &parens_space,
      "npsl", PRO_BOOL, true, OFF, &procnames_start_line,
      "nps", PRO_BOOL, false, OFF, &pointer_as_binop,
      "nsc", PRO_BOOL, true, OFF, &star_comment_cont,
***************
*** 119,124 ****
--- 123,129 ----
      "nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines,
      "nv", PRO_BOOL, false, OFF, &verbose,
      "pcs", PRO_BOOL, false, ON, &proc_calls_space,
+     "prs", PRO_BOOL, false, ON, &parens_space,
      "psl", PRO_BOOL, true, ON, &procnames_start_line,
      "ps", PRO_BOOL, false, ON, &pointer_as_binop,
      "sc", PRO_BOOL, true, ON, &star_comment_cont,
***************
*** 193,202 ****
      register struct pro *p;
  
      /*
!      * Because ps.case_indent is a float, we can't initialize it from the
!      * table:
       */
      ps.case_indent = 0.0;	/* -cli0.0 */
      for (p = pro; p->p_name; p++)
  	if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT)
  	    *p->p_obj = p->p_default;
--- 198,208 ----
      register struct pro *p;
  
      /*
!      * Because ps.case_indent and ps.case_code_indent are floats, we can't
!      * initialize them from the table:
       */
      ps.case_indent = 0.0;	/* -cli0.0 */
+     ps.case_code_indent = 1.0;	/* -cci1.0 */
      for (p = pro; p->p_name; p++)
  	if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT)
  	    *p->p_obj = p->p_default;
***************
*** 227,232 ****
--- 233,244 ----
  	    if (*param_start == 0)
  		goto need_param;
  	    ps.case_indent = atof(param_start);
+ 	    break;
+ 
+ 	case CCI:
+ 	    if (*param_start == 0)
+ 		goto need_param;
+ 	    ps.case_code_indent = atof(param_start);
  	    break;
  
  	case STDIN:
*** /tmp/,RCSt1a04112	Wed Mar 22 22:52:40 1989
--- indent.c	Wed Mar 22 22:52:00 1989
***************
*** 130,141 ****
--- 130,144 ----
  				 * by an arg, we will set this equal to
  				 * ps.com_ind */
      btype_2 = 1;		/* -br */
+     btype_3 = 0;		/* not -brr */
      cuddle_else = 1;		/* -ce */
      ps.unindent_displace = 0;	/* -d0 */
      ps.case_indent = 0;		/* -cli0 */
+     ps.case_code_indent = 1;	/* -cci1 */
      format_col1_comments = 1;	/* -fc1 */
      procnames_start_line = 1;	/* -psl */
      proc_calls_space = 0;	/* -npcs */
+     parens_space = 0;		/* -nprs */
      comment_delimiter_on_blankline = 1;	/* -cdb */
      ps.leave_comma = 1;		/* -nbc */
  #endif
***************
*** 411,417 ****
  		(type_code != form_feed)) {
  	    if (force_nl &&
  		    (type_code != semicolon) &&
! 		    (type_code != lbrace || !btype_2)) {
  		/* we should force a broken line here */
  		if (verbose && !flushed_nl)
  		    diag(0, "Line broken");
--- 414,420 ----
  		(type_code != form_feed)) {
  	    if (force_nl &&
  		    (type_code != semicolon) &&
! 		    (type_code != lbrace || btype_3 || !btype_2)) {
  		/* we should force a broken line here */
  		if (verbose && !flushed_nl)
  		    diag(0, "Line broken");
***************
*** 484,489 ****
--- 487,494 ----
  		}
  	    else
  		*e_code++ = token[0];
+ 	    if (parens_space && *token != '[')
+ 		*e_code++ = ' ';
  	    ps.paren_indents[ps.p_l_follow - 1] = e_code - s_code;
  	    if (sp_sw && ps.p_l_follow == 1 && extra_expression_indent
  		    && ps.paren_indents[0] < 2 * ps.ind_size)
***************
*** 516,521 ****
--- 521,528 ----
  	    if (e_code == s_code)	/* if the paren starts the line */
  		ps.paren_level = ps.p_l_follow;	/* then indent it */
  
+ 	    if (parens_space && *token != ']')
+ 		*e_code++ = ' ';
  	    *e_code++ = token[0];
  	    ps.want_blank = true;
  
***************
*** 530,539 ****
  
  		parse(hd_type);	/* let parser worry about if, or whatever */
  	    }
! 	    ps.search_brace = btype_2;	/* this should insure that constructs
! 					 * such as main(){...} and int[]{...}
! 					 * have their braces put in the right
! 					 * place */
  	    break;
  
  	case unary_op:		/* this could be any unary operation */
--- 537,547 ----
  
  		parse(hd_type);	/* let parser worry about if, or whatever */
  	    }
! 	    ps.search_brace = btype_2 && !btype_3;
! 	    /*
! 	     * this should insure that constructs such as main(){...} and
! 	     * int[]{...} have their braces put in the right place
! 	     */
  	    break;
  
  	case unary_op:		/* this could be any unary operation */
***************
*** 716,722 ****
  		ps.block_init_level++;
  
  	    if (s_code != e_code && !ps.block_init) {
! 		if (!btype_2) {
  		    dump_line();
  		    ps.want_blank = false;
  		}
--- 724,730 ----
  		ps.block_init_level++;
  
  	    if (s_code != e_code && !ps.block_init) {
! 		if (btype_3 || !btype_2) {
  		    dump_line();
  		    ps.want_blank = false;
  		}
***************
*** 739,745 ****
  		    ps.ind_level = ps.i_l_follow;
  		}
  	    }
! 	    if (s_code == e_code)
  		ps.ind_stmt = false;	/* dont put extra indentation on line
  					 * with '{' */
  	    if (ps.in_decl && ps.in_or_st) {	/* this is either a structure
--- 747,755 ----
  		    ps.ind_level = ps.i_l_follow;
  		}
  	    }
! 	    if (btype_3)
! 		ps.ind_stmt = true;
! 	    else if (s_code == e_code)
  		ps.ind_stmt = false;	/* dont put extra indentation on line
  					 * with '{' */
  	    if (ps.in_decl && ps.in_or_st) {	/* this is either a structure
***************
*** 801,806 ****
--- 811,822 ----
  		&& ps.il[ps.tos] >= ps.ind_level;
  	    if (ps.tos <= 1 && blanklines_after_procs && ps.dec_nest <= 0)
  		postfix_blankline_requested = 1;
+ 	    if (btype_3 && ps.p_stack[ps.tos] == dohead) {
+ 		if (verbose)
+ 		    diag(0, "Line broken");
+ 		dump_line();
+ 		ps.want_blank = false;
+ 	    }
  	    break;
  
  	case swstmt:		/* got keyword "switch" */
*** /tmp/,RCSt1a04112	Wed Mar 22 22:52:43 1989
--- io.c	Wed Mar 22 22:52:05 1989
***************
*** 73,82 ****
  	while (--n_real_blanklines >= 0)
  	    putc('\n', output);
  	n_real_blanklines = 0;
! 	if (ps.ind_level == 0)
! 	    ps.ind_stmt = 0;	/* this is a class A kludge. dont do
! 				 * additional statement indentation if we are
! 				 * at bracket level 0 */
  
  	if (e_lab != s_lab || e_code != s_code)
  	    ++code_lines;	/* keep count of lines with code */
--- 73,89 ----
  	while (--n_real_blanklines >= 0)
  	    putc('\n', output);
  	n_real_blanklines = 0;
! 	if (ps.ind_level == 0) {
! 	    if (!btype_3)
! 		ps.ind_stmt = 0;	/* this is a class A kludge. dont do
! 					 * additional statement indentation
! 					 * if we are at bracket level 0 */
! 	    else
! 		if (*s_code != '{')
! 		    ps.ind_stmt = 0;	/* this one is a class AA kludge:
! 					 * defeat the class A kludge if
! 					 * the statement is '{' */
! 	}
  
  	if (e_lab != s_lab || e_code != s_code)
  	    ++code_lines;	/* keep count of lines with code */
*** /tmp/,RCSt1a04112	Wed Mar 22 22:52:45 1989
--- parse.c	Wed Mar 22 22:52:07 1989
***************
*** 49,55 ****
  				 * input */
  
      case decl:			/* scanned a declaration word */
! 	ps.search_brace = btype_2;
  	/* indicate that following brace should be on same line */
  	if (ps.p_stack[ps.tos] != decl) {	/* only put one declaration
  						 * onto stack */
--- 49,55 ----
  				 * input */
  
      case decl:			/* scanned a declaration word */
! 	ps.search_brace = btype_2 && !btype_3;
  	/* indicate that following brace should be on same line */
  	if (ps.p_stack[ps.tos] != decl) {	/* only put one declaration
  						 * onto stack */
***************
*** 78,84 ****
  	ps.p_stack[++ps.tos] = tk;
  	ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
  	++ps.i_l_follow;	/* subsequent statements should be indented 1 */
! 	ps.search_brace = btype_2;
  	break;
  
      case lbrace:		/* scanned { */
--- 78,84 ----
  	ps.p_stack[++ps.tos] = tk;
  	ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
  	++ps.i_l_follow;	/* subsequent statements should be indented 1 */
! 	ps.search_brace = btype_2 && !btype_3;
  	break;
  
      case lbrace:		/* scanned { */
***************
*** 96,102 ****
  		/*
  		 * it is a group as part of a while, for, etc.
  		 */
! 		if (ps.p_stack[ps.tos] == swstmt && ps.case_indent >= 1)
  		    --ps.ind_level;
  		/*
  		 * for a switch, brace should be two levels out from the code
--- 96,103 ----
  		/*
  		 * it is a group as part of a while, for, etc.
  		 */
! 		if (ps.p_stack[ps.tos] == swstmt && ps.case_indent >= 1 &&
! 		    ps.case_code_indent >= 1)
  		    --ps.ind_level;
  		/*
  		 * for a switch, brace should be two levels out from the code
***************
*** 122,128 ****
  	    ps.p_stack[++ps.tos] = whilestmt;
  	    ps.il[ps.tos] = ps.i_l_follow;
  	    ++ps.i_l_follow;
! 	    ps.search_brace = btype_2;
  	}
  
  	break;
--- 123,129 ----
  	    ps.p_stack[++ps.tos] = whilestmt;
  	    ps.il[ps.tos] = ps.i_l_follow;
  	    ++ps.i_l_follow;
! 	    ps.search_brace = btype_2 && !btype_3;
  	}
  
  	break;
***************
*** 138,144 ****
  						 * be in 1 level */
  	    ps.p_stack[ps.tos] = elsehead;
  	    /* remember if with else */
! 	    ps.search_brace = btype_2 | ps.else_if;
  	}
  	break;
  
--- 139,145 ----
  						 * be in 1 level */
  	    ps.p_stack[ps.tos] = elsehead;
  	    /* remember if with else */
! 	    ps.search_brace = (btype_2 && !btype_3) | ps.else_if;
  	}
  	break;
  
***************
*** 145,151 ****
      case rbrace:		/* scanned a } */
  	/* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
  	if (ps.p_stack[ps.tos - 1] == lbrace) {
! 	    ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
  	    ps.p_stack[ps.tos] = stmt;
  	}
  	else
--- 146,155 ----
      case rbrace:		/* scanned a } */
  	/* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
  	if (ps.p_stack[ps.tos - 1] == lbrace) {
! 	    if (btype_3)
! 		ps.i_l_follow = ps.il[--ps.tos];
! 	    else
! 		ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
  	    ps.p_stack[ps.tos] = stmt;
  	}
  	else
***************
*** 157,168 ****
  	ps.cstk[ps.tos] = case_ind;
  	/* save current case indent level */
  	ps.il[ps.tos] = ps.i_l_follow;
! 	case_ind = ps.i_l_follow + ps.case_indent;	/* cases should be one
! 							 * level down from
! 							 * switch */
! 	ps.i_l_follow += ps.case_indent + 1;	/* statements should be two
! 						 * levels in */
! 	ps.search_brace = btype_2;
  	break;
  
      case semicolon:		/* this indicates a simple stmt */
--- 161,175 ----
  	ps.cstk[ps.tos] = case_ind;
  	/* save current case indent level */
  	ps.il[ps.tos] = ps.i_l_follow;
! 	case_ind = ps.i_l_follow + ps.case_indent;
! 	/*
! 	 * cases should be one level down from switch
! 	 */
! 	ps.i_l_follow += ps.case_indent + ps.case_code_indent;
! 	/*
! 	 * statements should be two levels in
! 	 */
! 	ps.search_brace = btype_2 && !btype_3;
  	break;
  
      case semicolon:		/* this indicates a simple stmt */