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