blob: 162430ecd0f5f0fcb8e977b75342dc8497fece0b [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 Moolenaar73fef332020-06-21 22:12:03 +02003" Last Change: 2020 Jun 18
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 Moolenaar84a05ac2013-05-06 04:24:17 +02008" - the "CursorMoved" autocmd event is not available.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009if exists("g:loaded_matchparen") || &cp || !exists("##CursorMoved")
10 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
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021augroup matchparen
22 " Replace all matchparen autocommands
Bram Moolenaarfa2e0442007-08-18 16:21:50 +000023 autocmd! CursorMoved,CursorMovedI,WinEnter * call s:Highlight_Matching_Pair()
Bram Moolenaar73fef332020-06-21 22:12:03 +020024 autocmd! WinLeave * call s:Remove_Matches()
Bram Moolenaar186628f2013-03-19 13:33:23 +010025 if exists('##TextChanged')
26 autocmd! TextChanged,TextChangedI * call s:Highlight_Matching_Pair()
27 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000028augroup END
29
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000030" Skip the rest if it was already done.
31if exists("*s:Highlight_Matching_Pair")
32 finish
33endif
34
Bram Moolenaar5c736222010-01-06 20:54:52 +010035let s:cpo_save = &cpo
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +000036set cpo-=C
37
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000038" The function that is invoked (very often) to define a ":match" highlighting
39" for any matching paren.
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +010040func s:Highlight_Matching_Pair()
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000041 " Remove any previous match.
Bram Moolenaar73fef332020-06-21 22:12:03 +020042 call s:Remove_Matches()
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000043
Bram Moolenaar36fc5352006-03-04 21:49:37 +000044 " Avoid that we remove the popup menu.
Bram Moolenaarf2b2e702008-03-09 15:45:53 +000045 " Return when there are no colors (looks like the cursor jumps).
46 if pumvisible() || (&t_Co < 8 && !has("gui_running"))
Bram Moolenaar36fc5352006-03-04 21:49:37 +000047 return
48 endif
49
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000050 " Get the character under the cursor and check if it's in 'matchpairs'.
51 let c_lnum = line('.')
52 let c_col = col('.')
53 let before = 0
54
Bram Moolenaardb6ea062014-07-10 22:01:47 +020055 let text = getline(c_lnum)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010056 let matches = matchlist(text, '\(.\)\=\%'.c_col.'c\(.\=\)')
Bram Moolenaar256972a2015-12-29 19:10:25 +010057 if empty(matches)
58 let [c_before, c] = ['', '']
59 else
60 let [c_before, c] = matches[1:2]
61 endif
Bram Moolenaar41e6cd52006-09-09 11:37:51 +000062 let plist = split(&matchpairs, '.\zs[:,]')
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000063 let i = index(plist, c)
64 if i < 0
65 " not found, in Insert mode try character before the cursor
66 if c_col > 1 && (mode() == 'i' || mode() == 'R')
Bram Moolenaar256972a2015-12-29 19:10:25 +010067 let before = strlen(c_before)
68 let c = c_before
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000069 let i = index(plist, c)
70 endif
71 if i < 0
72 " not found, nothing to do
73 return
74 endif
75 endif
76
77 " Figure out the arguments for searchpairpos().
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000078 if i % 2 == 0
79 let s_flags = 'nW'
80 let c2 = plist[i + 1]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000081 else
82 let s_flags = 'nbW'
83 let c2 = c
84 let c = plist[i - 1]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000085 endif
86 if c == '['
87 let c = '\['
88 let c2 = '\]'
89 endif
90
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000091 " Find the match. When it was just before the cursor move it there for a
Bram Moolenaarc06ac342006-03-02 22:43:39 +000092 " moment.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000093 if before > 0
Bram Moolenaar07d87792014-07-19 14:04:47 +020094 let has_getcurpos = exists("*getcurpos")
95 if has_getcurpos
96 " getcurpos() is more efficient but doesn't exist before 7.4.313.
97 let save_cursor = getcurpos()
98 else
99 let save_cursor = winsaveview()
100 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000101 call cursor(c_lnum, c_col - before)
102 endif
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000103
Bram Moolenaar3d1d6472018-07-03 18:18:23 +0200104 if !has("syntax") || !exists("g:syntax_on")
105 let s_skip = "0"
106 else
107 " Build an expression that detects whether the current cursor position is
108 " in certain syntax types (string, comment, etc.), for use as
109 " searchpairpos()'s skip argument.
110 " We match "escape" for special items, such as lispEscapeSpecial.
111 let s_skip = '!empty(filter(map(synstack(line("."), col(".")), ''synIDattr(v:val, "name")''), ' .
Bram Moolenaare98cfe12014-08-06 19:09:16 +0200112 \ '''v:val =~? "string\\|character\\|singlequote\\|escape\\|comment"''))'
Bram Moolenaar3d1d6472018-07-03 18:18:23 +0200113 " If executing the expression determines that the cursor is currently in
114 " one of the syntax types, then we want searchpairpos() to find the pair
115 " within those syntax types (i.e., not skip). Otherwise, the cursor is
116 " outside of the syntax types and s_skip should keep its value so we skip
117 " any matching pair inside the syntax types.
118 " Catch if this throws E363: pattern uses more memory than 'maxmempattern'.
119 try
120 execute 'if ' . s_skip . ' | let s_skip = "0" | endif'
121 catch /^Vim\%((\a\+)\)\=:E363/
122 " We won't find anything, so skip searching, should keep Vim responsive.
123 return
124 endtry
125 endif
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000126
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000127 " Limit the search to lines visible in the window.
128 let stoplinebottom = line('w$')
129 let stoplinetop = line('w0')
130 if i % 2 == 0
131 let stopline = stoplinebottom
132 else
133 let stopline = stoplinetop
134 endif
135
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200136 " Limit the search time to 300 msec to avoid a hang on very long lines.
137 " This fails when a timeout is not supported.
138 if mode() == 'i' || mode() == 'R'
139 let timeout = exists("b:matchparen_insert_timeout") ? b:matchparen_insert_timeout : g:matchparen_insert_timeout
140 else
141 let timeout = exists("b:matchparen_timeout") ? b:matchparen_timeout : g:matchparen_timeout
142 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000143 try
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200144 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, timeout)
Bram Moolenaar76929292008-01-06 19:07:36 +0000145 catch /E118/
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000146 " Can't use the timeout, restrict the stopline a bit more to avoid taking
147 " a long time on closed folds and long lines.
148 " The "viewable" variables give a range in which we can scroll while
149 " keeping the cursor at the same position.
150 " adjustedScrolloff accounts for very large numbers of scrolloff.
151 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2])
152 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2])
153 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2])
154 " one of these stoplines will be adjusted below, but the current values are
155 " minimal boundaries within the current window
156 if i % 2 == 0
157 if has("byte_offset") && has("syntax_items") && &smc > 0
158 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2])
159 let stopline = min([bottom_viewable, byte2line(stopbyte)])
160 else
161 let stopline = min([bottom_viewable, c_lnum + 100])
162 endif
163 let stoplinebottom = stopline
164 else
165 if has("byte_offset") && has("syntax_items") && &smc > 0
166 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2])
167 let stopline = max([top_viewable, byte2line(stopbyte)])
168 else
169 let stopline = max([top_viewable, c_lnum - 100])
170 endif
171 let stoplinetop = stopline
172 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000173 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
174 endtry
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000175
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000176 if before > 0
Bram Moolenaar07d87792014-07-19 14:04:47 +0200177 if has_getcurpos
178 call setpos('.', save_cursor)
179 else
180 call winrestview(save_cursor)
181 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000182 endif
183
184 " If a match is found setup match highlighting.
Bram Moolenaaref045862007-08-02 21:00:50 +0000185 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom
Bram Moolenaarb3414592014-06-17 17:48:32 +0200186 if exists('*matchaddpos')
187 call matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10, 3)
188 else
189 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
190 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
191 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000192 let w:paren_hl_on = 1
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000193 endif
194endfunction
195
Bram Moolenaar73fef332020-06-21 22:12:03 +0200196func s:Remove_Matches()
197 if exists('w:paren_hl_on') && w:paren_hl_on
198 silent! call matchdelete(3)
199 let w:paren_hl_on = 0
200 endif
201endfunc
202
203
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000204" Define commands that will disable and enable the plugin.
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100205command DoMatchParen call s:DoMatchParen()
206command NoMatchParen call s:NoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100207
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100208func s:NoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100209 let w = winnr()
210 noau windo silent! call matchdelete(3)
211 unlet! g:loaded_matchparen
212 exe "noau ". w . "wincmd w"
213 au! matchparen
214endfunc
215
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +0100216func s:DoMatchParen()
Bram Moolenaar01164a62017-11-02 22:58:42 +0100217 runtime plugin/matchparen.vim
218 let w = winnr()
219 silent windo doau CursorMoved
220 exe "noau ". w . "wincmd w"
221endfunc
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +0000222
Bram Moolenaar5c736222010-01-06 20:54:52 +0100223let &cpo = s:cpo_save
224unlet s:cpo_save