ocaml.vim 9.6 KB

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