blob: 2965ad147191a0059da557eb9a959a40ebf5cc9b [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 Moolenaar71afbfe2013-03-19 16:49:16 +01003" Last Change: 2013 Mar 19
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
14augroup matchparen
15 " Replace all matchparen autocommands
Bram Moolenaarfa2e0442007-08-18 16:21:50 +000016 autocmd! CursorMoved,CursorMovedI,WinEnter * call s:Highlight_Matching_Pair()
Bram Moolenaar186628f2013-03-19 13:33:23 +010017 if exists('##TextChanged')
18 autocmd! TextChanged,TextChangedI * call s:Highlight_Matching_Pair()
19 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020augroup END
21
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022" Skip the rest if it was already done.
23if exists("*s:Highlight_Matching_Pair")
24 finish
25endif
26
Bram Moolenaar5c736222010-01-06 20:54:52 +010027let s:cpo_save = &cpo
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +000028set cpo-=C
29
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000030" The function that is invoked (very often) to define a ":match" highlighting
31" for any matching paren.
32function! s:Highlight_Matching_Pair()
33 " Remove any previous match.
Bram Moolenaar910f66f2006-04-05 20:41:53 +000034 if exists('w:paren_hl_on') && w:paren_hl_on
Bram Moolenaare1438bb2006-03-01 22:01:55 +000035 3match none
Bram Moolenaar910f66f2006-04-05 20:41:53 +000036 let w:paren_hl_on = 0
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000037 endif
38
Bram Moolenaar36fc5352006-03-04 21:49:37 +000039 " Avoid that we remove the popup menu.
Bram Moolenaarf2b2e702008-03-09 15:45:53 +000040 " Return when there are no colors (looks like the cursor jumps).
41 if pumvisible() || (&t_Co < 8 && !has("gui_running"))
Bram Moolenaar36fc5352006-03-04 21:49:37 +000042 return
43 endif
44
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000045 " Get the character under the cursor and check if it's in 'matchpairs'.
46 let c_lnum = line('.')
47 let c_col = col('.')
48 let before = 0
49
50 let c = getline(c_lnum)[c_col - 1]
Bram Moolenaar41e6cd52006-09-09 11:37:51 +000051 let plist = split(&matchpairs, '.\zs[:,]')
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000052 let i = index(plist, c)
53 if i < 0
54 " not found, in Insert mode try character before the cursor
55 if c_col > 1 && (mode() == 'i' || mode() == 'R')
56 let before = 1
57 let c = getline(c_lnum)[c_col - 2]
58 let i = index(plist, c)
59 endif
60 if i < 0
61 " not found, nothing to do
62 return
63 endif
64 endif
65
66 " Figure out the arguments for searchpairpos().
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000067 if i % 2 == 0
68 let s_flags = 'nW'
69 let c2 = plist[i + 1]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000070 else
71 let s_flags = 'nbW'
72 let c2 = c
73 let c = plist[i - 1]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000074 endif
75 if c == '['
76 let c = '\['
77 let c2 = '\]'
78 endif
79
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000080 " Find the match. When it was just before the cursor move it there for a
Bram Moolenaarc06ac342006-03-02 22:43:39 +000081 " moment.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000082 if before > 0
Bram Moolenaar2347f062006-05-13 12:48:30 +000083 let save_cursor = winsaveview()
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000084 call cursor(c_lnum, c_col - before)
85 endif
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +000086
87 " When not in a string or comment ignore matches inside them.
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020088 " We match "escape" for special items, such as lispEscapeSpecial.
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +000089 let s_skip ='synIDattr(synID(line("."), col("."), 0), "name") ' .
Bram Moolenaar81af9252010-12-10 20:35:50 +010090 \ '=~? "string\\|character\\|singlequote\\|escape\\|comment"'
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +000091 execute 'if' s_skip '| let s_skip = 0 | endif'
92
Bram Moolenaarf2b2e702008-03-09 15:45:53 +000093 " Limit the search to lines visible in the window.
94 let stoplinebottom = line('w$')
95 let stoplinetop = line('w0')
96 if i % 2 == 0
97 let stopline = stoplinebottom
98 else
99 let stopline = stoplinetop
100 endif
101
Bram Moolenaar76929292008-01-06 19:07:36 +0000102 try
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000103 " Limit the search time to 300 msec to avoid a hang on very long lines.
104 " This fails when a timeout is not supported.
105 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, 300)
Bram Moolenaar76929292008-01-06 19:07:36 +0000106 catch /E118/
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000107 " Can't use the timeout, restrict the stopline a bit more to avoid taking
108 " a long time on closed folds and long lines.
109 " The "viewable" variables give a range in which we can scroll while
110 " keeping the cursor at the same position.
111 " adjustedScrolloff accounts for very large numbers of scrolloff.
112 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2])
113 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2])
114 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2])
115 " one of these stoplines will be adjusted below, but the current values are
116 " minimal boundaries within the current window
117 if i % 2 == 0
118 if has("byte_offset") && has("syntax_items") && &smc > 0
119 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2])
120 let stopline = min([bottom_viewable, byte2line(stopbyte)])
121 else
122 let stopline = min([bottom_viewable, c_lnum + 100])
123 endif
124 let stoplinebottom = stopline
125 else
126 if has("byte_offset") && has("syntax_items") && &smc > 0
127 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2])
128 let stopline = max([top_viewable, byte2line(stopbyte)])
129 else
130 let stopline = max([top_viewable, c_lnum - 100])
131 endif
132 let stoplinetop = stopline
133 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000134 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
135 endtry
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000136
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000137 if before > 0
Bram Moolenaar2347f062006-05-13 12:48:30 +0000138 call winrestview(save_cursor)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000139 endif
140
141 " If a match is found setup match highlighting.
Bram Moolenaaref045862007-08-02 21:00:50 +0000142 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000143 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000144 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000145 let w:paren_hl_on = 1
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000146 endif
147endfunction
148
149" Define commands that will disable and enable the plugin.
Bram Moolenaarfa2e0442007-08-18 16:21:50 +0000150command! NoMatchParen windo 3match none | unlet! g:loaded_matchparen |
151 \ au! matchparen
152command! DoMatchParen runtime plugin/matchparen.vim | windo doau CursorMoved
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +0000153
Bram Moolenaar5c736222010-01-06 20:54:52 +0100154let &cpo = s:cpo_save
155unlet s:cpo_save