blob: 3bd65c63f0208fb05c69d06808e986dbd5d9871c [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002" Language: OCaml
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01003" 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
Bram Moolenaar14b69452013-06-29 23:05:20 +02007" Last Change: 2013 Jun 29
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01008" 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)
Bram Moolenaar14b69452013-06-29 23:05:20 +020010" 2013 June - commented textwidth (Marc Weber)
11"
12" Marc Weber's comment: This file may contain a lot of (very custom) stuff
13" which eventually should be moved somewhere else ..
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15" Only load this indent file when no other was loaded.
16if exists("b:did_indent")
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000017 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000018endif
19let b:did_indent = 1
20
21setlocal expandtab
22setlocal indentexpr=GetOCamlIndent()
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000023setlocal 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 +000024setlocal nolisp
25setlocal nosmartindent
Bram Moolenaar14b69452013-06-29 23:05:20 +020026
27" At least Marc Weber and Markus Mottl do not like this:
28" setlocal textwidth=80
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
30" Comment formatting
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000031if !exists("no_ocaml_comments")
32 if (has("comments"))
33 setlocal comments=sr:(*,mb:*,ex:*)
34 setlocal fo=cqort
35 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000036endif
37
38" Only define the function once.
39if exists("*GetOCamlIndent")
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000040 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000041endif
42
43" Define some patterns:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000044let s:beflet = '^\s*\(initializer\|method\|try\)\|\(\<\(begin\|do\|else\|in\|then\|try\)\|->\|<-\|=\|;\|(\)\s*$'
Bram Moolenaar071d4272004-06-13 20:20:40 +000045let 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 = '\<\%(begin\|sig\|struct\|object\)\>'
49let s:obj = '^\s*\(constraint\|inherit\|initializer\|method\|val\)\>\|\<\(object\|object\s*(.*)\)\s*$'
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000050let s:type = '^\s*\%(class\|let\|type\)\>.*='
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
52" Skipping pattern, for comments
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010053function! s:GetLineWithoutFullComment(lnum)
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000054 let lnum = prevnonblank(a:lnum - 1)
55 let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
56 while lline =~ '^\s*$' && lnum > 0
57 let lnum = prevnonblank(lnum - 1)
58 let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
59 endwhile
60 return lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +000061endfunction
62
63" Indent for ';;' to match multiple 'let'
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010064function! s:GetInd(lnum, pat, lim)
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000065 let llet = search(a:pat, 'bW')
66 let old = indent(a:lnum)
67 while llet > 0
68 let old = indent(llet)
69 let nb = s:GetLineWithoutFullComment(llet)
70 if getline(nb) =~ a:lim
71 return old
72 endif
73 let llet = search(a:pat, 'bW')
74 endwhile
75 return old
Bram Moolenaar071d4272004-06-13 20:20:40 +000076endfunction
77
78" Indent pairs
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010079function! s:FindPair(pstart, pmid, pend)
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000080 call search(a:pend, 'bW')
81 return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000082endfunction
83
84" Indent 'let'
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010085function! s:FindLet(pstart, pmid, pend)
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000086 call search(a:pend, 'bW')
87 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 +000088endfunction
89
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010090function! GetOCamlIndent()
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000091 " Find a non-commented line above the current line.
92 let lnum = s:GetLineWithoutFullComment(v:lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000093
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000094 " At the start of the file use zero indent.
95 if lnum == 0
96 return 0
97 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000098
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000099 let ind = indent(lnum)
100 let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000102 " Return double 'shiftwidth' after lines matching:
103 if lline =~ '^\s*|.*->\s*$'
104 return ind + &sw + &sw
105 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000107 let line = getline(v:lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000109 " Indent if current line begins with 'end':
110 if line =~ '^\s*end\>'
111 return s:FindPair(s:module, '','\<end\>')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000113 " Indent if current line begins with 'done' for 'do':
114 elseif line =~ '^\s*done\>'
115 return s:FindPair('\<do\>', '','\<done\>')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000117 " Indent if current line begins with '}' or '>}':
118 elseif line =~ '^\s*\(\|>\)}'
119 return s:FindPair('{', '','}')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000121 " Indent if current line begins with ']', '|]' or '>]':
122 elseif line =~ '^\s*\(\||\|>\)\]'
123 return s:FindPair('\[', '','\]')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000125 " Indent if current line begins with ')':
126 elseif line =~ '^\s*)'
127 return s:FindPair('(', '',')')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000129 " Indent if current line begins with 'let':
130 elseif line =~ '^\s*let\>'
131 if lline !~ s:lim . '\|' . s:letlim . '\|' . s:beflet
132 return s:FindLet(s:type, '','\<let\s*$')
133 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000135 " Indent if current line begins with 'class' or 'type':
136 elseif line =~ '^\s*\(class\|type\)\>'
137 if lline !~ s:lim . '\|\<and\s*$\|' . s:letlim
138 return s:FindLet(s:type, '','\<\(class\|type\)\s*$')
139 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000141 " Indent for pattern matching:
142 elseif line =~ '^\s*|'
143 if lline !~ '^\s*\(|[^\]]\|\(match\|type\|with\)\>\)\|\<\(function\|parser\|private\|with\)\s*$'
144 call search('|', 'bW')
145 return indent(searchpair('^\s*\(match\|type\)\>\|\<\(function\|parser\|private\|with\)\s*$', '', '^\s*|', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment" || getline(".") !~ "^\\s*|.*->"'))
146 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000148 " Indent if current line begins with ';;':
149 elseif line =~ '^\s*;;'
150 if lline !~ ';;\s*$'
151 return s:GetInd(v:lnum, s:letpat, s:letlim)
152 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000154 " Indent if current line begins with 'in':
155 elseif line =~ '^\s*in\>'
156 if lline !~ '^\s*\(let\|and\)\>'
157 return s:FindPair('\<let\>', '', '\<in\>')
158 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000160 " Indent if current line begins with 'else':
161 elseif line =~ '^\s*else\>'
162 if lline !~ '^\s*\(if\|then\)\>'
163 return s:FindPair('\<if\>', '', '\<else\>')
164 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000166 " Indent if current line begins with 'then':
167 elseif line =~ '^\s*then\>'
168 if lline !~ '^\s*\(if\|else\)\>'
169 return s:FindPair('\<if\>', '', '\<then\>')
170 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000172 " Indent if current line begins with 'and':
173 elseif line =~ '^\s*and\>'
174 if lline !~ '^\s*\(and\|let\|type\)\>\|\<end\s*$'
175 return ind - &sw
176 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000178 " Indent if current line begins with 'with':
179 elseif line =~ '^\s*with\>'
180 if lline !~ '^\s*\(match\|try\)\>'
181 return s:FindPair('\<\%(match\|try\)\>', '','\<with\>')
182 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000184 " Indent if current line begins with 'exception', 'external', 'include' or
185 " 'open':
186 elseif line =~ '^\s*\(exception\|external\|include\|open\)\>'
187 if lline !~ s:lim . '\|' . s:letlim
188 call search(line)
189 return indent(search('^\s*\(\(exception\|external\|include\|open\|type\)\>\|val\>.*:\)', 'bW'))
190 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000192 " Indent if current line begins with 'val':
193 elseif line =~ '^\s*val\>'
194 if lline !~ '^\s*\(exception\|external\|include\|open\)\>\|' . s:obj . '\|' . s:letlim
195 return indent(search('^\s*\(\(exception\|include\|initializer\|method\|open\|type\|val\)\>\|external\>.*:\)', 'bW'))
196 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000198 " Indent if current line begins with 'constraint', 'inherit', 'initializer'
199 " or 'method':
200 elseif line =~ '^\s*\(constraint\|inherit\|initializer\|method\)\>'
201 if lline !~ s:obj
202 return indent(search('\<\(object\|object\s*(.*)\)\s*$', 'bW')) + &sw
203 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000205 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000207 " Add a 'shiftwidth' after lines ending with:
208 if lline =~ '\(:\|=\|->\|<-\|(\|\[\|{\|{<\|\[|\|\[<\|\<\(begin\|do\|else\|fun\|function\|functor\|if\|initializer\|object\|parser\|private\|sig\|struct\|then\|try\)\|\<object\s*(.*)\)\s*$'
209 let ind = ind + &sw
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000211 " Back to normal indent after lines ending with ';;':
212 elseif lline =~ ';;\s*$' && lline !~ '^\s*;;'
213 let ind = s:GetInd(v:lnum, s:letpat, s:letlim)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000215 " Back to normal indent after lines ending with 'end':
216 elseif lline =~ '\<end\s*$'
217 let ind = s:FindPair(s:module, '','\<end\>')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000219 " Back to normal indent after lines ending with 'in':
220 elseif lline =~ '\<in\s*$' && lline !~ '^\s*in\>'
221 let ind = s:FindPair('\<let\>', '', '\<in\>')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000223 " Back to normal indent after lines ending with 'done':
224 elseif lline =~ '\<done\s*$'
225 let ind = s:FindPair('\<do\>', '','\<done\>')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000227 " Back to normal indent after lines ending with '}' or '>}':
228 elseif lline =~ '\(\|>\)}\s*$'
229 let ind = s:FindPair('{', '','}')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000231 " Back to normal indent after lines ending with ']', '|]' or '>]':
232 elseif lline =~ '\(\||\|>\)\]\s*$'
233 let ind = s:FindPair('\[', '','\]')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000235 " Back to normal indent after comments:
236 elseif lline =~ '\*)\s*$'
237 call search('\*)', 'bW')
238 let ind = indent(searchpair('(\*', '', '\*)', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000240 " Back to normal indent after lines ending with ')':
241 elseif lline =~ ')\s*$'
242 let ind = s:FindPair('(', '',')')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000244 " If this is a multiline comment then align '*':
245 elseif lline =~ '^\s*(\*' && line =~ '^\s*\*'
246 let ind = ind + 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100248 else
249 " Don't change indentation of this line
250 " for new lines (indent==0) use indentation of previous line
251
252 " This is for preventing removing indentation of these args:
253 " let f x =
254 " let y = x + 1 in
255 " Printf.printf
256 " "o" << here
257 " "oeuth" << don't touch indentation
258
259 let i = indent(v:lnum)
260 return i == 0 ? ind : i
261
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000262 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000264 " Subtract a 'shiftwidth' after lines matching 'match ... with parser':
265 if lline =~ '\<match\>.*\<with\>\s*\<parser\s*$'
266 let ind = ind - &sw
267 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000269 return ind
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270
271endfunction
272
273" vim:sw=2