1" Language: xml 2" Maintainer: Johannes Zellner <[email protected]> 3" Last Change: Tue, 27 Apr 2004 14:54:59 CEST 4" Notes: 1) does not indent pure non-xml code (e.g. embedded scripts) 5" 2) will be confused by unbalanced tags in comments 6" or CDATA sections. 7" TODO: implement pre-like tags, see xml_indent_open / xml_indent_close 8 9" Only load this indent file when no other was loaded. 10if exists("b:did_indent") 11 finish 12endif 13let b:did_indent = 1 14 15" [-- local settings (must come before aborting the script) --] 16setlocal indentexpr=XmlIndentGet(v:lnum,1) 17setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,} 18 19set cpo-=C 20 21if !exists('b:xml_indent_open') 22 let b:xml_indent_open = '.\{-}<\a' 23 " pre tag, e.g. <address> 24 " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!' 25endif 26 27if !exists('b:xml_indent_close') 28 let b:xml_indent_close = '.\{-}</' 29 " end pre tag, e.g. </address> 30 " let b:xml_indent_close = '.\{-}</\(address\)\@!' 31endif 32 33" [-- finish, if the function already exists --] 34if exists('*XmlIndentGet') | finish | endif 35 36fun! <SID>XmlIndentWithPattern(line, pat) 37 let s = substitute('x'.a:line, a:pat, "\1", 'g') 38 return strlen(substitute(s, "[^\1].*$", '', '')) 39endfun 40 41" [-- check if it's xml --] 42fun! <SID>XmlIndentSynCheck(lnum) 43 if '' != &syntax 44 let syn1 = synIDattr(synID(a:lnum, 1, 1), 'name') 45 let syn2 = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name') 46 if '' != syn1 && syn1 !~ 'xml' && '' != syn2 && syn2 !~ 'xml' 47 " don't indent pure non-xml code 48 return 0 49 endif 50 endif 51 return 1 52endfun 53 54" [-- return the sum of indents of a:lnum --] 55fun! <SID>XmlIndentSum(lnum, style, add) 56 let line = getline(a:lnum) 57 if a:style == match(line, '^\s*</') 58 return (&sw * 59 \ (<SID>XmlIndentWithPattern(line, b:xml_indent_open) 60 \ - <SID>XmlIndentWithPattern(line, b:xml_indent_close) 61 \ - <SID>XmlIndentWithPattern(line, '.\{-}/>'))) + a:add 62 else 63 return a:add 64 endif 65endfun 66 67fun! XmlIndentGet(lnum, use_syntax_check) 68 " Find a non-empty line above the current line. 69 let lnum = prevnonblank(a:lnum - 1) 70 71 " Hit the start of the file, use zero indent. 72 if lnum == 0 73 return 0 74 endif 75 76 if a:use_syntax_check 77 if 0 == <SID>XmlIndentSynCheck(lnum) || 0 == <SID>XmlIndentSynCheck(a:lnum) 78 return indent(a:lnum) 79 endif 80 endif 81 82 let ind = <SID>XmlIndentSum(lnum, -1, indent(lnum)) 83 let ind = <SID>XmlIndentSum(a:lnum, 0, ind) 84 85 return ind 86endfun 87 88" vim:ts=8 89