blob: 5bf53ad1f8f5d56b86802c24775e5102a782c651 [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 Moolenaarfa3b7232021-12-24 13:18:38 +000042let b:undo_indent = "setl ai< inde< indk<"
43
Bram Moolenaar071d4272004-06-13 20:20:40 +000044if !exists('b:xml_indent_open')
Bram Moolenaar9d87a372018-12-18 21:41:50 +010045 let b:xml_indent_open = '.\{-}<[:A-Z_a-z]'
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 " pre tag, e.g. <address>
47 " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!'
48endif
49
50if !exists('b:xml_indent_close')
Bram Moolenaar23515b42020-11-29 14:36:24 +010051 let b:xml_indent_close = '.\{-}</\|/>.\{-}'
Bram Moolenaar071d4272004-06-13 20:20:40 +000052 " end pre tag, e.g. </address>
53 " let b:xml_indent_close = '.\{-}</\(address\)\@!'
54endif
55
Bram Moolenaarfa3b7232021-12-24 13:18:38 +000056if !exists('b:xml_indent_continuation_filetype')
57 let b:xml_indent_continuation_filetype = 'xml'
58endif
59
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020060let &cpo = s:keepcpo
61unlet s:keepcpo
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063" [-- finish, if the function already exists --]
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020064if exists('*XmlIndentGet')
Bram Moolenaar9d87a372018-12-18 21:41:50 +010065 finish
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020066endif
67
68let s:keepcpo= &cpo
69set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
71fun! <SID>XmlIndentWithPattern(line, pat)
72 let s = substitute('x'.a:line, a:pat, "\1", 'g')
73 return strlen(substitute(s, "[^\1].*$", '', ''))
74endfun
75
76" [-- check if it's xml --]
77fun! <SID>XmlIndentSynCheck(lnum)
Bram Moolenaar9d87a372018-12-18 21:41:50 +010078 if &syntax != ''
79 let syn1 = synIDattr(synID(a:lnum, 1, 1), 'name')
80 let syn2 = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name')
81 if syn1 != '' && syn1 !~ 'xml' && syn2 != '' && syn2 !~ 'xml'
82 " don't indent pure non-xml code
83 return 0
84 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 endif
86 return 1
87endfun
88
89" [-- return the sum of indents of a:lnum --]
Bram Moolenaar63b74a82019-03-24 15:09:13 +010090fun! <SID>XmlIndentSum(line, style, add)
Bram Moolenaar23515b42020-11-29 14:36:24 +010091 if <SID>IsXMLContinuation(a:line) && a:style == 0 && !<SID>IsXMLEmptyClosingTag(a:line)
Bram Moolenaar63b74a82019-03-24 15:09:13 +010092 " no complete tag, add one additional indent level
93 " but only for the current line
94 return a:add + shiftwidth()
95 elseif <SID>HasNoTagEnd(a:line)
96 " no complete tag, return initial indent
97 return a:add
98 endif
99 if a:style == match(a:line, '^\s*</')
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100100 return (shiftwidth() *
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100101 \ (<SID>XmlIndentWithPattern(a:line, b:xml_indent_open)
102 \ - <SID>XmlIndentWithPattern(a:line, b:xml_indent_close)
103 \ - <SID>XmlIndentWithPattern(a:line, '.\{-}/>'))) + a:add
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104 else
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100105 return a:add
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 endif
107endfun
108
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100109" Main indent function
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110fun! XmlIndentGet(lnum, use_syntax_check)
111 " Find a non-empty line above the current line.
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100112 if prevnonblank(a:lnum - 1) == 0
113 " Hit the start of the file, use zero indent.
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100114 return 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 endif
Bram Moolenaar314dd792019-02-03 15:27:20 +0100116 " Find previous line with a tag (regardless whether open or closed,
Bram Moolenaar54775062019-07-31 21:07:14 +0200117 " but always restrict the match to a line before the current one
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100118 " Note: xml declaration: <?xml version="1.0"?>
119 " won't be found, as it is not a legal tag name
Bram Moolenaar54775062019-07-31 21:07:14 +0200120 let ptag_pattern = '\%(.\{-}<[/:A-Z_a-z]\)'. '\%(\&\%<'. a:lnum .'l\)'
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100121 let ptag = search(ptag_pattern, 'bnW')
122 " no previous tag
123 if ptag == 0
124 return 0
125 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
Bram Moolenaar54775062019-07-31 21:07:14 +0200127 let pline = getline(ptag)
128 let pind = indent(ptag)
129
130 let syn_name_start = '' " Syntax element at start of line (excluding whitespace)
131 let syn_name_end = '' " Syntax element at end of line
132 let curline = getline(a:lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 if a:use_syntax_check
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100134 let check_lnum = <SID>XmlIndentSynCheck(ptag)
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100135 let check_alnum = <SID>XmlIndentSynCheck(a:lnum)
136 if check_lnum == 0 || check_alnum == 0
137 return indent(a:lnum)
138 endif
Bram Moolenaar54775062019-07-31 21:07:14 +0200139 let syn_name_end = synIDattr(synID(a:lnum, strlen(curline) - 1, 1), 'name')
140 let syn_name_start = synIDattr(synID(a:lnum, match(curline, '\S') + 1, 1), 'name')
Bram Moolenaar23515b42020-11-29 14:36:24 +0100141 let prev_syn_name_end = synIDattr(synID(ptag, strlen(pline) - 1, 1), 'name')
142 " not needed (yet?)
143 " let prev_syn_name_start = synIDattr(synID(ptag, match(pline, '\S') + 1, 1), 'name')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 endif
145
Bram Moolenaar54775062019-07-31 21:07:14 +0200146 if syn_name_end =~ 'Comment' && syn_name_start =~ 'Comment'
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100147 return <SID>XmlIndentComment(a:lnum)
Bram Moolenaar4ceaa3a2019-12-03 22:49:09 +0100148 elseif empty(syn_name_start) && empty(syn_name_end) && a:use_syntax_check
Bram Moolenaar54775062019-07-31 21:07:14 +0200149 " non-xml tag content: use indent from 'autoindent'
Bram Moolenaar23515b42020-11-29 14:36:24 +0100150 if pline =~ b:xml_indent_close
151 return pind
152 elseif !empty(prev_syn_name_end)
153 " only indent by an extra shiftwidth, if the previous line ends
154 " with an XML like tag
155 return pind + shiftwidth()
156 else
157 " no extra indent, looks like a text continuation line
158 return pind
159 endif
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100160 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100162 " Get indent from previous tag line
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100163 let ind = <SID>XmlIndentSum(pline, -1, pind)
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100164 " Determine indent from current line
Bram Moolenaar54775062019-07-31 21:07:14 +0200165 let ind = <SID>XmlIndentSum(curline, 0, ind)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 return ind
167endfun
168
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100169func! <SID>IsXMLContinuation(line)
170 " Checks, whether or not the line matches a start-of-tag
Bram Moolenaarfa3b7232021-12-24 13:18:38 +0000171 return a:line !~ '^\s*<' && &ft =~# b:xml_indent_continuation_filetype
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100172endfunc
173
174func! <SID>HasNoTagEnd(line)
175 " Checks whether or not the line matches '>' (so finishes a tag)
176 return a:line !~ '>\s*$'
177endfunc
178
Bram Moolenaar23515b42020-11-29 14:36:24 +0100179func! <SID>IsXMLEmptyClosingTag(line)
180 " Checks whether the line ends with an empty closing tag such as <lb/>
181 return a:line =~? '<[^>]*/>\s*$'
182endfunc
183
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100184" return indent for a commented line,
Bram Moolenaar54775062019-07-31 21:07:14 +0200185" the middle part might be indented one additional level
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100186func! <SID>XmlIndentComment(lnum)
Bram Moolenaar23515b42020-11-29 14:36:24 +0100187 let ptagopen = search('.\{-}<[:A-Z_a-z]\_[^/]\{-}>.\{-}', 'bnW')
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100188 let ptagclose = search(b:xml_indent_close, 'bnW')
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100189 if getline(a:lnum) =~ '<!--'
190 " if previous tag was a closing tag, do not add
191 " one additional level of indent
192 if ptagclose > ptagopen && a:lnum > ptagclose
Bram Moolenaar23515b42020-11-29 14:36:24 +0100193 " If the previous tag was closed on the same line as it was
194 " declared, we should indent with its indent level.
195 if !<SID>IsXMLContinuation(getline(ptagclose))
196 return indent(ptagclose)
197 else
198 return indent(ptagclose) - shiftwidth()
199 endif
200 elseif ptagclose == ptagopen
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100201 return indent(ptagclose)
202 else
203 " start of comment, add one indentation level
204 return indent(ptagopen) + shiftwidth()
205 endif
206 elseif getline(a:lnum) =~ '-->'
207 " end of comment, same as start of comment
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100208 return indent(search('<!--', 'bnW'))
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100209 else
210 " middle part of comment, add one additional level
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100211 return indent(search('<!--', 'bnW')) + shiftwidth()
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100212 endif
213endfunc
214
Bram Moolenaar8e52a592012-05-18 21:49:28 +0200215let &cpo = s:keepcpo
216unlet s:keepcpo
217
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100218" vim:ts=4 et sts=-1 sw=0