Permalink
Browse files

Add html and xml ftplugin

  • Loading branch information...
1 parent 447b5c7 commit 73fad6ce119b98f8c39f5dd496d34bb8a48dc3f0 @aredridel committed Apr 2, 2012
Showing with 191 additions and 0 deletions.
  1. +24 −0 vim/README.mkd
  2. +87 −0 vim/ftplugin/html.vim
  3. +2 −0 vim/ftplugin/xml.vim
  4. +78 −0 vim/test.html
View
@@ -0,0 +1,24 @@
+# Readme
+
+This plugin highlights the matching HTML tag when the cursor is
+positioned on a tag. It works in much the same way as the MatchParen
+plugin.
+
+It was inspired by this question <http://stackoverflow.com/q/8168320/20388>.
+
+The latest 'release' is available on [vim.org](http://www.vim.org/scripts/script.php?script_id=3818).
+
+## Other file types
+
+To get this to work for file types other than html that use a similar
+tag structure (e.g XML) you can do something similar to this.
+
+ echo 'runtime! ftplugin/html.vim' > ~/.vim/ftplugin/xml.vim
+
+The xml.vim file demonstrated here is included with MatchTag.
+
+## Screenshot
+
+Thanks to [ithaca.arpinum.org](http://ithaca.arpinum.org/) for this screenshot.
+
+![Screenshot of MatchTag](http://www.gregsexton.org/images/matchtag/matchtag.jpg)
View
@@ -0,0 +1,87 @@
+" Vim plugin for showing matching html tags.
+" Maintainer: Greg Sexton <gregsexton@gmail.com>
+" 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 <buffer> 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 == "" || matched =~ '/>$'
+ return ""
+ 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:tagname.'.\{-}'.(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
+
+ " The searchpairpos() timeout parameter was added in 7.2
+ if v:version >= 702
+ return searchpairpos(starttag, midtag, endtag, flags, skip, stopline, timeout)
+ else
+ return searchpairpos(starttag, midtag, endtag, flags, skip, stopline)
+ endif
+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
View
@@ -0,0 +1,2 @@
+runtime! ftplugin/html.vim
+
View
@@ -0,0 +1,78 @@
+<div>
+ <div>
+ <div>
+ </div>
+ </div>
+</div>
+
+<div>
+ <div>
+ <div>
+ <!-- </div> -->
+ </div>
+ </div>
+</div>
+
+<div style="blah">
+ <div>
+ <img />
+ </div>
+</div>
+
+<div style="stuff" id="blah">
+ <div style=">" id="blah">
+ </div>
+</div>
+
+<property>
+ <property name="&lt;Super&gt;p" type="empty"/>
+ <property name="&lt;Control&gt;Escape" type="empty"/>
+</property>
+
+<img width="50" height="10"/>
+<img width="50" height="10" />
+
+<div><div></div><div></div></div>
+
+<div>
+
+
+
+ <p> test offscreen </p>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+</div>

0 comments on commit 73fad6c

Please sign in to comment.