| |
| |
| |
| |
|
|
| |
| |
| |
| |
| if exists("g:loaded_matchparen") || &cp || |
| \ exists(":defer") != 2 |
| finish |
| endif |
| let g:loaded_matchparen = 1 |
|
|
| 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 |
|
|
| augroup matchparen |
| |
| 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() |
| autocmd! TextChanged,TextChangedI * call s:Highlight_Matching_Pair() |
| autocmd! TextChangedP * call s:Remove_Matches() |
| augroup END |
|
|
| |
| if exists("*s:Highlight_Matching_Pair") |
| finish |
| endif |
|
|
| let s:cpo_save = &cpo |
| set cpo-=C |
|
|
| " The function that is invoked (very often) to define a ":match |
| |
| func s:Highlight_Matching_Pair() |
| if !exists("w:matchparen_ids") |
| let w:matchparen_ids = [] |
| endif |
| |
| call s:Remove_Matches() |
|
|
| |
| |
| if pumvisible() || (&t_Co < 8 && !has("gui_running")) |
| return |
| endif |
|
|
| |
| let c_lnum = line('.') |
| let c_col = col('.') |
| let before = 0 |
|
|
| 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) |
| let plist = split(&matchpairs, '.\zs[:,]') |
| let i = index(plist, c) |
| if i < 0 |
| |
| if c_col > 1 && (mode() == 'i' || mode() == 'R') |
| let before = strlen(c_before) |
| let c = c_before |
| let i = index(plist, c) |
| endif |
| if i < 0 |
| |
| return |
| endif |
| endif |
|
|
| |
| 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 |
|
|
| |
| |
| if before > 0 |
| let save_cursor = getcurpos() |
| call cursor(c_lnum, c_col - before) |
| defer setpos('.', save_cursor) |
| endif |
|
|
| if !has("syntax") || !exists("g:syntax_on") |
| let s_skip = "0" |
| elseif exists("b:ts_highlight") && &syntax != 'on' |
| let s_skip = "match(v:lua.vim.treesitter.get_captures_at_cursor(), '" |
| \ .. 'string\|character\|singlequote\|escape\|symbol\|comment' |
| \ .. "') != -1" |
| else |
| |
| |
| " case statement: "case $var in foobar) |
| |
| |
| |
| if ['sh']->index(&filetype) >= 0 && |
| \ synstack(".", col("."))->indexof({_, id -> synIDattr(id, "name") |
| \ =~? "shSnglCase"}) >= 0 |
| return |
| endif |
| |
| |
| |
| " We match "escape |
| " match "symbol |
| let s_skip = 'synstack(".", col("."))' |
| \ . '->indexof({_, id -> synIDattr(id, "name") =~? ' |
| \ . '"string\\|character\\|singlequote\\|escape\\|symbol\\|comment"}) >= 0' |
| |
| |
| |
| |
| |
| |
| try |
| execute 'if ' . s_skip . ' | let s_skip = "0" | endif' |
| catch /^Vim\%((\a\+)\)\=:E363/ |
| |
| return |
| endtry |
| endif |
|
|
| |
| let stoplinebottom = line('w$') |
| let stoplinetop = line('w0') |
| if i % 2 == 0 |
| let stopline = stoplinebottom |
| else |
| let stopline = stoplinetop |
| endif |
|
|
| |
| |
| 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 |
| try |
| let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, timeout) |
| catch /E118/ |
| |
| |
| " The "viewable |
| |
| |
| 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]) |
| |
| |
| 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 |
| let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline) |
| endtry |
|
|
| |
| if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom |
| 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 |
| let w:paren_hl_on = 1 |
| endif |
| endfunction |
|
|
| 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 |
| let w:paren_hl_on = 0 |
| endif |
| endfunc |
|
|
| |
| command DoMatchParen call s:DoMatchParen() |
| command NoMatchParen call s:NoMatchParen() |
|
|
| func s:NoMatchParen() |
| let w = winnr() |
| noau windo call s:Remove_Matches() |
| unlet! g:loaded_matchparen |
| exe "noau ". w . "wincmd w" |
| au! matchparen |
| endfunc |
|
|
| func s:DoMatchParen() |
| runtime plugin/matchparen.vim |
| let w = winnr() |
| silent windo doau CursorMoved |
| exe "noau ". w . "wincmd w" |
| endfunc |
|
|
| let &cpo = s:cpo_save |
| unlet s:cpo_save |
|
|