Mercurial > vim
annotate runtime/indent/xml.vim @ 24760:ca0f983f08cf v8.2.2918
patch 8.2.2918: builtin function can be shadowed by global variable
Commit: https://github.com/vim/vim/commit/3d9c4eefe656ee8bf58c0496a48bd56bac180056
Author: Bram Moolenaar <Bram@vim.org>
Date: Mon May 31 22:15:26 2021 +0200
patch 8.2.2918: builtin function can be shadowed by global variable
Problem: Builtin function can be shadowed by global variable.
Solution: Check for builtin function before variable. (Yasuhiro Matsumoto,
closes #8302)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Mon, 31 May 2021 22:30:02 +0200 |
parents | 29c5f168c6fd |
children | f0d7cb510ce3 |
rev | line source |
---|---|
18456 | 1 " Language: XML |
2 " Maintainer: Christian Brabandt <cb@256bit.org> | |
3 " Repository: https://github.com/chrisbra/vim-xml-ftplugin | |
4 " Previous Maintainer: Johannes Zellner <johannes@zellner.org> | |
23047 | 5 " Last Changed: 2020 Nov 4th |
15194 | 6 " Last Change: |
23047 | 7 " 20200529 - Handle empty closing tags correctly |
18790 | 8 " 20191202 - Handle docbk filetype |
17571 | 9 " 20190726 - Correctly handle non-tagged data |
16086 | 10 " 20190204 - correctly handle wrap tags |
11 " https://github.com/chrisbra/vim-xml-ftplugin/issues/5 | |
15729 | 12 " 20190128 - Make sure to find previous tag |
13 " https://github.com/chrisbra/vim-xml-ftplugin/issues/4 | |
15194 | 14 " 20181116 - Fix indentation when tags start with a colon or an underscore |
15 " https://github.com/vim/vim/pull/926 | |
16 " 20181022 - Do not overwrite indentkeys setting | |
17 " https://github.com/chrisbra/vim-xml-ftplugin/issues/1 | |
18 " 20180724 - Correctly indent xml comments https://github.com/vim/vim/issues/3200 | |
19 " | |
20 " Notes: | |
21 " 1) does not indent pure non-xml code (e.g. embedded scripts) | |
22 " 2) will be confused by unbalanced tags in comments | |
23 " or CDATA sections. | |
24 " 2009-05-26 patch by Nikolai Weibull | |
25 " TODO: implement pre-like tags, see xml_indent_open / xml_indent_close | |
7 | 26 |
27 " Only load this indent file when no other was loaded. | |
28 if exists("b:did_indent") | |
29 finish | |
30 endif | |
31 let b:did_indent = 1 | |
3526
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2034
diff
changeset
|
32 let s:keepcpo= &cpo |
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2034
diff
changeset
|
33 set cpo&vim |
7 | 34 |
35 " [-- local settings (must come before aborting the script) --] | |
15194 | 36 " Attention: Parameter use_syntax_check is used by the docbk.vim indent script |
7 | 37 setlocal indentexpr=XmlIndentGet(v:lnum,1) |
14999 | 38 setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,},!^F |
17571 | 39 " autoindent: used when the indentexpr returns -1 |
40 setlocal autoindent | |
7 | 41 |
42 if !exists('b:xml_indent_open') | |
15194 | 43 let b:xml_indent_open = '.\{-}<[:A-Z_a-z]' |
7 | 44 " pre tag, e.g. <address> |
45 " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!' | |
46 endif | |
47 | |
48 if !exists('b:xml_indent_close') | |
23047 | 49 let b:xml_indent_close = '.\{-}</\|/>.\{-}' |
7 | 50 " end pre tag, e.g. </address> |
51 " let b:xml_indent_close = '.\{-}</\(address\)\@!' | |
52 endif | |
53 | |
3713 | 54 let &cpo = s:keepcpo |
55 unlet s:keepcpo | |
56 | |
7 | 57 " [-- finish, if the function already exists --] |
3713 | 58 if exists('*XmlIndentGet') |
15194 | 59 finish |
3713 | 60 endif |
61 | |
62 let s:keepcpo= &cpo | |
63 set cpo&vim | |
7 | 64 |
65 fun! <SID>XmlIndentWithPattern(line, pat) | |
66 let s = substitute('x'.a:line, a:pat, "\1", 'g') | |
67 return strlen(substitute(s, "[^\1].*$", '', '')) | |
68 endfun | |
69 | |
70 " [-- check if it's xml --] | |
71 fun! <SID>XmlIndentSynCheck(lnum) | |
15194 | 72 if &syntax != '' |
73 let syn1 = synIDattr(synID(a:lnum, 1, 1), 'name') | |
74 let syn2 = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name') | |
75 if syn1 != '' && syn1 !~ 'xml' && syn2 != '' && syn2 !~ 'xml' | |
76 " don't indent pure non-xml code | |
77 return 0 | |
78 endif | |
7 | 79 endif |
80 return 1 | |
81 endfun | |
82 | |
83 " [-- return the sum of indents of a:lnum --] | |
16086 | 84 fun! <SID>XmlIndentSum(line, style, add) |
23047 | 85 if <SID>IsXMLContinuation(a:line) && a:style == 0 && !<SID>IsXMLEmptyClosingTag(a:line) |
16086 | 86 " no complete tag, add one additional indent level |
87 " but only for the current line | |
88 return a:add + shiftwidth() | |
89 elseif <SID>HasNoTagEnd(a:line) | |
90 " no complete tag, return initial indent | |
91 return a:add | |
92 endif | |
93 if a:style == match(a:line, '^\s*</') | |
15194 | 94 return (shiftwidth() * |
16086 | 95 \ (<SID>XmlIndentWithPattern(a:line, b:xml_indent_open) |
96 \ - <SID>XmlIndentWithPattern(a:line, b:xml_indent_close) | |
97 \ - <SID>XmlIndentWithPattern(a:line, '.\{-}/>'))) + a:add | |
7 | 98 else |
15194 | 99 return a:add |
7 | 100 endif |
101 endfun | |
102 | |
15194 | 103 " Main indent function |
7 | 104 fun! XmlIndentGet(lnum, use_syntax_check) |
105 " Find a non-empty line above the current line. | |
16086 | 106 if prevnonblank(a:lnum - 1) == 0 |
107 " Hit the start of the file, use zero indent. | |
15194 | 108 return 0 |
7 | 109 endif |
15729 | 110 " Find previous line with a tag (regardless whether open or closed, |
17571 | 111 " but always restrict the match to a line before the current one |
16086 | 112 " Note: xml declaration: <?xml version="1.0"?> |
113 " won't be found, as it is not a legal tag name | |
17571 | 114 let ptag_pattern = '\%(.\{-}<[/:A-Z_a-z]\)'. '\%(\&\%<'. a:lnum .'l\)' |
16086 | 115 let ptag = search(ptag_pattern, 'bnW') |
116 " no previous tag | |
117 if ptag == 0 | |
118 return 0 | |
119 endif | |
15729 | 120 |
17571 | 121 let pline = getline(ptag) |
122 let pind = indent(ptag) | |
123 | |
124 let syn_name_start = '' " Syntax element at start of line (excluding whitespace) | |
125 let syn_name_end = '' " Syntax element at end of line | |
126 let curline = getline(a:lnum) | |
7 | 127 if a:use_syntax_check |
16086 | 128 let check_lnum = <SID>XmlIndentSynCheck(ptag) |
15194 | 129 let check_alnum = <SID>XmlIndentSynCheck(a:lnum) |
130 if check_lnum == 0 || check_alnum == 0 | |
131 return indent(a:lnum) | |
132 endif | |
17571 | 133 let syn_name_end = synIDattr(synID(a:lnum, strlen(curline) - 1, 1), 'name') |
134 let syn_name_start = synIDattr(synID(a:lnum, match(curline, '\S') + 1, 1), 'name') | |
23047 | 135 let prev_syn_name_end = synIDattr(synID(ptag, strlen(pline) - 1, 1), 'name') |
136 " not needed (yet?) | |
137 " let prev_syn_name_start = synIDattr(synID(ptag, match(pline, '\S') + 1, 1), 'name') | |
7 | 138 endif |
139 | |
17571 | 140 if syn_name_end =~ 'Comment' && syn_name_start =~ 'Comment' |
141 return <SID>XmlIndentComment(a:lnum) | |
18790 | 142 elseif empty(syn_name_start) && empty(syn_name_end) && a:use_syntax_check |
17571 | 143 " non-xml tag content: use indent from 'autoindent' |
23047 | 144 if pline =~ b:xml_indent_close |
145 return pind | |
146 elseif !empty(prev_syn_name_end) | |
147 " only indent by an extra shiftwidth, if the previous line ends | |
148 " with an XML like tag | |
149 return pind + shiftwidth() | |
150 else | |
151 " no extra indent, looks like a text continuation line | |
152 return pind | |
153 endif | |
17571 | 154 endif |
155 | |
15194 | 156 " Get indent from previous tag line |
16086 | 157 let ind = <SID>XmlIndentSum(pline, -1, pind) |
15194 | 158 " Determine indent from current line |
17571 | 159 let ind = <SID>XmlIndentSum(curline, 0, ind) |
7 | 160 return ind |
161 endfun | |
162 | |
16086 | 163 func! <SID>IsXMLContinuation(line) |
164 " Checks, whether or not the line matches a start-of-tag | |
18790 | 165 return a:line !~ '^\s*<' && &ft is# 'xml' |
16086 | 166 endfunc |
167 | |
168 func! <SID>HasNoTagEnd(line) | |
169 " Checks whether or not the line matches '>' (so finishes a tag) | |
170 return a:line !~ '>\s*$' | |
171 endfunc | |
172 | |
23047 | 173 func! <SID>IsXMLEmptyClosingTag(line) |
174 " Checks whether the line ends with an empty closing tag such as <lb/> | |
175 return a:line =~? '<[^>]*/>\s*$' | |
176 endfunc | |
177 | |
15194 | 178 " return indent for a commented line, |
17571 | 179 " the middle part might be indented one additional level |
15194 | 180 func! <SID>XmlIndentComment(lnum) |
23047 | 181 let ptagopen = search('.\{-}<[:A-Z_a-z]\_[^/]\{-}>.\{-}', 'bnW') |
16086 | 182 let ptagclose = search(b:xml_indent_close, 'bnW') |
15194 | 183 if getline(a:lnum) =~ '<!--' |
184 " if previous tag was a closing tag, do not add | |
185 " one additional level of indent | |
186 if ptagclose > ptagopen && a:lnum > ptagclose | |
23047 | 187 " If the previous tag was closed on the same line as it was |
188 " declared, we should indent with its indent level. | |
189 if !<SID>IsXMLContinuation(getline(ptagclose)) | |
190 return indent(ptagclose) | |
191 else | |
192 return indent(ptagclose) - shiftwidth() | |
193 endif | |
194 elseif ptagclose == ptagopen | |
15194 | 195 return indent(ptagclose) |
196 else | |
197 " start of comment, add one indentation level | |
198 return indent(ptagopen) + shiftwidth() | |
199 endif | |
200 elseif getline(a:lnum) =~ '-->' | |
201 " end of comment, same as start of comment | |
16086 | 202 return indent(search('<!--', 'bnW')) |
15194 | 203 else |
204 " middle part of comment, add one additional level | |
16086 | 205 return indent(search('<!--', 'bnW')) + shiftwidth() |
15194 | 206 endif |
207 endfunc | |
208 | |
3526
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2034
diff
changeset
|
209 let &cpo = s:keepcpo |
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2034
diff
changeset
|
210 unlet s:keepcpo |
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2034
diff
changeset
|
211 |
15194 | 212 " vim:ts=4 et sts=-1 sw=0 |