开发者

Seeking a C Beautifier that will insert spaces between line elements [closed]

开发者 https://www.devze.com 2022-12-09 02:21 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 7 years ago.

Improve this question

I like spaces between almost all semantic elements in my C code.

Thus I prefer

if ( ( foo = bar ( arg1, arg2, arg3 ) 开发者_运维知识库) ==  NULL ) {
    printf ( "Error 42" );
}

to

if((foo=bar(arg1,arg2,arg3))==NULL){
    printf("Error 42");
}

Is there a C beautifier around (unix platform) that can do this?

It takes a seriously smart beautifier 'cos it has to leave function macros alone.


indent -prs -br -i 4 file

Turns this:

#define f(x) ((x)+(x))

if((foo=bar(arg1,arg2,arg3))==NULL){
    printf("Error 42");
    f(42);
}

Into this:

#define f(x) ((x)+(x))

if ( ( foo = bar ( arg1, arg2, arg3 ) ) == NULL ) {
    printf ( "Error 42" );
    f ( 42 );
}

The -prs option puts spaces around parenthesis, the spaces around operators and after the commas come standard. The -br option enforces your bracing style, and -i 4 uses 4 spaces to indent. Note that the macro definition is not modified but the call to the function-like macro in the code is (presumably what you'd want).


You may want to look at GNU Indent. I believe it can do everything you're looking for.


Gnu Indent can probably do that. Unfortunately, indent has a huge number of options, many of which aren't at all intuitive, and many of them interact in extremely strange ways. I've never (not even once) managed to get it to format code in a way that wasn't uglier coming out than going in. In some cases it was more uniform. In others I suppose it must really have been uniform, but the rules it was following were still strange enough that the result often just looked wierd.

After struggling with indent for a while, I decided that it was easier to write a much simpler program that only supported one format, and just edit the code if I wanted to change the format.


i had used vim with options cindent and formatoptions to a satisfactory effect. you can indent a whole file by

  gg=G
  gg  -- go to 1st line 
  =G  -- indent upto lastline

you migh want to write a formatexpr (vim7) or you might want to write a custom function with s///g commands and map it to a key.

the follwing will put a space after parantheses except in #define lines

v/#define/ s/[()]/\1 /g

yes, you will be learning regexes :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号