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