blob: e19b28322872a1288278bde8d760d430b2ea5fea [file] [log] [blame]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001" Vim plugin for showing matching parens
2" Maintainer: Bram Moolenaar <Bram@vim.org>
Bram Moolenaar86b48162022-12-06 18:20:10 +00003" Last Change: 2022 Dec 01
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004
5" Exit quickly when:
6" - this plugin was already loaded (or disabled)
7" - when 'compatible' is set
Bram Moolenaarfd999452022-08-24 18:30:14 +01008if exists("g:loaded_matchparen") || &cp
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009 finish
10endif
11let g:loaded_matchparen = 1
12
Bram Moolenaarad3b3662013-05-17 18:14:19 +020013if !exists("g:matchparen_timeout")
14 let g:matchparen_timeout = 300
15endif
16if !exists("g:matchparen_insert_timeout")
17 let g:matchparen_insert_timeout = 60
18endif
19
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020augroup matchparen
21 " Replace all matchparen autocommands
Bram Moolenaar28a896f2022-11-28 22:21:12 +000022 autocmd! CursorMoved,CursorMovedI,WinEnter,BufWinEnter,WinScrolled * call s:Highlight_Matching_Pair()
23 autocmd! WinLeave,BufLeave * call s:Remove_Matches()
Bram Moolenaar186628f2013-03-19 13:33:23 +010024 if exists('##TextChanged')
25 autocmd! TextChanged,TextChangedI * call s:Highlight_Matching_Pair()
26 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000027augroup END
28
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000029" Skip the rest if it was already done.
30if exists("*s:Highlight_Matching_Pair")
31 finish
32endif
33
Bram Moolenaar5c736222010-01-06 20:54:52 +010034let s:cpo_save = &cpo
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +000035set cpo-=C
36
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000037" The function that is invoked (very often) to define a ":match" highlighting
38" for any matching paren.
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +010039func s:Highlight_Matching_Pair()
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000040 " Remove any previous match.
Bram Moolenaar73fef332020-06-21 22:12:03 +020041 call s:Remove_Matches()
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000042
Bram Moolenaar36fc5352006-03-04 21:49:37 +000043 " Avoid that we remove the popup menu.
Bram Moolenaarf2b2e702008-03-09 15:45:53 +000044 " Return when there are no colors (looks like the cursor jumps).
45 if pumvisible() || (&t_Co < 8 && !has("gui_running"))
Bram Moolenaar36fc5352006-03-04 21:49:37 +000046 return
47 endif
48
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000049 " Get the character under the cursor and check if it's in 'matchpairs'.
50 let c_lnum = line('.')
51 let c_col = col('.')
52 let before = 0
53
Bram Moolenaardb6ea062014-07-10 22:01:47 +020054 let text = getline(c_lnum)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010055 let matches = matchlist(text, '\(.\)\=\%'.c_col.'c\(.\=\)')
Bram Moolenaar256972a2015-12-29 19:10:25 +010056 if empty(matches)
57 let [c_before, c] = ['', '']
58 else
59 let [c_before, c] = matches[1:2]
60 endif
Bram Moolenaar41e6cd52006-09-09 11:37:51 +000061 let plist = split(&matchpairs, '.\zs[:,]')
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000062 let i = index(plist, c)
63 if i < 0
64 " not found, in Insert mode try character before the cursor
65 if c_col > 1 && (mode() == 'i' || mode() == 'R')
Bram Moolenaar256972a2015-12-29 19:10:25 +010066 let before = strlen(c_before)
67 let c = c_before
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000068 let i = index(plist, c)
69 endif
70 if i < 0
71 " not found, nothing to do
72 return
73 endif
74 endif
75
76 " Figure out the arguments for searchpairpos().
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000077 if i % 2 == 0
78 let s_flags = 'nW'
79 let c2 = plist[i + 1]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000080 else
81 let s_flags = 'nbW'
82 let c2 = c
83 let c = plist[i - 1]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000084 endif
85 if c == '['
86 let c = '\['
87 let c2 = '\]'
88 endif
89
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000090 " Find the match. When it was just before the cursor move it there for a
Bram Moolenaarc06ac342006-03-02 22:43:39 +000091 " moment.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000092 if before > 0
Bram Moolenaar07d87792014-07-19 14:04:47 +020093 let has_getcurpos = exists("*getcurpos")
94 if has_getcurpos
95 " getcurpos() is more efficient but doesn't exist before 7.4.313.
96 let save_cursor = getcurpos()
97 else
98 let save_cursor = winsaveview()
99 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000100 call cursor(c_lnum, c_col - before)
101 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
106 " Build an expression that detects whether the current cursor position is
107 " in certain syntax types (string, comment, etc.), for use as
108 " searchpairpos()'s skip argument.
Bram Moolenaar130cbfc2021-04-07 21:07:20 +0200109 " We match "escape" for special items, such as lispEscapeSpecial, and
110 " match "symbol" for lispBarSymbol.
Bram Moolenaardd60c362023-02-27 15:49:53 +0000111 let s_skip = 'synstack(".", col("."))'
112 \ . '->indexof({_, id -> synIDattr(id, "name") =~? '
113 \ . '"string\\|character\\|singlequote\\|escape\\|symbol\\|comment"}) >= 0'
Bram Moolenaar3d1d6472018-07-03 18:18:23 +0200114 " If executing the expression determines that the cursor is currently in
115 " one of the syntax types, then we want searchpairpos() to find the pair
116 " within those syntax types (i.e., not skip). Otherwise, the cursor is
117 " outside of the syntax types and s_skip should keep its value so we skip
118 " any matching pair inside the syntax types.
119 " Catch if this throws E363: pattern uses more memory than 'maxmempattern'.
120 try
121 execute 'if ' . s_skip . ' | let s_skip = "0" | endif'
122 catch /^Vim\%((\a\+)\)\=:E363/
123 " We won't find anything, so skip searching, should keep Vim responsive.
124 return
125 endtry
126 endif
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000127
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000128 " Limit the search to lines visible in the window.
129 let stoplinebottom = line('w$')
130 let stoplinetop = line('w0')
131 if i % 2 == 0
132 let stopline = stoplinebottom
133 else
134 let stopline = stoplinetop
135 endif
136
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200137 " Limit the search time to 300 msec to avoid a hang on very long lines.
138 " This fails when a timeout is not supported.
139 if mode() == 'i' || mode() == 'R'
140 let timeout = exists("b:matchparen_insert_timeout") ? b:matchparen_insert_timeout : g:matchparen_insert_timeout
141 else
142 let timeout = exists("b:matchparen_timeout") ? b:matchparen_timeout : g:matchparen_timeout
143 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000144 try
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200145 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, timeout)
Bram Moolenaar76929292008-01-06 19:07:36 +0000146 catch /E118/
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000147 " Can't use the timeout, restrict the stopline a bit more to avoid taking
148 " a long time on closed folds and long lines.
149 " The "viewable" variables give a range in which we can scroll while
150 " keeping the cursor at the same position.
151 " adjustedScrolloff accounts for very large numbers of scrolloff.
152 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2])
153 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2])
154 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2])
155 " one of these stoplines will be adjusted below, but the current values are
156 " minimal boundaries within the current window
157 if i % 2 == 0
158 if has("byte_offset") && has("syntax_items") && &smc > 0
159 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2])
160 let stopline = min([bottom_viewable, byte2line(stopbyte)])
161 else
162 let stopline = min([bottom_viewable, c_lnum + 100])
163 endif
164 let stoplinebottom = stopline
165 else
166 if has("byte_offset") && has("syntax_items") && &smc > 0
167 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2])
168 let stopline = max([top_viewable, byte2line(stopbyte)])
169 else
170 let stopline = max([top_viewable, c_lnum - 100])
171 endif
172 let stoplinetop = stopline
173 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000174 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
175 endtry
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000176
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000177 if before > 0
Bram Moolenaar07d87792014-07-19 14:04:47 +0200178 if has_getcurpos
179 call setpos('.', save_cursor)
180 else
181 call winrestview(save_cursor)
182 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000183 endif
184
185 " If a match is found setup match highlighting.
Bram Moolenaaref045862007-08-02 21:00:50 +0000186 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom
Bram Moolenaarb3414592014-06-17 17:48:32 +0200187 if exists('*matchaddpos')
188 call matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10, 3)
189 else
190 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
191 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
192 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000193 let w:paren_hl_on = 1
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000194 endif
195endfunction
196
Bram Moolenaar73fef332020-06-21 22:12:03 +0200197func s:Remove_Matches()
198 if exists('w:paren_hl_on') && w:paren_hl_on
199 silent! call matchdelete(3)
200 let w:paren_hl_on = 0
201 endif
202endfunc
203
204
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000205" Define commands that will disable and enable the plugin.
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100206command DoMatchParen call s:DoMatchParen()
207command NoMatchParen call s:NoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100208
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100209func s:NoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100210 let w = winnr()
211 noau windo silent! call matchdelete(3)
212 unlet! g:loaded_matchparen
213 exe "noau ". w . "wincmd w"
214 au! matchparen
215endfunc
216
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100217func s:DoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100218 runtime plugin/matchparen.vim
219 let w = winnr()
220 silent windo doau CursorMoved
221 exe "noau ". w . "wincmd w"
222endfunc
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +0000223
Bram Moolenaar5c736222010-01-06 20:54:52 +0100224let &cpo = s:cpo_save
225unlet s:cpo_save