blob: da65417939b9d4b97131c355a6e446da1621fcf0 [file] [log] [blame]
Bram Moolenaar96f45c02019-10-26 19:53:45 +02001" Language: XML
2" Maintainer: Christian Brabandt <cb@256bit.org>
3" Repository: https://github.com/chrisbra/vim-xml-ftplugin
4" Previous Maintainer: Johannes Zellner <johannes@zellner.org>
Bram Moolenaar23515b42020-11-29 14:36:24 +01005" Last Changed: 2020 Nov 4th
Bram Moolenaar9d87a372018-12-18 21:41:50 +01006" Last Change:
Bram Moolenaar23515b42020-11-29 14:36:24 +01007" 20200529 - Handle empty closing tags correctly
Bram Moolenaar4ceaa3a2019-12-03 22:49:09 +01008" 20191202 - Handle docbk filetype
Bram Moolenaar54775062019-07-31 21:07:14 +02009" 20190726 - Correctly handle non-tagged data
Bram Moolenaar63b74a82019-03-24 15:09:13 +010010" 20190204 - correctly handle wrap tags
11" https://github.com/chrisbra/vim-xml-ftplugin/issues/5
Bram Moolenaar314dd792019-02-03 15:27:20 +010012" 20190128 - Make sure to find previous tag
13" https://github.com/chrisbra/vim-xml-ftplugin/issues/4
Bram Moolenaar9d87a372018-12-18 21:41:50 +010014" 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
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
27" Only load this indent file when no other was loaded.
28if exists("b:did_indent")
29 finish
30endif
31let b:did_indent = 1
Bram Moolenaar8e52a592012-05-18 21:49:28 +020032let s:keepcpo= &cpo
33set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35" [-- local settings (must come before aborting the script) --]
Bram Moolenaar9d87a372018-12-18 21:41:50 +010036" Attention: Parameter use_syntax_check is used by the docbk.vim indent script
Bram Moolenaar071d4272004-06-13 20:20:40 +000037setlocal indentexpr=XmlIndentGet(v:lnum,1)
Bram Moolenaarba3ff532018-11-04 14:45:49 +010038setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,},!^F
Bram Moolenaar54775062019-07-31 21:07:14 +020039" autoindent: used when the indentexpr returns -1
40setlocal autoindent
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaar071d4272004-06-13 20:20:40 +000042if !exists('b:xml_indent_open')
Bram Moolenaar9d87a372018-12-18 21:41:50 +010043 let b:xml_indent_open = '.\{-}<[:A-Z_a-z]'
Bram Moolenaar071d4272004-06-13 20:20:40 +000044 " pre tag, e.g. <address>
45 " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!'
46endif
47
48if !exists('b:xml_indent_close')
Bram Moolenaar23515b42020-11-29 14:36:24 +010049 let b:xml_indent_close = '.\{-}</\|/>.\{-}'
Bram Moolenaar071d4272004-06-13 20:20:40 +000050 " end pre tag, e.g. </address>
51 " let b:xml_indent_close = '.\{-}</\(address\)\@!'
52endif
53
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020054let &cpo = s:keepcpo
55unlet s:keepcpo
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057" [-- finish, if the function already exists --]
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020058if exists('*XmlIndentGet')
Bram Moolenaar9d87a372018-12-18 21:41:50 +010059 finish
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020060endif
61
62let s:keepcpo= &cpo
63set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000064
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)
Bram Moolenaar9d87a372018-12-18 21:41:50 +010072 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
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 endif
80 return 1
81endfun
82
83" [-- return the sum of indents of a:lnum --]
Bram Moolenaar63b74a82019-03-24 15:09:13 +010084fun! <SID>XmlIndentSum(line, style, add)
Bram Moolenaar23515b42020-11-29 14:36:24 +010085 if <SID>IsXMLContinuation(a:line) && a:style == 0 && !<SID>IsXMLEmptyClosingTag(a:line)
Bram Moolenaar63b74a82019-03-24 15:09:13 +010086 " 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*</')
Bram Moolenaar9d87a372018-12-18 21:41:50 +010094 return (shiftwidth() *
Bram Moolenaar63b74a82019-03-24 15:09:13 +010095 \ (<SID>XmlIndentWithPattern(a:line, b:xml_indent_open)
96 \ - <SID>XmlIndentWithPattern(a:line, b:xml_indent_close)
97 \ - <SID>XmlIndentWithPattern(a:line, '.\{-}/>'))) + a:add
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 else
Bram Moolenaar9d87a372018-12-18 21:41:50 +010099 return a:add
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100 endif
101endfun
102
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100103" Main indent function
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104fun! XmlIndentGet(lnum, use_syntax_check)
105 " Find a non-empty line above the current line.
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100106 if prevnonblank(a:lnum - 1) == 0
107 " Hit the start of the file, use zero indent.
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100108 return 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109 endif
Bram Moolenaar314dd792019-02-03 15:27:20 +0100110 " Find previous line with a tag (regardless whether open or closed,
Bram Moolenaar54775062019-07-31 21:07:14 +0200111 " but always restrict the match to a line before the current one
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100112 " Note: xml declaration: <?xml version="1.0"?>
113 " won't be found, as it is not a legal tag name
Bram Moolenaar54775062019-07-31 21:07:14 +0200114 let ptag_pattern = '\%(.\{-}<[/:A-Z_a-z]\)'. '\%(\&\%<'. a:lnum .'l\)'
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100115 let ptag = search(ptag_pattern, 'bnW')
116 " no previous tag
117 if ptag == 0
118 return 0
119 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
Bram Moolenaar54775062019-07-31 21:07:14 +0200121 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)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127 if a:use_syntax_check
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100128 let check_lnum = <SID>XmlIndentSynCheck(ptag)
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100129 let check_alnum = <SID>XmlIndentSynCheck(a:lnum)
130 if check_lnum == 0 || check_alnum == 0
131 return indent(a:lnum)
132 endif
Bram Moolenaar54775062019-07-31 21:07:14 +0200133 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')
Bram Moolenaar23515b42020-11-29 14:36:24 +0100135 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')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 endif
139
Bram Moolenaar54775062019-07-31 21:07:14 +0200140 if syn_name_end =~ 'Comment' && syn_name_start =~ 'Comment'
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100141 return <SID>XmlIndentComment(a:lnum)
Bram Moolenaar4ceaa3a2019-12-03 22:49:09 +0100142 elseif empty(syn_name_start) && empty(syn_name_end) && a:use_syntax_check
Bram Moolenaar54775062019-07-31 21:07:14 +0200143 " non-xml tag content: use indent from 'autoindent'
Bram Moolenaar23515b42020-11-29 14:36:24 +0100144 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
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100154 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100156 " Get indent from previous tag line
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100157 let ind = <SID>XmlIndentSum(pline, -1, pind)
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100158 " Determine indent from current line
Bram Moolenaar54775062019-07-31 21:07:14 +0200159 let ind = <SID>XmlIndentSum(curline, 0, ind)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 return ind
161endfun
162
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100163func! <SID>IsXMLContinuation(line)
164 " Checks, whether or not the line matches a start-of-tag
Bram Moolenaar4ceaa3a2019-12-03 22:49:09 +0100165 return a:line !~ '^\s*<' && &ft is# 'xml'
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100166endfunc
167
168func! <SID>HasNoTagEnd(line)
169 " Checks whether or not the line matches '>' (so finishes a tag)
170 return a:line !~ '>\s*$'
171endfunc
172
Bram Moolenaar23515b42020-11-29 14:36:24 +0100173func! <SID>IsXMLEmptyClosingTag(line)
174 " Checks whether the line ends with an empty closing tag such as <lb/>
175 return a:line =~? '<[^>]*/>\s*$'
176endfunc
177
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100178" return indent for a commented line,
Bram Moolenaar54775062019-07-31 21:07:14 +0200179" the middle part might be indented one additional level
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100180func! <SID>XmlIndentComment(lnum)
Bram Moolenaar23515b42020-11-29 14:36:24 +0100181 let ptagopen = search('.\{-}<[:A-Z_a-z]\_[^/]\{-}>.\{-}', 'bnW')
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100182 let ptagclose = search(b:xml_indent_close, 'bnW')
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100183 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
Bram Moolenaar23515b42020-11-29 14:36:24 +0100187 " 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
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100195 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
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100202 return indent(search('<!--', 'bnW'))
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100203 else
204 " middle part of comment, add one additional level
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100205 return indent(search('<!--', 'bnW')) + shiftwidth()
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100206 endif
207endfunc
208
Bram Moolenaar8e52a592012-05-18 21:49:28 +0200209let &cpo = s:keepcpo
210unlet s:keepcpo
211
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100212" vim:ts=4 et sts=-1 sw=0