blob: 0af2f95f11d78ba73846e57e928777a24aed48b4 [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 Moolenaarbfb2d402006-03-03 22:50:42 +00003" Last Change: 2006 Mar 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
16 autocmd! CursorMoved,CursorMovedI * call s:Highlight_Matching_Pair()
17augroup END
18
19let s:paren_hl_on = 0
20
21" Skip the rest if it was already done.
22if exists("*s:Highlight_Matching_Pair")
23 finish
24endif
25
26" The function that is invoked (very often) to define a ":match" highlighting
27" for any matching paren.
28function! s:Highlight_Matching_Pair()
29 " Remove any previous match.
30 if s:paren_hl_on
Bram Moolenaare1438bb2006-03-01 22:01:55 +000031 3match none
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000032 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
Bram Moolenaarc06ac342006-03-02 22:43:39 +000079 " moment.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000080 if before > 0
Bram Moolenaarc06ac342006-03-02 22:43:39 +000081 let save_cursor = getpos('.')
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000082 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
Bram Moolenaarbfb2d402006-03-03 22:50:42 +000086 call setpos('.', save_cursor)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000087 endif
88
89 " If a match is found setup match highlighting.
90 if m_lnum > 0 && m_lnum >= line('w0') && m_lnum <= line('w$')
Bram Moolenaare1438bb2006-03-01 22:01:55 +000091 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000092 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
93 let s:paren_hl_on = 1
94 endif
95endfunction
96
97" Define commands that will disable and enable the plugin.
Bram Moolenaare1438bb2006-03-01 22:01:55 +000098command! NoMatchParen 3match none | unlet! g:loaded_matchparen | au! matchparen
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000099command! DoMatchParen runtime plugin/matchparen.vim | doau CursorMoved