xref: /vim-8.2.3635/runtime/indent/xml.vim (revision 147e7d0c)
1"     Language: xml
2"   Repository: https://github.com/chrisbra/vim-xml-ftplugin
3" Last Changed: Dec 07th, 2018
4"   Maintainer: Christian Brabandt <[email protected]>
5" Previous Maintainer:  Johannes Zellner <[email protected]>
6" Last Change:
7" 20181116 - Fix indentation when tags start with a colon or an underscore
8"            https://github.com/vim/vim/pull/926
9" 20181022 - Do not overwrite indentkeys setting
10"            https://github.com/chrisbra/vim-xml-ftplugin/issues/1
11" 20180724 - Correctly indent xml comments https://github.com/vim/vim/issues/3200
12"
13" Notes:
14"   1) does not indent pure non-xml code (e.g. embedded scripts)
15"       2) will be confused by unbalanced tags in comments
16"       or CDATA sections.
17"       2009-05-26 patch by Nikolai Weibull
18" TODO:     implement pre-like tags, see xml_indent_open / xml_indent_close
19
20" Only load this indent file when no other was loaded.
21if exists("b:did_indent")
22    finish
23endif
24let b:did_indent = 1
25let s:keepcpo= &cpo
26set cpo&vim
27
28" [-- local settings (must come before aborting the script) --]
29" Attention: Parameter use_syntax_check is used by the docbk.vim indent script
30setlocal indentexpr=XmlIndentGet(v:lnum,1)
31setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,},!^F
32
33if !exists('b:xml_indent_open')
34    let b:xml_indent_open = '.\{-}<[:A-Z_a-z]'
35    " pre tag, e.g. <address>
36    " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!'
37endif
38
39if !exists('b:xml_indent_close')
40    let b:xml_indent_close = '.\{-}</'
41    " end pre tag, e.g. </address>
42    " let b:xml_indent_close = '.\{-}</\(address\)\@!'
43endif
44
45let &cpo = s:keepcpo
46unlet s:keepcpo
47
48" [-- finish, if the function already exists --]
49if exists('*XmlIndentGet')
50    finish
51endif
52
53let s:keepcpo= &cpo
54set cpo&vim
55
56fun! <SID>XmlIndentWithPattern(line, pat)
57    let s = substitute('x'.a:line, a:pat, "\1", 'g')
58    return strlen(substitute(s, "[^\1].*$", '', ''))
59endfun
60
61" [-- check if it's xml --]
62fun! <SID>XmlIndentSynCheck(lnum)
63    if &syntax != ''
64        let syn1 = synIDattr(synID(a:lnum, 1, 1), 'name')
65        let syn2 = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name')
66        if syn1 != '' && syn1 !~ 'xml' && syn2 != '' && syn2 !~ 'xml'
67            " don't indent pure non-xml code
68            return 0
69        endif
70    endif
71    return 1
72endfun
73
74" [-- return the sum of indents of a:lnum --]
75fun! <SID>XmlIndentSum(lnum, style, add)
76    let line = getline(a:lnum)
77    if a:style == match(line, '^\s*</')
78        return (shiftwidth() *
79        \  (<SID>XmlIndentWithPattern(line, b:xml_indent_open)
80        \ - <SID>XmlIndentWithPattern(line, b:xml_indent_close)
81        \ - <SID>XmlIndentWithPattern(line, '.\{-}/>'))) + a:add
82    else
83        return a:add
84    endif
85endfun
86
87" Main indent function
88fun! XmlIndentGet(lnum, use_syntax_check)
89    " Find a non-empty line above the current line.
90    let plnum = prevnonblank(a:lnum - 1)
91    " Find previous line with a tag (regardless whether open or closed)
92    let ptag = search('.\{-}<[/:A-Z_a-z]', 'bnw')
93
94    " Hit the start of the file, use zero indent.
95    if plnum == 0
96        return 0
97    endif
98    let syn_name = ''
99
100    if a:use_syntax_check
101        let check_lnum = <SID>XmlIndentSynCheck(plnum)
102        let check_alnum = <SID>XmlIndentSynCheck(a:lnum)
103        if check_lnum == 0 || check_alnum == 0
104            return indent(a:lnum)
105        endif
106        let syn_name = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name')
107    endif
108
109    if syn_name =~ 'Comment'
110        return <SID>XmlIndentComment(a:lnum)
111    endif
112
113    " Get indent from previous tag line
114    let ind = <SID>XmlIndentSum(ptag, -1, indent(ptag))
115    " Determine indent from current line
116    let ind = <SID>XmlIndentSum(a:lnum, 0, ind)
117    return ind
118endfun
119
120" return indent for a commented line,
121" the middle part might be indented on additional level
122func! <SID>XmlIndentComment(lnum)
123    let ptagopen = search(b:xml_indent_open, 'bnw')
124    let ptagclose = search(b:xml_indent_close, 'bnw')
125    if getline(a:lnum) =~ '<!--'
126        " if previous tag was a closing tag, do not add
127        " one additional level of indent
128        if ptagclose > ptagopen && a:lnum > ptagclose
129            return indent(ptagclose)
130        else
131            " start of comment, add one indentation level
132            return indent(ptagopen) + shiftwidth()
133        endif
134    elseif getline(a:lnum) =~ '-->'
135        " end of comment, same as start of comment
136        return indent(search('<!--', 'bnw'))
137    else
138        " middle part of comment, add one additional level
139        return indent(search('<!--', 'bnw')) + shiftwidth()
140    endif
141endfunc
142
143let &cpo = s:keepcpo
144unlet s:keepcpo
145
146" vim:ts=4 et sts=-1 sw=0
147