blob: ce992c589aa1b5643baaf04c565e120a5ba5cc4e [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>
5" Last Changed: 2019 Oct 24
Bram Moolenaar9d87a372018-12-18 21:41:50 +01006" Last Change:
Bram Moolenaar54775062019-07-31 21:07:14 +02007" 20190726 - Correctly handle non-tagged data
Bram Moolenaar63b74a82019-03-24 15:09:13 +01008" 20190204 - correctly handle wrap tags
9" https://github.com/chrisbra/vim-xml-ftplugin/issues/5
Bram Moolenaar314dd792019-02-03 15:27:20 +010010" 20190128 - Make sure to find previous tag
11" https://github.com/chrisbra/vim-xml-ftplugin/issues/4
Bram Moolenaar9d87a372018-12-18 21:41:50 +010012" 20181116 - Fix indentation when tags start with a colon or an underscore
13" https://github.com/vim/vim/pull/926
14" 20181022 - Do not overwrite indentkeys setting
15" https://github.com/chrisbra/vim-xml-ftplugin/issues/1
16" 20180724 - Correctly indent xml comments https://github.com/vim/vim/issues/3200
17"
18" Notes:
19" 1) does not indent pure non-xml code (e.g. embedded scripts)
20" 2) will be confused by unbalanced tags in comments
21" or CDATA sections.
22" 2009-05-26 patch by Nikolai Weibull
23" TODO: implement pre-like tags, see xml_indent_open / xml_indent_close
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25" Only load this indent file when no other was loaded.
26if exists("b:did_indent")
27 finish
28endif
29let b:did_indent = 1
Bram Moolenaar8e52a592012-05-18 21:49:28 +020030let s:keepcpo= &cpo
31set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
33" [-- local settings (must come before aborting the script) --]
Bram Moolenaar9d87a372018-12-18 21:41:50 +010034" Attention: Parameter use_syntax_check is used by the docbk.vim indent script
Bram Moolenaar071d4272004-06-13 20:20:40 +000035setlocal indentexpr=XmlIndentGet(v:lnum,1)
Bram Moolenaarba3ff532018-11-04 14:45:49 +010036setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,},!^F
Bram Moolenaar54775062019-07-31 21:07:14 +020037" autoindent: used when the indentexpr returns -1
38setlocal autoindent
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
Bram Moolenaar071d4272004-06-13 20:20:40 +000040if !exists('b:xml_indent_open')
Bram Moolenaar9d87a372018-12-18 21:41:50 +010041 let b:xml_indent_open = '.\{-}<[:A-Z_a-z]'
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 " pre tag, e.g. <address>
43 " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!'
44endif
45
46if !exists('b:xml_indent_close')
47 let b:xml_indent_close = '.\{-}</'
48 " end pre tag, e.g. </address>
49 " let b:xml_indent_close = '.\{-}</\(address\)\@!'
50endif
51
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020052let &cpo = s:keepcpo
53unlet s:keepcpo
54
Bram Moolenaar071d4272004-06-13 20:20:40 +000055" [-- finish, if the function already exists --]
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020056if exists('*XmlIndentGet')
Bram Moolenaar9d87a372018-12-18 21:41:50 +010057 finish
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020058endif
59
60let s:keepcpo= &cpo
61set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000062
63fun! <SID>XmlIndentWithPattern(line, pat)
64 let s = substitute('x'.a:line, a:pat, "\1", 'g')
65 return strlen(substitute(s, "[^\1].*$", '', ''))
66endfun
67
68" [-- check if it's xml --]
69fun! <SID>XmlIndentSynCheck(lnum)
Bram Moolenaar9d87a372018-12-18 21:41:50 +010070 if &syntax != ''
71 let syn1 = synIDattr(synID(a:lnum, 1, 1), 'name')
72 let syn2 = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name')
73 if syn1 != '' && syn1 !~ 'xml' && syn2 != '' && syn2 !~ 'xml'
74 " don't indent pure non-xml code
75 return 0
76 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000077 endif
78 return 1
79endfun
80
81" [-- return the sum of indents of a:lnum --]
Bram Moolenaar63b74a82019-03-24 15:09:13 +010082fun! <SID>XmlIndentSum(line, style, add)
83 if <SID>IsXMLContinuation(a:line) && a:style == 0
84 " no complete tag, add one additional indent level
85 " but only for the current line
86 return a:add + shiftwidth()
87 elseif <SID>HasNoTagEnd(a:line)
88 " no complete tag, return initial indent
89 return a:add
90 endif
91 if a:style == match(a:line, '^\s*</')
Bram Moolenaar9d87a372018-12-18 21:41:50 +010092 return (shiftwidth() *
Bram Moolenaar63b74a82019-03-24 15:09:13 +010093 \ (<SID>XmlIndentWithPattern(a:line, b:xml_indent_open)
94 \ - <SID>XmlIndentWithPattern(a:line, b:xml_indent_close)
95 \ - <SID>XmlIndentWithPattern(a:line, '.\{-}/>'))) + a:add
Bram Moolenaar071d4272004-06-13 20:20:40 +000096 else
Bram Moolenaar9d87a372018-12-18 21:41:50 +010097 return a:add
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 endif
99endfun
100
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100101" Main indent function
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102fun! XmlIndentGet(lnum, use_syntax_check)
103 " Find a non-empty line above the current line.
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100104 if prevnonblank(a:lnum - 1) == 0
105 " Hit the start of the file, use zero indent.
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100106 return 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107 endif
Bram Moolenaar314dd792019-02-03 15:27:20 +0100108 " Find previous line with a tag (regardless whether open or closed,
Bram Moolenaar54775062019-07-31 21:07:14 +0200109 " but always restrict the match to a line before the current one
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100110 " Note: xml declaration: <?xml version="1.0"?>
111 " won't be found, as it is not a legal tag name
Bram Moolenaar54775062019-07-31 21:07:14 +0200112 let ptag_pattern = '\%(.\{-}<[/:A-Z_a-z]\)'. '\%(\&\%<'. a:lnum .'l\)'
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100113 let ptag = search(ptag_pattern, 'bnW')
114 " no previous tag
115 if ptag == 0
116 return 0
117 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
Bram Moolenaar54775062019-07-31 21:07:14 +0200119 let pline = getline(ptag)
120 let pind = indent(ptag)
121
122 let syn_name_start = '' " Syntax element at start of line (excluding whitespace)
123 let syn_name_end = '' " Syntax element at end of line
124 let curline = getline(a:lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125 if a:use_syntax_check
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100126 let check_lnum = <SID>XmlIndentSynCheck(ptag)
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100127 let check_alnum = <SID>XmlIndentSynCheck(a:lnum)
128 if check_lnum == 0 || check_alnum == 0
129 return indent(a:lnum)
130 endif
Bram Moolenaar54775062019-07-31 21:07:14 +0200131 let syn_name_end = synIDattr(synID(a:lnum, strlen(curline) - 1, 1), 'name')
132 let syn_name_start = synIDattr(synID(a:lnum, match(curline, '\S') + 1, 1), 'name')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 endif
134
Bram Moolenaar54775062019-07-31 21:07:14 +0200135 if syn_name_end =~ 'Comment' && syn_name_start =~ 'Comment'
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100136 return <SID>XmlIndentComment(a:lnum)
Bram Moolenaar54775062019-07-31 21:07:14 +0200137 elseif empty(syn_name_start) && empty(syn_name_end)
138 " non-xml tag content: use indent from 'autoindent'
139 return pind + shiftwidth()
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100140 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100142 " Get indent from previous tag line
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100143 let ind = <SID>XmlIndentSum(pline, -1, pind)
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100144 " Determine indent from current line
Bram Moolenaar54775062019-07-31 21:07:14 +0200145 let ind = <SID>XmlIndentSum(curline, 0, ind)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 return ind
147endfun
148
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100149func! <SID>IsXMLContinuation(line)
150 " Checks, whether or not the line matches a start-of-tag
151 return a:line !~ '^\s*<'
152endfunc
153
154func! <SID>HasNoTagEnd(line)
155 " Checks whether or not the line matches '>' (so finishes a tag)
156 return a:line !~ '>\s*$'
157endfunc
158
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100159" return indent for a commented line,
Bram Moolenaar54775062019-07-31 21:07:14 +0200160" the middle part might be indented one additional level
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100161func! <SID>XmlIndentComment(lnum)
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100162 let ptagopen = search(b:xml_indent_open, 'bnW')
163 let ptagclose = search(b:xml_indent_close, 'bnW')
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100164 if getline(a:lnum) =~ '<!--'
165 " if previous tag was a closing tag, do not add
166 " one additional level of indent
167 if ptagclose > ptagopen && a:lnum > ptagclose
168 return indent(ptagclose)
169 else
170 " start of comment, add one indentation level
171 return indent(ptagopen) + shiftwidth()
172 endif
173 elseif getline(a:lnum) =~ '-->'
174 " end of comment, same as start of comment
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100175 return indent(search('<!--', 'bnW'))
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100176 else
177 " middle part of comment, add one additional level
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100178 return indent(search('<!--', 'bnW')) + shiftwidth()
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100179 endif
180endfunc
181
Bram Moolenaar8e52a592012-05-18 21:49:28 +0200182let &cpo = s:keepcpo
183unlet s:keepcpo
184
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100185" vim:ts=4 et sts=-1 sw=0