blob: dcafb467a6ed9ff6017ec317c0d00cfe6806f202 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Language: xml
Bram Moolenaar91f84f62018-07-29 15:07:52 +02002" Repository: https://github.com/chrisbra/vim-xml-ftplugin
3" Maintainer: Christian Brabandt <cb@256bit.org>
4" Previous Maintainer: Johannes Zellner <johannes@zellner.org>
5" Last Change: 20180724 - Correctly indent xml comments https://github.com/vim/vim/issues/3200
Bram Moolenaar071d4272004-06-13 20:20:40 +00006" Notes: 1) does not indent pure non-xml code (e.g. embedded scripts)
7" 2) will be confused by unbalanced tags in comments
8" or CDATA sections.
Bram Moolenaar5c736222010-01-06 20:54:52 +01009" 2009-05-26 patch by Nikolai Weibull
10" TODO: implement pre-like tags, see xml_indent_open / xml_indent_close
Bram Moolenaar071d4272004-06-13 20:20:40 +000011
12" Only load this indent file when no other was loaded.
13if exists("b:did_indent")
14 finish
15endif
16let b:did_indent = 1
Bram Moolenaar8e52a592012-05-18 21:49:28 +020017let s:keepcpo= &cpo
18set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000019
20" [-- local settings (must come before aborting the script) --]
21setlocal indentexpr=XmlIndentGet(v:lnum,1)
22setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,}
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024if !exists('b:xml_indent_open')
25 let b:xml_indent_open = '.\{-}<\a'
26 " pre tag, e.g. <address>
27 " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!'
28endif
29
30if !exists('b:xml_indent_close')
31 let b:xml_indent_close = '.\{-}</'
32 " end pre tag, e.g. </address>
33 " let b:xml_indent_close = '.\{-}</\(address\)\@!'
34endif
35
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020036let &cpo = s:keepcpo
37unlet s:keepcpo
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039" [-- finish, if the function already exists --]
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020040if exists('*XmlIndentGet')
41 finish
42endif
43
44let s:keepcpo= &cpo
45set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000046
47fun! <SID>XmlIndentWithPattern(line, pat)
48 let s = substitute('x'.a:line, a:pat, "\1", 'g')
49 return strlen(substitute(s, "[^\1].*$", '', ''))
50endfun
51
52" [-- check if it's xml --]
53fun! <SID>XmlIndentSynCheck(lnum)
54 if '' != &syntax
55 let syn1 = synIDattr(synID(a:lnum, 1, 1), 'name')
56 let syn2 = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name')
57 if '' != syn1 && syn1 !~ 'xml' && '' != syn2 && syn2 !~ 'xml'
58 " don't indent pure non-xml code
59 return 0
60 endif
61 endif
62 return 1
63endfun
64
65" [-- return the sum of indents of a:lnum --]
66fun! <SID>XmlIndentSum(lnum, style, add)
67 let line = getline(a:lnum)
68 if a:style == match(line, '^\s*</')
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020069 return (shiftwidth() *
Bram Moolenaar071d4272004-06-13 20:20:40 +000070 \ (<SID>XmlIndentWithPattern(line, b:xml_indent_open)
71 \ - <SID>XmlIndentWithPattern(line, b:xml_indent_close)
72 \ - <SID>XmlIndentWithPattern(line, '.\{-}/>'))) + a:add
73 else
74 return a:add
75 endif
76endfun
77
78fun! XmlIndentGet(lnum, use_syntax_check)
79 " Find a non-empty line above the current line.
80 let lnum = prevnonblank(a:lnum - 1)
81
82 " Hit the start of the file, use zero indent.
83 if lnum == 0
84 return 0
85 endif
86
87 if a:use_syntax_check
Bram Moolenaar5c736222010-01-06 20:54:52 +010088 let check_lnum = <SID>XmlIndentSynCheck(lnum)
89 let check_alnum = <SID>XmlIndentSynCheck(a:lnum)
90 if 0 == check_lnum || 0 == check_alnum
Bram Moolenaar071d4272004-06-13 20:20:40 +000091 return indent(a:lnum)
Bram Moolenaar5c736222010-01-06 20:54:52 +010092 elseif -1 == check_lnum || -1 == check_alnum
93 return -1
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 endif
95 endif
96
97 let ind = <SID>XmlIndentSum(lnum, -1, indent(lnum))
98 let ind = <SID>XmlIndentSum(a:lnum, 0, ind)
99
100 return ind
101endfun
102
Bram Moolenaar8e52a592012-05-18 21:49:28 +0200103let &cpo = s:keepcpo
104unlet s:keepcpo
105
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106" vim:ts=8