PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/plugin/authorinfo.vim

https://github.com/Alan0521/vimfiles
Vim Script | 177 lines | 154 code | 7 blank | 16 comment | 26 complexity | ee2404a764a7f710d9a237f45f422d21 MD5 | raw file
  1. "=============================================================================
  2. " Author: dantezhu - http://www.vimer.cn
  3. " Email: zny2008@gmail.com
  4. " FileName: authorinfo.vim
  5. " Description:
  6. " Version: 1.5
  7. " LastChange: 2011-02-23 16:42:42
  8. " History: support bash's #!xxx
  9. " fix bug for NerdComment's <leader>
  10. "=============================================================================
  11. if exists('g:loaded_authorinfo')
  12. finish
  13. endif
  14. let g:loaded_authorinfo= 1
  15. if exists("mapleader")
  16. let s:t_mapleader = mapleader
  17. elseif exists("g:mapleader")
  18. let s:t_mapleader = g:mapleader
  19. else
  20. let s:t_mapleader = '\'
  21. endif
  22. function! g:CheckFileType(type)
  23. let t_filetypes = split(&filetype,'\.')
  24. if index(t_filetypes,a:type)>=0
  25. return 1
  26. else
  27. return 0
  28. endif
  29. endfunction
  30. function s:DetectFirstLine()
  31. "跳转到指定区域的第一行,开始操作
  32. exe 'normal '.1.'G'
  33. let arrData = [
  34. \['sh',['^#!.*$']],
  35. \['python',['^#!.*$','^#.*coding:.*$']],
  36. \['php',['^<?.*']]
  37. \]
  38. let oldNum = line('.')
  39. while 1
  40. let line = getline('.')
  41. let findMatch = 0
  42. for [t,v] in arrData
  43. if g:CheckFileType(t)
  44. for it in v
  45. if line =~ it
  46. let findMatch = 1
  47. break
  48. endif
  49. endfor
  50. endif
  51. endfor
  52. if findMatch != 1
  53. break
  54. endif
  55. normal j
  56. "到了最后一行了,所以直接o就可以了
  57. if oldNum == line('.')
  58. normal o
  59. return
  60. endif
  61. let oldNum = line('.')
  62. endwhile
  63. normal O
  64. endfunction
  65. function s:BeforeTitle()
  66. let arrData = [['python',"'''"]]
  67. for [t,v] in arrData
  68. if g:CheckFileType(t)
  69. call setline('.',v)
  70. normal o
  71. break
  72. endif
  73. endfor
  74. endfunction
  75. function s:AfterTitle()
  76. let arrData = [['python',"'''"]]
  77. for [t,v] in arrData
  78. if g:CheckFileType(t)
  79. normal o
  80. call setline('.',v)
  81. normal k
  82. break
  83. endif
  84. endfor
  85. endfunction
  86. function s:AddTitle()
  87. "检查开始插入作者信息的行
  88. call s:DetectFirstLine()
  89. "判断是否支持多行注释
  90. let hasMul = 0
  91. let preChar = ''
  92. let noTypeChar = ''
  93. call setline('.','test mul')
  94. let oldline = getline('.')
  95. exec 'normal '.s:t_mapleader.'cm'
  96. let newline = getline('.')
  97. if oldline != newline
  98. let hasMul = 1
  99. let preChar = '#'
  100. else
  101. exec 'normal '.s:t_mapleader.'cl'
  102. let newline = getline('.')
  103. if oldline == newline
  104. let hasMul = -1
  105. let noTypeChar = '#'
  106. endif
  107. endif
  108. "在第一行之前做的事情
  109. call s:BeforeTitle()
  110. let firstLine = line('.')
  111. call setline('.',noTypeChar.'=============================================================================')
  112. normal o
  113. call setline('.',noTypeChar.preChar.' FileName: '.expand("%:t"))
  114. normal o
  115. call setline('.',noTypeChar.preChar.' Desc: ')
  116. let gotoLn = line('.')
  117. normal o
  118. call setline('.',noTypeChar.preChar.' Author: '.g:vimrc_author)
  119. normal o
  120. call setline('.',noTypeChar.preChar.' Email: '.g:vimrc_email)
  121. normal o
  122. call setline('.',noTypeChar.preChar.' HomePage: '.g:vimrc_homepage)
  123. normal o
  124. call setline('.',noTypeChar.preChar.' Version: 0.0.1')
  125. normal o
  126. call setline('.',noTypeChar.preChar.' LastChange: '.strftime("%Y-%m-%d %H:%M:%S"))
  127. normal o
  128. call setline('.',noTypeChar.preChar.' History:')
  129. normal o
  130. call setline('.',noTypeChar.'=============================================================================')
  131. let lastLine = line('.')
  132. "在最后一行之后做的事情
  133. call s:AfterTitle()
  134. if hasMul == 1
  135. exe 'normal '.firstLine.'Gv'.lastLine.'G'.s:t_mapleader.'cm'
  136. else
  137. exe 'normal '.firstLine.'Gv'.lastLine.'G'.s:t_mapleader.'cl'
  138. endif
  139. exe 'normal '.gotoLn.'G'
  140. startinsert!
  141. echohl WarningMsg | echo "Succ to add the copyright." | echohl None
  142. endf
  143. function s:TitleDet()
  144. silent! normal ms
  145. let updated = 0
  146. let n = 1
  147. "默认为添加
  148. while n < 20
  149. let line = getline(n)
  150. if line =~ '^.*FileName:\S*.*$'
  151. let newline=substitute(line,':\(\s*\)\(\S.*$\)$',':\1'.expand("%:t"),'g')
  152. call setline(n,newline)
  153. let updated = 1
  154. endif
  155. if line =~ '^.*LastChange:\S*.*$'
  156. let newline=substitute(line,':\(\s*\)\(\S.*$\)$',':\1'.strftime("%Y-%m-%d %H:%M:%S"),'g')
  157. call setline(n,newline)
  158. let updated = 1
  159. endif
  160. let n = n + 1
  161. endwhile
  162. if updated == 1
  163. silent! normal 's
  164. echohl WarningMsg | echo "Succ to update the copyright." | echohl None
  165. return
  166. endif
  167. call s:AddTitle()
  168. endfunction
  169. command! -nargs=0 AuthorInfoDetect :call s:TitleDet()