annotate runtime/plugin/matchparen.vim @ 1817:a954a30e4073 v7.2.115

updated for version 7.2-115
author vimboss
date Sat, 21 Feb 2009 21:30:39 +0000
parents 1c597397f006
children 7bc41231fbc7
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>
1556
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
3 " Last Change: 2008 Feb 27
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
1368
048f26eb43a3 updated for version 7.1-082
vimboss
parents: 1334
diff changeset
16 autocmd! CursorMoved,CursorMovedI,WinEnter * call s:Highlight_Matching_Pair()
694
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 " Skip the rest if it was already done.
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
20 if exists("*s:Highlight_Matching_Pair")
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
21 finish
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
22 endif
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
23
757
8b0484fd9119 updated for version 7.0224
vimboss
parents: 711
diff changeset
24 let cpo_save = &cpo
8b0484fd9119 updated for version 7.0224
vimboss
parents: 711
diff changeset
25 set cpo-=C
8b0484fd9119 updated for version 7.0224
vimboss
parents: 711
diff changeset
26
694
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
27 " The function that is invoked (very often) to define a ":match" highlighting
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
28 " for any matching paren.
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
29 function! s:Highlight_Matching_Pair()
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
30 " Remove any previous match.
819
23f82b5d2814 updated for version 7.0c10
vimboss
parents: 818
diff changeset
31 if exists('w:paren_hl_on') && w:paren_hl_on
698
e402b0af6083 updated for version 7.0211
vimboss
parents: 694
diff changeset
32 3match none
819
23f82b5d2814 updated for version 7.0c10
vimboss
parents: 818
diff changeset
33 let w:paren_hl_on = 0
694
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
34 endif
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
35
711
1babf94e0b24 updated for version 7.0214
vimboss
parents: 706
diff changeset
36 " Avoid that we remove the popup menu.
1556
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
37 " Return when there are no colors (looks like the cursor jumps).
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
38 if pumvisible() || (&t_Co < 8 && !has("gui_running"))
711
1babf94e0b24 updated for version 7.0214
vimboss
parents: 706
diff changeset
39 return
1babf94e0b24 updated for version 7.0214
vimboss
parents: 706
diff changeset
40 endif
1babf94e0b24 updated for version 7.0214
vimboss
parents: 706
diff changeset
41
694
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
42 " Get the character under the cursor and check if it's in 'matchpairs'.
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
43 let c_lnum = line('.')
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
44 let c_col = col('.')
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
45 let before = 0
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
46
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
47 let c = getline(c_lnum)[c_col - 1]
967
daa1e34570ff updated for version 7.0-093
vimboss
parents: 920
diff changeset
48 let plist = split(&matchpairs, '.\zs[:,]')
694
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
49 let i = index(plist, c)
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, in Insert mode try character before the cursor
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
52 if c_col > 1 && (mode() == 'i' || mode() == 'R')
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
53 let before = 1
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
54 let c = getline(c_lnum)[c_col - 2]
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
55 let i = index(plist, c)
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
56 endif
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
57 if i < 0
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
58 " not found, nothing to do
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
59 return
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
60 endif
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
61 endif
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
62
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
63 " Figure out the arguments for searchpairpos().
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
64 if i % 2 == 0
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
65 let s_flags = 'nW'
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
66 let c2 = plist[i + 1]
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
67 else
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
68 let s_flags = 'nbW'
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
69 let c2 = c
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
70 let c = plist[i - 1]
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
71 endif
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
72 if c == '['
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
73 let c = '\['
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
74 let c2 = '\]'
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
75 endif
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
76
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
77 " 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
78 " moment.
694
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
79 if before > 0
886
ddfb6f3f3b2b updated for version 7.0-012
vimboss
parents: 845
diff changeset
80 let save_cursor = winsaveview()
694
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
81 call cursor(c_lnum, c_col - before)
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
82 endif
845
0fe7765dcb8e updated for version 7.0f03
vimboss
parents: 819
diff changeset
83
0fe7765dcb8e updated for version 7.0f03
vimboss
parents: 819
diff changeset
84 " When not in a string or comment ignore matches inside them.
0fe7765dcb8e updated for version 7.0f03
vimboss
parents: 819
diff changeset
85 let s_skip ='synIDattr(synID(line("."), col("."), 0), "name") ' .
920
09fcdfde9afc updated for version 7.0-046
vimboss
parents: 886
diff changeset
86 \ '=~? "string\\|character\\|singlequote\\|comment"'
845
0fe7765dcb8e updated for version 7.0f03
vimboss
parents: 819
diff changeset
87 execute 'if' s_skip '| let s_skip = 0 | endif'
0fe7765dcb8e updated for version 7.0f03
vimboss
parents: 819
diff changeset
88
1556
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
89 " Limit the search to lines visible in the window.
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
90 let stoplinebottom = line('w$')
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
91 let stoplinetop = line('w0')
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
92 if i % 2 == 0
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
93 let stopline = stoplinebottom
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
94 else
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
95 let stopline = stoplinetop
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
96 endif
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
97
1496
29c09fa57168 updated for version 7.1-211
vimboss
parents: 1368
diff changeset
98 try
1556
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
99 " Limit the search time to 300 msec to avoid a hang on very long lines.
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
100 " This fails when a timeout is not supported.
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
101 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, 300)
1496
29c09fa57168 updated for version 7.1-211
vimboss
parents: 1368
diff changeset
102 catch /E118/
1556
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
103 " Can't use the timeout, restrict the stopline a bit more to avoid taking
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
104 " a long time on closed folds and long lines.
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
105 " The "viewable" variables give a range in which we can scroll while
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
106 " keeping the cursor at the same position.
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
107 " adjustedScrolloff accounts for very large numbers of scrolloff.
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
108 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2])
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
109 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2])
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
110 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2])
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
111 " one of these stoplines will be adjusted below, but the current values are
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
112 " minimal boundaries within the current window
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
113 if i % 2 == 0
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
114 if has("byte_offset") && has("syntax_items") && &smc > 0
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
115 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2])
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
116 let stopline = min([bottom_viewable, byte2line(stopbyte)])
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
117 else
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
118 let stopline = min([bottom_viewable, c_lnum + 100])
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
119 endif
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
120 let stoplinebottom = stopline
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
121 else
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
122 if has("byte_offset") && has("syntax_items") && &smc > 0
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
123 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2])
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
124 let stopline = max([top_viewable, byte2line(stopbyte)])
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
125 else
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
126 let stopline = max([top_viewable, c_lnum - 100])
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
127 endif
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
128 let stoplinetop = stopline
1c597397f006 updated for version 7.1-269
vimboss
parents: 1496
diff changeset
129 endif
1496
29c09fa57168 updated for version 7.1-211
vimboss
parents: 1368
diff changeset
130 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
29c09fa57168 updated for version 7.1-211
vimboss
parents: 1368
diff changeset
131 endtry
845
0fe7765dcb8e updated for version 7.0f03
vimboss
parents: 819
diff changeset
132
694
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
133 if before > 0
886
ddfb6f3f3b2b updated for version 7.0-012
vimboss
parents: 845
diff changeset
134 call winrestview(save_cursor)
694
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
135 endif
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
136
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
137 " If a match is found setup match highlighting.
1334
f3abb6aec8c8 updated for version 7.1-048
vimboss
parents: 1136
diff changeset
138 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom
698
e402b0af6083 updated for version 7.0211
vimboss
parents: 694
diff changeset
139 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
694
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
140 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
819
23f82b5d2814 updated for version 7.0c10
vimboss
parents: 818
diff changeset
141 let w:paren_hl_on = 1
694
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
142 endif
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
143 endfunction
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
144
07d199fe02ed updated for version 7.0209
vimboss
parents:
diff changeset
145 " Define commands that will disable and enable the plugin.
1368
048f26eb43a3 updated for version 7.1-082
vimboss
parents: 1334
diff changeset
146 command! NoMatchParen windo 3match none | unlet! g:loaded_matchparen |
048f26eb43a3 updated for version 7.1-082
vimboss
parents: 1334
diff changeset
147 \ au! matchparen
048f26eb43a3 updated for version 7.1-082
vimboss
parents: 1334
diff changeset
148 command! DoMatchParen runtime plugin/matchparen.vim | windo doau CursorMoved
757
8b0484fd9119 updated for version 7.0224
vimboss
parents: 711
diff changeset
149
8b0484fd9119 updated for version 7.0224
vimboss
parents: 711
diff changeset
150 let &cpo = cpo_save