Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1390465
  • 博文数量: 1334
  • 博客积分: 645
  • 博客等级: 上士
  • 技术积分: 5762
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-25 16:56
文章分类

全部博文(1334)

文章存档

2014年(108)

2013年(1059)

2012年(169)

分类: Python/Ruby

2013-06-05 14:34:51

indent是linux下一个能力极强的代码整理软件。

/linux-2.6/scripts/Lindent 文件 ,可以看到一行代码:

indent -npro -kr -i8 -ts8 -sob -l80 -ss -ncs

这一行就是linux内核使用indent整理代码的格式,使用这条命令就可以实现风格十分良好的C或C++代码

其中-l80是每一行最多80个字母,超出会拆行,如果不喜欢可以使用更长的行字数

 

功能说明:调整C原始代码文件的格式。
语  法:indent [参数][源文件] 或 indent [参数][源文件][-o 目标文件]
补充说明:indent可辨识C的原始代码文件,并加以格式化。
--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来缩进 

  1. #!/bin/bash
  2. #fileformat.sh
  3. ######################################################################################
  4. #作用:把当前目录下的所有.c,.h文件统一按indent的对齐风格进行代码格式化
  5. #方法: 到指定目录下执行~/.fileformat.sh
  6. #######################################################################################

  7. THIS_CMD=$0
  8. COMMAND=$1

  9. USRPATH=$PWD

  10. echo "THIS_CMD" $THIS_CMD
  11. echo "COMMAND" $COMMAND

  12. #循环的到每个目录下面去把每一个文件的^M删掉

  13. if [ "$1" = "help" ]; then
  14.     echo reccurrun COMMAND
  15.     echo " Navigate recursivly the current directory and run COMMAND on"
  16.     echo " all the files(not directory)."
  17.     exit 0
  18. fi

  19. FILES=`ls`
  20. for file in $FILES;
  21. do
  22.     if [ "$file" = "." ] || [ "$file" = ".." ]; then
  23.         continue
  24.     fi
  25.     if [ -d "$file" ]; then
  26.         TopDir=`pwd`
  27.         cd $file
  28.         #$THIS_CMD $COMMAND
  29.    ls -la |xargs perl -p -i -e "s/[\015]//;"
  30.         cd $TopDir
  31.     fi
  32.     if [ -f "$file" ]; then
  33.         #$COMMAND $file
  34.    perl -p -i -e "s/[\015]//;" $file
  35.     fi
  36. done

  37. #exit

  38. #find $USRPATH -name "*.c" | xargs indent
  39. #find $USRPATH -name "*.h" | xargs indent
  40. find $PWD -name "*.c" |xargs indent -bad -bap -bbb -bbo -nbc -bl -bli0 -bls -c33 -cd33 -ncdb -ncdw -nce -cli0 -cp33 -cs -d0 \
  41. -nbfda -di2 -nfc1 -nfca -hnl -ip5 -l90 -lp -pcs -nprs -psl -saf -sai -saw -nsc -nsob -nss -i4 -ts4 -ut
  42. find $PWD -name "*.h" |xargs indent -bad -bap -bbb -bbo -nbc -bl -bli0 -bls -c33 -cd33 -ncdb -ncdw -nce -cli0 -cp33 -cs -d0 \
  43. -nbfda -di2 -nfc1 -nfca -hnl -ip5 -l90 -lp -pcs -nprs -psl -saf -sai -saw -nsc -nsob -nss -i4 -ts4 -ut

阅读(189) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~