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