blob: d137796e3d5bc2a76879e91f8489b8cd4920440d [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
Yinzuo Jiang700cf8c2024-07-14 17:02:33 +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
dkearns0382f052023-08-29 05:32:59 +100027let b:undo_indent = "setl et< inde< indk< lisp< si<"
28
Bram Moolenaar14b69452013-06-29 23:05:20 +020029" At least Marc Weber and Markus Mottl do not like this:
30" setlocal textwidth=80
Bram Moolenaar071d4272004-06-13 20:20:40 +000031
32" Comment formatting
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000033if !exists("no_ocaml_comments")
34 if (has("comments"))
Bram Moolenaar7e6a5152021-01-02 16:39:53 +010035 setlocal comments=sr:(*\ ,mb:\ ,ex:*)
36 setlocal comments^=sr:(**,mb:\ \ ,ex:*)
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000037 setlocal fo=cqort
Yinzuo Jiang700cf8c2024-07-14 17:02:33 +020038 let b:undo_indent .= " | setl com< fo<"
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000039 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000040endif
41
42" Only define the function once.
43if exists("*GetOCamlIndent")
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000044 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000045endif
46
47" Define some patterns:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000048let s:beflet = '^\s*\(initializer\|method\|try\)\|\(\<\(begin\|do\|else\|in\|then\|try\)\|->\|<-\|=\|;\|(\)\s*$'
Bram Moolenaar071d4272004-06-13 20:20:40 +000049let s:letpat = '^\s*\(let\|type\|module\|class\|open\|exception\|val\|include\|external\)\>'
50let s:letlim = '\(\<\(sig\|struct\)\|;;\)\s*$'
51let s:lim = '^\s*\(exception\|external\|include\|let\|module\|open\|type\|val\)\>'
52let s:module = '\<\%(begin\|sig\|struct\|object\)\>'
53let s:obj = '^\s*\(constraint\|inherit\|initializer\|method\|val\)\>\|\<\(object\|object\s*(.*)\)\s*$'
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000054let s:type = '^\s*\%(class\|let\|type\)\>.*='
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
56" Skipping pattern, for comments
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010057function! s:GetLineWithoutFullComment(lnum)
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000058 let lnum = prevnonblank(a:lnum - 1)
59 let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
60 while lline =~ '^\s*$' && lnum > 0
61 let lnum = prevnonblank(lnum - 1)
62 let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
63 endwhile
64 return lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +000065endfunction
66
67" Indent for ';;' to match multiple 'let'
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010068function! s:GetInd(lnum, pat, lim)
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000069 let llet = search(a:pat, 'bW')
70 let old = indent(a:lnum)
71 while llet > 0
72 let old = indent(llet)
73 let nb = s:GetLineWithoutFullComment(llet)
74 if getline(nb) =~ a:lim
75 return old
76 endif
77 let llet = search(a:pat, 'bW')
78 endwhile
79 return old
Bram Moolenaar071d4272004-06-13 20:20:40 +000080endfunction
81
82" Indent pairs
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010083function! s:FindPair(pstart, pmid, pend)
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000084 call search(a:pend, 'bW')
85 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 +000086endfunction
87
88" Indent 'let'
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010089function! s:FindLet(pstart, pmid, pend)
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000090 call search(a:pend, 'bW')
91 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 +000092endfunction
93
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010094function! GetOCamlIndent()
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000095 " Find a non-commented line above the current line.
96 let lnum = s:GetLineWithoutFullComment(v:lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000097
Bram Moolenaar95b28ec2005-10-11 20:32:28 +000098 " At the start of the file use zero indent.
99 if lnum == 0
100 return 0
101 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000103 let ind = indent(lnum)
104 let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000106 " Return double 'shiftwidth' after lines matching:
107 if lline =~ '^\s*|.*->\s*$'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200108 return ind + 2 * shiftwidth()
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000109 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000111 let line = getline(v:lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000113 " Indent if current line begins with 'end':
114 if line =~ '^\s*end\>'
115 return s:FindPair(s:module, '','\<end\>')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000117 " Indent if current line begins with 'done' for 'do':
118 elseif line =~ '^\s*done\>'
119 return s:FindPair('\<do\>', '','\<done\>')
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 ']', '|]' or '>]':
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 ')':
130 elseif line =~ '^\s*)'
131 return s:FindPair('(', '',')')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000133 " Indent if current line begins with 'let':
134 elseif line =~ '^\s*let\>'
135 if lline !~ s:lim . '\|' . s:letlim . '\|' . s:beflet
136 return s:FindLet(s:type, '','\<let\s*$')
137 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000139 " Indent if current line begins with 'class' or 'type':
140 elseif line =~ '^\s*\(class\|type\)\>'
141 if lline !~ s:lim . '\|\<and\s*$\|' . s:letlim
142 return s:FindLet(s:type, '','\<\(class\|type\)\s*$')
143 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000145 " Indent for pattern matching:
146 elseif line =~ '^\s*|'
147 if lline !~ '^\s*\(|[^\]]\|\(match\|type\|with\)\>\)\|\<\(function\|parser\|private\|with\)\s*$'
148 call search('|', 'bW')
149 return indent(searchpair('^\s*\(match\|type\)\>\|\<\(function\|parser\|private\|with\)\s*$', '', '^\s*|', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment" || getline(".") !~ "^\\s*|.*->"'))
150 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000152 " Indent if current line begins with ';;':
153 elseif line =~ '^\s*;;'
154 if lline !~ ';;\s*$'
155 return s:GetInd(v:lnum, s:letpat, s:letlim)
156 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000158 " Indent if current line begins with 'in':
159 elseif line =~ '^\s*in\>'
160 if lline !~ '^\s*\(let\|and\)\>'
161 return s:FindPair('\<let\>', '', '\<in\>')
162 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000164 " Indent if current line begins with 'else':
165 elseif line =~ '^\s*else\>'
166 if lline !~ '^\s*\(if\|then\)\>'
167 return s:FindPair('\<if\>', '', '\<else\>')
168 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000170 " Indent if current line begins with 'then':
171 elseif line =~ '^\s*then\>'
172 if lline !~ '^\s*\(if\|else\)\>'
173 return s:FindPair('\<if\>', '', '\<then\>')
174 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000176 " Indent if current line begins with 'and':
177 elseif line =~ '^\s*and\>'
178 if lline !~ '^\s*\(and\|let\|type\)\>\|\<end\s*$'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200179 return ind - shiftwidth()
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000180 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000182 " Indent if current line begins with 'with':
183 elseif line =~ '^\s*with\>'
184 if lline !~ '^\s*\(match\|try\)\>'
185 return s:FindPair('\<\%(match\|try\)\>', '','\<with\>')
186 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000188 " Indent if current line begins with 'exception', 'external', 'include' or
189 " 'open':
190 elseif line =~ '^\s*\(exception\|external\|include\|open\)\>'
191 if lline !~ s:lim . '\|' . s:letlim
192 call search(line)
193 return indent(search('^\s*\(\(exception\|external\|include\|open\|type\)\>\|val\>.*:\)', 'bW'))
194 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000196 " Indent if current line begins with 'val':
197 elseif line =~ '^\s*val\>'
198 if lline !~ '^\s*\(exception\|external\|include\|open\)\>\|' . s:obj . '\|' . s:letlim
199 return indent(search('^\s*\(\(exception\|include\|initializer\|method\|open\|type\|val\)\>\|external\>.*:\)', 'bW'))
200 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000202 " Indent if current line begins with 'constraint', 'inherit', 'initializer'
203 " or 'method':
204 elseif line =~ '^\s*\(constraint\|inherit\|initializer\|method\)\>'
205 if lline !~ s:obj
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200206 return indent(search('\<\(object\|object\s*(.*)\)\s*$', 'bW')) + shiftwidth()
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000207 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000209 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000211 " Add a 'shiftwidth' after lines ending with:
212 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 +0200213 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000215 " Back to normal indent after lines ending with ';;':
216 elseif lline =~ ';;\s*$' && lline !~ '^\s*;;'
217 let ind = s:GetInd(v:lnum, s:letpat, s:letlim)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000219 " Back to normal indent after lines ending with 'end':
220 elseif lline =~ '\<end\s*$'
221 let ind = s:FindPair(s:module, '','\<end\>')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000223 " Back to normal indent after lines ending with 'in':
224 elseif lline =~ '\<in\s*$' && lline !~ '^\s*in\>'
225 let ind = s:FindPair('\<let\>', '', '\<in\>')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000227 " Back to normal indent after lines ending with 'done':
228 elseif lline =~ '\<done\s*$'
229 let ind = s:FindPair('\<do\>', '','\<done\>')
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 lines ending with ']', '|]' or '>]':
236 elseif lline =~ '\(\||\|>\)\]\s*$'
237 let ind = s:FindPair('\[', '','\]')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000239 " Back to normal indent after comments:
240 elseif lline =~ '\*)\s*$'
241 call search('\*)', 'bW')
242 let ind = indent(searchpair('(\*', '', '\*)', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000244 " Back to normal indent after lines ending with ')':
245 elseif lline =~ ')\s*$'
246 let ind = s:FindPair('(', '',')')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000248 " If this is a multiline comment then align '*':
249 elseif lline =~ '^\s*(\*' && line =~ '^\s*\*'
250 let ind = ind + 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100252 else
253 " Don't change indentation of this line
254 " for new lines (indent==0) use indentation of previous line
255
256 " This is for preventing removing indentation of these args:
257 " let f x =
258 " let y = x + 1 in
259 " Printf.printf
260 " "o" << here
261 " "oeuth" << don't touch indentation
262
263 let i = indent(v:lnum)
264 return i == 0 ? ind : i
265
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000266 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000268 " Subtract a 'shiftwidth' after lines matching 'match ... with parser':
269 if lline =~ '\<match\>.*\<with\>\s*\<parser\s*$'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200270 let ind = ind - shiftwidth()
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000271 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272
Bram Moolenaar95b28ec2005-10-11 20:32:28 +0000273 return ind
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
275endfunction
276
277" vim:sw=2