robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
16c8152229
master
1" Vim indent file 2" Language: SML 3" Maintainer: Saikat Guha <sg266@cornell.edu> 4" Hubert Chao <hc85@cornell.edu> 5" Original OCaml Version: 6" Jean-Francois Yuen <jfyuen@ifrance.com> 7" Mike Leary <leary@nwlink.com> 8" Markus Mottl <markus@oefai.at> 9" OCaml URL: http://www.oefai.at/~markus/vim/indent/ocaml.vim 10" Last Change: 2022 Apr 06 11" 2002 Nov 06 - Some fixes (JY) 12" 2002 Oct 28 - Fixed bug with indentation of ']' (MM) 13" 2002 Oct 22 - Major rewrite (JY) 14" 2022 Apr 08 - b:undo_indent added by Doug Kearns 15" 2025 Nov 04 - Move comments and formatoptions to ftplugin 16 17" Only load this indent file when no other was loaded. 18if exists ( "b:did_indent" ) 19finish 20endif 21let b: did_indent = 1 22 23setlocal expandtab 24setlocal indentexpr = GetSMLIndent () 25setlocal indentkeys += 0=and,0=else,0=end,0=handle,0=if,0=in,0=let,0=then,0=val,0=fun,0=\|,0=*),0) 26setlocal nolisp 27setlocal nosmartindent 28setlocal textwidth = 80 29setlocal shiftwidth = 2 30 31let b: undo_indent = "setl et< inde< indk< lisp< si< sw< tw<" 32 33" Only define the function once. 34"if exists("*GetSMLIndent") 35"finish 36"endif 37 38" Define some patterns: 39let s: beflet = '^\s*\(initializer\|method\|try\)\|\(\<\(begin\|do\|else\|in\|then\|try\)\|->\|;\)\s*$' 40let s: letpat = '^\s*\(let\|type\|module\|class\|open\|exception\|val\|include\|external\)\>' 41let s: letlim = '\(\<\(sig\|struct\)\|;;\)\s*$' 42let s: lim = '^\s*\(exception\|external\|include\|let\|module\|open\|type\|val\)\>' 43let s: module = '\<\%(let\|sig\|struct\)\>' 44let s: obj = '^\s*\(constraint\|inherit\|initializer\|method\|val\)\>\|\<\(object\|object\s*(.*)\)\s*$' 45let s: type = '^\s*\%(let\|type\)\>.*=' 46let s: val = '^\s*\(val\|external\)\>.*:' 47 48" Skipping pattern, for comments 49function ! s: SkipPattern ( lnum , pat ) 50let def = prevnonblank ( a: lnum - 1 ) 51while def > 0 && getline ( def ) =~ a: pat 52let def = prevnonblank ( def - 1 ) 53endwhile 54return def 55endfunction 56 57" Indent for ';;' to match multiple 'let' 58function ! s: GetInd ( lnum , pat , lim ) 59let llet = search ( a: pat , 'bW' ) 60let old = indent ( a: lnum ) 61while llet > 0 62let old = indent ( llet ) 63let nb = s: SkipPattern ( llet , '^\s*(\*.*\*)\s*$' ) 64if getline ( nb ) =~ a: lim 65return old 66endif 67let llet = search ( a: pat , 'bW' ) 68endwhile 69return old 70endfunction 71 72" Indent pairs 73function ! s: FindPair ( pstart , pmid , pend ) 74call search ( a: pend , 'bW' ) 75" return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"')) 76let lno = searchpair ( a: pstart , a: pmid , a: pend , 'bW' , 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"' ) 77if lno == -1 78return indent ( lno ) 79else 80return col ( "." ) - 1 81endif 82endfunction 83 84function ! s: FindLet ( pstart , pmid , pend ) 85call search ( a: pend , 'bW' ) 86" return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"')) 87let lno = searchpair ( a: pstart , a: pmid , a: pend , 'bW' , 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"' ) 88let moduleLine = getline ( lno ) 89if lno == -1 || moduleLine =~ '^\s*\(fun\|structure\|signature\)\>' 90return indent ( lno ) 91else 92return col ( "." ) - 1 93endif 94endfunction 95 96" Indent 'let' 97"function! s:FindLet(pstart, pmid, pend) 98" call search(a:pend, 'bW') 99" 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')) 100"endfunction 101 102function ! GetSMLIndent () 103" Find a non-blank line above the current line. 104let lnum = prevnonblank ( v: lnum - 1 ) 105 106" At the start of the file use zero indent. 107if lnum == 0 108return 0 109endif 110 111let ind = indent ( lnum ) 112let lline = getline ( lnum ) 113 114" Return double 'shiftwidth' after lines matching: 115if lline =~ '^\s*|.*=>\s*$' 116return ind + 2 * shiftwidth () 117elseif lline =~ '^\s*val\>.*=\s*$' 118return ind + shiftwidth () 119endif 120 121let line = getline ( v: lnum ) 122 123" Indent lines starting with 'end' to matching module 124if line =~ '^\s*end\>' 125return s: FindLet ( s: module , '' , '\<end\>' ) 126 127" Match 'else' with 'if' 128elseif line =~ '^\s*else\>' 129if lline !~ '^\s*\(if\|else\|then\)\>' 130return s: FindPair ( '\<if\>' , '' , '\<then\>' ) 131else 132return ind 133endif 134 135" Match 'then' with 'if' 136elseif line =~ '^\s*then\>' 137if lline !~ '^\s*\(if\|else\|then\)\>' 138return s: FindPair ( '\<if\>' , '' , '\<then\>' ) 139else 140return ind 141endif 142 143" Indent if current line begins with ']' 144elseif line =~ '^\s*\]' 145return s: FindPair ( '\[' , '' , '\]' ) 146 147" Indent current line starting with 'in' to last matching 'let' 148elseif line =~ '^\s*in\>' 149let ind = s: FindLet ( '\<let\>' , '' , '\<in\>' ) 150 151" Indent from last matching module if line matches: 152elseif line =~ '^\s*\(fun\|val\|open\|structure\|and\|datatype\|type\|exception\)\>' 153cursor (lnum,1) 154let lastModule = indent ( searchpair ( s: module , '' , '\<end\>' , 'bWn' , 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"' )) 155if lastModule == -1 156return 0 157else 158return lastModule + shiftwidth () 159endif 160 161" Indent lines starting with '|' from matching 'case', 'handle' 162elseif line =~ '^\s*|' 163" cursor(lnum,1) 164let lastSwitch = search ( '\<\(case\|handle\|fun\|datatype\)\>' , 'bW' ) 165let switchLine = getline ( lastSwitch ) 166let switchLineIndent = indent ( lastSwitch ) 167if lline =~ '^\s*|' 168return ind 169endif 170if switchLine =~ '\<case\>' 171return col ( "." ) + 2 172elseif switchLine =~ '\<handle\>' 173return switchLineIndent + shiftwidth () 174elseif switchLine =~ '\<datatype\>' 175call search ( '=' ) 176return col ( "." ) - 1 177else 178return switchLineIndent + 2 179endif 180 181 182" Indent if last line ends with 'sig', 'struct', 'let', 'then', 'else', 183" 'in' 184elseif lline =~ '\<\(sig\|struct\|let\|in\|then\|else\)\s*$' 185let ind = ind + shiftwidth () 186 187" Indent if last line ends with 'of', align from 'case' 188elseif lline =~ '\<\(of\)\s*$' 189call search ( '\<case\>' , "bW" ) 190let ind = col ( "." ) + 4 191 192" Indent if current line starts with 'of' 193elseif line =~ '^\s*of\>' 194call search ( '\<case\>' , "bW" ) 195let ind = col ( "." ) + 1 196 197 198" Indent if last line starts with 'fun', 'case', 'fn' 199elseif lline =~ '^\s*\(fun\|fn\|case\)\>' 200let ind = ind + shiftwidth () 201 202endif 203 204" Don't indent 'let' if last line started with 'fun', 'fn' 205if line =~ '^\s*let\>' 206if lline =~ '^\s*\(fun\|fn\)' 207let ind = ind - shiftwidth () 208endif 209endif 210 211return ind 212 213endfunction 214 215" vim:sw=2