苦练1天半,终于写出了一些常用doxygen风格的vim注释脚本

来源:互联网 发布:node.js 与spring 编辑:程序博客网 时间:2024/06/18 02:39

 脚本内容如下,欢迎大家补充

  1. "一些常用的doxygen风格的注释
  2. "作者:peacon
  3. "Email:copy2china@gmail.com
  4. "Copyright (c) 2008 任何人都可以自由获取、修改、发布该程序。
  5. "参考:本脚本参考了大量的网上资料以及VIM中文手册、doxygen user manual
  6. "不足之处:函数注释并不能提取函数的参数列表和返回值。虽然www.vim.org有大量的doxygen风格函数注释脚本,
  7. "但是经过测试我还没有发现有完全工作正确的。提取函数参数列表的难点在于有字符串作为默认参数以及有函数指针
  8. "的参数。虽然已经想到了些方法,但是鉴于时间关系没有去实现。如果有你实现了该功能或者有好的想法,欢迎与
  9. "我进行交流。

  10. "添加只包含一次文件的宏#ifndef ..#define #endif
  11. "使用方法:在普通模式下击键zho
  12. function InsertPragmaOnceTag()
  13.     let CurTime = strftime("%Y_%m_%d_%H_%M")    "将当前时间格式化后存入到CurTime当中去
  14.     let CurFilePath = toupper(expand("%"))      "expand(/"%/")获取当前编辑的文件路径
  15.     let LastSlash = strridx(CurFilePath, "/")   "找出路径中最后一个/
  16.     let HeadTag1 = "#ifndef "
  17.     let HeadTag2 = "#define "
  18.     let HeadTag3 = "#endif //"
  19.     "将非数字、字母替换为_
  20.     let Suffix = substitute(strpart(CurFilePath, LastSlash + 1), "[^a-zA-Z0-9]""_""g")."_".CurTime  
  21.     let HeadTag1 = HeadTag1.Suffix
  22.     let HeadTag2 = HeadTag2.Suffix
  23.     let HeadTag3 = HeadTag3.Suffix
  24.     
  25.     "跳转到第一行并输入#ifndef ..
  26.     exe "normal ggO".HeadTag1                   
  27.     exe "normal O".HeadTag2
  28.     exe "normal Go".HeadTag3
  29.     "添加一个空白行
  30.     exe "normal o"
  31.     "添加两行空白行并将光标停在第四行
  32.     normal 3GO
  33.     normal 4GO
  34. endfunction

  35. "添加doxygen风格的文件注释
  36. "使用方法:在普通模式下击键zfc
  37. function InsertFileCommentTag()
  38.     let l:File_Path = expand("%")
  39.     let l:File_Name = strpart(l:File_Path, strridx(l:File_Path, "/") + 1)
  40.     exe "normal ggO/**"
  41.     exe "normal o*  @file ".l:File_Name
  42.     exe "normal o*  @brief  "
  43.     let l:wRow = winline()
  44.     let l:wCol = wincol()
  45.     exe "normal o*  @author tanjinwen.8@qq.com"
  46.     exe "normal o*  @version VERSION_PLACEHOLDER"
  47.     exe "normal o*  @date ".strftime("%Y/%m/%d")
  48.     exe "normal o*  @note "
  49.     exe "normal o*  @remarks  Copyright 1998 - 2008 Company Name Inc. All Rights Reserved."
  50.     exe "normal o*/"
  51.     "重新缩进文件
  52.     exe "silent normal gg=G"
  53.     "设置光标在@brief后面
  54.     :call cursor(l:wRow, l:wCol)
  55. endfunction

  56. "添加doxygen风格的类注释
  57. "使用方法:在普通模式下,将光标停留在类名上面击键zcc
  58. function InsertClassCommentTag()
  59.     let Class_Name = expand("<cword>")
  60.     exe "normal O/**"
  61.     exe "normal o*  @class ".Class_Name
  62.     exe "normal o*  @brief  "
  63.     let l:wRow = winline()
  64.     let l:wCol = wincol()
  65.     exe "normal o*  @author tanjinwen.8@qq.com"
  66.     exe "normal o*  @version VERSION_PLACEHOLDER"
  67.     exe "normal o*  @date ".strftime("%Y/%m/%d")
  68.     exe "normal o*  @note "
  69.     exe "normal o*/"
  70.     exe "silent normal gg=G"
  71.     :call cursor(l:wRow, l:wCol)
  72. endfunction

  73. "添加doxygen风格的函数注释
  74. "使用方法:在普通模式下面,将光标停留在函数名称行击键zmc
  75. function InsertFunctionCommentTag()
  76.     exe "normal O/**"
  77.     exe "normal o*  @brief  "
  78.     let l:wRow = winline()
  79.     let l:wCol = wincol()
  80.     exe "normal o*  @param "
  81.     exe "normal o*  @return "
  82.     exe "normal o*  @note "
  83.     exe "normal o*/"
  84.     exe "silent normal gg=G"
  85.     :call cursor(l:wRow, l:wCol)
  86. endfunction

  87. "添加doxygen风格的结构体注释
  88. "使用方法:在普通模式下,将光标停留在结构体名称上面击键zsc
  89. function InsertStructCommentTag()
  90.     let l:Struct_Name = expand("<cword>")
  91.     exe "normal O/**"
  92.     exe "normal o*  @struct ".l:Struct_Name
  93.     exe "normal o*  @brief  "
  94.     let l:wRow = winline()
  95.     let l:wCol = wincol()
  96.     exe "normal o*  @note "
  97.     exe "normal o*/"
  98.     exe "silent normal gg=G"
  99.     :call cursor(l:wRow, l:wCol)
  100. endfunction

  101. "添加doxygen风格的枚举体注释
  102. "使用方法:在普通模式下,将光标停留在枚举体名称上面击键zec
  103. function InsertEnumCommentTag()
  104.     let l:Enum_Name = expand("<cword>")
  105.     exe "normal O/**"
  106.     exe "normal o*  @enum ".l:Enum_Name
  107.     exe "normal o*  @brief  "
  108.     let l:wRow = winline()
  109.     let l:wCol = wincol()
  110.     exe "normal o*  @note "
  111.     exe "normal o*/"
  112.     exe "silent normal gg=G"
  113.     :call cursor(l:wRow, l:wCol)
  114. endfunction

  115. "添加doxygen风格的模块定义注释
  116. "使用方法:在visual模式下选定要加入到模块的行,击键zgc
  117. function InsertGroupCommentTag()
  118.     "获取选定的行
  119.     let l:wBegin = line("'<")
  120.     let l:wEnd = line("'>")
  121.     exe "normal ".l:wBegin."G"
  122.     exe "normal O/**"
  123.     exe "normal o*  @defgroup  "
  124.     let l:wRow = winline()
  125.     let l:wCol = wincol()
  126.     exe "normal o*  @brief "
  127.     exe "normal o*  @detail "
  128.     exe "normal o*  @{"
  129.     exe "normal o*  */"
  130.     exe "normal o"
  131.     let l:wEnd = l:wEnd + 7 
  132.     exe "normal ".l:wEnd."G"
  133.     exe "normal o/** @} */ //end define of group" 
  134.     :call cursor(l:wRow, l:wCol)
  135. endfunction

  136. "在符号(宏、成员变量、全局变量)后增加doxygen风格的注释
  137. "使用方法:普通模式下,在符号定义行击键zpc
  138. function InsertItemPostCommentTag()
  139.     exe "normal A/t"
  140.     let l:wRow = winline()
  141.     let l:wCol = wincol()
  142.     while l:wCol < 60
  143.         exe "normal A/t"
  144.         let l:wCol = wincol()
  145.     endwhile
  146.     exe "normal A/*!<  */"
  147.     exe "normal 2h"
  148. endfunction

  149. "在符号(宏、成员变量、全局变量)上方增加doxygen风格的注释或者是函数的简单说明
  150. "使用方法:在普通模式下,在符号定义行击键zuc
  151. function InsertItemUpCommentTag()
  152.     exe "normal O"
  153.     exe "normal o///  "
  154. endfunction

  155. nmap zho <Esc>:call InsertPragmaOnceTag()<CR>i
  156. nmap zfc <Esc>:call InsertFileCommentTag()<CR>i
  157. nmap zcc <Esc>:call InsertClassCommentTag()<CR>i
  158. nmap zmc <Esc>:call InsertFunctionCommentTag()<CR>i
  159. nmap zsc <Esc>:call InsertStructCommentTag()<CR>i
  160. nmap zec <Esc>:call InsertEnumCommentTag()<CR>i
  161. nmap zpc <Esc>:call InsertItemPostCommentTag()<CR>i
  162. nmap zuc <Esc>:call InsertItemUpCommentTag()<CR>i
  163. vmap zgc <Esc>:call InsertGroupCommentTag()<CR>i