annotate runtime/plugin/matchparen.vim @ 702:8a99b25de218

updated for version 7.0212
author vimboss
date Thu, 02 Mar 2006 22:43:39 +0000
parents e402b0af6083
children ef3b59af4207
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
694
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
1 " Vim plugin for showing matching parens
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
2 " Maintainer: Bram Moolenaar <Bram@vim.org>
702
8a99b25de218 updated for version 7.0212
vimboss
parents: 698
diff changeset
3 " Last Change: 2006 Mar 02
694
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
4
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
5 " Exit quickly when:
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
6 " - this plugin was already loaded (or disabled)
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
7 " - when 'compatible' is set
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
8 " - the "CursorMoved" autocmd event is not availble.
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
9 if exists("g:loaded_matchparen") || &cp || !exists("##CursorMoved")
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
10 finish
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
11 endif
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
12 let g:loaded_matchparen = 1
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
13
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
14 augroup matchparen
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
15 " Replace all matchparen autocommands
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
16 autocmd! CursorMoved,CursorMovedI * call s:Highlight_Matching_Pair()
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
17 augroup END
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
18
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
19 let s:paren_hl_on = 0
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
20
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
21 " Skip the rest if it was already done.
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
22 if exists("*s:Highlight_Matching_Pair")
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
23 finish
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
24 endif
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
25
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
26 " The function that is invoked (very often) to define a ":match" highlighting
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
27 " for any matching paren.
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
28 function! s:Highlight_Matching_Pair()
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
29 " Remove any previous match.
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
30 if s:paren_hl_on
698
e402b0af6083 updated for version 7.0211
vimboss
parents: 694
diff changeset
31 3match none
694
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
32 let s:paren_hl_on = 0
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
33 endif
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
34
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
35 " Get the character under the cursor and check if it's in 'matchpairs'.
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
36 let c_lnum = line('.')
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
37 let c_col = col('.')
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
38 let before = 0
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
39
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
40 let c = getline(c_lnum)[c_col - 1]
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
41 let plist = split(&matchpairs, ':\|,')
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
42 let i = index(plist, c)
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
43 if i < 0
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
44 " not found, in Insert mode try character before the cursor
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
45 if c_col > 1 && (mode() == 'i' || mode() == 'R')
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
46 let before = 1
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
47 let c = getline(c_lnum)[c_col - 2]
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
48 let i = index(plist, c)
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
49 endif
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
50 if i < 0
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
51 " not found, nothing to do
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
52 return
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
53 endif
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
54 endif
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
55
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
56 " Figure out the arguments for searchpairpos().
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
57 " Restrict the search to visible lines with "stopline".
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
58 if i % 2 == 0
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
59 let s_flags = 'nW'
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
60 let c2 = plist[i + 1]
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
61 let stopline = line('w$')
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
62 else
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
63 let s_flags = 'nbW'
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
64 let c2 = c
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
65 let c = plist[i - 1]
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
66 let stopline = line('w0')
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
67 endif
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
68 if c == '['
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
69 let c = '\['
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
70 let c2 = '\]'
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
71 endif
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
72
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
73 " When not in a string or comment ignore matches inside them.
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
74 let s_skip ='synIDattr(synID(c_lnum, c_col - before, 0), "name") ' .
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
75 \ '=~? "string\\|comment"'
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
76 execute 'if' s_skip '| let s_skip = 0 | endif'
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
77
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
78 " Find the match. When it was just before the cursor move it there for a
702
8a99b25de218 updated for version 7.0212
vimboss
parents: 698
diff changeset
79 " moment.
694
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
80 if before > 0
702
8a99b25de218 updated for version 7.0212
vimboss
parents: 698
diff changeset
81 let save_cursor = getpos('.')
694
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
82 call cursor(c_lnum, c_col - before)
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
83 endif
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
84 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
85 if before > 0
702
8a99b25de218 updated for version 7.0212
vimboss
parents: 698
diff changeset
86 call cursor(save_cursor)
694
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
87 endif
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
88
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
89 " If a match is found setup match highlighting.
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
90 if m_lnum > 0 && m_lnum >= line('w0') && m_lnum <= line('w$')
698
e402b0af6083 updated for version 7.0211
vimboss
parents: 694
diff changeset
91 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
694
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
92 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
93 let s:paren_hl_on = 1
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
94 endif
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
95 endfunction
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
96
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
97 " Define commands that will disable and enable the plugin.
698
e402b0af6083 updated for version 7.0211
vimboss
parents: 694
diff changeset
98 command! NoMatchParen 3match none | unlet! g:loaded_matchparen | au! matchparen
694
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
99 command! DoMatchParen runtime plugin/matchparen.vim | doau CursorMoved