blob: fc593097682a87425bacb5047c71320935f058cb [file] [log] [blame]
Bram Moolenaare1f3fd12022-08-15 18:51:32 +01001vim9script
Bram Moolenaar46fceaa2016-10-23 21:21:08 +02002
Bram Moolenaare1f3fd12022-08-15 18:51:32 +01003# Language: ConTeXt typesetting engine
4# Maintainer: Nicola Vitacolonna <nvitacolonna@gmail.com>
5# Former Maintainers: Nikolai Weibull <now@bitwi.se>
Bram Moolenaar9fbdbb82022-09-27 17:30:34 +01006# Latest Revision: 2022 Sep 19
Bram Moolenaar46fceaa2016-10-23 21:21:08 +02007
Bram Moolenaare1f3fd12022-08-15 18:51:32 +01008# Typesetting {{{
9import autoload './typeset.vim'
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020010
Bram Moolenaare1f3fd12022-08-15 18:51:32 +010011export def ConTeXtCmd(path: string): list<string>
Bram Moolenaar9fbdbb82022-09-27 17:30:34 +010012 var cmd = ['mtxrun', '--script', 'context', '--nonstopmode', '--autogenerate']
13 if !empty(get(g:, 'context_extra_options', ''))
14 cmd += g:context_extra_options
15 endif
16 cmd->add(path)
17 return cmd
Bram Moolenaare1f3fd12022-08-15 18:51:32 +010018enddef
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020019
Bram Moolenaare1f3fd12022-08-15 18:51:32 +010020export def Typeset(bufname: string, env = {}, Cmd = ConTeXtCmd): bool
21 return typeset.TypesetBuffer(bufname, Cmd, env, 'ConTeXt')
22enddef
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020023
Bram Moolenaare1f3fd12022-08-15 18:51:32 +010024export def JobStatus()
25 typeset.JobStatus('ConTeXt')
26enddef
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020027
Bram Moolenaare1f3fd12022-08-15 18:51:32 +010028export def StopJobs()
29 typeset.StopJobs('ConTeXt')
30enddef
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020031
Bram Moolenaare1f3fd12022-08-15 18:51:32 +010032export def Log(bufname: string)
33 execute 'edit' typeset.LogPath(bufname)
34enddef
35# }}}
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020036
Bram Moolenaare1f3fd12022-08-15 18:51:32 +010037# Completion {{{
38def BinarySearch(base: string, keywords: list<string>): list<string>
39 const pat = '^' .. base
40 const len = len(keywords)
41 var res = []
42 var lft = 0
43 var rgt = len
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020044
Bram Moolenaare1f3fd12022-08-15 18:51:32 +010045 # Find the leftmost index matching base
46 while lft < rgt
47 var i = (lft + rgt) / 2
48 if keywords[i] < base
49 lft = i + 1
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020050 else
Bram Moolenaare1f3fd12022-08-15 18:51:32 +010051 rgt = i
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020052 endif
Bram Moolenaare1f3fd12022-08-15 18:51:32 +010053 endwhile
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020054
Bram Moolenaare1f3fd12022-08-15 18:51:32 +010055 while lft < len && keywords[lft] =~ pat
56 add(res, keywords[lft])
57 lft += 1
58 endwhile
59
60 return res
61enddef
62
63var isMetaPostBlock = false
64
65var MP_KEYWORDS: list<string> = []
66var CTX_KEYWORDS: list<string> = []
67
68# Complete only MetaPost keywords in MetaPost blocks, and complete only
69# ConTeXt keywords otherwise.
70export def Complete(findstart: number, base: string): any
71 if findstart == 1
72 if len(synstack(line("."), 1)) > 0 && synIDattr(synstack(line("."), 1)[0], "name") ==# 'contextMPGraphic'
73 isMetaPostBlock = true
74 return match(getline('.'), '\S\+\%' .. col('.') .. 'c')
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020075 endif
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020076
Bram Moolenaare1f3fd12022-08-15 18:51:32 +010077 # Complete only \commands starting with a backslash
78 isMetaPostBlock = false
79 var pos = match(getline('.'), '\\\zs\S\+\%' .. col('.') .. 'c')
80 return (pos == -1) ? -3 : pos
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020081 endif
Bram Moolenaare1f3fd12022-08-15 18:51:32 +010082
83 if isMetaPostBlock
84 if empty(MP_KEYWORDS)
85 MP_KEYWORDS = sort(syntaxcomplete#OmniSyntaxList(['mf\w\+', 'mp\w\+']))
86 endif
87 return BinarySearch(base, MP_KEYWORDS)
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020088 endif
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020089
Bram Moolenaare1f3fd12022-08-15 18:51:32 +010090 if empty(CTX_KEYWORDS)
91 CTX_KEYWORDS = sort(syntaxcomplete#OmniSyntaxList([
92 'context\w\+', 'texAleph', 'texEtex', 'texLuatex', 'texOmega',
93 'texPdftex', 'texTex', 'texXeTeX'
94 ]))
95 endif
96 return BinarySearch(base, CTX_KEYWORDS)
97enddef
98# }}}
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020099
Bram Moolenaare1f3fd12022-08-15 18:51:32 +0100100# vim: sw=2 fdm=marker