资源描述
用 indent 进行代码格式化
1. introduce indent
2. Indent参数规范
一个范例,胜过前言万语。用 indent 格式化代码,再从格式化前后的格式变化,来学习格式化规范。
可以用不同的参数调用 indent,
1. introduce indent
1. What is Indent
The `indent' program can be used to make code easier to read. It can also convert from one style of writing C to another.
2. Download
Current Version: GNU indent 2.2.7。 Download : ftp.gnu.org/gnu/indent/indent-2.2.7.tar.gz
2. Indent参数规范
我们参照GNU,Kernighan & Ritchie,Berkeley风格,制定了自己风格:
1. indent命令参数:
-bad -bap -bbb -bbo -nbc -bl -bli0 -bls -c33 -cd33 -ncdb -ncdw -nce -cli0 -cp33 -cs -d0 -nbfda -di2 -nfc1 -nfca -hnl -ip5 -l75 -lp -pcs -nprs -psl -saf -sai -saw -nsc -nsob -nss -i4 -ts4 -ut
2. indent配置文件
如上参数可写入用户目录下的文件:".indent.pro",作为运行indent的确省参数。
3. indent配置说明
表 1. 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详细参数以及各种编程排版风格见:
A. 不同编程风格参考
通过indent参数分析,比较自定义风格和GNU,KR,BSD编程风格。
表 A.1. 自定义风格和GNU,KR,BSD风格比较
参数
含义
我们的风格
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.
展开阅读全文