sml.vim 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. " Vim indent file
  2. " Language: SML
  3. " Maintainer: Saikat Guha <sg266@cornell.edu>
  4. " Hubert Chao <hc85@cornell.edu>
  5. " Original OCaml Version:
  6. " Jean-Francois Yuen <jfyuen@ifrance.com>
  7. " Mike Leary <leary@nwlink.com>
  8. " Markus Mottl <markus@oefai.at>
  9. " OCaml URL: http://www.oefai.at/~markus/vim/indent/ocaml.vim
  10. " Last Change: 2022 Apr 06
  11. " 2002 Nov 06 - Some fixes (JY)
  12. " 2002 Oct 28 - Fixed bug with indentation of ']' (MM)
  13. " 2002 Oct 22 - Major rewrite (JY)
  14. " 2022 April: b:undo_indent added by Doug Kearns
  15. " Only load this indent file when no other was loaded.
  16. if exists("b:did_indent")
  17. finish
  18. endif
  19. let b:did_indent = 1
  20. setlocal expandtab
  21. setlocal indentexpr=GetSMLIndent()
  22. setlocal indentkeys+=0=and,0=else,0=end,0=handle,0=if,0=in,0=let,0=then,0=val,0=fun,0=\|,0=*),0)
  23. setlocal nolisp
  24. setlocal nosmartindent
  25. setlocal textwidth=80
  26. setlocal shiftwidth=2
  27. let b:undo_indent = "setl et< inde< indk< lisp< si< sw< tw<"
  28. " Comment formatting
  29. if (has("comments"))
  30. set comments=sr:(*,mb:*,ex:*)
  31. set fo=cqort
  32. endif
  33. " Only define the function once.
  34. "if exists("*GetSMLIndent")
  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 = '\<\%(let\|sig\|struct\)\>'
  43. let s:obj = '^\s*\(constraint\|inherit\|initializer\|method\|val\)\>\|\<\(object\|object\s*(.*)\)\s*$'
  44. let s:type = '^\s*\%(let\|type\)\>.*='
  45. let s:val = '^\s*\(val\|external\)\>.*:'
  46. " Skipping pattern, for comments
  47. function! s:SkipPattern(lnum, pat)
  48. let def = prevnonblank(a:lnum - 1)
  49. while def > 0 && getline(def) =~ a:pat
  50. let def = prevnonblank(def - 1)
  51. endwhile
  52. return def
  53. endfunction
  54. " Indent for ';;' to match multiple 'let'
  55. function! s:GetInd(lnum, pat, lim)
  56. let llet = search(a:pat, 'bW')
  57. let old = indent(a:lnum)
  58. while llet > 0
  59. let old = indent(llet)
  60. let nb = s:SkipPattern(llet, '^\s*(\*.*\*)\s*$')
  61. if getline(nb) =~ a:lim
  62. return old
  63. endif
  64. let llet = search(a:pat, 'bW')
  65. endwhile
  66. return old
  67. endfunction
  68. " Indent pairs
  69. function! s:FindPair(pstart, pmid, pend)
  70. call search(a:pend, 'bW')
  71. " return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"'))
  72. let lno = searchpair(a:pstart, a:pmid, a:pend, 'bW', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"')
  73. if lno == -1
  74. return indent(lno)
  75. else
  76. return col(".") - 1
  77. endif
  78. endfunction
  79. function! s:FindLet(pstart, pmid, pend)
  80. call search(a:pend, 'bW')
  81. " return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"'))
  82. let lno = searchpair(a:pstart, a:pmid, a:pend, 'bW', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"')
  83. let moduleLine = getline(lno)
  84. if lno == -1 || moduleLine =~ '^\s*\(fun\|structure\|signature\)\>'
  85. return indent(lno)
  86. else
  87. return col(".") - 1
  88. endif
  89. endfunction
  90. " Indent 'let'
  91. "function! s:FindLet(pstart, pmid, pend)
  92. " call search(a:pend, 'bW')
  93. " 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*let\\>.*=\\s*$\\|" . s:beflet'))
  94. "endfunction
  95. function! GetSMLIndent()
  96. " Find a non-blank line above the current line.
  97. let lnum = prevnonblank(v:lnum - 1)
  98. " At the start of the file use zero indent.
  99. if lnum == 0
  100. return 0
  101. endif
  102. let ind = indent(lnum)
  103. let lline = getline(lnum)
  104. " Return double 'shiftwidth' after lines matching:
  105. if lline =~ '^\s*|.*=>\s*$'
  106. return ind + 2 *shiftwidth()
  107. elseif lline =~ '^\s*val\>.*=\s*$'
  108. return ind + shiftwidth()
  109. endif
  110. let line = getline(v:lnum)
  111. " Indent lines starting with 'end' to matching module
  112. if line =~ '^\s*end\>'
  113. return s:FindLet(s:module, '', '\<end\>')
  114. " Match 'else' with 'if'
  115. elseif line =~ '^\s*else\>'
  116. if lline !~ '^\s*\(if\|else\|then\)\>'
  117. return s:FindPair('\<if\>', '', '\<then\>')
  118. else
  119. return ind
  120. endif
  121. " Match 'then' with 'if'
  122. elseif line =~ '^\s*then\>'
  123. if lline !~ '^\s*\(if\|else\|then\)\>'
  124. return s:FindPair('\<if\>', '', '\<then\>')
  125. else
  126. return ind
  127. endif
  128. " Indent if current line begins with ']'
  129. elseif line =~ '^\s*\]'
  130. return s:FindPair('\[','','\]')
  131. " Indent current line starting with 'in' to last matching 'let'
  132. elseif line =~ '^\s*in\>'
  133. let ind = s:FindLet('\<let\>','','\<in\>')
  134. " Indent from last matching module if line matches:
  135. elseif line =~ '^\s*\(fun\|val\|open\|structure\|and\|datatype\|type\|exception\)\>'
  136. cursor(lnum,1)
  137. let lastModule = indent(searchpair(s:module, '', '\<end\>', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"'))
  138. if lastModule == -1
  139. return 0
  140. else
  141. return lastModule + shiftwidth()
  142. endif
  143. " Indent lines starting with '|' from matching 'case', 'handle'
  144. elseif line =~ '^\s*|'
  145. " cursor(lnum,1)
  146. let lastSwitch = search('\<\(case\|handle\|fun\|datatype\)\>','bW')
  147. let switchLine = getline(lastSwitch)
  148. let switchLineIndent = indent(lastSwitch)
  149. if lline =~ '^\s*|'
  150. return ind
  151. endif
  152. if switchLine =~ '\<case\>'
  153. return col(".") + 2
  154. elseif switchLine =~ '\<handle\>'
  155. return switchLineIndent + shiftwidth()
  156. elseif switchLine =~ '\<datatype\>'
  157. call search('=')
  158. return col(".") - 1
  159. else
  160. return switchLineIndent + 2
  161. endif
  162. " Indent if last line ends with 'sig', 'struct', 'let', 'then', 'else',
  163. " 'in'
  164. elseif lline =~ '\<\(sig\|struct\|let\|in\|then\|else\)\s*$'
  165. let ind = ind + shiftwidth()
  166. " Indent if last line ends with 'of', align from 'case'
  167. elseif lline =~ '\<\(of\)\s*$'
  168. call search('\<case\>',"bW")
  169. let ind = col(".")+4
  170. " Indent if current line starts with 'of'
  171. elseif line =~ '^\s*of\>'
  172. call search('\<case\>',"bW")
  173. let ind = col(".")+1
  174. " Indent if last line starts with 'fun', 'case', 'fn'
  175. elseif lline =~ '^\s*\(fun\|fn\|case\)\>'
  176. let ind = ind + shiftwidth()
  177. endif
  178. " Don't indent 'let' if last line started with 'fun', 'fn'
  179. if line =~ '^\s*let\>'
  180. if lline =~ '^\s*\(fun\|fn\)'
  181. let ind = ind - shiftwidth()
  182. endif
  183. endif
  184. return ind
  185. endfunction
  186. " vim:sw=2