Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 1 | " Vim plugin for showing matching parens |
| 2 | " Maintainer: Bram Moolenaar <Bram@vim.org> |
Bram Moolenaar | 1ff14ba | 2019-11-02 14:09:23 +0100 | [diff] [blame] | 3 | " Last Change: 2019 Oct 28 |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 4 | |
| 5 | " Exit quickly when: |
| 6 | " - this plugin was already loaded (or disabled) |
| 7 | " - when 'compatible' is set |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 8 | " - the "CursorMoved" autocmd event is not available. |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 9 | if exists("g:loaded_matchparen") || &cp || !exists("##CursorMoved") |
| 10 | finish |
| 11 | endif |
| 12 | let g:loaded_matchparen = 1 |
| 13 | |
Bram Moolenaar | ad3b366 | 2013-05-17 18:14:19 +0200 | [diff] [blame] | 14 | if !exists("g:matchparen_timeout") |
| 15 | let g:matchparen_timeout = 300 |
| 16 | endif |
| 17 | if !exists("g:matchparen_insert_timeout") |
| 18 | let g:matchparen_insert_timeout = 60 |
| 19 | endif |
| 20 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 21 | augroup matchparen |
| 22 | " Replace all matchparen autocommands |
Bram Moolenaar | fa2e044 | 2007-08-18 16:21:50 +0000 | [diff] [blame] | 23 | autocmd! CursorMoved,CursorMovedI,WinEnter * call s:Highlight_Matching_Pair() |
Bram Moolenaar | 186628f | 2013-03-19 13:33:23 +0100 | [diff] [blame] | 24 | if exists('##TextChanged') |
| 25 | autocmd! TextChanged,TextChangedI * call s:Highlight_Matching_Pair() |
| 26 | endif |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 27 | augroup END |
| 28 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 29 | " Skip the rest if it was already done. |
| 30 | if exists("*s:Highlight_Matching_Pair") |
| 31 | finish |
| 32 | endif |
| 33 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 34 | let s:cpo_save = &cpo |
Bram Moolenaar | 3b1ddfe | 2006-03-14 22:55:34 +0000 | [diff] [blame] | 35 | set cpo-=C |
| 36 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 37 | " The function that is invoked (very often) to define a ":match" highlighting |
| 38 | " for any matching paren. |
Bram Moolenaar | 1ff14ba | 2019-11-02 14:09:23 +0100 | [diff] [blame] | 39 | func s:Highlight_Matching_Pair() |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 40 | " Remove any previous match. |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 41 | if exists('w:paren_hl_on') && w:paren_hl_on |
Bram Moolenaar | b341459 | 2014-06-17 17:48:32 +0200 | [diff] [blame] | 42 | silent! call matchdelete(3) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 43 | let w:paren_hl_on = 0 |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 44 | endif |
| 45 | |
Bram Moolenaar | 36fc535 | 2006-03-04 21:49:37 +0000 | [diff] [blame] | 46 | " Avoid that we remove the popup menu. |
Bram Moolenaar | f2b2e70 | 2008-03-09 15:45:53 +0000 | [diff] [blame] | 47 | " Return when there are no colors (looks like the cursor jumps). |
| 48 | if pumvisible() || (&t_Co < 8 && !has("gui_running")) |
Bram Moolenaar | 36fc535 | 2006-03-04 21:49:37 +0000 | [diff] [blame] | 49 | return |
| 50 | endif |
| 51 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 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 | |
Bram Moolenaar | db6ea06 | 2014-07-10 22:01:47 +0200 | [diff] [blame] | 57 | let text = getline(c_lnum) |
Bram Moolenaar | c21d67e | 2015-12-31 22:27:55 +0100 | [diff] [blame] | 58 | let matches = matchlist(text, '\(.\)\=\%'.c_col.'c\(.\=\)') |
Bram Moolenaar | 256972a | 2015-12-29 19:10:25 +0100 | [diff] [blame] | 59 | if empty(matches) |
| 60 | let [c_before, c] = ['', ''] |
| 61 | else |
| 62 | let [c_before, c] = matches[1:2] |
| 63 | endif |
Bram Moolenaar | 41e6cd5 | 2006-09-09 11:37:51 +0000 | [diff] [blame] | 64 | let plist = split(&matchpairs, '.\zs[:,]') |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 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') |
Bram Moolenaar | 256972a | 2015-12-29 19:10:25 +0100 | [diff] [blame] | 69 | let before = strlen(c_before) |
| 70 | let c = c_before |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 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(). |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 80 | if i % 2 == 0 |
| 81 | let s_flags = 'nW' |
| 82 | let c2 = plist[i + 1] |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 83 | else |
| 84 | let s_flags = 'nbW' |
| 85 | let c2 = c |
| 86 | let c = plist[i - 1] |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 87 | endif |
| 88 | if c == '[' |
| 89 | let c = '\[' |
| 90 | let c2 = '\]' |
| 91 | endif |
| 92 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 93 | " Find the match. When it was just before the cursor move it there for a |
Bram Moolenaar | c06ac34 | 2006-03-02 22:43:39 +0000 | [diff] [blame] | 94 | " moment. |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 95 | if before > 0 |
Bram Moolenaar | 07d8779 | 2014-07-19 14:04:47 +0200 | [diff] [blame] | 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() |
| 100 | else |
| 101 | let save_cursor = winsaveview() |
| 102 | endif |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 103 | call cursor(c_lnum, c_col - before) |
| 104 | endif |
Bram Moolenaar | 25e2c9e | 2006-04-27 21:40:34 +0000 | [diff] [blame] | 105 | |
Bram Moolenaar | 3d1d647 | 2018-07-03 18:18:23 +0200 | [diff] [blame] | 106 | if !has("syntax") || !exists("g:syntax_on") |
| 107 | let s_skip = "0" |
| 108 | else |
| 109 | " Build an expression that detects whether the current cursor position is |
| 110 | " in certain syntax types (string, comment, etc.), for use as |
| 111 | " searchpairpos()'s skip argument. |
| 112 | " We match "escape" for special items, such as lispEscapeSpecial. |
| 113 | let s_skip = '!empty(filter(map(synstack(line("."), col(".")), ''synIDattr(v:val, "name")''), ' . |
Bram Moolenaar | e98cfe1 | 2014-08-06 19:09:16 +0200 | [diff] [blame] | 114 | \ '''v:val =~? "string\\|character\\|singlequote\\|escape\\|comment"''))' |
Bram Moolenaar | 3d1d647 | 2018-07-03 18:18:23 +0200 | [diff] [blame] | 115 | " If executing the expression determines that the cursor is currently in |
| 116 | " one of the syntax types, then we want searchpairpos() to find the pair |
| 117 | " within those syntax types (i.e., not skip). Otherwise, the cursor is |
| 118 | " outside of the syntax types and s_skip should keep its value so we skip |
| 119 | " any matching pair inside the syntax types. |
| 120 | " Catch if this throws E363: pattern uses more memory than 'maxmempattern'. |
| 121 | try |
| 122 | execute 'if ' . s_skip . ' | let s_skip = "0" | endif' |
| 123 | catch /^Vim\%((\a\+)\)\=:E363/ |
| 124 | " We won't find anything, so skip searching, should keep Vim responsive. |
| 125 | return |
| 126 | endtry |
| 127 | endif |
Bram Moolenaar | 25e2c9e | 2006-04-27 21:40:34 +0000 | [diff] [blame] | 128 | |
Bram Moolenaar | f2b2e70 | 2008-03-09 15:45:53 +0000 | [diff] [blame] | 129 | " Limit the search to lines visible in the window. |
| 130 | let stoplinebottom = line('w$') |
| 131 | let stoplinetop = line('w0') |
| 132 | if i % 2 == 0 |
| 133 | let stopline = stoplinebottom |
| 134 | else |
| 135 | let stopline = stoplinetop |
| 136 | endif |
| 137 | |
Bram Moolenaar | ad3b366 | 2013-05-17 18:14:19 +0200 | [diff] [blame] | 138 | " Limit the search time to 300 msec to avoid a hang on very long lines. |
| 139 | " This fails when a timeout is not supported. |
| 140 | if mode() == 'i' || mode() == 'R' |
| 141 | let timeout = exists("b:matchparen_insert_timeout") ? b:matchparen_insert_timeout : g:matchparen_insert_timeout |
| 142 | else |
| 143 | let timeout = exists("b:matchparen_timeout") ? b:matchparen_timeout : g:matchparen_timeout |
| 144 | endif |
Bram Moolenaar | 7692929 | 2008-01-06 19:07:36 +0000 | [diff] [blame] | 145 | try |
Bram Moolenaar | ad3b366 | 2013-05-17 18:14:19 +0200 | [diff] [blame] | 146 | let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, timeout) |
Bram Moolenaar | 7692929 | 2008-01-06 19:07:36 +0000 | [diff] [blame] | 147 | catch /E118/ |
Bram Moolenaar | f2b2e70 | 2008-03-09 15:45:53 +0000 | [diff] [blame] | 148 | " Can't use the timeout, restrict the stopline a bit more to avoid taking |
| 149 | " a long time on closed folds and long lines. |
| 150 | " The "viewable" variables give a range in which we can scroll while |
| 151 | " keeping the cursor at the same position. |
| 152 | " adjustedScrolloff accounts for very large numbers of scrolloff. |
| 153 | let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2]) |
| 154 | let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2]) |
| 155 | let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2]) |
| 156 | " one of these stoplines will be adjusted below, but the current values are |
| 157 | " minimal boundaries within the current window |
| 158 | if i % 2 == 0 |
| 159 | if has("byte_offset") && has("syntax_items") && &smc > 0 |
| 160 | let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2]) |
| 161 | let stopline = min([bottom_viewable, byte2line(stopbyte)]) |
| 162 | else |
| 163 | let stopline = min([bottom_viewable, c_lnum + 100]) |
| 164 | endif |
| 165 | let stoplinebottom = stopline |
| 166 | else |
| 167 | if has("byte_offset") && has("syntax_items") && &smc > 0 |
| 168 | let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2]) |
| 169 | let stopline = max([top_viewable, byte2line(stopbyte)]) |
| 170 | else |
| 171 | let stopline = max([top_viewable, c_lnum - 100]) |
| 172 | endif |
| 173 | let stoplinetop = stopline |
| 174 | endif |
Bram Moolenaar | 7692929 | 2008-01-06 19:07:36 +0000 | [diff] [blame] | 175 | let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline) |
| 176 | endtry |
Bram Moolenaar | 25e2c9e | 2006-04-27 21:40:34 +0000 | [diff] [blame] | 177 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 178 | if before > 0 |
Bram Moolenaar | 07d8779 | 2014-07-19 14:04:47 +0200 | [diff] [blame] | 179 | if has_getcurpos |
| 180 | call setpos('.', save_cursor) |
| 181 | else |
| 182 | call winrestview(save_cursor) |
| 183 | endif |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 184 | endif |
| 185 | |
| 186 | " If a match is found setup match highlighting. |
Bram Moolenaar | ef04586 | 2007-08-02 21:00:50 +0000 | [diff] [blame] | 187 | if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom |
Bram Moolenaar | b341459 | 2014-06-17 17:48:32 +0200 | [diff] [blame] | 188 | if exists('*matchaddpos') |
| 189 | call matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10, 3) |
| 190 | else |
| 191 | exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) . |
| 192 | \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/' |
| 193 | endif |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 194 | let w:paren_hl_on = 1 |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 195 | endif |
| 196 | endfunction |
| 197 | |
| 198 | " Define commands that will disable and enable the plugin. |
Bram Moolenaar | 1ff14ba | 2019-11-02 14:09:23 +0100 | [diff] [blame] | 199 | command DoMatchParen call s:DoMatchParen() |
| 200 | command NoMatchParen call s:NoMatchParen() |
Bram Moolenaar | 01164a6 | 2017-11-02 22:58:42 +0100 | [diff] [blame] | 201 | |
Bram Moolenaar | 1ff14ba | 2019-11-02 14:09:23 +0100 | [diff] [blame] | 202 | func s:NoMatchParen() |
Bram Moolenaar | 01164a6 | 2017-11-02 22:58:42 +0100 | [diff] [blame] | 203 | let w = winnr() |
| 204 | noau windo silent! call matchdelete(3) |
| 205 | unlet! g:loaded_matchparen |
| 206 | exe "noau ". w . "wincmd w" |
| 207 | au! matchparen |
| 208 | endfunc |
| 209 | |
Bram Moolenaar | 1ff14ba | 2019-11-02 14:09:23 +0100 | [diff] [blame] | 210 | func s:DoMatchParen() |
Bram Moolenaar | 01164a6 | 2017-11-02 22:58:42 +0100 | [diff] [blame] | 211 | runtime plugin/matchparen.vim |
| 212 | let w = winnr() |
| 213 | silent windo doau CursorMoved |
| 214 | exe "noau ". w . "wincmd w" |
| 215 | endfunc |
Bram Moolenaar | 3b1ddfe | 2006-03-14 22:55:34 +0000 | [diff] [blame] | 216 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 217 | let &cpo = s:cpo_save |
| 218 | unlet s:cpo_save |