0
0
mirror of https://github.com/vim/vim.git synced 2025-07-04 23:07:33 -04:00

matchparen: do not use hard-coded match id (#13393)

* matchparen: do not use hard-coded match id

Instead of using the hard-coded match id 3, which may also be used by
other plugins, let the matchparen plugin use whatever ids are
automatically returned when calling matchaddpos().

For backwards-compatibility, keep the `:3match` call, which will still
use the hard-coded id 3 (as mentioned in :h :3match).

closes: #13381

Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Christian Brabandt 2023-10-21 11:06:50 +02:00 committed by GitHub
parent 47416d1a74
commit d3e277f279
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 10 deletions

View File

@ -1,4 +1,4 @@
*pattern.txt* For Vim version 9.0. Last change: 2023 Feb 04 *pattern.txt* For Vim version 9.0. Last change: 2023 Oct 20
VIM REFERENCE MANUAL by Bram Moolenaar VIM REFERENCE MANUAL by Bram Moolenaar
@ -1461,10 +1461,11 @@ Finally, these constructs are unique to Perl:
Just like |:match| above, but set a separate match. Thus Just like |:match| above, but set a separate match. Thus
there can be three matches active at the same time. The match there can be three matches active at the same time. The match
with the lowest number has priority if several match at the with the lowest number has priority if several match at the
same position. same position. It uses the match id 3.
The ":3match" command is used by the |matchparen| plugin. You The ":3match" command is used by (older Vims) |matchparen|
are suggested to use ":match" for manual matching and plugin. You are suggested to use ":match" for manual matching
":2match" for another plugin. and ":2match" for another plugin or even better make use of
the more flexible |matchadd()| (and similar) functions instead.
============================================================================== ==============================================================================
11. Fuzzy matching *fuzzy-matching* 11. Fuzzy matching *fuzzy-matching*

View File

@ -1,6 +1,6 @@
" Vim plugin for showing matching parens " Vim plugin for showing matching parens
" Maintainer: The Vim Project <https://github.com/vim/vim> " Maintainer: The Vim Project <https://github.com/vim/vim>
" Last Change: 2023 Aug 10 " Last Change: 2023 Oct 20
" Former Maintainer: Bram Moolenaar <Bram@vim.org> " Former Maintainer: Bram Moolenaar <Bram@vim.org>
" Exit quickly when: " Exit quickly when:
@ -18,6 +18,8 @@ if !exists("g:matchparen_insert_timeout")
let g:matchparen_insert_timeout = 60 let g:matchparen_insert_timeout = 60
endif endif
let s:has_matchaddpos = exists('*matchaddpos')
augroup matchparen augroup matchparen
" Replace all matchparen autocommands " Replace all matchparen autocommands
autocmd! CursorMoved,CursorMovedI,WinEnter,BufWinEnter,WinScrolled * call s:Highlight_Matching_Pair() autocmd! CursorMoved,CursorMovedI,WinEnter,BufWinEnter,WinScrolled * call s:Highlight_Matching_Pair()
@ -38,6 +40,9 @@ set cpo-=C
" The function that is invoked (very often) to define a ":match" highlighting " The function that is invoked (very often) to define a ":match" highlighting
" for any matching paren. " for any matching paren.
func s:Highlight_Matching_Pair() func s:Highlight_Matching_Pair()
if !exists("w:matchparen_ids")
let w:matchparen_ids = []
endif
" Remove any previous match. " Remove any previous match.
call s:Remove_Matches() call s:Remove_Matches()
@ -185,11 +190,12 @@ func s:Highlight_Matching_Pair()
" If a match is found setup match highlighting. " If a match is found setup match highlighting.
if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom
if exists('*matchaddpos') if s:has_matchaddpos
call matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10, 3) call add(w:matchparen_ids, matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10))
else else
exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) . exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
\ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/' \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
call add(w:matchparen_ids, 3)
endif endif
let w:paren_hl_on = 1 let w:paren_hl_on = 1
endif endif
@ -197,12 +203,13 @@ endfunction
func s:Remove_Matches() func s:Remove_Matches()
if exists('w:paren_hl_on') && w:paren_hl_on if exists('w:paren_hl_on') && w:paren_hl_on
silent! call matchdelete(3) while !empty(w:matchparen_ids)
silent! call remove(w:matchparen_ids, 0)->matchdelete()
endwhile
let w:paren_hl_on = 0 let w:paren_hl_on = 0
endif endif
endfunc endfunc
" Define commands that will disable and enable the plugin. " Define commands that will disable and enable the plugin.
command DoMatchParen call s:DoMatchParen() command DoMatchParen call s:DoMatchParen()
command NoMatchParen call s:NoMatchParen() command NoMatchParen call s:NoMatchParen()