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