annotate runtime/autoload/xmlformat.vim @ 17447:c8bdaff91140

Added tag v8.1.1721 for changeset dcc4120f841294dfadbc2c59e479a669410474b5
author Bram Moolenaar <Bram@vim.org>
date Sat, 20 Jul 2019 19:15:05 +0200
parents bc1a8d21c811
children 6d11fc4aa683
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13912
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1 " Vim plugin for formatting XML
15131
bc1a8d21c811 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 14421
diff changeset
2 " Last Change: Thu, 07 Dec 2018
bc1a8d21c811 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 14421
diff changeset
3 " Version: 0.1
bc1a8d21c811 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 14421
diff changeset
4 " Author: Christian Brabandt <cb@256bit.org>
bc1a8d21c811 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 14421
diff changeset
5 " Repository: https://github.com/chrisbra/vim-xml-ftplugin
bc1a8d21c811 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 14421
diff changeset
6 " License: VIM License
13912
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
7 " Documentation: see :h xmlformat.txt (TODO!)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
8 " ---------------------------------------------------------------------
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
9 " Load Once: {{{1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
10 if exists("g:loaded_xmlformat") || &cp
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
11 finish
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
12 endif
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
13 let g:loaded_xmlformat = 1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
14 let s:keepcpo = &cpo
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
15 set cpo&vim
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
16
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
17 " Main function: Format the input {{{1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
18 func! xmlformat#Format()
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
19 " only allow reformatting through the gq command
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
20 " (e.g. Vim is in normal mode)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
21 if mode() != 'n'
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
22 " do not fall back to internal formatting
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
23 return 0
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
24 endif
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
25 let sw = shiftwidth()
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
26 let prev = prevnonblank(v:lnum-1)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
27 let s:indent = indent(prev)/sw
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
28 let result = []
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
29 let lastitem = prev ? getline(prev) : ''
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
30 let is_xml_decl = 0
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
31 " split on `<`, but don't split on very first opening <
14006
665fe1f419b0 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents: 13912
diff changeset
32 for item in split(join(getline(v:lnum, (v:lnum + v:count - 1))), '.\@<=[>]\zs')
13912
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
33 if s:EndTag(item)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
34 let s:indent = s:DecreaseIndent()
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
35 call add(result, s:Indent(item))
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
36 elseif s:EmptyTag(lastitem)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
37 call add(result, s:Indent(item))
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
38 elseif s:StartTag(lastitem) && s:IsTag(item)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
39 let s:indent += 1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
40 call add(result, s:Indent(item))
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
41 else
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
42 if !s:IsTag(item)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
43 " Simply split on '<'
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
44 let t=split(item, '.<\@=\zs')
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
45 let s:indent+=1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
46 call add(result, s:Indent(t[0]))
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
47 let s:indent = s:DecreaseIndent()
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
48 call add(result, s:Indent(t[1]))
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
49 else
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
50 call add(result, s:Indent(item))
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
51 endif
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
52 endif
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
53 let lastitem = item
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
54 endfor
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
55
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
56 if !empty(result)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
57 exe v:lnum. ",". (v:lnum + v:count - 1). 'd'
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
58 call append(v:lnum - 1, result)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
59 " Might need to remove the last line, if it became empty because of the
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
60 " append() call
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
61 let last = v:lnum + len(result)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
62 if getline(last) is ''
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
63 exe last. 'd'
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
64 endif
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
65 endif
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
66
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
67 " do not run internal formatter!
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
68 return 0
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
69 endfunc
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
70 " Check if given tag is XML Declaration header {{{1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
71 func! s:IsXMLDecl(tag)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
72 return a:tag =~? '^\s*<?xml\s\?\%(version="[^"]*"\)\?\s\?\%(encoding="[^"]*"\)\? ?>\s*$'
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
73 endfunc
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
74 " Return tag indented by current level {{{1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
75 func! s:Indent(item)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
76 return repeat(' ', shiftwidth()*s:indent). s:Trim(a:item)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
77 endfu
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
78 " Return item trimmed from leading whitespace {{{1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
79 func! s:Trim(item)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
80 if exists('*trim')
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
81 return trim(a:item)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
82 else
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
83 return matchstr(a:item, '\S\+.*')
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
84 endif
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
85 endfunc
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
86 " Check if tag is a new opening tag <tag> {{{1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
87 func! s:StartTag(tag)
15131
bc1a8d21c811 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 14421
diff changeset
88 let is_comment = s:IsComment(a:tag)
bc1a8d21c811 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 14421
diff changeset
89 return a:tag =~? '^\s*<[^/?]' && !is_comment
bc1a8d21c811 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 14421
diff changeset
90 endfunc
bc1a8d21c811 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 14421
diff changeset
91 func! s:IsComment(tag)
bc1a8d21c811 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 14421
diff changeset
92 return a:tag =~? '<!--'
13912
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
93 endfunc
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
94 " Remove one level of indentation {{{1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
95 func! s:DecreaseIndent()
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
96 return (s:indent > 0 ? s:indent - 1 : 0)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
97 endfunc
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
98 " Check if tag is a closing tag </tag> {{{1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
99 func! s:EndTag(tag)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
100 return a:tag =~? '^\s*</'
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
101 endfunc
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
102 " Check that the tag is actually a tag and not {{{1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
103 " something like "foobar</foobar>"
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
104 func! s:IsTag(tag)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
105 return s:Trim(a:tag)[0] == '<'
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
106 endfunc
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
107 " Check if tag is empty <tag/> {{{1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
108 func! s:EmptyTag(tag)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
109 return a:tag =~ '/>\s*$'
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
110 endfunc
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
111 " Restoration And Modelines: {{{1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
112 let &cpo= s:keepcpo
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
113 unlet s:keepcpo
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
114 " Modeline {{{1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
115 " vim: fdm=marker fdl=0 ts=2 et sw=0 sts=-1