annotate runtime/autoload/xmlformat.vim @ 14421:2f7e67dd088c

Update runtime files. commit https://github.com/vim/vim/commit/91f84f6e11cd879d43d651c0903d85bff95f0716 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Jul 29 15:07:52 2018 +0200 Update runtime files.
author Christian Brabandt <cb@256bit.org>
date Sun, 29 Jul 2018 15:15:06 +0200
parents 665fe1f419b0
children bc1a8d21c811
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
14006
665fe1f419b0 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents: 13912
diff changeset
2 " Last Change: Thu, 22 May 2018 21:26:55 +0100
13912
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
3 " Version: 0.1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
4 " Author: Christian Brabandt <cb@256bit.org>
14421
2f7e67dd088c Update runtime files.
Christian Brabandt <cb@256bit.org>
parents: 14006
diff changeset
5 " Repository: https://github.com/chrisbra/vim-xml-ftplugin
13912
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
6 " License: VIM License
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)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
88 return a:tag =~? '^\s*<[^/?]'
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
89 endfunc
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
90 " Remove one level of indentation {{{1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
91 func! s:DecreaseIndent()
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
92 return (s:indent > 0 ? s:indent - 1 : 0)
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 " Check if tag is a closing tag </tag> {{{1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
95 func! s:EndTag(tag)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
96 return a:tag =~? '^\s*</'
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 that the tag is actually a tag and not {{{1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
99 " something like "foobar</foobar>"
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
100 func! s:IsTag(tag)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
101 return s:Trim(a:tag)[0] == '<'
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
102 endfunc
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
103 " Check if tag is empty <tag/> {{{1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
104 func! s:EmptyTag(tag)
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
105 return a:tag =~ '/>\s*$'
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 " Restoration And Modelines: {{{1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
108 let &cpo= s:keepcpo
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
109 unlet s:keepcpo
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
110 " Modeline {{{1
a9fdf01085a8 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
111 " vim: fdm=marker fdl=0 ts=2 et sw=0 sts=-1