context.vim 4.3 KB

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