1" Vim plugin for showing matching parens 2" Maintainer: Bram Moolenaar <[email protected]> 3" Last Change: 2006 Mar 14 4 5" Exit quickly when: 6" - this plugin was already loaded (or disabled) 7" - when 'compatible' is set 8" - the "CursorMoved" autocmd event is not availble. 9if exists("g:loaded_matchparen") || &cp || !exists("##CursorMoved") 10 finish 11endif 12let g:loaded_matchparen = 1 13 14augroup matchparen 15 " Replace all matchparen autocommands 16 autocmd! CursorMoved,CursorMovedI * call s:Highlight_Matching_Pair() 17augroup END 18 19let s:paren_hl_on = 0 20 21" Skip the rest if it was already done. 22if exists("*s:Highlight_Matching_Pair") 23 finish 24endif 25 26let cpo_save = &cpo 27set cpo-=C 28 29" The function that is invoked (very often) to define a ":match" highlighting 30" for any matching paren. 31function! s:Highlight_Matching_Pair() 32 " Remove any previous match. 33 if s:paren_hl_on 34 3match none 35 let s:paren_hl_on = 0 36 endif 37 38 " Avoid that we remove the popup menu. 39 if pumvisible() 40 return 41 endif 42 43 " Get the character under the cursor and check if it's in 'matchpairs'. 44 let c_lnum = line('.') 45 let c_col = col('.') 46 let before = 0 47 48 let c = getline(c_lnum)[c_col - 1] 49 let plist = split(&matchpairs, ':\|,') 50 let i = index(plist, c) 51 if i < 0 52 " not found, in Insert mode try character before the cursor 53 if c_col > 1 && (mode() == 'i' || mode() == 'R') 54 let before = 1 55 let c = getline(c_lnum)[c_col - 2] 56 let i = index(plist, c) 57 endif 58 if i < 0 59 " not found, nothing to do 60 return 61 endif 62 endif 63 64 " Figure out the arguments for searchpairpos(). 65 " Restrict the search to visible lines with "stopline". 66 if i % 2 == 0 67 let s_flags = 'nW' 68 let c2 = plist[i + 1] 69 let stopline = line('w$') 70 else 71 let s_flags = 'nbW' 72 let c2 = c 73 let c = plist[i - 1] 74 let stopline = line('w0') 75 endif 76 if c == '[' 77 let c = '\[' 78 let c2 = '\]' 79 endif 80 81 " When not in a string or comment ignore matches inside them. 82 let s_skip ='synIDattr(synID(c_lnum, c_col - before, 0), "name") ' . 83 \ '=~? "string\\|comment"' 84 execute 'if' s_skip '| let s_skip = 0 | endif' 85 86 " Find the match. When it was just before the cursor move it there for a 87 " moment. 88 if before > 0 89 let save_cursor = getpos('.') 90 call cursor(c_lnum, c_col - before) 91 endif 92 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline) 93 if before > 0 94 call setpos('.', save_cursor) 95 endif 96 97 " If a match is found setup match highlighting. 98 if m_lnum > 0 && m_lnum >= line('w0') && m_lnum <= line('w$') 99 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) . 100 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/' 101 let s:paren_hl_on = 1 102 endif 103endfunction 104 105" Define commands that will disable and enable the plugin. 106command! NoMatchParen 3match none | unlet! g:loaded_matchparen | au! matchparen 107command! DoMatchParen runtime plugin/matchparen.vim | doau CursorMoved 108 109let &cpo = cpo_save 110