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