1" Language: xml 2" Repository: https://github.com/chrisbra/vim-xml-ftplugin 3" Last Changed: Feb 04, 2019 4" Maintainer: Christian Brabandt <[email protected]> 5" Previous Maintainer: Johannes Zellner <[email protected]> 6" Last Change: 7" 20190204 - correctly handle wrap tags 8" https://github.com/chrisbra/vim-xml-ftplugin/issues/5 9" 20190128 - Make sure to find previous tag 10" https://github.com/chrisbra/vim-xml-ftplugin/issues/4 11" 20181116 - Fix indentation when tags start with a colon or an underscore 12" https://github.com/vim/vim/pull/926 13" 20181022 - Do not overwrite indentkeys setting 14" https://github.com/chrisbra/vim-xml-ftplugin/issues/1 15" 20180724 - Correctly indent xml comments https://github.com/vim/vim/issues/3200 16" 17" Notes: 18" 1) does not indent pure non-xml code (e.g. embedded scripts) 19" 2) will be confused by unbalanced tags in comments 20" or CDATA sections. 21" 2009-05-26 patch by Nikolai Weibull 22" TODO: implement pre-like tags, see xml_indent_open / xml_indent_close 23 24" Only load this indent file when no other was loaded. 25if exists("b:did_indent") 26 finish 27endif 28let b:did_indent = 1 29let s:keepcpo= &cpo 30set cpo&vim 31 32" [-- local settings (must come before aborting the script) --] 33" Attention: Parameter use_syntax_check is used by the docbk.vim indent script 34setlocal indentexpr=XmlIndentGet(v:lnum,1) 35setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,},!^F 36 37if !exists('b:xml_indent_open') 38 let b:xml_indent_open = '.\{-}<[:A-Z_a-z]' 39 " pre tag, e.g. <address> 40 " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!' 41endif 42 43if !exists('b:xml_indent_close') 44 let b:xml_indent_close = '.\{-}</' 45 " end pre tag, e.g. </address> 46 " let b:xml_indent_close = '.\{-}</\(address\)\@!' 47endif 48 49let &cpo = s:keepcpo 50unlet s:keepcpo 51 52" [-- finish, if the function already exists --] 53if exists('*XmlIndentGet') 54 finish 55endif 56 57let s:keepcpo= &cpo 58set cpo&vim 59 60fun! <SID>XmlIndentWithPattern(line, pat) 61 let s = substitute('x'.a:line, a:pat, "\1", 'g') 62 return strlen(substitute(s, "[^\1].*$", '', '')) 63endfun 64 65" [-- check if it's xml --] 66fun! <SID>XmlIndentSynCheck(lnum) 67 if &syntax != '' 68 let syn1 = synIDattr(synID(a:lnum, 1, 1), 'name') 69 let syn2 = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name') 70 if syn1 != '' && syn1 !~ 'xml' && syn2 != '' && syn2 !~ 'xml' 71 " don't indent pure non-xml code 72 return 0 73 endif 74 endif 75 return 1 76endfun 77 78" [-- return the sum of indents of a:lnum --] 79fun! <SID>XmlIndentSum(line, style, add) 80 if <SID>IsXMLContinuation(a:line) && a:style == 0 81 " no complete tag, add one additional indent level 82 " but only for the current line 83 return a:add + shiftwidth() 84 elseif <SID>HasNoTagEnd(a:line) 85 " no complete tag, return initial indent 86 return a:add 87 endif 88 if a:style == match(a:line, '^\s*</') 89 return (shiftwidth() * 90 \ (<SID>XmlIndentWithPattern(a:line, b:xml_indent_open) 91 \ - <SID>XmlIndentWithPattern(a:line, b:xml_indent_close) 92 \ - <SID>XmlIndentWithPattern(a:line, '.\{-}/>'))) + a:add 93 else 94 return a:add 95 endif 96endfun 97 98" Main indent function 99fun! XmlIndentGet(lnum, use_syntax_check) 100 " Find a non-empty line above the current line. 101 if prevnonblank(a:lnum - 1) == 0 102 " Hit the start of the file, use zero indent. 103 return 0 104 endif 105 " Find previous line with a tag (regardless whether open or closed, 106 " but always start restrict the match to a line before the current one 107 " Note: xml declaration: <?xml version="1.0"?> 108 " won't be found, as it is not a legal tag name 109 let ptag_pattern = '\%(.\{-}<[/:A-Z_a-z]\)'. '\%(\&\%<'. line('.').'l\)' 110 let ptag = search(ptag_pattern, 'bnW') 111 " no previous tag 112 if ptag == 0 113 return 0 114 endif 115 116 let syn_name = '' 117 if a:use_syntax_check 118 let check_lnum = <SID>XmlIndentSynCheck(ptag) 119 let check_alnum = <SID>XmlIndentSynCheck(a:lnum) 120 if check_lnum == 0 || check_alnum == 0 121 return indent(a:lnum) 122 endif 123 let syn_name = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name') 124 endif 125 126 if syn_name =~ 'Comment' 127 return <SID>XmlIndentComment(a:lnum) 128 endif 129 130 let pline = getline(ptag) 131 let pind = indent(ptag) 132 " Get indent from previous tag line 133 let ind = <SID>XmlIndentSum(pline, -1, pind) 134 let t_ind = ind 135 " Determine indent from current line 136 let ind = <SID>XmlIndentSum(getline(a:lnum), 0, ind) 137 return ind 138endfun 139 140func! <SID>IsXMLContinuation(line) 141 " Checks, whether or not the line matches a start-of-tag 142 return a:line !~ '^\s*<' 143endfunc 144 145func! <SID>HasNoTagEnd(line) 146 " Checks whether or not the line matches '>' (so finishes a tag) 147 return a:line !~ '>\s*$' 148endfunc 149 150" return indent for a commented line, 151" the middle part might be indented on additional level 152func! <SID>XmlIndentComment(lnum) 153 let ptagopen = search(b:xml_indent_open, 'bnW') 154 let ptagclose = search(b:xml_indent_close, 'bnW') 155 if getline(a:lnum) =~ '<!--' 156 " if previous tag was a closing tag, do not add 157 " one additional level of indent 158 if ptagclose > ptagopen && a:lnum > ptagclose 159 return indent(ptagclose) 160 else 161 " start of comment, add one indentation level 162 return indent(ptagopen) + shiftwidth() 163 endif 164 elseif getline(a:lnum) =~ '-->' 165 " end of comment, same as start of comment 166 return indent(search('<!--', 'bnW')) 167 else 168 " middle part of comment, add one additional level 169 return indent(search('<!--', 'bnW')) + shiftwidth() 170 endif 171endfunc 172 173let &cpo = s:keepcpo 174unlet s:keepcpo 175 176" vim:ts=4 et sts=-1 sw=0 177