blob: a0b0c3e91128d644620a6de7ad63ad1612e21ea4 [file] [log] [blame]
Bram Moolenaar1e015462005-09-25 22:16:38 +00001" 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
Bram Moolenaarcbaff5e2022-04-08 17:45:08 +010010" Last Change: 2022 Apr 06
Bram Moolenaar1e015462005-09-25 22:16:38 +000011" 2002 Nov 06 - Some fixes (JY)
12" 2002 Oct 28 - Fixed bug with indentation of ']' (MM)
13" 2002 Oct 22 - Major rewrite (JY)
Bram Moolenaarcbaff5e2022-04-08 17:45:08 +010014" 2022 April: b:undo_indent added by Doug Kearns
Bram Moolenaar1e015462005-09-25 22:16:38 +000015
16" Only load this indent file when no other was loaded.
17if exists("b:did_indent")
18 finish
19endif
20let b:did_indent = 1
21
22setlocal expandtab
23setlocal indentexpr=GetSMLIndent()
24setlocal indentkeys+=0=and,0=else,0=end,0=handle,0=if,0=in,0=let,0=then,0=val,0=fun,0=\|,0=*),0)
25setlocal nolisp
26setlocal nosmartindent
27setlocal textwidth=80
28setlocal shiftwidth=2
29
Bram Moolenaarcbaff5e2022-04-08 17:45:08 +010030let b:undo_indent = "setl et< inde< indk< lisp< si< sw< tw<"
31
Bram Moolenaar1e015462005-09-25 22:16:38 +000032" Comment formatting
33if (has("comments"))
34 set comments=sr:(*,mb:*,ex:*)
35 set fo=cqort
36endif
37
38" Only define the function once.
39"if exists("*GetSMLIndent")
40"finish
41"endif
42
43" Define some patterns:
44let s:beflet = '^\s*\(initializer\|method\|try\)\|\(\<\(begin\|do\|else\|in\|then\|try\)\|->\|;\)\s*$'
45let s:letpat = '^\s*\(let\|type\|module\|class\|open\|exception\|val\|include\|external\)\>'
46let s:letlim = '\(\<\(sig\|struct\)\|;;\)\s*$'
47let s:lim = '^\s*\(exception\|external\|include\|let\|module\|open\|type\|val\)\>'
48let s:module = '\<\%(let\|sig\|struct\)\>'
49let s:obj = '^\s*\(constraint\|inherit\|initializer\|method\|val\)\>\|\<\(object\|object\s*(.*)\)\s*$'
50let s:type = '^\s*\%(let\|type\)\>.*='
51let s:val = '^\s*\(val\|external\)\>.*:'
52
53" Skipping pattern, for comments
54function! s:SkipPattern(lnum, pat)
55 let def = prevnonblank(a:lnum - 1)
56 while def > 0 && getline(def) =~ a:pat
57 let def = prevnonblank(def - 1)
58 endwhile
59 return def
60endfunction
61
62" Indent for ';;' to match multiple 'let'
63function! s:GetInd(lnum, pat, lim)
64 let llet = search(a:pat, 'bW')
65 let old = indent(a:lnum)
66 while llet > 0
67 let old = indent(llet)
68 let nb = s:SkipPattern(llet, '^\s*(\*.*\*)\s*$')
69 if getline(nb) =~ a:lim
70 return old
71 endif
72 let llet = search(a:pat, 'bW')
73 endwhile
74 return old
75endfunction
76
77" Indent pairs
78function! s:FindPair(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"'))
81 let lno = searchpair(a:pstart, a:pmid, a:pend, 'bW', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"')
82 if lno == -1
83 return indent(lno)
84 else
85 return col(".") - 1
86 endif
87endfunction
88
89function! 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"'))
92 let lno = searchpair(a:pstart, a:pmid, a:pend, 'bW', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"')
93 let moduleLine = getline(lno)
94 if lno == -1 || moduleLine =~ '^\s*\(fun\|structure\|signature\)\>'
95 return indent(lno)
96 else
97 return col(".") - 1
98 endif
99endfunction
100
101" Indent 'let'
102"function! s:FindLet(pstart, pmid, pend)
103" call search(a:pend, 'bW')
104" 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'))
105"endfunction
106
107function! GetSMLIndent()
108 " Find a non-blank line above the current line.
109 let lnum = prevnonblank(v:lnum - 1)
110
111 " At the start of the file use zero indent.
112 if lnum == 0
113 return 0
114 endif
115
116 let ind = indent(lnum)
117 let lline = getline(lnum)
118
119 " Return double 'shiftwidth' after lines matching:
120 if lline =~ '^\s*|.*=>\s*$'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200121 return ind + 2 *shiftwidth()
Bram Moolenaar1e015462005-09-25 22:16:38 +0000122 elseif lline =~ '^\s*val\>.*=\s*$'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200123 return ind + shiftwidth()
Bram Moolenaar1e015462005-09-25 22:16:38 +0000124 endif
125
126 let line = getline(v:lnum)
127
128 " Indent lines starting with 'end' to matching module
129 if line =~ '^\s*end\>'
130 return s:FindLet(s:module, '', '\<end\>')
131
132 " Match 'else' with 'if'
133 elseif line =~ '^\s*else\>'
134 if lline !~ '^\s*\(if\|else\|then\)\>'
135 return s:FindPair('\<if\>', '', '\<then\>')
Bram Moolenaar57657d82006-04-21 22:12:41 +0000136 else
137 return ind
Bram Moolenaar1e015462005-09-25 22:16:38 +0000138 endif
139
140 " Match 'then' with 'if'
141 elseif line =~ '^\s*then\>'
142 if lline !~ '^\s*\(if\|else\|then\)\>'
143 return s:FindPair('\<if\>', '', '\<then\>')
Bram Moolenaar57657d82006-04-21 22:12:41 +0000144 else
145 return ind
Bram Moolenaar1e015462005-09-25 22:16:38 +0000146 endif
147
148 " Indent if current line begins with ']'
149 elseif line =~ '^\s*\]'
150 return s:FindPair('\[','','\]')
151
152 " Indent current line starting with 'in' to last matching 'let'
153 elseif line =~ '^\s*in\>'
154 let ind = s:FindLet('\<let\>','','\<in\>')
155
156 " Indent from last matching module if line matches:
157 elseif line =~ '^\s*\(fun\|val\|open\|structure\|and\|datatype\|type\|exception\)\>'
158 cursor(lnum,1)
159 let lastModule = indent(searchpair(s:module, '', '\<end\>', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"'))
160 if lastModule == -1
161 return 0
162 else
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200163 return lastModule + shiftwidth()
Bram Moolenaar1e015462005-09-25 22:16:38 +0000164 endif
165
166 " Indent lines starting with '|' from matching 'case', 'handle'
167 elseif line =~ '^\s*|'
168 " cursor(lnum,1)
169 let lastSwitch = search('\<\(case\|handle\|fun\|datatype\)\>','bW')
170 let switchLine = getline(lastSwitch)
171 let switchLineIndent = indent(lastSwitch)
172 if lline =~ '^\s*|'
173 return ind
174 endif
175 if switchLine =~ '\<case\>'
176 return col(".") + 2
177 elseif switchLine =~ '\<handle\>'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200178 return switchLineIndent + shiftwidth()
Bram Moolenaar1e015462005-09-25 22:16:38 +0000179 elseif switchLine =~ '\<datatype\>'
180 call search('=')
181 return col(".") - 1
182 else
183 return switchLineIndent + 2
184 endif
185
186
187 " Indent if last line ends with 'sig', 'struct', 'let', 'then', 'else',
188 " 'in'
189 elseif lline =~ '\<\(sig\|struct\|let\|in\|then\|else\)\s*$'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200190 let ind = ind + shiftwidth()
Bram Moolenaar1e015462005-09-25 22:16:38 +0000191
192 " Indent if last line ends with 'of', align from 'case'
193 elseif lline =~ '\<\(of\)\s*$'
194 call search('\<case\>',"bW")
195 let ind = col(".")+4
196
197 " Indent if current line starts with 'of'
198 elseif line =~ '^\s*of\>'
199 call search('\<case\>',"bW")
200 let ind = col(".")+1
201
202
203 " Indent if last line starts with 'fun', 'case', 'fn'
204 elseif lline =~ '^\s*\(fun\|fn\|case\)\>'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200205 let ind = ind + shiftwidth()
Bram Moolenaar1e015462005-09-25 22:16:38 +0000206
207 endif
208
209 " Don't indent 'let' if last line started with 'fun', 'fn'
210 if line =~ '^\s*let\>'
211 if lline =~ '^\s*\(fun\|fn\)'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200212 let ind = ind - shiftwidth()
Bram Moolenaar1e015462005-09-25 22:16:38 +0000213 endif
214 endif
215
216 return ind
217
218endfunction
219
220" vim:sw=2