blob: 19c81f49c4b3809f97712f8aaef706a9d494accb [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>
Bram Moolenaar7e6a5152021-01-02 16:39:53 +01006" URL: https://github.com/ocaml/vim-ocaml
Bram Moolenaar3ec574f2017-06-13 18:12:01 +02007" Last Change: 2017 Jun 13
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"))
Bram Moolenaar7e6a5152021-01-02 16:39:53 +010033 setlocal comments=sr:(*\ ,mb:\ ,ex:*)
34 setlocal comments^=sr:(**,mb:\ \ ,ex:*)
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000035 setlocal fo=cqort
36 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000037endif
38
39" Only define the function once.
40if exists("*GetOCamlIndent")
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000041 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000042endif
43
44" Define some patterns:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000045let s:beflet = '^\s*\(initializer\|method\|try\)\|\(\<\(begin\|do\|else\|in\|then\|try\)\|->\|<-\|=\|;\|(\)\s*$'
Bram Moolenaar071d4272004-06-13 20:20:40 +000046let s:letpat = '^\s*\(let\|type\|module\|class\|open\|exception\|val\|include\|external\)\>'
47let s:letlim = '\(\<\(sig\|struct\)\|;;\)\s*$'
48let s:lim = '^\s*\(exception\|external\|include\|let\|module\|open\|type\|val\)\>'
49let s:module = '\<\%(begin\|sig\|struct\|object\)\>'
50let s:obj = '^\s*\(constraint\|inherit\|initializer\|method\|val\)\>\|\<\(object\|object\s*(.*)\)\s*$'
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000051let s:type = '^\s*\%(class\|let\|type\)\>.*='
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53" Skipping pattern, for comments
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010054function! s:GetLineWithoutFullComment(lnum)
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000055 let lnum = prevnonblank(a:lnum - 1)
56 let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
57 while lline =~ '^\s*$' && lnum > 0
58 let lnum = prevnonblank(lnum - 1)
59 let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
60 endwhile
61 return lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +000062endfunction
63
64" Indent for ';;' to match multiple 'let'
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010065function! s:GetInd(lnum, pat, lim)
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000066 let llet = search(a:pat, 'bW')
67 let old = indent(a:lnum)
68 while llet > 0
69 let old = indent(llet)
70 let nb = s:GetLineWithoutFullComment(llet)
71 if getline(nb) =~ a:lim
72 return old
73 endif
74 let llet = search(a:pat, 'bW')
75 endwhile
76 return old
Bram Moolenaar071d4272004-06-13 20:20:40 +000077endfunction
78
79" Indent pairs
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010080function! s:FindPair(pstart, pmid, pend)
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000081 call search(a:pend, 'bW')
82 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 +000083endfunction
84
85" Indent 'let'
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010086function! s:FindLet(pstart, pmid, pend)
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000087 call search(a:pend, 'bW')
88 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 +000089endfunction
90
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010091function! GetOCamlIndent()
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000092 " Find a non-commented line above the current line.
93 let lnum = s:GetLineWithoutFullComment(v:lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000095 " At the start of the file use zero indent.
96 if lnum == 0
97 return 0
98 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000100 let ind = indent(lnum)
101 let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000103 " Return double 'shiftwidth' after lines matching:
104 if lline =~ '^\s*|.*->\s*$'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200105 return ind + 2 * shiftwidth()
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000106 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000108 let line = getline(v:lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000110 " Indent if current line begins with 'end':
111 if line =~ '^\s*end\>'
112 return s:FindPair(s:module, '','\<end\>')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000114 " Indent if current line begins with 'done' for 'do':
115 elseif line =~ '^\s*done\>'
116 return s:FindPair('\<do\>', '','\<done\>')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000118 " Indent if current line begins with '}' or '>}':
119 elseif line =~ '^\s*\(\|>\)}'
120 return s:FindPair('{', '','}')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000122 " Indent if current line begins with ']', '|]' or '>]':
123 elseif line =~ '^\s*\(\||\|>\)\]'
124 return s:FindPair('\[', '','\]')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000126 " Indent if current line begins with ')':
127 elseif line =~ '^\s*)'
128 return s:FindPair('(', '',')')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000130 " Indent if current line begins with 'let':
131 elseif line =~ '^\s*let\>'
132 if lline !~ s:lim . '\|' . s:letlim . '\|' . s:beflet
133 return s:FindLet(s:type, '','\<let\s*$')
134 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000136 " Indent if current line begins with 'class' or 'type':
137 elseif line =~ '^\s*\(class\|type\)\>'
138 if lline !~ s:lim . '\|\<and\s*$\|' . s:letlim
139 return s:FindLet(s:type, '','\<\(class\|type\)\s*$')
140 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000142 " Indent for pattern matching:
143 elseif line =~ '^\s*|'
144 if lline !~ '^\s*\(|[^\]]\|\(match\|type\|with\)\>\)\|\<\(function\|parser\|private\|with\)\s*$'
145 call search('|', 'bW')
146 return indent(searchpair('^\s*\(match\|type\)\>\|\<\(function\|parser\|private\|with\)\s*$', '', '^\s*|', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment" || getline(".") !~ "^\\s*|.*->"'))
147 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000149 " Indent if current line begins with ';;':
150 elseif line =~ '^\s*;;'
151 if lline !~ ';;\s*$'
152 return s:GetInd(v:lnum, s:letpat, s:letlim)
153 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000155 " Indent if current line begins with 'in':
156 elseif line =~ '^\s*in\>'
157 if lline !~ '^\s*\(let\|and\)\>'
158 return s:FindPair('\<let\>', '', '\<in\>')
159 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000161 " Indent if current line begins with 'else':
162 elseif line =~ '^\s*else\>'
163 if lline !~ '^\s*\(if\|then\)\>'
164 return s:FindPair('\<if\>', '', '\<else\>')
165 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000167 " Indent if current line begins with 'then':
168 elseif line =~ '^\s*then\>'
169 if lline !~ '^\s*\(if\|else\)\>'
170 return s:FindPair('\<if\>', '', '\<then\>')
171 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000173 " Indent if current line begins with 'and':
174 elseif line =~ '^\s*and\>'
175 if lline !~ '^\s*\(and\|let\|type\)\>\|\<end\s*$'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200176 return ind - shiftwidth()
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000177 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000179 " Indent if current line begins with 'with':
180 elseif line =~ '^\s*with\>'
181 if lline !~ '^\s*\(match\|try\)\>'
182 return s:FindPair('\<\%(match\|try\)\>', '','\<with\>')
183 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000185 " Indent if current line begins with 'exception', 'external', 'include' or
186 " 'open':
187 elseif line =~ '^\s*\(exception\|external\|include\|open\)\>'
188 if lline !~ s:lim . '\|' . s:letlim
189 call search(line)
190 return indent(search('^\s*\(\(exception\|external\|include\|open\|type\)\>\|val\>.*:\)', 'bW'))
191 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000193 " Indent if current line begins with 'val':
194 elseif line =~ '^\s*val\>'
195 if lline !~ '^\s*\(exception\|external\|include\|open\)\>\|' . s:obj . '\|' . s:letlim
196 return indent(search('^\s*\(\(exception\|include\|initializer\|method\|open\|type\|val\)\>\|external\>.*:\)', 'bW'))
197 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000199 " Indent if current line begins with 'constraint', 'inherit', 'initializer'
200 " or 'method':
201 elseif line =~ '^\s*\(constraint\|inherit\|initializer\|method\)\>'
202 if lline !~ s:obj
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200203 return indent(search('\<\(object\|object\s*(.*)\)\s*$', 'bW')) + shiftwidth()
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000204 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000206 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000208 " Add a 'shiftwidth' after lines ending with:
209 if lline =~ '\(:\|=\|->\|<-\|(\|\[\|{\|{<\|\[|\|\[<\|\<\(begin\|do\|else\|fun\|function\|functor\|if\|initializer\|object\|parser\|private\|sig\|struct\|then\|try\)\|\<object\s*(.*)\)\s*$'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200210 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000212 " Back to normal indent after lines ending with ';;':
213 elseif lline =~ ';;\s*$' && lline !~ '^\s*;;'
214 let ind = s:GetInd(v:lnum, s:letpat, s:letlim)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000216 " Back to normal indent after lines ending with 'end':
217 elseif lline =~ '\<end\s*$'
218 let ind = s:FindPair(s:module, '','\<end\>')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000220 " Back to normal indent after lines ending with 'in':
221 elseif lline =~ '\<in\s*$' && lline !~ '^\s*in\>'
222 let ind = s:FindPair('\<let\>', '', '\<in\>')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000224 " Back to normal indent after lines ending with 'done':
225 elseif lline =~ '\<done\s*$'
226 let ind = s:FindPair('\<do\>', '','\<done\>')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000228 " Back to normal indent after lines ending with '}' or '>}':
229 elseif lline =~ '\(\|>\)}\s*$'
230 let ind = s:FindPair('{', '','}')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000232 " Back to normal indent after lines ending with ']', '|]' or '>]':
233 elseif lline =~ '\(\||\|>\)\]\s*$'
234 let ind = s:FindPair('\[', '','\]')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000236 " Back to normal indent after comments:
237 elseif lline =~ '\*)\s*$'
238 call search('\*)', 'bW')
239 let ind = indent(searchpair('(\*', '', '\*)', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000241 " Back to normal indent after lines ending with ')':
242 elseif lline =~ ')\s*$'
243 let ind = s:FindPair('(', '',')')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000245 " If this is a multiline comment then align '*':
246 elseif lline =~ '^\s*(\*' && line =~ '^\s*\*'
247 let ind = ind + 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100249 else
250 " Don't change indentation of this line
251 " for new lines (indent==0) use indentation of previous line
252
253 " This is for preventing removing indentation of these args:
254 " let f x =
255 " let y = x + 1 in
256 " Printf.printf
257 " "o" << here
258 " "oeuth" << don't touch indentation
259
260 let i = indent(v:lnum)
261 return i == 0 ? ind : i
262
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000263 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000265 " Subtract a 'shiftwidth' after lines matching 'match ... with parser':
266 if lline =~ '\<match\>.*\<with\>\s*\<parser\s*$'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200267 let ind = ind - shiftwidth()
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000268 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000270 return ind
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271
272endfunction
273
274" vim:sw=2