context.vim 1.7 KB

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