annotate runtime/indent/xml.vim @ 926:bde3b38593f9 v7.0.052

updated for version 7.0-052
author vimboss
date Tue, 15 Aug 2006 19:40:35 +0000
parents 3fc0f57ecb91
children 7bc41231fbc7
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
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2 " Maintainer: Johannes Zellner <johannes@zellner.org>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3 " Last Change: Tue, 27 Apr 2004 14:54:59 CEST
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
4 " Notes: 1) does not indent pure non-xml code (e.g. embedded scripts)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
5 " 2) will be confused by unbalanced tags in comments
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
6 " or CDATA sections.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
7 " TODO: implement pre-like tags, see xml_indent_open / xml_indent_close
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
8
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
9 " Only load this indent file when no other was loaded.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
10 if exists("b:did_indent")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
11 finish
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
12 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
13 let b:did_indent = 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
14
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
15 " [-- local settings (must come before aborting the script) --]
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
16 setlocal indentexpr=XmlIndentGet(v:lnum,1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
17 setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,}
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
18
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
19 set cpo-=C
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
20
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
21 if !exists('b:xml_indent_open')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
22 let b:xml_indent_open = '.\{-}<\a'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
23 " pre tag, e.g. <address>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
24 " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
25 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
26
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
27 if !exists('b:xml_indent_close')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
28 let b:xml_indent_close = '.\{-}</'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
29 " end pre tag, e.g. </address>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
30 " let b:xml_indent_close = '.\{-}</\(address\)\@!'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
31 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
32
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
33 " [-- finish, if the function already exists --]
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
34 if exists('*XmlIndentGet') | finish | endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
35
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
36 fun! <SID>XmlIndentWithPattern(line, pat)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
37 let s = substitute('x'.a:line, a:pat, "\1", 'g')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
38 return strlen(substitute(s, "[^\1].*$", '', ''))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
39 endfun
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
40
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
41 " [-- check if it's xml --]
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
42 fun! <SID>XmlIndentSynCheck(lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
43 if '' != &syntax
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
44 let syn1 = synIDattr(synID(a:lnum, 1, 1), 'name')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
45 let syn2 = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
46 if '' != syn1 && syn1 !~ 'xml' && '' != syn2 && syn2 !~ 'xml'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
47 " don't indent pure non-xml code
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
48 return 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
49 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
50 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
51 return 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
52 endfun
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
53
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
54 " [-- return the sum of indents of a:lnum --]
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
55 fun! <SID>XmlIndentSum(lnum, style, add)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
56 let line = getline(a:lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
57 if a:style == match(line, '^\s*</')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
58 return (&sw *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
59 \ (<SID>XmlIndentWithPattern(line, b:xml_indent_open)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
60 \ - <SID>XmlIndentWithPattern(line, b:xml_indent_close)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
61 \ - <SID>XmlIndentWithPattern(line, '.\{-}/>'))) + a:add
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
62 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
63 return a:add
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
64 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
65 endfun
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
66
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
67 fun! XmlIndentGet(lnum, use_syntax_check)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
68 " Find a non-empty line above the current line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
69 let lnum = prevnonblank(a:lnum - 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
70
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
71 " Hit the start of the file, use zero indent.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
72 if lnum == 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
73 return 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
74 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
75
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
76 if a:use_syntax_check
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
77 if 0 == <SID>XmlIndentSynCheck(lnum) || 0 == <SID>XmlIndentSynCheck(a:lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
78 return indent(a:lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
79 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
80 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
81
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
82 let ind = <SID>XmlIndentSum(lnum, -1, indent(lnum))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
83 let ind = <SID>XmlIndentSum(a:lnum, 0, ind)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
84
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
85 return ind
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
86 endfun
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
87
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
88 " vim:ts=8