xref: /vim-8.2.3635/runtime/indent/xml.vim (revision f0d58efc)
1" Language:	xml
2" Repository:   https://github.com/chrisbra/vim-xml-ftplugin
3" Maintainer:	Christian Brabandt <[email protected]>
4" Previous Maintainer:	Johannes Zellner <[email protected]>
5" Last Change:	20181022 - Do not overwrite indentkeys setting
6"                          https://github.com/chrisbra/vim-xml-ftplugin/issues/1
7"             	20180724 - Correctly indent xml comments https://github.com/vim/vim/issues/3200
8" Notes:	1) does not indent pure non-xml code (e.g. embedded scripts)
9"		2) will be confused by unbalanced tags in comments
10"		or CDATA sections.
11"		2009-05-26 patch by Nikolai Weibull
12" TODO: 	implement pre-like tags, see xml_indent_open / xml_indent_close
13
14" Only load this indent file when no other was loaded.
15if exists("b:did_indent")
16    finish
17endif
18let b:did_indent = 1
19let s:keepcpo= &cpo
20set cpo&vim
21
22" [-- local settings (must come before aborting the script) --]
23setlocal indentexpr=XmlIndentGet(v:lnum,1)
24setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,},!^F
25
26if !exists('b:xml_indent_open')
27    let b:xml_indent_open = '.\{-}<\a'
28    " pre tag, e.g. <address>
29    " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!'
30endif
31
32if !exists('b:xml_indent_close')
33    let b:xml_indent_close = '.\{-}</'
34    " end pre tag, e.g. </address>
35    " let b:xml_indent_close = '.\{-}</\(address\)\@!'
36endif
37
38let &cpo = s:keepcpo
39unlet s:keepcpo
40
41" [-- finish, if the function already exists --]
42if exists('*XmlIndentGet')
43  finish
44endif
45
46let s:keepcpo= &cpo
47set cpo&vim
48
49fun! <SID>XmlIndentWithPattern(line, pat)
50    let s = substitute('x'.a:line, a:pat, "\1", 'g')
51    return strlen(substitute(s, "[^\1].*$", '', ''))
52endfun
53
54" [-- check if it's xml --]
55fun! <SID>XmlIndentSynCheck(lnum)
56    if '' != &syntax
57	let syn1 = synIDattr(synID(a:lnum, 1, 1), 'name')
58	let syn2 = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name')
59	if '' != syn1 && syn1 !~ 'xml' && '' != syn2 && syn2 !~ 'xml'
60	    " don't indent pure non-xml code
61	    return 0
62	endif
63    endif
64    return 1
65endfun
66
67" [-- return the sum of indents of a:lnum --]
68fun! <SID>XmlIndentSum(lnum, style, add)
69    let line = getline(a:lnum)
70    if a:style == match(line, '^\s*</')
71	return (shiftwidth() *
72	\  (<SID>XmlIndentWithPattern(line, b:xml_indent_open)
73	\ - <SID>XmlIndentWithPattern(line, b:xml_indent_close)
74	\ - <SID>XmlIndentWithPattern(line, '.\{-}/>'))) + a:add
75    else
76	return a:add
77    endif
78endfun
79
80fun! XmlIndentGet(lnum, use_syntax_check)
81    " Find a non-empty line above the current line.
82    let lnum = prevnonblank(a:lnum - 1)
83
84    " Hit the start of the file, use zero indent.
85    if lnum == 0
86	return 0
87    endif
88
89    if a:use_syntax_check
90	let check_lnum = <SID>XmlIndentSynCheck(lnum)
91	let check_alnum = <SID>XmlIndentSynCheck(a:lnum)
92	if 0 == check_lnum || 0 == check_alnum
93	    return indent(a:lnum)
94	elseif -1 == check_lnum || -1 == check_alnum
95	    return -1
96	endif
97    endif
98
99    let ind = <SID>XmlIndentSum(lnum, -1, indent(lnum))
100    let ind = <SID>XmlIndentSum(a:lnum, 0, ind)
101
102    return ind
103endfun
104
105let &cpo = s:keepcpo
106unlet s:keepcpo
107
108" vim:ts=8
109