1 " Vim plugin for showing matching parens
2 " Maintainer: Bram Moolenaar <Bram@vim.org>
3 " Last Change: 2007 Aug 8
6 " - this plugin was already loaded (or disabled)
7 " - when 'compatible' is set
8 " - the "CursorMoved" autocmd event is not availble.
9 if exists("g:loaded_matchparen") || &cp || !exists("##CursorMoved")
12 let g:loaded_matchparen = 1
15 " Replace all matchparen autocommands
16 autocmd! CursorMoved,CursorMovedI,WinEnter * call s:Highlight_Matching_Pair()
19 " Skip the rest if it was already done.
20 if exists("*s:Highlight_Matching_Pair")
27 " The function that is invoked (very often) to define a ":match" highlighting
28 " for any matching paren.
29 function! s:Highlight_Matching_Pair()
30 " Remove any previous match.
31 if exists('w:paren_hl_on') && w:paren_hl_on
36 " Avoid that we remove the popup menu.
41 " Get the character under the cursor and check if it's in 'matchpairs'.
42 let c_lnum = line('.')
46 let c = getline(c_lnum)[c_col - 1]
47 let plist = split(&matchpairs, '.\zs[:,]')
48 let i = index(plist, c)
50 " not found, in Insert mode try character before the cursor
51 if c_col > 1 && (mode() == 'i' || mode() == 'R')
53 let c = getline(c_lnum)[c_col - 2]
54 let i = index(plist, c)
57 " not found, nothing to do
62 " Figure out the arguments for searchpairpos().
63 " Restrict the search to visible lines with "stopline".
64 " And avoid searching very far (e.g., for closed folds and long lines)
65 " The "viewable" variables give a range in which we can scroll while keeping
66 " the cursor at the same position
67 " adjustedScrolloff accounts for very large numbers of scrolloff
68 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2])
69 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2])
70 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2])
71 " one of these stoplines will be adjusted below, but the current values are
72 " minimal boundaries within the current window
73 let stoplinebottom = line('w$')
74 let stoplinetop = line('w0')
78 if has("byte_offset") && has("syntax_items") && &smc > 0
79 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2])
80 let stopline = min([bottom_viewable, byte2line(stopbyte)])
82 let stopline = min([bottom_viewable, c_lnum + 100])
84 let stoplinebottom = stopline
89 if has("byte_offset") && has("syntax_items") && &smc > 0
90 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2])
91 let stopline = max([top_viewable, byte2line(stopbyte)])
93 let stopline = max([top_viewable, c_lnum - 100])
95 let stoplinetop = stopline
102 " Find the match. When it was just before the cursor move it there for a
105 let save_cursor = winsaveview()
106 call cursor(c_lnum, c_col - before)
109 " When not in a string or comment ignore matches inside them.
110 let s_skip ='synIDattr(synID(line("."), col("."), 0), "name") ' .
111 \ '=~? "string\\|character\\|singlequote\\|comment"'
112 execute 'if' s_skip '| let s_skip = 0 | endif'
114 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
117 call winrestview(save_cursor)
120 " If a match is found setup match highlighting.
121 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom
122 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
123 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
124 let w:paren_hl_on = 1
128 " Define commands that will disable and enable the plugin.
129 command! NoMatchParen windo 3match none | unlet! g:loaded_matchparen |
131 command! DoMatchParen runtime plugin/matchparen.vim | windo doau CursorMoved