matchit.vim (5076B)
1 " matchit.vim: (global plugin) Extended "%" matching 2 " Maintainer: Christian Brabandt 3 " Version: 1.21 4 " Last Change: 2024 May 20 5 " Repository: https://github.com/chrisbra/matchit 6 " Previous URL:http://www.vim.org/script.php?script_id=39 7 " Previous Maintainer: Benji Fisher PhD <benji@member.AMS.org> 8 9 " Documentation: 10 " The documentation is in a separate file: ../doc/matchit.txt 11 12 " Credits: 13 " Vim editor by Bram Moolenaar (Thanks, Bram!) 14 " Original script and design by Raul Segura Acevedo 15 " Support for comments by Douglas Potts 16 " Support for back references and other improvements by Benji Fisher 17 " Support for many languages by Johannes Zellner 18 " Suggestions for improvement, bug reports, and support for additional 19 " languages by Jordi-Albert Batalla, Neil Bird, Servatius Brandt, Mark 20 " Collett, Stephen Wall, Dany St-Amant, Yuheng Xie, and Johannes Zellner. 21 22 " Debugging: 23 " If you'd like to try the built-in debugging commands... 24 " :MatchDebug to activate debugging for the current buffer 25 " This saves the values of several key script variables as buffer-local 26 " variables. See the MatchDebug() function, below, for details. 27 28 " TODO: I should think about multi-line patterns for b:match_words. 29 " This would require an option: how many lines to scan (default 1). 30 " This would be useful for Python, maybe also for *ML. 31 " TODO: Maybe I should add a menu so that people will actually use some of 32 " the features that I have implemented. 33 " TODO: Eliminate the MultiMatch function. Add yet another argument to 34 " Match_wrapper() instead. 35 " TODO: Allow :let b:match_words = '\(\(foo\)\(bar\)\):\3\2:end\1' 36 " TODO: Make backrefs safer by using '\V' (very no-magic). 37 " TODO: Add a level of indirection, so that custom % scripts can use my 38 " work but extend it. 39 40 " Allow user to prevent loading and prevent duplicate loading. 41 if exists("g:loaded_matchit") || &cp 42 finish 43 endif 44 let g:loaded_matchit = 1 45 46 let s:save_cpo = &cpo 47 set cpo&vim 48 49 fun MatchEnable() 50 nnoremap <silent> <Plug>(MatchitNormalForward) :<C-U>call matchit#Match_wrapper('',1,'n')<CR> 51 nnoremap <silent> <Plug>(MatchitNormalBackward) :<C-U>call matchit#Match_wrapper('',0,'n')<CR> 52 xnoremap <silent> <Plug>(MatchitVisualForward) :<C-U>call matchit#Match_wrapper('',1,'v')<CR> 53 \:if col("''") != col("$") \| exe ":normal! m'" \| endif<cr>gv`` 54 xnoremap <silent> <Plug>(MatchitVisualBackward) :<C-U>call matchit#Match_wrapper('',0,'v')<CR>m'gv`` 55 onoremap <silent> <Plug>(MatchitOperationForward) :<C-U>call matchit#Match_wrapper('',1,'o')<CR> 56 onoremap <silent> <Plug>(MatchitOperationBackward) :<C-U>call matchit#Match_wrapper('',0,'o')<CR> 57 58 " Analogues of [{ and ]} using matching patterns: 59 nnoremap <silent> <Plug>(MatchitNormalMultiBackward) :<C-U>call matchit#MultiMatch("bW", "n")<CR> 60 nnoremap <silent> <Plug>(MatchitNormalMultiForward) :<C-U>call matchit#MultiMatch("W", "n")<CR> 61 xnoremap <silent> <Plug>(MatchitVisualMultiBackward) :<C-U>call matchit#MultiMatch("bW", "n")<CR>m'gv`` 62 xnoremap <silent> <Plug>(MatchitVisualMultiForward) :<C-U>call matchit#MultiMatch("W", "n")<CR>m'gv`` 63 onoremap <silent> <Plug>(MatchitOperationMultiBackward) :<C-U>call matchit#MultiMatch("bW", "o")<CR> 64 onoremap <silent> <Plug>(MatchitOperationMultiForward) :<C-U>call matchit#MultiMatch("W", "o")<CR> 65 66 " text object: 67 xmap <silent> <Plug>(MatchitVisualTextObject) <Plug>(MatchitVisualMultiBackward)o<Plug>(MatchitVisualMultiForward) 68 69 if !exists("g:no_plugin_maps") 70 nmap <silent> % <Plug>(MatchitNormalForward) 71 nmap <silent> g% <Plug>(MatchitNormalBackward) 72 xmap <silent> % <Plug>(MatchitVisualForward) 73 xmap <silent> g% <Plug>(MatchitVisualBackward) 74 omap <silent> % <Plug>(MatchitOperationForward) 75 omap <silent> g% <Plug>(MatchitOperationBackward) 76 77 " Analogues of [{ and ]} using matching patterns: 78 nmap <silent> [% <Plug>(MatchitNormalMultiBackward) 79 nmap <silent> ]% <Plug>(MatchitNormalMultiForward) 80 xmap <silent> [% <Plug>(MatchitVisualMultiBackward) 81 xmap <silent> ]% <Plug>(MatchitVisualMultiForward) 82 omap <silent> [% <Plug>(MatchitOperationMultiBackward) 83 omap <silent> ]% <Plug>(MatchitOperationMultiForward) 84 85 " Text object 86 xmap a% <Plug>(MatchitVisualTextObject) 87 endif 88 endfun 89 90 fun MatchDisable() 91 " remove all the setup keymappings 92 nunmap % 93 nunmap g% 94 xunmap % 95 xunmap g% 96 ounmap % 97 ounmap g% 98 99 nunmap [% 100 nunmap ]% 101 xunmap [% 102 xunmap ]% 103 ounmap [% 104 ounmap ]% 105 106 xunmap a% 107 endfun 108 109 " Call this function to turn on debugging information. Every time the main 110 " script is run, buffer variables will be saved. These can be used directly 111 " or viewed using the menu items below. 112 if !exists(":MatchDebug") 113 command! -nargs=0 MatchDebug call matchit#Match_debug() 114 endif 115 if !exists(":MatchDisable") 116 command! -nargs=0 MatchDisable :call MatchDisable() 117 endif 118 if !exists(":MatchEnable") 119 command! -nargs=0 MatchEnable :call MatchEnable() 120 endif 121 122 call MatchEnable() 123 124 let &cpo = s:save_cpo 125 unlet s:save_cpo 126 127 " vim:sts=2:sw=2:et: