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