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