snipMate.vim 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. " File: snipMate.vim
  2. " Author: Michael Sanders
  3. " Last Updated: July 13, 2009
  4. " Version: 0.83
  5. " Description: snipMate.vim implements some of TextMate's snippets features in
  6. " Vim. A snippet is a piece of often-typed text that you can
  7. " insert into your document using a trigger word followed by a "<tab>".
  8. "
  9. " For more help see snipMate.txt; you can do this by using:
  10. " :helptags ~/.vim/doc
  11. " :h snipMate.txt
  12. if exists('loaded_snips') || &cp || version < 700
  13. finish
  14. endif
  15. let loaded_snips = 1
  16. if !exists('snips_author') | let snips_author = 'Me' | endif
  17. au BufRead,BufNewFile *.snippets\= set ft=snippet
  18. au FileType snippet setl noet fdm=indent
  19. let s:snippets = {} | let s:multi_snips = {}
  20. if !exists('snippets_dir')
  21. let snippets_dir = substitute(globpath(&rtp, 'snippets/'), "\n", ',', 'g')
  22. endif
  23. fun! MakeSnip(scope, trigger, content, ...)
  24. let multisnip = a:0 && a:1 != ''
  25. let var = multisnip ? 's:multi_snips' : 's:snippets'
  26. if !has_key({var}, a:scope) | let {var}[a:scope] = {} | endif
  27. if !has_key({var}[a:scope], a:trigger)
  28. let {var}[a:scope][a:trigger] = multisnip ? [[a:1, a:content]] : a:content
  29. elseif multisnip | let {var}[a:scope][a:trigger] += [[a:1, a:content]]
  30. else
  31. echom 'Warning in snipMate.vim: Snippet '.a:trigger.' is already defined.'
  32. \ .' See :h multi_snip for help on snippets with multiple matches.'
  33. endif
  34. endf
  35. fun! ExtractSnips(dir, ft)
  36. for path in split(globpath(a:dir, '*'), "\n")
  37. if isdirectory(path)
  38. let pathname = fnamemodify(path, ':t')
  39. for snipFile in split(globpath(path, '*.snippet'), "\n")
  40. call s:ProcessFile(snipFile, a:ft, pathname)
  41. endfor
  42. elseif fnamemodify(path, ':e') == 'snippet'
  43. call s:ProcessFile(path, a:ft)
  44. endif
  45. endfor
  46. endf
  47. " Processes a single-snippet file; optionally add the name of the parent
  48. " directory for a snippet with multiple matches.
  49. fun s:ProcessFile(file, ft, ...)
  50. let keyword = fnamemodify(a:file, ':t:r')
  51. if keyword == '' | return | endif
  52. try
  53. let text = join(readfile(a:file), "\n")
  54. catch /E484/
  55. echom "Error in snipMate.vim: couldn't read file: ".a:file
  56. endtry
  57. return a:0 ? MakeSnip(a:ft, a:1, text, keyword)
  58. \ : MakeSnip(a:ft, keyword, text)
  59. endf
  60. fun! ExtractSnipsFile(file, ft)
  61. if !filereadable(a:file) | return | endif
  62. let text = readfile(a:file)
  63. let inSnip = 0
  64. for line in text + ["\n"]
  65. if inSnip && (line[0] == "\t" || line == '')
  66. let content .= strpart(line, 1)."\n"
  67. continue
  68. elseif inSnip
  69. call MakeSnip(a:ft, trigger, content[:-2], name)
  70. let inSnip = 0
  71. endif
  72. if line[:6] == 'snippet'
  73. let inSnip = 1
  74. let trigger = strpart(line, 8)
  75. let name = ''
  76. let space = stridx(trigger, ' ') + 1
  77. if space " Process multi snip
  78. let name = strpart(trigger, space)
  79. let trigger = strpart(trigger, 0, space - 1)
  80. endif
  81. let content = ''
  82. endif
  83. endfor
  84. endf
  85. fun! ResetSnippets()
  86. let s:snippets = {} | let s:multi_snips = {} | let g:did_ft = {}
  87. endf
  88. let g:did_ft = {}
  89. fun! GetSnippets(dir, filetypes)
  90. for ft in split(a:filetypes, '\.')
  91. if has_key(g:did_ft, ft) | continue | endif
  92. call s:DefineSnips(a:dir, ft, ft)
  93. if ft == 'objc' || ft == 'cpp' || ft == 'cs'
  94. call s:DefineSnips(a:dir, 'c', ft)
  95. elseif ft == 'xhtml'
  96. call s:DefineSnips(a:dir, 'html', 'xhtml')
  97. endif
  98. let g:did_ft[ft] = 1
  99. endfor
  100. endf
  101. " Define "aliasft" snippets for the filetype "realft".
  102. fun s:DefineSnips(dir, aliasft, realft)
  103. for path in split(globpath(a:dir, a:aliasft.'/')."\n".
  104. \ globpath(a:dir, a:aliasft.'-*/'), "\n")
  105. call ExtractSnips(path, a:realft)
  106. endfor
  107. for path in split(globpath(a:dir, a:aliasft.'.snippets')."\n".
  108. \ globpath(a:dir, a:aliasft.'-*.snippets'), "\n")
  109. call ExtractSnipsFile(path, a:realft)
  110. endfor
  111. endf
  112. fun! TriggerSnippet()
  113. if exists('g:SuperTabMappingForward')
  114. if g:SuperTabMappingForward == "<tab>"
  115. let SuperTabKey = "\<c-n>"
  116. elseif g:SuperTabMappingBackward == "<tab>"
  117. let SuperTabKey = "\<c-p>"
  118. endif
  119. endif
  120. if pumvisible() " Update snippet if completion is used, or deal with supertab
  121. if exists('SuperTabKey')
  122. call feedkeys(SuperTabKey) | return ''
  123. endif
  124. call feedkeys("\<esc>a", 'n') " Close completion menu
  125. call feedkeys("\<tab>") | return ''
  126. endif
  127. if exists('g:snipPos') | return snipMate#jumpTabStop(0) | endif
  128. let word = matchstr(getline('.'), '\S\+\%'.col('.').'c')
  129. for scope in [bufnr('%')] + split(&ft, '\.') + ['_']
  130. let [trigger, snippet] = s:GetSnippet(word, scope)
  131. " If word is a trigger for a snippet, delete the trigger & expand
  132. " the snippet.
  133. if snippet != ''
  134. let col = col('.') - len(trigger)
  135. sil exe 's/\V'.escape(trigger, '/.').'\%#//'
  136. return snipMate#expandSnip(snippet, col)
  137. endif
  138. endfor
  139. if exists('SuperTabKey')
  140. call feedkeys(SuperTabKey)
  141. return ''
  142. endif
  143. return "\<tab>"
  144. endf
  145. fun! BackwardsSnippet()
  146. if exists('g:snipPos') | return snipMate#jumpTabStop(1) | endif
  147. if exists('g:SuperTabMappingForward')
  148. if g:SuperTabMappingBackward == "<s-tab>"
  149. let SuperTabKey = "\<c-p>"
  150. elseif g:SuperTabMappingForward == "<s-tab>"
  151. let SuperTabKey = "\<c-n>"
  152. endif
  153. endif
  154. if exists('SuperTabKey')
  155. call feedkeys(SuperTabKey)
  156. return ''
  157. endif
  158. return "\<s-tab>"
  159. endf
  160. " Check if word under cursor is snippet trigger; if it isn't, try checking if
  161. " the text after non-word characters is (e.g. check for "foo" in "bar.foo")
  162. fun s:GetSnippet(word, scope)
  163. let word = a:word | let snippet = ''
  164. while snippet == ''
  165. if exists('s:snippets["'.a:scope.'"]["'.escape(word, '\"').'"]')
  166. let snippet = s:snippets[a:scope][word]
  167. elseif exists('s:multi_snips["'.a:scope.'"]["'.escape(word, '\"').'"]')
  168. let snippet = s:ChooseSnippet(a:scope, word)
  169. if snippet == '' | break | endif
  170. else
  171. if match(word, '\W') == -1 | break | endif
  172. let word = substitute(word, '.\{-}\W', '', '')
  173. endif
  174. endw
  175. if word == '' && a:word != '.' && stridx(a:word, '.') != -1
  176. let [word, snippet] = s:GetSnippet('.', a:scope)
  177. endif
  178. return [word, snippet]
  179. endf
  180. fun s:ChooseSnippet(scope, trigger)
  181. let snippet = []
  182. let i = 1
  183. for snip in s:multi_snips[a:scope][a:trigger]
  184. let snippet += [i.'. '.snip[0]]
  185. let i += 1
  186. endfor
  187. if i == 2 | return s:multi_snips[a:scope][a:trigger][0][1] | endif
  188. let num = inputlist(snippet) - 1
  189. return num == -1 ? '' : s:multi_snips[a:scope][a:trigger][num][1]
  190. endf
  191. fun! ShowAvailableSnips()
  192. let line = getline('.')
  193. let col = col('.')
  194. let word = matchstr(getline('.'), '\S\+\%'.col.'c')
  195. let words = [word]
  196. if stridx(word, '.')
  197. let words += split(word, '\.', 1)
  198. endif
  199. let matchlen = 0
  200. let matches = []
  201. for scope in [bufnr('%')] + split(&ft, '\.') + ['_']
  202. let triggers = has_key(s:snippets, scope) ? keys(s:snippets[scope]) : []
  203. if has_key(s:multi_snips, scope)
  204. let triggers += keys(s:multi_snips[scope])
  205. endif
  206. for trigger in triggers
  207. for word in words
  208. if word == ''
  209. let matches += [trigger] " Show all matches if word is empty
  210. elseif trigger =~ '^'.word
  211. let matches += [trigger]
  212. let len = len(word)
  213. if len > matchlen | let matchlen = len | endif
  214. endif
  215. endfor
  216. endfor
  217. endfor
  218. " This is to avoid a bug with Vim when using complete(col - matchlen, matches)
  219. " (Issue#46 on the Google Code snipMate issue tracker).
  220. call setline(line('.'), substitute(line, repeat('.', matchlen).'\%'.col.'c', '', ''))
  221. call complete(col, matches)
  222. return ''
  223. endf
  224. " vim:noet:sw=4:ts=4:ft=vim