codekingpro/portable-devtools
114k
1" Vim indent file2" Language: OCaml3" 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-ocaml7" Last Change: 2017 Jun 138" 2005 Jun 25 - Fixed multiple bugs due to 'else\nreturn ind' working9" 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) stuff13" which eventually should be moved somewhere else ..14 15" Only load this indent file when no other was loaded.16if exists("b:did_indent")17 finish18endif19let b:did_indent = 120 21setlocal expandtab22setlocal 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 nolisp25setlocal nosmartindent26 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=8031 32" Comment formatting33if !exists("no_ocaml_comments")34 if (has("comments"))35 setlocal comments=sr:(*\ ,mb:\ ,ex:*)36 setlocal comments^=sr:(**,mb:\ \ ,ex:*)37 setlocal fo=cqort38 let b:undo_indent .= " | setl com< fo<"39 endif40endif41 42" Only define the function once.43if exists("*GetOCamlIndent")44 finish45endif46 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 comments57function! s:GetLineWithoutFullComment(lnum)58 let lnum = prevnonblank(a:lnum - 1)59 let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')60 while lline =~ '^\s*$' && lnum > 061 let lnum = prevnonblank(lnum - 1)62 let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')63 endwhile64 return lnum65endfunction66 67" Indent for ';;' to match multiple 'let'68function! s:GetInd(lnum, pat, lim)69 let llet = search(a:pat, 'bW')70 let old = indent(a:lnum)71 while llet > 072 let old = indent(llet)73 let nb = s:GetLineWithoutFullComment(llet)74 if getline(nb) =~ a:lim75 return old76 endif77 let llet = search(a:pat, 'bW')78 endwhile79 return old80endfunction81 82" Indent pairs83function! s:FindPair(pstart, pmid, pend)84 call search(a:pend, 'bW')85 return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"'))86endfunction87 88" Indent 'let'89function! 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" || getline(".") =~ "^\\s*let\\>.*=.*\\<in\\s*$" || getline(prevnonblank(".") - 1) =~ s:beflet'))92endfunction93 94function! GetOCamlIndent()95 " Find a non-commented line above the current line.96 let lnum = s:GetLineWithoutFullComment(v:lnum)97 98 " At the start of the file use zero indent.99 if lnum == 0100 return 0101 endif102 103 let ind = indent(lnum)104 let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')105 106 " Return double 'shiftwidth' after lines matching:107 if lline =~ '^\s*|.*->\s*$'108 return ind + 2 * shiftwidth()109 endif110 111 let line = getline(v:lnum)112 113 " Indent if current line begins with 'end':114 if line =~ '^\s*end\>'115 return s:FindPair(s:module, '','\<end\>')116 117 " Indent if current line begins with 'done' for 'do':118 elseif line =~ '^\s*done\>'119 return s:FindPair('\<do\>', '','\<done\>')120 121 " Indent if current line begins with '}' or '>}':122 elseif line =~ '^\s*\(\|>\)}'123 return s:FindPair('{', '','}')124 125 " Indent if current line begins with ']', '|]' or '>]':126 elseif line =~ '^\s*\(\||\|>\)\]'127 return s:FindPair('\[', '','\]')128 129 " Indent if current line begins with ')':130 elseif line =~ '^\s*)'131 return s:FindPair('(', '',')')132 133 " Indent if current line begins with 'let':134 elseif line =~ '^\s*let\>'135 if lline !~ s:lim . '\|' . s:letlim . '\|' . s:beflet136 return s:FindLet(s:type, '','\<let\s*$')137 endif138 139 " Indent if current line begins with 'class' or 'type':140 elseif line =~ '^\s*\(class\|type\)\>'141 if lline !~ s:lim . '\|\<and\s*$\|' . s:letlim142 return s:FindLet(s:type, '','\<\(class\|type\)\s*$')143 endif144 145 " Indent for pattern matching:146 elseif line =~ '^\s*|'147 if lline !~ '^\s*\(|[^\]]\|\(match\|type\|with\)\>\)\|\<\(function\|parser\|private\|with\)\s*$'148 call search('|', 'bW')149 return indent(searchpair('^\s*\(match\|type\)\>\|\<\(function\|parser\|private\|with\)\s*$', '', '^\s*|', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment" || getline(".") !~ "^\\s*|.*->"'))150 endif151 152 " Indent if current line begins with ';;':153 elseif line =~ '^\s*;;'154 if lline !~ ';;\s*$'155 return s:GetInd(v:lnum, s:letpat, s:letlim)156 endif157 158 " Indent if current line begins with 'in':159 elseif line =~ '^\s*in\>'160 if lline !~ '^\s*\(let\|and\)\>'161 return s:FindPair('\<let\>', '', '\<in\>')162 endif163 164 " Indent if current line begins with 'else':165 elseif line =~ '^\s*else\>'166 if lline !~ '^\s*\(if\|then\)\>'167 return s:FindPair('\<if\>', '', '\<else\>')168 endif169 170 " Indent if current line begins with 'then':171 elseif line =~ '^\s*then\>'172 if lline !~ '^\s*\(if\|else\)\>'173 return s:FindPair('\<if\>', '', '\<then\>')174 endif175 176 " Indent if current line begins with 'and':177 elseif line =~ '^\s*and\>'178 if lline !~ '^\s*\(and\|let\|type\)\>\|\<end\s*$'179 return ind - shiftwidth()180 endif181 182 " Indent if current line begins with 'with':183 elseif line =~ '^\s*with\>'184 if lline !~ '^\s*\(match\|try\)\>'185 return s:FindPair('\<\%(match\|try\)\>', '','\<with\>')186 endif187 188 " Indent if current line begins with 'exception', 'external', 'include' or189 " 'open':190 elseif line =~ '^\s*\(exception\|external\|include\|open\)\>'191 if lline !~ s:lim . '\|' . s:letlim192 call search(line)193 return indent(search('^\s*\(\(exception\|external\|include\|open\|type\)\>\|val\>.*:\)', 'bW'))194 endif195 196 " Indent if current line begins with 'val':197 elseif line =~ '^\s*val\>'198 if lline !~ '^\s*\(exception\|external\|include\|open\)\>\|' . s:obj . '\|' . s:letlim199 return indent(search('^\s*\(\(exception\|include\|initializer\|method\|open\|type\|val\)\>\|external\>.*:\)', 'bW'))200 endif201 202 " Indent if current line begins with 'constraint', 'inherit', 'initializer'203 " or 'method':204 elseif line =~ '^\s*\(constraint\|inherit\|initializer\|method\)\>'205 if lline !~ s:obj206 return indent(search('\<\(object\|object\s*(.*)\)\s*$', 'bW')) + shiftwidth()207 endif208 209 endif210 211 " Add a 'shiftwidth' after lines ending with:212 if lline =~ '\(:\|=\|->\|<-\|(\|\[\|{\|{<\|\[|\|\[<\|\<\(begin\|do\|else\|fun\|function\|functor\|if\|initializer\|object\|parser\|private\|sig\|struct\|then\|try\)\|\<object\s*(.*)\)\s*$'213 let ind = ind + shiftwidth()214 215 " Back to normal indent after lines ending with ';;':216 elseif lline =~ ';;\s*$' && lline !~ '^\s*;;'217 let ind = s:GetInd(v:lnum, s:letpat, s:letlim)218 219 " Back to normal indent after lines ending with 'end':220 elseif lline =~ '\<end\s*$'221 let ind = s:FindPair(s:module, '','\<end\>')222 223 " Back to normal indent after lines ending with 'in':224 elseif lline =~ '\<in\s*$' && lline !~ '^\s*in\>'225 let ind = s:FindPair('\<let\>', '', '\<in\>')226 227 " Back to normal indent after lines ending with 'done':228 elseif lline =~ '\<done\s*$'229 let ind = s:FindPair('\<do\>', '','\<done\>')230 231 " Back to normal indent after lines ending with '}' or '>}':232 elseif lline =~ '\(\|>\)}\s*$'233 let ind = s:FindPair('{', '','}')234 235 " Back to normal indent after lines ending with ']', '|]' or '>]':236 elseif lline =~ '\(\||\|>\)\]\s*$'237 let ind = s:FindPair('\[', '','\]')238 239 " Back to normal indent after comments:240 elseif lline =~ '\*)\s*$'241 call search('\*)', 'bW')242 let ind = indent(searchpair('(\*', '', '\*)', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))243 244 " Back to normal indent after lines ending with ')':245 elseif lline =~ ')\s*$'246 let ind = s:FindPair('(', '',')')247 248 " If this is a multiline comment then align '*':249 elseif lline =~ '^\s*(\*' && line =~ '^\s*\*'250 let ind = ind + 1251 252 else253 " Don't change indentation of this line254 " for new lines (indent==0) use indentation of previous line255 256 " This is for preventing removing indentation of these args:257 " let f x =258 " let y = x + 1 in259 " Printf.printf260 " "o" << here261 " "oeuth" << don't touch indentation262 263 let i = indent(v:lnum)264 return i == 0 ? ind : i265 266 endif267 268 " Subtract a 'shiftwidth' after lines matching 'match ... with parser':269 if lline =~ '\<match\>.*\<with\>\s*\<parser\s*$'270 let ind = ind - shiftwidth()271 endif272 273 return ind274 275endfunction276 277" vim:sw=2278 