annotate runtime/autoload/xmlformat.vim @ 13912:a9fdf01085a8

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