indent的使用

用 indent 进行代码格式化 3.5. 用 indent 进行代码格式化3.5.1. introduce indent 
3.5.2. Indent参数规范 
一个范例,胜过前言万语。用 indent 格式化代码,再从格式化前后的格式变化,来学习格式化规范。 

可以用不同的参数调用 indent, 

3.5.1. introduce indentWhat 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. 

Download 

Current Version: GNU indent 2.2.7。 Download : ftp.gnu.org/gnu/indent/indent-2.2.7.tar.gz 
3.5.2. Indent参数规范
我们参照GNU,Kernighan & Ritchie,Berkeley风格,制定了自己风格: 


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 

indent配置文件 

如上参数可写入用户目录下的文件:".indent.pro",作为运行indent的确省参数。 

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.


功能说明:调整C原始代码文件的格式。

语  法:indent [参数][源文件] 或 indent [参数][源文件][-o 目标文件]

补充说明:indent可辨识C的原始代码文件,并加以格式化,以方便程序设计师阅读。

参  数:
 -bad或--blank-lines-after-declarations  在声明区段或加上空白行。
 -bap或--blank-lines-after-procedures  在程序或加上空白行。
 -bbb或--blank-lines-after-block-comments  在注释区段后加上空白行。
 -bc或--blank-lines-after-commas  在声明区段中,若出现逗号即换行。
 -bl或--braces-after-if-line  if(或是else,for等等)与后面执行区段的"{"不同行,且"}"自成一行。
 -bli<缩排格数>或--brace-indent<缩排格数>  设置{ }缩排的格数。
 -br或--braces-on-if-line  if(或是else,for等等)与后面执行跛段的"{"不同行,且"}"自成一行。
 -bs或--blank-before-sizeof  在sizeof之后空一格。
 -c<栏数>或--comment-indentation<栏数>  将注释置于程序码右侧指定的栏位。
 -cd<栏数>或--declaration-comment-column<栏数>  将注释置于声明右侧指定的栏位。
 -cdb或--comment-delimiters-on-blank-lines  注释符号自成一行。
 -ce或--cuddle-else  将else置于"}"(if执行区段的结尾)之后。
 -ci<缩排格数>或--continuation-indentation<缩排格数>  叙述过长而换行时,指定换行后缩排的格数。
 -cli<缩排格数>或--case-indentation-<缩排格数>  使用case时,switch缩排的格数。
 -cp<栏数>或-else-endif-column<栏数>  将注释置于else与elseif叙述右侧定的栏位。
 -cs或--space-after-cast  在cast之后空一格。
 -d<缩排格数>或-line-comments-indentation<缩排格数>  针对不是放在程序码右侧的注释,设置其缩排格数。
 -di<栏数>或--declaration-indentation<栏数>  将声明区段的变量置于指定的栏位。
 -fc1或--format-first-column-comments  针对放在每行最前端的注释,设置其格式。
 -fca或--format-all-comments  设置所有注释的格式。
 -gnu或--gnu-style  指定使用GNU的格式,此为预设值。
 -i<格数>或--indent-level<格数>  设置缩排的格数。
 -ip<格数>或--parameter-indentation<格数>  设置参数的缩排格数。
 -kr或--k-and-r-style  指定使用Kernighan&Ritchie的格式。
 -lp或--continue-at-parentheses  叙述过长而换行,且叙述中包含了括弧时,将括弧中的每行起始栏位内容垂直对其排列。
 -nbad或--no-blank-lines-after-declarations  在声明区段后不要加上空白行。
 -nbap或--no-blank-lines-after-procedures  在程序后不要加上空白行。
 -nbbb或--no-blank-lines-after-block-comments  在注释区段后不要加上空白行。
 -nbc或--no-blank-lines-after-commas  在声明区段中,即使出现逗号,仍旧不要换行。
 -ncdb或--no-comment-delimiters-on-blank-lines  注释符号不要自成一行。
 -nce或--dont-cuddle-else  不要将else置于"}"之后。
 -ncs或--no-space-after-casts  不要在cast之后空一格。
 -nfc1或--dont-format-first-column-comments  不要格式化放在每行最前端的注释。
 -nfca或--dont-format-comments  不要格式化任何的注释。
 -nip或--no-parameter-indentation  参数不要缩排。
 -nlp或--dont-line-up-parentheses  叙述过长而换行,且叙述中包含了括弧时,不用将括弧中的每行起始栏位垂直对其排列。
 -npcs或--no-space-after-function-call-names  在调用的函数名称之后,不要加上空格。
 -npro或--ignore-profile  不要读取indent的配置文件.indent.pro。
 -npsl或--dont-break-procedure-type  程序类型与程序名称放在同一行。
 -nsc或--dont-star-comments  注解左侧不要加上星号(*)。
 -nsob或--leave-optional-semicolon  不用处理多余的空白行。
 -nss或--dont-space-special-semicolon  若for或while区段仅有一行时,在分号前不加上空格。
 -nv或--no-verbosity  不显示详细的信息。
 -orig或--original  使用Berkeley的格式。
 -pcs或--space-after-procedure-calls  在调用的函数名称与"{"之间加上空格。
 -psl或--procnames-start-lines  程序类型置于程序名称的前一行。
 -sc或--start-left-side-of-comments  在每行注释左侧加上星号(*)。
 -sob或--swallow-optional-blank-lines  删除多余的空白行。
 -ss或--space-special-semicolon  若for或swile区段今有一行时,在分号前加上空格。
 -st或--standard-output  将结果显示在标准输出设备。
 -T  数据类型名称缩排。
 -ts<格数>或--tab-size<格数>  设置tab的长度。
 -v或--verbose  执行时显示详细的信息。
 -version  显示版本信息。 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值