Mercurial > vim
annotate runtime/plugin/matchparen.vim @ 33762:7b26c36e9b3b v9.0.2102
patch 9.0.2102: matchparen highlight not cleared in completion mode
Commit: https://github.com/vim/vim/commit/9588666360e94de3ff58d4bc79aa9148fbf5fc44
Author: Christian Brabandt <cb@256bit.org>
Date: Sun Nov 12 16:55:01 2023 +0100
patch 9.0.2102: matchparen highlight not cleared in completion mode
Problem: matchparen highlight not cleared in completion mode
Solution: Clear matchparen highlighting in completion mode
Remove hard-coded hack in insexpand.c to clear the :3match before
displaying the completion menu.
Add a test for matchparen highlighting. While at it, move all test tests
related to the matchparen plugin into a separate test file.
closes: #13493
closes: #13524
Signed-off-by: Christian Brabandt <cb@256bit.org>
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sun, 12 Nov 2023 17:00:04 +0100 |
parents | 9f55ea4702b1 |
children | 2ee041017eb4 |
rev | line source |
---|---|
694 | 1 " Vim plugin for showing matching parens |
32770
4027cefc2aab
Farewell to Bram and dedicate upcoming Vim 9.1 to him (#12749)
Christian Brabandt <cb@256bit.org>
parents:
32061
diff
changeset
|
2 " Maintainer: The Vim Project <https://github.com/vim/vim> |
33631
9f55ea4702b1
matchparen: do not use hard-coded match id (#13393)
Christian Brabandt <cb@256bit.org>
parents:
32770
diff
changeset
|
3 " Last Change: 2023 Oct 20 |
32770
4027cefc2aab
Farewell to Bram and dedicate upcoming Vim 9.1 to him (#12749)
Christian Brabandt <cb@256bit.org>
parents:
32061
diff
changeset
|
4 " Former Maintainer: Bram Moolenaar <Bram@vim.org> |
694 | 5 |
6 " Exit quickly when: | |
7 " - this plugin was already loaded (or disabled) | |
8 " - when 'compatible' is set | |
29840 | 9 if exists("g:loaded_matchparen") || &cp |
694 | 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 | |
33631
9f55ea4702b1
matchparen: do not use hard-coded match id (#13393)
Christian Brabandt <cb@256bit.org>
parents:
32770
diff
changeset
|
21 let s:has_matchaddpos = exists('*matchaddpos') |
9f55ea4702b1
matchparen: do not use hard-coded match id (#13393)
Christian Brabandt <cb@256bit.org>
parents:
32770
diff
changeset
|
22 |
694 | 23 augroup matchparen |
24 " Replace all matchparen autocommands | |
31271
985a3d6e1d4b
patch 9.0.0969: matchparen highlight is not updated when switching buffers
Bram Moolenaar <Bram@vim.org>
parents:
29840
diff
changeset
|
25 autocmd! CursorMoved,CursorMovedI,WinEnter,BufWinEnter,WinScrolled * call s:Highlight_Matching_Pair() |
985a3d6e1d4b
patch 9.0.0969: matchparen highlight is not updated when switching buffers
Bram Moolenaar <Bram@vim.org>
parents:
29840
diff
changeset
|
26 autocmd! WinLeave,BufLeave * call s:Remove_Matches() |
4232 | 27 if exists('##TextChanged') |
28 autocmd! TextChanged,TextChangedI * call s:Highlight_Matching_Pair() | |
33762
7b26c36e9b3b
patch 9.0.2102: matchparen highlight not cleared in completion mode
Christian Brabandt <cb@256bit.org>
parents:
33631
diff
changeset
|
29 autocmd! TextChangedP * call s:Remove_Matches() |
4232 | 30 endif |
694 | 31 augroup END |
32 | |
33 " Skip the rest if it was already done. | |
34 if exists("*s:Highlight_Matching_Pair") | |
35 finish | |
36 endif | |
37 | |
2034 | 38 let s:cpo_save = &cpo |
757 | 39 set cpo-=C |
40 | |
694 | 41 " The function that is invoked (very often) to define a ":match" highlighting |
42 " for any matching paren. | |
18489 | 43 func s:Highlight_Matching_Pair() |
33631
9f55ea4702b1
matchparen: do not use hard-coded match id (#13393)
Christian Brabandt <cb@256bit.org>
parents:
32770
diff
changeset
|
44 if !exists("w:matchparen_ids") |
9f55ea4702b1
matchparen: do not use hard-coded match id (#13393)
Christian Brabandt <cb@256bit.org>
parents:
32770
diff
changeset
|
45 let w:matchparen_ids = [] |
9f55ea4702b1
matchparen: do not use hard-coded match id (#13393)
Christian Brabandt <cb@256bit.org>
parents:
32770
diff
changeset
|
46 endif |
694 | 47 " Remove any previous match. |
20965 | 48 call s:Remove_Matches() |
694 | 49 |
711 | 50 " Avoid that we remove the popup menu. |
1556 | 51 " Return when there are no colors (looks like the cursor jumps). |
52 if pumvisible() || (&t_Co < 8 && !has("gui_running")) | |
711 | 53 return |
54 endif | |
55 | |
694 | 56 " Get the character under the cursor and check if it's in 'matchpairs'. |
57 let c_lnum = line('.') | |
58 let c_col = col('.') | |
59 let before = 0 | |
60 | |
6051 | 61 let text = getline(c_lnum) |
7422
35af1e06696b
commit https://github.com/vim/vim/commit/c21d67e33c1b42a492e04788cbb14a23a6724e39
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
62 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
|
63 if empty(matches) |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6118
diff
changeset
|
64 let [c_before, c] = ['', ''] |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6118
diff
changeset
|
65 else |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6118
diff
changeset
|
66 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
|
67 endif |
967 | 68 let plist = split(&matchpairs, '.\zs[:,]') |
694 | 69 let i = index(plist, c) |
70 if i < 0 | |
71 " not found, in Insert mode try character before the cursor | |
72 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
|
73 let before = strlen(c_before) |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6118
diff
changeset
|
74 let c = c_before |
694 | 75 let i = index(plist, c) |
76 endif | |
77 if i < 0 | |
78 " not found, nothing to do | |
79 return | |
80 endif | |
81 endif | |
82 | |
83 " Figure out the arguments for searchpairpos(). | |
84 if i % 2 == 0 | |
85 let s_flags = 'nW' | |
86 let c2 = plist[i + 1] | |
87 else | |
88 let s_flags = 'nbW' | |
89 let c2 = c | |
90 let c = plist[i - 1] | |
91 endif | |
92 if c == '[' | |
93 let c = '\[' | |
94 let c2 = '\]' | |
95 endif | |
96 | |
97 " Find the match. When it was just before the cursor move it there for a | |
702 | 98 " moment. |
694 | 99 if before > 0 |
6070
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
100 let has_getcurpos = exists("*getcurpos") |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
101 if has_getcurpos |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
102 " 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
|
103 let save_cursor = getcurpos() |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
104 else |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
105 let save_cursor = winsaveview() |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
106 endif |
694 | 107 call cursor(c_lnum, c_col - before) |
108 endif | |
845 | 109 |
14254
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
110 if !has("syntax") || !exists("g:syntax_on") |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
111 let s_skip = "0" |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
112 else |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
113 " Build an expression that detects whether the current cursor position is |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
114 " in certain syntax types (string, comment, etc.), for use as |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
115 " searchpairpos()'s skip argument. |
24387 | 116 " We match "escape" for special items, such as lispEscapeSpecial, and |
117 " match "symbol" for lispBarSymbol. | |
32061 | 118 let s_skip = 'synstack(".", col("."))' |
119 \ . '->indexof({_, id -> synIDattr(id, "name") =~? ' | |
120 \ . '"string\\|character\\|singlequote\\|escape\\|symbol\\|comment"}) >= 0' | |
14254
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
121 " If executing the expression determines that the cursor is currently in |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
122 " one of the syntax types, then we want searchpairpos() to find the pair |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
123 " within those syntax types (i.e., not skip). Otherwise, the cursor is |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
124 " outside of the syntax types and s_skip should keep its value so we skip |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
125 " any matching pair inside the syntax types. |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
126 " Catch if this throws E363: pattern uses more memory than 'maxmempattern'. |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
127 try |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
128 execute 'if ' . s_skip . ' | let s_skip = "0" | endif' |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
129 catch /^Vim\%((\a\+)\)\=:E363/ |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
130 " We won't find anything, so skip searching, should keep Vim responsive. |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
131 return |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
132 endtry |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
133 endif |
845 | 134 |
1556 | 135 " Limit the search to lines visible in the window. |
136 let stoplinebottom = line('w$') | |
137 let stoplinetop = line('w0') | |
138 if i % 2 == 0 | |
139 let stopline = stoplinebottom | |
140 else | |
141 let stopline = stoplinetop | |
142 endif | |
143 | |
4437 | 144 " Limit the search time to 300 msec to avoid a hang on very long lines. |
145 " This fails when a timeout is not supported. | |
146 if mode() == 'i' || mode() == 'R' | |
147 let timeout = exists("b:matchparen_insert_timeout") ? b:matchparen_insert_timeout : g:matchparen_insert_timeout | |
148 else | |
149 let timeout = exists("b:matchparen_timeout") ? b:matchparen_timeout : g:matchparen_timeout | |
150 endif | |
1496 | 151 try |
4437 | 152 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, timeout) |
1496 | 153 catch /E118/ |
1556 | 154 " Can't use the timeout, restrict the stopline a bit more to avoid taking |
155 " a long time on closed folds and long lines. | |
156 " The "viewable" variables give a range in which we can scroll while | |
157 " keeping the cursor at the same position. | |
158 " adjustedScrolloff accounts for very large numbers of scrolloff. | |
159 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2]) | |
160 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2]) | |
161 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2]) | |
162 " one of these stoplines will be adjusted below, but the current values are | |
163 " minimal boundaries within the current window | |
164 if i % 2 == 0 | |
165 if has("byte_offset") && has("syntax_items") && &smc > 0 | |
166 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2]) | |
167 let stopline = min([bottom_viewable, byte2line(stopbyte)]) | |
168 else | |
169 let stopline = min([bottom_viewable, c_lnum + 100]) | |
170 endif | |
171 let stoplinebottom = stopline | |
172 else | |
173 if has("byte_offset") && has("syntax_items") && &smc > 0 | |
174 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2]) | |
175 let stopline = max([top_viewable, byte2line(stopbyte)]) | |
176 else | |
177 let stopline = max([top_viewable, c_lnum - 100]) | |
178 endif | |
179 let stoplinetop = stopline | |
180 endif | |
1496 | 181 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline) |
182 endtry | |
845 | 183 |
694 | 184 if before > 0 |
6070
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
185 if has_getcurpos |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
186 call setpos('.', save_cursor) |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
187 else |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
188 call winrestview(save_cursor) |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
189 endif |
694 | 190 endif |
191 | |
192 " If a match is found setup match highlighting. | |
1334 | 193 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom |
33631
9f55ea4702b1
matchparen: do not use hard-coded match id (#13393)
Christian Brabandt <cb@256bit.org>
parents:
32770
diff
changeset
|
194 if s:has_matchaddpos |
9f55ea4702b1
matchparen: do not use hard-coded match id (#13393)
Christian Brabandt <cb@256bit.org>
parents:
32770
diff
changeset
|
195 call add(w:matchparen_ids, matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10)) |
5979 | 196 else |
197 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) . | |
198 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/' | |
33631
9f55ea4702b1
matchparen: do not use hard-coded match id (#13393)
Christian Brabandt <cb@256bit.org>
parents:
32770
diff
changeset
|
199 call add(w:matchparen_ids, 3) |
5979 | 200 endif |
819 | 201 let w:paren_hl_on = 1 |
694 | 202 endif |
203 endfunction | |
204 | |
20965 | 205 func s:Remove_Matches() |
206 if exists('w:paren_hl_on') && w:paren_hl_on | |
33631
9f55ea4702b1
matchparen: do not use hard-coded match id (#13393)
Christian Brabandt <cb@256bit.org>
parents:
32770
diff
changeset
|
207 while !empty(w:matchparen_ids) |
9f55ea4702b1
matchparen: do not use hard-coded match id (#13393)
Christian Brabandt <cb@256bit.org>
parents:
32770
diff
changeset
|
208 silent! call remove(w:matchparen_ids, 0)->matchdelete() |
9f55ea4702b1
matchparen: do not use hard-coded match id (#13393)
Christian Brabandt <cb@256bit.org>
parents:
32770
diff
changeset
|
209 endwhile |
20965 | 210 let w:paren_hl_on = 0 |
211 endif | |
212 endfunc | |
213 | |
694 | 214 " Define commands that will disable and enable the plugin. |
18489 | 215 command DoMatchParen call s:DoMatchParen() |
216 command NoMatchParen call s:NoMatchParen() | |
12756
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
217 |
18489 | 218 func s:NoMatchParen() |
12756
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
219 let w = winnr() |
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
220 noau windo silent! call matchdelete(3) |
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
221 unlet! g:loaded_matchparen |
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
222 exe "noau ". w . "wincmd w" |
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
223 au! matchparen |
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
224 endfunc |
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
225 |
18489 | 226 func s:DoMatchParen() |
12756
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
227 runtime plugin/matchparen.vim |
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
228 let w = winnr() |
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
229 silent windo doau CursorMoved |
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
230 exe "noau ". w . "wincmd w" |
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
231 endfunc |
757 | 232 |
2034 | 233 let &cpo = s:cpo_save |
234 unlet s:cpo_save |