blob: 8cc865f6d2144d21f501d27296089eb88b6cc865 [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>
zeertzjq47071c62025-03-15 09:36:13 +01003" Last Change: 2025 Mar 14
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
zeertzjq47071c62025-03-15 09:36:13 +01009" - Vim has no support for :defer
10if exists("g:loaded_matchparen") || &cp ||
11 \ exists(":defer") != 2
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012 finish
13endif
14let g:loaded_matchparen = 1
15
Bram Moolenaarad3b3662013-05-17 18:14:19 +020016if !exists("g:matchparen_timeout")
17 let g:matchparen_timeout = 300
18endif
19if !exists("g:matchparen_insert_timeout")
20 let g:matchparen_insert_timeout = 60
21endif
Matteo Landi59834ba2024-11-04 20:46:54 +010022if !exists("g:matchparen_disable_cursor_hl")
23 let g:matchparen_disable_cursor_hl = 0
24endif
Bram Moolenaarad3b3662013-05-17 18:14:19 +020025
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()
zeertzjq47071c62025-03-15 09:36:13 +010031 autocmd! TextChanged,TextChangedI * call s:Highlight_Matching_Pair()
32 autocmd! TextChangedP * call s:Remove_Matches()
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000033augroup END
34
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000035" Skip the rest if it was already done.
36if exists("*s:Highlight_Matching_Pair")
37 finish
38endif
39
Bram Moolenaar5c736222010-01-06 20:54:52 +010040let s:cpo_save = &cpo
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +000041set cpo-=C
42
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000043" The function that is invoked (very often) to define a ":match" highlighting
44" for any matching paren.
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +010045func s:Highlight_Matching_Pair()
Christian Brabandtd3e277f2023-10-21 11:06:50 +020046 if !exists("w:matchparen_ids")
47 let w:matchparen_ids = []
48 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000049 " Remove any previous match.
Bram Moolenaar73fef332020-06-21 22:12:03 +020050 call s:Remove_Matches()
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000051
Bram Moolenaar36fc5352006-03-04 21:49:37 +000052 " Avoid that we remove the popup menu.
Bram Moolenaarf2b2e702008-03-09 15:45:53 +000053 " Return when there are no colors (looks like the cursor jumps).
54 if pumvisible() || (&t_Co < 8 && !has("gui_running"))
Bram Moolenaar36fc5352006-03-04 21:49:37 +000055 return
56 endif
57
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000058 " Get the character under the cursor and check if it's in 'matchpairs'.
59 let c_lnum = line('.')
60 let c_col = col('.')
61 let before = 0
62
Bram Moolenaardb6ea062014-07-10 22:01:47 +020063 let text = getline(c_lnum)
zeertzjq81e75132024-08-24 16:32:24 +020064 let c_before = text->strpart(0, c_col - 1)->slice(-1)
65 let c = text->strpart(c_col - 1)->slice(0, 1)
Bram Moolenaar41e6cd52006-09-09 11:37:51 +000066 let plist = split(&matchpairs, '.\zs[:,]')
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000067 let i = index(plist, c)
68 if i < 0
69 " not found, in Insert mode try character before the cursor
70 if c_col > 1 && (mode() == 'i' || mode() == 'R')
Bram Moolenaar256972a2015-12-29 19:10:25 +010071 let before = strlen(c_before)
72 let c = c_before
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000073 let i = index(plist, c)
74 endif
75 if i < 0
76 " not found, nothing to do
77 return
78 endif
79 endif
80
81 " Figure out the arguments for searchpairpos().
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000082 if i % 2 == 0
83 let s_flags = 'nW'
84 let c2 = plist[i + 1]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000085 else
86 let s_flags = 'nbW'
87 let c2 = c
88 let c = plist[i - 1]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000089 endif
90 if c == '['
91 let c = '\['
92 let c2 = '\]'
93 endif
94
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000095 " Find the match. When it was just before the cursor move it there for a
Bram Moolenaarc06ac342006-03-02 22:43:39 +000096 " moment.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000097 if before > 0
zeertzjq47071c62025-03-15 09:36:13 +010098 let save_cursor = getcurpos()
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000099 call cursor(c_lnum, c_col - before)
zeertzjq47071c62025-03-15 09:36:13 +0100100 defer setpos('.', save_cursor)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000101 endif
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000102
Bram Moolenaar3d1d6472018-07-03 18:18:23 +0200103 if !has("syntax") || !exists("g:syntax_on")
104 let s_skip = "0"
105 else
Christian Brabandt9102ac12025-03-09 08:40:33 +0100106 " do not attempt to match when the syntax item where the cursor is
107 " indicates there does not exist a matching parenthesis, e.g. for shells
108 " case statement: "case $var in foobar)"
109 "
110 " add the check behind a filetype check, so it only needs to be
111 " evaluated for certain filetypes
112 if ['sh']->index(&filetype) >= 0 &&
113 \ synstack(".", col("."))->indexof({_, id -> synIDattr(id, "name")
114 \ =~? "shSnglCase"}) >= 0
115 return
116 endif
Bram Moolenaar3d1d6472018-07-03 18:18:23 +0200117 " Build an expression that detects whether the current cursor position is
118 " in certain syntax types (string, comment, etc.), for use as
119 " searchpairpos()'s skip argument.
Bram Moolenaar130cbfc2021-04-07 21:07:20 +0200120 " We match "escape" for special items, such as lispEscapeSpecial, and
121 " match "symbol" for lispBarSymbol.
Bram Moolenaardd60c362023-02-27 15:49:53 +0000122 let s_skip = 'synstack(".", col("."))'
123 \ . '->indexof({_, id -> synIDattr(id, "name") =~? '
124 \ . '"string\\|character\\|singlequote\\|escape\\|symbol\\|comment"}) >= 0'
Bram Moolenaar3d1d6472018-07-03 18:18:23 +0200125 " If executing the expression determines that the cursor is currently in
126 " one of the syntax types, then we want searchpairpos() to find the pair
127 " within those syntax types (i.e., not skip). Otherwise, the cursor is
128 " outside of the syntax types and s_skip should keep its value so we skip
129 " any matching pair inside the syntax types.
130 " Catch if this throws E363: pattern uses more memory than 'maxmempattern'.
131 try
132 execute 'if ' . s_skip . ' | let s_skip = "0" | endif'
133 catch /^Vim\%((\a\+)\)\=:E363/
134 " We won't find anything, so skip searching, should keep Vim responsive.
135 return
136 endtry
137 endif
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000138
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000139 " Limit the search to lines visible in the window.
140 let stoplinebottom = line('w$')
141 let stoplinetop = line('w0')
142 if i % 2 == 0
143 let stopline = stoplinebottom
144 else
145 let stopline = stoplinetop
146 endif
147
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200148 " Limit the search time to 300 msec to avoid a hang on very long lines.
149 " This fails when a timeout is not supported.
150 if mode() == 'i' || mode() == 'R'
151 let timeout = exists("b:matchparen_insert_timeout") ? b:matchparen_insert_timeout : g:matchparen_insert_timeout
152 else
153 let timeout = exists("b:matchparen_timeout") ? b:matchparen_timeout : g:matchparen_timeout
154 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000155 try
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200156 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, timeout)
Bram Moolenaar76929292008-01-06 19:07:36 +0000157 catch /E118/
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000158 " Can't use the timeout, restrict the stopline a bit more to avoid taking
159 " a long time on closed folds and long lines.
160 " The "viewable" variables give a range in which we can scroll while
161 " keeping the cursor at the same position.
162 " adjustedScrolloff accounts for very large numbers of scrolloff.
163 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2])
164 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2])
165 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2])
166 " one of these stoplines will be adjusted below, but the current values are
167 " minimal boundaries within the current window
168 if i % 2 == 0
169 if has("byte_offset") && has("syntax_items") && &smc > 0
170 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2])
171 let stopline = min([bottom_viewable, byte2line(stopbyte)])
172 else
173 let stopline = min([bottom_viewable, c_lnum + 100])
174 endif
175 let stoplinebottom = stopline
176 else
177 if has("byte_offset") && has("syntax_items") && &smc > 0
178 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2])
179 let stopline = max([top_viewable, byte2line(stopbyte)])
180 else
181 let stopline = max([top_viewable, c_lnum - 100])
182 endif
183 let stoplinetop = stopline
184 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000185 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
186 endtry
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000187
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000188 " If a match is found setup match highlighting.
zeertzjq47071c62025-03-15 09:36:13 +0100189 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom
190 if !g:matchparen_disable_cursor_hl
191 call add(w:matchparen_ids, matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10))
Bram Moolenaarb3414592014-06-17 17:48:32 +0200192 else
zeertzjq47071c62025-03-15 09:36:13 +0100193 call add(w:matchparen_ids, matchaddpos('MatchParen', [[m_lnum, m_col]], 10))
Bram Moolenaarb3414592014-06-17 17:48:32 +0200194 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000195 let w:paren_hl_on = 1
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000196 endif
197endfunction
198
Bram Moolenaar73fef332020-06-21 22:12:03 +0200199func s:Remove_Matches()
200 if exists('w:paren_hl_on') && w:paren_hl_on
Christian Brabandtd3e277f2023-10-21 11:06:50 +0200201 while !empty(w:matchparen_ids)
202 silent! call remove(w:matchparen_ids, 0)->matchdelete()
203 endwhile
Bram Moolenaar73fef332020-06-21 22:12:03 +0200204 let w:paren_hl_on = 0
205 endif
206endfunc
207
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000208" Define commands that will disable and enable the plugin.
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100209command DoMatchParen call s:DoMatchParen()
210command NoMatchParen call s:NoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100211
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100212func s:NoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100213 let w = winnr()
zeertzjq94043782024-05-18 14:55:49 +0800214 noau windo call s:Remove_Matches()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100215 unlet! g:loaded_matchparen
216 exe "noau ". w . "wincmd w"
217 au! matchparen
218endfunc
219
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100220func s:DoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100221 runtime plugin/matchparen.vim
222 let w = winnr()
223 silent windo doau CursorMoved
224 exe "noau ". w . "wincmd w"
225endfunc
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +0000226
Bram Moolenaar5c736222010-01-06 20:54:52 +0100227let &cpo = s:cpo_save
228unlet s:cpo_save