annotate runtime/indent/xml.vim @ 14621:89cc3e7ecb68 v8.1.0324

patch 8.1.0324: off-by-one error in cmdidx check commit https://github.com/vim/vim/commit/74c8be2c6803eda3a57991b8867c5c65259b73d6 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Aug 23 22:51:40 2018 +0200 patch 8.1.0324: off-by-one error in cmdidx check Problem: Off-by-one error in cmdidx check. (Coverity) Solution: Use ">=" instead of ">".
author Christian Brabandt <cb@256bit.org>
date Thu, 23 Aug 2018 23:00:05 +0200
parents 2f7e67dd088c
children 2b30a2b4bde2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1 " Language: xml
14421
2f7e67dd088c Update runtime files.
Christian Brabandt <cb@256bit.org>
parents: 11518
diff changeset
2 " Repository: https://github.com/chrisbra/vim-xml-ftplugin
2f7e67dd088c Update runtime files.
Christian Brabandt <cb@256bit.org>
parents: 11518
diff changeset
3 " Maintainer: Christian Brabandt <cb@256bit.org>
2f7e67dd088c Update runtime files.
Christian Brabandt <cb@256bit.org>
parents: 11518
diff changeset
4 " Previous Maintainer: Johannes Zellner <johannes@zellner.org>
2f7e67dd088c Update runtime files.
Christian Brabandt <cb@256bit.org>
parents: 11518
diff changeset
5 " Last Change: 20180724 - Correctly indent xml comments https://github.com/vim/vim/issues/3200
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
6 " Notes: 1) does not indent pure non-xml code (e.g. embedded scripts)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
7 " 2) will be confused by unbalanced tags in comments
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
8 " or CDATA sections.
2034
7bc41231fbc7 Update runtime files.
Bram Moolenaar <bram@zimbu.org>
parents: 7
diff changeset
9 " 2009-05-26 patch by Nikolai Weibull
7bc41231fbc7 Update runtime files.
Bram Moolenaar <bram@zimbu.org>
parents: 7
diff changeset
10 " TODO: implement pre-like tags, see xml_indent_open / xml_indent_close
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
11
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
12 " Only load this indent file when no other was loaded.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
13 if exists("b:did_indent")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
14 finish
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
15 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
16 let b:did_indent = 1
3526
dd6c2497c997 Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents: 2034
diff changeset
17 let s:keepcpo= &cpo
dd6c2497c997 Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents: 2034
diff changeset
18 set cpo&vim
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
19
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
20 " [-- local settings (must come before aborting the script) --]
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
21 setlocal indentexpr=XmlIndentGet(v:lnum,1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
22 setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,}
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
23
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
24 if !exists('b:xml_indent_open')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
25 let b:xml_indent_open = '.\{-}<\a'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
26 " pre tag, e.g. <address>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
27 " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
28 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
29
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
30 if !exists('b:xml_indent_close')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
31 let b:xml_indent_close = '.\{-}</'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
32 " end pre tag, e.g. </address>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
33 " let b:xml_indent_close = '.\{-}</\(address\)\@!'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
34 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
35
3713
9910cbff5f16 Updated runtime files.
Bram Moolenaar <bram@vim.org>
parents: 3526
diff changeset
36 let &cpo = s:keepcpo
9910cbff5f16 Updated runtime files.
Bram Moolenaar <bram@vim.org>
parents: 3526
diff changeset
37 unlet s:keepcpo
9910cbff5f16 Updated runtime files.
Bram Moolenaar <bram@vim.org>
parents: 3526
diff changeset
38
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
39 " [-- finish, if the function already exists --]
3713
9910cbff5f16 Updated runtime files.
Bram Moolenaar <bram@vim.org>
parents: 3526
diff changeset
40 if exists('*XmlIndentGet')
9910cbff5f16 Updated runtime files.
Bram Moolenaar <bram@vim.org>
parents: 3526
diff changeset
41 finish
9910cbff5f16 Updated runtime files.
Bram Moolenaar <bram@vim.org>
parents: 3526
diff changeset
42 endif
9910cbff5f16 Updated runtime files.
Bram Moolenaar <bram@vim.org>
parents: 3526
diff changeset
43
9910cbff5f16 Updated runtime files.
Bram Moolenaar <bram@vim.org>
parents: 3526
diff changeset
44 let s:keepcpo= &cpo
9910cbff5f16 Updated runtime files.
Bram Moolenaar <bram@vim.org>
parents: 3526
diff changeset
45 set cpo&vim
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
46
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
47 fun! <SID>XmlIndentWithPattern(line, pat)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
48 let s = substitute('x'.a:line, a:pat, "\1", 'g')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
49 return strlen(substitute(s, "[^\1].*$", '', ''))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
50 endfun
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
51
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
52 " [-- check if it's xml --]
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
53 fun! <SID>XmlIndentSynCheck(lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
54 if '' != &syntax
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
55 let syn1 = synIDattr(synID(a:lnum, 1, 1), 'name')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
56 let syn2 = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
57 if '' != syn1 && syn1 !~ 'xml' && '' != syn2 && syn2 !~ 'xml'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
58 " don't indent pure non-xml code
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
59 return 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
60 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
61 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
62 return 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
63 endfun
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
64
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
65 " [-- return the sum of indents of a:lnum --]
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
66 fun! <SID>XmlIndentSum(lnum, style, add)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
67 let line = getline(a:lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
68 if a:style == match(line, '^\s*</')
11518
63b0b7b79b25 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents: 3713
diff changeset
69 return (shiftwidth() *
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
70 \ (<SID>XmlIndentWithPattern(line, b:xml_indent_open)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
71 \ - <SID>XmlIndentWithPattern(line, b:xml_indent_close)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
72 \ - <SID>XmlIndentWithPattern(line, '.\{-}/>'))) + a:add
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
73 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
74 return a:add
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
75 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
76 endfun
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
77
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
78 fun! XmlIndentGet(lnum, use_syntax_check)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
79 " Find a non-empty line above the current line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
80 let lnum = prevnonblank(a:lnum - 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
81
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
82 " Hit the start of the file, use zero indent.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
83 if lnum == 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
84 return 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
85 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
86
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
87 if a:use_syntax_check
2034
7bc41231fbc7 Update runtime files.
Bram Moolenaar <bram@zimbu.org>
parents: 7
diff changeset
88 let check_lnum = <SID>XmlIndentSynCheck(lnum)
7bc41231fbc7 Update runtime files.
Bram Moolenaar <bram@zimbu.org>
parents: 7
diff changeset
89 let check_alnum = <SID>XmlIndentSynCheck(a:lnum)
7bc41231fbc7 Update runtime files.
Bram Moolenaar <bram@zimbu.org>
parents: 7
diff changeset
90 if 0 == check_lnum || 0 == check_alnum
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
91 return indent(a:lnum)
2034
7bc41231fbc7 Update runtime files.
Bram Moolenaar <bram@zimbu.org>
parents: 7
diff changeset
92 elseif -1 == check_lnum || -1 == check_alnum
7bc41231fbc7 Update runtime files.
Bram Moolenaar <bram@zimbu.org>
parents: 7
diff changeset
93 return -1
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
94 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
95 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
96
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
97 let ind = <SID>XmlIndentSum(lnum, -1, indent(lnum))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
98 let ind = <SID>XmlIndentSum(a:lnum, 0, ind)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
99
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
100 return ind
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
101 endfun
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
102
3526
dd6c2497c997 Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents: 2034
diff changeset
103 let &cpo = s:keepcpo
dd6c2497c997 Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents: 2034
diff changeset
104 unlet s:keepcpo
dd6c2497c997 Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents: 2034
diff changeset
105
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
106 " vim:ts=8