Mercurial > vim
annotate runtime/plugin/matchparen.vim @ 6889:9d41289b5512 v7.4.764
patch 7.4.764
Problem: test_increment fails on MS-Windows. (Ken Takata)
Solution: Clear Visual mappings. (Taro Muraoka)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Sun, 28 Jun 2015 19:24:39 +0200 |
parents | 8d361608fe86 |
children | aea5ebf352c4 |
rev | line source |
---|---|
694 | 1 " Vim plugin for showing matching parens |
2 " Maintainer: Bram Moolenaar <Bram@vim.org> | |
6070
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
3 " Last Change: 2014 Jul 19 |
694 | 4 |
5 " Exit quickly when: | |
6 " - this plugin was already loaded (or disabled) | |
7 " - when 'compatible' is set | |
4352 | 8 " - the "CursorMoved" autocmd event is not available. |
694 | 9 if exists("g:loaded_matchparen") || &cp || !exists("##CursorMoved") |
10 finish | |
11 endif | |
12 let g:loaded_matchparen = 1 | |
13 | |
4437 | 14 if !exists("g:matchparen_timeout") |
15 let g:matchparen_timeout = 300 | |
16 endif | |
17 if !exists("g:matchparen_insert_timeout") | |
18 let g:matchparen_insert_timeout = 60 | |
19 endif | |
20 | |
694 | 21 augroup matchparen |
22 " Replace all matchparen autocommands | |
1368 | 23 autocmd! CursorMoved,CursorMovedI,WinEnter * call s:Highlight_Matching_Pair() |
4232 | 24 if exists('##TextChanged') |
25 autocmd! TextChanged,TextChangedI * call s:Highlight_Matching_Pair() | |
26 endif | |
694 | 27 augroup END |
28 | |
29 " Skip the rest if it was already done. | |
30 if exists("*s:Highlight_Matching_Pair") | |
31 finish | |
32 endif | |
33 | |
2034 | 34 let s:cpo_save = &cpo |
757 | 35 set cpo-=C |
36 | |
694 | 37 " The function that is invoked (very often) to define a ":match" highlighting |
38 " for any matching paren. | |
39 function! s:Highlight_Matching_Pair() | |
40 " Remove any previous match. | |
819 | 41 if exists('w:paren_hl_on') && w:paren_hl_on |
5979 | 42 silent! call matchdelete(3) |
819 | 43 let w:paren_hl_on = 0 |
694 | 44 endif |
45 | |
711 | 46 " Avoid that we remove the popup menu. |
1556 | 47 " Return when there are no colors (looks like the cursor jumps). |
48 if pumvisible() || (&t_Co < 8 && !has("gui_running")) | |
711 | 49 return |
50 endif | |
51 | |
694 | 52 " Get the character under the cursor and check if it's in 'matchpairs'. |
53 let c_lnum = line('.') | |
54 let c_col = col('.') | |
55 let before = 0 | |
56 | |
6051 | 57 let text = getline(c_lnum) |
58 let c = text[c_col - 1] | |
967 | 59 let plist = split(&matchpairs, '.\zs[:,]') |
694 | 60 let i = index(plist, c) |
61 if i < 0 | |
62 " not found, in Insert mode try character before the cursor | |
63 if c_col > 1 && (mode() == 'i' || mode() == 'R') | |
64 let before = 1 | |
6051 | 65 let c = text[c_col - 2] |
694 | 66 let i = index(plist, c) |
67 endif | |
68 if i < 0 | |
69 " not found, nothing to do | |
70 return | |
71 endif | |
72 endif | |
73 | |
74 " Figure out the arguments for searchpairpos(). | |
75 if i % 2 == 0 | |
76 let s_flags = 'nW' | |
77 let c2 = plist[i + 1] | |
78 else | |
79 let s_flags = 'nbW' | |
80 let c2 = c | |
81 let c = plist[i - 1] | |
82 endif | |
83 if c == '[' | |
84 let c = '\[' | |
85 let c2 = '\]' | |
86 endif | |
87 | |
88 " Find the match. When it was just before the cursor move it there for a | |
702 | 89 " moment. |
694 | 90 if before > 0 |
6070
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
91 let has_getcurpos = exists("*getcurpos") |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
92 if has_getcurpos |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
93 " getcurpos() is more efficient but doesn't exist before 7.4.313. |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
94 let save_cursor = getcurpos() |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
95 else |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
96 let save_cursor = winsaveview() |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
97 endif |
694 | 98 call cursor(c_lnum, c_col - before) |
99 endif | |
845 | 100 |
6118 | 101 " Build an expression that detects whether the current cursor position is in |
102 " certain syntax types (string, comment, etc.), for use as searchpairpos()'s | |
103 " skip argument. | |
3082 | 104 " We match "escape" for special items, such as lispEscapeSpecial. |
6118 | 105 let s_skip = '!empty(filter(map(synstack(line("."), col(".")), ''synIDattr(v:val, "name")''), ' . |
106 \ '''v:val =~? "string\\|character\\|singlequote\\|escape\\|comment"''))' | |
107 " If executing the expression determines that the cursor is currently in | |
108 " one of the syntax types, then we want searchpairpos() to find the pair | |
109 " within those syntax types (i.e., not skip). Otherwise, the cursor is | |
110 " outside of the syntax types and s_skip should keep its value so we skip any | |
111 " matching pair inside the syntax types. | |
845 | 112 execute 'if' s_skip '| let s_skip = 0 | endif' |
113 | |
1556 | 114 " Limit the search to lines visible in the window. |
115 let stoplinebottom = line('w$') | |
116 let stoplinetop = line('w0') | |
117 if i % 2 == 0 | |
118 let stopline = stoplinebottom | |
119 else | |
120 let stopline = stoplinetop | |
121 endif | |
122 | |
4437 | 123 " Limit the search time to 300 msec to avoid a hang on very long lines. |
124 " This fails when a timeout is not supported. | |
125 if mode() == 'i' || mode() == 'R' | |
126 let timeout = exists("b:matchparen_insert_timeout") ? b:matchparen_insert_timeout : g:matchparen_insert_timeout | |
127 else | |
128 let timeout = exists("b:matchparen_timeout") ? b:matchparen_timeout : g:matchparen_timeout | |
129 endif | |
1496 | 130 try |
4437 | 131 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, timeout) |
1496 | 132 catch /E118/ |
1556 | 133 " Can't use the timeout, restrict the stopline a bit more to avoid taking |
134 " a long time on closed folds and long lines. | |
135 " The "viewable" variables give a range in which we can scroll while | |
136 " keeping the cursor at the same position. | |
137 " adjustedScrolloff accounts for very large numbers of scrolloff. | |
138 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2]) | |
139 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2]) | |
140 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2]) | |
141 " one of these stoplines will be adjusted below, but the current values are | |
142 " minimal boundaries within the current window | |
143 if i % 2 == 0 | |
144 if has("byte_offset") && has("syntax_items") && &smc > 0 | |
145 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2]) | |
146 let stopline = min([bottom_viewable, byte2line(stopbyte)]) | |
147 else | |
148 let stopline = min([bottom_viewable, c_lnum + 100]) | |
149 endif | |
150 let stoplinebottom = stopline | |
151 else | |
152 if has("byte_offset") && has("syntax_items") && &smc > 0 | |
153 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2]) | |
154 let stopline = max([top_viewable, byte2line(stopbyte)]) | |
155 else | |
156 let stopline = max([top_viewable, c_lnum - 100]) | |
157 endif | |
158 let stoplinetop = stopline | |
159 endif | |
1496 | 160 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline) |
161 endtry | |
845 | 162 |
694 | 163 if before > 0 |
6070
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
164 if has_getcurpos |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
165 call setpos('.', save_cursor) |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
166 else |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
167 call winrestview(save_cursor) |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
168 endif |
694 | 169 endif |
170 | |
171 " If a match is found setup match highlighting. | |
1334 | 172 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom |
5979 | 173 if exists('*matchaddpos') |
174 call matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10, 3) | |
175 else | |
176 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) . | |
177 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/' | |
178 endif | |
819 | 179 let w:paren_hl_on = 1 |
694 | 180 endif |
181 endfunction | |
182 | |
183 " Define commands that will disable and enable the plugin. | |
5979 | 184 command! NoMatchParen windo silent! call matchdelete(3) | unlet! g:loaded_matchparen | |
1368 | 185 \ au! matchparen |
186 command! DoMatchParen runtime plugin/matchparen.vim | windo doau CursorMoved | |
757 | 187 |
2034 | 188 let &cpo = s:cpo_save |
189 unlet s:cpo_save |