blob: ad22de1d50cf54f82f225eb9d74ea02706e2efb7 [file] [log] [blame]
Bram Moolenaar9d87a372018-12-18 21:41:50 +01001" Language: xml
2" Repository: https://github.com/chrisbra/vim-xml-ftplugin
Bram Moolenaar63b74a82019-03-24 15:09:13 +01003" Last Changed: Feb 04, 2019
Bram Moolenaar9d87a372018-12-18 21:41:50 +01004" Maintainer: Christian Brabandt <cb@256bit.org>
5" Previous Maintainer: Johannes Zellner <johannes@zellner.org>
6" Last Change:
Bram Moolenaar63b74a82019-03-24 15:09:13 +01007" 20190204 - correctly handle wrap tags
8" https://github.com/chrisbra/vim-xml-ftplugin/issues/5
Bram Moolenaar314dd792019-02-03 15:27:20 +01009" 20190128 - Make sure to find previous tag
10" https://github.com/chrisbra/vim-xml-ftplugin/issues/4
Bram Moolenaar9d87a372018-12-18 21:41:50 +010011" 20181116 - Fix indentation when tags start with a colon or an underscore
12" https://github.com/vim/vim/pull/926
13" 20181022 - Do not overwrite indentkeys setting
14" https://github.com/chrisbra/vim-xml-ftplugin/issues/1
15" 20180724 - Correctly indent xml comments https://github.com/vim/vim/issues/3200
16"
17" Notes:
18" 1) does not indent pure non-xml code (e.g. embedded scripts)
19" 2) will be confused by unbalanced tags in comments
20" or CDATA sections.
21" 2009-05-26 patch by Nikolai Weibull
22" TODO: implement pre-like tags, see xml_indent_open / xml_indent_close
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
24" Only load this indent file when no other was loaded.
25if exists("b:did_indent")
26 finish
27endif
28let b:did_indent = 1
Bram Moolenaar8e52a592012-05-18 21:49:28 +020029let s:keepcpo= &cpo
30set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000031
32" [-- local settings (must come before aborting the script) --]
Bram Moolenaar9d87a372018-12-18 21:41:50 +010033" Attention: Parameter use_syntax_check is used by the docbk.vim indent script
Bram Moolenaar071d4272004-06-13 20:20:40 +000034setlocal indentexpr=XmlIndentGet(v:lnum,1)
Bram Moolenaarba3ff532018-11-04 14:45:49 +010035setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,},!^F
Bram Moolenaar071d4272004-06-13 20:20:40 +000036
Bram Moolenaar071d4272004-06-13 20:20:40 +000037if !exists('b:xml_indent_open')
Bram Moolenaar9d87a372018-12-18 21:41:50 +010038 let b:xml_indent_open = '.\{-}<[:A-Z_a-z]'
Bram Moolenaar071d4272004-06-13 20:20:40 +000039 " pre tag, e.g. <address>
40 " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!'
41endif
42
43if !exists('b:xml_indent_close')
44 let b:xml_indent_close = '.\{-}</'
45 " end pre tag, e.g. </address>
46 " let b:xml_indent_close = '.\{-}</\(address\)\@!'
47endif
48
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020049let &cpo = s:keepcpo
50unlet s:keepcpo
51
Bram Moolenaar071d4272004-06-13 20:20:40 +000052" [-- finish, if the function already exists --]
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020053if exists('*XmlIndentGet')
Bram Moolenaar9d87a372018-12-18 21:41:50 +010054 finish
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020055endif
56
57let s:keepcpo= &cpo
58set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000059
60fun! <SID>XmlIndentWithPattern(line, pat)
61 let s = substitute('x'.a:line, a:pat, "\1", 'g')
62 return strlen(substitute(s, "[^\1].*$", '', ''))
63endfun
64
65" [-- check if it's xml --]
66fun! <SID>XmlIndentSynCheck(lnum)
Bram Moolenaar9d87a372018-12-18 21:41:50 +010067 if &syntax != ''
68 let syn1 = synIDattr(synID(a:lnum, 1, 1), 'name')
69 let syn2 = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name')
70 if syn1 != '' && syn1 !~ 'xml' && syn2 != '' && syn2 !~ 'xml'
71 " don't indent pure non-xml code
72 return 0
73 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 endif
75 return 1
76endfun
77
78" [-- return the sum of indents of a:lnum --]
Bram Moolenaar63b74a82019-03-24 15:09:13 +010079fun! <SID>XmlIndentSum(line, style, add)
80 if <SID>IsXMLContinuation(a:line) && a:style == 0
81 " no complete tag, add one additional indent level
82 " but only for the current line
83 return a:add + shiftwidth()
84 elseif <SID>HasNoTagEnd(a:line)
85 " no complete tag, return initial indent
86 return a:add
87 endif
88 if a:style == match(a:line, '^\s*</')
Bram Moolenaar9d87a372018-12-18 21:41:50 +010089 return (shiftwidth() *
Bram Moolenaar63b74a82019-03-24 15:09:13 +010090 \ (<SID>XmlIndentWithPattern(a:line, b:xml_indent_open)
91 \ - <SID>XmlIndentWithPattern(a:line, b:xml_indent_close)
92 \ - <SID>XmlIndentWithPattern(a:line, '.\{-}/>'))) + a:add
Bram Moolenaar071d4272004-06-13 20:20:40 +000093 else
Bram Moolenaar9d87a372018-12-18 21:41:50 +010094 return a:add
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 endif
96endfun
97
Bram Moolenaar9d87a372018-12-18 21:41:50 +010098" Main indent function
Bram Moolenaar071d4272004-06-13 20:20:40 +000099fun! XmlIndentGet(lnum, use_syntax_check)
100 " Find a non-empty line above the current line.
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100101 if prevnonblank(a:lnum - 1) == 0
102 " Hit the start of the file, use zero indent.
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100103 return 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104 endif
Bram Moolenaar314dd792019-02-03 15:27:20 +0100105 " Find previous line with a tag (regardless whether open or closed,
106 " but always start restrict the match to a line before the current one
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100107 " Note: xml declaration: <?xml version="1.0"?>
108 " won't be found, as it is not a legal tag name
Bram Moolenaar314dd792019-02-03 15:27:20 +0100109 let ptag_pattern = '\%(.\{-}<[/:A-Z_a-z]\)'. '\%(\&\%<'. line('.').'l\)'
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100110 let ptag = search(ptag_pattern, 'bnW')
111 " no previous tag
112 if ptag == 0
113 return 0
114 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115
Bram Moolenaar314dd792019-02-03 15:27:20 +0100116 let syn_name = ''
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 if a:use_syntax_check
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100118 let check_lnum = <SID>XmlIndentSynCheck(ptag)
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100119 let check_alnum = <SID>XmlIndentSynCheck(a:lnum)
120 if check_lnum == 0 || check_alnum == 0
121 return indent(a:lnum)
122 endif
123 let syn_name = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 endif
125
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100126 if syn_name =~ 'Comment'
127 return <SID>XmlIndentComment(a:lnum)
128 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100130 let pline = getline(ptag)
131 let pind = indent(ptag)
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100132 " Get indent from previous tag line
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100133 let ind = <SID>XmlIndentSum(pline, -1, pind)
134 let t_ind = ind
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100135 " Determine indent from current line
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100136 let ind = <SID>XmlIndentSum(getline(a:lnum), 0, ind)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 return ind
138endfun
139
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100140func! <SID>IsXMLContinuation(line)
141 " Checks, whether or not the line matches a start-of-tag
142 return a:line !~ '^\s*<'
143endfunc
144
145func! <SID>HasNoTagEnd(line)
146 " Checks whether or not the line matches '>' (so finishes a tag)
147 return a:line !~ '>\s*$'
148endfunc
149
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100150" return indent for a commented line,
151" the middle part might be indented on additional level
152func! <SID>XmlIndentComment(lnum)
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100153 let ptagopen = search(b:xml_indent_open, 'bnW')
154 let ptagclose = search(b:xml_indent_close, 'bnW')
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100155 if getline(a:lnum) =~ '<!--'
156 " if previous tag was a closing tag, do not add
157 " one additional level of indent
158 if ptagclose > ptagopen && a:lnum > ptagclose
159 return indent(ptagclose)
160 else
161 " start of comment, add one indentation level
162 return indent(ptagopen) + shiftwidth()
163 endif
164 elseif getline(a:lnum) =~ '-->'
165 " end of comment, same as start of comment
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100166 return indent(search('<!--', 'bnW'))
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100167 else
168 " middle part of comment, add one additional level
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100169 return indent(search('<!--', 'bnW')) + shiftwidth()
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100170 endif
171endfunc
172
Bram Moolenaar8e52a592012-05-18 21:49:28 +0200173let &cpo = s:keepcpo
174unlet s:keepcpo
175
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100176" vim:ts=4 et sts=-1 sw=0