Mercurial > vim
annotate runtime/plugin/matchparen.vim @ 32045:55926b4f2246 v9.0.1354
patch 9.0.1354: "gr CTRL-G" stays in virtual replace mode
Commit: https://github.com/vim/vim/commit/d6a4ea3aa0d3f4a886ea900e94bf4e8ca8ae8d63
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat Feb 25 14:24:44 2023 +0000
patch 9.0.1354: "gr CTRL-G" stays in virtual replace mode
Problem: "gr CTRL-G" stays in virtual replace mode. (Pierre Ganty)
Solution: Prepend CTRL-V before control characters. (closes https://github.com/vim/vim/issues/12045)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sat, 25 Feb 2023 15:30:02 +0100 |
parents | 15c80d8bc515 |
children | b2412874362f |
rev | line source |
---|---|
694 | 1 " Vim plugin for showing matching parens |
2 " Maintainer: Bram Moolenaar <Bram@vim.org> | |
31383 | 3 " Last Change: 2022 Dec 01 |
694 | 4 |
5 " Exit quickly when: | |
6 " - this plugin was already loaded (or disabled) | |
7 " - when 'compatible' is set | |
29840 | 8 if exists("g:loaded_matchparen") || &cp |
694 | 9 finish |
10 endif | |
11 let g:loaded_matchparen = 1 | |
12 | |
4437 | 13 if !exists("g:matchparen_timeout") |
14 let g:matchparen_timeout = 300 | |
15 endif | |
16 if !exists("g:matchparen_insert_timeout") | |
17 let g:matchparen_insert_timeout = 60 | |
18 endif | |
19 | |
694 | 20 augroup matchparen |
21 " 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
|
22 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
|
23 autocmd! WinLeave,BufLeave * call s:Remove_Matches() |
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. | |
18489 | 39 func s:Highlight_Matching_Pair() |
694 | 40 " Remove any previous match. |
20965 | 41 call s:Remove_Matches() |
694 | 42 |
711 | 43 " Avoid that we remove the popup menu. |
1556 | 44 " Return when there are no colors (looks like the cursor jumps). |
45 if pumvisible() || (&t_Co < 8 && !has("gui_running")) | |
711 | 46 return |
47 endif | |
48 | |
694 | 49 " Get the character under the cursor and check if it's in 'matchpairs'. |
50 let c_lnum = line('.') | |
51 let c_col = col('.') | |
52 let before = 0 | |
53 | |
6051 | 54 let text = getline(c_lnum) |
7422
35af1e06696b
commit https://github.com/vim/vim/commit/c21d67e33c1b42a492e04788cbb14a23a6724e39
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
55 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
|
56 if empty(matches) |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6118
diff
changeset
|
57 let [c_before, c] = ['', ''] |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6118
diff
changeset
|
58 else |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6118
diff
changeset
|
59 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
|
60 endif |
967 | 61 let plist = split(&matchpairs, '.\zs[:,]') |
694 | 62 let i = index(plist, c) |
63 if i < 0 | |
64 " not found, in Insert mode try character before the cursor | |
65 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
|
66 let before = strlen(c_before) |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6118
diff
changeset
|
67 let c = c_before |
694 | 68 let i = index(plist, c) |
69 endif | |
70 if i < 0 | |
71 " not found, nothing to do | |
72 return | |
73 endif | |
74 endif | |
75 | |
76 " Figure out the arguments for searchpairpos(). | |
77 if i % 2 == 0 | |
78 let s_flags = 'nW' | |
79 let c2 = plist[i + 1] | |
80 else | |
81 let s_flags = 'nbW' | |
82 let c2 = c | |
83 let c = plist[i - 1] | |
84 endif | |
85 if c == '[' | |
86 let c = '\[' | |
87 let c2 = '\]' | |
88 endif | |
89 | |
90 " Find the match. When it was just before the cursor move it there for a | |
702 | 91 " moment. |
694 | 92 if before > 0 |
6070
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
93 let has_getcurpos = exists("*getcurpos") |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
94 if has_getcurpos |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
95 " 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
|
96 let save_cursor = getcurpos() |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
97 else |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
98 let save_cursor = winsaveview() |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
99 endif |
694 | 100 call cursor(c_lnum, c_col - before) |
101 endif | |
845 | 102 |
14254
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
103 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
|
104 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
|
105 else |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
106 " 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
|
107 " 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
|
108 " searchpairpos()'s skip argument. |
24387 | 109 " We match "escape" for special items, such as lispEscapeSpecial, and |
110 " match "symbol" for lispBarSymbol. | |
14254
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 = '!empty(filter(map(synstack(line("."), col(".")), ''synIDattr(v:val, "name")''), ' . |
24468 | 112 \ '''v:val =~? "string\\|character\\|singlequote\\|escape\\|symbol\\|comment"''))' |
14254
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
113 " 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
|
114 " 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
|
115 " 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
|
116 " 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
|
117 " 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
|
118 " 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
|
119 try |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
120 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
|
121 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
|
122 " 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
|
123 return |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
124 endtry |
1cfeee3ba4cd
patch 8.1.0143: matchit and matchparen don't handle E363
Christian Brabandt <cb@256bit.org>
parents:
14249
diff
changeset
|
125 endif |
845 | 126 |
1556 | 127 " Limit the search to lines visible in the window. |
128 let stoplinebottom = line('w$') | |
129 let stoplinetop = line('w0') | |
130 if i % 2 == 0 | |
131 let stopline = stoplinebottom | |
132 else | |
133 let stopline = stoplinetop | |
134 endif | |
135 | |
4437 | 136 " Limit the search time to 300 msec to avoid a hang on very long lines. |
137 " This fails when a timeout is not supported. | |
138 if mode() == 'i' || mode() == 'R' | |
139 let timeout = exists("b:matchparen_insert_timeout") ? b:matchparen_insert_timeout : g:matchparen_insert_timeout | |
140 else | |
141 let timeout = exists("b:matchparen_timeout") ? b:matchparen_timeout : g:matchparen_timeout | |
142 endif | |
1496 | 143 try |
4437 | 144 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, timeout) |
1496 | 145 catch /E118/ |
1556 | 146 " Can't use the timeout, restrict the stopline a bit more to avoid taking |
147 " a long time on closed folds and long lines. | |
148 " The "viewable" variables give a range in which we can scroll while | |
149 " keeping the cursor at the same position. | |
150 " adjustedScrolloff accounts for very large numbers of scrolloff. | |
151 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2]) | |
152 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2]) | |
153 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2]) | |
154 " one of these stoplines will be adjusted below, but the current values are | |
155 " minimal boundaries within the current window | |
156 if i % 2 == 0 | |
157 if has("byte_offset") && has("syntax_items") && &smc > 0 | |
158 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2]) | |
159 let stopline = min([bottom_viewable, byte2line(stopbyte)]) | |
160 else | |
161 let stopline = min([bottom_viewable, c_lnum + 100]) | |
162 endif | |
163 let stoplinebottom = stopline | |
164 else | |
165 if has("byte_offset") && has("syntax_items") && &smc > 0 | |
166 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2]) | |
167 let stopline = max([top_viewable, byte2line(stopbyte)]) | |
168 else | |
169 let stopline = max([top_viewable, c_lnum - 100]) | |
170 endif | |
171 let stoplinetop = stopline | |
172 endif | |
1496 | 173 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline) |
174 endtry | |
845 | 175 |
694 | 176 if before > 0 |
6070
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
177 if has_getcurpos |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
178 call setpos('.', save_cursor) |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
179 else |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
180 call winrestview(save_cursor) |
32a77cc160d9
Update runtime files. Make matchparen plugin backwards compatible.
Bram Moolenaar <bram@vim.org>
parents:
6051
diff
changeset
|
181 endif |
694 | 182 endif |
183 | |
184 " If a match is found setup match highlighting. | |
1334 | 185 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom |
5979 | 186 if exists('*matchaddpos') |
187 call matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10, 3) | |
188 else | |
189 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) . | |
190 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/' | |
191 endif | |
819 | 192 let w:paren_hl_on = 1 |
694 | 193 endif |
194 endfunction | |
195 | |
20965 | 196 func s:Remove_Matches() |
197 if exists('w:paren_hl_on') && w:paren_hl_on | |
198 silent! call matchdelete(3) | |
199 let w:paren_hl_on = 0 | |
200 endif | |
201 endfunc | |
202 | |
203 | |
694 | 204 " Define commands that will disable and enable the plugin. |
18489 | 205 command DoMatchParen call s:DoMatchParen() |
206 command NoMatchParen call s:NoMatchParen() | |
12756
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
207 |
18489 | 208 func s:NoMatchParen() |
12756
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
209 let w = winnr() |
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
210 noau windo silent! call matchdelete(3) |
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
211 unlet! g:loaded_matchparen |
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
212 exe "noau ". w . "wincmd w" |
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
213 au! matchparen |
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
214 endfunc |
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
215 |
18489 | 216 func s:DoMatchParen() |
12756
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
217 runtime plugin/matchparen.vim |
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
218 let w = winnr() |
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
219 silent windo doau CursorMoved |
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
220 exe "noau ". w . "wincmd w" |
3b26420fc639
Long overdue runtime update.
Christian Brabandt <cb@256bit.org>
parents:
8148
diff
changeset
|
221 endfunc |
757 | 222 |
2034 | 223 let &cpo = s:cpo_save |
224 unlet s:cpo_save |