Mercurial > vim
annotate runtime/indent/xml.vim @ 17410:5f5c552c1acb
Added tag v8.1.1703 for changeset e24bbd061233d8d27ed38c10210b39d955f0a8e0
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Tue, 16 Jul 2019 21:30:06 +0200 |
parents | bd7461db24b3 |
children | 2704c4e3e20a |
rev | line source |
---|---|
15194 | 1 " Language: xml |
2 " Repository: https://github.com/chrisbra/vim-xml-ftplugin | |
16086 | 3 " Last Changed: Feb 04, 2019 |
15194 | 4 " Maintainer: Christian Brabandt <cb@256bit.org> |
5 " Previous Maintainer: Johannes Zellner <johannes@zellner.org> | |
6 " Last Change: | |
16086 | 7 " 20190204 - correctly handle wrap tags |
8 " https://github.com/chrisbra/vim-xml-ftplugin/issues/5 | |
15729 | 9 " 20190128 - Make sure to find previous tag |
10 " https://github.com/chrisbra/vim-xml-ftplugin/issues/4 | |
15194 | 11 " 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 | |
7 | 23 |
24 " Only load this indent file when no other was loaded. | |
25 if exists("b:did_indent") | |
26 finish | |
27 endif | |
28 let b:did_indent = 1 | |
3526
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2034
diff
changeset
|
29 let s:keepcpo= &cpo |
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2034
diff
changeset
|
30 set cpo&vim |
7 | 31 |
32 " [-- local settings (must come before aborting the script) --] | |
15194 | 33 " Attention: Parameter use_syntax_check is used by the docbk.vim indent script |
7 | 34 setlocal indentexpr=XmlIndentGet(v:lnum,1) |
14999 | 35 setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,},!^F |
7 | 36 |
37 if !exists('b:xml_indent_open') | |
15194 | 38 let b:xml_indent_open = '.\{-}<[:A-Z_a-z]' |
7 | 39 " pre tag, e.g. <address> |
40 " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!' | |
41 endif | |
42 | |
43 if !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\)\@!' | |
47 endif | |
48 | |
3713 | 49 let &cpo = s:keepcpo |
50 unlet s:keepcpo | |
51 | |
7 | 52 " [-- finish, if the function already exists --] |
3713 | 53 if exists('*XmlIndentGet') |
15194 | 54 finish |
3713 | 55 endif |
56 | |
57 let s:keepcpo= &cpo | |
58 set cpo&vim | |
7 | 59 |
60 fun! <SID>XmlIndentWithPattern(line, pat) | |
61 let s = substitute('x'.a:line, a:pat, "\1", 'g') | |
62 return strlen(substitute(s, "[^\1].*$", '', '')) | |
63 endfun | |
64 | |
65 " [-- check if it's xml --] | |
66 fun! <SID>XmlIndentSynCheck(lnum) | |
15194 | 67 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 | |
7 | 74 endif |
75 return 1 | |
76 endfun | |
77 | |
78 " [-- return the sum of indents of a:lnum --] | |
16086 | 79 fun! <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*</') | |
15194 | 89 return (shiftwidth() * |
16086 | 90 \ (<SID>XmlIndentWithPattern(a:line, b:xml_indent_open) |
91 \ - <SID>XmlIndentWithPattern(a:line, b:xml_indent_close) | |
92 \ - <SID>XmlIndentWithPattern(a:line, '.\{-}/>'))) + a:add | |
7 | 93 else |
15194 | 94 return a:add |
7 | 95 endif |
96 endfun | |
97 | |
15194 | 98 " Main indent function |
7 | 99 fun! XmlIndentGet(lnum, use_syntax_check) |
100 " Find a non-empty line above the current line. | |
16086 | 101 if prevnonblank(a:lnum - 1) == 0 |
102 " Hit the start of the file, use zero indent. | |
15194 | 103 return 0 |
7 | 104 endif |
15729 | 105 " 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 | |
16086 | 107 " Note: xml declaration: <?xml version="1.0"?> |
108 " won't be found, as it is not a legal tag name | |
15729 | 109 let ptag_pattern = '\%(.\{-}<[/:A-Z_a-z]\)'. '\%(\&\%<'. line('.').'l\)' |
16086 | 110 let ptag = search(ptag_pattern, 'bnW') |
111 " no previous tag | |
112 if ptag == 0 | |
113 return 0 | |
114 endif | |
15729 | 115 |
15194 | 116 let syn_name = '' |
7 | 117 if a:use_syntax_check |
16086 | 118 let check_lnum = <SID>XmlIndentSynCheck(ptag) |
15194 | 119 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') | |
124 endif | |
125 | |
126 if syn_name =~ 'Comment' | |
127 return <SID>XmlIndentComment(a:lnum) | |
7 | 128 endif |
129 | |
16086 | 130 let pline = getline(ptag) |
131 let pind = indent(ptag) | |
15194 | 132 " Get indent from previous tag line |
16086 | 133 let ind = <SID>XmlIndentSum(pline, -1, pind) |
134 let t_ind = ind | |
15194 | 135 " Determine indent from current line |
16086 | 136 let ind = <SID>XmlIndentSum(getline(a:lnum), 0, ind) |
7 | 137 return ind |
138 endfun | |
139 | |
16086 | 140 func! <SID>IsXMLContinuation(line) |
141 " Checks, whether or not the line matches a start-of-tag | |
142 return a:line !~ '^\s*<' | |
143 endfunc | |
144 | |
145 func! <SID>HasNoTagEnd(line) | |
146 " Checks whether or not the line matches '>' (so finishes a tag) | |
147 return a:line !~ '>\s*$' | |
148 endfunc | |
149 | |
15194 | 150 " return indent for a commented line, |
151 " the middle part might be indented on additional level | |
152 func! <SID>XmlIndentComment(lnum) | |
16086 | 153 let ptagopen = search(b:xml_indent_open, 'bnW') |
154 let ptagclose = search(b:xml_indent_close, 'bnW') | |
15194 | 155 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 | |
16086 | 166 return indent(search('<!--', 'bnW')) |
15194 | 167 else |
168 " middle part of comment, add one additional level | |
16086 | 169 return indent(search('<!--', 'bnW')) + shiftwidth() |
15194 | 170 endif |
171 endfunc | |
172 | |
3526
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2034
diff
changeset
|
173 let &cpo = s:keepcpo |
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2034
diff
changeset
|
174 unlet s:keepcpo |
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2034
diff
changeset
|
175 |
15194 | 176 " vim:ts=4 et sts=-1 sw=0 |