context.vim (1706B)
1 vim9script 2 3 # Language: ConTeXt typesetting engine 4 # Maintainer: Nicola Vitacolonna <nvitacolonna@gmail.com> 5 # Former Maintainers: Nikolai Weibull <now@bitwi.se> 6 # Latest Revision: 2026 Jan 10 7 8 if exists("b:did_indent") 9 finish 10 endif 11 12 # Load MetaPost indentation script (this will also set b:did_indent) 13 runtime! indent/mp.vim 14 15 setlocal indentexpr=ConTeXtIndent() 16 17 b:undo_indent = "setl indentexpr<" 18 19 def PrevNotComment(l: number): number 20 var prevlnum = prevnonblank(l) 21 22 while prevlnum > 0 && getline(prevlnum) =~# '^\s*%' 23 prevlnum = prevnonblank(prevlnum - 1) 24 endwhile 25 26 return prevlnum 27 enddef 28 29 def FindPair(pstart: string, pmid: string, pend: string): number 30 cursor(v:lnum, 1) 31 return indent(searchpair(pstart, pmid, pend, 'bWn', 32 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"')) 33 enddef 34 35 def ConTeXtIndent(): number 36 # Use MetaPost rules inside MetaPost graphic environments 37 if len(synstack(v:lnum, 1)) > 0 && 38 synIDattr(synstack(v:lnum, 1)[0], "name") ==# 'contextMPGraphic' 39 return g:MetaPostIndent() 40 endif 41 42 var prevlnum = PrevNotComment(v:lnum - 1) 43 var prevind = indent(prevlnum) 44 var prevline = getline(prevlnum) 45 var currline = getline(v:lnum) 46 47 # If the current line starts with ], match indentation. 48 if currline =~# '^\s*\]' 49 return FindPair('\[', '', '\]') 50 endif 51 52 # If the current line starts with }, match indentation. 53 if currline =~# '^\s*}' 54 return FindPair('{', '', '}') 55 endif 56 57 # If the previous line ends with [ or { (possibly followed by a comment) then indent. 58 if prevline =~# '[{[]\s*\%(%.*\)\=$' 59 return prevind + shiftwidth() 60 endif 61 62 return -1 63 enddef 64 65 # vim: sw=2 fdm=marker