1
0
Fork 0
mirror of https://github.com/vim/vim synced 2025-03-23 02:05:11 +01:00
vim/runtime/plugin/matchparen.vim

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

243 lines
7.8 KiB
VimL
Raw Normal View History

2006-02-27 23:58:35 +00:00
" Vim plugin for showing matching parens
" Maintainer: The Vim Project <https://github.com/vim/vim>
" Last Change: 2024 May 18
" Former Maintainer: Bram Moolenaar <Bram@vim.org>
2006-02-27 23:58:35 +00:00
" Exit quickly when:
" - this plugin was already loaded (or disabled)
" - when 'compatible' is set
2022-08-24 18:30:14 +01:00
if exists("g:loaded_matchparen") || &cp
2006-02-27 23:58:35 +00:00
finish
endif
let g:loaded_matchparen = 1
2013-05-17 18:14:19 +02:00
if !exists("g:matchparen_timeout")
let g:matchparen_timeout = 300
endif
if !exists("g:matchparen_insert_timeout")
let g:matchparen_insert_timeout = 60
endif
if !exists("g:matchparen_disable_cursor_hl")
let g:matchparen_disable_cursor_hl = 0
endif
2013-05-17 18:14:19 +02:00
let s:has_matchaddpos = exists('*matchaddpos')
2006-02-27 23:58:35 +00:00
augroup matchparen
" Replace all matchparen autocommands
autocmd! CursorMoved,CursorMovedI,WinEnter,WinScrolled * call s:Highlight_Matching_Pair()
autocmd! BufWinEnter * autocmd SafeState * ++once call s:Highlight_Matching_Pair()
autocmd! WinLeave,BufLeave * call s:Remove_Matches()
if exists('##TextChanged')
autocmd! TextChanged,TextChangedI * call s:Highlight_Matching_Pair()
autocmd! TextChangedP * call s:Remove_Matches()
endif
2006-02-27 23:58:35 +00:00
augroup END
" Skip the rest if it was already done.
if exists("*s:Highlight_Matching_Pair")
finish
endif
2010-01-06 20:54:52 +01:00
let s:cpo_save = &cpo
2006-03-14 22:55:34 +00:00
set cpo-=C
2006-02-27 23:58:35 +00:00
" The function that is invoked (very often) to define a ":match" highlighting
" for any matching paren.
2019-11-02 14:09:23 +01:00
func s:Highlight_Matching_Pair()
if !exists("w:matchparen_ids")
let w:matchparen_ids = []
endif
2006-02-27 23:58:35 +00:00
" Remove any previous match.
2020-06-21 22:12:03 +02:00
call s:Remove_Matches()
2006-02-27 23:58:35 +00:00
2006-03-04 21:49:37 +00:00
" Avoid that we remove the popup menu.
2008-03-09 15:45:53 +00:00
" Return when there are no colors (looks like the cursor jumps).
if pumvisible() || (&t_Co < 8 && !has("gui_running"))
2006-03-04 21:49:37 +00:00
return
endif
2006-02-27 23:58:35 +00:00
" Get the character under the cursor and check if it's in 'matchpairs'.
let c_lnum = line('.')
let c_col = col('.')
let before = 0
2014-07-10 22:01:47 +02:00
let text = getline(c_lnum)
let c_before = text->strpart(0, c_col - 1)->slice(-1)
let c = text->strpart(c_col - 1)->slice(0, 1)
2006-09-09 11:37:51 +00:00
let plist = split(&matchpairs, '.\zs[:,]')
2006-02-27 23:58:35 +00:00
let i = index(plist, c)
if i < 0
" not found, in Insert mode try character before the cursor
if c_col > 1 && (mode() == 'i' || mode() == 'R')
2015-12-29 19:10:25 +01:00
let before = strlen(c_before)
let c = c_before
2006-02-27 23:58:35 +00:00
let i = index(plist, c)
endif
if i < 0
" not found, nothing to do
return
endif
endif
" Figure out the arguments for searchpairpos().
if i % 2 == 0
let s_flags = 'nW'
let c2 = plist[i + 1]
else
let s_flags = 'nbW'
let c2 = c
let c = plist[i - 1]
endif
if c == '['
let c = '\['
let c2 = '\]'
endif
" Find the match. When it was just before the cursor move it there for a
2006-03-02 22:43:39 +00:00
" moment.
2006-02-27 23:58:35 +00:00
if before > 0
let has_getcurpos = exists("*getcurpos")
if has_getcurpos
" getcurpos() is more efficient but doesn't exist before 7.4.313.
let save_cursor = getcurpos()
else
let save_cursor = winsaveview()
endif
2006-02-27 23:58:35 +00:00
call cursor(c_lnum, c_col - before)
endif
2006-04-27 21:40:34 +00:00
if !has("syntax") || !exists("g:syntax_on")
let s_skip = "0"
else
" Build an expression that detects whether the current cursor position is
" in certain syntax types (string, comment, etc.), for use as
" searchpairpos()'s skip argument.
2021-04-07 21:07:20 +02:00
" We match "escape" for special items, such as lispEscapeSpecial, and
" match "symbol" for lispBarSymbol.
2023-02-27 15:49:53 +00:00
let s_skip = 'synstack(".", col("."))'
\ . '->indexof({_, id -> synIDattr(id, "name") =~? '
\ . '"string\\|character\\|singlequote\\|escape\\|symbol\\|comment"}) >= 0'
" If executing the expression determines that the cursor is currently in
" one of the syntax types, then we want searchpairpos() to find the pair
" within those syntax types (i.e., not skip). Otherwise, the cursor is
" outside of the syntax types and s_skip should keep its value so we skip
" any matching pair inside the syntax types.
" Catch if this throws E363: pattern uses more memory than 'maxmempattern'.
try
execute 'if ' . s_skip . ' | let s_skip = "0" | endif'
catch /^Vim\%((\a\+)\)\=:E363/
" We won't find anything, so skip searching, should keep Vim responsive.
return
endtry
endif
2006-04-27 21:40:34 +00:00
2008-03-09 15:45:53 +00:00
" Limit the search to lines visible in the window.
let stoplinebottom = line('w$')
let stoplinetop = line('w0')
if i % 2 == 0
let stopline = stoplinebottom
else
let stopline = stoplinetop
endif
2013-05-17 18:14:19 +02:00
" Limit the search time to 300 msec to avoid a hang on very long lines.
" This fails when a timeout is not supported.
if mode() == 'i' || mode() == 'R'
let timeout = exists("b:matchparen_insert_timeout") ? b:matchparen_insert_timeout : g:matchparen_insert_timeout
else
let timeout = exists("b:matchparen_timeout") ? b:matchparen_timeout : g:matchparen_timeout
endif
2008-01-06 19:07:36 +00:00
try
2013-05-17 18:14:19 +02:00
let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, timeout)
2008-01-06 19:07:36 +00:00
catch /E118/
2008-03-09 15:45:53 +00:00
" Can't use the timeout, restrict the stopline a bit more to avoid taking
" a long time on closed folds and long lines.
" The "viewable" variables give a range in which we can scroll while
" keeping the cursor at the same position.
" adjustedScrolloff accounts for very large numbers of scrolloff.
let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2])
let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2])
let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2])
" one of these stoplines will be adjusted below, but the current values are
" minimal boundaries within the current window
if i % 2 == 0
if has("byte_offset") && has("syntax_items") && &smc > 0
let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2])
let stopline = min([bottom_viewable, byte2line(stopbyte)])
else
let stopline = min([bottom_viewable, c_lnum + 100])
endif
let stoplinebottom = stopline
else
if has("byte_offset") && has("syntax_items") && &smc > 0
let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2])
let stopline = max([top_viewable, byte2line(stopbyte)])
else
let stopline = max([top_viewable, c_lnum - 100])
endif
let stoplinetop = stopline
endif
2008-01-06 19:07:36 +00:00
let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
endtry
2006-04-27 21:40:34 +00:00
2006-02-27 23:58:35 +00:00
if before > 0
if has_getcurpos
call setpos('.', save_cursor)
else
call winrestview(save_cursor)
endif
2006-02-27 23:58:35 +00:00
endif
" If a match is found setup match highlighting.
2007-08-02 21:00:50 +00:00
if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom
if s:has_matchaddpos
if !g:matchparen_disable_cursor_hl
call add(w:matchparen_ids, matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10))
else
call add(w:matchparen_ids, matchaddpos('MatchParen', [[m_lnum, m_col]], 10))
endif
else
if !g:matchparen_disable_cursor_hl
exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
\ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
else
exe '3match MatchParen /\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
endif
call add(w:matchparen_ids, 3)
endif
2006-04-05 20:41:53 +00:00
let w:paren_hl_on = 1
2006-02-27 23:58:35 +00:00
endif
endfunction
2020-06-21 22:12:03 +02:00
func s:Remove_Matches()
if exists('w:paren_hl_on') && w:paren_hl_on
while !empty(w:matchparen_ids)
silent! call remove(w:matchparen_ids, 0)->matchdelete()
endwhile
2020-06-21 22:12:03 +02:00
let w:paren_hl_on = 0
endif
endfunc
2006-02-27 23:58:35 +00:00
" Define commands that will disable and enable the plugin.
2019-11-02 14:09:23 +01:00
command DoMatchParen call s:DoMatchParen()
command NoMatchParen call s:NoMatchParen()
2017-11-02 22:58:42 +01:00
2019-11-02 14:09:23 +01:00
func s:NoMatchParen()
2017-11-02 22:58:42 +01:00
let w = winnr()
noau windo call s:Remove_Matches()
2017-11-02 22:58:42 +01:00
unlet! g:loaded_matchparen
exe "noau ". w . "wincmd w"
au! matchparen
endfunc
2019-11-02 14:09:23 +01:00
func s:DoMatchParen()
2017-11-02 22:58:42 +01:00
runtime plugin/matchparen.vim
let w = winnr()
silent windo doau CursorMoved
exe "noau ". w . "wincmd w"
endfunc
2006-03-14 22:55:34 +00:00
2010-01-06 20:54:52 +01:00
let &cpo = s:cpo_save
unlet s:cpo_save