Mercurial > vim
annotate runtime/plugin/matchparen.vim @ 9399:43b8570abbec v7.4.1981
commit https://github.com/vim/vim/commit/ee2615af64fdcee87d8e4b13b65356e77fbd969b
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat Jul 2 18:25:34 2016 +0200
patch 7.4.1981
Problem: No testing for Farsi code.
Solution: Add a minimal test. Clean up Farsi code.
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sat, 02 Jul 2016 18:30:05 +0200 |
parents | f5da459c5698 |
children | 3b26420fc639 |
rev | line source |
---|---|
694 | 1 " Vim plugin for showing matching parens |
2 " Maintainer: Bram Moolenaar <Bram@vim.org> | |
8148
f5da459c5698
commit https://github.com/vim/vim/commit/e0fa3742ead676a3074a10edadbc955e1a89153d
Christian Brabandt <cb@256bit.org>
parents:
8061
diff
changeset
|
3 " Last Change: 2016 Feb 16 |
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) |
7422
35af1e06696b
commit https://github.com/vim/vim/commit/c21d67e33c1b42a492e04788cbb14a23a6724e39
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
58 let matches = matchlist(text, '\(.\)\=\%'.c_col.'c\(.\=\)') |
7384
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6118
diff
changeset
|
59 if empty(matches) |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6118
diff
changeset
|
60 let [c_before, c] = ['', ''] |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6118
diff
changeset
|
61 else |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6118
diff
changeset
|
62 let [c_before, c] = matches[1:2] |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6118
diff
changeset
|
63 endif |
967 | 64 let plist = split(&matchpairs, '.\zs[:,]') |
694 | 65 let i = index(plist, c) |
66 if i < 0 | |
67 " not found, in Insert mode try character before the cursor | |
68 if c_col > 1 && (mode() == 'i' || mode() == 'R') | |
7384
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6118
diff
changeset
|
69 let before = strlen(c_before) |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6118
diff
changeset
|
70 let c = c_before |
694 | 71 let i = index(plist, c) |
72 endif | |
73 if i < 0 | |
74 " not found, nothing to do | |
75 return | |
76 endif | |
77 endif | |
78 | |
79 " Figure out the arguments for searchpairpos(). | |
80 if i % 2 == 0 | |
81 let s_flags = 'nW' | |
82 let c2 = plist[i + 1] | |
83 else | |
84 let s_flags = 'nbW' | |
85 let c2 = c | |
86 let c = plist[i - 1] | |
87 endif | |
88 if c == '[' | |
89 let c = '\[' | |
90 let c2 = '\]' | |
91 endif | |
92 | |
93 " Find the match. When it was just before the cursor move it there for a | |
702 | 94 " moment. |
694 | 95 if before > 0 |
6070
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
96 let has_getcurpos = exists("*getcurpos") |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
97 if has_getcurpos |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
98 " 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
|
99 let save_cursor = getcurpos() |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
100 else |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
101 let save_cursor = winsaveview() |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
102 endif |
694 | 103 call cursor(c_lnum, c_col - before) |
104 endif | |
845 | 105 |
6118 | 106 " Build an expression that detects whether the current cursor position is in |
107 " certain syntax types (string, comment, etc.), for use as searchpairpos()'s | |
108 " skip argument. | |
3082 | 109 " We match "escape" for special items, such as lispEscapeSpecial. |
6118 | 110 let s_skip = '!empty(filter(map(synstack(line("."), col(".")), ''synIDattr(v:val, "name")''), ' . |
111 \ '''v:val =~? "string\\|character\\|singlequote\\|escape\\|comment"''))' | |
112 " If executing the expression determines that the cursor is currently in | |
113 " one of the syntax types, then we want searchpairpos() to find the pair | |
114 " within those syntax types (i.e., not skip). Otherwise, the cursor is | |
115 " outside of the syntax types and s_skip should keep its value so we skip any | |
116 " matching pair inside the syntax types. | |
845 | 117 execute 'if' s_skip '| let s_skip = 0 | endif' |
118 | |
1556 | 119 " Limit the search to lines visible in the window. |
120 let stoplinebottom = line('w$') | |
121 let stoplinetop = line('w0') | |
122 if i % 2 == 0 | |
123 let stopline = stoplinebottom | |
124 else | |
125 let stopline = stoplinetop | |
126 endif | |
127 | |
4437 | 128 " Limit the search time to 300 msec to avoid a hang on very long lines. |
129 " This fails when a timeout is not supported. | |
130 if mode() == 'i' || mode() == 'R' | |
131 let timeout = exists("b:matchparen_insert_timeout") ? b:matchparen_insert_timeout : g:matchparen_insert_timeout | |
132 else | |
133 let timeout = exists("b:matchparen_timeout") ? b:matchparen_timeout : g:matchparen_timeout | |
134 endif | |
1496 | 135 try |
4437 | 136 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, timeout) |
1496 | 137 catch /E118/ |
1556 | 138 " Can't use the timeout, restrict the stopline a bit more to avoid taking |
139 " a long time on closed folds and long lines. | |
140 " The "viewable" variables give a range in which we can scroll while | |
141 " keeping the cursor at the same position. | |
142 " adjustedScrolloff accounts for very large numbers of scrolloff. | |
143 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2]) | |
144 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2]) | |
145 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2]) | |
146 " one of these stoplines will be adjusted below, but the current values are | |
147 " minimal boundaries within the current window | |
148 if i % 2 == 0 | |
149 if has("byte_offset") && has("syntax_items") && &smc > 0 | |
150 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2]) | |
151 let stopline = min([bottom_viewable, byte2line(stopbyte)]) | |
152 else | |
153 let stopline = min([bottom_viewable, c_lnum + 100]) | |
154 endif | |
155 let stoplinebottom = stopline | |
156 else | |
157 if has("byte_offset") && has("syntax_items") && &smc > 0 | |
158 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2]) | |
159 let stopline = max([top_viewable, byte2line(stopbyte)]) | |
160 else | |
161 let stopline = max([top_viewable, c_lnum - 100]) | |
162 endif | |
163 let stoplinetop = stopline | |
164 endif | |
1496 | 165 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline) |
166 endtry | |
845 | 167 |
694 | 168 if before > 0 |
6070
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
169 if has_getcurpos |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
170 call setpos('.', save_cursor) |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
171 else |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
172 call winrestview(save_cursor) |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
173 endif |
694 | 174 endif |
175 | |
176 " If a match is found setup match highlighting. | |
1334 | 177 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom |
5979 | 178 if exists('*matchaddpos') |
179 call matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10, 3) | |
180 else | |
181 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) . | |
182 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/' | |
183 endif | |
819 | 184 let w:paren_hl_on = 1 |
694 | 185 endif |
186 endfunction | |
187 | |
188 " Define commands that will disable and enable the plugin. | |
5979 | 189 command! NoMatchParen windo silent! call matchdelete(3) | unlet! g:loaded_matchparen | |
1368 | 190 \ au! matchparen |
191 command! DoMatchParen runtime plugin/matchparen.vim | windo doau CursorMoved | |
757 | 192 |
2034 | 193 let &cpo = s:cpo_save |
194 unlet s:cpo_save |