context.vim 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 Sep 19
  6. # Typesetting {{{
  7. import autoload './typeset.vim'
  8. export def ConTeXtCmd(path: string): list<string>
  9. var cmd = ['mtxrun', '--script', 'context', '--nonstopmode', '--autogenerate']
  10. if !empty(get(g:, 'context_extra_options', ''))
  11. cmd += g:context_extra_options
  12. endif
  13. cmd->add(path)
  14. return cmd
  15. enddef
  16. export def Typeset(bufname: string, env = {}, Cmd = ConTeXtCmd): bool
  17. return typeset.TypesetBuffer(bufname, Cmd, env, 'ConTeXt')
  18. enddef
  19. export def JobStatus()
  20. typeset.JobStatus('ConTeXt')
  21. enddef
  22. export def StopJobs()
  23. typeset.StopJobs('ConTeXt')
  24. enddef
  25. export def Log(bufname: string)
  26. execute 'edit' typeset.LogPath(bufname)
  27. enddef
  28. # }}}
  29. # Completion {{{
  30. def BinarySearch(base: string, keywords: list<string>): list<string>
  31. const pat = '^' .. base
  32. const len = len(keywords)
  33. var res = []
  34. var lft = 0
  35. var rgt = len
  36. # Find the leftmost index matching base
  37. while lft < rgt
  38. var i = (lft + rgt) / 2
  39. if keywords[i] < base
  40. lft = i + 1
  41. else
  42. rgt = i
  43. endif
  44. endwhile
  45. while lft < len && keywords[lft] =~ pat
  46. add(res, keywords[lft])
  47. lft += 1
  48. endwhile
  49. return res
  50. enddef
  51. var isMetaPostBlock = false
  52. var MP_KEYWORDS: list<string> = []
  53. var CTX_KEYWORDS: list<string> = []
  54. # Complete only MetaPost keywords in MetaPost blocks, and complete only
  55. # ConTeXt keywords otherwise.
  56. export def Complete(findstart: number, base: string): any
  57. if findstart == 1
  58. if len(synstack(line("."), 1)) > 0 && synIDattr(synstack(line("."), 1)[0], "name") ==# 'contextMPGraphic'
  59. isMetaPostBlock = true
  60. return match(getline('.'), '\S\+\%' .. col('.') .. 'c')
  61. endif
  62. # Complete only \commands starting with a backslash
  63. isMetaPostBlock = false
  64. var pos = match(getline('.'), '\\\zs\S\+\%' .. col('.') .. 'c')
  65. return (pos == -1) ? -3 : pos
  66. endif
  67. if isMetaPostBlock
  68. if empty(MP_KEYWORDS)
  69. MP_KEYWORDS = sort(syntaxcomplete#OmniSyntaxList(['mf\w\+', 'mp\w\+']))
  70. endif
  71. return BinarySearch(base, MP_KEYWORDS)
  72. endif
  73. if empty(CTX_KEYWORDS)
  74. CTX_KEYWORDS = sort(syntaxcomplete#OmniSyntaxList([
  75. 'context\w\+', 'texAleph', 'texEtex', 'texLuatex', 'texOmega',
  76. 'texPdftex', 'texTex', 'texXeTeX'
  77. ]))
  78. endif
  79. return BinarySearch(base, CTX_KEYWORDS)
  80. enddef
  81. # }}}
  82. # vim: sw=2 fdm=marker