comparison runtime/indent/ocaml.vim @ 551:7dfd6f1443a7

updated for version 7.0156
author vimboss
date Tue, 11 Oct 2005 20:32:28 +0000
parents 4ac1dce8dd5e
children 8cd729851562
comparison
equal deleted inserted replaced
550:74c2ea3c3081 551:7dfd6f1443a7
1 " Vim indent file 1 " Vim indent file
2 " Language: OCaml 2 " Language: OCaml
3 " Maintainers: Jean-Francois Yuen <jfyuen@happycoders.org> 3 " Maintainers: Jean-Francois Yuen <jfyuen@happycoders.org>
4 " Mike Leary <leary@nwlink.com> 4 " Mike Leary <leary@nwlink.com>
5 " Markus Mottl <markus@oefai.at> 5 " Markus Mottl <markus.mottl@gmail.com>
6 " URL: http://www.oefai.at/~markus/vim/indent/ocaml.vim 6 " URL: http://www.ocaml.info/vim/indent/ocaml.vim
7 " Last Change: 2004 Apr 11 - Added indent for 'class' (JY) 7 " Last Change: 2005 Jun 25 - Fixed multiple bugs due to 'else\nreturn ind' working
8 " 2003 Sep 16 - Added 'private' as keyword (JY) 8 " 2005 May 09 - Added an option to not indent OCaml-indents specially (MM)
9 " 2003 Mar 29 - Fixed bug with 'if' and 'else' (JY) 9 " 2005 Apr 11 - Fixed an indentation bug concerning "let" (MM)
10 10
11 " Only load this indent file when no other was loaded. 11 " Only load this indent file when no other was loaded.
12 if exists("b:did_indent") 12 if exists("b:did_indent")
13 finish 13 finish
14 endif 14 endif
15 let b:did_indent = 1 15 let b:did_indent = 1
16 16
17 setlocal expandtab 17 setlocal expandtab
18 setlocal indentexpr=GetOCamlIndent() 18 setlocal indentexpr=GetOCamlIndent()
20 setlocal nolisp 20 setlocal nolisp
21 setlocal nosmartindent 21 setlocal nosmartindent
22 setlocal textwidth=80 22 setlocal textwidth=80
23 23
24 " Comment formatting 24 " Comment formatting
25 if (has("comments")) 25 if !exists("no_ocaml_comments")
26 setlocal comments=sr:(*,mb:*,ex:*) 26 if (has("comments"))
27 setlocal fo=cqort 27 setlocal comments=sr:(*,mb:*,ex:*)
28 setlocal fo=cqort
29 endif
28 endif 30 endif
29 31
30 " Only define the function once. 32 " Only define the function once.
31 if exists("*GetOCamlIndent") 33 if exists("*GetOCamlIndent")
32 finish 34 finish
33 endif 35 endif
34 36
35 " Define some patterns: 37 " Define some patterns:
36 let s:beflet = '^\s*\(initializer\|method\|try\)\|\(\<\(begin\|do\|else\|in\|then\|try\)\|->\|<-\|=\|;\|(\)\s*$' 38 let s:beflet = '^\s*\(initializer\|method\|try\)\|\(\<\(begin\|do\|else\|in\|then\|try\)\|->\|<-\|=\|;\|(\)\s*$'
37 let s:letpat = '^\s*\(let\|type\|module\|class\|open\|exception\|val\|include\|external\)\>' 39 let s:letpat = '^\s*\(let\|type\|module\|class\|open\|exception\|val\|include\|external\)\>'
40 let s:module = '\<\%(begin\|sig\|struct\|object\)\>' 42 let s:module = '\<\%(begin\|sig\|struct\|object\)\>'
41 let s:obj = '^\s*\(constraint\|inherit\|initializer\|method\|val\)\>\|\<\(object\|object\s*(.*)\)\s*$' 43 let s:obj = '^\s*\(constraint\|inherit\|initializer\|method\|val\)\>\|\<\(object\|object\s*(.*)\)\s*$'
42 let s:type = '^\s*\%(class\|let\|type\)\>.*=' 44 let s:type = '^\s*\%(class\|let\|type\)\>.*='
43 45
44 " Skipping pattern, for comments 46 " Skipping pattern, for comments
45 function s:SkipPattern(lnum, pat) 47 function s:GetLineWithoutFullComment(lnum)
46 let def = prevnonblank(a:lnum - 1) 48 let lnum = prevnonblank(a:lnum - 1)
47 while def > 0 && getline(def) =~ a:pat 49 let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
48 let def = prevnonblank(def - 1) 50 while lline =~ '^\s*$' && lnum > 0
49 endwhile 51 let lnum = prevnonblank(lnum - 1)
50 return def 52 let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
53 endwhile
54 return lnum
51 endfunction 55 endfunction
52 56
53 " Indent for ';;' to match multiple 'let' 57 " Indent for ';;' to match multiple 'let'
54 function s:GetInd(lnum, pat, lim) 58 function s:GetInd(lnum, pat, lim)
55 let llet = search(a:pat, 'bW') 59 let llet = search(a:pat, 'bW')
56 let old = indent(a:lnum) 60 let old = indent(a:lnum)
57 while llet > 0 61 while llet > 0
58 let old = indent(llet) 62 let old = indent(llet)
59 let nb = s:SkipPattern(llet, '^\s*(\*.*\*)\s*$') 63 let nb = s:GetLineWithoutFullComment(llet)
60 if getline(nb) =~ a:lim 64 if getline(nb) =~ a:lim
61 return old 65 return old
62 endif 66 endif
63 let llet = search(a:pat, 'bW') 67 let llet = search(a:pat, 'bW')
64 endwhile 68 endwhile
65 return old 69 return old
66 endfunction 70 endfunction
67 71
68 " Indent pairs 72 " Indent pairs
69 function s:FindPair(pstart, pmid, pend) 73 function s:FindPair(pstart, pmid, pend)
70 call search(a:pend, 'bW') 74 call search(a:pend, 'bW')
71 return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"')) 75 return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"'))
72 endfunction 76 endfunction
73 77
74 " Indent 'let' 78 " Indent 'let'
75 function s:FindLet(pstart, pmid, pend) 79 function s:FindLet(pstart, pmid, pend)
76 call search(a:pend, 'bW') 80 call search(a:pend, 'bW')
77 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')) 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'))
78 endfunction 82 endfunction
79 83
80 function GetOCamlIndent() 84 function GetOCamlIndent()
81 " Find a non-blank line above the current line. 85 " Find a non-commented line above the current line.
82 let lnum = prevnonblank(v:lnum - 1) 86 let lnum = s:GetLineWithoutFullComment(v:lnum)
83 87
84 " At the start of the file use zero indent. 88 " At the start of the file use zero indent.
85 if lnum == 0 89 if lnum == 0
86 return 0 90 return 0
87 endif 91 endif
88 92
89 let ind = indent(lnum) 93 let ind = indent(lnum)
90 let lline = getline(lnum) 94 let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
91 95
92 " Return double 'shiftwidth' after lines matching: 96 " Return double 'shiftwidth' after lines matching:
93 if lline =~ '^\s*|.*->\s*$' 97 if lline =~ '^\s*|.*->\s*$'
94 return ind + &sw + &sw 98 return ind + &sw + &sw
95 endif 99 endif
96 100
97 let line = getline(v:lnum) 101 let line = getline(v:lnum)
98 102
99 " Indent if current line begins with 'end': 103 " Indent if current line begins with 'end':
100 if line =~ '^\s*end\>' 104 if line =~ '^\s*end\>'
101 return s:FindPair(s:module, '','\<end\>') 105 return s:FindPair(s:module, '','\<end\>')
102 106
103 " Indent if current line begins with 'done' for 'do': 107 " Indent if current line begins with 'done' for 'do':
104 elseif line =~ '^\s*done\>' 108 elseif line =~ '^\s*done\>'
105 return s:FindPair('\<do\>', '','\<done\>') 109 return s:FindPair('\<do\>', '','\<done\>')
106 110
107 " Indent if current line begins with '}' or '>}': 111 " Indent if current line begins with '}' or '>}':
108 elseif line =~ '^\s*\(\|>\)}' 112 elseif line =~ '^\s*\(\|>\)}'
109 return s:FindPair('{', '','}') 113 return s:FindPair('{', '','}')
110 114
111 " Indent if current line begins with ']', '|]' or '>]': 115 " Indent if current line begins with ']', '|]' or '>]':
112 elseif line =~ '^\s*\(\||\|>\)\]' 116 elseif line =~ '^\s*\(\||\|>\)\]'
113 return s:FindPair('\[', '','\]') 117 return s:FindPair('\[', '','\]')
114 118
115 " Indent if current line begins with ')': 119 " Indent if current line begins with ')':
116 elseif line =~ '^\s*)' 120 elseif line =~ '^\s*)'
117 return s:FindPair('(', '',')') 121 return s:FindPair('(', '',')')
118 122
119 " Indent if current line begins with 'let': 123 " Indent if current line begins with 'let':
120 elseif line =~ '^\s*let\>' 124 elseif line =~ '^\s*let\>'
121 if lline !~ s:lim . '\|' . s:letlim . '\|' . s:beflet 125 if lline !~ s:lim . '\|' . s:letlim . '\|' . s:beflet
122 return s:FindLet(s:type, '','\<let\s*$') 126 return s:FindLet(s:type, '','\<let\s*$')
123 else return ind 127 endif
124 endif 128
125 129 " Indent if current line begins with 'class' or 'type':
126 " Indent if current line begins with 'class' or 'type': 130 elseif line =~ '^\s*\(class\|type\)\>'
127 elseif line =~ '^\s*\(class\|type\)\>' 131 if lline !~ s:lim . '\|\<and\s*$\|' . s:letlim
128 if lline !~ s:lim . '\|\<and\s*$\|' . s:letlim 132 return s:FindLet(s:type, '','\<\(class\|type\)\s*$')
129 return s:FindLet(s:type, '','\<\(class\|type\)\s*$') 133 endif
130 else return ind 134
131 endif 135 " Indent for pattern matching:
132 136 elseif line =~ '^\s*|'
133 " Indent for pattern matching: 137 if lline !~ '^\s*\(|[^\]]\|\(match\|type\|with\)\>\)\|\<\(function\|parser\|private\|with\)\s*$'
134 elseif line =~ '^\s*|' 138 call search('|', 'bW')
135 if lline !~ '^\s*\(|[^\]]\|\(match\|type\|with\)\>\)\|\<\(function\|parser\|private\|with\)\s*$' 139 return indent(searchpair('^\s*\(match\|type\)\>\|\<\(function\|parser\|private\|with\)\s*$', '', '^\s*|', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment" || getline(".") !~ "^\\s*|.*->"'))
136 call search('|', 'bW') 140 endif
137 return indent(searchpair('^\s*\(match\|type\)\>\|\<\(function\|parser\|private\|with\)\s*$', '', '^\s*|', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment" || getline(".") !~ "^\\s*|.*->"')) 141
138 else return ind 142 " Indent if current line begins with ';;':
139 endif 143 elseif line =~ '^\s*;;'
140 144 if lline !~ ';;\s*$'
141 " Indent if current line begins with ';;': 145 return s:GetInd(v:lnum, s:letpat, s:letlim)
142 elseif line =~ '^\s*;;' 146 endif
143 if lline !~ ';;\s*$' 147
144 return s:GetInd(v:lnum, s:letpat, s:letlim) 148 " Indent if current line begins with 'in':
145 else return ind 149 elseif line =~ '^\s*in\>'
146 endif 150 if lline !~ '^\s*\(let\|and\)\>'
147 151 return s:FindPair('\<let\>', '', '\<in\>')
148 " Indent if current line begins with 'in': 152 endif
149 elseif line =~ '^\s*in\>' 153
150 if lline !~ '^\s*\(let\|and\)\>' 154 " Indent if current line begins with 'else':
151 return s:FindPair('\<let\>', '', '\<in\>') 155 elseif line =~ '^\s*else\>'
152 else return ind 156 if lline !~ '^\s*\(if\|then\)\>'
153 endif 157 return s:FindPair('\<if\>', '', '\<else\>')
154 158 endif
155 " Indent if current line begins with 'else': 159
156 elseif line =~ '^\s*else\>' 160 " Indent if current line begins with 'then':
157 if lline !~ '^\s*\(if\|then\)\>' 161 elseif line =~ '^\s*then\>'
158 return s:FindPair('\<if\>', '', '\<else\>') 162 if lline !~ '^\s*\(if\|else\)\>'
159 else return ind 163 return s:FindPair('\<if\>', '', '\<then\>')
160 endif 164 endif
161 165
162 " Indent if current line begins with 'then': 166 " Indent if current line begins with 'and':
163 elseif line =~ '^\s*then\>' 167 elseif line =~ '^\s*and\>'
164 if lline !~ '^\s*\(if\|else\)\>' 168 if lline !~ '^\s*\(and\|let\|type\)\>\|\<end\s*$'
165 return s:FindPair('\<if\>', '', '\<then\>') 169 return ind - &sw
166 else return ind 170 endif
167 endif 171
168 172 " Indent if current line begins with 'with':
169 " Indent if current line begins with 'and': 173 elseif line =~ '^\s*with\>'
170 elseif line =~ '^\s*and\>' 174 if lline !~ '^\s*\(match\|try\)\>'
171 if lline !~ '^\s*\(and\|let\|type\)\>\|\<end\s*$' 175 return s:FindPair('\<\%(match\|try\)\>', '','\<with\>')
172 return ind - &sw 176 endif
173 else return ind 177
174 endif 178 " Indent if current line begins with 'exception', 'external', 'include' or
175 179 " 'open':
176 " Indent if current line begins with 'with': 180 elseif line =~ '^\s*\(exception\|external\|include\|open\)\>'
177 elseif line =~ '^\s*with\>' 181 if lline !~ s:lim . '\|' . s:letlim
178 if lline !~ '^\s*\(match\|try\)\>' 182 call search(line)
179 return s:FindPair('\<\%(match\|try\)\>', '','\<with\>') 183 return indent(search('^\s*\(\(exception\|external\|include\|open\|type\)\>\|val\>.*:\)', 'bW'))
180 else return ind 184 endif
181 endif 185
182 186 " Indent if current line begins with 'val':
183 " Indent if current line begins with 'exception': 187 elseif line =~ '^\s*val\>'
184 elseif line =~ '^\s*exception\>' 188 if lline !~ '^\s*\(exception\|external\|include\|open\)\>\|' . s:obj . '\|' . s:letlim
185 if lline !~ s:lim . '\|' . s:letlim 189 return indent(search('^\s*\(\(exception\|include\|initializer\|method\|open\|type\|val\)\>\|external\>.*:\)', 'bW'))
186 return indent(search('^\s*\(\(external\|include\|open\|type\)\>\|val\>.*:\)', 'bW')) 190 endif
187 else return ind 191
188 endif 192 " Indent if current line begins with 'constraint', 'inherit', 'initializer'
189 193 " or 'method':
190 " Indent if current line begins with 'external': 194 elseif line =~ '^\s*\(constraint\|inherit\|initializer\|method\)\>'
191 elseif line =~ '^\s*external\>' 195 if lline !~ s:obj
192 if lline !~ s:lim . '\|' . s:letlim 196 return indent(search('\<\(object\|object\s*(.*)\)\s*$', 'bW')) + &sw
193 return indent(search('^\s*\(\(exception\|external\|include\|open\|type\)\>\|val\>.*:\)', 'bW')) 197 endif
194 else return ind 198
195 endif 199 endif
196 200
197 " Indent if current line begins with 'include': 201 " Add a 'shiftwidth' after lines ending with:
198 elseif line =~ '^\s*include\>' 202 if lline =~ '\(:\|=\|->\|<-\|(\|\[\|{\|{<\|\[|\|\[<\|\<\(begin\|do\|else\|fun\|function\|functor\|if\|initializer\|object\|parser\|private\|sig\|struct\|then\|try\)\|\<object\s*(.*)\)\s*$'
199 if lline !~ s:lim . '\|' . s:letlim 203 let ind = ind + &sw
200 return indent(search('^\s*\(\(exception\|external\|open\|type\)\>\|val\>.*:\)', 'bW')) 204
201 else return ind 205 " Back to normal indent after lines ending with ';;':
202 endif 206 elseif lline =~ ';;\s*$' && lline !~ '^\s*;;'
203 207 let ind = s:GetInd(v:lnum, s:letpat, s:letlim)
204 " Indent if current line begins with 'open': 208
205 elseif line =~ '^\s*open\>' 209 " Back to normal indent after lines ending with 'end':
206 if lline !~ s:lim . '\|' . s:letlim 210 elseif lline =~ '\<end\s*$'
207 return indent(search('^\s*\(\(exception\|external\|include\|type\)\>\|val\>.*:\)', 'bW')) 211 let ind = s:FindPair(s:module, '','\<end\>')
208 else return ind 212
209 endif 213 " Back to normal indent after lines ending with 'in':
210 214 elseif lline =~ '\<in\s*$' && lline !~ '^\s*in\>'
211 " Indent if current line begins with 'val': 215 let ind = s:FindPair('\<let\>', '', '\<in\>')
212 elseif line =~ '^\s*val\>' 216
213 if lline !~ '^\s*\(exception\|external\|include\|open\)\>\|' . s:obj . '\|' . s:letlim 217 " Back to normal indent after lines ending with 'done':
214 return indent(search('^\s*\(\(exception\|include\|initializer\|method\|open\|type\|val\)\>\|external\>.*:\)', 'bW')) 218 elseif lline =~ '\<done\s*$'
215 else return ind 219 let ind = s:FindPair('\<do\>', '','\<done\>')
216 endif 220
217 221 " Back to normal indent after lines ending with '}' or '>}':
218 " Indent if current line begins with 'constraint': 222 elseif lline =~ '\(\|>\)}\s*$'
219 elseif line =~ '^\s*constraint\>' 223 let ind = s:FindPair('{', '','}')
220 if lline !~ s:obj 224
221 return indent(search('^\s*\(inherit\|initializer\|method\|val\)\>', 'bW')) 225 " Back to normal indent after lines ending with ']', '|]' or '>]':
222 else return ind 226 elseif lline =~ '\(\||\|>\)\]\s*$'
223 endif 227 let ind = s:FindPair('\[', '','\]')
224 228
225 " Indent if current line begins with 'inherit': 229 " Back to normal indent after comments:
226 elseif line =~ '^\s*inherit\>' 230 elseif lline =~ '\*)\s*$'
227 if lline !~ s:obj 231 call search('\*)', 'bW')
228 return indent(search('^\s*\(constraint\|initializer\|method\|val\)\>', 'bW')) 232 let ind = indent(searchpair('(\*', '', '\*)', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))
229 else return ind 233
230 endif 234 " Back to normal indent after lines ending with ')':
231 235 elseif lline =~ ')\s*$'
232 " Indent if current line begins with 'inherit': 236 let ind = s:FindPair('(', '',')')
233 elseif line =~ '^\s*initializer\>' 237
234 if lline !~ s:obj 238 " If this is a multiline comment then align '*':
235 return indent(search('^\s*\(constraint\|inherit\|method\|val\)\>', 'bW')) 239 elseif lline =~ '^\s*(\*' && line =~ '^\s*\*'
236 else return ind 240 let ind = ind + 1
237 endif 241
238 242 endif
239 " Indent if current line begins with 'method': 243
240 elseif line =~ '^\s*method\>' 244 " Subtract a 'shiftwidth' after lines matching 'match ... with parser':
241 if lline !~ s:obj 245 if lline =~ '\<match\>.*\<with\>\s*\<parser\s*$'
242 return indent(search('^\s*\(\(constraint\|inherit\|initializer\|val\)\>\|method\>.*\(:\|=\)\)', 'bW')) 246 let ind = ind - &sw
243 else return ind 247 endif
244 endif 248
245 249 return ind
246 endif
247
248 " Add a 'shiftwidth' after lines ending with:
249 if lline =~ '\(:\|=\|->\|<-\|(\|\[\|{\|{<\|\[|\|\[<\|\<\(begin\|do\|else\|fun\|function\|functor\|if\|initializer\|object\|parser\|private\|sig\|struct\|then\|try\)\|\<object\s*(.*)\)\s*$'
250 let ind = ind + &sw
251
252 " Back to normal indent after lines ending with ';;':
253 elseif lline =~ ';;\s*$' && lline !~ '^\s*;;'
254 let ind = s:GetInd(v:lnum, s:letpat, s:letlim)
255
256 " Back to normal indent after lines ending with 'end':
257 elseif lline =~ '\<end\s*$'
258 let ind = s:FindPair(s:module, '','\<end\>')
259
260 " Back to normal indent after lines ending with 'in':
261 elseif lline =~ '\<in\s*$' && lline !~ '^\s*in\>'
262 let ind = s:FindPair('\<let\>', '', '\<in\>')
263
264 " Back to normal indent after lines ending with 'done':
265 elseif lline =~ '\<done\s*$'
266 let ind = s:FindPair('\<do\>', '','\<done\>')
267
268 " Back to normal indent after lines ending with '}' or '>}':
269 elseif lline =~ '\(\|>\)}\s*$'
270 let ind = s:FindPair('{', '','}')
271
272 " Back to normal indent after lines ending with ']', '|]' or '>]':
273 elseif lline =~ '\(\||\|>\)\]\s*$'
274 let ind = s:FindPair('\[', '','\]')
275
276 " Back to normal indent after comments:
277 elseif lline =~ '\*)\s*$'
278 call search('\*)', 'bW')
279 let ind = indent(searchpair('(\*', '', '\*)', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))
280
281 " Back to normal indent after lines ending with ')':
282 elseif lline =~ ')\s*$'
283 let ind = s:FindPair('(', '',')')
284
285 endif
286
287 " Subtract a 'shiftwidth' after lines matching 'match ... with parser':
288 if lline =~ '^\s*match\>.*\<with\>\s*\<parser\s*$'
289 let ind = ind - &sw
290 endif
291
292 return ind
293 250
294 endfunction 251 endfunction
295 252
296 " vim:sw=2 253 " vim:sw=2