mp.vim 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. vim9script
  2. # Vim filetype plugin file
  3. # Language: MetaPost
  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. b:did_ftplugin = 1
  11. b:undo_ftplugin = "setl com< cms< fo< sua< inc< def< ofu<"
  12. setlocal comments=:%
  13. setlocal commentstring=%\ %s
  14. setlocal formatoptions+=cjroql2
  15. setlocal formatoptions-=t
  16. setlocal omnifunc=syntaxcomplete#Complete
  17. setlocal suffixesadd=.mp,.mpiv,.mpvi,.mpxl
  18. &l:include = '\<\%(input\|loadmodule\)\>' # loadmodule is from MetaFun
  19. &l:define = '\<\%(let\|newinternal\|interim\|def\|vardef\)\>\|\<\%(primary\|secondary\|tertiary\)def\>\s*[^ .]\+'
  20. g:omni_syntax_group_include_mp = 'mf\w\+,mp\w\+,metafun\w\+'
  21. g:omni_syntax_group_exclude_mp = 'mfTodoComment'
  22. var fignum: number
  23. def FixBeginfigs()
  24. fignum = 1
  25. g/^\s*beginfig(\d*)\s*;\(\s*%.*\)\=$/s/^.\{-};/\='beginfig(' .. fignum .. ');'/ | ++fignum
  26. enddef
  27. command! -buffer -nargs=0 -bar FixBeginfigs FixBeginfigs()
  28. if exists("g:loaded_matchit") && !exists("b:match_words")
  29. b:match_ignorecase = 0
  30. b:match_skip = 'synIDattr(synID(line("."), col("."), 1), "name") =~# "^mf\\%(Comment\\|String\\|\\)$\\|^mpTeXinsert$"'
  31. b:match_words = '\<if\>:\<else\%[if]\>:\<fi\>,'
  32. .. '\<for\%(\|suffixes\|ever\)\>:\<exit\%(if\|unless\)\>:\<endfor\>,'
  33. .. '\<\%(\|var\|primary\|secondary\|tertiary\)def\>:\<enddef\>,'
  34. .. '\<begin\(\a\+\)\>:end\1,'
  35. .. '\<beginlogochar\>:\<endchar\>'
  36. b:undo_ftplugin ..= "| unlet! b:match_ignorecase b:match_words b:match_skip"
  37. endif
  38. if !get(g:, 'no_mp_maps', 0) && !get(g:, 'no_plugin_maps', 0)
  39. const mp_regex = {
  40. 'beginsection': '^\s*\%(\%(\|var\|primary\|secondary\|tertiary\)def\|begin\%(fig\|char\|logochar\|glyph\|graph\)\)\>',
  41. 'endsection': '^\s*\%(enddef\|end\%(fig\|char\|glyph\|graph\)\)\>',
  42. 'beginblock': '^\s*\%(begingroup\|if\|for\%(\|suffixes\|ever\)\)\>',
  43. 'endblock': '^\s*\%(endgroup\|fi\|endfor\)\>'}
  44. def MoveAround(count: number, what: string, flags: string)
  45. search(mp_regex[what], flags .. 's') # 's' sets previous context mark
  46. var i = 2
  47. while i <= count
  48. search(mp_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. b:undo_ftplugin ..= printf(" | silent! execute 'nunmap <buffer> %s'", mapping)
  67. b:undo_ftplugin ..= printf(" | silent! execute 'vunmap <buffer> %s'", mapping)
  68. endfor
  69. endif
  70. if (has('gui_win32') || has('gui_gtk')) && !exists('b:browsefilter')
  71. b:browsefilter = "MetaPost Source Files (*.mp)\t*.mp\n"
  72. .. "All Files (*.*)\t*.*\n"
  73. b:undo_ftplugin ..= ' | unlet! b:browsefilter'
  74. endif
  75. # vim: sw=2 fdm=marker