blob: d5a0ac5a97fc93321880755e18c672143bb87f3d [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 Moolenaar5c736222010-01-06 20:54:52 +01003" Last Change: 2008 Sep 03
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
8" - the "CursorMoved" autocmd event is not availble.
9if 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 Moolenaar5e3cb7e2006-02-27 23:58:35 +000017augroup END
18
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019" Skip the rest if it was already done.
20if exists("*s:Highlight_Matching_Pair")
21 finish
22endif
23
Bram Moolenaar5c736222010-01-06 20:54:52 +010024let s:cpo_save = &cpo
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +000025set cpo-=C
26
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000027" The function that is invoked (very often) to define a ":match" highlighting
28" for any matching paren.
29function! s:Highlight_Matching_Pair()
30 " Remove any previous match.
Bram Moolenaar910f66f2006-04-05 20:41:53 +000031 if exists('w:paren_hl_on') && w:paren_hl_on
Bram Moolenaare1438bb2006-03-01 22:01:55 +000032 3match none
Bram Moolenaar910f66f2006-04-05 20:41:53 +000033 let w:paren_hl_on = 0
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000034 endif
35
Bram Moolenaar36fc5352006-03-04 21:49:37 +000036 " Avoid that we remove the popup menu.
Bram Moolenaarf2b2e702008-03-09 15:45:53 +000037 " Return when there are no colors (looks like the cursor jumps).
38 if pumvisible() || (&t_Co < 8 && !has("gui_running"))
Bram Moolenaar36fc5352006-03-04 21:49:37 +000039 return
40 endif
41
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000042 " Get the character under the cursor and check if it's in 'matchpairs'.
43 let c_lnum = line('.')
44 let c_col = col('.')
45 let before = 0
46
47 let c = getline(c_lnum)[c_col - 1]
Bram Moolenaar41e6cd52006-09-09 11:37:51 +000048 let plist = split(&matchpairs, '.\zs[:,]')
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000049 let i = index(plist, c)
50 if i < 0
51 " not found, in Insert mode try character before the cursor
52 if c_col > 1 && (mode() == 'i' || mode() == 'R')
53 let before = 1
54 let c = getline(c_lnum)[c_col - 2]
55 let i = index(plist, c)
56 endif
57 if i < 0
58 " not found, nothing to do
59 return
60 endif
61 endif
62
63 " Figure out the arguments for searchpairpos().
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000064 if i % 2 == 0
65 let s_flags = 'nW'
66 let c2 = plist[i + 1]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000067 else
68 let s_flags = 'nbW'
69 let c2 = c
70 let c = plist[i - 1]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000071 endif
72 if c == '['
73 let c = '\['
74 let c2 = '\]'
75 endif
76
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000077 " Find the match. When it was just before the cursor move it there for a
Bram Moolenaarc06ac342006-03-02 22:43:39 +000078 " moment.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000079 if before > 0
Bram Moolenaar2347f062006-05-13 12:48:30 +000080 let save_cursor = winsaveview()
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000081 call cursor(c_lnum, c_col - before)
82 endif
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +000083
84 " When not in a string or comment ignore matches inside them.
85 let s_skip ='synIDattr(synID(line("."), col("."), 0), "name") ' .
Bram Moolenaar8ff38002006-08-08 16:07:03 +000086 \ '=~? "string\\|character\\|singlequote\\|comment"'
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +000087 execute 'if' s_skip '| let s_skip = 0 | endif'
88
Bram Moolenaarf2b2e702008-03-09 15:45:53 +000089 " Limit the search to lines visible in the window.
90 let stoplinebottom = line('w$')
91 let stoplinetop = line('w0')
92 if i % 2 == 0
93 let stopline = stoplinebottom
94 else
95 let stopline = stoplinetop
96 endif
97
Bram Moolenaar76929292008-01-06 19:07:36 +000098 try
Bram Moolenaarf2b2e702008-03-09 15:45:53 +000099 " Limit the search time to 300 msec to avoid a hang on very long lines.
100 " This fails when a timeout is not supported.
101 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, 300)
Bram Moolenaar76929292008-01-06 19:07:36 +0000102 catch /E118/
Bram Moolenaarf2b2e702008-03-09 15:45:53 +0000103 " Can't use the timeout, restrict the stopline a bit more to avoid taking
104 " a long time on closed folds and long lines.
105 " The "viewable" variables give a range in which we can scroll while
106 " keeping the cursor at the same position.
107 " adjustedScrolloff accounts for very large numbers of scrolloff.
108 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2])
109 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2])
110 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2])
111 " one of these stoplines will be adjusted below, but the current values are
112 " minimal boundaries within the current window
113 if i % 2 == 0
114 if has("byte_offset") && has("syntax_items") && &smc > 0
115 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2])
116 let stopline = min([bottom_viewable, byte2line(stopbyte)])
117 else
118 let stopline = min([bottom_viewable, c_lnum + 100])
119 endif
120 let stoplinebottom = stopline
121 else
122 if has("byte_offset") && has("syntax_items") && &smc > 0
123 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2])
124 let stopline = max([top_viewable, byte2line(stopbyte)])
125 else
126 let stopline = max([top_viewable, c_lnum - 100])
127 endif
128 let stoplinetop = stopline
129 endif
Bram Moolenaar76929292008-01-06 19:07:36 +0000130 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
131 endtry
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000132
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000133 if before > 0
Bram Moolenaar2347f062006-05-13 12:48:30 +0000134 call winrestview(save_cursor)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000135 endif
136
137 " If a match is found setup match highlighting.
Bram Moolenaaref045862007-08-02 21:00:50 +0000138 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000139 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000140 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000141 let w:paren_hl_on = 1
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000142 endif
143endfunction
144
145" Define commands that will disable and enable the plugin.
Bram Moolenaarfa2e0442007-08-18 16:21:50 +0000146command! NoMatchParen windo 3match none | unlet! g:loaded_matchparen |
147 \ au! matchparen
148command! DoMatchParen runtime plugin/matchparen.vim | windo doau CursorMoved
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +0000149
Bram Moolenaar5c736222010-01-06 20:54:52 +0100150let &cpo = s:cpo_save
151unlet s:cpo_save