syntastic.vim 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. "============================================================================
  2. "File: syntastic.vim
  3. "Description: Vim plugin for on the fly syntax checking.
  4. "License: This program is free software. It comes without any warranty,
  5. " to the extent permitted by applicable law. You can redistribute
  6. " it and/or modify it under the terms of the Do What The Fuck You
  7. " Want To Public License, Version 2, as published by Sam Hocevar.
  8. " See http://sam.zoy.org/wtfpl/COPYING for more details.
  9. "
  10. "============================================================================
  11. if exists('g:loaded_syntastic_plugin') || &compatible
  12. finish
  13. endif
  14. let g:loaded_syntastic_plugin = 1
  15. if has('reltime')
  16. let g:_SYNTASTIC_START = reltime()
  17. lockvar! g:_SYNTASTIC_START
  18. endif
  19. let g:_SYNTASTIC_VERSION = '3.10.0-36'
  20. lockvar g:_SYNTASTIC_VERSION
  21. " Sanity checks {{{1
  22. if v:version < 700 || (v:version == 700 && !has('patch175'))
  23. call syntastic#log#error('need Vim version 7.0.175 or later')
  24. finish
  25. endif
  26. for s:feature in [
  27. \ 'autocmd',
  28. \ 'eval',
  29. \ 'file_in_path',
  30. \ 'modify_fname',
  31. \ 'quickfix',
  32. \ 'reltime',
  33. \ 'statusline',
  34. \ 'user_commands',
  35. \ ]
  36. if !has(s:feature)
  37. call syntastic#log#error('need Vim compiled with feature ' . s:feature)
  38. finish
  39. endif
  40. endfor
  41. let s:_running_windows = syntastic#util#isRunningWindows()
  42. lockvar s:_running_windows
  43. if !exists('g:syntastic_shell')
  44. let g:syntastic_shell = &shell
  45. endif
  46. if s:_running_windows
  47. let g:_SYNTASTIC_UNAME = 'Windows'
  48. elseif executable('uname')
  49. try
  50. let g:_SYNTASTIC_UNAME = split(syntastic#util#system('uname'), "\n")[0]
  51. catch /\m^E145$/
  52. call syntastic#log#error("can't run in rvim")
  53. finish
  54. catch /\m^E484$/
  55. call syntastic#log#error("can't run external programs (misconfigured shell options?)")
  56. finish
  57. catch /\m^E684$/
  58. let g:_SYNTASTIC_UNAME = 'Unknown'
  59. endtry
  60. else
  61. let g:_SYNTASTIC_UNAME = 'Unknown'
  62. endif
  63. lockvar g:_SYNTASTIC_UNAME
  64. " XXX Ugly hack to make g:_SYNTASTIC_UNAME available to :SyntasticInfo without
  65. " polluting session namespaces
  66. let g:syntastic_version =
  67. \ g:_SYNTASTIC_VERSION .
  68. \ ' (Vim ' . v:version . (has('nvim') ? ', Neovim' : '') . ', ' .
  69. \ g:_SYNTASTIC_UNAME .
  70. \ (has('gui') ? ', GUI' : '') . ')'
  71. lockvar g:syntastic_version
  72. " }}}1
  73. " Defaults {{{1
  74. let g:_SYNTASTIC_DEFAULTS = {
  75. \ 'aggregate_errors': 0,
  76. \ 'always_populate_loc_list': 0,
  77. \ 'auto_jump': 0,
  78. \ 'auto_loc_list': 2,
  79. \ 'check_on_open': 0,
  80. \ 'check_on_wq': 1,
  81. \ 'cursor_columns': 1,
  82. \ 'debug': 0,
  83. \ 'echo_current_error': 1,
  84. \ 'enable_balloons': 1,
  85. \ 'enable_highlighting': 1,
  86. \ 'enable_signs': 1,
  87. \ 'error_symbol': '>>',
  88. \ 'exit_checks': !(s:_running_windows && syntastic#util#var('shell', &shell) =~? '\m\<cmd\.exe$'),
  89. \ 'filetype_map': {},
  90. \ 'full_redraws': !(has('gui_running') || has('gui_macvim')),
  91. \ 'id_checkers': 1,
  92. \ 'ignore_extensions': '\c\v^([gx]?z|lzma|bz2)$',
  93. \ 'ignore_files': [],
  94. \ 'loc_list_height': 10,
  95. \ 'nested_autocommands': 0,
  96. \ 'quiet_messages': {},
  97. \ 'reuse_loc_lists': 1,
  98. \ 'shell': &shell,
  99. \ 'sort_aggregated_errors': 1,
  100. \ 'stl_format': '[Syntax: line:%F (%t)]',
  101. \ 'style_error_symbol': 'S>',
  102. \ 'style_warning_symbol': 'S>',
  103. \ 'warning_symbol': '>>'
  104. \ }
  105. lockvar! g:_SYNTASTIC_DEFAULTS
  106. for s:key in keys(g:_SYNTASTIC_DEFAULTS)
  107. if !exists('g:syntastic_' . s:key)
  108. let g:syntastic_{s:key} = copy(g:_SYNTASTIC_DEFAULTS[s:key])
  109. endif
  110. endfor
  111. if exists('g:syntastic_quiet_warnings')
  112. call syntastic#log#oneTimeWarn("variable g:syntastic_quiet_warnings is deprecated, please use let g:syntastic_quiet_messages = {'level': 'warnings'} instead")
  113. if g:syntastic_quiet_warnings
  114. let s:quiet_warnings = get(g:syntastic_quiet_messages, 'type', [])
  115. if type(s:quiet_warnings) != type([])
  116. let s:quiet_warnings = [s:quiet_warnings]
  117. endif
  118. call add(s:quiet_warnings, 'warnings')
  119. let g:syntastic_quiet_messages['type'] = s:quiet_warnings
  120. endif
  121. endif
  122. " }}}1
  123. " Debug {{{1
  124. let g:_SYNTASTIC_SHELL_OPTIONS = [
  125. \ 'shell',
  126. \ 'shellcmdflag',
  127. \ 'shellpipe',
  128. \ 'shellquote',
  129. \ 'shellredir',
  130. \ 'shelltemp',
  131. \ 'shellxquote'
  132. \ ]
  133. for s:feature in [
  134. \ 'autochdir',
  135. \ 'shellslash',
  136. \ 'shellxescape',
  137. \ ]
  138. if exists('+' . s:feature)
  139. call add(g:_SYNTASTIC_SHELL_OPTIONS, s:feature)
  140. endif
  141. endfor
  142. lockvar! g:_SYNTASTIC_SHELL_OPTIONS
  143. " debug constants
  144. let g:_SYNTASTIC_DEBUG_TRACE = 1
  145. lockvar g:_SYNTASTIC_DEBUG_TRACE
  146. let g:_SYNTASTIC_DEBUG_LOCLIST = 2
  147. lockvar g:_SYNTASTIC_DEBUG_LOCLIST
  148. let g:_SYNTASTIC_DEBUG_NOTIFICATIONS = 4
  149. lockvar g:_SYNTASTIC_DEBUG_NOTIFICATIONS
  150. let g:_SYNTASTIC_DEBUG_AUTOCOMMANDS = 8
  151. lockvar g:_SYNTASTIC_DEBUG_AUTOCOMMANDS
  152. let g:_SYNTASTIC_DEBUG_VARIABLES = 16
  153. lockvar g:_SYNTASTIC_DEBUG_VARIABLES
  154. let g:_SYNTASTIC_DEBUG_CHECKERS = 32
  155. lockvar g:_SYNTASTIC_DEBUG_CHECKERS
  156. " }}}1
  157. runtime! plugin/syntastic/*.vim
  158. let s:registry = g:SyntasticRegistry.Instance()
  159. let s:notifiers = g:SyntasticNotifiers.Instance()
  160. let s:modemap = g:SyntasticModeMap.Instance()
  161. let s:_check_stack = []
  162. let s:_quit_pre = []
  163. " Commands {{{1
  164. " @vimlint(EVL103, 1, a:cursorPos)
  165. " @vimlint(EVL103, 1, a:cmdLine)
  166. " @vimlint(EVL103, 1, a:argLead)
  167. function! s:CompleteCheckerName(argLead, cmdLine, cursorPos) abort " {{{2
  168. let names = []
  169. let sep_idx = stridx(a:argLead, '/')
  170. if sep_idx >= 1
  171. let ft = a:argLead[: sep_idx-1]
  172. call extend(names, map( s:registry.getNamesOfAvailableCheckers(ft), 'ft . "/" . v:val' ))
  173. else
  174. for ft in s:registry.resolveFiletypes(&filetype)
  175. call extend(names, s:registry.getNamesOfAvailableCheckers(ft))
  176. endfor
  177. call extend(names, map( copy(s:registry.getKnownFiletypes()), 'v:val . "/"' ))
  178. endif
  179. return join(names, "\n")
  180. endfunction " }}}2
  181. " @vimlint(EVL103, 0, a:cursorPos)
  182. " @vimlint(EVL103, 0, a:cmdLine)
  183. " @vimlint(EVL103, 0, a:argLead)
  184. " @vimlint(EVL103, 1, a:cursorPos)
  185. " @vimlint(EVL103, 1, a:cmdLine)
  186. " @vimlint(EVL103, 1, a:argLead)
  187. function! s:CompleteFiletypes(argLead, cmdLine, cursorPos) abort " {{{2
  188. return join(s:registry.getKnownFiletypes(), "\n")
  189. endfunction " }}}2
  190. " @vimlint(EVL103, 0, a:cursorPos)
  191. " @vimlint(EVL103, 0, a:cmdLine)
  192. " @vimlint(EVL103, 0, a:argLead)
  193. command! -bar -nargs=* -complete=custom,s:CompleteCheckerName SyntasticCheck call SyntasticCheck(<f-args>)
  194. command! -bar -nargs=? -complete=custom,s:CompleteFiletypes SyntasticInfo call SyntasticInfo(<f-args>)
  195. command! -bar Errors call SyntasticErrors()
  196. command! -bar SyntasticReset call SyntasticReset()
  197. command! -bar SyntasticToggleMode call SyntasticToggleMode()
  198. command! -bar SyntasticSetLoclist call SyntasticSetLoclist()
  199. command! SyntasticJavacEditClasspath runtime! syntax_checkers/java/*.vim | SyntasticJavacEditClasspath
  200. command! SyntasticJavacEditConfig runtime! syntax_checkers/java/*.vim | SyntasticJavacEditConfig
  201. " }}}1
  202. " Public API {{{1
  203. function! SyntasticCheck(...) abort " {{{2
  204. call s:UpdateErrors(bufnr(''), 0, a:000)
  205. call syntastic#util#redraw(g:syntastic_full_redraws)
  206. endfunction " }}}2
  207. function! SyntasticInfo(...) abort " {{{2
  208. call s:modemap.modeInfo(a:000)
  209. call s:registry.echoInfoFor(a:000)
  210. call s:_explain_skip(a:000)
  211. call syntastic#log#debugShowOptions(g:_SYNTASTIC_DEBUG_TRACE, g:_SYNTASTIC_SHELL_OPTIONS)
  212. call syntastic#log#debugDump(g:_SYNTASTIC_DEBUG_VARIABLES)
  213. endfunction " }}}2
  214. function! SyntasticErrors() abort " {{{2
  215. call g:SyntasticLoclist.current().show()
  216. endfunction " }}}2
  217. function! SyntasticReset() abort " {{{2
  218. call s:ClearCache(bufnr(''))
  219. call s:notifiers.refresh(g:SyntasticLoclist.New([]))
  220. endfunction " }}}2
  221. function! SyntasticToggleMode() abort " {{{2
  222. call s:modemap.toggleMode()
  223. call s:ClearCache(bufnr(''))
  224. call s:notifiers.refresh(g:SyntasticLoclist.New([]))
  225. call s:modemap.echoMode()
  226. endfunction " }}}2
  227. function! SyntasticSetLoclist() abort " {{{2
  228. call g:SyntasticLoclist.current().setloclist(0)
  229. endfunction " }}}2
  230. " }}}1
  231. " Autocommands {{{1
  232. augroup syntastic
  233. autocmd!
  234. autocmd VimEnter * call s:VimEnterHook()
  235. autocmd BufEnter * call s:BufEnterHook(expand('<afile>', 1))
  236. autocmd BufWinEnter * call s:BufWinEnterHook(expand('<afile>', 1))
  237. augroup END
  238. if g:syntastic_nested_autocommands
  239. augroup syntastic
  240. autocmd BufReadPost * nested call s:BufReadPostHook(expand('<afile>', 1))
  241. autocmd BufWritePost * nested call s:BufWritePostHook(expand('<afile>', 1))
  242. augroup END
  243. else
  244. augroup syntastic
  245. autocmd BufReadPost * call s:BufReadPostHook(expand('<afile>', 1))
  246. autocmd BufWritePost * call s:BufWritePostHook(expand('<afile>', 1))
  247. augroup END
  248. endif
  249. if exists('##QuitPre')
  250. " QuitPre was added in Vim 7.3.544
  251. augroup syntastic
  252. autocmd QuitPre * call s:QuitPreHook(expand('<afile>', 1))
  253. augroup END
  254. endif
  255. function! s:BufReadPostHook(fname) abort " {{{2
  256. let buf = syntastic#util#fname2buf(a:fname)
  257. if g:syntastic_check_on_open && buf > 0
  258. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_AUTOCOMMANDS,
  259. \ 'autocmd: BufReadPost, buffer ' . buf . ' = ' . string(a:fname))
  260. if index(s:_check_stack, buf) == -1
  261. call add(s:_check_stack, buf)
  262. endif
  263. endif
  264. endfunction " }}}2
  265. function! s:BufWritePostHook(fname) abort " {{{2
  266. let buf = syntastic#util#fname2buf(a:fname)
  267. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_AUTOCOMMANDS,
  268. \ 'autocmd: BufWritePost, buffer ' . buf . ' = ' . string(a:fname))
  269. call s:UpdateErrors(buf, 1, [])
  270. endfunction " }}}2
  271. function! s:BufEnterHook(fname) abort " {{{2
  272. let buf = syntastic#util#fname2buf(a:fname)
  273. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_AUTOCOMMANDS,
  274. \ 'autocmd: BufEnter, buffer ' . buf . ' = ' . string(a:fname) . ', &buftype = ' . string(&buftype))
  275. if buf > 0 && getbufvar(buf, '&buftype') ==# ''
  276. let idx = index(reverse(copy(s:_check_stack)), buf)
  277. if idx >= 0
  278. if !has('vim_starting')
  279. call remove(s:_check_stack, -idx - 1)
  280. call s:UpdateErrors(buf, 1, [])
  281. endif
  282. elseif &buftype ==# ''
  283. call s:notifiers.refresh(g:SyntasticLoclist.current())
  284. endif
  285. elseif &buftype ==# 'quickfix'
  286. " TODO: this is needed because in recent versions of Vim lclose
  287. " can no longer be called from BufWinLeave
  288. " TODO: at this point there is no b:syntastic_loclist
  289. let loclist = filter(copy(getloclist(0)), 'v:val["valid"]')
  290. let owner = str2nr(getbufvar(buf, 'syntastic_owner_buffer'))
  291. let buffers = syntastic#util#unique(map(loclist, 'v:val["bufnr"]') + (owner ? [owner] : []))
  292. if !empty(get(w:, 'syntastic_loclist_set', [])) && !empty(loclist) && empty(filter( buffers, 'syntastic#util#bufIsActive(v:val)' ))
  293. call SyntasticLoclistHide()
  294. endif
  295. endif
  296. endfunction " }}}2
  297. function! s:BufWinEnterHook(fname) abort " {{{2
  298. let buf = syntastic#util#fname2buf(a:fname)
  299. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_AUTOCOMMANDS,
  300. \ 'autocmd: BufWinEnter, buffer ' . buf . ' = ' . string(a:fname) . ', &buftype = ' . string(&buftype))
  301. if buf > 0 && getbufvar(buf, '&buftype') ==# ''
  302. let idx = index(reverse(copy(s:_check_stack)), buf)
  303. if idx >= 0 && !has('vim_starting')
  304. call remove(s:_check_stack, -idx - 1)
  305. call s:UpdateErrors(buf, 1, [])
  306. endif
  307. endif
  308. endfunction " }}}2
  309. function! s:VimEnterHook() abort " {{{2
  310. let buf = bufnr('')
  311. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_AUTOCOMMANDS,
  312. \ 'autocmd: VimEnter, buffer ' . buf . ' = ' . string(bufname(buf)) . ', &buftype = ' . string(&buftype))
  313. let idx = index(reverse(copy(s:_check_stack)), buf)
  314. if idx >= 0 && getbufvar(buf, '&buftype') ==# ''
  315. call remove(s:_check_stack, -idx - 1)
  316. call s:UpdateErrors(buf, 1, [])
  317. endif
  318. endfunction " }}}2
  319. function! s:QuitPreHook(fname) abort " {{{2
  320. let buf = syntastic#util#fname2buf(a:fname)
  321. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_AUTOCOMMANDS, 'autocmd: QuitPre, buffer ' . buf . ' = ' . string(a:fname))
  322. if !syntastic#util#var('check_on_wq')
  323. call syntastic#util#setWids()
  324. call add(s:_quit_pre, buf . '_' . getbufvar(buf, 'changetick') . '_' . w:syntastic_wid)
  325. endif
  326. if !empty(get(w:, 'syntastic_loclist_set', []))
  327. call SyntasticLoclistHide()
  328. endif
  329. endfunction " }}}2
  330. " }}}1
  331. " Main {{{1
  332. "refresh and redraw all the error info for this buf when saving or reading
  333. function! s:UpdateErrors(buf, auto_invoked, checker_names) abort " {{{2
  334. call syntastic#log#debugShowVariables(g:_SYNTASTIC_DEBUG_TRACE, 'version')
  335. call syntastic#log#debugShowOptions(g:_SYNTASTIC_DEBUG_TRACE, g:_SYNTASTIC_SHELL_OPTIONS)
  336. call syntastic#log#debugDump(g:_SYNTASTIC_DEBUG_VARIABLES)
  337. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_TRACE, 'UpdateErrors' . (a:auto_invoked ? ' (auto)' : '') .
  338. \ ': ' . (len(a:checker_names) ? join(a:checker_names) : 'default checkers'))
  339. call s:modemap.synch()
  340. if s:_skip_file(a:buf)
  341. return
  342. endif
  343. let run_checks = !a:auto_invoked || s:modemap.doAutoChecking(a:buf)
  344. if run_checks
  345. call s:CacheErrors(a:buf, a:checker_names)
  346. call syntastic#util#setLastTick(a:buf)
  347. elseif a:auto_invoked
  348. return
  349. endif
  350. let loclist = g:SyntasticLoclist.current(a:buf)
  351. if exists('*SyntasticCheckHook')
  352. call SyntasticCheckHook(loclist.getRaw())
  353. endif
  354. " populate loclist and jump {{{3
  355. let do_jump = syntastic#util#var('auto_jump') + 0
  356. if do_jump == 2
  357. let do_jump = loclist.getFirstError(1)
  358. elseif do_jump == 3
  359. let do_jump = loclist.getFirstError()
  360. elseif 0 > do_jump || do_jump > 3
  361. let do_jump = 0
  362. endif
  363. if syntastic#util#var('always_populate_loc_list') || do_jump
  364. call loclist.setloclist(1)
  365. if run_checks && do_jump && !loclist.isEmpty()
  366. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_NOTIFICATIONS, 'loclist: jump')
  367. execute 'silent! lrewind ' . do_jump
  368. " XXX: Vim doesn't call autocmd commands in a predictible
  369. " order, which can lead to missing filetype when jumping
  370. " to a new file; the following is a workaround for the
  371. " resulting brain damage
  372. if &filetype ==# ''
  373. silent! filetype detect
  374. endif
  375. endif
  376. endif
  377. " }}}3
  378. call s:notifiers.refresh(loclist)
  379. endfunction " }}}2
  380. "clear the loc list for the buffer
  381. function! s:ClearCache(buf) abort " {{{2
  382. let loclist = g:SyntasticLoclist.current(a:buf)
  383. call s:notifiers.reset(loclist)
  384. call loclist.destroy()
  385. endfunction " }}}2
  386. "detect and cache all syntax errors in this buffer
  387. function! s:CacheErrors(buf, checker_names) abort " {{{2
  388. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_TRACE, 'CacheErrors: ' .
  389. \ (len(a:checker_names) ? join(a:checker_names) : 'default checkers'))
  390. call s:ClearCache(a:buf)
  391. let newLoclist = g:SyntasticLoclist.New([])
  392. call newLoclist.setOwner(a:buf)
  393. if !s:_skip_file(a:buf)
  394. " debug logging {{{3
  395. call syntastic#log#debugShowVariables(g:_SYNTASTIC_DEBUG_TRACE, 'aggregate_errors')
  396. if syntastic#util#isRunningWindows()
  397. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_CHECKERS, '$TMP = ' . string($TMP) . ', $TEMP = ' . string($TEMP))
  398. else
  399. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_CHECKERS, '$TERM = ' . string($TERM))
  400. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_CHECKERS, '$TMPDIR = ' . string($TMPDIR))
  401. endif
  402. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_CHECKERS, '$PATH = ' . string($PATH))
  403. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_TRACE, 'getcwd() = ' . string(getcwd()))
  404. " }}}3
  405. let clist = s:registry.getCheckers(getbufvar(a:buf, '&filetype'), a:checker_names)
  406. let aggregate_errors =
  407. \ syntastic#util#var('aggregate_errors') || len(syntastic#util#unique(map(copy(clist), 'v:val.getFiletype()'))) > 1
  408. let decorate_errors = aggregate_errors && syntastic#util#var('id_checkers')
  409. let sort_aggregated_errors = aggregate_errors && syntastic#util#var('sort_aggregated_errors')
  410. let names = []
  411. let unavailable_checkers = 0
  412. for checker in clist
  413. let cname = checker.getCName()
  414. if !checker.isAvailable()
  415. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_TRACE, 'CacheErrors: Checker ' . cname . ' is not available')
  416. let unavailable_checkers += 1
  417. continue
  418. endif
  419. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_TRACE, 'CacheErrors: Invoking checker: ' . cname)
  420. let loclist = checker.getLocList()
  421. if !loclist.isEmpty()
  422. if decorate_errors
  423. call loclist.decorate(cname)
  424. endif
  425. call add(names, cname)
  426. if checker.wantSort() && !sort_aggregated_errors
  427. call loclist.sort()
  428. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_LOCLIST, 'sorted:', loclist)
  429. endif
  430. call newLoclist.extend(loclist)
  431. if !aggregate_errors
  432. break
  433. endif
  434. endif
  435. endfor
  436. " set names {{{3
  437. if !empty(names)
  438. if len(syntastic#util#unique(map( copy(names), 'substitute(v:val, "\\m/.*", "", "")' ))) == 1
  439. let type = substitute(names[0], '\m/.*', '', '')
  440. let name = join(map( names, 'substitute(v:val, "\\m.\\{-}/", "", "")' ), ', ')
  441. call newLoclist.setName( name . ' ('. type . ')' )
  442. else
  443. " checkers from mixed types
  444. call newLoclist.setName(join(names, ', '))
  445. endif
  446. endif
  447. " }}}3
  448. " issue warning about no active checkers {{{3
  449. if len(clist) == unavailable_checkers
  450. if !empty(a:checker_names)
  451. if len(a:checker_names) == 1
  452. call syntastic#log#warn('checker ' . a:checker_names[0] . ' is not available')
  453. else
  454. call syntastic#log#warn('checkers ' . join(a:checker_names, ', ') . ' are not available')
  455. endif
  456. else
  457. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_TRACE, 'CacheErrors: no checkers available for ' . &filetype)
  458. endif
  459. endif
  460. " }}}3
  461. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_LOCLIST, 'aggregated:', newLoclist)
  462. if sort_aggregated_errors
  463. call newLoclist.sort()
  464. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_LOCLIST, 'sorted:', newLoclist)
  465. endif
  466. endif
  467. call newLoclist.deploy()
  468. endfunction " }}}2
  469. "Emulates the :lmake command. Sets up the make environment according to the
  470. "options given, runs make, resets the environment, returns the location list
  471. "
  472. "a:options can contain the following keys:
  473. " 'makeprg'
  474. " 'errorformat'
  475. "
  476. "The corresponding options are set for the duration of the function call. They
  477. "are set with :let, so dont escape spaces.
  478. "
  479. "a:options may also contain:
  480. " 'defaults' - a dict containing default values for the returned errors
  481. " 'subtype' - all errors will be assigned the given subtype
  482. " 'preprocess' - a function to be applied to the error file before parsing errors
  483. " 'postprocess' - a list of functions to be applied to the error list
  484. " 'cwd' - change directory to the given path before running the checker
  485. " 'env' - environment variables to set before running the checker
  486. " 'returns' - a list of valid exit codes for the checker
  487. " @vimlint(EVL102, 1, l:env_save)
  488. function! SyntasticMake(options) abort " {{{2
  489. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_TRACE, 'SyntasticMake: called with options:', a:options)
  490. " save options and locale env variables {{{3
  491. let old_local_errorformat = &l:errorformat
  492. let old_errorformat = &errorformat
  493. let old_cwd = getcwd()
  494. " }}}3
  495. if has_key(a:options, 'errorformat')
  496. let &errorformat = a:options['errorformat']
  497. set errorformat<
  498. endif
  499. if has_key(a:options, 'cwd')
  500. execute 'lcd ' . fnameescape(a:options['cwd'])
  501. endif
  502. " set environment variables {{{3
  503. let env_save = {}
  504. if has_key(a:options, 'env') && len(a:options['env'])
  505. for key in keys(a:options['env'])
  506. if key =~? '\m^[a-z_][a-z0-9_]*$'
  507. execute 'let env_save[' . string(key) . '] = $' . key
  508. execute 'let $' . key . ' = ' . string(a:options['env'][key])
  509. endif
  510. endfor
  511. endif
  512. " }}}3
  513. let err_lines = split(syntastic#util#system(a:options['makeprg']), "\n", 1)
  514. " restore environment variables {{{3
  515. if len(env_save)
  516. for key in keys(env_save)
  517. execute 'let $' . key . ' = ' . string(env_save[key])
  518. endfor
  519. endif
  520. " }}}3
  521. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_LOCLIST, 'checker output:', err_lines)
  522. " Does it still make sense to go on?
  523. let bailout =
  524. \ syntastic#util#var('exit_checks') &&
  525. \ has_key(a:options, 'returns') &&
  526. \ index(a:options['returns'], v:shell_error) == -1
  527. if !bailout
  528. if has_key(a:options, 'Preprocess')
  529. let err_lines = call(a:options['Preprocess'], [err_lines])
  530. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_LOCLIST, 'preprocess (external):', err_lines)
  531. elseif has_key(a:options, 'preprocess')
  532. let err_lines = call('syntastic#preprocess#' . a:options['preprocess'], [err_lines])
  533. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_LOCLIST, 'preprocess:', err_lines)
  534. endif
  535. noautocmd lgetexpr err_lines
  536. let errors = deepcopy(getloclist(0))
  537. if has_key(a:options, 'cwd')
  538. execute 'lcd ' . fnameescape(old_cwd)
  539. endif
  540. try
  541. silent lolder
  542. catch /\m^Vim\%((\a\+)\)\=:E380/
  543. " E380: At bottom of quickfix stack
  544. call setloclist(0, [], 'r')
  545. try
  546. " Vim 7.4.2200 or later
  547. call setloclist(0, [], 'r', { 'title': '' })
  548. catch /\m^Vim\%((\a\+)\)\=:E\%(118\|731\)/
  549. " do nothing
  550. endtry
  551. catch /\m^Vim\%((\a\+)\)\=:E776/
  552. " E776: No location list
  553. " do nothing
  554. endtry
  555. else
  556. let errors = []
  557. endif
  558. " restore options {{{3
  559. let &errorformat = old_errorformat
  560. let &l:errorformat = old_local_errorformat
  561. " }}}3
  562. if !s:_running_windows && (s:_os_name() =~? 'FreeBSD' || s:_os_name() =~? 'OpenBSD')
  563. call syntastic#util#redraw(g:syntastic_full_redraws)
  564. endif
  565. if bailout
  566. call syntastic#log#ndebug(g:_SYNTASTIC_DEBUG_LOCLIST, 'checker output:', err_lines)
  567. throw 'Syntastic: checker error'
  568. endif
  569. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_LOCLIST, 'raw loclist:', errors)
  570. if has_key(a:options, 'defaults')
  571. call s:_add_to_errors(errors, a:options['defaults'])
  572. endif
  573. " Add subtype info if present.
  574. if has_key(a:options, 'subtype')
  575. call s:_add_to_errors(errors, { 'subtype': a:options['subtype'] })
  576. endif
  577. if has_key(a:options, 'Postprocess') && !empty(a:options['Postprocess'])
  578. for rule in a:options['Postprocess']
  579. let errors = call(rule, [errors])
  580. endfor
  581. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_LOCLIST, 'postprocess (external):', errors)
  582. elseif has_key(a:options, 'postprocess') && !empty(a:options['postprocess'])
  583. for rule in a:options['postprocess']
  584. let errors = call('syntastic#postprocess#' . rule, [errors])
  585. endfor
  586. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_LOCLIST, 'postprocess:', errors)
  587. endif
  588. return errors
  589. endfunction " }}}2
  590. " @vimlint(EVL102, 0, l:env_save)
  591. "return a string representing the state of buffer according to
  592. "g:syntastic_stl_format
  593. "
  594. "return '' if no errors are cached for the buffer
  595. function! SyntasticStatuslineFlag() abort " {{{2
  596. return g:SyntasticLoclist.current().getStatuslineFlag()
  597. endfunction " }}}2
  598. " }}}1
  599. " Utilities {{{1
  600. function! s:_ignore_file(filename) abort " {{{2
  601. let fname = fnamemodify(a:filename, ':p')
  602. for pattern in g:syntastic_ignore_files
  603. if fname =~# pattern
  604. return 1
  605. endif
  606. endfor
  607. return 0
  608. endfunction " }}}2
  609. function! s:_is_quitting(buf) abort " {{{2
  610. let quitting = 0
  611. if exists('w:syntastic_wid')
  612. let key = a:buf . '_' . getbufvar(a:buf, 'changetick') . '_' . w:syntastic_wid
  613. let idx = index(s:_quit_pre, key)
  614. if idx >= 0
  615. call remove(s:_quit_pre, idx)
  616. let quitting = 1
  617. endif
  618. endif
  619. return quitting
  620. endfunction " }}}2
  621. " Skip running in special buffers
  622. function! s:_skip_file(buf) abort " {{{2
  623. let fname = bufname(a:buf)
  624. let skip = s:_is_quitting(a:buf) || getbufvar(a:buf, 'syntastic_skip_checks') ||
  625. \ (getbufvar(a:buf, '&buftype') !=# '') || !filereadable(fname) || getwinvar(0, '&diff') ||
  626. \ getwinvar(0, '&previewwindow') || s:_ignore_file(fname) ||
  627. \ fnamemodify(fname, ':e') =~? g:syntastic_ignore_extensions
  628. if skip
  629. call syntastic#log#debug(g:_SYNTASTIC_DEBUG_TRACE, '_skip_file: skipping checks')
  630. endif
  631. return skip
  632. endfunction " }}}2
  633. " Explain why checks will be skipped for the current file
  634. function! s:_explain_skip(filetypes) abort " {{{2
  635. let buf = bufnr('')
  636. if empty(a:filetypes) && s:_skip_file(buf)
  637. let why = []
  638. let fname = bufname(buf)
  639. let bt = getbufvar(buf, '&buftype')
  640. if s:_is_quitting(buf)
  641. call add(why, 'quitting buffer')
  642. endif
  643. if getbufvar(buf, 'syntastic_skip_checks')
  644. call add(why, 'b:syntastic_skip_checks set')
  645. endif
  646. if bt !=# ''
  647. call add(why, 'buftype = ' . string(&buftype))
  648. endif
  649. if !filereadable(fname)
  650. call add(why, 'file not readable / not local')
  651. endif
  652. if getwinvar(0, '&diff')
  653. call add(why, 'diff mode')
  654. endif
  655. if getwinvar(0, '&previewwindow')
  656. call add(why, 'preview window')
  657. endif
  658. if s:_ignore_file(fname)
  659. call add(why, 'filename matching g:syntastic_ignore_files')
  660. endif
  661. if fnamemodify(fname, ':e') =~? g:syntastic_ignore_extensions
  662. call add(why, 'extension matching g:syntastic_ignore_extensions')
  663. endif
  664. echomsg 'The current file will not be checked (' . join(why, ', ') . ')'
  665. endif
  666. endfunction " }}}2
  667. " Take a list of errors and add default values to them from a:options
  668. function! s:_add_to_errors(errors, options) abort " {{{2
  669. for err in a:errors
  670. for key in keys(a:options)
  671. if !has_key(err, key) || empty(err[key])
  672. let err[key] = a:options[key]
  673. endif
  674. endfor
  675. endfor
  676. return a:errors
  677. endfunction " }}}2
  678. function! s:_os_name() abort " {{{2
  679. return g:_SYNTASTIC_UNAME
  680. endfunction " }}}2
  681. " }}}1
  682. " vim: set sw=4 sts=4 et fdm=marker: