代码也需要风格(各种 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-declarations | bad | 变量声明后加空行 |
| --blank-lines-after-procedures | bap | 函数结束后加空行 |
| --blank-lines-before-block-comments | bbb | 块注释前加空行 |
| --break-before-boolean-operator | bbo | 较长的行,在逻辑运算符前分行 |
| --blank-lines-after-commas | nbc | 变量声明中,逗号分隔的变量不分行 |
| --braces-after-if-line | bl | "if"和"{"分做两行 |
| --brace-indent 0 | bli0 | "{"不继续缩进 |
| --braces-after-struct-decl-line | bls | 定义结构,"struct"和"{"分行 |
| --comment-indentationn | c33 | 语句后注释开始于行33 |
| --declaration-comment-columnn | cd33 | 变量声明后注释开始于行33 |
| --comment-delimiters-on-blank-lines | ncdb | 不将单行注释变为块注释 |
| --cuddle-do-while | ncdw | "do --- while"的"while"和其前面的"}"另起一行 |
| --cuddle-else | nce | "else"和其前面的"}"另起一行 |
| --case-indentation 0 | cli0 | switch中的case语句所进0个空格 |
| --else-endif-columnn | cp33 | #else, #endif后面的注释开始于行33 |
| --space-after-cast | cs | 在类型转换后面加空格 |
| --line-comments-indentation n | d0 | 单行注释(不从1列开始的),不向左缩进 |
| --break-function-decl-args | nbfda | 关闭:函数的参数一个一行 |
| --declaration-indentationn | di2 | 变量声明,变量开始于2行,即不必对齐 |
| --format-first-column-comments | nfc1 | 不格式化起于第一行的注释 |
| --format-all-comments | nfca | 不开启全部格式化注释的开关 |
| --honour-newlines | hnl | Prefer to break long lines at the position of newlines in the input. |
| --indent-leveln | i4 | 设置缩进多少字符,如果为tab的整数倍,用tab来缩进,否则用空格填充。 |
| --parameter-indentationn | ip5 | 旧风格的函数定义中参数说明缩进5个空格 |
| --line-length 75 | l75 | 非注释行最长75 |
| --continue-at-parentheses | lp | 续行从上一行出现的括号开始 |
| --space-after-procedure-calls | pcs | 函数和"("之间插入一个空格 |
| --space-after-parentheses | nprs | 在"("后")"前不插入空格 |
| --procnames-start-lines | psl | 将函数名和返回类型放在两行定义 |
| --space-after-for | saf | for后面有空格 |
| --space-after-if | sai | if后面有空格 |
| --space-after-while | saw | while后面有空格 |
| --start-left-side-of-comments | nsc | 不在生成的块注释中加* |
| --swallow-optional-blank-lines | nsob | 不去掉可添加的空行 |
| --space-special-semicolon | nss | 一行的for或while语句,在";"前不加空。 |
| --tab-size | ts4 | 一个tab为4个空格(要能整除"-in") |
| --use-tabs | ut | 使用tab来缩进 |
常见风格 indent 参数设置(参看):
| 参数 | 含义 | 自定义风格 | GNU风格 | KR风格 | BSD风格 |
|---|---|---|---|---|---|
| -bad | --blank-lines-after-declarations | y | n | n | n |
| -bap | --blank-lines-after-procedures | y | y | y | n |
| -bbb | --blank-lines-before-block-comments | y | |||
| -bbo | --break-before-boolean-operator | y | y | y | y |
| -bc | --blank-lines-after-commas | n | n | n | y |
| -bl | --braces-after-if-line | y | y | ||
| -blin | --brace-indent n | 0 | 2 | ||
| -bls | --braces-after-struct-decl-line | y | y | ||
| -br | --braces-on-if-line | y | y | ||
| -brs | --braces-on-struct-decl-line | y | y | ||
| -bs | --blank-before-sizeof | ||||
| -cn | --comment-indentationn | 33 | 33 | 33 | |
| -cbin | --case-brace-indentationn | ||||
| -cdn | --declaration-comment-columnn | 33 | 33 | 33 | |
| -cdb | --comment-delimiters-on-blank-lines | n | n | n | y |
| -cdw | --cuddle-do-while | ||||
| -ce | --cuddle-else | n | n | n | y |
| -cin | --continuation-indentationn | 4 | 4 | ||
| -clin | --case-indentationn | 0 | 0 | 0 | |
| -cpn | --else-endif-columnn | 33 | 1 | 33 | 33 |
| -cs | --space-after-cast | y | y | y | |
| -dn | --line-comments-indentationn | 0 | 0 | ||
| -ndj | indents declarations the same as code | y | |||
| -bfda | --break-function-decl-args | n | |||
| -din | --declaration-indentationn | 2 | 2 | 1 | 16 |
| -fc1 | --format-first-column-comments | n | n | n | y |
| -fca | --format-all-comments | n | n | n | y |
| -gnu | --gnu-style | ||||
| -hnl | --honour-newlines | y | y | y | y |
| -in | --indent-leveln | 4 | 2 | 4 | 4 |
| -ipn | --parameter-indentationn | 5 | 5 | 0 | 4 |
| -kr | --k-and-r-style | ||||
| -ln | --line-lengthn | 75 | 75 | 75 | |
| -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-parentheses | y | y | y | |
| -lps | --leave-preprocessor-space | ||||
| -orig | --original | ||||
| -npro | --ignore-profile | ||||
| -pcs | --space-after-procedure-calls | y | y | n | n |
| -pin | --paren-indentationn | ||||
| -pmt | --preserve-mtime | ||||
| -prs | --space-after-parentheses | n | n | n | n |
| -psl | --procnames-start-lines | y | y | n | y |
| -saf | --space-after-for | y | y | y | y |
| -sai | --space-after-if | y | y | y | y |
| -saw | --space-after-while | y | y | y | y |
| -sbin | --struct-brace-indentationn | ||||
| -sc | --start-left-side-of-comments | n | n | n | y |
| -sob | --swallow-optional-blank-lines | n | n | n | n |
| -ss | --space-special-semicolon | n | n | n | |
| -st | --standard-output | ||||
| -T typenames | Tell indent the name of typenames. | ||||
| -tsn | --tab-sizen | 4 | 8 | ||
| -ut | --use-tabs | y | |||
| -v | --verbose | ||||
| -version | Output 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