Bram Moolenaar | 9d87a37 | 2018-12-18 21:41:50 +0100 | [diff] [blame] | 1 | " Language: xml |
| 2 | " Repository: https://github.com/chrisbra/vim-xml-ftplugin |
| 3 | " Last Changed: Dec 07th, 2018 |
| 4 | " Maintainer: Christian Brabandt <cb@256bit.org> |
| 5 | " Previous Maintainer: Johannes Zellner <johannes@zellner.org> |
| 6 | " Last Change: |
| 7 | " 20181116 - Fix indentation when tags start with a colon or an underscore |
| 8 | " https://github.com/vim/vim/pull/926 |
| 9 | " 20181022 - Do not overwrite indentkeys setting |
| 10 | " https://github.com/chrisbra/vim-xml-ftplugin/issues/1 |
| 11 | " 20180724 - Correctly indent xml comments https://github.com/vim/vim/issues/3200 |
| 12 | " |
| 13 | " Notes: |
| 14 | " 1) does not indent pure non-xml code (e.g. embedded scripts) |
| 15 | " 2) will be confused by unbalanced tags in comments |
| 16 | " or CDATA sections. |
| 17 | " 2009-05-26 patch by Nikolai Weibull |
| 18 | " TODO: implement pre-like tags, see xml_indent_open / xml_indent_close |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 19 | |
| 20 | " Only load this indent file when no other was loaded. |
| 21 | if exists("b:did_indent") |
| 22 | finish |
| 23 | endif |
| 24 | let b:did_indent = 1 |
Bram Moolenaar | 8e52a59 | 2012-05-18 21:49:28 +0200 | [diff] [blame] | 25 | let s:keepcpo= &cpo |
| 26 | set cpo&vim |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 27 | |
| 28 | " [-- local settings (must come before aborting the script) --] |
Bram Moolenaar | 9d87a37 | 2018-12-18 21:41:50 +0100 | [diff] [blame] | 29 | " Attention: Parameter use_syntax_check is used by the docbk.vim indent script |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 30 | setlocal indentexpr=XmlIndentGet(v:lnum,1) |
Bram Moolenaar | ba3ff53 | 2018-11-04 14:45:49 +0100 | [diff] [blame] | 31 | setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,},!^F |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 32 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 33 | if !exists('b:xml_indent_open') |
Bram Moolenaar | 9d87a37 | 2018-12-18 21:41:50 +0100 | [diff] [blame] | 34 | let b:xml_indent_open = '.\{-}<[:A-Z_a-z]' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 35 | " pre tag, e.g. <address> |
| 36 | " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!' |
| 37 | endif |
| 38 | |
| 39 | if !exists('b:xml_indent_close') |
| 40 | let b:xml_indent_close = '.\{-}</' |
| 41 | " end pre tag, e.g. </address> |
| 42 | " let b:xml_indent_close = '.\{-}</\(address\)\@!' |
| 43 | endif |
| 44 | |
Bram Moolenaar | 6c35bea | 2012-07-25 17:49:10 +0200 | [diff] [blame] | 45 | let &cpo = s:keepcpo |
| 46 | unlet s:keepcpo |
| 47 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 48 | " [-- finish, if the function already exists --] |
Bram Moolenaar | 6c35bea | 2012-07-25 17:49:10 +0200 | [diff] [blame] | 49 | if exists('*XmlIndentGet') |
Bram Moolenaar | 9d87a37 | 2018-12-18 21:41:50 +0100 | [diff] [blame] | 50 | finish |
Bram Moolenaar | 6c35bea | 2012-07-25 17:49:10 +0200 | [diff] [blame] | 51 | endif |
| 52 | |
| 53 | let s:keepcpo= &cpo |
| 54 | set cpo&vim |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 55 | |
| 56 | fun! <SID>XmlIndentWithPattern(line, pat) |
| 57 | let s = substitute('x'.a:line, a:pat, "\1", 'g') |
| 58 | return strlen(substitute(s, "[^\1].*$", '', '')) |
| 59 | endfun |
| 60 | |
| 61 | " [-- check if it's xml --] |
| 62 | fun! <SID>XmlIndentSynCheck(lnum) |
Bram Moolenaar | 9d87a37 | 2018-12-18 21:41:50 +0100 | [diff] [blame] | 63 | if &syntax != '' |
| 64 | let syn1 = synIDattr(synID(a:lnum, 1, 1), 'name') |
| 65 | let syn2 = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name') |
| 66 | if syn1 != '' && syn1 !~ 'xml' && syn2 != '' && syn2 !~ 'xml' |
| 67 | " don't indent pure non-xml code |
| 68 | return 0 |
| 69 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 70 | endif |
| 71 | return 1 |
| 72 | endfun |
| 73 | |
| 74 | " [-- return the sum of indents of a:lnum --] |
| 75 | fun! <SID>XmlIndentSum(lnum, style, add) |
| 76 | let line = getline(a:lnum) |
| 77 | if a:style == match(line, '^\s*</') |
Bram Moolenaar | 9d87a37 | 2018-12-18 21:41:50 +0100 | [diff] [blame] | 78 | return (shiftwidth() * |
| 79 | \ (<SID>XmlIndentWithPattern(line, b:xml_indent_open) |
| 80 | \ - <SID>XmlIndentWithPattern(line, b:xml_indent_close) |
| 81 | \ - <SID>XmlIndentWithPattern(line, '.\{-}/>'))) + a:add |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 82 | else |
Bram Moolenaar | 9d87a37 | 2018-12-18 21:41:50 +0100 | [diff] [blame] | 83 | return a:add |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 84 | endif |
| 85 | endfun |
| 86 | |
Bram Moolenaar | 9d87a37 | 2018-12-18 21:41:50 +0100 | [diff] [blame] | 87 | " Main indent function |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 88 | fun! XmlIndentGet(lnum, use_syntax_check) |
| 89 | " Find a non-empty line above the current line. |
Bram Moolenaar | 9d87a37 | 2018-12-18 21:41:50 +0100 | [diff] [blame] | 90 | let plnum = prevnonblank(a:lnum - 1) |
| 91 | " Find previous line with a tag (regardless whether open or closed) |
| 92 | let ptag = search('.\{-}<[/:A-Z_a-z]', 'bnw') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 93 | |
| 94 | " Hit the start of the file, use zero indent. |
Bram Moolenaar | 9d87a37 | 2018-12-18 21:41:50 +0100 | [diff] [blame] | 95 | if plnum == 0 |
| 96 | return 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 97 | endif |
Bram Moolenaar | 9d87a37 | 2018-12-18 21:41:50 +0100 | [diff] [blame] | 98 | let syn_name = '' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 99 | |
| 100 | if a:use_syntax_check |
Bram Moolenaar | 9d87a37 | 2018-12-18 21:41:50 +0100 | [diff] [blame] | 101 | let check_lnum = <SID>XmlIndentSynCheck(plnum) |
| 102 | let check_alnum = <SID>XmlIndentSynCheck(a:lnum) |
| 103 | if check_lnum == 0 || check_alnum == 0 |
| 104 | return indent(a:lnum) |
| 105 | endif |
| 106 | let syn_name = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 107 | endif |
| 108 | |
Bram Moolenaar | 9d87a37 | 2018-12-18 21:41:50 +0100 | [diff] [blame] | 109 | if syn_name =~ 'Comment' |
| 110 | return <SID>XmlIndentComment(a:lnum) |
| 111 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 112 | |
Bram Moolenaar | 9d87a37 | 2018-12-18 21:41:50 +0100 | [diff] [blame] | 113 | " Get indent from previous tag line |
| 114 | let ind = <SID>XmlIndentSum(ptag, -1, indent(ptag)) |
| 115 | " Determine indent from current line |
| 116 | let ind = <SID>XmlIndentSum(a:lnum, 0, ind) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 117 | return ind |
| 118 | endfun |
| 119 | |
Bram Moolenaar | 9d87a37 | 2018-12-18 21:41:50 +0100 | [diff] [blame] | 120 | " return indent for a commented line, |
| 121 | " the middle part might be indented on additional level |
| 122 | func! <SID>XmlIndentComment(lnum) |
| 123 | let ptagopen = search(b:xml_indent_open, 'bnw') |
| 124 | let ptagclose = search(b:xml_indent_close, 'bnw') |
| 125 | if getline(a:lnum) =~ '<!--' |
| 126 | " if previous tag was a closing tag, do not add |
| 127 | " one additional level of indent |
| 128 | if ptagclose > ptagopen && a:lnum > ptagclose |
| 129 | return indent(ptagclose) |
| 130 | else |
| 131 | " start of comment, add one indentation level |
| 132 | return indent(ptagopen) + shiftwidth() |
| 133 | endif |
| 134 | elseif getline(a:lnum) =~ '-->' |
| 135 | " end of comment, same as start of comment |
| 136 | return indent(search('<!--', 'bnw')) |
| 137 | else |
| 138 | " middle part of comment, add one additional level |
| 139 | return indent(search('<!--', 'bnw')) + shiftwidth() |
| 140 | endif |
| 141 | endfunc |
| 142 | |
Bram Moolenaar | 8e52a59 | 2012-05-18 21:49:28 +0200 | [diff] [blame] | 143 | let &cpo = s:keepcpo |
| 144 | unlet s:keepcpo |
| 145 | |
Bram Moolenaar | 9d87a37 | 2018-12-18 21:41:50 +0100 | [diff] [blame] | 146 | " vim:ts=4 et sts=-1 sw=0 |