syntastic.vim 23 KB

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