blob: 23c79382fff22e479a638b83c0a9ce075608bd65 [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 Moolenaardb6ea062014-07-10 22:01:47 +02003" Last Change: 2014 Jul 09
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)
58 let c = text[c_col - 1]
Bram Moolenaar41e6cd52006-09-09 11:37:51 +000059 let plist = split(&matchpairs, '.\zs[:,]')
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000060 let i = index(plist, c)
61 if i < 0
62 " not found, in Insert mode try character before the cursor
63 if c_col > 1 && (mode() == 'i' || mode() == 'R')
64 let before = 1
Bram Moolenaardb6ea062014-07-10 22:01:47 +020065 let c = text[c_col - 2]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000066 let i = index(plist, c)
67 endif
68 if i < 0
69 " not found, nothing to do
70 return
71 endif
72 endif
73
74 " Figure out the arguments for searchpairpos().
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000075 if i % 2 == 0
76 let s_flags = 'nW'
77 let c2 = plist[i + 1]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000078 else
79 let s_flags = 'nbW'
80 let c2 = c
81 let c = plist[i - 1]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000082 endif
83 if c == '['
84 let c = '\['
85 let c2 = '\]'
86 endif
87
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000088 " Find the match. When it was just before the cursor move it there for a
Bram Moolenaarc06ac342006-03-02 22:43:39 +000089 " moment.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000090 if before > 0
Bram Moolenaardb6ea062014-07-10 22:01:47 +020091 let save_cursor = getcurpos()
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000092 call cursor(c_lnum, c_col - before)
93 endif
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +000094
95 " When not in a string or comment ignore matches inside them.
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020096 " We match "escape" for special items, such as lispEscapeSpecial.
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +000097 let s_skip ='synIDattr(synID(line("."), col("."), 0), "name") ' .
Bram Moolenaar81af9252010-12-10 20:35:50 +010098 \ '=~? "string\\|character\\|singlequote\\|escape\\|comment"'
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +000099 execute 'if' s_skip '| let s_skip = 0 | endif'
100
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000101 " Limit the search to lines visible in the window.
102 let stoplinebottom = line('w$')
103 let stoplinetop = line('w0')
104 if i % 2 == 0
105 let stopline = stoplinebottom
106 else
107 let stopline = stoplinetop
108 endif
109
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200110 " Limit the search time to 300 msec to avoid a hang on very long lines.
111 " This fails when a timeout is not supported.
112 if mode() == 'i' || mode() == 'R'
113 let timeout = exists("b:matchparen_insert_timeout") ? b:matchparen_insert_timeout : g:matchparen_insert_timeout
114 else
115 let timeout = exists("b:matchparen_timeout") ? b:matchparen_timeout : g:matchparen_timeout
116 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000117 try
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200118 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, timeout)
Bram Moolenaar76929292008-01-06 19:07:36 +0000119 catch /E118/
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000120 " Can't use the timeout, restrict the stopline a bit more to avoid taking
121 " a long time on closed folds and long lines.
122 " The "viewable" variables give a range in which we can scroll while
123 " keeping the cursor at the same position.
124 " adjustedScrolloff accounts for very large numbers of scrolloff.
125 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2])
126 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2])
127 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2])
128 " one of these stoplines will be adjusted below, but the current values are
129 " minimal boundaries within the current window
130 if i % 2 == 0
131 if has("byte_offset") && has("syntax_items") && &smc > 0
132 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2])
133 let stopline = min([bottom_viewable, byte2line(stopbyte)])
134 else
135 let stopline = min([bottom_viewable, c_lnum + 100])
136 endif
137 let stoplinebottom = stopline
138 else
139 if has("byte_offset") && has("syntax_items") && &smc > 0
140 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2])
141 let stopline = max([top_viewable, byte2line(stopbyte)])
142 else
143 let stopline = max([top_viewable, c_lnum - 100])
144 endif
145 let stoplinetop = stopline
146 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000147 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
148 endtry
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000149
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000150 if before > 0
Bram Moolenaardb6ea062014-07-10 22:01:47 +0200151 call setpos('.', save_cursor)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000152 endif
153
154 " If a match is found setup match highlighting.
Bram Moolenaaref045862007-08-02 21:00:50 +0000155 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom
Bram Moolenaarb3414592014-06-17 17:48:32 +0200156 if exists('*matchaddpos')
157 call matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10, 3)
158 else
159 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
160 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
161 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000162 let w:paren_hl_on = 1
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000163 endif
164endfunction
165
166" Define commands that will disable and enable the plugin.
Bram Moolenaarb3414592014-06-17 17:48:32 +0200167command! NoMatchParen windo silent! call matchdelete(3) | unlet! g:loaded_matchparen |
Bram Moolenaarfa2e0442007-08-18 16:21:50 +0000168 \ au! matchparen
169command! DoMatchParen runtime plugin/matchparen.vim | windo doau CursorMoved
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +0000170
Bram Moolenaar5c736222010-01-06 20:54:52 +0100171let &cpo = s:cpo_save
172unlet s:cpo_save