Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2015
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. " Vim plugin for showing matching parens
  2. " Maintainer: Bram Moolenaar <Bram@vim.org>
  3. " Last Change: 2014 Jul 19
  4.  
  5. " Exit quickly when:
  6. " - this plugin was already loaded (or disabled)
  7. " - when 'compatible' is set
  8. " - the "CursorMoved" autocmd event is not available.
  9. if exists("g:loaded_matchparen") || &cp || !exists("##CursorMoved")
  10. finish
  11. endif
  12. let g:loaded_matchparen = 1
  13.  
  14. if !exists("g:matchparen_timeout")
  15. let g:matchparen_timeout = 300
  16. endif
  17. if !exists("g:matchparen_insert_timeout")
  18. let g:matchparen_insert_timeout = 60
  19. endif
  20.  
  21. augroup matchparen
  22. " Replace all matchparen autocommands
  23. autocmd! CursorMoved,CursorMovedI,WinEnter * call s:Highlight_Matching_Pair()
  24. "autocmd! CursorMoved,WinEnter * call s:Highlight_Matching_Pair()
  25. if exists('##TextChanged')
  26. autocmd! TextChanged,TextChangedI * call s:Highlight_Matching_Pair()
  27. endif
  28. augroup END
  29.  
  30. " Skip the rest if it was already done.
  31. if exists("*s:Highlight_Matching_Pair")
  32. finish
  33. endif
  34.  
  35. let s:cpo_save = &cpo
  36. set cpo-=C
  37.  
  38. " The function that is invoked (very often) to define a ":match" highlighting
  39. " for any matching paren.
  40. function! s:Highlight_Matching_Pair()
  41. " Remove any previous match.
  42. if exists('w:paren_hl_on') && w:paren_hl_on
  43. silent! call matchdelete(3)
  44. let w:paren_hl_on = 0
  45. endif
  46.  
  47. " Avoid that we remove the popup menu.
  48. " Return when there are no colors (looks like the cursor jumps).
  49. if pumvisible() || (&t_Co < 8 && !has("gui_running"))
  50. return
  51. endif
  52.  
  53. " Get the character under the cursor and check if it's in 'matchpairs'.
  54. let c_lnum = line('.')
  55. let c_col = col('.')
  56. let before = 0
  57.  
  58. let text = getline(c_lnum)
  59. let c = text[c_col - 1]
  60. let plist = split(&matchpairs, '.\zs[:,]')
  61. let i = index(plist, c)
  62. if i < 0
  63. " not found, in Insert mode try character before the cursor
  64. if c_col > 1 && (mode() == 'i' || mode() == 'R')
  65. let before = 1
  66. let c = text[c_col - 2]
  67. let i = index(plist, c)
  68. endif
  69. if i < 0
  70. " not found, nothing to do
  71. return
  72. endif
  73. endif
  74.  
  75. " Figure out the arguments for searchpairpos().
  76. if i % 2 == 0
  77. let s_flags = 'nW'
  78. let c2 = plist[i + 1]
  79. else
  80. let s_flags = 'nbW'
  81. let c2 = c
  82. let c = plist[i - 1]
  83. endif
  84. if c == '['
  85. let c = '\['
  86. let c2 = '\]'
  87. endif
  88.  
  89. " Find the match. When it was just before the cursor move it there for a
  90. " moment.
  91. if before > 0
  92. let has_getcurpos = exists("*getcurpos")
  93. " if has_getcurpos
  94. " " getcurpos() is more efficient but doesn't exist before 7.4.313.
  95. " let save_cursor = getcurpos()
  96. " else
  97. let save_cursor = winsaveview()
  98. " endif
  99. call cursor(c_lnum, c_col - before)
  100. endif
  101.  
  102. " Build an expression that detects whether the current cursor position is in
  103. " certain syntax types (string, comment, etc.), for use as searchpairpos()'s
  104. " skip argument.
  105. " We match "escape" for special items, such as lispEscapeSpecial.
  106. let s_skip = '!empty(filter(map(synstack(line("."), col(".")), ''synIDattr(v:val, "name")''), ' .
  107. \ '''v:val =~? "string\\|character\\|singlequote\\|escape\\|comment"''))'
  108. " If executing the expression determines that the cursor is currently in
  109. " one of the syntax types, then we want searchpairpos() to find the pair
  110. " within those syntax types (i.e., not skip). Otherwise, the cursor is
  111. " outside of the syntax types and s_skip should keep its value so we skip any
  112. " matching pair inside the syntax types.
  113. execute 'if' s_skip '| let s_skip = 0 | endif'
  114.  
  115. " Limit the search to lines visible in the window.
  116. let stoplinebottom = line('w$')
  117. let stoplinetop = line('w0')
  118. if i % 2 == 0
  119. let stopline = stoplinebottom
  120. else
  121. let stopline = stoplinetop
  122. endif
  123.  
  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.  
  164. if before > 0
  165. " if has_getcurpos
  166. " call setpos('.', save_cursor)
  167. " else
  168. call winrestview(save_cursor)
  169. " endif
  170. endif
  171.  
  172. " If a match is found setup match highlighting.
  173. if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom
  174. if exists('*matchaddpos')
  175. call matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10, 3)
  176. else
  177. exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
  178. \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
  179. endif
  180. let w:paren_hl_on = 1
  181. endif
  182. endfunction
  183.  
  184. " Define commands that will disable and enable the plugin.
  185. command! NoMatchParen windo silent! call matchdelete(3) | unlet! g:loaded_matchparen |
  186. \ au! matchparen
  187. command! DoMatchParen runtime plugin/matchparen.vim | windo doau CursorMoved
  188.  
  189. let &cpo = s:cpo_save
  190. unlet s:cpo_save
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement