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