ocaml.vim 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. " Vim indent file
  2. " Language: OCaml
  3. " Maintainers: Jean-Francois Yuen <jfyuen@happycoders.org>
  4. " Mike Leary <leary@nwlink.com>
  5. " Markus Mottl <markus.mottl@gmail.com>
  6. " URL: http://www.ocaml.info/vim/indent/ocaml.vim
  7. " Last Change: 2017 Jun 13
  8. " 2005 Jun 25 - Fixed multiple bugs due to 'else\nreturn ind' working
  9. " 2005 May 09 - Added an option to not indent OCaml-indents specially (MM)
  10. " 2013 June - commented textwidth (Marc Weber)
  11. "
  12. " Marc Weber's comment: This file may contain a lot of (very custom) stuff
  13. " which eventually should be moved somewhere else ..
  14. " Only load this indent file when no other was loaded.
  15. if exists("b:did_indent")
  16. finish
  17. endif
  18. let b:did_indent = 1
  19. setlocal expandtab
  20. setlocal indentexpr=GetOCamlIndent()
  21. setlocal indentkeys+=0=and,0=class,0=constraint,0=done,0=else,0=end,0=exception,0=external,0=if,0=in,0=include,0=inherit,0=initializer,0=let,0=method,0=open,0=then,0=type,0=val,0=with,0;;,0>\],0\|\],0>},0\|,0},0\],0)
  22. setlocal nolisp
  23. setlocal nosmartindent
  24. " At least Marc Weber and Markus Mottl do not like this:
  25. " setlocal textwidth=80
  26. " Comment formatting
  27. if !exists("no_ocaml_comments")
  28. if (has("comments"))
  29. setlocal comments=sr:(*,mb:*,ex:*)
  30. setlocal fo=cqort
  31. endif
  32. endif
  33. " Only define the function once.
  34. if exists("*GetOCamlIndent")
  35. finish
  36. endif
  37. " Define some patterns:
  38. let s:beflet = '^\s*\(initializer\|method\|try\)\|\(\<\(begin\|do\|else\|in\|then\|try\)\|->\|<-\|=\|;\|(\)\s*$'
  39. let s:letpat = '^\s*\(let\|type\|module\|class\|open\|exception\|val\|include\|external\)\>'
  40. let s:letlim = '\(\<\(sig\|struct\)\|;;\)\s*$'
  41. let s:lim = '^\s*\(exception\|external\|include\|let\|module\|open\|type\|val\)\>'
  42. let s:module = '\<\%(begin\|sig\|struct\|object\)\>'
  43. let s:obj = '^\s*\(constraint\|inherit\|initializer\|method\|val\)\>\|\<\(object\|object\s*(.*)\)\s*$'
  44. let s:type = '^\s*\%(class\|let\|type\)\>.*='
  45. " Skipping pattern, for comments
  46. function! s:GetLineWithoutFullComment(lnum)
  47. let lnum = prevnonblank(a:lnum - 1)
  48. let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
  49. while lline =~ '^\s*$' && lnum > 0
  50. let lnum = prevnonblank(lnum - 1)
  51. let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
  52. endwhile
  53. return lnum
  54. endfunction
  55. " Indent for ';;' to match multiple 'let'
  56. function! s:GetInd(lnum, pat, lim)
  57. let llet = search(a:pat, 'bW')
  58. let old = indent(a:lnum)
  59. while llet > 0
  60. let old = indent(llet)
  61. let nb = s:GetLineWithoutFullComment(llet)
  62. if getline(nb) =~ a:lim
  63. return old
  64. endif
  65. let llet = search(a:pat, 'bW')
  66. endwhile
  67. return old
  68. endfunction
  69. " Indent pairs
  70. function! s:FindPair(pstart, pmid, pend)
  71. call search(a:pend, 'bW')
  72. return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"'))
  73. endfunction
  74. " Indent 'let'
  75. function! s:FindLet(pstart, pmid, pend)
  76. call search(a:pend, 'bW')
  77. return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment" || getline(".") =~ "^\\s*let\\>.*=.*\\<in\\s*$" || getline(prevnonblank(".") - 1) =~ s:beflet'))
  78. endfunction
  79. function! GetOCamlIndent()
  80. " Find a non-commented line above the current line.
  81. let lnum = s:GetLineWithoutFullComment(v:lnum)
  82. " At the start of the file use zero indent.
  83. if lnum == 0
  84. return 0
  85. endif
  86. let ind = indent(lnum)
  87. let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
  88. " Return double 'shiftwidth' after lines matching:
  89. if lline =~ '^\s*|.*->\s*$'
  90. return ind + 2 * shiftwidth()
  91. endif
  92. let line = getline(v:lnum)
  93. " Indent if current line begins with 'end':
  94. if line =~ '^\s*end\>'
  95. return s:FindPair(s:module, '','\<end\>')
  96. " Indent if current line begins with 'done' for 'do':
  97. elseif line =~ '^\s*done\>'
  98. return s:FindPair('\<do\>', '','\<done\>')
  99. " Indent if current line begins with '}' or '>}':
  100. elseif line =~ '^\s*\(\|>\)}'
  101. return s:FindPair('{', '','}')
  102. " Indent if current line begins with ']', '|]' or '>]':
  103. elseif line =~ '^\s*\(\||\|>\)\]'
  104. return s:FindPair('\[', '','\]')
  105. " Indent if current line begins with ')':
  106. elseif line =~ '^\s*)'
  107. return s:FindPair('(', '',')')
  108. " Indent if current line begins with 'let':
  109. elseif line =~ '^\s*let\>'
  110. if lline !~ s:lim . '\|' . s:letlim . '\|' . s:beflet
  111. return s:FindLet(s:type, '','\<let\s*$')
  112. endif
  113. " Indent if current line begins with 'class' or 'type':
  114. elseif line =~ '^\s*\(class\|type\)\>'
  115. if lline !~ s:lim . '\|\<and\s*$\|' . s:letlim
  116. return s:FindLet(s:type, '','\<\(class\|type\)\s*$')
  117. endif
  118. " Indent for pattern matching:
  119. elseif line =~ '^\s*|'
  120. if lline !~ '^\s*\(|[^\]]\|\(match\|type\|with\)\>\)\|\<\(function\|parser\|private\|with\)\s*$'
  121. call search('|', 'bW')
  122. return indent(searchpair('^\s*\(match\|type\)\>\|\<\(function\|parser\|private\|with\)\s*$', '', '^\s*|', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment" || getline(".") !~ "^\\s*|.*->"'))
  123. endif
  124. " Indent if current line begins with ';;':
  125. elseif line =~ '^\s*;;'
  126. if lline !~ ';;\s*$'
  127. return s:GetInd(v:lnum, s:letpat, s:letlim)
  128. endif
  129. " Indent if current line begins with 'in':
  130. elseif line =~ '^\s*in\>'
  131. if lline !~ '^\s*\(let\|and\)\>'
  132. return s:FindPair('\<let\>', '', '\<in\>')
  133. endif
  134. " Indent if current line begins with 'else':
  135. elseif line =~ '^\s*else\>'
  136. if lline !~ '^\s*\(if\|then\)\>'
  137. return s:FindPair('\<if\>', '', '\<else\>')
  138. endif
  139. " Indent if current line begins with 'then':
  140. elseif line =~ '^\s*then\>'
  141. if lline !~ '^\s*\(if\|else\)\>'
  142. return s:FindPair('\<if\>', '', '\<then\>')
  143. endif
  144. " Indent if current line begins with 'and':
  145. elseif line =~ '^\s*and\>'
  146. if lline !~ '^\s*\(and\|let\|type\)\>\|\<end\s*$'
  147. return ind - shiftwidth()
  148. endif
  149. " Indent if current line begins with 'with':
  150. elseif line =~ '^\s*with\>'
  151. if lline !~ '^\s*\(match\|try\)\>'
  152. return s:FindPair('\<\%(match\|try\)\>', '','\<with\>')
  153. endif
  154. " Indent if current line begins with 'exception', 'external', 'include' or
  155. " 'open':
  156. elseif line =~ '^\s*\(exception\|external\|include\|open\)\>'
  157. if lline !~ s:lim . '\|' . s:letlim
  158. call search(line)
  159. return indent(search('^\s*\(\(exception\|external\|include\|open\|type\)\>\|val\>.*:\)', 'bW'))
  160. endif
  161. " Indent if current line begins with 'val':
  162. elseif line =~ '^\s*val\>'
  163. if lline !~ '^\s*\(exception\|external\|include\|open\)\>\|' . s:obj . '\|' . s:letlim
  164. return indent(search('^\s*\(\(exception\|include\|initializer\|method\|open\|type\|val\)\>\|external\>.*:\)', 'bW'))
  165. endif
  166. " Indent if current line begins with 'constraint', 'inherit', 'initializer'
  167. " or 'method':
  168. elseif line =~ '^\s*\(constraint\|inherit\|initializer\|method\)\>'
  169. if lline !~ s:obj
  170. return indent(search('\<\(object\|object\s*(.*)\)\s*$', 'bW')) + shiftwidth()
  171. endif
  172. endif
  173. " Add a 'shiftwidth' after lines ending with:
  174. if lline =~ '\(:\|=\|->\|<-\|(\|\[\|{\|{<\|\[|\|\[<\|\<\(begin\|do\|else\|fun\|function\|functor\|if\|initializer\|object\|parser\|private\|sig\|struct\|then\|try\)\|\<object\s*(.*)\)\s*$'
  175. let ind = ind + shiftwidth()
  176. " Back to normal indent after lines ending with ';;':
  177. elseif lline =~ ';;\s*$' && lline !~ '^\s*;;'
  178. let ind = s:GetInd(v:lnum, s:letpat, s:letlim)
  179. " Back to normal indent after lines ending with 'end':
  180. elseif lline =~ '\<end\s*$'
  181. let ind = s:FindPair(s:module, '','\<end\>')
  182. " Back to normal indent after lines ending with 'in':
  183. elseif lline =~ '\<in\s*$' && lline !~ '^\s*in\>'
  184. let ind = s:FindPair('\<let\>', '', '\<in\>')
  185. " Back to normal indent after lines ending with 'done':
  186. elseif lline =~ '\<done\s*$'
  187. let ind = s:FindPair('\<do\>', '','\<done\>')
  188. " Back to normal indent after lines ending with '}' or '>}':
  189. elseif lline =~ '\(\|>\)}\s*$'
  190. let ind = s:FindPair('{', '','}')
  191. " Back to normal indent after lines ending with ']', '|]' or '>]':
  192. elseif lline =~ '\(\||\|>\)\]\s*$'
  193. let ind = s:FindPair('\[', '','\]')
  194. " Back to normal indent after comments:
  195. elseif lline =~ '\*)\s*$'
  196. call search('\*)', 'bW')
  197. let ind = indent(searchpair('(\*', '', '\*)', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))
  198. " Back to normal indent after lines ending with ')':
  199. elseif lline =~ ')\s*$'
  200. let ind = s:FindPair('(', '',')')
  201. " If this is a multiline comment then align '*':
  202. elseif lline =~ '^\s*(\*' && line =~ '^\s*\*'
  203. let ind = ind + 1
  204. else
  205. " Don't change indentation of this line
  206. " for new lines (indent==0) use indentation of previous line
  207. " This is for preventing removing indentation of these args:
  208. " let f x =
  209. " let y = x + 1 in
  210. " Printf.printf
  211. " "o" << here
  212. " "oeuth" << don't touch indentation
  213. let i = indent(v:lnum)
  214. return i == 0 ? ind : i
  215. endif
  216. " Subtract a 'shiftwidth' after lines matching 'match ... with parser':
  217. if lline =~ '\<match\>.*\<with\>\s*\<parser\s*$'
  218. let ind = ind - shiftwidth()
  219. endif
  220. return ind
  221. endfunction
  222. " vim:sw=2