Changing the C Indent style

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

Changing the C Indent style

A. S. Budden

Dear all,

I was wondering whether it is possible to make a (minor) change to the
indent style used for C code.  Generally I like the style and I don't
want to change much.  The only exception that I'd like to add is such
that the following code is indented as shown (I've changed tabs to
spaces for email readability):

for (;;)
{
    /* This is indented correctly: I don't want to change this */
    CallFunction1();
    CallFunction2();
}

for (;;)
    {CallFunction();} /* This is what I'd like to support for single
lines with open and close braces */

for (;;)
{CallFunction();} /* This is how vim does it */

Is this possible without messing up the rest of the indentation style?

Many thanks in advance,

Al

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Changing the C Indent style

panshizhu

A. S. Budden 写道:

> for (;;)
> {
>     /* This is indented correctly: I don't want to change this */
>     CallFunction1();
>     CallFunction2();
> }
>
> for (;;)
>     {CallFunction();} /* This is what I'd like to support for single
> lines with open and close braces */
>
> for (;;)
> {CallFunction();} /* This is how vim does it */
>
> Is this possible without messing up the rest of the indentation style?

I don't know if I understand you well, but you can use v command to
select the lines you want, and you can use :!indent to indent the
particular block.


The external indent utility is extremely useful and customizable, and it
is very convenient to use inside vim.


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Changing the C Indent style

Benjamin Fritz
In reply to this post by A. S. Budden



On Mar 10, 10:02 am, "A. S. Budden" <[hidden email]> wrote:


> I was wondering whether it is possible to make a (minor) change to the
> indent style used for C code.  Generally I like the style and I don't
> want to change much.  The only exception that I'd like to add is such
> that the following code is indented as shown (I've changed tabs to
> spaces for email readability):
>

C code indentation is controlled by the 'cindent' option.

The behaviour of 'cindent' is controlled by 'cinoptions'

See the :help for each of these options and the links included
therein, and you should be able to figure something out without ever
leaving Vim.
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Changing the C Indent style

Gary Johnson-4

On 2009-03-10, Ben Fritz wrote:

> On Mar 10, 10:02 am, "A. S. Budden" <[hidden email]> wrote:
>
>
> > I was wondering whether it is possible to make a (minor) change to the
> > indent style used for C code.  Generally I like the style and I don't
> > want to change much.  The only exception that I'd like to add is such
> > that the following code is indented as shown (I've changed tabs to
> > spaces for email readability):
> >
>
> C code indentation is controlled by the 'cindent' option.
>
> The behaviour of 'cindent' is controlled by 'cinoptions'
>
> See the :help for each of these options and the links included
> therein, and you should be able to figure something out without ever
> leaving Vim.

I don't think there's any way to use those options to do what he
wants to do, though.  His indentation of opening braces is
inconsistent.


    for (;;)
    {
        /* This is indented correctly: I don't want to change this */
        CallFunction1();
        CallFunction2();
    }

    for (;;)
        {CallFunction();} /* This is what I'd like to support for single
                             lines with open and close braces */


Regards,
Gary



--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Changing the C Indent style

A. S. Budden

2009/3/11 Gary Johnson <[hidden email]>:

>
> On 2009-03-10, Ben Fritz wrote:
>> On Mar 10, 10:02 am, "A. S. Budden" <[hidden email]> wrote:
>>
>>
>> > I was wondering whether it is possible to make a (minor) change to the
>> > indent style used for C code.  Generally I like the style and I don't
>> > want to change much.  The only exception that I'd like to add is such
>> > that the following code is indented as shown (I've changed tabs to
>> > spaces for email readability):
>> >
>>
>> C code indentation is controlled by the 'cindent' option.
>>
>> The behaviour of 'cindent' is controlled by 'cinoptions'
>>
>> See the :help for each of these options and the links included
>> therein, and you should be able to figure something out without ever
>> leaving Vim.
>
> I don't think there's any way to use those options to do what he
> wants to do, though.

That was certainly the impression I got from reading the help on
cinoptions-values.  I guess I was hoping that there would be a way to
change the indenting when the closing brace is on the same line as the
opening brace (much like the way the case statement is realigned when
you add a ':' on the end of the line).

> His indentation of opening braces is inconsistent.

Yes, but it's clearer (to me) than

for (;;)
{CallFunction();}

Al

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Changing the C Indent style

Benjamin Fritz
In reply to this post by Gary Johnson-4



On Mar 11, 1:47 am, Gary Johnson <[hidden email]> wrote:

>
> I don't think there's any way to use those options to do what he
> wants to do, though.  His indentation of opening braces is
> inconsistent.
>

Yeah, I guess not.

Looks like it is time to write your own function for use in an
indentexpr. You can have it return cindent() in most cases, and in
your one case override it.
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

RE: Changing the C Indent style

Gene Kwiecinski
In reply to this post by A. S. Budden

>>His indentation of opening braces is inconsistent.

>Yes, but it's clearer (to me) than
>for (;;)
>{CallFunction();}

Might be academic at this point, but for a single *statement* (not line)
following a for(), if(), while(), etc., enclosing braces are *not*
required.  So

        for(;;)
                CallFunction();

would be sufficient.  Similarly,

        if( condition ) DoStuff(); else DoSomethingElse();

works fine.  Only if you'd want to do something funky like

        for(;;)
                { DoThis(); DoThat(); DoSomethingElseToo(); }

would that style be <coff!> "required".


Dunno, but I turn off all "automatic" indenting and indent things myself
manually.  Get much more control that way for minimal extra effort...
<shrug/>


Ooh..  Quick example:  Why bother with a rather ghastly

        <table class='caution'>
                <thead>
                        <tr>
                                <th>Caution</th>
                        </tr>
                </thead>
                <tbody>
                        <tr>
                                <td>Coffee is hot!</td>
                        </tr>
                </tbody>
        </table>

when a much more compact

        <table class='caution'>
        <thead><tr><th>Caution</th></tr></thead>
        <tbody><tr><td>Coffee is hot!</td></tr></tbody>
        </table>

will do, *and* be more readable?

The first would be generated with automatic indenting rules, and you'd
have to expend *more* effort compacting it afterward, when you could
much more easily just forego automatic indenting and just type what you
*want* directly.

Same goes for C/javascript/perl/etc. "cliche" code snippets.

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Changing the C Indent style

Benjamin Fritz



On Mar 11, 11:16 am, "Gene Kwiecinski" <[hidden email]> wrote:

>
> Ooh..  Quick example:  Why bother with a rather ghastly
>
>         <table class='caution'>
>                 <thead>
>                         <tr>
>                                 <th>Caution</th>
>                         </tr>
>                 </thead>
>                 <tbody>
>                         <tr>
>                                 <td>Coffee is hot!</td>
>                         </tr>
>                 </tbody>
>         </table>
>
> when a much more compact
>
>         <table class='caution'>
>         <thead><tr><th>Caution</th></tr></thead>
>         <tbody><tr><td>Coffee is hot!</td></tr></tbody>
>         </table>
>
> will do, *and* be more readable?
>
> The first would be generated with automatic indenting rules, and you'd
> have to expend *more* effort compacting it afterward, when you could
> much more easily just forego automatic indenting and just type what you
> *want* directly.
>

Actually, the first would only be generated with automatic indenting
rules if you inserted all the line endings yourself, in which case the
second would look even worse:

<table class='caution'>
<thead>
<tr>
<th>Caution</th>
</tr>
</thead>
<tbody>
<tr>
<td>Coffee is hot!</td>
</tr>
</tbody>
</table>

I find that automatic indentation rules, even if they aren't perfect,
usually get the indentation close enough that it takes minimal effort
to fix it. In the case given by the original poster, after typing the
code, a simple >> will fix the indent.

If find yourself constantly re-indenting code (manually or otherwise),
it might be worthwhile to hack together an indent script. If not, it's
probably just better to find settings that get you close, then correct
the indent as required.
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Changing the C Indent style

Ben Schmidt
In reply to this post by A. S. Budden

> Yes, but it's clearer (to me) than
>
> for (;;)
> {CallFunction();}

It looks like you resort to:

func! MyCindent(l)
   let extra = match(getline(prevnonblank(a:l-1)),'^\s*{.*}$')==-1?0:-&sw
   let extra += match(getline(a:l),'^\s*{.*}$')==-1?0:&sw
   return cindent(a:l)+extra
