blob: 413a3ddb53b61fbc383a9824e76a4c56fecbe725 [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 Moolenaar4ceaa3a2019-12-03 22:49:09 +01005" Last Changed: 2019 Dec 02
Bram Moolenaar9d87a372018-12-18 21:41:50 +01006" Last Change:
Bram Moolenaar4ceaa3a2019-12-03 22:49:09 +01007" 20191202 - Handle docbk filetype
Bram Moolenaar54775062019-07-31 21:07:14 +02008" 20190726 - Correctly handle non-tagged data
Bram Moolenaar63b74a82019-03-24 15:09:13 +01009" 20190204 - correctly handle wrap tags
10" https://github.com/chrisbra/vim-xml-ftplugin/issues/5
Bram Moolenaar314dd792019-02-03 15:27:20 +010011" 20190128 - Make sure to find previous tag
12" https://github.com/chrisbra/vim-xml-ftplugin/issues/4
Bram Moolenaar9d87a372018-12-18 21:41:50 +010013" 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
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
26" Only load this indent file when no other was loaded.
27if exists("b:did_indent")
28 finish
29endif
30let b:did_indent = 1
Bram Moolenaar8e52a592012-05-18 21:49:28 +020031let s:keepcpo= &cpo
32set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000033
34" [-- local settings (must come before aborting the script) --]
Bram Moolenaar9d87a372018-12-18 21:41:50 +010035" Attention: Parameter use_syntax_check is used by the docbk.vim indent script
Bram Moolenaar071d4272004-06-13 20:20:40 +000036setlocal indentexpr=XmlIndentGet(v:lnum,1)
Bram Moolenaarba3ff532018-11-04 14:45:49 +010037setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,},!^F
Bram Moolenaar54775062019-07-31 21:07:14 +020038" autoindent: used when the indentexpr returns -1
39setlocal autoindent
Bram Moolenaar071d4272004-06-13 20:20:40 +000040
Bram Moolenaar071d4272004-06-13 20:20:40 +000041if !exists('b:xml_indent_open')
Bram Moolenaar9d87a372018-12-18 21:41:50 +010042 let b:xml_indent_open = '.\{-}<[:A-Z_a-z]'
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 " 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
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020053let &cpo = s:keepcpo
54unlet s:keepcpo
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056" [-- finish, if the function already exists --]
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020057if exists('*XmlIndentGet')
Bram Moolenaar9d87a372018-12-18 21:41:50 +010058 finish
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020059endif
60
61let s:keepcpo= &cpo
62set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
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)
Bram Moolenaar9d87a372018-12-18 21:41:50 +010071 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
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 endif
79 return 1
80endfun
81
82" [-- return the sum of indents of a:lnum --]
Bram Moolenaar63b74a82019-03-24 15:09:13 +010083fun! <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*</')
Bram Moolenaar9d87a372018-12-18 21:41:50 +010093 return (shiftwidth() *
Bram Moolenaar63b74a82019-03-24 15:09:13 +010094 \ (<SID>XmlIndentWithPattern(a:line, b:xml_indent_open)
95 \ - <SID>XmlIndentWithPattern(a:line, b:xml_indent_close)
96 \ - <SID>XmlIndentWithPattern(a:line, '.\{-}/>'))) + a:add
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 else
Bram Moolenaar9d87a372018-12-18 21:41:50 +010098 return a:add
Bram Moolenaar071d4272004-06-13 20:20:40 +000099 endif
100endfun
101
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100102" Main indent function
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103fun! XmlIndentGet(lnum, use_syntax_check)
104 " Find a non-empty line above the current line.
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100105 if prevnonblank(a:lnum - 1) == 0
106 " Hit the start of the file, use zero indent.
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100107 return 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108 endif
Bram Moolenaar314dd792019-02-03 15:27:20 +0100109 " Find previous line with a tag (regardless whether open or closed,
Bram Moolenaar54775062019-07-31 21:07:14 +0200110 " but always restrict the match to a line before the current one
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100111 " Note: xml declaration: <?xml version="1.0"?>
112 " won't be found, as it is not a legal tag name
Bram Moolenaar54775062019-07-31 21:07:14 +0200113 let ptag_pattern = '\%(.\{-}<[/:A-Z_a-z]\)'. '\%(\&\%<'. a:lnum .'l\)'
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100114 let ptag = search(ptag_pattern, 'bnW')
115 " no previous tag
116 if ptag == 0
117 return 0
118 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119
Bram Moolenaar54775062019-07-31 21:07:14 +0200120 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)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126 if a:use_syntax_check
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100127 let check_lnum = <SID>XmlIndentSynCheck(ptag)
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100128 let check_alnum = <SID>XmlIndentSynCheck(a:lnum)
129 if check_lnum == 0 || check_alnum == 0
130 return indent(a:lnum)
131 endif
Bram Moolenaar54775062019-07-31 21:07:14 +0200132 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')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 endif
135
Bram Moolenaar54775062019-07-31 21:07:14 +0200136 if syn_name_end =~ 'Comment' && syn_name_start =~ 'Comment'
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100137 return <SID>XmlIndentComment(a:lnum)
Bram Moolenaar4ceaa3a2019-12-03 22:49:09 +0100138 elseif empty(syn_name_start) && empty(syn_name_end) && a:use_syntax_check
Bram Moolenaar54775062019-07-31 21:07:14 +0200139 " non-xml tag content: use indent from 'autoindent'
140 return pind + shiftwidth()
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100141 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100143 " Get indent from previous tag line
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100144 let ind = <SID>XmlIndentSum(pline, -1, pind)
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100145 " Determine indent from current line
Bram Moolenaar54775062019-07-31 21:07:14 +0200146 let ind = <SID>XmlIndentSum(curline, 0, ind)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 return ind
148endfun
149
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100150func! <SID>IsXMLContinuation(line)
151 " Checks, whether or not the line matches a start-of-tag
Bram Moolenaar4ceaa3a2019-12-03 22:49:09 +0100152 return a:line !~ '^\s*<' && &ft is# 'xml'
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100153endfunc
154
155func! <SID>HasNoTagEnd(line)
156 " Checks whether or not the line matches '>' (so finishes a tag)
157 return a:line !~ '>\s*$'
158endfunc
159
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100160" return indent for a commented line,
Bram Moolenaar54775062019-07-31 21:07:14 +0200161" the middle part might be indented one additional level
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100162func! <SID>XmlIndentComment(lnum)
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100163 let ptagopen = search(b:xml_indent_open, 'bnW')
164 let ptagclose = search(b:xml_indent_close, 'bnW')
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100165 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
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100176 return indent(search('<!--', 'bnW'))
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100177 else
178 " middle part of comment, add one additional level
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100179 return indent(search('<!--', 'bnW')) + shiftwidth()
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100180 endif
181endfunc
182
Bram Moolenaar8e52a592012-05-18 21:49:28 +0200183let &cpo = s:keepcpo
184unlet s:keepcpo
185
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100186" vim:ts=4 et sts=-1 sw=0