1" Vim plugin for showing matching parens 2" Maintainer: Bram Moolenaar <[email protected]> 3" Last Change: 2013 Mar 19 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 available. 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,WinEnter * call s:Highlight_Matching_Pair() 17 if exists('##TextChanged') 18 autocmd! TextChanged,TextChangedI * call s:Highlight_Matching_Pair() 19 endif 20augroup END 21 22" Skip the rest if it was already done. 23if exists("*s:Highlight_Matching_Pair") 24 finish 25endif 26 27let s:cpo_save = &cpo 28set cpo-=C 29 30" The function that is invoked (very often) to define a ":match" highlighting 31" for any matching paren. 32function! s:Highlight_Matching_Pair() 33 " Remove any previous match. 34 if exists('w:paren_hl_on') && w:paren_hl_on 35 3match none 36 let w:paren_hl_on = 0 37 endif 38 39 " Avoid that we remove the popup menu. 40 " Return when there are no colors (looks like the cursor jumps). 41 if pumvisible() || (&t_Co < 8 && !has("gui_running")) 42 return 43 endif 44 45 " Get the character under the cursor and check if it's in 'matchpairs'. 46 let c_lnum = line('.') 47 let c_col = col('.') 48 let before = 0 49 50 let c = getline(c_lnum)[c_col - 1] 51 let plist = split(&matchpairs, '.\zs[:,]') 52 let i = index(plist, c) 53 if i < 0 54 " not found, in Insert mode try character before the cursor 55 if c_col > 1 && (mode() == 'i' || mode() == 'R') 56 let before = 1 57 let c = getline(c_lnum)[c_col - 2] 58 let i = index(plist, c) 59 endif 60 if i < 0 61 " not found, nothing to do 62 return 63 endif 64 endif 65 66 " Figure out the arguments for searchpairpos(). 67 if i % 2 == 0 68 let s_flags = 'nW' 69 let c2 = plist[i + 1] 70 else 71 let s_flags = 'nbW' 72 let c2 = c 73 let c = plist[i - 1] 74 endif 75 if c == '[' 76 let c = '\[' 77 let c2 = '\]' 78 endif 79 80 " Find the match. When it was just before the cursor move it there for a 81 " moment. 82 if before > 0 83 let save_cursor = winsaveview() 84 call cursor(c_lnum, c_col - before) 85 endif 86 87 " When not in a string or comment ignore matches inside them. 88 " We match "escape" for special items, such as lispEscapeSpecial. 89 let s_skip ='synIDattr(synID(line("."), col("."), 0), "name") ' . 90 \ '=~? "string\\|character\\|singlequote\\|escape\\|comment"' 91 execute 'if' s_skip '| let s_skip = 0 | endif' 92 93 " Limit the search to lines visible in the window. 94 let stoplinebottom = line('w$') 95 let stoplinetop = line('w0') 96 if i % 2 == 0 97 let stopline = stoplinebottom 98 else 99 let stopline = stoplinetop 100 endif 101 102 try 103 " Limit the search time to 300 msec to avoid a hang on very long lines. 104 " This fails when a timeout is not supported. 105 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, 300) 106 catch /E118/ 107 " Can't use the timeout, restrict the stopline a bit more to avoid taking 108 " a long time on closed folds and long lines. 109 " The "viewable" variables give a range in which we can scroll while 110 " keeping the cursor at the same position. 111 " adjustedScrolloff accounts for very large numbers of scrolloff. 112 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2]) 113 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2]) 114 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2]) 115 " one of these stoplines will be adjusted below, but the current values are 116 " minimal boundaries within the current window 117 if i % 2 == 0 118 if has("byte_offset") && has("syntax_items") && &smc > 0 119 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2]) 120 let stopline = min([bottom_viewable, byte2line(stopbyte)]) 121 else 122 let stopline = min([bottom_viewable, c_lnum + 100]) 123 endif 124 let stoplinebottom = stopline 125 else 126 if has("byte_offset") && has("syntax_items") && &smc > 0 127 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2]) 128 let stopline = max([top_viewable, byte2line(stopbyte)]) 129 else 130 let stopline = max([top_viewable, c_lnum - 100]) 131 endif 132 let stoplinetop = stopline 133 endif 134 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline) 135 endtry 136 137 if before > 0 138 call winrestview(save_cursor) 139 endif 140 141 " If a match is found setup match highlighting. 142 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom 143 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) . 144 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/' 145 let w:paren_hl_on = 1 146 endif 147endfunction 148 149" Define commands that will disable and enable the plugin. 150command! NoMatchParen windo 3match none | unlet! g:loaded_matchparen | 151 \ au! matchparen 152command! DoMatchParen runtime plugin/matchparen.vim | windo doau CursorMoved 153 154let &cpo = s:cpo_save 155unlet s:cpo_save 156