Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Language: xml |
Bram Moolenaar | 91f84f6 | 2018-07-29 15:07:52 +0200 | [diff] [blame] | 2 | " Repository: https://github.com/chrisbra/vim-xml-ftplugin |
| 3 | " Maintainer: Christian Brabandt <cb@256bit.org> |
| 4 | " Previous Maintainer: Johannes Zellner <johannes@zellner.org> |
Bram Moolenaar | ba3ff53 | 2018-11-04 14:45:49 +0100 | [diff] [blame] | 5 | " 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8 | " 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 Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 11 | " 2009-05-26 patch by Nikolai Weibull |
| 12 | " TODO: implement pre-like tags, see xml_indent_open / xml_indent_close |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 13 | |
| 14 | " Only load this indent file when no other was loaded. |
| 15 | if exists("b:did_indent") |
| 16 | finish |
| 17 | endif |
| 18 | let b:did_indent = 1 |
Bram Moolenaar | 8e52a59 | 2012-05-18 21:49:28 +0200 | [diff] [blame] | 19 | let s:keepcpo= &cpo |
| 20 | set cpo&vim |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 21 | |
| 22 | " [-- local settings (must come before aborting the script) --] |
| 23 | setlocal indentexpr=XmlIndentGet(v:lnum,1) |
Bram Moolenaar | ba3ff53 | 2018-11-04 14:45:49 +0100 | [diff] [blame] | 24 | setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,},!^F |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 25 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 26 | if !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\)\@!' |
| 30 | endif |
| 31 | |
| 32 | if !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\)\@!' |
| 36 | endif |
| 37 | |
Bram Moolenaar | 6c35bea | 2012-07-25 17:49:10 +0200 | [diff] [blame] | 38 | let &cpo = s:keepcpo |
| 39 | unlet s:keepcpo |
| 40 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 41 | " [-- finish, if the function already exists --] |
Bram Moolenaar | 6c35bea | 2012-07-25 17:49:10 +0200 | [diff] [blame] | 42 | if exists('*XmlIndentGet') |
| 43 | finish |
| 44 | endif |
| 45 | |
| 46 | let s:keepcpo= &cpo |
| 47 | set cpo&vim |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 48 | |
| 49 | fun! <SID>XmlIndentWithPattern(line, pat) |
| 50 | let s = substitute('x'.a:line, a:pat, "\1", 'g') |
| 51 | return strlen(substitute(s, "[^\1].*$", '', '')) |
| 52 | endfun |
| 53 | |
| 54 | " [-- check if it's xml --] |
| 55 | fun! <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 |
| 65 | endfun |
| 66 | |
| 67 | " [-- return the sum of indents of a:lnum --] |
| 68 | fun! <SID>XmlIndentSum(lnum, style, add) |
| 69 | let line = getline(a:lnum) |
| 70 | if a:style == match(line, '^\s*</') |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 71 | return (shiftwidth() * |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 72 | \ (<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 |
| 78 | endfun |
| 79 | |
| 80 | fun! 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 Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 90 | let check_lnum = <SID>XmlIndentSynCheck(lnum) |
| 91 | let check_alnum = <SID>XmlIndentSynCheck(a:lnum) |
| 92 | if 0 == check_lnum || 0 == check_alnum |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 93 | return indent(a:lnum) |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 94 | elseif -1 == check_lnum || -1 == check_alnum |
| 95 | return -1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 96 | 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 |
| 103 | endfun |
| 104 | |
Bram Moolenaar | 8e52a59 | 2012-05-18 21:49:28 +0200 | [diff] [blame] | 105 | let &cpo = s:keepcpo |
| 106 | unlet s:keepcpo |
| 107 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 108 | " vim:ts=8 |