From f608a64f6627156be379ec73632d6b0f6963d2fa Mon Sep 17 00:00:00 2001 From: gryf Date: Tue, 14 Feb 2012 21:25:38 +0100 Subject: [PATCH] Added MatchTag html plugin, updated GetLatestVimScripts datafile --- GetLatest/GetLatestVimScripts.dat | 4 +- bundle/ft_matchtag/ftplugin/html.vim | 82 ++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 bundle/ft_matchtag/ftplugin/html.vim diff --git a/GetLatest/GetLatestVimScripts.dat b/GetLatest/GetLatestVimScripts.dat index ccacfb6..ad53cda 100644 --- a/GetLatest/GetLatestVimScripts.dat +++ b/GetLatest/GetLatestVimScripts.dat @@ -2,13 +2,10 @@ ScriptID SourceID Filename -------------------------- ### plugins 2572 10433 ack.vim -3619 16816 buffergator 102 16171 DirDiff.vim -1984 13961 FuzzyFinder 311 7645 grep.vim 3304 17406 gundo.vim 2727 11120 jsbeautify.vim -3252 13948 L9 2289 8922 loremipsum 2666 16840 Mark 1218 14455 nerdcommenter @@ -36,6 +33,7 @@ ScriptID SourceID Filename 415 15531 zenburn #3597 1 ColorV # ftplugin +3818 1 MatchTag 910 14691 pydoc.vim 2441 14403 pyflakes.vim 30 9196 python_fn.vim diff --git a/bundle/ft_matchtag/ftplugin/html.vim b/bundle/ft_matchtag/ftplugin/html.vim new file mode 100644 index 0000000..320c7e8 --- /dev/null +++ b/bundle/ft_matchtag/ftplugin/html.vim @@ -0,0 +1,82 @@ +" Vim plugin for showing matching html tags. +" Maintainer: Greg Sexton +" Credits: Bram Moolenar and the 'matchparen' plugin from which this draws heavily. + +if exists("b:did_ftplugin") + finish +endif + +augroup matchhtmlparen + autocmd! CursorMoved,CursorMovedI,WinEnter call s:Highlight_Matching_Pair() +augroup END + +fu! s:Highlight_Matching_Pair() + " Remove any previous match. + if exists('w:tag_hl_on') && w:tag_hl_on + 2match none + let w:tag_hl_on = 0 + endif + + " Avoid that we remove the popup menu. + " Return when there are no colors (looks like the cursor jumps). + if pumvisible() || (&t_Co < 8 && !has("gui_running")) + return + endif + + "get html tag under cursor + let tagname = s:GetCurrentCursorTag() + if tagname == ""|return|endif + + if tagname[0] == '/' + let position = s:SearchForMatchingTag(tagname[1:], 0) + else + let position = s:SearchForMatchingTag(tagname, 1) + endif + call s:HighlightTagAtPosition(position) +endfu + +fu! s:GetCurrentCursorTag() + "returns the tag under the cursor, includes the '/' if on a closing tag. + + let c_col = col('.') + let matched = matchstr(getline('.'), '\(<[^<>]*\%'.c_col.'c.\{-}>\)\|\(\%'.c_col.'c<.\{-}>\)') + if matched == "" + return matched + endif + + let tagname = matchstr(matched, '<\zs.\{-}\ze[ >]') + return tagname +endfu + +fu! s:SearchForMatchingTag(tagname, forwards) + "returns the position of a matching tag or [0 0] + + let starttag = '<'.a:tagname.'.\{-}>' + let midtag = '' + let endtag = ''.(a:forwards?'':'\zs') + let flags = 'nW'.(a:forwards?'':'b') + + " When not in a string or comment ignore matches inside them. + let skip ='synIDattr(synID(line("."), col("."), 0), "name") ' . + \ '=~? "htmlString\\|htmlCommentPart"' + execute 'if' skip '| let skip = 0 | endif' + + " Limit the search to lines visible in the window. + let stopline = a:forwards ? line('w$') : line('w0') + let timeout = 300 + + return searchpairpos(starttag, midtag, endtag, flags, skip, stopline, timeout) +endfu + +fu! s:HighlightTagAtPosition(position) + if a:position == [0, 0] + return + endif + + let [m_lnum, m_col] = a:position + exe '2match MatchParen /\(\%' . m_lnum . 'l\%' . m_col . 'c<\zs.\{-}\ze[ >]\)\|' + \ .'\(\%' . line('.') . 'l\%' . col('.') . 'c<\zs.\{-}\ze[ >]\)\|' + \ .'\(\%' . line('.') . 'l<\zs[^<> ]*\%' . col('.') . 'c.\{-}\ze[ >]\)\|' + \ .'\(\%' . line('.') . 'l<\zs[^<>]\{-}\ze\s[^<>]*\%' . col('.') . 'c.\{-}>\)/' + let w:tag_hl_on = 1 +endfu