694
|
1 " Vim plugin for showing matching parens
|
|
2 " Maintainer: Bram Moolenaar <Bram@vim.org>
|
3082
|
3 " Last Change: 2011 Aug 27
|
694
|
4
|
|
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.
|
|
9 if exists("g:loaded_matchparen") || &cp || !exists("##CursorMoved")
|
|
10 finish
|
|
11 endif
|
|
12 let g:loaded_matchparen = 1
|
|
13
|
|
14 augroup matchparen
|
|
15 " Replace all matchparen autocommands
|
1368
|
16 autocmd! CursorMoved,CursorMovedI,WinEnter * call s:Highlight_Matching_Pair()
|
4232
|
17 if exists('##TextChanged')
|
|
18 autocmd! TextChanged,TextChangedI * call s:Highlight_Matching_Pair()
|
|
19 endif
|
694
|
20 augroup END
|
|
21
|
|
22 " Skip the rest if it was already done.
|
|
23 if exists("*s:Highlight_Matching_Pair")
|
|
24 finish
|
|
25 endif
|
|
26
|
2034
|
27 let s:cpo_save = &cpo
|
757
|
28 set cpo-=C
|
|
29
|
694
|
30 " The function that is invoked (very often) to define a ":match" highlighting
|
|
31 " for any matching paren.
|
|
32 function! s:Highlight_Matching_Pair()
|
|
33 " Remove any previous match.
|
819
|
34 if exists('w:paren_hl_on') && w:paren_hl_on
|
698
|
35 3match none
|
819
|
36 let w:paren_hl_on = 0
|
694
|
37 endif
|
|
38
|
711
|
39 " Avoid that we remove the popup menu.
|
1556
|
40 " Return when there are no colors (looks like the cursor jumps).
|
|
41 if pumvisible() || (&t_Co < 8 && !has("gui_running"))
|
711
|
42 return
|
|
43 endif
|
|
44
|
694
|
45 " 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]
|
967
|
51 let plist = split(&matchpairs, '.\zs[:,]')
|
694
|
52 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().
|
|
67 if i % 2 == 0
|
|
68 let s_flags = 'nW'
|
|
69 let c2 = plist[i + 1]
|
|
70 else
|
|
71 let s_flags = 'nbW'
|
|
72 let c2 = c
|
|
73 let c = plist[i - 1]
|
|
74 endif
|
|
75 if c == '['
|
|
76 let c = '\['
|
|
77 let c2 = '\]'
|
|
78 endif
|
|
79
|
|
80 " Find the match. When it was just before the cursor move it there for a
|
702
|
81 " moment.
|
694
|
82 if before > 0
|
886
|
83 let save_cursor = winsaveview()
|
694
|
84 call cursor(c_lnum, c_col - before)
|
|
85 endif
|
845
|
86
|
|
87 " When not in a string or comment ignore matches inside them.
|
3082
|
88 " We match "escape" for special items, such as lispEscapeSpecial.
|
845
|
89 let s_skip ='synIDattr(synID(line("."), col("."), 0), "name") ' .
|
2662
|
90 \ '=~? "string\\|character\\|singlequote\\|escape\\|comment"'
|
845
|
91 execute 'if' s_skip '| let s_skip = 0 | endif'
|
|
92
|
1556
|
93 " 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
|
1496
|
102 try
|
1556
|
103 " 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)
|
1496
|
106 catch /E118/
|
1556
|
107 " 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
|
1496
|
134 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
|
|
135 endtry
|
845
|
136
|
694
|
137 if before > 0
|
886
|
138 call winrestview(save_cursor)
|
694
|
139 endif
|
|
140
|
|
141 " If a match is found setup match highlighting.
|
1334
|
142 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom
|
698
|
143 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
|
694
|
144 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
|
819
|
145 let w:paren_hl_on = 1
|
694
|
146 endif
|
|
147 endfunction
|
|
148
|
|
149 " Define commands that will disable and enable the plugin.
|
1368
|
150 command! NoMatchParen windo 3match none | unlet! g:loaded_matchparen |
|
|
151 \ au! matchparen
|
|
152 command! DoMatchParen runtime plugin/matchparen.vim | windo doau CursorMoved
|
757
|
153
|
2034
|
154 let &cpo = s:cpo_save
|
|
155 unlet s:cpo_save
|