blob: 2fa30b12ecda5c97466d3ddba805a3bda3f52208 [file] [log] [blame]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001" Vim plugin for showing matching parens
Christian Brabandte978b452023-08-13 10:33:05 +02002" Maintainer: The Vim Project <https://github.com/vim/vim>
Christian Brabandt9102ac12025-03-09 08:40:33 +01003" Last Change: 2025 Mar 08
Christian Brabandte978b452023-08-13 10:33:05 +02004" Former Maintainer: Bram Moolenaar <Bram@vim.org>
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005
6" Exit quickly when:
7" - this plugin was already loaded (or disabled)
8" - when 'compatible' is set
Bram Moolenaarfd999452022-08-24 18:30:14 +01009if exists("g:loaded_matchparen") || &cp
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010 finish
11endif
12let g:loaded_matchparen = 1
13
Bram Moolenaarad3b3662013-05-17 18:14:19 +020014if !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
Matteo Landi59834ba2024-11-04 20:46:54 +010020if !exists("g:matchparen_disable_cursor_hl")
21 let g:matchparen_disable_cursor_hl = 0
22endif
Bram Moolenaarad3b3662013-05-17 18:14:19 +020023
Christian Brabandtd3e277f2023-10-21 11:06:50 +020024let s:has_matchaddpos = exists('*matchaddpos')
25
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026augroup matchparen
27 " Replace all matchparen autocommands
zeertzjq49ffb6b2024-03-11 21:38:58 +010028 autocmd! CursorMoved,CursorMovedI,WinEnter,WinScrolled * call s:Highlight_Matching_Pair()
29 autocmd! BufWinEnter * autocmd SafeState * ++once call s:Highlight_Matching_Pair()
Bram Moolenaar28a896f2022-11-28 22:21:12 +000030 autocmd! WinLeave,BufLeave * call s:Remove_Matches()
Bram Moolenaar186628f2013-03-19 13:33:23 +010031 if exists('##TextChanged')
32 autocmd! TextChanged,TextChangedI * call s:Highlight_Matching_Pair()
Christian Brabandt95886662023-11-12 16:55:01 +010033 autocmd! TextChangedP * call s:Remove_Matches()
Bram Moolenaar186628f2013-03-19 13:33:23 +010034 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000035augroup END
36
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000037" Skip the rest if it was already done.
38if exists("*s:Highlight_Matching_Pair")
39 finish
40endif
41
Bram Moolenaar5c736222010-01-06 20:54:52 +010042let s:cpo_save = &cpo
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +000043set cpo-=C
44
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000045" The function that is invoked (very often) to define a ":match" highlighting
46" for any matching paren.
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +010047func s:Highlight_Matching_Pair()
Christian Brabandtd3e277f2023-10-21 11:06:50 +020048 if !exists("w:matchparen_ids")
49 let w:matchparen_ids = []
50 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000051 " Remove any previous match.
Bram Moolenaar73fef332020-06-21 22:12:03 +020052 call s:Remove_Matches()
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000053
Bram Moolenaar36fc5352006-03-04 21:49:37 +000054 " Avoid that we remove the popup menu.
Bram Moolenaarf2b2e702008-03-09 15:45:53 +000055 " Return when there are no colors (looks like the cursor jumps).
56 if pumvisible() || (&t_Co < 8 && !has("gui_running"))
Bram Moolenaar36fc5352006-03-04 21:49:37 +000057 return
58 endif
59
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000060 " Get the character under the cursor and check if it's in 'matchpairs'.
61 let c_lnum = line('.')
62 let c_col = col('.')
63 let before = 0
64
Bram Moolenaardb6ea062014-07-10 22:01:47 +020065 let text = getline(c_lnum)
zeertzjq81e75132024-08-24 16:32:24 +020066 let c_before = text->strpart(0, c_col - 1)->slice(-1)
67 let c = text->strpart(c_col - 1)->slice(0, 1)
Bram Moolenaar41e6cd52006-09-09 11:37:51 +000068 let plist = split(&matchpairs, '.\zs[:,]')
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000069 let i = index(plist, c)
70 if i < 0
71 " not found, in Insert mode try character before the cursor
72 if c_col > 1 && (mode() == 'i' || mode() == 'R')
Bram Moolenaar256972a2015-12-29 19:10:25 +010073 let before = strlen(c_before)
74 let c = c_before
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000075 let i = index(plist, c)
76 endif
77 if i < 0
78 " not found, nothing to do
79 return
80 endif
81 endif
82
83 " Figure out the arguments for searchpairpos().
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000084 if i % 2 == 0
85 let s_flags = 'nW'
86 let c2 = plist[i + 1]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000087 else
88 let s_flags = 'nbW'
89 let c2 = c
90 let c = plist[i - 1]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000091 endif
92 if c == '['
93 let c = '\['
94 let c2 = '\]'
95 endif
96
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000097 " Find the match. When it was just before the cursor move it there for a
Bram Moolenaarc06ac342006-03-02 22:43:39 +000098 " moment.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000099 if before > 0
Bram Moolenaar07d87792014-07-19 14:04:47 +0200100 let has_getcurpos = exists("*getcurpos")
101 if has_getcurpos
102 " getcurpos() is more efficient but doesn't exist before 7.4.313.
103 let save_cursor = getcurpos()
104 else
105 let save_cursor = winsaveview()
106 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000107 call cursor(c_lnum, c_col - before)
108 endif
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000109
Bram Moolenaar3d1d6472018-07-03 18:18:23 +0200110 if !has("syntax") || !exists("g:syntax_on")
111 let s_skip = "0"
112 else
Christian Brabandt9102ac12025-03-09 08:40:33 +0100113 " do not attempt to match when the syntax item where the cursor is
114 " indicates there does not exist a matching parenthesis, e.g. for shells
115 " case statement: "case $var in foobar)"
116 "
117 " add the check behind a filetype check, so it only needs to be
118 " evaluated for certain filetypes
119 if ['sh']->index(&filetype) >= 0 &&
120 \ synstack(".", col("."))->indexof({_, id -> synIDattr(id, "name")
121 \ =~? "shSnglCase"}) >= 0
122 return
123 endif
Bram Moolenaar3d1d6472018-07-03 18:18:23 +0200124 " Build an expression that detects whether the current cursor position is
125 " in certain syntax types (string, comment, etc.), for use as
126 " searchpairpos()'s skip argument.
Bram Moolenaar130cbfc2021-04-07 21:07:20 +0200127 " We match "escape" for special items, such as lispEscapeSpecial, and
128 " match "symbol" for lispBarSymbol.
Bram Moolenaardd60c362023-02-27 15:49:53 +0000129 let s_skip = 'synstack(".", col("."))'
130 \ . '->indexof({_, id -> synIDattr(id, "name") =~? '
131 \ . '"string\\|character\\|singlequote\\|escape\\|symbol\\|comment"}) >= 0'
Bram Moolenaar3d1d6472018-07-03 18:18:23 +0200132 " If executing the expression determines that the cursor is currently in
133 " one of the syntax types, then we want searchpairpos() to find the pair
134 " within those syntax types (i.e., not skip). Otherwise, the cursor is
135 " outside of the syntax types and s_skip should keep its value so we skip
136 " any matching pair inside the syntax types.
137 " Catch if this throws E363: pattern uses more memory than 'maxmempattern'.
138 try
139 execute 'if ' . s_skip . ' | let s_skip = "0" | endif'
140 catch /^Vim\%((\a\+)\)\=:E363/
141 " We won't find anything, so skip searching, should keep Vim responsive.
142 return
143 endtry
144 endif
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000145
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000146 " Limit the search to lines visible in the window.
147 let stoplinebottom = line('w$')
148 let stoplinetop = line('w0')
149 if i % 2 == 0
150 let stopline = stoplinebottom
151 else
152 let stopline = stoplinetop
153 endif
154
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200155 " Limit the search time to 300 msec to avoid a hang on very long lines.
156 " This fails when a timeout is not supported.
157 if mode() == 'i' || mode() == 'R'
158 let timeout = exists("b:matchparen_insert_timeout") ? b:matchparen_insert_timeout : g:matchparen_insert_timeout
159 else
160 let timeout = exists("b:matchparen_timeout") ? b:matchparen_timeout : g:matchparen_timeout
161 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000162 try
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200163 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, timeout)
Bram Moolenaar76929292008-01-06 19:07:36 +0000164 catch /E118/
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000165 " Can't use the timeout, restrict the stopline a bit more to avoid taking
166 " a long time on closed folds and long lines.
167 " The "viewable" variables give a range in which we can scroll while
168 " keeping the cursor at the same position.
169 " adjustedScrolloff accounts for very large numbers of scrolloff.
170 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2])
171 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2])
172 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2])
173 " one of these stoplines will be adjusted below, but the current values are
174 " minimal boundaries within the current window
175 if i % 2 == 0
176 if has("byte_offset") && has("syntax_items") && &smc > 0
177 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2])
178 let stopline = min([bottom_viewable, byte2line(stopbyte)])
179 else
180 let stopline = min([bottom_viewable, c_lnum + 100])
181 endif
182 let stoplinebottom = stopline
183 else
184 if has("byte_offset") && has("syntax_items") && &smc > 0
185 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2])
186 let stopline = max([top_viewable, byte2line(stopbyte)])
187 else
188 let stopline = max([top_viewable, c_lnum - 100])
189 endif
190 let stoplinetop = stopline
191 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000192 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
193 endtry
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000194
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000195 if before > 0
Bram Moolenaar07d87792014-07-19 14:04:47 +0200196 if has_getcurpos
197 call setpos('.', save_cursor)
198 else
199 call winrestview(save_cursor)
200 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000201 endif
202
203 " If a match is found setup match highlighting.
Bram Moolenaaref045862007-08-02 21:00:50 +0000204 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom
Christian Brabandtd3e277f2023-10-21 11:06:50 +0200205 if s:has_matchaddpos
Matteo Landi59834ba2024-11-04 20:46:54 +0100206 if !g:matchparen_disable_cursor_hl
207 call add(w:matchparen_ids, matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10))
208 else
209 call add(w:matchparen_ids, matchaddpos('MatchParen', [[m_lnum, m_col]], 10))
210 endif
Bram Moolenaarb3414592014-06-17 17:48:32 +0200211 else
Matteo Landi59834ba2024-11-04 20:46:54 +0100212 if !g:matchparen_disable_cursor_hl
213 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
214 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
215 else
216 exe '3match MatchParen /\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
217 endif
Christian Brabandtd3e277f2023-10-21 11:06:50 +0200218 call add(w:matchparen_ids, 3)
Bram Moolenaarb3414592014-06-17 17:48:32 +0200219 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000220 let w:paren_hl_on = 1
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000221 endif
222endfunction
223
Bram Moolenaar73fef332020-06-21 22:12:03 +0200224func s:Remove_Matches()
225 if exists('w:paren_hl_on') && w:paren_hl_on
Christian Brabandtd3e277f2023-10-21 11:06:50 +0200226 while !empty(w:matchparen_ids)
227 silent! call remove(w:matchparen_ids, 0)->matchdelete()
228 endwhile
Bram Moolenaar73fef332020-06-21 22:12:03 +0200229 let w:paren_hl_on = 0
230 endif
231endfunc
232
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000233" Define commands that will disable and enable the plugin.
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100234command DoMatchParen call s:DoMatchParen()
235command NoMatchParen call s:NoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100236
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100237func s:NoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100238 let w = winnr()
zeertzjq94043782024-05-18 14:55:49 +0800239 noau windo call s:Remove_Matches()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100240 unlet! g:loaded_matchparen
241 exe "noau ". w . "wincmd w"
242 au! matchparen
243endfunc
244
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100245func s:DoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100246 runtime plugin/matchparen.vim
247 let w = winnr()
248 silent windo doau CursorMoved
249 exe "noau ". w . "wincmd w"
250endfunc
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +0000251
Bram Moolenaar5c736222010-01-06 20:54:52 +0100252let &cpo = s:cpo_save
253unlet s:cpo_save