endfunc
:let &l:indentexpr='MyCindent(v:lnum)'
:setlocal cinkeys+=}

Ben.




--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Changing the C Indent style

A. S. Budden
In reply to this post by Gene Kwiecinski

2009/3/11 Gene Kwiecinski <[hidden email]>:

>
>>>His indentation of opening braces is inconsistent.
>
>>Yes, but it's clearer (to me) than
>>for (;;)
>>{CallFunction();}
>
> Might be academic at this point, but for a single *statement* (not line)
> following a for(), if(), while(), etc., enclosing braces are *not*
> required.  So
>
>        for(;;)
>                CallFunction();
>
> would be sufficient.  Similarly,
>
>        if( condition ) DoStuff(); else DoSomethingElse();
>
> works fine.  Only if you'd want to do something funky like
>
>        for(;;)
>                { DoThis(); DoThat(); DoSomethingElseToo(); }
>
> would that style be <coff!> "required".

I realise this, but all C software that I write must be compliant with
the MISRA-C guidelines for the use of the C language in critical
systems, which has rules 14.8/9 which read something along the lines
of:

"The statement forming the body of an if, else if, else, while, do ...
while or for statement shall always be enclosed in braces"

This precludes the use of the brace-free statement form.

> Ooh..  Quick example:  Why bother with a rather ghastly
>
>        <table class='caution'>
>                <thead>
>                        <tr>
>                                <th>Caution</th>
>                        </tr>
>                </thead>
>                <tbody>
>                        <tr>
>                                <td>Coffee is hot!</td>
>                        </tr>
>                </tbody>
>        </table>
>
> when a much more compact
>
>        <table class='caution'>
>        <thead><tr><th>Caution</th></tr></thead>
>        <tbody><tr><td>Coffee is hot!</td></tr></tbody>
>        </table>
>
> will do, *and* be more readable?

The problem with this is when you have a line that looks like this:

for (u16Index=0;u16Index<GLOBAL_u8NumDataPoints;u16Index++)
    {DeviceManager_au8DeviceDetails[u16Index] = GLOBAL_u8DeviceInitialised;}

If you try to fit that all one line you end up with a 132 character line.

Al

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Changing the C Indent style

A. S. Budden
In reply to this post by Ben Schmidt

2009/3/12 Ben Schmidt <[hidden email]>:

>
>> Yes, but it's clearer (to me) than
>>
>> for (;;)
>> {CallFunction();}
>
> It looks like you resort to:
>
> func! MyCindent(l)
>   let extra = match(getline(prevnonblank(a:l-1)),'^\s*{.*}$')==-1?0:-&sw
>   let extra += match(getline(a:l),'^\s*{.*}$')==-1?0:&sw
>   return cindent(a:l)+extra
> endfunc
> :let &l:indentexpr='MyCindent(v:lnum)'
> :setlocal cinkeys+=}
>
> Ben.

This looks really good, thank you!  Is there any way to make it indent
when the '}' is typed?  I thought that this should happen with

:setlocal cinkeys+=}

but it doesn't seem to happen.  If I hit gg=G, the indentation is correct.

My cinkeys is:

0{,0},0),:,0#,!^F,o,O,e,}

Many thanks for the help,

Al

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Changing the C Indent style

Ben Schmidt

A. S. Budden wrote:

> 2009/3/12 Ben Schmidt <[hidden email]>:
>>> Yes, but it's clearer (to me) than
>>>
>>> for (;;)
>>> {CallFunction();}
>> It looks like you resort to:
>>
>> func! MyCindent(l)
>>   let extra = match(getline(prevnonblank(a:l-1)),'^\s*{.*}$')==-1?0:-&sw
>>   let extra += match(getline(a:l),'^\s*{.*}$')==-1?0:&sw
>>   return cindent(a:l)+extra
>> endfunc
>> :let &l:indentexpr='MyCindent(v:lnum)'
>> :setlocal cinkeys+=}
>>
>> Ben.
>
> This looks really good, thank you!  Is there any way to make it indent
> when the '}' is typed?  I thought that this should happen with
>
> :setlocal cinkeys+=}

Yeah, that was my intention...But that would work for cindent, not
indentexpr. :-) So we really want

:setlocal indentkeys+=}

or perhaps

