vim

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

context.vim (4646B)


      1 vim9script
      2 
      3 # Vim filetype plugin file
      4 # Language:           ConTeXt typesetting engine
      5 # Maintainer:         Nicola Vitacolonna <nvitacolonna@gmail.com>
      6 # Former Maintainers: Nikolai Weibull <now@bitwi.se>
      7 # Latest Revision:    2026 Jan 10
      8 
      9 if exists("b:did_ftplugin")
     10   finish
     11 endif
     12 
     13 import autoload '../autoload/context.vim'
     14 
     15 b:did_ftplugin = 1
     16 
     17 b:undo_ftplugin = "setl com< cms< def< inc< sua< fo< ofu<"
     18 
     19 setlocal comments=b:%D,b:%C,b:%M,:%
     20 setlocal commentstring=%\ %s
     21 setlocal formatoptions+=tjcroql2
     22 setlocal omnifunc=context.Complete
     23 setlocal suffixesadd=.tex,.mkxl,.mkvi,.mkiv,.mkii
     24 
     25 &l:define = '\\\%([egx]\|char\|mathchar\|count\|dimen\|muskip\|skip\|toks\)\='
     26 ..          'def\|\\font\|\\\%(future\)\=let'
     27 ..          '\|\\new\%(count\|dimen\|skip\|muskip\|box\|toks\|read\|write'
     28 ..          '\|fam\|insert\|if\)'
     29 
     30 &l:include = '^\s*\\\%(input\|component\|product\|project\|environment\)'
     31 
     32 if exists("g:loaded_matchit") && !exists("b:match_words")
     33   b:match_ignorecase = 0
     34   b:match_skip = 'r:\\\@<!\%(\\\\\)*%'
     35   b:match_words = '(:),\[:],{:},\\(:\\),\\\[:\\],\\start\(\a\+\):\\stop\1'
     36   b:undo_ftplugin ..= "| unlet! b:match_ignorecase b:match_words b:match_skip"
     37 endif
     38 
     39 if !get(g:, 'no_context_maps', 0) && !get(g:, 'no_plugin_maps', 0)
     40   const context_regex = {
     41     'beginsection': '\\\%(start\)\=\%(\%(sub\)*section\|\%(sub\)*subject\|chapter\|part\|component\|product\|title\)\>',
     42     'endsection':   '\\\%(stop\)\=\%(\%(sub\)*section\|\%(sub\)*subject\|chapter\|part\|component\|product\|title\)\>',
     43     'beginblock':   '\\\%(start\|setup\|define\)',
     44     'endblock':     '\\\%(stop\|setup\|define\)',
     45     }
     46 
     47   def UndoMap(mapping: string, modes: string)
     48     for mode in modes
     49       b:undo_ftplugin ..= printf(" | silent! execute '%sunmap <buffer> %s'", mode, mapping)
     50     endfor
     51   enddef
     52 
     53   def MoveAround(count: number, what: string, flags: string)
     54     search(context_regex[what], flags .. 's')  # 's' sets previous context mark
     55     var i = 2
     56     while i <= count
     57       search(context_regex[what], flags)
     58       i += 1
     59     endwhile
     60   enddef
     61 
     62   # Macros to move around
     63   nnoremap <silent><buffer> [[ <scriptcmd>MoveAround(v:count1, "beginsection", "bW")<cr>
     64   vnoremap <silent><buffer> [[ <scriptcmd>MoveAround(v:count1, "beginsection", "bW")<cr>
     65   nnoremap <silent><buffer> ]] <scriptcmd>MoveAround(v:count1, "beginsection", "W") <cr>
     66   vnoremap <silent><buffer> ]] <scriptcmd>MoveAround(v:count1, "beginsection", "W") <cr>
     67   nnoremap <silent><buffer> [] <scriptcmd>MoveAround(v:count1, "endsection",   "bW")<cr>
     68   vnoremap <silent><buffer> [] <scriptcmd>MoveAround(v:count1, "endsection",   "bW")<cr>
     69   nnoremap <silent><buffer> ][ <scriptcmd>MoveAround(v:count1, "endsection",   "W") <cr>
     70   vnoremap <silent><buffer> ][ <scriptcmd>MoveAround(v:count1, "endsection",   "W") <cr>
     71   nnoremap <silent><buffer> [{ <scriptcmd>MoveAround(v:count1, "beginblock",   "bW")<cr>
     72   vnoremap <silent><buffer> [{ <scriptcmd>MoveAround(v:count1, "beginblock",   "bW")<cr>
     73   nnoremap <silent><buffer> ]} <scriptcmd>MoveAround(v:count1, "endblock",     "W") <cr>
     74   vnoremap <silent><buffer> ]} <scriptcmd>MoveAround(v:count1, "endblock",     "W") <cr>
     75 
     76   for mapping in ['[[', ']]', '[]', '][', '[{', ']}']
     77     UndoMap(mapping, 'nv')
     78   endfor
     79 
     80   # Other useful mappings
     81   const tp_regex = '?^$\|^\s*\\\(item\|start\|stop\|blank\|\%(sub\)*section\|chapter\|\%(sub\)*subject\|title\|part\)'
     82 
     83   def TeXPar()
     84     cursor(search(tp_regex, 'bcW') + 1, 1)
     85     normal! V
     86     cursor(search(tp_regex, 'W') - 1, 1)
     87   enddef
     88 
     89   # Reflow paragraphs with mappings like gqtp ("gq TeX paragraph")
     90   onoremap <silent><buffer> tp <scriptcmd>TeXPar()<cr>
     91   # Select TeX paragraph
     92   vnoremap <silent><buffer> tp <scriptcmd>TeXPar()<cr>
     93 
     94   # $...$ text object
     95   onoremap <silent><buffer> i$ <scriptcmd>normal! T$vt$<cr>
     96   onoremap <silent><buffer> a$ <scriptcmd>normal! F$vf$<cr>
     97   vnoremap <buffer> i$ T$ot$
     98   vnoremap <buffer> a$ F$of$
     99 
    100   for mapping in ['tp', 'i$', 'a$']
    101     UndoMap(mapping, 'ov')
    102   endfor
    103 endif
    104 
    105 if !exists('current_compiler')
    106   b:undo_ftplugin ..= "| compiler make"
    107   compiler context
    108 endif
    109 
    110 b:undo_ftplugin ..= "| sil! delc -buffer ConTeXt | sil! delc -buffer ConTeXtLog | sil! delc -buffer ConTeXtJobStatus | sil! delc -buffer ConTeXtStopJobs"
    111 # Commands for asynchronous typesetting
    112 command! -buffer -nargs=? -complete=buffer ConTeXt          context.Typeset(<q-args>)
    113 command! -buffer -nargs=0                  ConTeXtLog       context.Log(bufname('%'))
    114 command!         -nargs=0                  ConTeXtJobStatus context.JobStatus()
    115 command!         -nargs=0                  ConTeXtStopJobs  context.StopJobs()
    116 
    117 # vim: sw=2 fdm=marker