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  顯示版本信息。 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章