blob: 5c267af8b16ece3a5feb8c339ffb5728874a9583 [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 Moolenaarc9b4b052006-04-30 18:54:39 +00003" 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 Moolenaar9964e462007-05-05 17:54:07 +00007" Last Change: 2005 Jun 25 - Fixed multiple bugs due to 'else\nreturn ind' working
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008" 2005 May 09 - Added an option to not indent OCaml-indents specially (MM)
9" 2005 Apr 11 - Fixed an indentation bug concerning "let" (MM)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010
11" Only load this indent file when no other was loaded.
12if exists("b:did_indent")
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000013 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000014endif
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
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000025if !exists("no_ocaml_comments")
26 if (has("comments"))
27 setlocal comments=sr:(*,mb:*,ex:*)
28 setlocal fo=cqort
29 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000030endif
31
32" Only define the function once.
33if exists("*GetOCamlIndent")
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000034 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000035endif
36
37" Define some patterns:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000038let s:beflet = '^\s*\(initializer\|method\|try\)\|\(\<\(begin\|do\|else\|in\|then\|try\)\|->\|<-\|=\|;\|(\)\s*$'
Bram Moolenaar071d4272004-06-13 20:20:40 +000039let s:letpat = '^\s*\(let\|type\|module\|class\|open\|exception\|val\|include\|external\)\>'
40let s:letlim = '\(\<\(sig\|struct\)\|;;\)\s*$'
41let s:lim = '^\s*\(exception\|external\|include\|let\|module\|open\|type\|val\)\>'
42let s:module = '\<\%(begin\|sig\|struct\|object\)\>'
43let s:obj = '^\s*\(constraint\|inherit\|initializer\|method\|val\)\>\|\<\(object\|object\s*(.*)\)\s*$'
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000044let s:type = '^\s*\%(class\|let\|type\)\>.*='
Bram Moolenaar071d4272004-06-13 20:20:40 +000045
46" Skipping pattern, for comments
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000047function s:GetLineWithoutFullComment(lnum)
48 let lnum = prevnonblank(a:lnum - 1)
49 let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
50 while lline =~ '^\s*$' && lnum > 0
51 let lnum = prevnonblank(lnum - 1)
52 let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
53 endwhile
54 return lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +000055endfunction
56
57" Indent for ';;' to match multiple 'let'
58function s:GetInd(lnum, pat, lim)
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000059 let llet = search(a:pat, 'bW')
60 let old = indent(a:lnum)
61 while llet > 0
62 let old = indent(llet)
63 let nb = s:GetLineWithoutFullComment(llet)
64 if getline(nb) =~ a:lim
65 return old
66 endif
67 let llet = search(a:pat, 'bW')
68 endwhile
69 return old
Bram Moolenaar071d4272004-06-13 20:20:40 +000070endfunction
71
72" Indent pairs
73function s:FindPair(pstart, pmid, pend)
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000074 call search(a:pend, 'bW')
75 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 +000076endfunction
77
78" Indent 'let'
79function s:FindLet(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" || getline(".") =~ "^\\s*let\\>.*=.*\\<in\\s*$" || getline(prevnonblank(".") - 1) =~ s:beflet'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000082endfunction
83
84function GetOCamlIndent()
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000085 " Find a non-commented line above the current line.
86 let lnum = s:GetLineWithoutFullComment(v:lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000087
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000088 " At the start of the file use zero indent.
89 if lnum == 0
90 return 0
91 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000093 let ind = indent(lnum)
94 let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
Bram Moolenaar071d4272004-06-13 20:20:40 +000095
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000096 " Return double 'shiftwidth' after lines matching:
97 if lline =~ '^\s*|.*->\s*$'
98 return ind + &sw + &sw
99 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000101 let line = getline(v:lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000103 " Indent if current line begins with 'end':
104 if line =~ '^\s*end\>'
105 return s:FindPair(s:module, '','\<end\>')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000107 " Indent if current line begins with 'done' for 'do':
108 elseif line =~ '^\s*done\>'
109 return s:FindPair('\<do\>', '','\<done\>')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000111 " Indent if current line begins with '}' or '>}':
112 elseif line =~ '^\s*\(\|>\)}'
113 return s:FindPair('{', '','}')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000115 " Indent if current line begins with ']', '|]' or '>]':
116 elseif line =~ '^\s*\(\||\|>\)\]'
117 return s:FindPair('\[', '','\]')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000119 " Indent if current line begins with ')':
120 elseif line =~ '^\s*)'
121 return s:FindPair('(', '',')')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000123 " Indent if current line begins with 'let':
124 elseif line =~ '^\s*let\>'
125 if lline !~ s:lim . '\|' . s:letlim . '\|' . s:beflet
126 return s:FindLet(s:type, '','\<let\s*$')
127 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000129 " Indent if current line begins with 'class' or 'type':
130 elseif line =~ '^\s*\(class\|type\)\>'
131 if lline !~ s:lim . '\|\<and\s*$\|' . s:letlim
132 return s:FindLet(s:type, '','\<\(class\|type\)\s*$')
133 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000135 " Indent for pattern matching:
136 elseif line =~ '^\s*|'
137 if lline !~ '^\s*\(|[^\]]\|\(match\|type\|with\)\>\)\|\<\(function\|parser\|private\|with\)\s*$'
138 call search('|', 'bW')
139 return indent(searchpair('^\s*\(match\|type\)\>\|\<\(function\|parser\|private\|with\)\s*$', '', '^\s*|', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment" || getline(".") !~ "^\\s*|.*->"'))
140 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000142 " Indent if current line begins with ';;':
143 elseif line =~ '^\s*;;'
144 if lline !~ ';;\s*$'
145 return s:GetInd(v:lnum, s:letpat, s:letlim)
146 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000148 " Indent if current line begins with 'in':
149 elseif line =~ '^\s*in\>'
150 if lline !~ '^\s*\(let\|and\)\>'
151 return s:FindPair('\<let\>', '', '\<in\>')
152 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000154 " Indent if current line begins with 'else':
155 elseif line =~ '^\s*else\>'
156 if lline !~ '^\s*\(if\|then\)\>'
157 return s:FindPair('\<if\>', '', '\<else\>')
158 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000160 " Indent if current line begins with 'then':
161 elseif line =~ '^\s*then\>'
162 if lline !~ '^\s*\(if\|else\)\>'
163 return s:FindPair('\<if\>', '', '\<then\>')
164 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000166 " Indent if current line begins with 'and':
167 elseif line =~ '^\s*and\>'
168 if lline !~ '^\s*\(and\|let\|type\)\>\|\<end\s*$'
169 return ind - &sw
170 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000172 " Indent if current line begins with 'with':
173 elseif line =~ '^\s*with\>'
174 if lline !~ '^\s*\(match\|try\)\>'
175 return s:FindPair('\<\%(match\|try\)\>', '','\<with\>')
176 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000178 " Indent if current line begins with 'exception', 'external', 'include' or
179 " 'open':
180 elseif line =~ '^\s*\(exception\|external\|include\|open\)\>'
181 if lline !~ s:lim . '\|' . s:letlim
182 call search(line)
183 return indent(search('^\s*\(\(exception\|external\|include\|open\|type\)\>\|val\>.*:\)', 'bW'))
184 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000186 " Indent if current line begins with 'val':
187 elseif line =~ '^\s*val\>'
188 if lline !~ '^\s*\(exception\|external\|include\|open\)\>\|' . s:obj . '\|' . s:letlim
189 return indent(search('^\s*\(\(exception\|include\|initializer\|method\|open\|type\|val\)\>\|external\>.*:\)', '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 'constraint', 'inherit', 'initializer'
193 " or 'method':
194 elseif line =~ '^\s*\(constraint\|inherit\|initializer\|method\)\>'
195 if lline !~ s:obj
196 return indent(search('\<\(object\|object\s*(.*)\)\s*$', 'bW')) + &sw
197 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000199 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000201 " Add a 'shiftwidth' after lines ending with:
202 if lline =~ '\(:\|=\|->\|<-\|(\|\[\|{\|{<\|\[|\|\[<\|\<\(begin\|do\|else\|fun\|function\|functor\|if\|initializer\|object\|parser\|private\|sig\|struct\|then\|try\)\|\<object\s*(.*)\)\s*$'
203 let ind = ind + &sw
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000205 " Back to normal indent after lines ending with ';;':
206 elseif lline =~ ';;\s*$' && lline !~ '^\s*;;'
207 let ind = s:GetInd(v:lnum, s:letpat, s:letlim)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000209 " Back to normal indent after lines ending with 'end':
210 elseif lline =~ '\<end\s*$'
211 let ind = s:FindPair(s:module, '','\<end\>')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000213 " Back to normal indent after lines ending with 'in':
214 elseif lline =~ '\<in\s*$' && lline !~ '^\s*in\>'
215 let ind = s:FindPair('\<let\>', '', '\<in\>')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000217 " Back to normal indent after lines ending with 'done':
218 elseif lline =~ '\<done\s*$'
219 let ind = s:FindPair('\<do\>', '','\<done\>')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000221 " Back to normal indent after lines ending with '}' or '>}':
222 elseif lline =~ '\(\|>\)}\s*$'
223 let ind = s:FindPair('{', '','}')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000225 " Back to normal indent after lines ending with ']', '|]' or '>]':
226 elseif lline =~ '\(\||\|>\)\]\s*$'
227 let ind = s:FindPair('\[', '','\]')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000229 " Back to normal indent after comments:
230 elseif lline =~ '\*)\s*$'
231 call search('\*)', 'bW')
232 let ind = indent(searchpair('(\*', '', '\*)', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000234 " Back to normal indent after lines ending with ')':
235 elseif lline =~ ')\s*$'
236 let ind = s:FindPair('(', '',')')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000238 " If this is a multiline comment then align '*':
239 elseif lline =~ '^\s*(\*' && line =~ '^\s*\*'
240 let ind = ind + 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000242 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000244 " Subtract a 'shiftwidth' after lines matching 'match ... with parser':
245 if lline =~ '\<match\>.*\<with\>\s*\<parser\s*$'
246 let ind = ind - &sw
247 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000249 return ind
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250
251endfunction
252
253" vim:sw=2