7
|
1 " Vim indent file
|
20
|
2 " Language: OCaml
|
3312
|
3 " 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
|
11518
|
7 " Last Change: 2017 Jun 13
|
3312
|
8 " 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)
|
5055
|
10 " 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 ..
|
7
|
14
|
|
15 " Only load this indent file when no other was loaded.
|
|
16 if exists("b:did_indent")
|
551
|
17 finish
|
7
|
18 endif
|
|
19 let b:did_indent = 1
|
|
20
|
|
21 setlocal expandtab
|
|
22 setlocal indentexpr=GetOCamlIndent()
|
20
|
23 setlocal 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)
|
7
|
24 setlocal nolisp
|
|
25 setlocal nosmartindent
|
5055
|
26
|
|
27 " At least Marc Weber and Markus Mottl do not like this:
|
|
28 " setlocal textwidth=80
|
7
|
29
|
|
30 " Comment formatting
|
551
|
31 if !exists("no_ocaml_comments")
|
|
32 if (has("comments"))
|
|
33 setlocal comments=sr:(*,mb:*,ex:*)
|
|
34 setlocal fo=cqort
|
|
35 endif
|
7
|
36 endif
|
|
37
|
|
38 " Only define the function once.
|
|
39 if exists("*GetOCamlIndent")
|
551
|
40 finish
|
7
|
41 endif
|
|
42
|
|
43 " Define some patterns:
|
20
|
44 let s:beflet = '^\s*\(initializer\|method\|try\)\|\(\<\(begin\|do\|else\|in\|then\|try\)\|->\|<-\|=\|;\|(\)\s*$'
|
7
|
45 let s:letpat = '^\s*\(let\|type\|module\|class\|open\|exception\|val\|include\|external\)\>'
|
|
46 let s:letlim = '\(\<\(sig\|struct\)\|;;\)\s*$'
|
|
47 let s:lim = '^\s*\(exception\|external\|include\|let\|module\|open\|type\|val\)\>'
|
|
48 let s:module = '\<\%(begin\|sig\|struct\|object\)\>'
|
|
49 let s:obj = '^\s*\(constraint\|inherit\|initializer\|method\|val\)\>\|\<\(object\|object\s*(.*)\)\s*$'
|
20
|
50 let s:type = '^\s*\%(class\|let\|type\)\>.*='
|
7
|
51
|
|
52 " Skipping pattern, for comments
|
3312
|
53 function! s:GetLineWithoutFullComment(lnum)
|
551
|
54 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
|
7
|
61 endfunction
|
|
62
|
|
63 " Indent for ';;' to match multiple 'let'
|
3312
|
64 function! s:GetInd(lnum, pat, lim)
|
551
|
65 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
|
7
|
76 endfunction
|
|
77
|
|
78 " Indent pairs
|
3312
|
79 function! s:FindPair(pstart, pmid, pend)
|
551
|
80 call search(a:pend, 'bW')
|
|
81 return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"'))
|
7
|
82 endfunction
|
|
83
|
|
84 " Indent 'let'
|
3312
|
85 function! s:FindLet(pstart, pmid, pend)
|
551
|
86 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'))
|
7
|
88 endfunction
|
|
89
|
3312
|
90 function! GetOCamlIndent()
|
551
|
91 " Find a non-commented line above the current line.
|
|
92 let lnum = s:GetLineWithoutFullComment(v:lnum)
|
7
|
93
|
551
|
94 " At the start of the file use zero indent.
|
|
95 if lnum == 0
|
|
96 return 0
|
|
97 endif
|
7
|
98
|
551
|
99 let ind = indent(lnum)
|
|
100 let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
|
7
|
101
|
551
|
102 " Return double 'shiftwidth' after lines matching:
|
|
103 if lline =~ '^\s*|.*->\s*$'
|
11518
|
104 return ind + 2 * shiftwidth()
|
551
|
105 endif
|
7
|
106
|
551
|
107 let line = getline(v:lnum)
|
7
|
108
|
551
|
109 " Indent if current line begins with 'end':
|
|
110 if line =~ '^\s*end\>'
|
|
111 return s:FindPair(s:module, '','\<end\>')
|
7
|
112
|
551
|
113 " Indent if current line begins with 'done' for 'do':
|
|
114 elseif line =~ '^\s*done\>'
|
|
115 return s:FindPair('\<do\>', '','\<done\>')
|
7
|
116
|
551
|
117 " Indent if current line begins with '}' or '>}':
|
|
118 elseif line =~ '^\s*\(\|>\)}'
|
|
119 return s:FindPair('{', '','}')
|
7
|
120
|
551
|
121 " Indent if current line begins with ']', '|]' or '>]':
|
|
122 elseif line =~ '^\s*\(\||\|>\)\]'
|
|
123 return s:FindPair('\[', '','\]')
|
7
|
124
|
551
|
125 " Indent if current line begins with ')':
|
|
126 elseif line =~ '^\s*)'
|
|
127 return s:FindPair('(', '',')')
|
7
|
128
|
551
|
129 " 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
|
7
|
134
|
551
|
135 " 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
|
7
|
140
|
551
|
141 " 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
|
7
|
147
|
551
|
148 " 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
|
7
|
153
|
551
|
154 " 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
|
7
|
159
|
551
|
160 " 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
|
7
|
165
|
551
|
166 " 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
|
7
|
171
|
551
|
172 " Indent if current line begins with 'and':
|
|
173 elseif line =~ '^\s*and\>'
|
|
174 if lline !~ '^\s*\(and\|let\|type\)\>\|\<end\s*$'
|
11518
|
175 return ind - shiftwidth()
|
551
|
176 endif
|
7
|
177
|
551
|
178 " 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
|
7
|
183
|
551
|
184 " 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
|
7
|
191
|
551
|
192 " 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
|
7
|
197
|
551
|
198 " 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
|
11518
|
202 return indent(search('\<\(object\|object\s*(.*)\)\s*$', 'bW')) + shiftwidth()
|
551
|
203 endif
|
|
204
|
|
205 endif
|
|
206
|
|
207 " 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*$'
|
11518
|
209 let ind = ind + shiftwidth()
|
7
|
210
|
551
|
211 " 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)
|
7
|
214
|
551
|
215 " Back to normal indent after lines ending with 'end':
|
|
216 elseif lline =~ '\<end\s*$'
|
|
217 let ind = s:FindPair(s:module, '','\<end\>')
|
7
|
218
|
551
|
219 " Back to normal indent after lines ending with 'in':
|
|
220 elseif lline =~ '\<in\s*$' && lline !~ '^\s*in\>'
|
|
221 let ind = s:FindPair('\<let\>', '', '\<in\>')
|
7
|
222
|
551
|
223 " Back to normal indent after lines ending with 'done':
|
|
224 elseif lline =~ '\<done\s*$'
|
|
225 let ind = s:FindPair('\<do\>', '','\<done\>')
|
7
|
226
|
551
|
227 " Back to normal indent after lines ending with '}' or '>}':
|
|
228 elseif lline =~ '\(\|>\)}\s*$'
|
|
229 let ind = s:FindPair('{', '','}')
|
7
|
230
|
551
|
231 " Back to normal indent after lines ending with ']', '|]' or '>]':
|
|
232 elseif lline =~ '\(\||\|>\)\]\s*$'
|
|
233 let ind = s:FindPair('\[', '','\]')
|
7
|
234
|
551
|
235 " 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"'))
|
7
|
239
|
551
|
240 " Back to normal indent after lines ending with ')':
|
|
241 elseif lline =~ ')\s*$'
|
|
242 let ind = s:FindPair('(', '',')')
|
7
|
243
|
551
|
244 " If this is a multiline comment then align '*':
|
|
245 elseif lline =~ '^\s*(\*' && line =~ '^\s*\*'
|
|
246 let ind = ind + 1
|
7
|
247
|
3312
|
248 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
|
551
|
262 endif
|
7
|
263
|
551
|
264 " Subtract a 'shiftwidth' after lines matching 'match ... with parser':
|
|
265 if lline =~ '\<match\>.*\<with\>\s*\<parser\s*$'
|
11518
|
266 let ind = ind - shiftwidth()
|
551
|
267 endif
|
7
|
268
|
551
|
269 return ind
|
7
|
270
|
|
271 endfunction
|
|
272
|
|
273 " vim:sw=2
|