" Description: html indenter
" Author: Johannes Zellner
" URL: http://www.zellner.org/vim/indent/html.vim
" Last Change: Tue, 25 Sep 2001 13:22:27 +0200
" Globals: g:html_indent_tags -- indenting tags
" g:html_indent_strict -- inhibit 'O O' elements
" g:html_indent_strict_table -- inhibit 'O -' elements
" Only load this indent file when no other was loaded.
if exists("b:did_indent")
finish
endif
let b:did_indent = 1
" [-- local settings (must come before aborting the script) --]
setlocal indentexpr=HtmlIndentGet(v:lnum)
setlocal indentkeys=o,O,*,<>>,,{,}
if exists('g:html_indent_tags')
unlet g:html_indent_tags
endif
" [-- helper function to assemble tag list --]
fun! HtmlIndentPush(tag)
if exists('g:html_indent_tags')
let g:html_indent_tags = g:html_indent_tags.'\|'.a:tag
else
let g:html_indent_tags = a:tag
endif
endfun
" [-- --]
call HtmlIndentPush('a')
call HtmlIndentPush('abbr')
call HtmlIndentPush('acronym')
call HtmlIndentPush('address')
call HtmlIndentPush('b')
call HtmlIndentPush('bdo')
call HtmlIndentPush('big')
call HtmlIndentPush('blockquote')
call HtmlIndentPush('button')
call HtmlIndentPush('caption')
call HtmlIndentPush('center')
call HtmlIndentPush('cite')
call HtmlIndentPush('code')
call HtmlIndentPush('del')
call HtmlIndentPush('dfn')
call HtmlIndentPush('dir')
call HtmlIndentPush('div')
call HtmlIndentPush('dl')
call HtmlIndentPush('em')
call HtmlIndentPush('fieldset')
call HtmlIndentPush('font')
call HtmlIndentPush('form')
call HtmlIndentPush('frameset')
call HtmlIndentPush('h1')
call HtmlIndentPush('h2')
call HtmlIndentPush('h3')
call HtmlIndentPush('h4')
call HtmlIndentPush('h5')
call HtmlIndentPush('h6')
call HtmlIndentPush('i')
call HtmlIndentPush('iframe')
call HtmlIndentPush('ins')
call HtmlIndentPush('kbd')
call HtmlIndentPush('label')
call HtmlIndentPush('legend')
call HtmlIndentPush('map')
call HtmlIndentPush('menu')
call HtmlIndentPush('noframes')
call HtmlIndentPush('noscript')
call HtmlIndentPush('object')
call HtmlIndentPush('ol')
call HtmlIndentPush('optgroup')
call HtmlIndentPush('pre')
call HtmlIndentPush('q')
call HtmlIndentPush('s')
call HtmlIndentPush('samp')
call HtmlIndentPush('script')
call HtmlIndentPush('select')
call HtmlIndentPush('small')
call HtmlIndentPush('span')
call HtmlIndentPush('strong')
call HtmlIndentPush('style')
call HtmlIndentPush('sub')
call HtmlIndentPush('sup')
call HtmlIndentPush('table')
call HtmlIndentPush('textarea')
call HtmlIndentPush('title')
call HtmlIndentPush('tt')
call HtmlIndentPush('u')
call HtmlIndentPush('ul')
call HtmlIndentPush('var')
" [-- --]
if !exists('g:html_indent_strict')
call HtmlIndentPush('body')
call HtmlIndentPush('head')
call HtmlIndentPush('html')
call HtmlIndentPush('tbody')
endif
" [-- --]
if !exists('g:html_indent_strict_table')
call HtmlIndentPush('th')
call HtmlIndentPush('td')
call HtmlIndentPush('tr')
call HtmlIndentPush('tfoot')
call HtmlIndentPush('thead')
endif
delfun HtmlIndentPush
set cpo-=C
" [-- count indent-increasing tags of line a:lnum --]
fun! HtmlIndentOpen(lnum)
let s = substitute('x'.getline(a:lnum),
\ '.\{-}\(\(<\)\('.g:html_indent_tags.'\)\>\)', '§', 'g')
let s = substitute(s, '[^§].*$', '', '')
return strlen(s)
endfun
" [-- count indent-decreasing tags of line a:lnum --]
fun! HtmlIndentClose(lnum)
let s = substitute('x'.getline(a:lnum),
\ '.\{-}\(\(<\)/\('.g:html_indent_tags.'\)\>>\)', '§', 'g')
let s = substitute(s, '[^§].*$', '', '')
return strlen(s)
endfun
" [-- count indent-increasing '{' of (java|css) line a:lnum --]
fun! HtmlIndentOpenAlt(lnum)
return strlen(substitute(getline(a:lnum), '[^{]\+', '', 'g'))
endfun
" [-- count indent-decreasing '}' of (java|css) line a:lnum --]
fun! HtmlIndentCloseAlt(lnum)
return strlen(substitute(getline(a:lnum), '[^}]\+', '', 'g'))
endfun
" [-- return the sum of indents respecting the syntax of a:lnum --]
fun! HtmlIndentSum(lnum, style)
if a:style == match(getline(a:lnum), '^\s*')
if a:style == match(getline(a:lnum), '^\s*\<\('.g:html_indent_tags.'\)\>')
let open = HtmlIndentOpen(a:lnum)
let close = HtmlIndentClose(a:lnum)
if 0 != open || 0 != close
return open - close
endif
endif
endif
if '' != &syntax &&
\ synIDattr(synID(a:lnum, 1, 1), 'name') =~ '\(css\|java\).*' &&
\ synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name')
\ =~ '\(css\|java\).*'
if a:style == match(getline(a:lnum), '^\s*}')
return HtmlIndentOpenAlt(a:lnum) - HtmlIndentCloseAlt(a:lnum)
endif
endif
return 0
endfun
fun! HtmlIndentGet(lnum)
" Find a non-empty line above the current line.
let lnum = prevnonblank(a:lnum - 1)
" Hit the start of the file, use zero indent.
if lnum == 0
return 0
endif
let restore_ic=&ic
let &ic=1 " ignore case
let ind = HtmlIndentSum(lnum, -1)
let ind = ind + HtmlIndentSum(a:lnum, 0)
let &ic=restore_ic
return indent(lnum) + (&sw * ind)
endfun
" [-- EOF /indent/html.vim --]