blob: 634d69326393d3bb4713c8c1e72c35e5e9022ea4 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002" Language: OCaml
3" Maintainers: Jean-Francois Yuen <jfyuen@happycoders.org>
4" Mike Leary <leary@nwlink.com>
5" Markus Mottl <markus@oefai.at>
6" URL: http://www.oefai.at/~markus/vim/indent/ocaml.vim
7" Last Change: 2004 Apr 11 - Added indent for 'class' (JY)
8" 2003 Sep 16 - Added 'private' as keyword (JY)
9" 2003 Mar 29 - Fixed bug with 'if' and 'else' (JY)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010
11" Only load this indent file when no other was loaded.
12if exists("b:did_indent")
13 finish
14endif
15let b:did_indent = 1
16
17setlocal expandtab
18setlocal indentexpr=GetOCamlIndent()
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019setlocal 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)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020setlocal nolisp
21setlocal nosmartindent
22setlocal textwidth=80
23
24" Comment formatting
25if (has("comments"))
26 setlocal comments=sr:(*,mb:*,ex:*)
27 setlocal fo=cqort
28endif
29
30" Only define the function once.
31if exists("*GetOCamlIndent")
32 finish
33endif
34
35" Define some patterns:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000036let s:beflet = '^\s*\(initializer\|method\|try\)\|\(\<\(begin\|do\|else\|in\|then\|try\)\|->\|<-\|=\|;\|(\)\s*$'
Bram Moolenaar071d4272004-06-13 20:20:40 +000037let s:letpat = '^\s*\(let\|type\|module\|class\|open\|exception\|val\|include\|external\)\>'
38let s:letlim = '\(\<\(sig\|struct\)\|;;\)\s*$'
39let s:lim = '^\s*\(exception\|external\|include\|let\|module\|open\|type\|val\)\>'
40let s:module = '\<\%(begin\|sig\|struct\|object\)\>'
41let s:obj = '^\s*\(constraint\|inherit\|initializer\|method\|val\)\>\|\<\(object\|object\s*(.*)\)\s*$'
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000042let s:type = '^\s*\%(class\|let\|type\)\>.*='
Bram Moolenaar071d4272004-06-13 20:20:40 +000043
44" Skipping pattern, for comments
45function 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
51endfunction
52
53" Indent for ';;' to match multiple 'let'
54function s:GetInd(lnum, pat, lim)
55 let llet = search(a:pat, 'bW')
56 let old = indent(a:lnum)
57 while llet > 0
58 let old = indent(llet)
59 let nb = s:SkipPattern(llet, '^\s*(\*.*\*)\s*$')
60 if getline(nb) =~ a:lim
61 return old
62 endif
63 let llet = search(a:pat, 'bW')
64 endwhile
65 return old
66endfunction
67
68" Indent pairs
69function 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"'))
72endfunction
73
74" Indent 'let'
75function s:FindLet(pstart, pmid, pend)
76 call search(a:pend, 'bW')
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000077 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'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000078endfunction
79
80function GetOCamlIndent()
81 " Find a non-blank line above the current line.
82 let lnum = prevnonblank(v:lnum - 1)
83
84 " At the start of the file use zero indent.
85 if lnum == 0
86 return 0
87 endif
88
89 let ind = indent(lnum)
90 let lline = getline(lnum)
91
92 " Return double 'shiftwidth' after lines matching:
93 if lline =~ '^\s*|.*->\s*$'
94 return ind + &sw + &sw
95 endif
96
97 let line = getline(v:lnum)
98
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000099 " Indent if current line begins with 'end':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100 if line =~ '^\s*end\>'
101 return s:FindPair(s:module, '','\<end\>')
102
103 " Indent if current line begins with 'done' for 'do':
104 elseif line =~ '^\s*done\>'
105 return s:FindPair('\<do\>', '','\<done\>')
106
107 " Indent if current line begins with '}' or '>}':
108 elseif line =~ '^\s*\(\|>\)}'
109 return s:FindPair('{', '','}')
110
111 " Indent if current line begins with ']', '|]' or '>]':
112 elseif line =~ '^\s*\(\||\|>\)\]'
113 return s:FindPair('\[', '','\]')
114
115 " Indent if current line begins with ')':
116 elseif line =~ '^\s*)'
117 return s:FindPair('(', '',')')
118
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000119 " Indent if current line begins with 'let':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 elseif line =~ '^\s*let\>'
121 if lline !~ s:lim . '\|' . s:letlim . '\|' . s:beflet
122 return s:FindLet(s:type, '','\<let\s*$')
123 else return ind
124 endif
125
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000126 " Indent if current line begins with 'class' or 'type':
127 elseif line =~ '^\s*\(class\|type\)\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 if lline !~ s:lim . '\|\<and\s*$\|' . s:letlim
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000129 return s:FindLet(s:type, '','\<\(class\|type\)\s*$')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 else return ind
131 endif
132
133 " Indent for pattern matching:
134 elseif line =~ '^\s*|'
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000135 if lline !~ '^\s*\(|[^\]]\|\(match\|type\|with\)\>\)\|\<\(function\|parser\|private\|with\)\s*$'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136 call search('|', 'bW')
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000137 return indent(searchpair('^\s*\(match\|type\)\>\|\<\(function\|parser\|private\|with\)\s*$', '', '^\s*|', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment" || getline(".") !~ "^\\s*|.*->"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 else return ind
139 endif
140
141 " Indent if current line begins with ';;':
142 elseif line =~ '^\s*;;'
143 if lline !~ ';;\s*$'
144 return s:GetInd(v:lnum, s:letpat, s:letlim)
145 else return ind
146 endif
147
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000148 " Indent if current line begins with 'in':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 elseif line =~ '^\s*in\>'
150 if lline !~ '^\s*\(let\|and\)\>'
151 return s:FindPair('\<let\>', '', '\<in\>')
152 else return ind
153 endif
154
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000155 " Indent if current line begins with 'else':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 elseif line =~ '^\s*else\>'
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000157 if lline !~ '^\s*\(if\|then\)\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 return s:FindPair('\<if\>', '', '\<else\>')
159 else return ind
160 endif
161
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000162 " Indent if current line begins with 'then':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 elseif line =~ '^\s*then\>'
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000164 if lline !~ '^\s*\(if\|else\)\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 return s:FindPair('\<if\>', '', '\<then\>')
166 else return ind
167 endif
168
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000169 " Indent if current line begins with 'and':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 elseif line =~ '^\s*and\>'
171 if lline !~ '^\s*\(and\|let\|type\)\>\|\<end\s*$'
172 return ind - &sw
173 else return ind
174 endif
175
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000176 " Indent if current line begins with 'with':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 elseif line =~ '^\s*with\>'
178 if lline !~ '^\s*\(match\|try\)\>'
179 return s:FindPair('\<\%(match\|try\)\>', '','\<with\>')
180 else return ind
181 endif
182
183 " Indent if current line begins with 'exception':
184 elseif line =~ '^\s*exception\>'
185 if lline !~ s:lim . '\|' . s:letlim
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000186 return indent(search('^\s*\(\(external\|include\|open\|type\)\>\|val\>.*:\)', 'bW'))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 else return ind
188 endif
189
190 " Indent if current line begins with 'external':
191 elseif line =~ '^\s*external\>'
192 if lline !~ s:lim . '\|' . s:letlim
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000193 return indent(search('^\s*\(\(exception\|external\|include\|open\|type\)\>\|val\>.*:\)', 'bW'))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 else return ind
195 endif
196
197 " Indent if current line begins with 'include':
198 elseif line =~ '^\s*include\>'
199 if lline !~ s:lim . '\|' . s:letlim
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000200 return indent(search('^\s*\(\(exception\|external\|open\|type\)\>\|val\>.*:\)', 'bW'))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 else return ind
202 endif
203
204 " Indent if current line begins with 'open':
205 elseif line =~ '^\s*open\>'
206 if lline !~ s:lim . '\|' . s:letlim
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000207 return indent(search('^\s*\(\(exception\|external\|include\|type\)\>\|val\>.*:\)', 'bW'))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 else return ind
209 endif
210
211 " Indent if current line begins with 'val':
212 elseif line =~ '^\s*val\>'
213 if lline !~ '^\s*\(exception\|external\|include\|open\)\>\|' . s:obj . '\|' . s:letlim
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000214 return indent(search('^\s*\(\(exception\|include\|initializer\|method\|open\|type\|val\)\>\|external\>.*:\)', 'bW'))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 else return ind
216 endif
217
218 " Indent if current line begins with 'constraint':
219 elseif line =~ '^\s*constraint\>'
220 if lline !~ s:obj
221 return indent(search('^\s*\(inherit\|initializer\|method\|val\)\>', 'bW'))
222 else return ind
223 endif
224
225 " Indent if current line begins with 'inherit':
226 elseif line =~ '^\s*inherit\>'
227 if lline !~ s:obj
228 return indent(search('^\s*\(constraint\|initializer\|method\|val\)\>', 'bW'))
229 else return ind
230 endif
231
232 " Indent if current line begins with 'inherit':
233 elseif line =~ '^\s*initializer\>'
234 if lline !~ s:obj
235 return indent(search('^\s*\(constraint\|inherit\|method\|val\)\>', 'bW'))
236 else return ind
237 endif
238
239 " Indent if current line begins with 'method':
240 elseif line =~ '^\s*method\>'
241 if lline !~ s:obj
242 return indent(search('^\s*\(\(constraint\|inherit\|initializer\|val\)\>\|method\>.*\(:\|=\)\)', 'bW'))
243 else return ind
244 endif
245
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 endif
247
248 " Add a 'shiftwidth' after lines ending with:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000249 if lline =~ '\(:\|=\|->\|<-\|(\|\[\|{\|{<\|\[|\|\[<\|\<\(begin\|do\|else\|fun\|function\|functor\|if\|initializer\|object\|parser\|private\|sig\|struct\|then\|try\)\|\<object\s*(.*)\)\s*$'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 let ind = ind + &sw
251
252 " Back to normal indent after lines ending with ';;':
253 elseif lline =~ ';;\s*$' && lline !~ '^\s*;;'
254 let ind = s:GetInd(v:lnum, s:letpat, s:letlim)
255
256 " Back to normal indent after lines ending with 'end':
257 elseif lline =~ '\<end\s*$'
258 let ind = s:FindPair(s:module, '','\<end\>')
259
260 " Back to normal indent after lines ending with 'in':
261 elseif lline =~ '\<in\s*$' && lline !~ '^\s*in\>'
262 let ind = s:FindPair('\<let\>', '', '\<in\>')
263
264 " Back to normal indent after lines ending with 'done':
265 elseif lline =~ '\<done\s*$'
266 let ind = s:FindPair('\<do\>', '','\<done\>')
267
268 " Back to normal indent after lines ending with '}' or '>}':
269 elseif lline =~ '\(\|>\)}\s*$'
270 let ind = s:FindPair('{', '','}')
271
272 " Back to normal indent after lines ending with ']', '|]' or '>]':
273 elseif lline =~ '\(\||\|>\)\]\s*$'
274 let ind = s:FindPair('\[', '','\]')
275
276 " Back to normal indent after comments:
277 elseif lline =~ '\*)\s*$'
278 call search('\*)', 'bW')
279 let ind = indent(searchpair('(\*', '', '\*)', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))
280
281 " Back to normal indent after lines ending with ')':
282 elseif lline =~ ')\s*$'
283 let ind = s:FindPair('(', '',')')
284
285 endif
286
287 " Subtract a 'shiftwidth' after lines matching 'match ... with parser':
288 if lline =~ '^\s*match\>.*\<with\>\s*\<parser\s*$'
289 let ind = ind - &sw
290 endif
291
292 return ind
293
294endfunction
295
296" vim:sw=2