bug-indent
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-indent] Addition to indent 2.2.9 - Whitesmiths style


From: Skip Egdorf
Subject: [Bug-indent] Addition to indent 2.2.9 - Whitesmiths style
Date: Thu, 18 Aug 2005 10:05:22 -0600
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050317)

I have always had a partiality to the indenting style known as 'Whitesmith's' style due to its use by Bill Plauger when he did his thing at Whitesmith's. I "invented" the style when I started Unix on V6 in 1976, and the old habit has stuck with me. I know that it is not the most popular style, but it is still found (e.g. with google) in lists of known indentation styles. And more important, I still like it. No accounting for taste I guess...

The style essentially says "Braces should be a part of the code block, not separate, and they should be as unobtrusive as possible". Thus, the braces get indented to the same level as their code block, always on separate lines from code. In a way this anticipates the Python idea that the indentation should show the nesting and the braces sort of disappear or just become white noise around each block.

So it looks like:

while (foo)
       {
       bar;
       }
if (foo)
       {
       bar;
       }
else
       {
       baz;
       }

I was trying to get indent 2.2.9 to format Whitesmith's style, and ended up feeling the need for one additional arg to get the braces and other indentation to work right. I added
  -bif
  --brace-indent-first
to get the braces to follow the Whitesmith's style. The name was loosely modeled after --brace-on-if-line and --brace-after-if-line I did not add a "Whitesmiths" to the general style parameters ala -gnu or -kr, but this could be done.

Now if I missed something big and there is already a clever way to do this with the existing indent, please give me a clue. otherwise, perhaps this can be a helpful start for addition of yet another feature to indent.

In either case, here is a diff against indent 2.2.9 for my added argument. Limited testing seems to have it working. I understand that a quick look through the code may not have given me a deep understanding, and I also suspect that there may be a better way of doing this already. If so, please let me know. Otherwise, this patch is made freely available to you guys for inclusion under the terms of the GPL, and my employer has no restrictions on my contribution of gpl-based patches to gpl-ed code.

I hope it's useful one way or the other.

Skip Egdorf
address@hidden
diff -r indent-2.2.9/doc/indent.texinfo indent-2.2.9-mod/doc/indent.texinfo
713c713,715
< The @option{-br} or @option{-bl} option specifies how to format braces.
---
> @kindex -bif
> @kindex --brace-indent-first
> The @option{-br} or @option{-bl} option along with @option{-bif} specifies 
> how to format braces.
738a741,756
> @noindent
> The @option{-bif} option in conjunction with @option{-in0}, @option{-bl} and 
> @option{-bli} causes the brace
> to indent along with the code in the
> Whitesmith's style.
> 
> @example
> @group
> if (x > 0)
>    @{
>     x--;
>    @}
> @end group
> @end example
> 
> 
> 
744c762,763
< result shown above.  @option{-bli0} results in the following:
---
> result shown above.  @option{bli8} along with @option{-in0} gives the 
> Whitesmith's style.
> @option{-bli0} results in the following:
diff -r indent-2.2.9/src/args.c indent-2.2.9-mod/src/args.c
99a100
> static int exp_bif  = 0;
287a289
>     {"bif",     PRO_BOOL,                           false,      ON,  
> &settings.bif,                              &exp_bif},
401a404
>     {"bif",     PRO_BOOL,                           false,      ON,  
> &settings.bif,                              &exp_bif},
526a530
>     {"brace-indent-first",                          "bif"},
diff -r indent-2.2.9/src/indent.h indent-2.2.9-mod/src/indent.h
253a254
>     int bif;                 /* when true, initially indent brace to code 
> level without affecting code indent */
diff -r indent-2.2.9/src/parse.c indent-2.2.9-mod/src/parse.c
311a312
>             if (!settings.bif)
327a329,332
>               if ( parser_state_tos->ind_level == 0 && settings.bif)
>                 {
>                     parser_state_tos->ind_level += settings.brace_indent;
>                 }
341c346
<                     parser_state_tos->ind_level -= settings.ind_size;
---
>                 parser_state_tos->ind_level -= settings.ind_size;
347,352c352,357
<                 
<                 if (!settings.btype_2)
<                 {
<                     parser_state_tos->ind_level += settings.brace_indent;
<                     parser_state_tos->i_l_follow += settings.brace_indent;
<                 }
---
>                
>                 if (!settings.btype_2 && !settings.bif)
>                   {
>                     parser_state_tos->ind_level += settings.brace_indent;
>                     parser_state_tos->i_l_follow += settings.brace_indent;
>                   }
375a381
> 
381,382c387,396
<                 parser_state_tos->ind_level                 = 
parser_state_tos->i_l_follow;
<                 parser_state_tos->il[parser_state_tos->tos] = 
parser_state_tos->i_l_follow;
---
>               if (settings.bif)
>                 {
>                   parser_state_tos->ind_level                 += 
> settings.ind_size;
>                   parser_state_tos->il[parser_state_tos->tos] += 
> settings.ind_size;
>                 }
>               else
>                 {
>                   parser_state_tos->ind_level                 = 
> parser_state_tos->i_l_follow;
>                   parser_state_tos->il[parser_state_tos->tos] = 
> parser_state_tos->i_l_follow;
>                 }
419,421c433,435
<                 parser_state_tos->i_l_follow = 
parser_state_tos->il[--parser_state_tos->tos];
<                 parser_state_tos->ind_level  = parser_state_tos->i_l_follow;
<                 parser_state_tos->p_stack[parser_state_tos->tos] = stmt;
---
>             parser_state_tos->i_l_follow = 
> parser_state_tos->il[--parser_state_tos->tos];
>             parser_state_tos->ind_level  = parser_state_tos->i_l_follow;
>             parser_state_tos->p_stack[parser_state_tos->tos] = stmt;

reply via email to

[Prev in Thread] Current Thread [Next in Thread]