代碼也需要風格(各種 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