syntastic.vim 27 KB

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