blob: d53fb22df023fd87b1c1f642c99de6b9cbe38c31 [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 Moolenaar6dc819b2018-07-03 16:42:19 +02003" Last Change: 2018 Jun 23
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 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.
39function! s:Highlight_Matching_Pair()
40 " Remove any previous match.
Bram Moolenaar910f66f2006-04-05 20:41:53 +000041 if exists('w:paren_hl_on') && w:paren_hl_on
Bram Moolenaarb3414592014-06-17 17:48:32 +020042 silent! call matchdelete(3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000043 let w:paren_hl_on = 0
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000044 endif
45
Bram Moolenaar36fc5352006-03-04 21:49:37 +000046 " Avoid that we remove the popup menu.
Bram Moolenaarf2b2e702008-03-09 15:45:53 +000047 " Return when there are no colors (looks like the cursor jumps).
48 if pumvisible() || (&t_Co < 8 && !has("gui_running"))
Bram Moolenaar36fc5352006-03-04 21:49:37 +000049 return
50 endif
51
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000052 " Get the character under the cursor and check if it's in 'matchpairs'.
53 let c_lnum = line('.')
54 let c_col = col('.')
55 let before = 0
56
Bram Moolenaardb6ea062014-07-10 22:01:47 +020057 let text = getline(c_lnum)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010058 let matches = matchlist(text, '\(.\)\=\%'.c_col.'c\(.\=\)')
Bram Moolenaar256972a2015-12-29 19:10:25 +010059 if empty(matches)
60 let [c_before, c] = ['', '']
61 else
62 let [c_before, c] = matches[1:2]
63 endif
Bram Moolenaar41e6cd52006-09-09 11:37:51 +000064 let plist = split(&matchpairs, '.\zs[:,]')
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000065 let i = index(plist, c)
66 if i < 0
67 " not found, in Insert mode try character before the cursor
68 if c_col > 1 && (mode() == 'i' || mode() == 'R')
Bram Moolenaar256972a2015-12-29 19:10:25 +010069 let before = strlen(c_before)
70 let c = c_before
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000071 let i = index(plist, c)
72 endif
73 if i < 0
74 " not found, nothing to do
75 return
76 endif
77 endif
78
79 " Figure out the arguments for searchpairpos().
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000080 if i % 2 == 0
81 let s_flags = 'nW'
82 let c2 = plist[i + 1]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000083 else
84 let s_flags = 'nbW'
85 let c2 = c
86 let c = plist[i - 1]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000087 endif
88 if c == '['
89 let c = '\['
90 let c2 = '\]'
91 endif
92
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000093 " Find the match. When it was just before the cursor move it there for a
Bram Moolenaarc06ac342006-03-02 22:43:39 +000094 " moment.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000095 if before > 0
Bram Moolenaar07d87792014-07-19 14:04:47 +020096 let has_getcurpos = exists("*getcurpos")
97 if has_getcurpos
98 " getcurpos() is more efficient but doesn't exist before 7.4.313.
99 let save_cursor = getcurpos()
100 else
101 let save_cursor = winsaveview()
102 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000103 call cursor(c_lnum, c_col - before)
104 endif
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000105
Bram Moolenaare98cfe12014-08-06 19:09:16 +0200106 " Build an expression that detects whether the current cursor position is in
107 " certain syntax types (string, comment, etc.), for use as searchpairpos()'s
108 " skip argument.
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200109 " We match "escape" for special items, such as lispEscapeSpecial.
Bram Moolenaare98cfe12014-08-06 19:09:16 +0200110 let s_skip = '!empty(filter(map(synstack(line("."), col(".")), ''synIDattr(v:val, "name")''), ' .
111 \ '''v:val =~? "string\\|character\\|singlequote\\|escape\\|comment"''))'
112 " If executing the expression determines that the cursor is currently in
113 " one of the syntax types, then we want searchpairpos() to find the pair
114 " within those syntax types (i.e., not skip). Otherwise, the cursor is
115 " outside of the syntax types and s_skip should keep its value so we skip any
116 " matching pair inside the syntax types.
Bram Moolenaarb7a5ab12018-06-25 00:05:59 +0200117 execute 'if' s_skip '| let s_skip = "0" | endif'
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000118
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000119 " Limit the search to lines visible in the window.
120 let stoplinebottom = line('w$')
121 let stoplinetop = line('w0')
122 if i % 2 == 0
123 let stopline = stoplinebottom
124 else
125 let stopline = stoplinetop
126 endif
127
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200128 " Limit the search time to 300 msec to avoid a hang on very long lines.
129 " This fails when a timeout is not supported.
130 if mode() == 'i' || mode() == 'R'
131 let timeout = exists("b:matchparen_insert_timeout") ? b:matchparen_insert_timeout : g:matchparen_insert_timeout
132 else
133 let timeout = exists("b:matchparen_timeout") ? b:matchparen_timeout : g:matchparen_timeout
134 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000135 try
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200136 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, timeout)
Bram Moolenaar76929292008-01-06 19:07:36 +0000137 catch /E118/
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000138 " Can't use the timeout, restrict the stopline a bit more to avoid taking
139 " a long time on closed folds and long lines.
140 " The "viewable" variables give a range in which we can scroll while
141 " keeping the cursor at the same position.
142 " adjustedScrolloff accounts for very large numbers of scrolloff.
143 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2])
144 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2])
145 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2])
146 " one of these stoplines will be adjusted below, but the current values are
147 " minimal boundaries within the current window
148 if i % 2 == 0
149 if has("byte_offset") && has("syntax_items") && &smc > 0
150 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2])
151 let stopline = min([bottom_viewable, byte2line(stopbyte)])
152 else
153 let stopline = min([bottom_viewable, c_lnum + 100])
154 endif
155 let stoplinebottom = stopline
156 else
157 if has("byte_offset") && has("syntax_items") && &smc > 0
158 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2])
159 let stopline = max([top_viewable, byte2line(stopbyte)])
160 else
161 let stopline = max([top_viewable, c_lnum - 100])
162 endif
163 let stoplinetop = stopline
164 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000165 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
166 endtry
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000167
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000168 if before > 0
Bram Moolenaar07d87792014-07-19 14:04:47 +0200169 if has_getcurpos
170 call setpos('.', save_cursor)
171 else
172 call winrestview(save_cursor)
173 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000174 endif
175
176 " If a match is found setup match highlighting.
Bram Moolenaaref045862007-08-02 21:00:50 +0000177 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom
Bram Moolenaarb3414592014-06-17 17:48:32 +0200178 if exists('*matchaddpos')
179 call matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10, 3)
180 else
181 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
182 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
183 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000184 let w:paren_hl_on = 1
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000185 endif
186endfunction
187
188" Define commands that will disable and enable the plugin.
Bram Moolenaar01164a62017-11-02 22:58:42 +0100189command! DoMatchParen call s:DoMatchParen()
190command! NoMatchParen call s:NoMatchParen()
191
192func! s:NoMatchParen()
193 let w = winnr()
194 noau windo silent! call matchdelete(3)
195 unlet! g:loaded_matchparen
196 exe "noau ". w . "wincmd w"
197 au! matchparen
198endfunc
199
200func! s:DoMatchParen()
201 runtime plugin/matchparen.vim
202 let w = winnr()
203 silent windo doau CursorMoved
204 exe "noau ". w . "wincmd w"
205endfunc
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +0000206
Bram Moolenaar5c736222010-01-06 20:54:52 +0100207let &cpo = s:cpo_save
208unlet s:cpo_save