robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
79130c0fd3
master
1" Vim indent file 2" Language: OCaml 3" Maintainers: Jean-Francois Yuen <jfyuen@happycoders.org> 4" Mike Leary <leary@nwlink.com> 5" Markus Mottl <markus.mottl@gmail.com> 6" URL: https://github.com/ocaml/vim-ocaml 7" Last Change: 2017 Jun 13 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) 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 .. 14 15" Only load this indent file when no other was loaded. 16if exists ( "b:did_indent" ) 17finish 18endif 19let b: did_indent = 1 20 21setlocal expandtab 22setlocal indentexpr = GetOCamlIndent () 23setlocal 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) 24setlocal nolisp 25setlocal nosmartindent 26 27let b: undo_indent = "setl et< inde< indk< lisp< si<" 28 29" At least Marc Weber and Markus Mottl do not like this: 30" setlocal textwidth=80 31 32" Comment formatting 33if ! exists ( "no_ocaml_comments" ) 34if ( has ( "comments" )) 35setlocal comments = sr:(*\ ,mb:\ ,ex:*) 36setlocal comments ^=sr:(**,mb:\ \ ,ex:*) 37setlocal fo = cqort 38let b: undo_indent .= " | setl com< fo<" 39endif 40endif 41 42" Only define the function once. 43if exists ( "*GetOCamlIndent" ) 44finish 45endif 46 47" Define some patterns: 48let s: beflet = '^\s*\(initializer\|method\|try\)\|\(\<\(begin\|do\|else\|in\|then\|try\)\|->\|<-\|=\|;\|(\)\s*$' 49let s: letpat = '^\s*\(let\|type\|module\|class\|open\|exception\|val\|include\|external\)\>' 50let s: letlim = '\(\<\(sig\|struct\)\|;;\)\s*$' 51let s: lim = '^\s*\(exception\|external\|include\|let\|module\|open\|type\|val\)\>' 52let s: module = '\<\%(begin\|sig\|struct\|object\)\>' 53let s: obj = '^\s*\(constraint\|inherit\|initializer\|method\|val\)\>\|\<\(object\|object\s*(.*)\)\s*$' 54let s: type = '^\s*\%(class\|let\|type\)\>.*=' 55 56" Skipping pattern, for comments 57function ! s: GetLineWithoutFullComment ( lnum ) 58let lnum = prevnonblank ( a: lnum - 1 ) 59let lline = substitute ( getline ( lnum ), '(\*.*\*)\s*$' , '' , '' ) 60while lline =~ '^\s*$' && lnum > 0 61let lnum = prevnonblank ( lnum - 1 ) 62let lline = substitute ( getline ( lnum ), '(\*.*\*)\s*$' , '' , '' ) 63endwhile 64return lnum 65endfunction 66 67" Indent for ';;' to match multiple 'let' 68function ! s: GetInd ( lnum , pat , lim ) 69let llet = search ( a: pat , 'bW' ) 70let old = indent ( a: lnum ) 71while llet > 0 72let old = indent ( llet ) 73let nb = s: GetLineWithoutFullComment ( llet ) 74if getline ( nb ) =~ a: lim 75return old 76endif 77let llet = search ( a: pat , 'bW' ) 78endwhile 79return old 80endfunction 81 82" Indent pairs 83function ! s: FindPair ( pstart , pmid , pend ) 84call search ( a: pend , 'bW' ) 85return indent ( searchpair ( a: pstart , a: pmid , a: pend , 'bWn' , 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"' )) 86endfunction 87 88" Indent 'let' 89function ! s: FindLet ( pstart , pmid , pend ) 90call search ( a: pend , 'bW' ) 91return 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' )) 92endfunction 93 94function ! GetOCamlIndent () 95" Find a non-commented line above the current line. 96let lnum = s: GetLineWithoutFullComment ( v: lnum ) 97 98" At the start of the file use zero indent. 99if lnum == 0 100return 0 101endif 102 103let ind = indent ( lnum ) 104let lline = substitute ( getline ( lnum ), '(\*.*\*)\s*$' , '' , '' ) 105 106" Return double 'shiftwidth' after lines matching: 107if lline =~ '^\s*|.*->\s*$' 108return ind + 2 * shiftwidth () 109endif 110 111let line = getline ( v: lnum ) 112 113" Indent if current line begins with 'end': 114if line =~ '^\s*end\>' 115return s: FindPair ( s: module , '' , '\<end\>' ) 116 117" Indent if current line begins with 'done' for 'do': 118elseif line =~ '^\s*done\>' 119return s: FindPair ( '\<do\>' , '' , '\<done\>' ) 120 121" Indent if current line begins with '}' or '>}': 122elseif line =~ '^\s*\(\|>\)}' 123return s: FindPair ( '{' , '' , '}' ) 124 125" Indent if current line begins with ']', '|]' or '>]': 126elseif line =~ '^\s*\(\||\|>\)\]' 127return s: FindPair ( '\[' , '' , '\]' ) 128 129" Indent if current line begins with ')': 130elseif line =~ '^\s*)' 131return s: FindPair ( '(' , '' , ')' ) 132 133" Indent if current line begins with 'let': 134elseif line =~ '^\s*let\>' 135if lline !~ s: lim . '\|' . s: letlim . '\|' . s: beflet 136return s: FindLet ( s: type , '' , '\<let\s*$' ) 137endif 138 139" Indent if current line begins with 'class' or 'type': 140elseif line =~ '^\s*\(class\|type\)\>' 141if lline !~ s: lim . '\|\<and\s*$\|' . s: letlim 142return s: FindLet ( s: type , '' , '\<\(class\|type\)\s*$' ) 143endif 144 145" Indent for pattern matching: 146elseif line =~ '^\s*|' 147if lline !~ '^\s*\(|[^\]]\|\(match\|type\|with\)\>\)\|\<\(function\|parser\|private\|with\)\s*$' 148call search ( '|' , 'bW' ) 149return indent ( searchpair ( '^\s*\(match\|type\)\>\|\<\(function\|parser\|private\|with\)\s*$' , '' , '^\s*|' , 'bWn' , 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment" || getline(".") !~ "^\\s*|.*->"' )) 150endif 151 152" Indent if current line begins with ';;': 153elseif line =~ '^\s*;;' 154if lline !~ ';;\s*$' 155return s: GetInd ( v: lnum , s: letpat , s: letlim ) 156endif 157 158" Indent if current line begins with 'in': 159elseif line =~ '^\s*in\>' 160if lline !~ '^\s*\(let\|and\)\>' 161return s: FindPair ( '\<let\>' , '' , '\<in\>' ) 162endif 163 164" Indent if current line begins with 'else': 165elseif line =~ '^\s*else\>' 166if lline !~ '^\s*\(if\|then\)\>' 167return s: FindPair ( '\<if\>' , '' , '\<else\>' ) 168endif 169 170" Indent if current line begins with 'then': 171elseif line =~ '^\s*then\>' 172if lline !~ '^\s*\(if\|else\)\>' 173return s: FindPair ( '\<if\>' , '' , '\<then\>' ) 174endif 175 176" Indent if current line begins with 'and': 177elseif line =~ '^\s*and\>' 178if lline !~ '^\s*\(and\|let\|type\)\>\|\<end\s*$' 179return ind - shiftwidth () 180endif 181 182" Indent if current line begins with 'with': 183elseif line =~ '^\s*with\>' 184if lline !~ '^\s*\(match\|try\)\>' 185return s: FindPair ( '\<\%(match\|try\)\>' , '' , '\<with\>' ) 186endif 187 188" Indent if current line begins with 'exception', 'external', 'include' or 189" 'open': 190elseif line =~ '^\s*\(exception\|external\|include\|open\)\>' 191if lline !~ s: lim . '\|' . s: letlim 192call search ( line ) 193return indent ( search ( '^\s*\(\(exception\|external\|include\|open\|type\)\>\|val\>.*:\)' , 'bW' )) 194endif 195 196" Indent if current line begins with 'val': 197elseif line =~ '^\s*val\>' 198if lline !~ '^\s*\(exception\|external\|include\|open\)\>\|' . s: obj . '\|' . s: letlim 199return indent ( search ( '^\s*\(\(exception\|include\|initializer\|method\|open\|type\|val\)\>\|external\>.*:\)' , 'bW' )) 200endif 201 202" Indent if current line begins with 'constraint', 'inherit', 'initializer' 203" or 'method': 204elseif line =~ '^\s*\(constraint\|inherit\|initializer\|method\)\>' 205if lline !~ s: obj 206return indent ( search ( '\<\(object\|object\s*(.*)\)\s*$' , 'bW' )) + shiftwidth () 207endif 208 209endif 210 211" Add a 'shiftwidth' after lines ending with: 212if lline =~ '\(:\|=\|->\|<-\|(\|\[\|{\|{<\|\[|\|\[<\|\<\(begin\|do\|else\|fun\|function\|functor\|if\|initializer\|object\|parser\|private\|sig\|struct\|then\|try\)\|\<object\s*(.*)\)\s*$' 213let ind = ind + shiftwidth () 214 215" Back to normal indent after lines ending with ';;': 216elseif lline =~ ';;\s*$' && lline !~ '^\s*;;' 217let ind = s: GetInd ( v: lnum , s: letpat , s: letlim ) 218 219" Back to normal indent after lines ending with 'end': 220elseif lline =~ '\<end\s*$' 221let ind = s: FindPair ( s: module , '' , '\<end\>' ) 222 223" Back to normal indent after lines ending with 'in': 224elseif lline =~ '\<in\s*$' && lline !~ '^\s*in\>' 225let ind = s: FindPair ( '\<let\>' , '' , '\<in\>' ) 226 227" Back to normal indent after lines ending with 'done': 228elseif lline =~ '\<done\s*$' 229let ind = s: FindPair ( '\<do\>' , '' , '\<done\>' ) 230 231" Back to normal indent after lines ending with '}' or '>}': 232elseif lline =~ '\(\|>\)}\s*$' 233let ind = s: FindPair ( '{' , '' , '}' ) 234 235" Back to normal indent after lines ending with ']', '|]' or '>]': 236elseif lline =~ '\(\||\|>\)\]\s*$' 237let ind = s: FindPair ( '\[' , '' , '\]' ) 238 239" Back to normal indent after comments: 240elseif lline =~ '\*)\s*$' 241call search ( '\*)' , 'bW' ) 242let ind = indent ( searchpair ( '(\*' , '' , '\*)' , 'bWn' , 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"' )) 243 244" Back to normal indent after lines ending with ')': 245elseif lline =~ ')\s*$' 246let ind = s: FindPair ( '(' , '' , ')' ) 247 248" If this is a multiline comment then align '*': 249elseif lline =~ '^\s*(\*' && line =~ '^\s*\*' 250let ind = ind + 1 251 252else 253" Don't change indentation of this line 254" for new lines (indent==0) use indentation of previous line 255 256" This is for preventing removing indentation of these args: 257" let f x = 258" let y = x + 1 in 259" Printf.printf 260" "o" << here 261" "oeuth" << don't touch indentation 262 263let i = indent ( v: lnum ) 264return i == 0 ? ind : i 265 266endif 267 268" Subtract a 'shiftwidth' after lines matching 'match ... with parser': 269if lline =~ '\<match\>.*\<with\>\s*\<parser\s*$' 270let ind = ind - shiftwidth () 271endif 272 273return ind 274 275endfunction 276 277" vim:sw=2