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