matchparen.vim 6.9 KB

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