camlp4o.vim 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. "============================================================================
  2. "File: ocaml.vim
  3. "Description: Syntax checking plugin for syntastic.vim
  4. "Maintainer: Török Edwin <edwintorok 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_ocaml_camlp4o_checker')
  13. finish
  14. endif
  15. let g:loaded_syntastic_ocaml_camlp4o_checker = 1
  16. if exists('g:syntastic_ocaml_camlp4r') && g:syntastic_ocaml_camlp4r != 0
  17. let s:ocamlpp='camlp4r'
  18. else
  19. let s:ocamlpp='camlp4o'
  20. endif
  21. let s:save_cpo = &cpo
  22. set cpo&vim
  23. " Checker options {{{1
  24. if !exists('g:syntastic_ocaml_use_ocamlc') || !executable('ocamlc')
  25. let g:syntastic_ocaml_use_ocamlc = 0
  26. endif
  27. if !exists('g:syntastic_ocaml_use_janestreet_core')
  28. let g:syntastic_ocaml_use_janestreet_core = 0
  29. endif
  30. if !exists('g:syntastic_ocaml_use_ocamlbuild') || !executable('ocamlbuild')
  31. let g:syntastic_ocaml_use_ocamlbuild = 0
  32. endif
  33. " }}}1
  34. function! SyntaxCheckers_ocaml_camlp4o_IsAvailable() dict " {{{1
  35. return executable(s:ocamlpp)
  36. endfunction " }}}1
  37. function! SyntaxCheckers_ocaml_camlp4o_GetLocList() dict " {{{1
  38. let makeprg = s:GetMakeprg()
  39. if makeprg ==# ''
  40. return []
  41. endif
  42. let errorformat =
  43. \ '%WWarning: File "%f"\, line %l\, chars %c-%n:,'.
  44. \ '%WWarning: line %l\, chars %c-%n:,'.
  45. \ '%AFile "%f"\, line %l\, characters %c-%n:,'.
  46. \ '%AFile "%f"\, line %l\, characters %c-%*\d (end at line %*\d\, character %*\d):,'.
  47. \ '%AFile "%f"\, line %l\, character %c:,'.
  48. \ '%AFile "%f"\, line %l\, character %c:%m,'.
  49. \ '%-GPreprocessing error %.%#,'.
  50. \ '%-GCommand exited %.%#,'.
  51. \ '%C%tarning %*\d: %m,'.
  52. \ '%C%m,'.
  53. \ '%-G+%.%#'
  54. let loclist = SyntasticMake({
  55. \ 'makeprg': makeprg,
  56. \ 'errorformat': errorformat,
  57. \ 'defaults': {'bufnr': bufnr('')} })
  58. for e in loclist
  59. if get(e, 'col', 0) && get(e, 'nr', 0)
  60. let e['hl'] = '\%>' . (e['col'] - 1) . 'c\%<' . (e['nr'] + 1) . 'c'
  61. let e['nr'] = 0
  62. endif
  63. endfor
  64. return loclist
  65. endfunction " }}}1
  66. " Utilities {{{1
  67. function! s:GetMakeprg() " {{{2
  68. if g:syntastic_ocaml_use_ocamlc
  69. return s:GetOcamlcMakeprg()
  70. endif
  71. if g:syntastic_ocaml_use_ocamlbuild && isdirectory('_build')
  72. return s:GetOcamlBuildMakeprg()
  73. endif
  74. return s:GetOtherMakeprg()
  75. endfunction " }}}2
  76. function! s:GetOcamlcMakeprg() " {{{2
  77. if g:syntastic_ocaml_use_janestreet_core
  78. let build_cmd = 'ocamlc -I '
  79. let build_cmd .= expand(g:syntastic_ocaml_janestreet_core_dir, 1)
  80. let build_cmd .= ' -c ' . syntastic#util#shexpand('%')
  81. return build_cmd
  82. else
  83. return 'ocamlc -c ' . syntastic#util#shexpand('%')
  84. endif
  85. endfunction " }}}2
  86. function! s:GetOcamlBuildMakeprg() " {{{2
  87. return 'ocamlbuild -quiet -no-log -tag annot,' . s:ocamlpp . ' -no-links -no-hygiene -no-sanitize ' .
  88. \ syntastic#util#shexpand('%:r') . '.cmi'
  89. endfunction " }}}2
  90. function! s:GetOtherMakeprg() " {{{2
  91. "TODO: give this function a better name?
  92. "
  93. "TODO: should use throw/catch instead of returning an empty makeprg
  94. let extension = expand('%:e', 1)
  95. let makeprg = ''
  96. if stridx(extension, 'mly') >= 0 && executable('menhir')
  97. " ocamlyacc output can't be redirected, so use menhir
  98. let makeprg = 'menhir --only-preprocess ' . syntastic#util#shexpand('%') . ' >' . syntastic#util#DevNull()
  99. elseif stridx(extension,'mll') >= 0 && executable('ocamllex')
  100. let makeprg = 'ocamllex -q ' . syntastic#c#NullOutput() . ' ' . syntastic#util#shexpand('%')
  101. else
  102. let makeprg = 'camlp4o ' . syntastic#c#NullOutput() . ' ' . syntastic#util#shexpand('%')
  103. endif
  104. return makeprg
  105. endfunction " }}}2
  106. " }}}1
  107. call g:SyntasticRegistry.CreateAndRegisterChecker({
  108. \ 'filetype': 'ocaml',
  109. \ 'name': 'camlp4o'})
  110. let &cpo = s:save_cpo
  111. unlet s:save_cpo
  112. " vim: set sw=4 sts=4 et fdm=marker: