blob: 57505179cc80ee9c80ac5369fb0624a70b104f36 [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 Moolenaare2f98b92006-03-29 21:18:24 +00003" Last Change: 2006 Mar 29
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
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +000026let cpo_save = &cpo
27set cpo-=C
28
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000029" The function that is invoked (very often) to define a ":match" highlighting
30" for any matching paren.
31function! s:Highlight_Matching_Pair()
32 " Remove any previous match.
33 if s:paren_hl_on
Bram Moolenaare1438bb2006-03-01 22:01:55 +000034 3match none
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000035 let s:paren_hl_on = 0
36 endif
37
Bram Moolenaar36fc5352006-03-04 21:49:37 +000038 " Avoid that we remove the popup menu.
39 if pumvisible()
40 return
41 endif
42
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000043 " 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".
Bram Moolenaare2f98b92006-03-29 21:18:24 +000066 " And avoid searching very far (e.g., for closed folds)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000067 if i % 2 == 0
68 let s_flags = 'nW'
69 let c2 = plist[i + 1]
70 let stopline = line('w$')
Bram Moolenaare2f98b92006-03-29 21:18:24 +000071 if stopline > c_lnum + 100
72 let stopline = c_lnu + 100
73 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000074 else
75 let s_flags = 'nbW'
76 let c2 = c
77 let c = plist[i - 1]
78 let stopline = line('w0')
Bram Moolenaare2f98b92006-03-29 21:18:24 +000079 if stopline < c_lnum - 100
80 let stopline = c_lnu - 100
81 endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000082 endif
83 if c == '['
84 let c = '\['
85 let c2 = '\]'
86 endif
87
88 " When not in a string or comment ignore matches inside them.
89 let s_skip ='synIDattr(synID(c_lnum, c_col - before, 0), "name") ' .
90 \ '=~? "string\\|comment"'
91 execute 'if' s_skip '| let s_skip = 0 | endif'
92
93 " Find the match. When it was just before the cursor move it there for a
Bram Moolenaarc06ac342006-03-02 22:43:39 +000094 " moment.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000095 if before > 0
Bram Moolenaarc06ac342006-03-02 22:43:39 +000096 let save_cursor = getpos('.')
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000097 call cursor(c_lnum, c_col - before)
98 endif
99 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
100 if before > 0
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000101 call setpos('.', save_cursor)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000102 endif
103
104 " If a match is found setup match highlighting.
105 if m_lnum > 0 && m_lnum >= line('w0') && m_lnum <= line('w$')
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000106 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000107 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
108 let s:paren_hl_on = 1
109 endif
110endfunction
111
112" Define commands that will disable and enable the plugin.
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000113command! NoMatchParen 3match none | unlet! g:loaded_matchparen | au! matchparen
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000114command! DoMatchParen runtime plugin/matchparen.vim | doau CursorMoved
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +0000115
116let &cpo = cpo_save