vim

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

mp.vim (3966B)


      1 vim9script
      2 
      3 # Vim filetype plugin file
      4 # Language:           MetaPost
      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 b:did_ftplugin = 1
     14 b:undo_ftplugin = "setl com< cms< fo< sua< inc< def< ofu<"
     15 
     16 setlocal comments=:%
     17 setlocal commentstring=%\ %s
     18 setlocal formatoptions+=cjroql2
     19 setlocal formatoptions-=t
     20 setlocal omnifunc=syntaxcomplete#Complete
     21 setlocal suffixesadd=.mp,.mpiv,.mpvi,.mpxl
     22 
     23 &l:include = '\<\%(input\|loadmodule\)\>'  # loadmodule is from MetaFun
     24 &l:define = '\<\%(let\|newinternal\|interim\|def\|vardef\)\>\|\<\%(primary\|secondary\|tertiary\)def\>\s*[^ .]\+'
     25 
     26 g:omni_syntax_group_include_mp = 'mf\w\+,mp\w\+,metafun\w\+'
     27 g:omni_syntax_group_exclude_mp = 'mfTodoComment'
     28 
     29 var fignum: number
     30 
     31 def FixBeginfigs()
     32   fignum = 1
     33   g/^\s*beginfig(\d*)\s*;\(\s*%.*\)\=$/s/^.\{-};/\='beginfig(' .. fignum .. ');'/ | ++fignum
     34 enddef
     35 
     36 command! -buffer -nargs=0 -bar FixBeginfigs FixBeginfigs()
     37 
     38 if exists("g:loaded_matchit") && !exists("b:match_words")
     39   b:match_ignorecase = 0
     40   b:match_skip = 'synIDattr(synID(line("."), col("."), 1), "name") =~# "^mf\\%(Comment\\|String\\|\\)$\\|^mpTeXinsert$"'
     41   b:match_words = '\<if\>:\<else\%[if]\>:\<fi\>,'
     42   ..              '\<for\%(\|suffixes\|ever\)\>:\<exit\%(if\|unless\)\>:\<endfor\>,'
     43   ..              '\<\%(\|var\|primary\|secondary\|tertiary\)def\>:\<enddef\>,'
     44   ..              '\<begin\(\a\+\)\>:end\1,'
     45   ..              '\<beginlogochar\>:\<endchar\>'
     46   b:undo_ftplugin ..= "| unlet! b:match_ignorecase b:match_words b:match_skip"
     47 endif
     48 
     49 if !get(g:, 'no_mp_maps', 0) && !get(g:, 'no_plugin_maps', 0)
     50   const mp_regex = {
     51     'beginsection': '^\s*\%(\%(\|var\|primary\|secondary\|tertiary\)def\|begin\%(fig\|char\|logochar\|glyph\|graph\)\)\>',
     52     'endsection':   '^\s*\%(enddef\|end\%(fig\|char\|glyph\|graph\)\)\>',
     53     'beginblock':   '^\s*\%(begingroup\|if\|for\%(\|suffixes\|ever\)\)\>',
     54     'endblock':     '^\s*\%(endgroup\|fi\|endfor\)\>'}
     55 
     56   def MoveAround(count: number, what: string, flags: string)
     57     search(mp_regex[what], flags .. 's')  # 's' sets previous context mark
     58     var i = 2
     59     while i <= count
     60       search(mp_regex[what], flags)
     61       i += 1
     62     endwhile
     63   enddef
     64 
     65   # Macros to move around
     66   nnoremap <silent><buffer> [[ <scriptcmd>MoveAround(v:count1, "beginsection", "bW")<cr>
     67   vnoremap <silent><buffer> [[ <scriptcmd>MoveAround(v:count1, "beginsection", "bW")<cr>
     68   nnoremap <silent><buffer> ]] <scriptcmd>MoveAround(v:count1, "beginsection", "W") <cr>
     69   vnoremap <silent><buffer> ]] <scriptcmd>MoveAround(v:count1, "beginsection", "W") <cr>
     70   nnoremap <silent><buffer> [] <scriptcmd>MoveAround(v:count1, "endsection",   "bW")<cr>
     71   vnoremap <silent><buffer> [] <scriptcmd>MoveAround(v:count1, "endsection",   "bW")<cr>
     72   nnoremap <silent><buffer> ][ <scriptcmd>MoveAround(v:count1, "endsection",   "W") <cr>
     73   vnoremap <silent><buffer> ][ <scriptcmd>MoveAround(v:count1, "endsection",   "W") <cr>
     74   nnoremap <silent><buffer> [{ <scriptcmd>MoveAround(v:count1, "beginblock",   "bW")<cr>
     75   vnoremap <silent><buffer> [{ <scriptcmd>MoveAround(v:count1, "beginblock",   "bW")<cr>
     76   nnoremap <silent><buffer> ]} <scriptcmd>MoveAround(v:count1, "endblock",     "W") <cr>
     77   vnoremap <silent><buffer> ]} <scriptcmd>MoveAround(v:count1, "endblock",     "W") <cr>
     78 
     79   for mapping in ["[[", "]]", "[]", "][", "[{", "]}"]
     80     b:undo_ftplugin ..= printf(" | silent! execute 'nunmap <buffer> %s'", mapping)
     81     b:undo_ftplugin ..= printf(" | silent! execute 'vunmap <buffer> %s'", mapping)
     82   endfor
     83 endif
     84 
     85 if (has('gui_win32') || has('gui_gtk')) && !exists('b:browsefilter')
     86   b:browsefilter = "MetaPost Source Files (*.mp)\t*.mp\n"
     87   if has("win32")
     88     b:browsefilter ..= "All Files (*.*)\t*\n"
     89   else
     90     b:browsefilter ..= "All Files (*)\t*\n"
     91   endif
     92   b:undo_ftplugin ..= ' | unlet! b:browsefilter'
     93 endif
     94 
     95 # vim: sw=2 fdm=marker