blob: 562148c19ed211087bbf34065a89a5f2234c7535 [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 Brabandt96a0b2a2025-04-09 19:29:18 +02003" Last Change: 2025 Apr 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
zeertzjq47071c62025-03-15 09:36:13 +01009" - Vim has no support for :defer
Christian Brabandt13dcea22025-03-15 10:24:11 +010010if exists("g:loaded_matchparen") || &cp || exists(":defer") != 2
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011 finish
12endif
13let g:loaded_matchparen = 1
14
Bram Moolenaarad3b3662013-05-17 18:14:19 +020015if !exists("g:matchparen_timeout")
16 let g:matchparen_timeout = 300
17endif
18if !exists("g:matchparen_insert_timeout")
19 let g:matchparen_insert_timeout = 60
20endif
Matteo Landi59834ba2024-11-04 20:46:54 +010021if !exists("g:matchparen_disable_cursor_hl")
22 let g:matchparen_disable_cursor_hl = 0
23endif
Bram Moolenaarad3b3662013-05-17 18:14:19 +020024
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025augroup matchparen
26 " Replace all matchparen autocommands
zeertzjq49ffb6b2024-03-11 21:38:58 +010027 autocmd! CursorMoved,CursorMovedI,WinEnter,WinScrolled * call s:Highlight_Matching_Pair()
28 autocmd! BufWinEnter * autocmd SafeState * ++once call s:Highlight_Matching_Pair()
Bram Moolenaar28a896f2022-11-28 22:21:12 +000029 autocmd! WinLeave,BufLeave * call s:Remove_Matches()
zeertzjq47071c62025-03-15 09:36:13 +010030 autocmd! TextChanged,TextChangedI * call s:Highlight_Matching_Pair()
31 autocmd! TextChangedP * call s:Remove_Matches()
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000032augroup END
33
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000034" Skip the rest if it was already done.
35if exists("*s:Highlight_Matching_Pair")
36 finish
37endif
38
Bram Moolenaar5c736222010-01-06 20:54:52 +010039let s:cpo_save = &cpo
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +000040set cpo-=C
41
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000042" The function that is invoked (very often) to define a ":match" highlighting
43" for any matching paren.
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +010044func s:Highlight_Matching_Pair()
Christian Brabandtd3e277f2023-10-21 11:06:50 +020045 if !exists("w:matchparen_ids")
46 let w:matchparen_ids = []
47 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000048 " Remove any previous match.
Bram Moolenaar73fef332020-06-21 22:12:03 +020049 call s:Remove_Matches()
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000050
Bram Moolenaar36fc5352006-03-04 21:49:37 +000051 " Avoid that we remove the popup menu.
Bram Moolenaarf2b2e702008-03-09 15:45:53 +000052 " Return when there are no colors (looks like the cursor jumps).
53 if pumvisible() || (&t_Co < 8 && !has("gui_running"))
Bram Moolenaar36fc5352006-03-04 21:49:37 +000054 return
55 endif
56
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000057 " 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 Moolenaardb6ea062014-07-10 22:01:47 +020062 let text = getline(c_lnum)
zeertzjq81e75132024-08-24 16:32:24 +020063 let c_before = text->strpart(0, c_col - 1)->slice(-1)
64 let c = text->strpart(c_col - 1)->slice(0, 1)
Bram Moolenaar41e6cd52006-09-09 11:37:51 +000065 let plist = split(&matchpairs, '.\zs[:,]')
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000066 let i = index(plist, c)
67 if i < 0
68 " not found, in Insert mode try character before the cursor
69 if c_col > 1 && (mode() == 'i' || mode() == 'R')
Bram Moolenaar256972a2015-12-29 19:10:25 +010070 let before = strlen(c_before)
71 let c = c_before
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000072 let i = index(plist, c)
73 endif
74 if i < 0
75 " not found, nothing to do
76 return
77 endif
78 endif
79
80 " Figure out the arguments for searchpairpos().
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000081 if i % 2 == 0
82 let s_flags = 'nW'
83 let c2 = plist[i + 1]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000084 else
85 let s_flags = 'nbW'
86 let c2 = c
87 let c = plist[i - 1]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000088 endif
89 if c == '['
90 let c = '\['
91 let c2 = '\]'
92 endif
93
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000094 " Find the match. When it was just before the cursor move it there for a
Bram Moolenaarc06ac342006-03-02 22:43:39 +000095 " moment.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000096 if before > 0
zeertzjq47071c62025-03-15 09:36:13 +010097 let save_cursor = getcurpos()
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000098 call cursor(c_lnum, c_col - before)
zeertzjq47071c62025-03-15 09:36:13 +010099 defer setpos('.', save_cursor)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000100 endif
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000101
Bram Moolenaar3d1d6472018-07-03 18:18:23 +0200102 if !has("syntax") || !exists("g:syntax_on")
103 let s_skip = "0"
104 else
Christian Brabandt9102ac12025-03-09 08:40:33 +0100105 " do not attempt to match when the syntax item where the cursor is
106 " indicates there does not exist a matching parenthesis, e.g. for shells
107 " case statement: "case $var in foobar)"
108 "
109 " add the check behind a filetype check, so it only needs to be
110 " evaluated for certain filetypes
111 if ['sh']->index(&filetype) >= 0 &&
112 \ synstack(".", col("."))->indexof({_, id -> synIDattr(id, "name")
113 \ =~? "shSnglCase"}) >= 0
114 return
115 endif
Bram Moolenaar3d1d6472018-07-03 18:18:23 +0200116 " Build an expression that detects whether the current cursor position is
117 " in certain syntax types (string, comment, etc.), for use as
118 " searchpairpos()'s skip argument.
Bram Moolenaar130cbfc2021-04-07 21:07:20 +0200119 " We match "escape" for special items, such as lispEscapeSpecial, and
120 " match "symbol" for lispBarSymbol.
Bram Moolenaardd60c362023-02-27 15:49:53 +0000121 let s_skip = 'synstack(".", col("."))'
122 \ . '->indexof({_, id -> synIDattr(id, "name") =~? '
123 \ . '"string\\|character\\|singlequote\\|escape\\|symbol\\|comment"}) >= 0'
Bram Moolenaar3d1d6472018-07-03 18:18:23 +0200124 " If executing the expression determines that the cursor is currently in
125 " one of the syntax types, then we want searchpairpos() to find the pair
126 " within those syntax types (i.e., not skip). Otherwise, the cursor is
127 " outside of the syntax types and s_skip should keep its value so we skip
128 " any matching pair inside the syntax types.
129 " Catch if this throws E363: pattern uses more memory than 'maxmempattern'.
130 try
131 execute 'if ' . s_skip . ' | let s_skip = "0" | endif'
132 catch /^Vim\%((\a\+)\)\=:E363/
133 " We won't find anything, so skip searching, should keep Vim responsive.
134 return
135 endtry
136 endif
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000137
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000138 " Limit the search to lines visible in the window.
139 let stoplinebottom = line('w$')
140 let stoplinetop = line('w0')
141 if i % 2 == 0
142 let stopline = stoplinebottom
143 else
144 let stopline = stoplinetop
145 endif
146
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200147 " Limit the search time to 300 msec to avoid a hang on very long lines.
148 " This fails when a timeout is not supported.
149 if mode() == 'i' || mode() == 'R'
150 let timeout = exists("b:matchparen_insert_timeout") ? b:matchparen_insert_timeout : g:matchparen_insert_timeout
151 else
152 let timeout = exists("b:matchparen_timeout") ? b:matchparen_timeout : g:matchparen_timeout
153 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000154 try
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200155 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, timeout)
Bram Moolenaar76929292008-01-06 19:07:36 +0000156 catch /E118/
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000157 " Can't use the timeout, restrict the stopline a bit more to avoid taking
158 " a long time on closed folds and long lines.
159 " The "viewable" variables give a range in which we can scroll while
160 " keeping the cursor at the same position.
161 " adjustedScrolloff accounts for very large numbers of scrolloff.
162 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2])
163 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2])
164 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2])
165 " one of these stoplines will be adjusted below, but the current values are
166 " minimal boundaries within the current window
167 if i % 2 == 0
168 if has("byte_offset") && has("syntax_items") && &smc > 0
169 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2])
170 let stopline = min([bottom_viewable, byte2line(stopbyte)])
171 else
172 let stopline = min([bottom_viewable, c_lnum + 100])
173 endif
174 let stoplinebottom = stopline
175 else
176 if has("byte_offset") && has("syntax_items") && &smc > 0
177 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2])
178 let stopline = max([top_viewable, byte2line(stopbyte)])
179 else
180 let stopline = max([top_viewable, c_lnum - 100])
181 endif
182 let stoplinetop = stopline
183 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000184 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
185 endtry
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000186
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000187 " If a match is found setup match highlighting.
zeertzjq47071c62025-03-15 09:36:13 +0100188 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom
189 if !g:matchparen_disable_cursor_hl
190 call add(w:matchparen_ids, matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10))
Bram Moolenaarb3414592014-06-17 17:48:32 +0200191 else
zeertzjq47071c62025-03-15 09:36:13 +0100192 call add(w:matchparen_ids, matchaddpos('MatchParen', [[m_lnum, m_col]], 10))
Bram Moolenaarb3414592014-06-17 17:48:32 +0200193 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000194 let w:paren_hl_on = 1
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000195 endif
196endfunction
197
Bram Moolenaar73fef332020-06-21 22:12:03 +0200198func s:Remove_Matches()
199 if exists('w:paren_hl_on') && w:paren_hl_on
Christian Brabandtd3e277f2023-10-21 11:06:50 +0200200 while !empty(w:matchparen_ids)
201 silent! call remove(w:matchparen_ids, 0)->matchdelete()
202 endwhile
Bram Moolenaar73fef332020-06-21 22:12:03 +0200203 let w:paren_hl_on = 0
204 endif
205endfunc
206
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000207" Define commands that will disable and enable the plugin.
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100208command DoMatchParen call s:DoMatchParen()
209command NoMatchParen call s:NoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100210
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100211func s:NoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100212 let w = winnr()
zeertzjq94043782024-05-18 14:55:49 +0800213 noau windo call s:Remove_Matches()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100214 unlet! g:loaded_matchparen
215 exe "noau ". w . "wincmd w"
216 au! matchparen
217endfunc
218
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100219func s:DoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100220 runtime plugin/matchparen.vim
221 let w = winnr()
222 silent windo doau CursorMoved
223 exe "noau ". w . "wincmd w"
224endfunc
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +0000225
Bram Moolenaar5c736222010-01-06 20:54:52 +0100226let &cpo = s:cpo_save
227unlet s:cpo_save