修正“苦练1天半,终于写出了一些常用doxygen风格的vim注释脚本”部分BUG

  1. "一些常用的doxygen风格的注释
  2. "作者:谭金文
  3. "QQ: 26906391
  4. "Email: tanjinwen.8@qq.com
  5. "Copyright (c) 2008 任何人都可以自由获取、修改、发布该程序。
  6. "参考:本脚本参考了大量的网上资料以及VIM中文手册、doxygen user manual
  7. "不足之处:
  8. "   1. 函数注释并不能提取函数的参数列表和返回值。虽然www.vim.org有大量的doxygen风格函数注释脚本,
  9. "但是经过测试我还没有发现有完全工作正确的。提取函数参数列表的难点在于有字符串作为默认参数以及有函数指针
  10. "的参数。虽然已经想到了些方法,但是鉴于时间关系没有去实现。如果有你实现了该功能或者有好的想法,欢迎与
  11. "我进行交流。
  12. "   2. 通过键映射的方式添加注释后为插入模式,但是通过输入自定义命令后是普通模式
  13. "添加只包含一次文件的宏#ifndef ..#define #endif
  14. "使用方法:在普通模式下击键zho
  15. function InsertIncludeOnceTag()
  16.     let CurTime = strftime("%Y_%m_%d_%H_%M")    "将当前时间格式化后存入到CurTime当中去
  17.     let CurFilePath = toupper(expand("%"))      "expand(/"%/")获取当前编辑的文件路径
  18.     let LastSlash = strridx(CurFilePath, "/")   "找出路径中最后一个/
  19.     let HeadTag1 = "#ifndef "
  20.     let HeadTag2 = "#define "
  21.     let HeadTag3 = "#endif //"
  22.     "将非数字、字母替换为_
  23.     let Suffix = substitute(strpart(CurFilePath, LastSlash + 1), "[^a-zA-Z0-9]""_""g")."_".CurTime  
  24.     let HeadTag1 = HeadTag1.Suffix
  25.     let HeadTag2 = HeadTag2.Suffix
  26.     let HeadTag3 = HeadTag3.Suffix
  27.     "跳转到第一行并输入#ifndef ..
  28.     exe "normal ggO".HeadTag1                   
  29.     exe "normal o".HeadTag2
  30.     exe "normal Go".HeadTag3
  31.     "添加一个空白行
  32.     exe "normal o"
  33.     "添加两行空白行并将光标停在第四行
  34.     normal 3GO
  35.     normal 4GO
  36. endfunction
  37. "添加doxygen风格的文件注释
  38. "使用方法:在普通模式下击键zfc
  39. function InsertFileCommentTag()
  40.     let l:File_Path = expand("%")
  41.     let l:File_Name = strpart(l:File_Path, strridx(l:File_Path, "/") + 1)
  42.     exe "normal ggO/**"
  43.     exe "normal o*  @file ".l:File_Name
  44.     exe "normal o*  @brief  "
  45.     let l:wRow = line(".")
  46.     let l:wCol = col(".")
  47.     exe "normal o*  @author jinwentan.8@qq.com"
  48.     exe "normal o*  @version VERSION_PLACEHOLDER"
  49.     exe "normal o*  @date ".strftime("%Y/%m/%d")
  50.     exe "normal o*  @note "
  51.     exe "normal o*  @remarks  Copyright 1998 - 2008 XXX Inc. All Rights Reserved."
  52.     exe "normal o*/"
  53.     "重新缩进文件
  54.     exe "silent normal gg=G"
  55.     "设置光标在@brief后面
  56.     :call cursor(l:wRow, l:wCol)
  57. endfunction
  58. "添加doxygen风格的类注释
  59. "使用方法:在普通模式下,将光标停留在类名上面击键zcc
  60. function InsertClassCommentTag()
  61.     let Class_Name = expand("<cword>")
  62.     exe "normal O/**"
  63.     exe "normal o*  @class ".Class_Name
  64.     exe "normal o*  @brief  "
  65.     let l:wRow = line(".")
  66.     let l:wCol = col(".")
  67.     exe "normal o*  @author jinwentan.8@qq.com"
  68.     exe "normal o*  @version VERSION_PLACEHOLDER"
  69.     exe "normal o*  @date ".strftime("%Y/%m/%d")
  70.     exe "normal o*  @note "
  71.     exe "normal o*/"
  72.     exe "silent normal gg=G"
  73.     :call cursor(l:wRow, l:wCol)
  74. endfunction
  75. "添加doxygen风格的函数注释
  76. "使用方法:在普通模式下面,将光标停留在函数名称行击键zmc
  77. function InsertFunctionCommentTag()
  78.     exe "normal O/**"
  79.     exe "normal o*  @brief  "
  80.     let l:wRow = line(".")
  81.     let l:wCol = col(".")
  82.     exe "normal o*  @param "
  83.     exe "normal o*  @return "
  84.     exe "normal o*  @note "
  85.     exe "normal o*/"
  86.     exe "silent normal gg=G"
  87.     :call cursor(l:wRow, l:wCol)
  88. endfunction
  89. "添加doxygen风格的结构体注释
  90. "使用方法:在普通模式下,将光标停留在结构体名称上面击键zsc
  91. function InsertStructCommentTag()
  92.     let l:Struct_Name = expand("<cword>")
  93.     exe "normal O/**"
  94.     exe "normal o*  @struct ".l:Struct_Name
  95.     exe "normal o*  @brief  "
  96.     let l:wRow = line(".")
  97.     let l:wCol = col(".")
  98.     exe "normal o*  @note "
  99.     exe "normal o*/"
  100.     exe "silent normal gg=G"
  101.     :call cursor(l:wRow, l:wCol)
  102. endfunction
  103. "添加doxygen风格的枚举体注释
  104. "使用方法:在普通模式下,将光标停留在枚举体名称上面击键zec
  105. function InsertEnumCommentTag()
  106.     let l:Enum_Name = expand("<cword>")
  107.     exe "normal O/**"
  108.     exe "normal o*  @enum ".l:Enum_Name
  109.     exe "normal o*  @brief  "
  110.     let l:wRow = line(".")
  111.     let l:wCol = col(".")
  112.     exe "normal o*  @note "
  113.     exe "normal o*/"
  114.     exe "silent normal gg=G"
  115.     :call cursor(l:wRow, l:wCol)
  116. endfunction
  117. "添加doxygen风格的模块定义注释
  118. "使用方法:在visual模式下选定要加入到模块的行,击键zgc
  119. function InsertGroupCommentTag()
  120.     "获取选定的行
  121.     let l:wBegin = line("'<")
  122.     let l:wEnd = line("'>")
  123.     exe "normal ".l:wBegin."G"
  124.     exe "normal O/**"
  125.     exe "normal o*  @defgroup  "
  126.     let l:wRow = line(".")
  127.     let l:wCol = col(".")
  128.     exe "normal o*  @brief "
  129.     exe "normal o*  @detail "
  130.     exe "normal o*  @{"
  131.     exe "normal o  */"
  132.     exe "normal o"
  133.     let l:wEnd = l:wEnd + 7 
  134.     exe "normal ".l:wEnd."G"
  135.     exe "normal o/** @} */ //end define of group" 
  136.     exe "silent normal gg=G"
  137.     :call cursor(l:wRow, l:wCol)
  138. endfunction
  139. "将代码块添加到特定模块的Doxygen注释
  140. "使用方法:在visual模式下选定要加入到模块的行,击键zag
  141. function InsertAddToGroupCommentTag()
  142.     "获取选定的行
  143.     let l:wBegin = line("'<")
  144.     let l:wEnd = line("'>")
  145.     exe "normal ".l:wBegin."G"
  146.     exe "normal O/*! @addtogroup  "
  147.     let l:wRow = line(".")
  148.     let l:wCol = col(".")
  149.     exe "normal o*  @{"
  150.     exe "normal o  */"
  151.     exe "normal o"
  152.     let l:wEnd = l:wEnd + 4 
  153.     exe "normal ".l:wEnd."G"
  154.     exe "normal o/** @} */ //end addtogroup" 
  155.     exe "silent normal gg=G"
  156.     :call cursor(l:wRow, l:wCol)
  157. endfunction
  158. "在符号(宏、成员变量、全局变量)后增加doxygen风格的注释
  159. "使用方法:普通模式下,在符号定义行击键zpc
  160. function InsertItemPostCommentTag()
  161.     exe "normal A/t"
  162.     let l:wRow = line(".")
  163.     let l:wCol = col(".")
  164.     while l:wCol < 50
  165.         exe "normal A    "
  166.         let l:wCol = col(".")
  167.     endwhile
  168.     exe "normal A/*!<  */"
  169.     exe "normal 2h"
  170. endfunction
  171. "在符号(宏、成员变量、全局变量)上方增加doxygen风格的注释或者是函数的简单说明
  172. "使用方法:在普通模式下,在符号定义行击键zuc
  173. function InsertItemUpCommentTag()
  174.     exe "normal O"
  175.     exe "normal o///  "
  176. endfunction
  177. nmap zho <Esc>:call InsertIncludeOnceTag()<CR>i
  178. nmap zfc <Esc>:call InsertFileCommentTag()<CR>i
  179. nmap zcc <Esc>:call InsertClassCommentTag()<CR>i
  180. nmap zmc <Esc>:call InsertFunctionCommentTag()<CR>i
  181. nmap zsc <Esc>:call InsertStructCommentTag()<CR>i
  182. nmap zec <Esc>:call InsertEnumCommentTag()<CR>i
  183. nmap zpc <Esc>:call InsertItemPostCommentTag()<CR>i
  184. nmap zuc <Esc>:call InsertItemUpCommentTag()<CR>i
  185. vmap zgc <Esc>:call InsertGroupCommentTag()<CR>i
  186. vmap zag <Esc>:call InsertAddToGroupCommentTag()<CR>i
  187. command! -nargs=0 DoxHeader     :call InsertIncludeOnceTag() 
  188. command! -nargs=0 DoxFile       :call InsertFileCommentTag()
  189. command! -nargs=0 DoxClass      :call InsertClassCommentTag()
  190. command! -nargs=0 DoxFunction   :call InsertFunctionCommentTag()
  191. command! -nargs=0 DoxStruct     :call InsertStructCommentTag()
  192. command! -nargs=0 DoxEnum       :call InsertEnumCommentTag()
  193. command! -nargs=0 DoxItemPost   :call InsertItemPostCommentTag()
  194. command! -nargs=0 DoxItemUp     :call InsertItemUpCommentTag()
  195. command! -nargs=0 DoxDefGroup   :call InsertGroupCommentTag()
  196. command! -nargs=0 DoxAddToGroup :call InsertAddToGroupCommentTag()
  197. "在.h/.cpp/.c间跳转
  198. function SwitchImpDeclare()
  199.     let l:File_Name = expand("%")
  200.     let l:LastDot = strridx(l:File_Name, ".")
  201.     let l:Suffix = strpart(l:File_Name, l:LastDot  + 1)
  202.     let l:Base = strpart(l:File_Name, 0, l:LastDot)
  203.     if l:Suffix == "cpp" || l:Suffix == "c"
  204.         if bufexists(l:Base.".h")
  205.             exe ":b ".l:Base.".h"
  206.         elseif filereadable(l:Base.".h")
  207.             exe ":e ".l:Base.".h"
  208.         else
  209.             echo "Can't edit file: ".l:Base.".h"
  210.         endif
  211.     elseif l:Suffix == "h"  
  212.         if bufexists(l:Base.".cpp")
  213.             exe ":b ".l:Base.".cpp"
  214.         elseif bufexists(l:Base.".c")
  215.             exe ":b ".l:Base.".c"
  216.         elseif filereadable(l:Base.".cpp")
  217.             exe ":e ".l:Base.".cpp"
  218.         elseif filereadable(l:Base.".c")
  219.             exe ":e ".l:Base.".c"
  220.         else
  221.             echo "Can't edit file: ".l:Base.".cpp or "l:Base.".c"
  222.         endif
  223.     endif
  224. endfunction
  225. function IncludeDeclare()
  226.     let l:File_Name = expand("%")
  227.     let l:LastDot = strridx(l:File_Name, ".")
  228.     let l:Suffix = strpart(l:File_Name, l:LastDot  + 1)
  229.     let l:Base = strpart(l:File_Name, 0, l:LastDot)
  230.     if l:Suffix == "cpp" || l:Suffix == "c"
  231.         exe "normal o#include /"".strpart(l:Base, strridx(l:Base, "/") + 1).".h/""
  232.     elseif l:Suffix == "h"  
  233.         echo "Current file is not a c/c++ implement file"
  234.     endif
  235. endfunction
  236. command! -nargs=0   BS  :call SwitchImpDeclare()
  237. command! -nargs=0   IH  :call IncludeDeclare()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值