robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
ef84ec69aa
master
1" Vim plugin for showing matching parens 2" Maintainer: The Vim Project <https://github.com/vim/vim> 3" Last Change: 2023 Oct 20 4" Former Maintainer: Bram Moolenaar <Bram@vim.org> 5 6" Exit quickly when: 7" - this plugin was already loaded (or disabled) 8" - when 'compatible' is set 9if exists ( "g:loaded_matchparen" ) || & cp 10finish 11endif 12let g: loaded_matchparen = 1 13 14if ! exists ( "g:matchparen_timeout" ) 15let g: matchparen_timeout = 300 16endif 17if ! exists ( "g:matchparen_insert_timeout" ) 18let g: matchparen_insert_timeout = 60 19endif 20 21let s: has_matchaddpos = exists ( '*matchaddpos' ) 22 23augroup matchparen 24" Replace all matchparen autocommands 25autocmd ! CursorMoved , CursorMovedI , WinEnter , BufWinEnter , WinScrolled * call s: Highlight_Matching_Pair () 26autocmd ! WinLeave , BufLeave * call s: Remove_Matches () 27if exists ( '##TextChanged' ) 28autocmd ! TextChanged , TextChangedI * call s: Highlight_Matching_Pair () 29autocmd ! TextChangedP * call s: Remove_Matches () 30endif 31augroup END 32 33" Skip the rest if it was already done. 34if exists ( "*s:Highlight_Matching_Pair" ) 35finish 36endif 37 38let s: cpo_save = & cpo 39set cpo -= C 40 41" The function that is invoked (very often) to define a ":match" highlighting 42" for any matching paren. 43func s: Highlight_Matching_Pair () 44if ! exists ( "w:matchparen_ids" ) 45let w: matchparen_ids = [] 46endif 47" Remove any previous match. 48call s: Remove_Matches () 49 50" Avoid that we remove the popup menu. 51" Return when there are no colors (looks like the cursor jumps). 52if pumvisible () || ( & t_Co < 8 && ! has ( "gui_running" )) 53return 54endif 55 56" Get the character under the cursor and check if it's in 'matchpairs'. 57let c_lnum = line ( '.' ) 58let c_col = col ( '.' ) 59let before = 0 60 61let text = getline ( c_lnum ) 62let matches = matchlist ( text , '\(.\)\=\%' . c_col . 'c\(.\=\)' ) 63if empty ( matches ) 64let [ c_before , c ] = [ '' , '' ] 65else 66let [ c_before , c ] = matches [ 1 : 2 ] 67endif 68let plist = split ( & matchpairs , '.\zs[:,]' ) 69let i = index ( plist , c ) 70if i < 0 71" not found, in Insert mode try character before the cursor 72if c_col > 1 && ( mode () == 'i' || mode () == 'R' ) 73let before = strlen ( c_before ) 74let c = c_before 75let i = index ( plist , c ) 76endif 77if i < 0 78" not found, nothing to do 79return 80endif 81endif 82 83" Figure out the arguments for searchpairpos(). 84if i % 2 == 0 85let s_flags = 'nW' 86let c2 = plist [ i + 1 ] 87else 88let s_flags = 'nbW' 89let c2 = c 90let c = plist [ i - 1 ] 91endif 92if c == '[' 93let c = '\[' 94let c2 = '\]' 95endif 96 97" Find the match. When it was just before the cursor move it there for a 98" moment. 99if before > 0 100let has_getcurpos = exists ( "*getcurpos" ) 101if has_getcurpos 102" getcurpos() is more efficient but doesn't exist before 7.4.313. 103let save_cursor = getcurpos () 104else 105let save_cursor = winsaveview () 106endif 107call cursor ( c_lnum , c_col - before ) 108endif 109 110if ! has ( "syntax" ) || ! exists ( "g:syntax_on" ) 111let s_skip = "0" 112else 113" Build an expression that detects whether the current cursor position is 114" in certain syntax types (string, comment, etc.), for use as 115" searchpairpos()'s skip argument. 116" We match "escape" for special items, such as lispEscapeSpecial, and 117" match "symbol" for lispBarSymbol. 118let s_skip = 'synstack(".", col("."))' 119 \. '->indexof({_, id -> synIDattr(id, "name") =~? ' 120 \. '"string\\|character\\|singlequote\\|escape\\|symbol\\|comment"}) >= 0' 121" If executing the expression determines that the cursor is currently in 122" one of the syntax types, then we want searchpairpos() to find the pair 123" within those syntax types (i.e., not skip). Otherwise, the cursor is 124" outside of the syntax types and s_skip should keep its value so we skip 125" any matching pair inside the syntax types. 126" Catch if this throws E363: pattern uses more memory than 'maxmempattern'. 127try 128execute 'if ' . s_skip . ' | let s_skip = "0" | endif' 129catch /^Vim\%((\a\+)\)\=:E363/ 130" We won't find anything, so skip searching, should keep Vim responsive. 131return 132endtry 133endif 134 135" Limit the search to lines visible in the window. 136let stoplinebottom = line ( 'w$' ) 137let stoplinetop = line ( 'w0' ) 138if i % 2 == 0 139let stopline = stoplinebottom 140else 141let stopline = stoplinetop 142endif 143 144" Limit the search time to 300 msec to avoid a hang on very long lines. 145" This fails when a timeout is not supported. 146if mode () == 'i' || mode () == 'R' 147let timeout = exists ( "b:matchparen_insert_timeout" ) ? b: matchparen_insert_timeout : g: matchparen_insert_timeout 148else 149let timeout = exists ( "b:matchparen_timeout" ) ? b: matchparen_timeout : g: matchparen_timeout 150endif 151try 152let [ m_lnum , m_col ] = searchpairpos ( c , '' , c2 , s_flags , s_skip , stopline , timeout ) 153catch /E118/ 154" Can't use the timeout, restrict the stopline a bit more to avoid taking 155" a long time on closed folds and long lines. 156" The "viewable" variables give a range in which we can scroll while 157" keeping the cursor at the same position. 158" adjustedScrolloff accounts for very large numbers of scrolloff. 159let adjustedScrolloff = min ([ & scrolloff , ( line ( 'w$' ) - line ( 'w0' )) / 2 ]) 160let bottom_viewable = min ([ line ( '$' ), c_lnum + & lines - adjustedScrolloff - 2 ]) 161let top_viewable = max ([ 1 , c_lnum -& lines + adjustedScrolloff + 2 ]) 162" one of these stoplines will be adjusted below, but the current values are 163" minimal boundaries within the current window 164if i % 2 == 0 165if has ( "byte_offset" ) && has ( "syntax_items" ) && & smc > 0 166let stopbyte = min ([ line2byte ( "$" ), line2byte ( "." ) + col ( "." ) + & smc * 2 ]) 167let stopline = min ([ bottom_viewable , byte2line ( stopbyte )]) 168else 169let stopline = min ([ bottom_viewable , c_lnum + 100 ]) 170endif 171let stoplinebottom = stopline 172else 173if has ( "byte_offset" ) && has ( "syntax_items" ) && & smc > 0 174let stopbyte = max ([ 1 , line2byte ( "." ) + col ( "." ) - & smc * 2 ]) 175let stopline = max ([ top_viewable , byte2line ( stopbyte )]) 176else 177let stopline = max ([ top_viewable , c_lnum - 100 ]) 178endif 179let stoplinetop = stopline 180endif 181let [ m_lnum , m_col ] = searchpairpos ( c , '' , c2 , s_flags , s_skip , stopline ) 182endtry 183 184if before > 0 185if has_getcurpos 186call setpos ( '.' , save_cursor ) 187else 188call winrestview ( save_cursor ) 189endif 190endif 191 192" If a match is found setup match highlighting. 193if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom 194if s: has_matchaddpos 195call add ( w: matchparen_ids , matchaddpos ( 'MatchParen' , [[ c_lnum , c_col - before ], [ m_lnum , m_col ]], 10 )) 196else 197exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . ( c_col - before ) . 198 \'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/' 199call add ( w: matchparen_ids , 3 ) 200endif 201let w: paren_hl_on = 1 202endif 203endfunction 204 205func s: Remove_Matches () 206if exists ( 'w:paren_hl_on' ) && w: paren_hl_on 207while ! empty ( w: matchparen_ids ) 208silent ! call remove ( w: matchparen_ids , 0 ) ->matchdelete () 209endwhile 210let w: paren_hl_on = 0 211endif 212endfunc 213 214" Define commands that will disable and enable the plugin. 215command DoMatchParen call s:DoMatchParen() 216command NoMatchParen call s:NoMatchParen() 217 218func s: NoMatchParen () 219let w = winnr () 220noau windo silent! call matchdelete(3) 221unlet ! g: loaded_matchparen 222exe "noau " . w . "wincmd w" 223au ! matchparen 224endfunc 225 226func s: DoMatchParen () 227runtime plugin/matchparen.lua 228let w = winnr () 229silent windo doau CursorMoved 230exe "noau " . w . "wincmd w" 231endfunc 232 233let & cpo = s: cpo_save 234unlet s: cpo_save