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>
|
23466
|
6 " URL: https://github.com/ocaml/vim-ocaml
|
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"))
|
23466
|
33 setlocal comments=sr:(*\ ,mb:\ ,ex:*)
|
|
34 setlocal comments^=sr:(**,mb:\ \ ,ex:*)
|
551
|
35 setlocal fo=cqort
|
|
36 endif
|
7
|
37 endif
|
|
38
|
|
39 " Only define the function once.
|
|
40 if exists("*GetOCamlIndent")
|
551
|
41 finish
|
7
|
42 endif
|
|
43
|
|
44 " Define some patterns:
|
20
|
45 let s:beflet = '^\s*\(initializer\|method\|try\)\|\(\<\(begin\|do\|else\|in\|then\|try\)\|->\|<-\|=\|;\|(\)\s*$'
|
7
|
46 let s:letpat = '^\s*\(let\|type\|module\|class\|open\|exception\|val\|include\|external\)\>'
|
|
47 let s:letlim = '\(\<\(sig\|struct\)\|;;\)\s*$'
|
|
48 let s:lim = '^\s*\(exception\|external\|include\|let\|module\|open\|type\|val\)\>'
|
|
49 let s:module = '\<\%(begin\|sig\|struct\|object\)\>'
|
|
50 let s:obj = '^\s*\(constraint\|inherit\|initializer\|method\|val\)\>\|\<\(object\|object\s*(.*)\)\s*$'
|
20
|
51 let s:type = '^\s*\%(class\|let\|type\)\>.*='
|
7
|
52
|
|
53 " Skipping pattern, for comments
|
3312
|
54 function! s:GetLineWithoutFullComment(lnum)
|
551
|
55 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
|
7
|
62 endfunction
|
|
63
|
|
64 " Indent for ';;' to match multiple 'let'
|
3312
|
65 function! s:GetInd(lnum, pat, lim)
|
551
|
66 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
|
7
|
77 endfunction
|
|
78
|
|
79 " Indent pairs
|
3312
|
80 function! s:FindPair(pstart, pmid, pend)
|
551
|
81 call search(a:pend, 'bW')
|
|
82 return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"'))
|
7
|
83 endfunction
|
|
84
|
|
85 " Indent 'let'
|
3312
|
86 function! s:FindLet(pstart, pmid, pend)
|
551
|
87 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'))
|
7
|
89 endfunction
|
|
90
|
3312
|
91 function! GetOCamlIndent()
|
551
|
92 " Find a non-commented line above the current line.
|
|
93 let lnum = s:GetLineWithoutFullComment(v:lnum)
|
7
|
94
|
551
|
95 " At the start of the file use zero indent.
|
|
96 if lnum == 0
|
|
97 return 0
|
|
98 endif
|
7
|
99
|
551
|
100 let ind = indent(lnum)
|
|
101 let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
|
7
|
102
|
551
|
103 " Return double 'shiftwidth' after lines matching:
|
|
104 if lline =~ '^\s*|.*->\s*$'
|
11518
|
105 return ind + 2 * shiftwidth()
|
551
|
106 endif
|
7
|
107
|
551
|
108 let line = getline(v:lnum)
|
7
|
109
|
551
|
110 " Indent if current line begins with 'end':
|
|
111 if line =~ '^\s*end\>'
|
|
112 return s:FindPair(s:module, '','\<end\>')
|
7
|
113
|
551
|
114 " Indent if current line begins with 'done' for 'do':
|
|
115 elseif line =~ '^\s*done\>'
|
|
116 return s:FindPair('\<do\>', '','\<done\>')
|
7
|
117
|
551
|
118 " Indent if current line begins with '}' or '>}':
|
|
119 elseif line =~ '^\s*\(\|>\)}'
|
|
120 return s:FindPair('{', '','}')
|
7
|
121
|
551
|
122 " Indent if current line begins with ']', '|]' or '>]':
|
|
123 elseif line =~ '^\s*\(\||\|>\)\]'
|
|
124 return s:FindPair('\[', '','\]')
|
7
|
125
|
551
|
126 " Indent if current line begins with ')':
|
|
127 elseif line =~ '^\s*)'
|
|
128 return s:FindPair('(', '',')')
|
7
|
129
|
551
|
130 " 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
|
7
|
135
|
551
|
136 " 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
|
7
|
141
|
551
|
142 " 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
|
7
|
148
|
551
|
149 " 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
|
7
|
154
|
551
|
155 " 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
|
7
|
160
|
551
|
161 " 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
|
7
|
166
|
551
|
167 " 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
|
7
|
172
|
551
|
173 " Indent if current line begins with 'and':
|
|
174 elseif line =~ '^\s*and\>'
|
|
175 if lline !~ '^\s*\(and\|let\|type\)\>\|\<end\s*$'
|
11518
|
176 return ind - shiftwidth()
|
551
|
177 endif
|
7
|
178
|
551
|
179 " 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
|
7
|
184
|
551
|
185 " 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
|
7
|
192
|
551
|
193 " 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
|
7
|
198
|
551
|
199 " 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
|
11518
|
203 return indent(search('\<\(object\|object\s*(.*)\)\s*$', 'bW')) + shiftwidth()
|
551
|
204 endif
|
|
205
|
|
206 endif
|
|
207
|
|
208 " 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*$'
|
11518
|
210 let ind = ind + shiftwidth()
|
7
|
211
|
551
|
212 " 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)
|
7
|
215
|
551
|
216 " Back to normal indent after lines ending with 'end':
|
|
217 elseif lline =~ '\<end\s*$'
|
|
218 let ind = s:FindPair(s:module, '','\<end\>')
|
7
|
219
|
551
|
220 " Back to normal indent after lines ending with 'in':
|
|
221 elseif lline =~ '\<in\s*$' && lline !~ '^\s*in\>'
|
|
222 let ind = s:FindPair('\<let\>', '', '\<in\>')
|
7
|
223
|
551
|
224 " Back to normal indent after lines ending with 'done':
|
|
225 elseif lline =~ '\<done\s*$'
|
|
226 let ind = s:FindPair('\<do\>', '','\<done\>')
|
7
|
227
|
551
|
228 " Back to normal indent after lines ending with '}' or '>}':
|
|
229 elseif lline =~ '\(\|>\)}\s*$'
|
|
230 let ind = s:FindPair('{', '','}')
|
7
|
231
|
551
|
232 " Back to normal indent after lines ending with ']', '|]' or '>]':
|
|
233 elseif lline =~ '\(\||\|>\)\]\s*$'
|
|
234 let ind = s:FindPair('\[', '','\]')
|
7
|
235
|
551
|
236 " 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"'))
|
7
|
240
|
551
|
241 " Back to normal indent after lines ending with ')':
|
|
242 elseif lline =~ ')\s*$'
|
|
243 let ind = s:FindPair('(', '',')')
|
7
|
244
|
551
|
245 " If this is a multiline comment then align '*':
|
|
246 elseif lline =~ '^\s*(\*' && line =~ '^\s*\*'
|
|
247 let ind = ind + 1
|
7
|
248
|
3312
|
249 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
|
551
|
263 endif
|
7
|
264
|
551
|
265 " Subtract a 'shiftwidth' after lines matching 'match ... with parser':
|
|
266 if lline =~ '\<match\>.*\<with\>\s*\<parser\s*$'
|
11518
|
267 let ind = ind - shiftwidth()
|
551
|
268 endif
|
7
|
269
|
551
|
270 return ind
|
7
|
271
|
|
272 endfunction
|
|
273
|
|
274 " vim:sw=2
|