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