blob: 4235a0d39b23943c244338a69f8404a9541b7195 [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 Brabandtd3e277f2023-10-21 11:06:50 +02003" Last Change: 2023 Oct 20
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
Bram Moolenaar28a896f2022-11-28 22:21:12 +000025 autocmd! CursorMoved,CursorMovedI,WinEnter,BufWinEnter,WinScrolled * call s:Highlight_Matching_Pair()
26 autocmd! WinLeave,BufLeave * call s:Remove_Matches()
Bram Moolenaar186628f2013-03-19 13:33:23 +010027 if exists('##TextChanged')
28 autocmd! TextChanged,TextChangedI * call s:Highlight_Matching_Pair()
Christian Brabandt95886662023-11-12 16:55:01 +010029 autocmd! TextChangedP * call s:Remove_Matches()
Bram Moolenaar186628f2013-03-19 13:33:23 +010030 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000031augroup END
32
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000033" Skip the rest if it was already done.
34if exists("*s:Highlight_Matching_Pair")
35 finish
36endif
37
Bram Moolenaar5c736222010-01-06 20:54:52 +010038let s:cpo_save = &cpo
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +000039set cpo-=C
40
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000041" The function that is invoked (very often) to define a ":match" highlighting
42" for any matching paren.
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +010043func s:Highlight_Matching_Pair()
Christian Brabandtd3e277f2023-10-21 11:06:50 +020044 if !exists("w:matchparen_ids")
45 let w:matchparen_ids = []
46 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000047 " Remove any previous match.
Bram Moolenaar73fef332020-06-21 22:12:03 +020048 call s:Remove_Matches()
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000049
Bram Moolenaar36fc5352006-03-04 21:49:37 +000050 " Avoid that we remove the popup menu.
Bram Moolenaarf2b2e702008-03-09 15:45:53 +000051 " Return when there are no colors (looks like the cursor jumps).
52 if pumvisible() || (&t_Co < 8 && !has("gui_running"))
Bram Moolenaar36fc5352006-03-04 21:49:37 +000053 return
54 endif
55
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000056 " Get the character under the cursor and check if it's in 'matchpairs'.
57 let c_lnum = line('.')
58 let c_col = col('.')
59 let before = 0
60
Bram Moolenaardb6ea062014-07-10 22:01:47 +020061 let text = getline(c_lnum)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010062 let matches = matchlist(text, '\(.\)\=\%'.c_col.'c\(.\=\)')
Bram Moolenaar256972a2015-12-29 19:10:25 +010063 if empty(matches)
64 let [c_before, c] = ['', '']
65 else
66 let [c_before, c] = matches[1:2]
67 endif
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
113 " Build an expression that detects whether the current cursor position is
114 " in certain syntax types (string, comment, etc.), for use as
115 " searchpairpos()'s skip argument.
Bram Moolenaar130cbfc2021-04-07 21:07:20 +0200116 " We match "escape" for special items, such as lispEscapeSpecial, and
117 " match "symbol" for lispBarSymbol.
Bram Moolenaardd60c362023-02-27 15:49:53 +0000118 let s_skip = 'synstack(".", col("."))'
119 \ . '->indexof({_, id -> synIDattr(id, "name") =~? '
120 \ . '"string\\|character\\|singlequote\\|escape\\|symbol\\|comment"}) >= 0'
Bram Moolenaar3d1d6472018-07-03 18:18:23 +0200121 " If executing the expression determines that the cursor is currently in
122 " one of the syntax types, then we want searchpairpos() to find the pair
123 " within those syntax types (i.e., not skip). Otherwise, the cursor is
124 " outside of the syntax types and s_skip should keep its value so we skip
125 " any matching pair inside the syntax types.
126 " Catch if this throws E363: pattern uses more memory than 'maxmempattern'.
127 try
128 execute 'if ' . s_skip . ' | let s_skip = "0" | endif'
129 catch /^Vim\%((\a\+)\)\=:E363/
130 " We won't find anything, so skip searching, should keep Vim responsive.
131 return
132 endtry
133 endif
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000134
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000135 " Limit the search to lines visible in the window.
136 let stoplinebottom = line('w$')
137 let stoplinetop = line('w0')
138 if i % 2 == 0
139 let stopline = stoplinebottom
140 else
141 let stopline = stoplinetop
142 endif
143
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200144 " Limit the search time to 300 msec to avoid a hang on very long lines.
145 " This fails when a timeout is not supported.
146 if mode() == 'i' || mode() == 'R'
147 let timeout = exists("b:matchparen_insert_timeout") ? b:matchparen_insert_timeout : g:matchparen_insert_timeout
148 else
149 let timeout = exists("b:matchparen_timeout") ? b:matchparen_timeout : g:matchparen_timeout
150 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000151 try
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200152 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, timeout)
Bram Moolenaar76929292008-01-06 19:07:36 +0000153 catch /E118/
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000154 " Can't use the timeout, restrict the stopline a bit more to avoid taking
155 " a long time on closed folds and long lines.
156 " The "viewable" variables give a range in which we can scroll while
157 " keeping the cursor at the same position.
158 " adjustedScrolloff accounts for very large numbers of scrolloff.
159 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2])
160 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2])
161 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2])
162 " one of these stoplines will be adjusted below, but the current values are
163 " minimal boundaries within the current window
164 if i % 2 == 0
165 if has("byte_offset") && has("syntax_items") && &smc > 0
166 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2])
167 let stopline = min([bottom_viewable, byte2line(stopbyte)])
168 else
169 let stopline = min([bottom_viewable, c_lnum + 100])
170 endif
171 let stoplinebottom = stopline
172 else
173 if has("byte_offset") && has("syntax_items") && &smc > 0
174 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2])
175 let stopline = max([top_viewable, byte2line(stopbyte)])
176 else
177 let stopline = max([top_viewable, c_lnum - 100])
178 endif
179 let stoplinetop = stopline
180 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000181 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
182 endtry
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000183
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000184 if before > 0
Bram Moolenaar07d87792014-07-19 14:04:47 +0200185 if has_getcurpos
186 call setpos('.', save_cursor)
187 else
188 call winrestview(save_cursor)
189 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000190 endif
191
192 " If a match is found setup match highlighting.
Bram Moolenaaref045862007-08-02 21:00:50 +0000193 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom
Christian Brabandtd3e277f2023-10-21 11:06:50 +0200194 if s:has_matchaddpos
195 call add(w:matchparen_ids, matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10))
Bram Moolenaarb3414592014-06-17 17:48:32 +0200196 else
197 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
198 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
Christian Brabandtd3e277f2023-10-21 11:06:50 +0200199 call add(w:matchparen_ids, 3)
Bram Moolenaarb3414592014-06-17 17:48:32 +0200200 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000201 let w:paren_hl_on = 1
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000202 endif
203endfunction
204
Bram Moolenaar73fef332020-06-21 22:12:03 +0200205func s:Remove_Matches()
206 if exists('w:paren_hl_on') && w:paren_hl_on
Christian Brabandtd3e277f2023-10-21 11:06:50 +0200207 while !empty(w:matchparen_ids)
208 silent! call remove(w:matchparen_ids, 0)->matchdelete()
209 endwhile
Bram Moolenaar73fef332020-06-21 22:12:03 +0200210 let w:paren_hl_on = 0
211 endif
212endfunc
213
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000214" Define commands that will disable and enable the plugin.
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100215command DoMatchParen call s:DoMatchParen()
216command NoMatchParen call s:NoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100217
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100218func s:NoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100219 let w = winnr()
220 noau windo silent! call matchdelete(3)
221 unlet! g:loaded_matchparen
222 exe "noau ". w . "wincmd w"
223 au! matchparen
224endfunc
225
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100226func s:DoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100227 runtime plugin/matchparen.vim
228 let w = winnr()
229 silent windo doau CursorMoved
230 exe "noau ". w . "wincmd w"
231endfunc
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +0000232
Bram Moolenaar5c736222010-01-06 20:54:52 +0100233let &cpo = s:cpo_save
234unlet s:cpo_save