blob: 7afcc89b7ff255af44e6e1f9a261caad20620cc0 [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>
Bram Moolenaarba3ff532018-11-04 14:45:49 +01005" Last Change: 20181022 - Do not overwrite indentkeys setting
6" https://github.com/chrisbra/vim-xml-ftplugin/issues/1
7" 20180724 - Correctly indent xml comments https://github.com/vim/vim/issues/3200
Bram Moolenaar071d4272004-06-13 20:20:40 +00008" Notes: 1) does not indent pure non-xml code (e.g. embedded scripts)
9" 2) will be confused by unbalanced tags in comments
10" or CDATA sections.
Bram Moolenaar5c736222010-01-06 20:54:52 +010011" 2009-05-26 patch by Nikolai Weibull
12" TODO: implement pre-like tags, see xml_indent_open / xml_indent_close
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14" Only load this indent file when no other was loaded.
15if exists("b:did_indent")
16 finish
17endif
18let b:did_indent = 1
Bram Moolenaar8e52a592012-05-18 21:49:28 +020019let s:keepcpo= &cpo
20set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000021
22" [-- local settings (must come before aborting the script) --]
23setlocal indentexpr=XmlIndentGet(v:lnum,1)
Bram Moolenaarba3ff532018-11-04 14:45:49 +010024setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,},!^F
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
Bram Moolenaar071d4272004-06-13 20:20:40 +000026if !exists('b:xml_indent_open')
27 let b:xml_indent_open = '.\{-}<\a'
28 " pre tag, e.g. <address>
29 " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!'
30endif
31
32if !exists('b:xml_indent_close')
33 let b:xml_indent_close = '.\{-}</'
34 " end pre tag, e.g. </address>
35 " let b:xml_indent_close = '.\{-}</\(address\)\@!'
36endif
37
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020038let &cpo = s:keepcpo
39unlet s:keepcpo
40
Bram Moolenaar071d4272004-06-13 20:20:40 +000041" [-- finish, if the function already exists --]
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020042if exists('*XmlIndentGet')
43 finish
44endif
45
46let s:keepcpo= &cpo
47set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
49fun! <SID>XmlIndentWithPattern(line, pat)
50 let s = substitute('x'.a:line, a:pat, "\1", 'g')
51 return strlen(substitute(s, "[^\1].*$", '', ''))
52endfun
53
54" [-- check if it's xml --]
55fun! <SID>XmlIndentSynCheck(lnum)
56 if '' != &syntax
57 let syn1 = synIDattr(synID(a:lnum, 1, 1), 'name')
58 let syn2 = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name')
59 if '' != syn1 && syn1 !~ 'xml' && '' != syn2 && syn2 !~ 'xml'
60 " don't indent pure non-xml code
61 return 0
62 endif
63 endif
64 return 1
65endfun
66
67" [-- return the sum of indents of a:lnum --]
68fun! <SID>XmlIndentSum(lnum, style, add)
69 let line = getline(a:lnum)
70 if a:style == match(line, '^\s*</')
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020071 return (shiftwidth() *
Bram Moolenaar071d4272004-06-13 20:20:40 +000072 \ (<SID>XmlIndentWithPattern(line, b:xml_indent_open)
73 \ - <SID>XmlIndentWithPattern(line, b:xml_indent_close)
74 \ - <SID>XmlIndentWithPattern(line, '.\{-}/>'))) + a:add
75 else
76 return a:add
77 endif
78endfun
79
80fun! XmlIndentGet(lnum, use_syntax_check)
81 " Find a non-empty line above the current line.
82 let lnum = prevnonblank(a:lnum - 1)
83
84 " Hit the start of the file, use zero indent.
85 if lnum == 0
86 return 0
87 endif
88
89 if a:use_syntax_check
Bram Moolenaar5c736222010-01-06 20:54:52 +010090 let check_lnum = <SID>XmlIndentSynCheck(lnum)
91 let check_alnum = <SID>XmlIndentSynCheck(a:lnum)
92 if 0 == check_lnum || 0 == check_alnum
Bram Moolenaar071d4272004-06-13 20:20:40 +000093 return indent(a:lnum)
Bram Moolenaar5c736222010-01-06 20:54:52 +010094 elseif -1 == check_lnum || -1 == check_alnum
95 return -1
Bram Moolenaar071d4272004-06-13 20:20:40 +000096 endif
97 endif
98
99 let ind = <SID>XmlIndentSum(lnum, -1, indent(lnum))
100 let ind = <SID>XmlIndentSum(a:lnum, 0, ind)
101
102 return ind
103endfun
104
Bram Moolenaar8e52a592012-05-18 21:49:28 +0200105let &cpo = s:keepcpo
106unlet s:keepcpo
107
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108" vim:ts=8