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