matchparen.vim 7.0 KB

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