Mercurial > vim
annotate runtime/indent/xml.vim @ 15729:fe57e4f0eac1
Update runtime files.
commit https://github.com/vim/vim/commit/314dd79cac2adc10304212d1980d23ecf6782cfc
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Feb 3 15:27:20 2019 +0100
Update runtime files.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sun, 03 Feb 2019 15:30:09 +0100 |
parents | 8b334e4cb97f |
children | bd7461db24b3 |
rev | line source |
---|---|
15194 | 1 " Language: xml |
2 " Repository: https://github.com/chrisbra/vim-xml-ftplugin | |
15729 | 3 " Last Changed: Jan 28, 2019 |
15194 | 4 " Maintainer: Christian Brabandt <cb@256bit.org> |
5 " Previous Maintainer: Johannes Zellner <johannes@zellner.org> | |
6 " Last Change: | |
15729 | 7 " 20190128 - Make sure to find previous tag |
8 " https://github.com/chrisbra/vim-xml-ftplugin/issues/4 | |
15194 | 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 | |
7 | 21 |
22 " Only load this indent file when no other was loaded. | |
23 if exists("b:did_indent") | |
24 finish | |
25 endif | |
26 let b:did_indent = 1 | |
3526
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2034
diff
changeset
|
27 let s:keepcpo= &cpo |
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2034
diff
changeset
|
28 set cpo&vim |
7 | 29 |
30 " [-- local settings (must come before aborting the script) --] | |
15194 | 31 " Attention: Parameter use_syntax_check is used by the docbk.vim indent script |
7 | 32 setlocal indentexpr=XmlIndentGet(v:lnum,1) |
14999 | 33 setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,},!^F |
7 | 34 |
35 if !exists('b:xml_indent_open') | |
15194 | 36 let b:xml_indent_open = '.\{-}<[:A-Z_a-z]' |
7 | 37 " pre tag, e.g. <address> |
38 " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!' | |
39 endif | |
40 | |
41 if !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\)\@!' | |
45 endif | |
46 | |
3713 | 47 let &cpo = s:keepcpo |
48 unlet s:keepcpo | |
49 | |
7 | 50 " [-- finish, if the function already exists --] |
3713 | 51 if exists('*XmlIndentGet') |
15194 | 52 finish |
3713 | 53 endif |
54 | |
55 let s:keepcpo= &cpo | |
56 set cpo&vim | |
7 | 57 |
58 fun! <SID>XmlIndentWithPattern(line, pat) | |
59 let s = substitute('x'.a:line, a:pat, "\1", 'g') | |
60 return strlen(substitute(s, "[^\1].*$", '', '')) | |
61 endfun | |
62 | |
63 " [-- check if it's xml --] | |
64 fun! <SID>XmlIndentSynCheck(lnum) | |
15194 | 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 | |
7 | 72 endif |
73 return 1 | |
74 endfun | |
75 | |
76 " [-- return the sum of indents of a:lnum --] | |
77 fun! <SID>XmlIndentSum(lnum, style, add) | |
78 let line = getline(a:lnum) | |
79 if a:style == match(line, '^\s*</') | |
15194 | 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 | |
7 | 84 else |
15194 | 85 return a:add |
7 | 86 endif |
87 endfun | |
88 | |
15194 | 89 " Main indent function |
7 | 90 fun! XmlIndentGet(lnum, use_syntax_check) |
91 " Find a non-empty line above the current line. | |
15194 | 92 let plnum = prevnonblank(a:lnum - 1) |
7 | 93 " Hit the start of the file, use zero indent. |
15194 | 94 if plnum == 0 |
95 return 0 | |
7 | 96 endif |
15729 | 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 | |
15194 | 102 let syn_name = '' |
7 | 103 if a:use_syntax_check |
15194 | 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) | |
7 | 114 endif |
115 | |
15194 | 116 " Get indent from previous tag line |
117 let ind = <SID>XmlIndentSum(ptag, -1, indent(ptag)) | |
118 " Determine indent from current line | |
7 | 119 let ind = <SID>XmlIndentSum(a:lnum, 0, ind) |
120 return ind | |
121 endfun | |
122 | |
15194 | 123 " return indent for a commented line, |
124 " the middle part might be indented on additional level | |
125 func! <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 | |
144 endfunc | |
145 | |
3526
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2034
diff
changeset
|
146 let &cpo = s:keepcpo |
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2034
diff
changeset
|
147 unlet s:keepcpo |
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2034
diff
changeset
|
148 |
15194 | 149 " vim:ts=4 et sts=-1 sw=0 |