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