blob: 9d57545ee8c7c38028d6410d4a0e539f61f55307 [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()
29 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000030augroup END
31
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000032" Skip the rest if it was already done.
33if exists("*s:Highlight_Matching_Pair")
34 finish
35endif
36
Bram Moolenaar5c736222010-01-06 20:54:52 +010037let s:cpo_save = &cpo
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +000038set cpo-=C
39
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000040" The function that is invoked (very often) to define a ":match" highlighting
41" for any matching paren.
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +010042func s:Highlight_Matching_Pair()
Christian Brabandtd3e277f2023-10-21 11:06:50 +020043 if !exists("w:matchparen_ids")
44 let w:matchparen_ids = []
45 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000046 " Remove any previous match.
Bram Moolenaar73fef332020-06-21 22:12:03 +020047 call s:Remove_Matches()
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000048
Bram Moolenaar36fc5352006-03-04 21:49:37 +000049 " Avoid that we remove the popup menu.
Bram Moolenaarf2b2e702008-03-09 15:45:53 +000050 " Return when there are no colors (looks like the cursor jumps).
51 if pumvisible() || (&t_Co < 8 && !has("gui_running"))
Bram Moolenaar36fc5352006-03-04 21:49:37 +000052 return
53 endif
54
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000055 " Get the character under the cursor and check if it's in 'matchpairs'.
56 let c_lnum = line('.')
57 let c_col = col('.')
58 let before = 0
59
Bram Moolenaardb6ea062014-07-10 22:01:47 +020060 let text = getline(c_lnum)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010061 let matches = matchlist(text, '\(.\)\=\%'.c_col.'c\(.\=\)')
Bram Moolenaar256972a2015-12-29 19:10:25 +010062 if empty(matches)
63 let [c_before, c] = ['', '']
64 else
65 let [c_before, c] = matches[1:2]
66 endif
Bram Moolenaar41e6cd52006-09-09 11:37:51 +000067 let plist = split(&matchpairs, '.\zs[:,]')
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000068 let i = index(plist, c)
69 if i < 0
70 " not found, in Insert mode try character before the cursor
71 if c_col > 1 && (mode() == 'i' || mode() == 'R')
Bram Moolenaar256972a2015-12-29 19:10:25 +010072 let before = strlen(c_before)
73 let c = c_before
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000074 let i = index(plist, c)
75 endif
76 if i < 0
77 " not found, nothing to do
78 return
79 endif
80 endif
81
82 " Figure out the arguments for searchpairpos().
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000083 if i % 2 == 0
84 let s_flags = 'nW'
85 let c2 = plist[i + 1]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000086 else
87 let s_flags = 'nbW'
88 let c2 = c
89 let c = plist[i - 1]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000090 endif
91 if c == '['
92 let c = '\['
93 let c2 = '\]'
94 endif
95
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000096 " Find the match. When it was just before the cursor move it there for a
Bram Moolenaarc06ac342006-03-02 22:43:39 +000097 " moment.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000098 if before > 0
Bram Moolenaar07d87792014-07-19 14:04:47 +020099 let has_getcurpos = exists("*getcurpos")
100 if has_getcurpos
101 " getcurpos() is more efficient but doesn't exist before 7.4.313.
102 let save_cursor = getcurpos()
103 else
104 let save_cursor = winsaveview()
105 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000106 call cursor(c_lnum, c_col - before)
107 endif
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000108
Bram Moolenaar3d1d6472018-07-03 18:18:23 +0200109 if !has("syntax") || !exists("g:syntax_on")
110 let s_skip = "0"
111 else
112 " Build an expression that detects whether the current cursor position is
113 " in certain syntax types (string, comment, etc.), for use as
114 " searchpairpos()'s skip argument.
Bram Moolenaar130cbfc2021-04-07 21:07:20 +0200115 " We match "escape" for special items, such as lispEscapeSpecial, and
116 " match "symbol" for lispBarSymbol.
Bram Moolenaardd60c362023-02-27 15:49:53 +0000117 let s_skip = 'synstack(".", col("."))'
118 \ . '->indexof({_, id -> synIDattr(id, "name") =~? '
119 \ . '"string\\|character\\|singlequote\\|escape\\|symbol\\|comment"}) >= 0'
Bram Moolenaar3d1d6472018-07-03 18:18:23 +0200120 " If executing the expression determines that the cursor is currently in
121 " one of the syntax types, then we want searchpairpos() to find the pair
122 " within those syntax types (i.e., not skip). Otherwise, the cursor is
123 " outside of the syntax types and s_skip should keep its value so we skip
124 " any matching pair inside the syntax types.
125 " Catch if this throws E363: pattern uses more memory than 'maxmempattern'.
126 try
127 execute 'if ' . s_skip . ' | let s_skip = "0" | endif'
128 catch /^Vim\%((\a\+)\)\=:E363/
129 " We won't find anything, so skip searching, should keep Vim responsive.
130 return
131 endtry
132 endif
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000133
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000134 " Limit the search to lines visible in the window.
135 let stoplinebottom = line('w$')
136 let stoplinetop = line('w0')
137 if i % 2 == 0
138 let stopline = stoplinebottom
139 else
140 let stopline = stoplinetop
141 endif
142
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200143 " Limit the search time to 300 msec to avoid a hang on very long lines.
144 " This fails when a timeout is not supported.
145 if mode() == 'i' || mode() == 'R'
146 let timeout = exists("b:matchparen_insert_timeout") ? b:matchparen_insert_timeout : g:matchparen_insert_timeout
147 else
148 let timeout = exists("b:matchparen_timeout") ? b:matchparen_timeout : g:matchparen_timeout
149 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000150 try
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200151 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, timeout)
Bram Moolenaar76929292008-01-06 19:07:36 +0000152 catch /E118/
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000153 " Can't use the timeout, restrict the stopline a bit more to avoid taking
154 " a long time on closed folds and long lines.
155 " The "viewable" variables give a range in which we can scroll while
156 " keeping the cursor at the same position.
157 " adjustedScrolloff accounts for very large numbers of scrolloff.
158 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2])
159 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2])
160 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2])
161 " one of these stoplines will be adjusted below, but the current values are
162 " minimal boundaries within the current window
163 if i % 2 == 0
164 if has("byte_offset") && has("syntax_items") && &smc > 0
165 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2])
166 let stopline = min([bottom_viewable, byte2line(stopbyte)])
167 else
168 let stopline = min([bottom_viewable, c_lnum + 100])
169 endif
170 let stoplinebottom = stopline
171 else
172 if has("byte_offset") && has("syntax_items") && &smc > 0
173 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2])
174 let stopline = max([top_viewable, byte2line(stopbyte)])
175 else
176 let stopline = max([top_viewable, c_lnum - 100])
177 endif
178 let stoplinetop = stopline
179 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000180 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
181 endtry
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000182
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000183 if before > 0
Bram Moolenaar07d87792014-07-19 14:04:47 +0200184 if has_getcurpos
185 call setpos('.', save_cursor)
186 else
187 call winrestview(save_cursor)
188 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000189 endif
190
191 " If a match is found setup match highlighting.
Bram Moolenaaref045862007-08-02 21:00:50 +0000192 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom
Christian Brabandtd3e277f2023-10-21 11:06:50 +0200193 if s:has_matchaddpos
194 call add(w:matchparen_ids, matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10))
Bram Moolenaarb3414592014-06-17 17:48:32 +0200195 else
196 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
197 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
Christian Brabandtd3e277f2023-10-21 11:06:50 +0200198 call add(w:matchparen_ids, 3)
Bram Moolenaarb3414592014-06-17 17:48:32 +0200199 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000200 let w:paren_hl_on = 1
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000201 endif
202endfunction
203
Bram Moolenaar73fef332020-06-21 22:12:03 +0200204func s:Remove_Matches()
205 if exists('w:paren_hl_on') && w:paren_hl_on
Christian Brabandtd3e277f2023-10-21 11:06:50 +0200206 while !empty(w:matchparen_ids)
207 silent! call remove(w:matchparen_ids, 0)->matchdelete()
208 endwhile
Bram Moolenaar73fef332020-06-21 22:12:03 +0200209 let w:paren_hl_on = 0
210 endif
211endfunc
212
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000213" Define commands that will disable and enable the plugin.
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100214command DoMatchParen call s:DoMatchParen()
215command NoMatchParen call s:NoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100216
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100217func s:NoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100218 let w = winnr()
219 noau windo silent! call matchdelete(3)
220 unlet! g:loaded_matchparen
221 exe "noau ". w . "wincmd w"
222 au! matchparen
223endfunc
224
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100225func s:DoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100226 runtime plugin/matchparen.vim
227 let w = winnr()
228 silent windo doau CursorMoved
229 exe "noau ". w . "wincmd w"
230endfunc
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +0000231
Bram Moolenaar5c736222010-01-06 20:54:52 +0100232let &cpo = s:cpo_save
233unlet s:cpo_save