blob: bc64aacfe1188db5e644c1caaa2750f8afad2186 [file] [log] [blame]
Bram Moolenaar9d87a372018-12-18 21:41:50 +01001" Language: xml
2" Repository: https://github.com/chrisbra/vim-xml-ftplugin
3" Last Changed: Dec 07th, 2018
4" Maintainer: Christian Brabandt <cb@256bit.org>
5" Previous Maintainer: Johannes Zellner <johannes@zellner.org>
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
Bram Moolenaar071d4272004-06-13 20:20:40 +000019
20" Only load this indent file when no other was loaded.
21if exists("b:did_indent")
22 finish
23endif
24let b:did_indent = 1
Bram Moolenaar8e52a592012-05-18 21:49:28 +020025let s:keepcpo= &cpo
26set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000027
28" [-- local settings (must come before aborting the script) --]
Bram Moolenaar9d87a372018-12-18 21:41:50 +010029" Attention: Parameter use_syntax_check is used by the docbk.vim indent script
Bram Moolenaar071d4272004-06-13 20:20:40 +000030setlocal indentexpr=XmlIndentGet(v:lnum,1)
Bram Moolenaarba3ff532018-11-04 14:45:49 +010031setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,},!^F
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar071d4272004-06-13 20:20:40 +000033if !exists('b:xml_indent_open')
Bram Moolenaar9d87a372018-12-18 21:41:50 +010034 let b:xml_indent_open = '.\{-}<[:A-Z_a-z]'
Bram Moolenaar071d4272004-06-13 20:20:40 +000035 " 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
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020045let &cpo = s:keepcpo
46unlet s:keepcpo
47
Bram Moolenaar071d4272004-06-13 20:20:40 +000048" [-- finish, if the function already exists --]
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020049if exists('*XmlIndentGet')
Bram Moolenaar9d87a372018-12-18 21:41:50 +010050 finish
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020051endif
52
53let s:keepcpo= &cpo
54set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
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)
Bram Moolenaar9d87a372018-12-18 21:41:50 +010063 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
Bram Moolenaar071d4272004-06-13 20:20:40 +000070 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*</')
Bram Moolenaar9d87a372018-12-18 21:41:50 +010078 return (shiftwidth() *
79 \ (<SID>XmlIndentWithPattern(line, b:xml_indent_open)
80 \ - <SID>XmlIndentWithPattern(line, b:xml_indent_close)
81 \ - <SID>XmlIndentWithPattern(line, '.\{-}/>'))) + a:add
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 else
Bram Moolenaar9d87a372018-12-18 21:41:50 +010083 return a:add
Bram Moolenaar071d4272004-06-13 20:20:40 +000084 endif
85endfun
86
Bram Moolenaar9d87a372018-12-18 21:41:50 +010087" Main indent function
Bram Moolenaar071d4272004-06-13 20:20:40 +000088fun! XmlIndentGet(lnum, use_syntax_check)
89 " Find a non-empty line above the current line.
Bram Moolenaar9d87a372018-12-18 21:41:50 +010090 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')
Bram Moolenaar071d4272004-06-13 20:20:40 +000093
94 " Hit the start of the file, use zero indent.
Bram Moolenaar9d87a372018-12-18 21:41:50 +010095 if plnum == 0
96 return 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 endif
Bram Moolenaar9d87a372018-12-18 21:41:50 +010098 let syn_name = ''
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
100 if a:use_syntax_check
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100101 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')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107 endif
108
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100109 if syn_name =~ 'Comment'
110 return <SID>XmlIndentComment(a:lnum)
111 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100113 " 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)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 return ind
118endfun
119
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100120" 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
Bram Moolenaar8e52a592012-05-18 21:49:28 +0200143let &cpo = s:keepcpo
144unlet s:keepcpo
145
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100146" vim:ts=4 et sts=-1 sw=0