comparison runtime/indent/ocaml.vim @ 20:4ac1dce8dd5e v7.0012

updated for version 7.0012
author vimboss
date Mon, 26 Jul 2004 12:53:41 +0000
parents 3fc0f57ecb91
children 7dfd6f1443a7
comparison
equal deleted inserted replaced
19:a81bc802c17c 20:4ac1dce8dd5e
1 " Vim indent file 1 " Vim indent file
2 " Language: OCaml 2 " Language: OCaml
3 " Maintainers: Jean-Francois Yuen <jfyuen@ifrance.com> 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@oefai.at>
6 " URL: http://www.oefai.at/~markus/vim/indent/ocaml.vim 6 " URL: http://www.oefai.at/~markus/vim/indent/ocaml.vim
7 " Last Change: 2003 Apr 14 7 " Last Change: 2004 Apr 11 - Added indent for 'class' (JY)
8 " 2003 Mar 05 - Added '{<' and some fixes (JY) 8 " 2003 Sep 16 - Added 'private' as keyword (JY)
9 " 2002 Nov 06 - Some fixes (JY) 9 " 2003 Mar 29 - Fixed bug with 'if' and 'else' (JY)
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()
19 setlocal indentkeys+=0=and,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\],0) 19 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)
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
31 if exists("*GetOCamlIndent") 31 if exists("*GetOCamlIndent")
32 finish 32 finish
33 endif 33 endif
34 34
35 " Define some patterns: 35 " Define some patterns:
36 let s:beflet = '^\s*\(initializer\|method\|try\)\|\(\<\(begin\|do\|else\|in\|then\|try\)\|->\|;\|(\)\s*$' 36 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\)\>' 37 let s:letpat = '^\s*\(let\|type\|module\|class\|open\|exception\|val\|include\|external\)\>'
38 let s:letlim = '\(\<\(sig\|struct\)\|;;\)\s*$' 38 let s:letlim = '\(\<\(sig\|struct\)\|;;\)\s*$'
39 let s:lim = '^\s*\(exception\|external\|include\|let\|module\|open\|type\|val\)\>' 39 let s:lim = '^\s*\(exception\|external\|include\|let\|module\|open\|type\|val\)\>'
40 let s:module = '\<\%(begin\|sig\|struct\|object\)\>' 40 let s:module = '\<\%(begin\|sig\|struct\|object\)\>'
41 let s:obj = '^\s*\(constraint\|inherit\|initializer\|method\|val\)\>\|\<\(object\|object\s*(.*)\)\s*$' 41 let s:obj = '^\s*\(constraint\|inherit\|initializer\|method\|val\)\>\|\<\(object\|object\s*(.*)\)\s*$'
42 let s:type = '^\s*\%(let\|type\)\>.*=' 42 let s:type = '^\s*\%(class\|let\|type\)\>.*='
43 let s:val = '^\s*\(val\|external\)\>.*:'
44 43
45 " Skipping pattern, for comments 44 " Skipping pattern, for comments
46 function s:SkipPattern(lnum, pat) 45 function s:SkipPattern(lnum, pat)
47 let def = prevnonblank(a:lnum - 1) 46 let def = prevnonblank(a:lnum - 1)
48 while def > 0 && getline(def) =~ a:pat 47 while def > 0 && getline(def) =~ a:pat
73 endfunction 72 endfunction
74 73
75 " Indent 'let' 74 " Indent 'let'
76 function s:FindLet(pstart, pmid, pend) 75 function s:FindLet(pstart, pmid, pend)
77 call search(a:pend, 'bW') 76 call search(a:pend, 'bW')
78 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*let\\>.*=\\s*$\\|" . s:beflet')) 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'))
79 endfunction 78 endfunction
80 79
81 function GetOCamlIndent() 80 function GetOCamlIndent()
82 " Find a non-blank line above the current line. 81 " Find a non-blank line above the current line.
83 let lnum = prevnonblank(v:lnum - 1) 82 let lnum = prevnonblank(v:lnum - 1)
95 return ind + &sw + &sw 94 return ind + &sw + &sw
96 endif 95 endif
97 96
98 let line = getline(v:lnum) 97 let line = getline(v:lnum)
99 98
100 " Indent if current line begins with 'end' 99 " Indent if current line begins with 'end':
101 " for 'sig', 'struct', 'object' and 'begin':
102 if line =~ '^\s*end\>' 100 if line =~ '^\s*end\>'
103 return s:FindPair(s:module, '','\<end\>') 101 return s:FindPair(s:module, '','\<end\>')
104 102
105 " Indent if current line begins with 'done' for 'do': 103 " Indent if current line begins with 'done' for 'do':
106 elseif line =~ '^\s*done\>' 104 elseif line =~ '^\s*done\>'
116 114
117 " Indent if current line begins with ')': 115 " Indent if current line begins with ')':
118 elseif line =~ '^\s*)' 116 elseif line =~ '^\s*)'
119 return s:FindPair('(', '',')') 117 return s:FindPair('(', '',')')
120 118
121 " Indent if current line begins with 'let' 119 " Indent if current line begins with 'let':
122 " and last line does not begin with 'let' or end with 'in' or ';;':
123 elseif line =~ '^\s*let\>' 120 elseif line =~ '^\s*let\>'
124 if lline !~ s:lim . '\|' . s:letlim . '\|' . s:beflet 121 if lline !~ s:lim . '\|' . s:letlim . '\|' . s:beflet
125 return s:FindLet(s:type, '','\<let\s*$') 122 return s:FindLet(s:type, '','\<let\s*$')
126 else return ind 123 else return ind
127 endif 124 endif
128 125
129 " Indent if current line begins with 'type' 126 " Indent if current line begins with 'class' or 'type':
130 " and last line does not end with 'and' or ';;': 127 elseif line =~ '^\s*\(class\|type\)\>'
131 elseif line =~ '^\s*type\>'
132 if lline !~ s:lim . '\|\<and\s*$\|' . s:letlim 128 if lline !~ s:lim . '\|\<and\s*$\|' . s:letlim
133 return s:FindLet(s:type, '','\<type\s*$') 129 return s:FindLet(s:type, '','\<\(class\|type\)\s*$')
134 else return ind 130 else return ind
135 endif 131 endif
136 132
137 " Indent for pattern matching: 133 " Indent for pattern matching:
138 elseif line =~ '^\s*|' 134 elseif line =~ '^\s*|'
139 if lline !~ '^\s*\(|\|\(match\|with\|type\)\>\)\|\<\(function\|parser\|with\)\s*$' 135 if lline !~ '^\s*\(|[^\]]\|\(match\|type\|with\)\>\)\|\<\(function\|parser\|private\|with\)\s*$'
140 call search('|', 'bW') 136 call search('|', 'bW')
141 return indent(searchpair('^\s*\(type\|match\)\>\|\<\(with\|function\|parser\)\s*$', '', '|', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment" || getline(".") =~ "\\[|\\||\\]" && getline(".") !~ "^\\s*|.*->"')) 137 return indent(searchpair('^\s*\(match\|type\)\>\|\<\(function\|parser\|private\|with\)\s*$', '', '^\s*|', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment" || getline(".") !~ "^\\s*|.*->"'))
142 else return ind 138 else return ind
143 endif 139 endif
144 140
145 " Indent if current line begins with ';;': 141 " Indent if current line begins with ';;':
146 elseif line =~ '^\s*;;' 142 elseif line =~ '^\s*;;'
147 if lline !~ ';;\s*$' 143 if lline !~ ';;\s*$'
148 return s:GetInd(v:lnum, s:letpat, s:letlim) 144 return s:GetInd(v:lnum, s:letpat, s:letlim)
149 else return ind 145 else return ind
150 endif 146 endif
151 147
152 " Indent if current line begins with 'in' and previous 148 " Indent if current line begins with 'in':
153 " line does not start with 'let' or 'and':
154 elseif line =~ '^\s*in\>' 149 elseif line =~ '^\s*in\>'
155 if lline !~ '^\s*\(let\|and\)\>' 150 if lline !~ '^\s*\(let\|and\)\>'
156 return s:FindPair('\<let\>', '', '\<in\>') 151 return s:FindPair('\<let\>', '', '\<in\>')
157 else return ind 152 else return ind
158 endif 153 endif
159 154
160 " Indent if current line begins with 'else' 155 " Indent if current line begins with 'else':
161 " and previous line does not start with 'if', 'then' or 'else':
162 elseif line =~ '^\s*else\>' 156 elseif line =~ '^\s*else\>'
163 if lline !~ '^\s*\(if\|else\|then\)\>' 157 if lline !~ '^\s*\(if\|then\)\>'
164 return s:FindPair('\<if\>', '', '\<else\>') 158 return s:FindPair('\<if\>', '', '\<else\>')
165 else return ind 159 else return ind
166 endif 160 endif
167 161
168 " Indent if current line begins with 'then' 162 " Indent if current line begins with 'then':
169 " and previous line does not start with 'if', 'then' or 'else':
170 elseif line =~ '^\s*then\>' 163 elseif line =~ '^\s*then\>'
171 if lline !~ '^\s*\(if\|else\|then\)\>' 164 if lline !~ '^\s*\(if\|else\)\>'
172 return s:FindPair('\<if\>', '', '\<then\>') 165 return s:FindPair('\<if\>', '', '\<then\>')
173 else return ind 166 else return ind
174 endif 167 endif
175 168
176 " Subtract a 'shiftwidth' if current line begins with 'and' and previous 169 " Indent if current line begins with 'and':
177 " line does not start with 'let', 'and' or 'type' or end with 'end'
178 " (for classes):
179 elseif line =~ '^\s*and\>' 170 elseif line =~ '^\s*and\>'
180 if lline !~ '^\s*\(and\|let\|type\)\>\|\<end\s*$' 171 if lline !~ '^\s*\(and\|let\|type\)\>\|\<end\s*$'
181 return ind - &sw 172 return ind - &sw
182 else return ind 173 else return ind
183 endif 174 endif
184 175
185 " Indent if current line begins with 'with' 176 " Indent if current line begins with 'with':
186 " and previous line does not start with 'match' or 'try':
187 elseif line =~ '^\s*with\>' 177 elseif line =~ '^\s*with\>'
188 if lline !~ '^\s*\(match\|try\)\>' 178 if lline !~ '^\s*\(match\|try\)\>'
189 return s:FindPair('\<\%(match\|try\)\>', '','\<with\>') 179 return s:FindPair('\<\%(match\|try\)\>', '','\<with\>')
190 else return ind 180 else return ind
191 endif 181 endif
192 182
193 " Indent if current line begins with 'exception': 183 " Indent if current line begins with 'exception':
194 elseif line =~ '^\s*exception\>' 184 elseif line =~ '^\s*exception\>'
195 if lline !~ s:lim . '\|' . s:letlim 185 if lline !~ s:lim . '\|' . s:letlim
196 return indent(search(s:val . '\|^\s*\(external\|include\|open\|type\)\>', 'bW')) 186 return indent(search('^\s*\(\(external\|include\|open\|type\)\>\|val\>.*:\)', 'bW'))
197 else return ind 187 else return ind
198 endif 188 endif
199 189
200 " Indent if current line begins with 'external': 190 " Indent if current line begins with 'external':
201 elseif line =~ '^\s*external\>' 191 elseif line =~ '^\s*external\>'
202 if lline !~ s:lim . '\|' . s:letlim 192 if lline !~ s:lim . '\|' . s:letlim
203 return indent(search(s:val . '\|^\s*\(exception\|include\|open\|type\)\>', 'bW')) 193 return indent(search('^\s*\(\(exception\|external\|include\|open\|type\)\>\|val\>.*:\)', 'bW'))
204 else return ind 194 else return ind
205 endif 195 endif
206 196
207 " Indent if current line begins with 'include': 197 " Indent if current line begins with 'include':
208 elseif line =~ '^\s*include\>' 198 elseif line =~ '^\s*include\>'
209 if lline !~ s:lim . '\|' . s:letlim 199 if lline !~ s:lim . '\|' . s:letlim
210 return indent(search(s:val . '\|^\s*\(exception\|external\|open\|type\)\>', 'bW')) 200 return indent(search('^\s*\(\(exception\|external\|open\|type\)\>\|val\>.*:\)', 'bW'))
211 else return ind 201 else return ind
212 endif 202 endif
213 203
214 " Indent if current line begins with 'open': 204 " Indent if current line begins with 'open':
215 elseif line =~ '^\s*open\>' 205 elseif line =~ '^\s*open\>'
216 if lline !~ s:lim . '\|' . s:letlim 206 if lline !~ s:lim . '\|' . s:letlim
217 return indent(search(s:val . '\|^\s*\(exception\|external\|include\|type\)\>', 'bW')) 207 return indent(search('^\s*\(\(exception\|external\|include\|type\)\>\|val\>.*:\)', 'bW'))
218 else return ind 208 else return ind
219 endif 209 endif
220 210
221 " Indent if current line begins with 'val': 211 " Indent if current line begins with 'val':
222 elseif line =~ '^\s*val\>' 212 elseif line =~ '^\s*val\>'
223 if lline !~ '^\s*\(exception\|external\|include\|open\)\>\|' . s:obj . '\|' . s:letlim 213 if lline !~ '^\s*\(exception\|external\|include\|open\)\>\|' . s:obj . '\|' . s:letlim
224 return indent(search(s:val . '\|^\s*\(exception\|include\|initializer\|method\|open\|type\)\>', 'bW')) 214 return indent(search('^\s*\(\(exception\|include\|initializer\|method\|open\|type\|val\)\>\|external\>.*:\)', 'bW'))
225 else return ind 215 else return ind
226 endif 216 endif
227 217
228 " Indent if current line begins with 'constraint': 218 " Indent if current line begins with 'constraint':
229 elseif line =~ '^\s*constraint\>' 219 elseif line =~ '^\s*constraint\>'
251 if lline !~ s:obj 241 if lline !~ s:obj
252 return indent(search('^\s*\(\(constraint\|inherit\|initializer\|val\)\>\|method\>.*\(:\|=\)\)', 'bW')) 242 return indent(search('^\s*\(\(constraint\|inherit\|initializer\|val\)\>\|method\>.*\(:\|=\)\)', 'bW'))
253 else return ind 243 else return ind
254 endif 244 endif
255 245
256 " Indent back to normal after comments:
257 elseif line =~ '^\s*\*)'
258 call search('\*)', 'bW')
259 return indent(searchpair('(\*', '', '\*)', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))
260
261 endif 246 endif
262 247
263 " Add a 'shiftwidth' after lines ending with: 248 " Add a 'shiftwidth' after lines ending with:
264 if lline =~ '\(:\|=\|->\|<-\|(\|\[\|{\|{<\|\[|\|\[<\|\<\(begin\|struct\|sig\|functor\|initializer\|object\|try\|do\|if\|then\|else\|fun\|function\|parser\)\|\<object\s*(.*)\)\s*$' 249 if lline =~ '\(:\|=\|->\|<-\|(\|\[\|{\|{<\|\[|\|\[<\|\<\(begin\|do\|else\|fun\|function\|functor\|if\|initializer\|object\|parser\|private\|sig\|struct\|then\|try\)\|\<object\s*(.*)\)\s*$'
265 let ind = ind + &sw 250 let ind = ind + &sw
266 251
267 " Back to normal indent after lines ending with ';;': 252 " Back to normal indent after lines ending with ';;':
268 elseif lline =~ ';;\s*$' && lline !~ '^\s*;;' 253 elseif lline =~ ';;\s*$' && lline !~ '^\s*;;'
269 let ind = s:GetInd(v:lnum, s:letpat, s:letlim) 254 let ind = s:GetInd(v:lnum, s:letpat, s:letlim)