Mercurial > vim
comparison runtime/plugin/matchparen.vim @ 694:07d199fe02ed v7.0209
updated for version 7.0209
author | vimboss |
---|---|
date | Mon, 27 Feb 2006 23:58:35 +0000 |
parents | |
children | e402b0af6083 |
comparison
equal
deleted
inserted
replaced
693:05dc93b9c61f | 694:07d199fe02ed |
---|---|
1 " Vim plugin for showing matching parens | |
2 " Maintainer: Bram Moolenaar <Bram@vim.org> | |
3 " Last Change: 2006 Feb 27 | |
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 | |
31 match none | |
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 | |
79 " moment. To restore the cursor position use "N|" and when 'virtualedit' | |
80 " is set, cursor() otherwise. | |
81 if before > 0 | |
82 if &ve != '' | |
83 let vcol = virtcol('.') | |
84 endif | |
85 call cursor(c_lnum, c_col - before) | |
86 endif | |
87 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline) | |
88 if before > 0 | |
89 if &ve != '' | |
90 exe 'normal ' . vcol . '|' | |
91 else | |
92 call cursor(0, c_col) | |
93 endif | |
94 endif | |
95 | |
96 " If a match is found setup match highlighting. | |
97 if m_lnum > 0 && m_lnum >= line('w0') && m_lnum <= line('w$') | |
98 exe 'match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) . | |
99 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/' | |
100 let s:paren_hl_on = 1 | |
101 endif | |
102 endfunction | |
103 | |
104 " Define commands that will disable and enable the plugin. | |
105 command! NoMatchParen match none | unlet! g:loaded_matchparen | au! matchparen | |
106 command! DoMatchParen runtime plugin/matchparen.vim | doau CursorMoved |