2006-02-27 23:58:35 +00:00
|
|
|
" Vim plugin for showing matching parens
|
|
|
|
" Maintainer: Bram Moolenaar <Bram@vim.org>
|
2006-03-29 21:18:24 +00:00
|
|
|
" Last Change: 2006 Mar 29
|
2006-02-27 23:58:35 +00:00
|
|
|
|
|
|
|
" Exit quickly when:
|
|
|
|
" - this plugin was already loaded (or disabled)
|
|
|
|
" - when 'compatible' is set
|
|
|
|
" - the "CursorMoved" autocmd event is not availble.
|
|
|
|
if exists("g:loaded_matchparen") || &cp || !exists("##CursorMoved")
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
let g:loaded_matchparen = 1
|
|
|
|
|
|
|
|
augroup matchparen
|
|
|
|
" Replace all matchparen autocommands
|
|
|
|
autocmd! CursorMoved,CursorMovedI * call s:Highlight_Matching_Pair()
|
|
|
|
augroup END
|
|
|
|
|
|
|
|
let s:paren_hl_on = 0
|
|
|
|
|
|
|
|
" Skip the rest if it was already done.
|
|
|
|
if exists("*s:Highlight_Matching_Pair")
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
|
2006-03-14 22:55:34 +00:00
|
|
|
let cpo_save = &cpo
|
|
|
|
set cpo-=C
|
|
|
|
|
2006-02-27 23:58:35 +00:00
|
|
|
" The function that is invoked (very often) to define a ":match" highlighting
|
|
|
|
" for any matching paren.
|
|
|
|
function! s:Highlight_Matching_Pair()
|
|
|
|
" Remove any previous match.
|
|
|
|
if s:paren_hl_on
|
2006-03-01 22:01:55 +00:00
|
|
|
3match none
|
2006-02-27 23:58:35 +00:00
|
|
|
let s:paren_hl_on = 0
|
|
|
|
endif
|
|
|
|
|
2006-03-04 21:49:37 +00:00
|
|
|
" Avoid that we remove the popup menu.
|
|
|
|
if pumvisible()
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2006-02-27 23:58:35 +00:00
|
|
|
" Get the character under the cursor and check if it's in 'matchpairs'.
|
|
|
|
let c_lnum = line('.')
|
|
|
|
let c_col = col('.')
|
|
|
|
let before = 0
|
|
|
|
|
|
|
|
let c = getline(c_lnum)[c_col - 1]
|
|
|
|
let plist = split(&matchpairs, ':\|,')
|
|
|
|
let i = index(plist, c)
|
|
|
|
if i < 0
|
|
|
|
" not found, in Insert mode try character before the cursor
|
|
|
|
if c_col > 1 && (mode() == 'i' || mode() == 'R')
|
|
|
|
let before = 1
|
|
|
|
let c = getline(c_lnum)[c_col - 2]
|
|
|
|
let i = index(plist, c)
|
|
|
|
endif
|
|
|
|
if i < 0
|
|
|
|
" not found, nothing to do
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
|
|
|
" Figure out the arguments for searchpairpos().
|
|
|
|
" Restrict the search to visible lines with "stopline".
|
2006-03-29 21:18:24 +00:00
|
|
|
" And avoid searching very far (e.g., for closed folds)
|
2006-02-27 23:58:35 +00:00
|
|
|
if i % 2 == 0
|
|
|
|
let s_flags = 'nW'
|
|
|
|
let c2 = plist[i + 1]
|
|
|
|
let stopline = line('w$')
|
2006-03-29 21:18:24 +00:00
|
|
|
if stopline > c_lnum + 100
|
|
|
|
let stopline = c_lnu + 100
|
|
|
|
endif
|
2006-02-27 23:58:35 +00:00
|
|
|
else
|
|
|
|
let s_flags = 'nbW'
|
|
|
|
let c2 = c
|
|
|
|
let c = plist[i - 1]
|
|
|
|
let stopline = line('w0')
|
2006-03-29 21:18:24 +00:00
|
|
|
if stopline < c_lnum - 100
|
|
|
|
let stopline = c_lnu - 100
|
|
|
|
endif
|
2006-02-27 23:58:35 +00:00
|
|
|
endif
|
|
|
|
if c == '['
|
|
|
|
let c = '\['
|
|
|
|
let c2 = '\]'
|
|
|
|
endif
|
|
|
|
|
|
|
|
" When not in a string or comment ignore matches inside them.
|
|
|
|
let s_skip ='synIDattr(synID(c_lnum, c_col - before, 0), "name") ' .
|
|
|
|
\ '=~? "string\\|comment"'
|
|
|
|
execute 'if' s_skip '| let s_skip = 0 | endif'
|
|
|
|
|
|
|
|
" Find the match. When it was just before the cursor move it there for a
|
2006-03-02 22:43:39 +00:00
|
|
|
" moment.
|
2006-02-27 23:58:35 +00:00
|
|
|
if before > 0
|
2006-03-02 22:43:39 +00:00
|
|
|
let save_cursor = getpos('.')
|
2006-02-27 23:58:35 +00:00
|
|
|
call cursor(c_lnum, c_col - before)
|
|
|
|
endif
|
|
|
|
let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
|
|
|
|
if before > 0
|
2006-03-03 22:50:42 +00:00
|
|
|
call setpos('.', save_cursor)
|
2006-02-27 23:58:35 +00:00
|
|
|
endif
|
|
|
|
|
|
|
|
" If a match is found setup match highlighting.
|
|
|
|
if m_lnum > 0 && m_lnum >= line('w0') && m_lnum <= line('w$')
|
2006-03-01 22:01:55 +00:00
|
|
|
exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
|
2006-02-27 23:58:35 +00:00
|
|
|
\ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
|
|
|
|
let s:paren_hl_on = 1
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Define commands that will disable and enable the plugin.
|
2006-03-01 22:01:55 +00:00
|
|
|
command! NoMatchParen 3match none | unlet! g:loaded_matchparen | au! matchparen
|
2006-02-27 23:58:35 +00:00
|
|
|
command! DoMatchParen runtime plugin/matchparen.vim | doau CursorMoved
|
2006-03-14 22:55:34 +00:00
|
|
|
|
|
|
|
let &cpo = cpo_save
|