PageRenderTime 26ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/vim/vim/bundle/syntastic/plugin/syntastic.vim

https://github.com/agross/dotfiles
Vim Script | 264 lines | 166 code | 46 blank | 52 comment | 37 complexity | 3200b43d38fdaed29acc316d5da71c4b MD5 | raw file
  1. "============================================================================
  2. "File: syntastic.vim
  3. "Description: vim plugin for on the fly syntax checking
  4. "Maintainer: Martin Grenfell <martin.grenfell at gmail dot com>
  5. "Version: 1.1.0
  6. "Last Change: 16 Dec, 2009
  7. "License: This program is free software. It comes without any warranty,
  8. " to the extent permitted by applicable law. You can redistribute
  9. " it and/or modify it under the terms of the Do What The Fuck You
  10. " Want To Public License, Version 2, as published by Sam Hocevar.
  11. " See http://sam.zoy.org/wtfpl/COPYING for more details.
  12. "
  13. "============================================================================
  14. if exists("g:loaded_syntastic_plugin")
  15. finish
  16. endif
  17. let g:loaded_syntastic_plugin = 1
  18. let s:running_windows = has("win16") || has("win32") || has("win64")
  19. if !exists("g:syntastic_enable_signs") || !has('signs')
  20. let g:syntastic_enable_signs = 0
  21. endif
  22. if !exists("g:syntastic_auto_loc_list")
  23. let g:syntastic_auto_loc_list = 0
  24. endif
  25. if !exists("g:syntastic_quiet_warnings")
  26. let g:syntastic_quiet_warnings = 0
  27. endif
  28. if !exists("g:syntastic_disabled_filetypes")
  29. let g:syntastic_disabled_filetypes = []
  30. endif
  31. "load all the syntax checkers
  32. runtime! syntax_checkers/*.vim
  33. "refresh and redraw all the error info for this buf when saving or reading
  34. autocmd bufreadpost,bufwritepost * call s:UpdateErrors()
  35. function! s:UpdateErrors()
  36. call s:CacheErrors()
  37. if g:syntastic_enable_signs
  38. call s:RefreshSigns()
  39. endif
  40. if g:syntastic_auto_loc_list
  41. if s:BufHasErrorsOrWarningsToDisplay()
  42. call s:ShowLocList()
  43. else
  44. "TODO: this will close the loc list window if one was opened by
  45. "something other than syntastic
  46. lclose
  47. endif
  48. endif
  49. endfunction
  50. "detect and cache all syntax errors in this buffer
  51. "
  52. "depends on a function called SyntaxCheckers_{&ft}_GetLocList() existing
  53. "elsewhere
  54. function! s:CacheErrors()
  55. let b:syntastic_loclist = []
  56. if filereadable(expand("%"))
  57. for ft in split(&ft, '\.')
  58. if s:Checkable(ft)
  59. let b:syntastic_loclist = extend(b:syntastic_loclist, SyntaxCheckers_{ft}_GetLocList())
  60. endif
  61. endfor
  62. endif
  63. endfunction
  64. "return true if there are cached errors/warnings for this buf
  65. function! s:BufHasErrorsOrWarnings()
  66. return exists("b:syntastic_loclist") && !empty(b:syntastic_loclist)
  67. endfunction
  68. "return true if there are cached errors for this buf
  69. function! s:BufHasErrors()
  70. return len(s:ErrorsForType('E')) > 0
  71. endfunction
  72. function! s:BufHasErrorsOrWarningsToDisplay()
  73. return s:BufHasErrors() || (!g:syntastic_quiet_warnings && s:BufHasErrorsOrWarnings())
  74. endfunction
  75. function! s:ErrorsForType(type)
  76. if !exists("b:syntastic_loclist")
  77. return []
  78. endif
  79. return filter(copy(b:syntastic_loclist), 'v:val["type"] ==# "' . a:type . '"')
  80. endfunction
  81. if g:syntastic_enable_signs
  82. "use >> to display syntax errors in the sign column
  83. sign define SyntasticError text=>> texthl=error
  84. sign define SyntasticWarning text=>> texthl=todo
  85. endif
  86. "start counting sign ids at 5000, start here to hopefully avoid conflicting
  87. "with any other code that places signs (not sure if this precaution is
  88. "actually needed)
  89. let s:first_sign_id = 5000
  90. let s:next_sign_id = s:first_sign_id
  91. "place signs by all syntax errs in the buffer
  92. function s:SignErrors()
  93. if s:BufHasErrorsOrWarningsToDisplay()
  94. for i in b:syntastic_loclist
  95. let sign_type = 'SyntasticError'
  96. if i['type'] == 'W'
  97. let sign_type = 'SyntasticWarning'
  98. endif
  99. exec "sign place ". s:next_sign_id ." line=". i['lnum'] ." name=". sign_type ." file=". expand("%:p")
  100. call add(s:BufSignIds(), s:next_sign_id)
  101. let s:next_sign_id += 1
  102. endfor
  103. endif
  104. endfunction
  105. "remove the signs with the given ids from this buffer
  106. function! s:RemoveSigns(ids)
  107. for i in a:ids
  108. exec "sign unplace " . i
  109. call remove(s:BufSignIds(), index(s:BufSignIds(), i))
  110. endfor
  111. endfunction
  112. "get all the ids of the SyntaxError signs in the buffer
  113. function! s:BufSignIds()
  114. if !exists("b:syntastic_sign_ids")
  115. let b:syntastic_sign_ids = []
  116. endif
  117. return b:syntastic_sign_ids
  118. endfunction
  119. "update the error signs
  120. function! s:RefreshSigns()
  121. let old_signs = copy(s:BufSignIds())
  122. call s:SignErrors()
  123. call s:RemoveSigns(old_signs)
  124. let s:first_sign_id = s:next_sign_id
  125. endfunction
  126. "display the cached errors for this buf in the location list
  127. function! s:ShowLocList()
  128. if exists("b:syntastic_loclist")
  129. call setloclist(0, b:syntastic_loclist)
  130. let num = winnr()
  131. lopen
  132. if num != winnr()
  133. wincmd p
  134. endif
  135. endif
  136. endfunction
  137. command Errors call s:ShowLocList()
  138. "return [syntax:X(Y)] if syntax errors are detected in the buffer, where X is the
  139. "line number of the first error and Y is the number of errors detected. (Y) is
  140. "only displayed if > 1 errors are detected
  141. "
  142. "return '' if no errors are cached for the buffer
  143. function! SyntasticStatuslineFlag()
  144. if s:BufHasErrorsOrWarningsToDisplay()
  145. let first_err_line = b:syntastic_loclist[0]['lnum']
  146. if g:syntastic_quiet_warnings
  147. let first_err_line = s:ErrorsForType('E')[0]['lnum']
  148. endif
  149. let err_count = len(b:syntastic_loclist)
  150. if g:syntastic_quiet_warnings
  151. let err_count = len(s:ErrorsForType('E'))
  152. endif
  153. let toReturn = '[syntax:' . first_err_line
  154. if err_count > 1
  155. let toReturn .= '(' . err_count . ')'
  156. endif
  157. let toReturn .= ']'
  158. return toReturn
  159. else
  160. return ''
  161. endif
  162. endfunction
  163. "A wrapper for the :lmake command. Sets up the make environment according to
  164. "the options given, runs make, resets the environment, returns the location
  165. "list
  166. "
  167. "a:options can contain the following keys:
  168. " 'makeprg'
  169. " 'errorformat'
  170. "
  171. "The corresponding options are set for the duration of the function call. They
  172. "are set with :let, so dont escape spaces.
  173. function! SyntasticMake(options)
  174. let old_loclist = getloclist(0)
  175. let old_makeprg = &makeprg
  176. let old_shellpipe = &shellpipe
  177. let old_errorformat = &errorformat
  178. if !s:running_windows
  179. "this is a hack to stop the screen needing to be ':redraw'n when
  180. "when :lmake is run. Otherwise the screen flickers annoyingly
  181. let &shellpipe='&>'
  182. endif
  183. if has_key(a:options, 'makeprg')
  184. let &makeprg = a:options['makeprg']
  185. endif
  186. if has_key(a:options, 'errorformat')
  187. let &errorformat = a:options['errorformat']
  188. endif
  189. silent lmake!
  190. let errors = getloclist(0)
  191. call setloclist(0, old_loclist)
  192. let &makeprg = old_makeprg
  193. let &errorformat = old_errorformat
  194. let &shellpipe=old_shellpipe
  195. return errors
  196. endfunction
  197. function! s:Checkable(ft)
  198. return exists("*SyntaxCheckers_". a:ft ."_GetLocList") &&
  199. \ index(g:syntastic_disabled_filetypes, a:ft) == -1
  200. endfunction
  201. command! -nargs=? SyntasticEnable call s:Enable(<f-args>)
  202. command! -nargs=? SyntasticDisable call s:Disable(<f-args>)
  203. "disable syntax checking for the given filetype (defaulting to current ft)
  204. function! s:Disable(...)
  205. let ft = a:0 ? a:1 : &filetype
  206. if !empty(ft) && index(g:syntastic_disabled_filetypes, ft) == -1
  207. call add(g:syntastic_disabled_filetypes, ft)
  208. endif
  209. endfunction
  210. "enable syntax checking for the given filetype (defaulting to current ft)
  211. function! s:Enable(...)
  212. let ft = a:0 ? a:1 : &filetype
  213. let i = index(g:syntastic_disabled_filetypes, ft)
  214. if i != -1
  215. call remove(g:syntastic_disabled_filetypes, i)
  216. endif
  217. endfunction
  218. " vim: set et sts=4 sw=4: