" Vim indent script " General: "{{{ " File: html.vim (Vimscript #2075) " Author: Andy Wokula, anwoku#yahoo*de (#* -> @.) " Last Change: 2007 Nov 20 " Version: 0.3 " Vim Version: Vim7 " Description: " improved version of the distributed html indent script " - Based on: " indent/html.vim from J. Zellner (2006 Jun 05) " indent/css.vim from N. Weibull (2006 Dec 20) " - indenting regions: " Blocktag : Indent-Expr "
: -1 (= keep indent) " ) " if comment opener found, " assume a:lnum within comment " else " assume usual html for a:lnum " if a:lnum-1 has a closing comment " look back to get indent of comment opener " FI " look back for blocktag call cursor(a:lnum, 1) let [stopline, stopcol] = searchpos('\c<\zs\/\=\%(pre\>\|script\>\|style\>\)', "bW") " fugly ... why isn't there searchstr() let tagline = tolower(getline(stopline)) let blocktag = matchstr(tagline, '\/\=\%(pre\>\|script\>\|style\>\)', stopcol-1) if stopline > 0 && blocktag[0] != "/" " opening tag found, assume a:lnum within block let state.block = s:indent_tags[blocktag] if state.block == 3 let state.scripttype = s:GetScriptType(matchstr(tagline, '\>[^>]*', stopcol)) endif let state.blocklnr = stopline " check preceding tags in the line: let s:altline = tagline[: stopcol-2] call s:CountITags(1) let state.blocktagind = indent(stopline) + (s:curind + s:nextrel) * &shiftwidth return state elseif stopline == state.lnum " handle special case: previous line (= state.lnum) contains a " closing blocktag which is preceded by line-noise; " blocktag == "/..." let swtag = match(tagline, '^\s*<') >= 0 if !swtag let [bline, bcol] = searchpos('<'.blocktag[1:].'\>', "bW") let s:altline = tolower(getline(bline)[: bcol-2]) call s:CountITags(1) let state.baseindent = indent(bline) + (s:nextrel+s:curline) * &shiftwidth return state endif endif " else look back for comment call cursor(a:lnum, 1) let [comline, comcol, found] = searchpos('\(', 'bpW', stopline) if found == 2 " comment opener found, assume a:lnum within comment let state.block = 5 let state.blocklnr = comline " check preceding tags in the line: let s:altline = tolower(getline(comline)[: comcol-2]) call s:CountITags(1) let state.blocktagind = indent(comline) + (s:curind + s:nextrel) * &shiftwidth return state endif " else within usual html let s:altline = tolower(getline(state.lnum)) " check a:lnum-1 for closing comment (we need indent from the opening line) let comcol = stridx(s:altline, '-->') if comcol >= 0 call cursor(state.lnum, comcol+1) let [comline, comcol] = searchpos(' return -1 endfunc "}}} func! HtmlIndent() "{{{ let s:curline = tolower(getline(v:lnum)) let s:newstate = {} let s:newstate.lnum = v:lnum " is the first non-blank in the line the start of a tag? let swtag = match(s:curline, '^\s*<') >= 0 if prevnonblank(v:lnum-1) == b:indent.lnum && s:usestate " use state (continue from previous line) else " start over (know nothing) let b:indent = s:FreshState(v:lnum) endif if b:indent.block != 0 " within block " if not 0 then always >= 2 (esp. not negative) let endtag = s:endtags[b:indent.block-2] let blockend = stridx(s:curline, endtag) if blockend >= 0 " block ends here let s:newstate.block = 0 " calc indent for REST OF LINE (may start more blocks): let s:curline = strpart(s:curline, blockend+strlen(endtag)) call s:CountITags() if swtag && b:indent.block != 5 let indent = b:indent.blocktagind + s:curind * &shiftwidth let s:newstate.baseindent = indent + s:nextrel * &shiftwidth else let indent = s:Alien{b:indent.block}() let s:newstate.baseindent = b:indent.blocktagind + s:nextrel * &shiftwidth endif call extend(b:indent, s:newstate, "force") return indent else " block continues " indent this line with alien method let indent = s:Alien{b:indent.block}() call extend(b:indent, s:newstate, "force") return indent endif else " not within a block - within usual html let s:newstate.block = b:indent.block call s:CountITags() if swtag let indent = b:indent.baseindent + s:curind * &shiftwidth let s:newstate.baseindent = indent + s:nextrel * &shiftwidth else let indent = b:indent.baseindent let s:newstate.baseindent = indent + (s:curind + s:nextrel) * &shiftwidth endif call extend(b:indent, s:newstate, "force") return indent endif endfunc "}}} " IndHtmlLocal, clear cpo, Modeline: {{{1 com! -nargs=* -complete=command IndHtmlLocallet &cpo = s:cpo_save unlet s:cpo_save " vim:set fdm=marker ts=8: