camlp4o.vim 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "============================================================================
  2. "File: ocaml.vim
  3. "Description: Syntax checking plugin for syntastic
  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 buf = bufnr('')
  38. let makeprg = s:GetMakeprg(buf)
  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': buf} })
  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(buf) " {{{2
  68. return
  69. \ g:syntastic_ocaml_use_ocamlc ? g:syntastic_ocaml_use_ocamlc :
  70. \ (g:syntastic_ocaml_use_ocamlbuild && isdirectory('_build')) ? s:GetOcamlcMakeprg(a:buf) :
  71. \ s:GetOtherMakeprg(a:buf)
  72. endfunction " }}}2
  73. function! s:GetOcamlcMakeprg(buf) " {{{2
  74. let build_cmd = g:syntastic_ocaml_use_janestreet_core ?
  75. \ 'ocamlc -I ' . syntastic#util#shexpand(g:syntastic_ocaml_janestreet_core_dir) : 'ocamlc'
  76. let build_cmd .= ' -c ' . syntastic#util#shescape(bufname(a:buf))
  77. return build_cmd
  78. endfunction " }}}2
  79. function! s:GetOcamlBuildMakeprg(buf) " {{{2
  80. return 'ocamlbuild -quiet -no-log -tag annot,' . s:ocamlpp . ' -no-links -no-hygiene -no-sanitize ' .
  81. \ syntastic#util#shexpand(fnamemodify(bufname(a:buf), ':r')) . '.cmi'
  82. endfunction " }}}2
  83. function! s:GetOtherMakeprg(buf) " {{{2
  84. "TODO: give this function a better name?
  85. "
  86. "TODO: should use throw/catch instead of returning an empty makeprg
  87. let fname = bufname(a:buf)
  88. let extension = fnamemodify(fname, ':e')
  89. let makeprg = ''
  90. if stridx(extension, 'mly') >= 0 && executable('menhir')
  91. " ocamlyacc output can't be redirected, so use menhir
  92. let makeprg = 'menhir --only-preprocess ' . syntastic#util#shescape(fname) . ' >' . syntastic#util#DevNull()
  93. elseif stridx(extension,'mll') >= 0 && executable('ocamllex')
  94. let makeprg = 'ocamllex -q ' . syntastic#c#NullOutput() . ' ' . syntastic#util#shescape(fname)
  95. else
  96. let makeprg = 'camlp4o ' . syntastic#c#NullOutput() . ' ' . syntastic#util#shescape(fname)
  97. endif
  98. return makeprg
  99. endfunction " }}}2
  100. " }}}1
  101. call g:SyntasticRegistry.CreateAndRegisterChecker({
  102. \ 'filetype': 'ocaml',
  103. \ 'name': 'camlp4o'})
  104. let &cpo = s:save_cpo
  105. unlet s:save_cpo
  106. " vim: set sw=4 sts=4 et fdm=marker: