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