Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim indent file |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 2 | " Language: OCaml |
| 3 | " Maintainers: Jean-Francois Yuen <jfyuen@happycoders.org> |
| 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: 2004 Apr 11 - Added indent for 'class' (JY) |
| 8 | " 2003 Sep 16 - Added 'private' as keyword (JY) |
| 9 | " 2003 Mar 29 - Fixed bug with 'if' and 'else' (JY) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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() |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 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) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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: |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 36 | let s:beflet = '^\s*\(initializer\|method\|try\)\|\(\<\(begin\|do\|else\|in\|then\|try\)\|->\|<-\|=\|;\|(\)\s*$' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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*$' |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 42 | let s:type = '^\s*\%(class\|let\|type\)\>.*=' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 43 | |
| 44 | " Skipping pattern, for comments |
| 45 | function s:SkipPattern(lnum, pat) |
| 46 | let def = prevnonblank(a:lnum - 1) |
| 47 | while def > 0 && getline(def) =~ a:pat |
| 48 | let def = prevnonblank(def - 1) |
| 49 | endwhile |
| 50 | return def |
| 51 | endfunction |
| 52 | |
| 53 | " Indent for ';;' to match multiple 'let' |
| 54 | function s:GetInd(lnum, pat, lim) |
| 55 | let llet = search(a:pat, 'bW') |
| 56 | let old = indent(a:lnum) |
| 57 | while llet > 0 |
| 58 | let old = indent(llet) |
| 59 | let nb = s:SkipPattern(llet, '^\s*(\*.*\*)\s*$') |
| 60 | if getline(nb) =~ a:lim |
| 61 | return old |
| 62 | endif |
| 63 | let llet = search(a:pat, 'bW') |
| 64 | endwhile |
| 65 | return old |
| 66 | endfunction |
| 67 | |
| 68 | " Indent pairs |
| 69 | function s:FindPair(pstart, pmid, pend) |
| 70 | call search(a:pend, 'bW') |
| 71 | return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"')) |
| 72 | endfunction |
| 73 | |
| 74 | " Indent 'let' |
| 75 | function s:FindLet(pstart, pmid, pend) |
| 76 | call search(a:pend, 'bW') |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 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')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 78 | endfunction |
| 79 | |
| 80 | function GetOCamlIndent() |
| 81 | " Find a non-blank line above the current line. |
| 82 | let lnum = prevnonblank(v:lnum - 1) |
| 83 | |
| 84 | " At the start of the file use zero indent. |
| 85 | if lnum == 0 |
| 86 | return 0 |
| 87 | endif |
| 88 | |
| 89 | let ind = indent(lnum) |
| 90 | let lline = getline(lnum) |
| 91 | |
| 92 | " Return double 'shiftwidth' after lines matching: |
| 93 | if lline =~ '^\s*|.*->\s*$' |
| 94 | return ind + &sw + &sw |
| 95 | endif |
| 96 | |
| 97 | let line = getline(v:lnum) |
| 98 | |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 99 | " Indent if current line begins with 'end': |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 100 | if line =~ '^\s*end\>' |
| 101 | return s:FindPair(s:module, '','\<end\>') |
| 102 | |
| 103 | " Indent if current line begins with 'done' for 'do': |
| 104 | elseif line =~ '^\s*done\>' |
| 105 | return s:FindPair('\<do\>', '','\<done\>') |
| 106 | |
| 107 | " Indent if current line begins with '}' or '>}': |
| 108 | elseif line =~ '^\s*\(\|>\)}' |
| 109 | return s:FindPair('{', '','}') |
| 110 | |
| 111 | " Indent if current line begins with ']', '|]' or '>]': |
| 112 | elseif line =~ '^\s*\(\||\|>\)\]' |
| 113 | return s:FindPair('\[', '','\]') |
| 114 | |
| 115 | " Indent if current line begins with ')': |
| 116 | elseif line =~ '^\s*)' |
| 117 | return s:FindPair('(', '',')') |
| 118 | |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 119 | " Indent if current line begins with 'let': |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 120 | elseif line =~ '^\s*let\>' |
| 121 | if lline !~ s:lim . '\|' . s:letlim . '\|' . s:beflet |
| 122 | return s:FindLet(s:type, '','\<let\s*$') |
| 123 | else return ind |
| 124 | endif |
| 125 | |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 126 | " Indent if current line begins with 'class' or 'type': |
| 127 | elseif line =~ '^\s*\(class\|type\)\>' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 128 | if lline !~ s:lim . '\|\<and\s*$\|' . s:letlim |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 129 | return s:FindLet(s:type, '','\<\(class\|type\)\s*$') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 130 | else return ind |
| 131 | endif |
| 132 | |
| 133 | " Indent for pattern matching: |
| 134 | elseif line =~ '^\s*|' |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 135 | if lline !~ '^\s*\(|[^\]]\|\(match\|type\|with\)\>\)\|\<\(function\|parser\|private\|with\)\s*$' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 136 | call search('|', 'bW') |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 137 | return indent(searchpair('^\s*\(match\|type\)\>\|\<\(function\|parser\|private\|with\)\s*$', '', '^\s*|', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment" || getline(".") !~ "^\\s*|.*->"')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 138 | else return ind |
| 139 | endif |
| 140 | |
| 141 | " Indent if current line begins with ';;': |
| 142 | elseif line =~ '^\s*;;' |
| 143 | if lline !~ ';;\s*$' |
| 144 | return s:GetInd(v:lnum, s:letpat, s:letlim) |
| 145 | else return ind |
| 146 | endif |
| 147 | |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 148 | " Indent if current line begins with 'in': |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 149 | elseif line =~ '^\s*in\>' |
| 150 | if lline !~ '^\s*\(let\|and\)\>' |
| 151 | return s:FindPair('\<let\>', '', '\<in\>') |
| 152 | else return ind |
| 153 | endif |
| 154 | |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 155 | " Indent if current line begins with 'else': |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 156 | elseif line =~ '^\s*else\>' |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 157 | if lline !~ '^\s*\(if\|then\)\>' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 158 | return s:FindPair('\<if\>', '', '\<else\>') |
| 159 | else return ind |
| 160 | endif |
| 161 | |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 162 | " Indent if current line begins with 'then': |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 163 | elseif line =~ '^\s*then\>' |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 164 | if lline !~ '^\s*\(if\|else\)\>' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 165 | return s:FindPair('\<if\>', '', '\<then\>') |
| 166 | else return ind |
| 167 | endif |
| 168 | |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 169 | " Indent if current line begins with 'and': |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 170 | elseif line =~ '^\s*and\>' |
| 171 | if lline !~ '^\s*\(and\|let\|type\)\>\|\<end\s*$' |
| 172 | return ind - &sw |
| 173 | else return ind |
| 174 | endif |
| 175 | |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 176 | " Indent if current line begins with 'with': |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 177 | elseif line =~ '^\s*with\>' |
| 178 | if lline !~ '^\s*\(match\|try\)\>' |
| 179 | return s:FindPair('\<\%(match\|try\)\>', '','\<with\>') |
| 180 | else return ind |
| 181 | endif |
| 182 | |
| 183 | " Indent if current line begins with 'exception': |
| 184 | elseif line =~ '^\s*exception\>' |
| 185 | if lline !~ s:lim . '\|' . s:letlim |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 186 | return indent(search('^\s*\(\(external\|include\|open\|type\)\>\|val\>.*:\)', 'bW')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 187 | else return ind |
| 188 | endif |
| 189 | |
| 190 | " Indent if current line begins with 'external': |
| 191 | elseif line =~ '^\s*external\>' |
| 192 | if lline !~ s:lim . '\|' . s:letlim |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 193 | return indent(search('^\s*\(\(exception\|external\|include\|open\|type\)\>\|val\>.*:\)', 'bW')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 194 | else return ind |
| 195 | endif |
| 196 | |
| 197 | " Indent if current line begins with 'include': |
| 198 | elseif line =~ '^\s*include\>' |
| 199 | if lline !~ s:lim . '\|' . s:letlim |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 200 | return indent(search('^\s*\(\(exception\|external\|open\|type\)\>\|val\>.*:\)', 'bW')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 201 | else return ind |
| 202 | endif |
| 203 | |
| 204 | " Indent if current line begins with 'open': |
| 205 | elseif line =~ '^\s*open\>' |
| 206 | if lline !~ s:lim . '\|' . s:letlim |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 207 | return indent(search('^\s*\(\(exception\|external\|include\|type\)\>\|val\>.*:\)', 'bW')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 208 | else return ind |
| 209 | endif |
| 210 | |
| 211 | " Indent if current line begins with 'val': |
| 212 | elseif line =~ '^\s*val\>' |
| 213 | if lline !~ '^\s*\(exception\|external\|include\|open\)\>\|' . s:obj . '\|' . s:letlim |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 214 | return indent(search('^\s*\(\(exception\|include\|initializer\|method\|open\|type\|val\)\>\|external\>.*:\)', 'bW')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 215 | else return ind |
| 216 | endif |
| 217 | |
| 218 | " Indent if current line begins with 'constraint': |
| 219 | elseif line =~ '^\s*constraint\>' |
| 220 | if lline !~ s:obj |
| 221 | return indent(search('^\s*\(inherit\|initializer\|method\|val\)\>', 'bW')) |
| 222 | else return ind |
| 223 | endif |
| 224 | |
| 225 | " Indent if current line begins with 'inherit': |
| 226 | elseif line =~ '^\s*inherit\>' |
| 227 | if lline !~ s:obj |
| 228 | return indent(search('^\s*\(constraint\|initializer\|method\|val\)\>', 'bW')) |
| 229 | else return ind |
| 230 | endif |
| 231 | |
| 232 | " Indent if current line begins with 'inherit': |
| 233 | elseif line =~ '^\s*initializer\>' |
| 234 | if lline !~ s:obj |
| 235 | return indent(search('^\s*\(constraint\|inherit\|method\|val\)\>', 'bW')) |
| 236 | else return ind |
| 237 | endif |
| 238 | |
| 239 | " Indent if current line begins with 'method': |
| 240 | elseif line =~ '^\s*method\>' |
| 241 | if lline !~ s:obj |
| 242 | return indent(search('^\s*\(\(constraint\|inherit\|initializer\|val\)\>\|method\>.*\(:\|=\)\)', 'bW')) |
| 243 | else return ind |
| 244 | endif |
| 245 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 246 | endif |
| 247 | |
| 248 | " Add a 'shiftwidth' after lines ending with: |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 249 | if lline =~ '\(:\|=\|->\|<-\|(\|\[\|{\|{<\|\[|\|\[<\|\<\(begin\|do\|else\|fun\|function\|functor\|if\|initializer\|object\|parser\|private\|sig\|struct\|then\|try\)\|\<object\s*(.*)\)\s*$' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | |
| 294 | endfunction |
| 295 | |
| 296 | " vim:sw=2 |