如何使用 indent 美化你的代码

代码也需要风格(各种 C 代码风格),经常纠结自己的代码排版不美观,作为一名十分“固执”的程序员,自己在写 C 代码的时候甚至连 #include 头文件顺序都要按拼音序排列,有时候花了很多时间手工检查自己的代码排版风格,虽然是件费力讨好的事,但是宝贵的时间浪费掉了。

最近试用了 indent,发现对代码风格的控制还是挺好的,Linux 内核里面就有一个 indent 脚本(script/Lindent),在 3.2 的内核中 Lindent 如下:

#!/bin/sh
PARAM="-npro -kr -i8 -ts8 -sob -l80 -ss -ncs -cp1"
RES=`indent --version`
V1=`echo $RES | cut -d' ' -f3 | cut -d'.' -f1`
V2=`echo $RES | cut -d' ' -f3 | cut -d'.' -f2`
V3=`echo $RES | cut -d' ' -f3 | cut -d'.' -f3`
if [ $V1 -gt 2 ]; then
  PARAM="$PARAM -il0"
elif [ $V1 -eq 2 ]; then
  if [ $V2 -gt 2 ]; then
    PARAM="$PARAM -il0";
  elif [ $V2 -eq 2 ]; then
    if [ $V3 -ge 10 ]; then
      PARAM="$PARAM -il0"
    fi
  fi
fi
indent $PARAM "$@"

下面是 indent 各个参数的说明(参看):

使用的indent参数含义
--blank-lines-after-declarationsbad变量声明后加空行
--blank-lines-after-proceduresbap函数结束后加空行
--blank-lines-before-block-commentsbbb块注释前加空行
--break-before-boolean-operatorbbo较长的行,在逻辑运算符前分行
--blank-lines-after-commasnbc变量声明中,逗号分隔的变量不分行
--braces-after-if-linebl"if"和"{"分做两行
--brace-indent 0bli0"{"不继续缩进
--braces-after-struct-decl-linebls定义结构,"struct"和"{"分行
--comment-indentationnc33语句后注释开始于行33
--declaration-comment-columnncd33变量声明后注释开始于行33
--comment-delimiters-on-blank-linesncdb不将单行注释变为块注释
--cuddle-do-whilencdw"do --- while"的"while"和其前面的"}"另起一行
--cuddle-elsence"else"和其前面的"}"另起一行
--case-indentation 0cli0switch中的case语句所进0个空格
--else-endif-columnncp33#else, #endif后面的注释开始于行33
--space-after-castcs在类型转换后面加空格
--line-comments-indentation nd0单行注释(不从1列开始的),不向左缩进
--break-function-decl-argsnbfda关闭:函数的参数一个一行
--declaration-indentationndi2变量声明,变量开始于2行,即不必对齐
--format-first-column-commentsnfc1不格式化起于第一行的注释
--format-all-commentsnfca不开启全部格式化注释的开关
--honour-newlineshnlPrefer to break long lines at the position of newlines in the input.
--indent-levelni4设置缩进多少字符,如果为tab的整数倍,用tab来缩进,否则用空格填充。
--parameter-indentationnip5旧风格的函数定义中参数说明缩进5个空格
--line-length 75l75非注释行最长75
--continue-at-parentheseslp续行从上一行出现的括号开始
--space-after-procedure-callspcs函数和"("之间插入一个空格
--space-after-parenthesesnprs在"("后")"前不插入空格
--procnames-start-linespsl将函数名和返回类型放在两行定义
--space-after-forsaffor后面有空格
--space-after-ifsaiif后面有空格
--space-after-whilesawwhile后面有空格
--start-left-side-of-commentsnsc不在生成的块注释中加*
--swallow-optional-blank-linesnsob不去掉可添加的空行
--space-special-semicolonnss一行的for或while语句,在";"前不加空。
--tab-sizets4一个tab为4个空格(要能整除"-in")
--use-tabsut使用tab来缩进

常见风格 indent 参数设置(参看):

参数含义自定义风格GNU风格KR风格BSD风格
-bad--blank-lines-after-declarationsynnn
-bap--blank-lines-after-proceduresyyyn
-bbb--blank-lines-before-block-commentsy   
-bbo--break-before-boolean-operatoryyyy
-bc--blank-lines-after-commasnnny
-bl--braces-after-if-lineyy  
-blin--brace-indent n02  
-bls--braces-after-struct-decl-lineyy  
-br--braces-on-if-line  yy
-brs--braces-on-struct-decl-line  yy
-bs--blank-before-sizeof    
-cn--comment-indentationn33 3333
-cbin--case-brace-indentationn    
-cdn--declaration-comment-columnn33 3333
-cdb--comment-delimiters-on-blank-linesnnny
-cdw--cuddle-do-while    
-ce--cuddle-elsennny
-cin--continuation-indentationn  44
-clin--case-indentationn0 00
-cpn--else-endif-columnn3313333
-cs--space-after-castyyy 
-dn--line-comments-indentationn0 0 
-ndjindents declarations the same as code y  
-bfda--break-function-decl-argsn   
-din--declaration-indentationn22116
-fc1--format-first-column-commentsnnny
-fca--format-all-commentsnnny
-gnu--gnu-style    
-hnl--honour-newlinesyyyy
-in--indent-leveln4244
-ipn--parameter-indentationn5504
-kr--k-and-r-style    
-ln--line-lengthn75 7575
-cs--space-after-cast    
-dn--line-comments-indentationn    
-bfda--break-function-decl-args    
-din--declaration-indentationn    
-fc1--format-first-column-comments    
-fca--format-all-comments    
-gnu--gnu-style    
-hnl--honour-newlines    
-in--indent-leveln    
-ipn--parameter-indentationn    
-kr--k-and-r-style    
-ln--line-lengthn    
-lcn--comment-line-lengthn    
-lp--continue-at-parenthesesy yy
-lps--leave-preprocessor-space    
-orig--original    
-npro--ignore-profile    
-pcs--space-after-procedure-callsyynn
-pin--paren-indentationn    
-pmt--preserve-mtime    
-prs--space-after-parenthesesnnnn
-psl--procnames-start-linesyyny
-saf--space-after-foryyyy
-sai--space-after-ifyyyy
-saw--space-after-whileyyyy
-sbin--struct-brace-indentationn    
-sc--start-left-side-of-commentsnnny
-sob--swallow-optional-blank-linesnnnn
-ss--space-special-semicolonn nn
-st--standard-output    
-T typenamesTell indent the name of typenames.    
-tsn--tab-sizen4  8
-ut--use-tabsy   
-v--verbose    
-versionOutput the version number of indent.  

参考:

C Style: Standards and Guidelines:http://syque.com/cstyle/index.htm

GNU Coding Standards:http://www.gnu.org/prep/standards/standards.html

几种常见风格配置:http://www.gnu.org/software/indent/manual/html_section/indent_4.html#SEC4

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值