694
|
1 " Vim plugin for showing matching parens
|
|
2 " Maintainer: Bram Moolenaar <Bram@vim.org>
|
757
|
3 " Last Change: 2006 Mar 14
|
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
|
|
16 autocmd! CursorMoved,CursorMovedI * call s:Highlight_Matching_Pair()
|
|
17 augroup END
|
|
18
|
|
19 let s:paren_hl_on = 0
|
|
20
|
|
21 " Skip the rest if it was already done.
|
|
22 if exists("*s:Highlight_Matching_Pair")
|
|
23 finish
|
|
24 endif
|
|
25
|
757
|
26 let cpo_save = &cpo
|
|
27 set cpo-=C
|
|
28
|
694
|
29 " The function that is invoked (very often) to define a ":match" highlighting
|
|
30 " for any matching paren.
|
|
31 function! s:Highlight_Matching_Pair()
|
|
32 " Remove any previous match.
|
|
33 if s:paren_hl_on
|
698
|
34 3match none
|
694
|
35 let s:paren_hl_on = 0
|
|
36 endif
|
|
37
|
711
|
38 " Avoid that we remove the popup menu.
|
|
39 if pumvisible()
|
|
40 return
|
|
41 endif
|
|
42
|
694
|
43 " Get the character under the cursor and check if it's in 'matchpairs'.
|
|
44 let c_lnum = line('.')
|
|
45 let c_col = col('.')
|
|
46 let before = 0
|
|
47
|
|
48 let c = getline(c_lnum)[c_col - 1]
|
|
49 let plist = split(&matchpairs, ':\|,')
|
|
50 let i = index(plist, c)
|
|
51 if i < 0
|
|
52 " not found, in Insert mode try character before the cursor
|
|
53 if c_col > 1 && (mode() == 'i' || mode() == 'R')
|
|
54 let before = 1
|
|
55 let c = getline(c_lnum)[c_col - 2]
|
|
56 let i = index(plist, c)
|
|
57 endif
|
|
58 if i < 0
|
|
59 " not found, nothing to do
|
|
60 return
|
|
61 endif
|
|
62 endif
|
|
63
|
|
64 " Figure out the arguments for searchpairpos().
|
|
65 " Restrict the search to visible lines with "stopline".
|
|
66 if i % 2 == 0
|
|
67 let s_flags = 'nW'
|
|
68 let c2 = plist[i + 1]
|
|
69 let stopline = line('w$')
|
|
70 else
|
|
71 let s_flags = 'nbW'
|
|
72 let c2 = c
|
|
73 let c = plist[i - 1]
|
|
74 let stopline = line('w0')
|
|
75 endif
|
|
76 if c == '['
|
|
77 let c = '\['
|
|
78 let c2 = '\]'
|
|
79 endif
|
|
80
|
|
81 " When not in a string or comment ignore matches inside them.
|
|
82 let s_skip ='synIDattr(synID(c_lnum, c_col - before, 0), "name") ' .
|
|
83 \ '=~? "string\\|comment"'
|
|
84 execute 'if' s_skip '| let s_skip = 0 | endif'
|
|
85
|
|
86 " Find the match. When it was just before the cursor move it there for a
|
702
|
87 " moment.
|
694
|
88 if before > 0
|
702
|
89 let save_cursor = getpos('.')
|
694
|
90 call cursor(c_lnum, c_col - before)
|
|
91 endif
|
|
92 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
|
|
93 if before > 0
|
706
|
94 call setpos('.', save_cursor)
|
694
|
95 endif
|
|
96
|
|
97 " If a match is found setup match highlighting.
|
|
98 if m_lnum > 0 && m_lnum >= line('w0') && m_lnum <= line('w$')
|
698
|
99 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
|
694
|
100 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
|
|
101 let s:paren_hl_on = 1
|
|
102 endif
|
|
103 endfunction
|
|
104
|
|
105 " Define commands that will disable and enable the plugin.
|
698
|
106 command! NoMatchParen 3match none | unlet! g:loaded_matchparen | au! matchparen
|
694
|
107 command! DoMatchParen runtime plugin/matchparen.vim | doau CursorMoved
|
757
|
108
|
|
109 let &cpo = cpo_save
|