1" Vim plugin for showing matching parens 2" Maintainer: Bram Moolenaar <[email protected]> 3" Last Change: 2016 Feb 09 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 14if !exists("g:matchparen_timeout") 15 let g:matchparen_timeout = 300 16endif 17if !exists("g:matchparen_insert_timeout") 18 let g:matchparen_insert_timeout = 60 19endif 20 21augroup matchparen 22 " Replace all matchparen autocommands 23 autocmd! CursorMoved,CursorMovedI,WinEnter * call s:Highlight_Matching_Pair() 24 if exists('##TextChanged') 25 autocmd! TextChanged,TextChangedI * call s:Highlight_Matching_Pair() 26 endif 27augroup END 28 29" Skip the rest if it was already done. 30if exists("*s:Highlight_Matching_Pair") 31 finish 32endif 33 34let s:cpo_save = &cpo 35set cpo-=C 36 37" The function that is invoked (very often) to define a ":match" highlighting 38" for any matching paren. 39function! s:Highlight_Matching_Pair() 40 " Remove any previous match. 41 if exists('w:paren_hl_on') && w:paren_hl_on 42 silent! call matchdelete(3) 43 let w:paren_hl_on = 0 44 endif 45 46 " Avoid that we remove the popup menu. 47 " Return when there are no colors (looks like the cursor jumps). 48 if pumvisible() || (&t_Co < 8 && !has("gui_running")) 49 return 50 endif 51 52 " Get the character under the cursor and check if it's in 'matchpairs'. 53 let c_lnum = line('.') 54 let c_col = col('.') 55 let before = 0 56 57 let text = getline(c_lnum) 58 let matches = matchlist(text, '\(.\)\=\%'.c_col.'c\(.\=\)') 59 if empty(matches) 60 let [c_before, c] = ['', ''] 61 else 62 let [c_before, c] = matches[1:2] 63 endif 64 let plist = split(&matchpairs, '.\zs[:,]') 65 let i = index(plist, c) 66 if i < 0 67 " not found, in Insert mode try character before the cursor 68 if c_col > 1 && (mode() == 'i' || mode() == 'R') 69 let before = strlen(c_before) 70 let c = c_before 71 let i = index(plist, c) 72 endif 73 if i < 0 74 " not found, nothing to do 75 return 76 endif 77 endif 78 79 " Figure out the arguments for searchpairpos(). 80 if i % 2 == 0 81 let s_flags = 'nW' 82 let c2 = plist[i + 1] 83 else 84 let s_flags = 'nbW' 85 let c2 = c 86 let c = plist[i - 1] 87 endif 88 if c == '[' 89 let c = '\[' 90 let c2 = '\]' 91 endif 92 93 " Find the match. When it was just before the cursor move it there for a 94 " moment. 95 if before > 0 96 let has_getcurpos = exists("*getcurpos") 97 if has_getcurpos 98 " getcurpos() is more efficient but doesn't exist before 7.4.313. 99 let save_cursor = getcurpos() 100let g:saved_cursor = save_cursor 101 else 102 let save_cursor = winsaveview() 103 endif 104 call cursor(c_lnum, c_col - before) 105 endif 106 107 " Build an expression that detects whether the current cursor position is in 108 " certain syntax types (string, comment, etc.), for use as searchpairpos()'s 109 " skip argument. 110 " We match "escape" for special items, such as lispEscapeSpecial. 111 let s_skip = '!empty(filter(map(synstack(line("."), col(".")), ''synIDattr(v:val, "name")''), ' . 112 \ '''v:val =~? "string\\|character\\|singlequote\\|escape\\|comment"''))' 113 " If executing the expression determines that the cursor is currently in 114 " one of the syntax types, then we want searchpairpos() to find the pair 115 " within those syntax types (i.e., not skip). Otherwise, the cursor is 116 " outside of the syntax types and s_skip should keep its value so we skip any 117 " matching pair inside the syntax types. 118 execute 'if' s_skip '| let s_skip = 0 | endif' 119 120 " Limit the search to lines visible in the window. 121 let stoplinebottom = line('w$') 122 let stoplinetop = line('w0') 123 if i % 2 == 0 124 let stopline = stoplinebottom 125 else 126 let stopline = stoplinetop 127 endif 128 129 " Limit the search time to 300 msec to avoid a hang on very long lines. 130 " This fails when a timeout is not supported. 131 if mode() == 'i' || mode() == 'R' 132 let timeout = exists("b:matchparen_insert_timeout") ? b:matchparen_insert_timeout : g:matchparen_insert_timeout 133 else 134 let timeout = exists("b:matchparen_timeout") ? b:matchparen_timeout : g:matchparen_timeout 135 endif 136 try 137 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, timeout) 138 catch /E118/ 139 " Can't use the timeout, restrict the stopline a bit more to avoid taking 140 " a long time on closed folds and long lines. 141 " The "viewable" variables give a range in which we can scroll while 142 " keeping the cursor at the same position. 143 " adjustedScrolloff accounts for very large numbers of scrolloff. 144 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2]) 145 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2]) 146 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2]) 147 " one of these stoplines will be adjusted below, but the current values are 148 " minimal boundaries within the current window 149 if i % 2 == 0 150 if has("byte_offset") && has("syntax_items") && &smc > 0 151 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2]) 152 let stopline = min([bottom_viewable, byte2line(stopbyte)]) 153 else 154 let stopline = min([bottom_viewable, c_lnum + 100]) 155 endif 156 let stoplinebottom = stopline 157 else 158 if has("byte_offset") && has("syntax_items") && &smc > 0 159 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2]) 160 let stopline = max([top_viewable, byte2line(stopbyte)]) 161 else 162 let stopline = max([top_viewable, c_lnum - 100]) 163 endif 164 let stoplinetop = stopline 165 endif 166 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline) 167 endtry 168 169 if before > 0 170 if has_getcurpos 171 call setpos('.', save_cursor) 172 else 173 call winrestview(save_cursor) 174 endif 175 endif 176 177 " If a match is found setup match highlighting. 178 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom 179 if exists('*matchaddpos') 180 call matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10, 3) 181 else 182 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) . 183 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/' 184 endif 185 let w:paren_hl_on = 1 186 endif 187endfunction 188 189" Define commands that will disable and enable the plugin. 190command! NoMatchParen windo silent! call matchdelete(3) | unlet! g:loaded_matchparen | 191 \ au! matchparen 192command! DoMatchParen runtime plugin/matchparen.vim | windo doau CursorMoved 193 194let &cpo = s:cpo_save 195unlet s:cpo_save 196