sh.vim 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "============================================================================
  2. "File: sh.vim
  3. "Description: Syntax checking plugin for syntastic
  4. "Maintainer: Gregor Uhlenheuer <kongo2002 at gmail dot com>
  5. "License: This program is free software. It comes without any warranty,
  6. " to the extent permitted by applicable law. You can redistribute
  7. " it and/or modify it under the terms of the Do What The Fuck You
  8. " Want To Public License, Version 2, as published by Sam Hocevar.
  9. " See http://sam.zoy.org/wtfpl/COPYING for more details.
  10. "
  11. "============================================================================
  12. if exists('g:loaded_syntastic_sh_sh_checker')
  13. finish
  14. endif
  15. let g:loaded_syntastic_sh_sh_checker = 1
  16. let s:save_cpo = &cpo
  17. set cpo&vim
  18. function! SyntaxCheckers_sh_sh_IsAvailable() dict " {{{1
  19. let buf = bufnr('')
  20. call self.log('shell =', s:GetShell(buf))
  21. return s:IsShellValid(buf)
  22. endfunction " }}}1
  23. function! SyntaxCheckers_sh_sh_GetLocList() dict " {{{1
  24. let buf = bufnr('')
  25. if s:GetShell(buf) ==# 'zsh'
  26. return s:ForwardToZshChecker()
  27. endif
  28. if !s:IsShellValid(buf)
  29. return []
  30. endif
  31. let makeprg = self.makeprgBuild({
  32. \ 'exe': s:GetShell(buf),
  33. \ 'args_after': '-n' })
  34. let errorformat =
  35. \ '%f: %tarning: line %l: %m,' .
  36. \ '%f: line %l: %m,' .
  37. \ '%f: syntax error at line %l: %m,' .
  38. \ '%f[%l]: %m'
  39. return SyntasticMake({
  40. \ 'makeprg': makeprg,
  41. \ 'errorformat': errorformat })
  42. endfunction " }}}1
  43. " Utilities {{{1
  44. function! s:GetShell(buf) " {{{2
  45. let shell = syntastic#util#getbufvar(a:buf, 'shell')
  46. if shell ==# ''
  47. let shebang = syntastic#util#parseShebang(a:buf)['exe']
  48. if shebang !=# ''
  49. if shebang[-strlen('bash'):-1] ==# 'bash'
  50. let shell = 'bash'
  51. elseif shebang[-strlen('zsh'):-1] ==# 'zsh'
  52. let shell = 'zsh'
  53. elseif shebang[-strlen('sh'):-1] ==# 'sh'
  54. let shell = 'sh'
  55. endif
  56. endif
  57. " try to use env variable in case no shebang could be found
  58. if shell ==# ''
  59. let shell = fnamemodify($SHELL, ':t')
  60. endif
  61. endif
  62. return shell
  63. endfunction " }}}2
  64. function! s:IsShellValid(buf) " {{{2
  65. let shell = s:GetShell(a:buf)
  66. return shell !=# '' && executable(shell)
  67. endfunction " }}}2
  68. function! s:ForwardToZshChecker() " {{{2
  69. let registry = g:SyntasticRegistry.Instance()
  70. let zsh_checkers = registry.getCheckersAvailable('zsh', ['zsh'])
  71. if !empty(zsh_checkers)
  72. return zsh_checkers[0].getLocListRaw()
  73. else
  74. return []
  75. endif
  76. endfunction " }}}2
  77. " }}}1
  78. call g:SyntasticRegistry.CreateAndRegisterChecker({
  79. \ 'filetype': 'sh',
  80. \ 'name': 'sh' })
  81. let &cpo = s:save_cpo
  82. unlet s:save_cpo
  83. " vim: set sw=4 sts=4 et fdm=marker: