camlp4o.vim 4.3 KB

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