ocaml.vim 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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: https://github.com/ocaml/vim-ocaml
  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. let b:undo_indent = "setl et< inde< indk< lisp< si<"
  25. " At least Marc Weber and Markus Mottl do not like this:
  26. " setlocal textwidth=80
  27. " Comment formatting
  28. if !exists("no_ocaml_comments")
  29. if (has("comments"))
  30. setlocal comments=sr:(*\ ,mb:\ ,ex:*)
  31. setlocal comments^=sr:(**,mb:\ \ ,ex:*)
  32. setlocal fo=cqort
  33. let b:undo_indent .= " | setl com< fo<"
  34. endif
  35. endif
  36. " Only define the function once.
  37. if exists("*GetOCamlIndent")
  38. finish
  39. endif
  40. " Define some patterns:
  41. let s:beflet = '^\s*\(initializer\|method\|try\)\|\(\<\(begin\|do\|else\|in\|then\|try\)\|->\|<-\|=\|;\|(\)\s*$'
  42. let s:letpat = '^\s*\(let\|type\|module\|class\|open\|exception\|val\|include\|external\)\>'
  43. let s:letlim = '\(\<\(sig\|struct\)\|;;\)\s*$'
  44. let s:lim = '^\s*\(exception\|external\|include\|let\|module\|open\|type\|val\)\>'
  45. let s:module = '\<\%(begin\|sig\|struct\|object\)\>'
  46. let s:obj = '^\s*\(constraint\|inherit\|initializer\|method\|val\)\>\|\<\(object\|object\s*(.*)\)\s*$'
  47. let s:type = '^\s*\%(class\|let\|type\)\>.*='
  48. " Skipping pattern, for comments
  49. function! s:GetLineWithoutFullComment(lnum)
  50. let lnum = prevnonblank(a:lnum - 1)
  51. let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
  52. while lline =~ '^\s*$' && lnum > 0
  53. let lnum = prevnonblank(lnum - 1)
  54. let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
  55. endwhile
  56. return lnum
  57. endfunction
  58. " Indent for ';;' to match multiple 'let'
  59. function! s:GetInd(lnum, pat, lim)
  60. let llet = search(a:pat, 'bW')
  61. let old = indent(a:lnum)
  62. while llet > 0
  63. let old = indent(llet)
  64. let nb = s:GetLineWithoutFullComment(llet)
  65. if getline(nb) =~ a:lim
  66. return old
  67. endif
  68. let llet = search(a:pat, 'bW')
  69. endwhile
  70. return old
  71. endfunction
  72. " Indent pairs
  73. function! s:FindPair(pstart, pmid, pend)
  74. call search(a:pend, 'bW')
  75. return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"'))
  76. endfunction
  77. " Indent 'let'
  78. function! s:FindLet(pstart, pmid, pend)
  79. call search(a:pend, 'bW')
  80. 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'))
  81. endfunction
  82. function! GetOCamlIndent()
  83. " Find a non-commented line above the current line.
  84. let lnum = s:GetLineWithoutFullComment(v:lnum)
  85. " At the start of the file use zero indent.
  86. if lnum == 0
  87. return 0
  88. endif
  89. let ind = indent(lnum)
  90. let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
  91. " Return double 'shiftwidth' after lines matching:
  92. if lline =~ '^\s*|.*->\s*$'
  93. return ind + 2 * shiftwidth()
  94. endif
  95. let line = getline(v:lnum)
  96. " Indent if current line begins with 'end':
  97. if line =~ '^\s*end\>'
  98. return s:FindPair(s:module, '','\<end\>')
  99. " Indent if current line begins with 'done' for 'do':
  100. elseif line =~ '^\s*done\>'
  101. return s:FindPair('\<do\>', '','\<done\>')
  102. " Indent if current line begins with '}' or '>}':
  103. elseif line =~ '^\s*\(\|>\)}'
  104. return s:FindPair('{', '','}')
  105. " Indent if current line begins with ']', '|]' or '>]':
  106. elseif line =~ '^\s*\(\||\|>\)\]'
  107. return s:FindPair('\[', '','\]')
  108. " Indent if current line begins with ')':
  109. elseif line =~ '^\s*)'
  110. return s:FindPair('(', '',')')
  111. " Indent if current line begins with 'let':
  112. elseif line =~ '^\s*let\>'
  113. if lline !~ s:lim . '\|' . s:letlim . '\|' . s:beflet
  114. return s:FindLet(s:type, '','\<let\s*$')
  115. endif
  116. " Indent if current line begins with 'class' or 'type':
  117. elseif line =~ '^\s*\(class\|type\)\>'
  118. if lline !~ s:lim . '\|\<and\s*$\|' . s:letlim
  119. return s:FindLet(s:type, '','\<\(class\|type\)\s*$')
  120. endif
  121. " Indent for pattern matching:
  122. elseif line =~ '^\s*|'
  123. if lline !~ '^\s*\(|[^\]]\|\(match\|type\|with\)\>\)\|\<\(function\|parser\|private\|with\)\s*$'
  124. call search('|', 'bW')
  125. return indent(searchpair('^\s*\(match\|type\)\>\|\<\(function\|parser\|private\|with\)\s*$', '', '^\s*|', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment" || getline(".") !~ "^\\s*|.*->"'))
  126. endif
  127. " Indent if current line begins with ';;':
  128. elseif line =~ '^\s*;;'
  129. if lline !~ ';;\s*$'
  130. return s:GetInd(v:lnum, s:letpat, s:letlim)
  131. endif
  132. " Indent if current line begins with 'in':
  133. elseif line =~ '^\s*in\>'
  134. if lline !~ '^\s*\(let\|and\)\>'
  135. return s:FindPair('\<let\>', '', '\<in\>')
  136. endif
  137. " Indent if current line begins with 'else':
  138. elseif line =~ '^\s*else\>'
  139. if lline !~ '^\s*\(if\|then\)\>'
  140. return s:FindPair('\<if\>', '', '\<else\>')
  141. endif
  142. " Indent if current line begins with 'then':
  143. elseif line =~ '^\s*then\>'
  144. if lline !~ '^\s*\(if\|else\)\>'
  145. return s:FindPair('\<if\>', '', '\<then\>')
  146. endif
  147. " Indent if current line begins with 'and':
  148. elseif line =~ '^\s*and\>'
  149. if lline !~ '^\s*\(and\|let\|type\)\>\|\<end\s*$'
  150. return ind - shiftwidth()
  151. endif
  152. " Indent if current line begins with 'with':
  153. elseif line =~ '^\s*with\>'
  154. if lline !~ '^\s*\(match\|try\)\>'
  155. return s:FindPair('\<\%(match\|try\)\>', '','\<with\>')
  156. endif
  157. " Indent if current line begins with 'exception', 'external', 'include' or
  158. " 'open':
  159. elseif line =~ '^\s*\(exception\|external\|include\|open\)\>'
  160. if lline !~ s:lim . '\|' . s:letlim
  161. call search(line)
  162. return indent(search('^\s*\(\(exception\|external\|include\|open\|type\)\>\|val\>.*:\)', 'bW'))
  163. endif
  164. " Indent if current line begins with 'val':
  165. elseif line =~ '^\s*val\>'
  166. if lline !~ '^\s*\(exception\|external\|include\|open\)\>\|' . s:obj . '\|' . s:letlim
  167. return indent(search('^\s*\(\(exception\|include\|initializer\|method\|open\|type\|val\)\>\|external\>.*:\)', 'bW'))
  168. endif
  169. " Indent if current line begins with 'constraint', 'inherit', 'initializer'
  170. " or 'method':
  171. elseif line =~ '^\s*\(constraint\|inherit\|initializer\|method\)\>'
  172. if lline !~ s:obj
  173. return indent(search('\<\(object\|object\s*(.*)\)\s*$', 'bW')) + shiftwidth()
  174. endif
  175. endif
  176. " Add a 'shiftwidth' after lines ending with:
  177. if lline =~ '\(:\|=\|->\|<-\|(\|\[\|{\|{<\|\[|\|\[<\|\<\(begin\|do\|else\|fun\|function\|functor\|if\|initializer\|object\|parser\|private\|sig\|struct\|then\|try\)\|\<object\s*(.*)\)\s*$'
  178. let ind = ind + shiftwidth()
  179. " Back to normal indent after lines ending with ';;':
  180. elseif lline =~ ';;\s*$' && lline !~ '^\s*;;'
  181. let ind = s:GetInd(v:lnum, s:letpat, s:letlim)
  182. " Back to normal indent after lines ending with 'end':
  183. elseif lline =~ '\<end\s*$'
  184. let ind = s:FindPair(s:module, '','\<end\>')
  185. " Back to normal indent after lines ending with 'in':
  186. elseif lline =~ '\<in\s*$' && lline !~ '^\s*in\>'
  187. let ind = s:FindPair('\<let\>', '', '\<in\>')
  188. " Back to normal indent after lines ending with 'done':
  189. elseif lline =~ '\<done\s*$'
  190. let ind = s:FindPair('\<do\>', '','\<done\>')
  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 lines ending with ']', '|]' or '>]':
  195. elseif lline =~ '\(\||\|>\)\]\s*$'
  196. let ind = s:FindPair('\[', '','\]')
  197. " Back to normal indent after comments:
  198. elseif lline =~ '\*)\s*$'
  199. call search('\*)', 'bW')
  200. let ind = indent(searchpair('(\*', '', '\*)', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))
  201. " Back to normal indent after lines ending with ')':
  202. elseif lline =~ ')\s*$'
  203. let ind = s:FindPair('(', '',')')
  204. " If this is a multiline comment then align '*':
  205. elseif lline =~ '^\s*(\*' && line =~ '^\s*\*'
  206. let ind = ind + 1
  207. else
  208. " Don't change indentation of this line
  209. " for new lines (indent==0) use indentation of previous line
  210. " This is for preventing removing indentation of these args:
  211. " let f x =
  212. " let y = x + 1 in
  213. " Printf.printf
  214. " "o" << here
  215. " "oeuth" << don't touch indentation
  216. let i = indent(v:lnum)
  217. return i == 0 ? ind : i
  218. endif
  219. " Subtract a 'shiftwidth' after lines matching 'match ... with parser':
  220. if lline =~ '\<match\>.*\<with\>\s*\<parser\s*$'
  221. let ind = ind - shiftwidth()
  222. endif
  223. return ind
  224. endfunction
  225. " vim:sw=2