vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

context.vim (2667B)


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