blob: 2899612dce59594d7a458004fdc1d8374439b1b4 [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>
zeertzjq94043782024-05-18 14:55:49 +08003" Last Change: 2024 May 18
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
20
Christian Brabandtd3e277f2023-10-21 11:06:50 +020021let s:has_matchaddpos = exists('*matchaddpos')
22
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023augroup matchparen
24 " Replace all matchparen autocommands
zeertzjq49ffb6b2024-03-11 21:38:58 +010025 autocmd! CursorMoved,CursorMovedI,WinEnter,WinScrolled * call s:Highlight_Matching_Pair()
26 autocmd! BufWinEnter * autocmd SafeState * ++once call s:Highlight_Matching_Pair()
Bram Moolenaar28a896f2022-11-28 22:21:12 +000027 autocmd! WinLeave,BufLeave * call s:Remove_Matches()
Bram Moolenaar186628f2013-03-19 13:33:23 +010028 if exists('##TextChanged')
29 autocmd! TextChanged,TextChangedI * call s:Highlight_Matching_Pair()
Christian Brabandt95886662023-11-12 16:55:01 +010030 autocmd! TextChangedP * call s:Remove_Matches()
Bram Moolenaar186628f2013-03-19 13:33:23 +010031 endif
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
Bram Moolenaar07d87792014-07-19 14:04:47 +020097 let has_getcurpos = exists("*getcurpos")
98 if has_getcurpos
99 " getcurpos() is more efficient but doesn't exist before 7.4.313.
100 let save_cursor = getcurpos()
101 else
102 let save_cursor = winsaveview()
103 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000104 call cursor(c_lnum, c_col - before)
105 endif
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000106
Bram Moolenaar3d1d6472018-07-03 18:18:23 +0200107 if !has("syntax") || !exists("g:syntax_on")
108 let s_skip = "0"
109 else
110 " Build an expression that detects whether the current cursor position is
111 " in certain syntax types (string, comment, etc.), for use as
112 " searchpairpos()'s skip argument.
Bram Moolenaar130cbfc2021-04-07 21:07:20 +0200113 " We match "escape" for special items, such as lispEscapeSpecial, and
114 " match "symbol" for lispBarSymbol.
Bram Moolenaardd60c362023-02-27 15:49:53 +0000115 let s_skip = 'synstack(".", col("."))'
116 \ . '->indexof({_, id -> synIDattr(id, "name") =~? '
117 \ . '"string\\|character\\|singlequote\\|escape\\|symbol\\|comment"}) >= 0'
Bram Moolenaar3d1d6472018-07-03 18:18:23 +0200118 " If executing the expression determines that the cursor is currently in
119 " one of the syntax types, then we want searchpairpos() to find the pair
120 " within those syntax types (i.e., not skip). Otherwise, the cursor is
121 " outside of the syntax types and s_skip should keep its value so we skip
122 " any matching pair inside the syntax types.
123 " Catch if this throws E363: pattern uses more memory than 'maxmempattern'.
124 try
125 execute 'if ' . s_skip . ' | let s_skip = "0" | endif'
126 catch /^Vim\%((\a\+)\)\=:E363/
127 " We won't find anything, so skip searching, should keep Vim responsive.
128 return
129 endtry
130 endif
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000131
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000132 " Limit the search to lines visible in the window.
133 let stoplinebottom = line('w$')
134 let stoplinetop = line('w0')
135 if i % 2 == 0
136 let stopline = stoplinebottom
137 else
138 let stopline = stoplinetop
139 endif
140
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200141 " Limit the search time to 300 msec to avoid a hang on very long lines.
142 " This fails when a timeout is not supported.
143 if mode() == 'i' || mode() == 'R'
144 let timeout = exists("b:matchparen_insert_timeout") ? b:matchparen_insert_timeout : g:matchparen_insert_timeout
145 else
146 let timeout = exists("b:matchparen_timeout") ? b:matchparen_timeout : g:matchparen_timeout
147 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000148 try
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200149 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, timeout)
Bram Moolenaar76929292008-01-06 19:07:36 +0000150 catch /E118/
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000151 " Can't use the timeout, restrict the stopline a bit more to avoid taking
152 " a long time on closed folds and long lines.
153 " The "viewable" variables give a range in which we can scroll while
154 " keeping the cursor at the same position.
155 " adjustedScrolloff accounts for very large numbers of scrolloff.
156 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2])
157 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2])
158 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2])
159 " one of these stoplines will be adjusted below, but the current values are
160 " minimal boundaries within the current window
161 if i % 2 == 0
162 if has("byte_offset") && has("syntax_items") && &smc > 0
163 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2])
164 let stopline = min([bottom_viewable, byte2line(stopbyte)])
165 else
166 let stopline = min([bottom_viewable, c_lnum + 100])
167 endif
168 let stoplinebottom = stopline
169 else
170 if has("byte_offset") && has("syntax_items") && &smc > 0
171 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2])
172 let stopline = max([top_viewable, byte2line(stopbyte)])
173 else
174 let stopline = max([top_viewable, c_lnum - 100])
175 endif
176 let stoplinetop = stopline
177 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000178 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
179 endtry
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000180
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000181 if before > 0
Bram Moolenaar07d87792014-07-19 14:04:47 +0200182 if has_getcurpos
183 call setpos('.', save_cursor)
184 else
185 call winrestview(save_cursor)
186 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000187 endif
188
189 " If a match is found setup match highlighting.
Bram Moolenaaref045862007-08-02 21:00:50 +0000190 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom
Christian Brabandtd3e277f2023-10-21 11:06:50 +0200191 if s:has_matchaddpos
192 call add(w:matchparen_ids, matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10))
Bram Moolenaarb3414592014-06-17 17:48:32 +0200193 else
194 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
195 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
Christian Brabandtd3e277f2023-10-21 11:06:50 +0200196 call add(w:matchparen_ids, 3)
Bram Moolenaarb3414592014-06-17 17:48:32 +0200197 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000198 let w:paren_hl_on = 1
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000199 endif
200endfunction
201
Bram Moolenaar73fef332020-06-21 22:12:03 +0200202func s:Remove_Matches()
203 if exists('w:paren_hl_on') && w:paren_hl_on
Christian Brabandtd3e277f2023-10-21 11:06:50 +0200204 while !empty(w:matchparen_ids)
205 silent! call remove(w:matchparen_ids, 0)->matchdelete()
206 endwhile
Bram Moolenaar73fef332020-06-21 22:12:03 +0200207 let w:paren_hl_on = 0
208 endif
209endfunc
210
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000211" Define commands that will disable and enable the plugin.
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100212command DoMatchParen call s:DoMatchParen()
213command NoMatchParen call s:NoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100214
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100215func s:NoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100216 let w = winnr()
zeertzjq94043782024-05-18 14:55:49 +0800217 noau windo call s:Remove_Matches()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100218 unlet! g:loaded_matchparen
219 exe "noau ". w . "wincmd w"
220 au! matchparen
221endfunc
222
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100223func s:DoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100224 runtime plugin/matchparen.vim
225 let w = winnr()
226 silent windo doau CursorMoved
227 exe "noau ". w . "wincmd w"
228endfunc
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +0000229
Bram Moolenaar5c736222010-01-06 20:54:52 +0100230let &cpo = s:cpo_save
231unlet s:cpo_save