:let &l:indentkeys = &l:cinkeys

is better, as indentkeys by default may not have the other stuff we want
for C-indenting in it.

Ben.




--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Changing the C Indent style

Ben Schmidt

>>> func! MyCindent(l)
>>>   let extra = match(getline(prevnonblank(a:l-1)),'^\s*{.*}$')==-1?0:-&sw
>>>   let extra += match(getline(a:l),'^\s*{.*}$')==-1?0:&sw
>>>   return cindent(a:l)+extra
>>> endfunc
>>> :let &l:indentexpr='MyCindent(v:lnum)'
>>> :setlocal cinkeys+=}

Hmm. It still doesn't seem quite right when used with real code. Perhaps
this is better:

func! MyCindent(l)
   let extra = match(getline(a:l),'^\s*{.*}$')==-1?0:&sw
   " let extra -= match(getline(prevnonblank(a:l-1)),'^\s*{.*}$')==-1?0:&sw
   return cindent(a:l)+extra
endfunc
setlocal indentexpr=MyCindent(v:lnum)
setlocal cinkeys+=}
let &l:indentkeys=&l:cinkeys

Don't know why the unindenting seemed to be necessary before, but it
did. Maybe I'd left out a semicolon or something yielding extra indent
that I didn't want.

Ben.




--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Changing the C Indent style

A. S. Budden

2009/3/12 Ben Schmidt <[hidden email]>:

>
>>>> func! MyCindent(l)
>>>>   let extra = match(getline(prevnonblank(a:l-1)),'^\s*{.*}$')==-1?0:-&sw
>>>>   let extra += match(getline(a:l),'^\s*{.*}$')==-1?0:&sw
>>>>   return cindent(a:l)+extra
>>>> endfunc
>>>> :let &l:indentexpr='MyCindent(v:lnum)'
>>>> :setlocal cinkeys+=}
>
> Hmm. It still doesn't seem quite right when used with real code. Perhaps
> this is better:
>
> func! MyCindent(l)
>   let extra = match(getline(a:l),'^\s*{.*}$')==-1?0:&sw
>   " let extra -= match(getline(prevnonblank(a:l-1)),'^\s*{.*}$')==-1?0:&sw
>   return cindent(a:l)+extra
> endfunc
> setlocal indentexpr=MyCindent(v:lnum)
> setlocal cinkeys+=}
> let &l:indentkeys=&l:cinkeys

This looks fantastic, thank you so much!

Al

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

RE: Changing the C Indent style

Gene Kwiecinski
In reply to this post by A. S. Budden

>>Might be academic at this point, but for a single *statement* (not line)
>>following a for(), if(), while(), etc., enclosing braces are *not*
>>required.  So

>I realise this, but all C software that I write must be compliant with
>the MISRA-C guidelines for the use of the C language in critical

Aha.  Yeah, I thought MISRA was a good idea, wanted to get my hands on the standard, only they want to charge a small fortune for 'em, at least way back when I looked (dunno about now).


>The problem with this is when you have a line that looks like this:
>for (u16Index=0;u16Index<GLOBAL_u8NumDataPoints;u16Index++)
>    {DeviceManager_au8DeviceDetails[u16Index] = GLOBAL_u8DeviceInitialised;}
>If you try to fit that all one line you end up with a 132 character line.

True enough, which is why I'd skip on requiring the braces around the statement being on the same line.

I used to wrap for, eg, 80chars/line, but eventually said screw it, let it flow as needed, especially if using ts=8.  If it'd get too cumbersome, I'd break it up by functional group, eg, in javascript something like

        foo = t.function1()
                   .function2()
                   .function3()
                + " "
                + s.function4()
                   .function5();

where technically the first term (concatenation of functions operating on the base string, t) "should" be written as

        t.function1().function2().function3()

to show the sequence of operations, or for something more C-ish

        sum = ( byte3 << 24 )
            + ( byte2 << 16 )
            + ( byte1 <<  8 )
            + ( byte0       );

to make it more aesthetically appealing and visually "sensible".  Nb the extra whitespace after 'byte0' when there's no left-shift, to keep the ')'s aligned, as well as aligning the [=+*/-] and similar operators.

I do crap like that all the time, which is why I generally find autoindenting counterproductive.  <shrug/>

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---