blob: 29069bab8402c2065faaf30b8b92b5ec93cc2b54 [file] [log] [blame]
Bram Moolenaar9d87a372018-12-18 21:41:50 +01001" Language: xml
2" Repository: https://github.com/chrisbra/vim-xml-ftplugin
Bram Moolenaar314dd792019-02-03 15:27:20 +01003" Last Changed: Jan 28, 2019
Bram Moolenaar9d87a372018-12-18 21:41:50 +01004" Maintainer: Christian Brabandt <cb@256bit.org>
5" Previous Maintainer: Johannes Zellner <johannes@zellner.org>
6" Last Change:
Bram Moolenaar314dd792019-02-03 15:27:20 +01007" 20190128 - Make sure to find previous tag
8" https://github.com/chrisbra/vim-xml-ftplugin/issues/4
Bram Moolenaar9d87a372018-12-18 21:41:50 +01009" 20181116 - Fix indentation when tags start with a colon or an underscore
10" https://github.com/vim/vim/pull/926
11" 20181022 - Do not overwrite indentkeys setting
12" https://github.com/chrisbra/vim-xml-ftplugin/issues/1
13" 20180724 - Correctly indent xml comments https://github.com/vim/vim/issues/3200
14"
15" Notes:
16" 1) does not indent pure non-xml code (e.g. embedded scripts)
17" 2) will be confused by unbalanced tags in comments
18" or CDATA sections.
19" 2009-05-26 patch by Nikolai Weibull
20" TODO: implement pre-like tags, see xml_indent_open / xml_indent_close
Bram Moolenaar071d4272004-06-13 20:20:40 +000021
22" Only load this indent file when no other was loaded.
23if exists("b:did_indent")
24 finish
25endif
26let b:did_indent = 1
Bram Moolenaar8e52a592012-05-18 21:49:28 +020027let s:keepcpo= &cpo
28set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
30" [-- local settings (must come before aborting the script) --]
Bram Moolenaar9d87a372018-12-18 21:41:50 +010031" Attention: Parameter use_syntax_check is used by the docbk.vim indent script
Bram Moolenaar071d4272004-06-13 20:20:40 +000032setlocal indentexpr=XmlIndentGet(v:lnum,1)
Bram Moolenaarba3ff532018-11-04 14:45:49 +010033setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,},!^F
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
Bram Moolenaar071d4272004-06-13 20:20:40 +000035if !exists('b:xml_indent_open')
Bram Moolenaar9d87a372018-12-18 21:41:50 +010036 let b:xml_indent_open = '.\{-}<[:A-Z_a-z]'
Bram Moolenaar071d4272004-06-13 20:20:40 +000037 " pre tag, e.g. <address>
38 " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!'
39endif
40
41if !exists('b:xml_indent_close')
42 let b:xml_indent_close = '.\{-}</'
43 " end pre tag, e.g. </address>
44 " let b:xml_indent_close = '.\{-}</\(address\)\@!'
45endif
46
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020047let &cpo = s:keepcpo
48unlet s:keepcpo
49
Bram Moolenaar071d4272004-06-13 20:20:40 +000050" [-- finish, if the function already exists --]
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020051if exists('*XmlIndentGet')
Bram Moolenaar9d87a372018-12-18 21:41:50 +010052 finish
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020053endif
54
55let s:keepcpo= &cpo
56set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000057
58fun! <SID>XmlIndentWithPattern(line, pat)
59 let s = substitute('x'.a:line, a:pat, "\1", 'g')
60 return strlen(substitute(s, "[^\1].*$", '', ''))
61endfun
62
63" [-- check if it's xml --]
64fun! <SID>XmlIndentSynCheck(lnum)
Bram Moolenaar9d87a372018-12-18 21:41:50 +010065 if &syntax != ''
66 let syn1 = synIDattr(synID(a:lnum, 1, 1), 'name')
67 let syn2 = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name')
68 if syn1 != '' && syn1 !~ 'xml' && syn2 != '' && syn2 !~ 'xml'
69 " don't indent pure non-xml code
70 return 0
71 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000072 endif
73 return 1
74endfun
75
76" [-- return the sum of indents of a:lnum --]
77fun! <SID>XmlIndentSum(lnum, style, add)
78 let line = getline(a:lnum)
79 if a:style == match(line, '^\s*</')
Bram Moolenaar9d87a372018-12-18 21:41:50 +010080 return (shiftwidth() *
81 \ (<SID>XmlIndentWithPattern(line, b:xml_indent_open)
82 \ - <SID>XmlIndentWithPattern(line, b:xml_indent_close)
83 \ - <SID>XmlIndentWithPattern(line, '.\{-}/>'))) + a:add
Bram Moolenaar071d4272004-06-13 20:20:40 +000084 else
Bram Moolenaar9d87a372018-12-18 21:41:50 +010085 return a:add
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 endif
87endfun
88
Bram Moolenaar9d87a372018-12-18 21:41:50 +010089" Main indent function
Bram Moolenaar071d4272004-06-13 20:20:40 +000090fun! XmlIndentGet(lnum, use_syntax_check)
91 " Find a non-empty line above the current line.
Bram Moolenaar9d87a372018-12-18 21:41:50 +010092 let plnum = prevnonblank(a:lnum - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000093 " Hit the start of the file, use zero indent.
Bram Moolenaar9d87a372018-12-18 21:41:50 +010094 if plnum == 0
95 return 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000096 endif
Bram Moolenaar314dd792019-02-03 15:27:20 +010097 " Find previous line with a tag (regardless whether open or closed,
98 " but always start restrict the match to a line before the current one
99 let ptag_pattern = '\%(.\{-}<[/:A-Z_a-z]\)'. '\%(\&\%<'. line('.').'l\)'
100 let ptag = search(ptag_pattern, 'bnw')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101
Bram Moolenaar314dd792019-02-03 15:27:20 +0100102 let syn_name = ''
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103 if a:use_syntax_check
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100104 let check_lnum = <SID>XmlIndentSynCheck(plnum)
105 let check_alnum = <SID>XmlIndentSynCheck(a:lnum)
106 if check_lnum == 0 || check_alnum == 0
107 return indent(a:lnum)
108 endif
109 let syn_name = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110 endif
111
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100112 if syn_name =~ 'Comment'
113 return <SID>XmlIndentComment(a:lnum)
114 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100116 " Get indent from previous tag line
117 let ind = <SID>XmlIndentSum(ptag, -1, indent(ptag))
118 " Determine indent from current line
119 let ind = <SID>XmlIndentSum(a:lnum, 0, ind)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 return ind
121endfun
122
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100123" return indent for a commented line,
124" the middle part might be indented on additional level
125func! <SID>XmlIndentComment(lnum)
126 let ptagopen = search(b:xml_indent_open, 'bnw')
127 let ptagclose = search(b:xml_indent_close, 'bnw')
128 if getline(a:lnum) =~ '<!--'
129 " if previous tag was a closing tag, do not add
130 " one additional level of indent
131 if ptagclose > ptagopen && a:lnum > ptagclose
132 return indent(ptagclose)
133 else
134 " start of comment, add one indentation level
135 return indent(ptagopen) + shiftwidth()
136 endif
137 elseif getline(a:lnum) =~ '-->'
138 " end of comment, same as start of comment
139 return indent(search('<!--', 'bnw'))
140 else
141 " middle part of comment, add one additional level
142 return indent(search('<!--', 'bnw')) + shiftwidth()
143 endif
144endfunc
145
Bram Moolenaar8e52a592012-05-18 21:49:28 +0200146let &cpo = s:keepcpo
147unlet s:keepcpo
148
Bram Moolenaar9d87a372018-12-18 21:41:50 +0100149" vim:ts=4 et sts=-1 sw=0