sml.vim 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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: 2003 Jan 04 - Adapted to SML
  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. " 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=GetSMLIndent()
  21. 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)
  22. setlocal nolisp
  23. setlocal nosmartindent
  24. setlocal textwidth=80
  25. setlocal shiftwidth=2
  26. " Comment formatting
  27. if (has("comments"))
  28. set comments=sr:(*,mb:*,ex:*)
  29. set fo=cqort
  30. endif
  31. " Only define the function once.
  32. "if exists("*GetSMLIndent")
  33. "finish
  34. "endif
  35. " Define some patterns:
  36. let s:beflet = '^\s*\(initializer\|method\|try\)\|\(\<\(begin\|do\|else\|in\|then\|try\)\|->\|;\)\s*$'
  37. let s:letpat = '^\s*\(let\|type\|module\|class\|open\|exception\|val\|include\|external\)\>'
  38. let s:letlim = '\(\<\(sig\|struct\)\|;;\)\s*$'
  39. let s:lim = '^\s*\(exception\|external\|include\|let\|module\|open\|type\|val\)\>'
  40. let s:module = '\<\%(let\|sig\|struct\)\>'
  41. let s:obj = '^\s*\(constraint\|inherit\|initializer\|method\|val\)\>\|\<\(object\|object\s*(.*)\)\s*$'
  42. let s:type = '^\s*\%(let\|type\)\>.*='
  43. let s:val = '^\s*\(val\|external\)\>.*:'
  44. " Skipping pattern, for comments
  45. function! s:SkipPattern(lnum, pat)
  46. let def = prevnonblank(a:lnum - 1)
  47. while def > 0 && getline(def) =~ a:pat
  48. let def = prevnonblank(def - 1)
  49. endwhile
  50. return def
  51. endfunction
  52. " Indent for ';;' to match multiple 'let'
  53. function! s:GetInd(lnum, pat, lim)
  54. let llet = search(a:pat, 'bW')
  55. let old = indent(a:lnum)
  56. while llet > 0
  57. let old = indent(llet)
  58. let nb = s:SkipPattern(llet, '^\s*(\*.*\*)\s*$')
  59. if getline(nb) =~ a:lim
  60. return old
  61. endif
  62. let llet = search(a:pat, 'bW')
  63. endwhile
  64. return old
  65. endfunction
  66. " Indent pairs
  67. function! s:FindPair(pstart, pmid, pend)
  68. call search(a:pend, 'bW')
  69. " return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"'))
  70. let lno = searchpair(a:pstart, a:pmid, a:pend, 'bW', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"')
  71. if lno == -1
  72. return indent(lno)
  73. else
  74. return col(".") - 1
  75. endif
  76. endfunction
  77. function! s:FindLet(pstart, pmid, pend)
  78. call search(a:pend, 'bW')
  79. " return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"'))
  80. let lno = searchpair(a:pstart, a:pmid, a:pend, 'bW', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"')
  81. let moduleLine = getline(lno)
  82. if lno == -1 || moduleLine =~ '^\s*\(fun\|structure\|signature\)\>'
  83. return indent(lno)
  84. else
  85. return col(".") - 1
  86. endif
  87. endfunction
  88. " Indent 'let'
  89. "function! s:FindLet(pstart, pmid, pend)
  90. " call search(a:pend, 'bW')
  91. " 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'))
  92. "endfunction
  93. function! GetSMLIndent()
  94. " Find a non-blank line above the current line.
  95. let lnum = prevnonblank(v:lnum - 1)
  96. " At the start of the file use zero indent.
  97. if lnum == 0
  98. return 0
  99. endif
  100. let ind = indent(lnum)
  101. let lline = getline(lnum)
  102. " Return double 'shiftwidth' after lines matching:
  103. if lline =~ '^\s*|.*=>\s*$'
  104. return ind + 2 *shiftwidth()
  105. elseif lline =~ '^\s*val\>.*=\s*$'
  106. return ind + shiftwidth()
  107. endif
  108. let line = getline(v:lnum)
  109. " Indent lines starting with 'end' to matching module
  110. if line =~ '^\s*end\>'
  111. return s:FindLet(s:module, '', '\<end\>')
  112. " Match 'else' with 'if'
  113. elseif line =~ '^\s*else\>'
  114. if lline !~ '^\s*\(if\|else\|then\)\>'
  115. return s:FindPair('\<if\>', '', '\<then\>')
  116. else
  117. return ind
  118. endif
  119. " Match 'then' with 'if'
  120. elseif line =~ '^\s*then\>'
  121. if lline !~ '^\s*\(if\|else\|then\)\>'
  122. return s:FindPair('\<if\>', '', '\<then\>')
  123. else
  124. return ind
  125. endif
  126. " Indent if current line begins with ']'
  127. elseif line =~ '^\s*\]'
  128. return s:FindPair('\[','','\]')
  129. " Indent current line starting with 'in' to last matching 'let'
  130. elseif line =~ '^\s*in\>'
  131. let ind = s:FindLet('\<let\>','','\<in\>')
  132. " Indent from last matching module if line matches:
  133. elseif line =~ '^\s*\(fun\|val\|open\|structure\|and\|datatype\|type\|exception\)\>'
  134. cursor(lnum,1)
  135. let lastModule = indent(searchpair(s:module, '', '\<end\>', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"'))
  136. if lastModule == -1
  137. return 0
  138. else
  139. return lastModule + shiftwidth()
  140. endif
  141. " Indent lines starting with '|' from matching 'case', 'handle'
  142. elseif line =~ '^\s*|'
  143. " cursor(lnum,1)
  144. let lastSwitch = search('\<\(case\|handle\|fun\|datatype\)\>','bW')
  145. let switchLine = getline(lastSwitch)
  146. let switchLineIndent = indent(lastSwitch)
  147. if lline =~ '^\s*|'
  148. return ind
  149. endif
  150. if switchLine =~ '\<case\>'
  151. return col(".") + 2
  152. elseif switchLine =~ '\<handle\>'
  153. return switchLineIndent + shiftwidth()
  154. elseif switchLine =~ '\<datatype\>'
  155. call search('=')
  156. return col(".") - 1
  157. else
  158. return switchLineIndent + 2
  159. endif
  160. " Indent if last line ends with 'sig', 'struct', 'let', 'then', 'else',
  161. " 'in'
  162. elseif lline =~ '\<\(sig\|struct\|let\|in\|then\|else\)\s*$'
  163. let ind = ind + shiftwidth()
  164. " Indent if last line ends with 'of', align from 'case'
  165. elseif lline =~ '\<\(of\)\s*$'
  166. call search('\<case\>',"bW")
  167. let ind = col(".")+4
  168. " Indent if current line starts with 'of'
  169. elseif line =~ '^\s*of\>'
  170. call search('\<case\>',"bW")
  171. let ind = col(".")+1
  172. " Indent if last line starts with 'fun', 'case', 'fn'
  173. elseif lline =~ '^\s*\(fun\|fn\|case\)\>'
  174. let ind = ind + shiftwidth()
  175. endif
  176. " Don't indent 'let' if last line started with 'fun', 'fn'
  177. if line =~ '^\s*let\>'
  178. if lline =~ '^\s*\(fun\|fn\)'
  179. let ind = ind - shiftwidth()
  180. endif
  181. endif
  182. return ind
  183. endfunction
  184. " vim:sw=2