xml.vim 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. " Language: xml
  2. " Maintainer: Johannes Zellner <johannes@zellner.org>
  3. " Last Change: 2017 Jun 13
  4. " Notes: 1) does not indent pure non-xml code (e.g. embedded scripts)
  5. " 2) will be confused by unbalanced tags in comments
  6. " or CDATA sections.
  7. " 2009-05-26 patch by Nikolai Weibull
  8. " TODO: implement pre-like tags, see xml_indent_open / xml_indent_close
  9. " Only load this indent file when no other was loaded.
  10. if exists("b:did_indent")
  11. finish
  12. endif
  13. let b:did_indent = 1
  14. let s:keepcpo= &cpo
  15. set cpo&vim
  16. " [-- local settings (must come before aborting the script) --]
  17. setlocal indentexpr=XmlIndentGet(v:lnum,0)
  18. setlocal indentkeys=o,O,*<Return>,<>>,<<>,/,{,}
  19. if !exists('b:xml_indent_open')
  20. let b:xml_indent_open = '.\{-}<\a'
  21. " pre tag, e.g. <address>
  22. " let b:xml_indent_open = '.\{-}<[/]\@!\(address\)\@!'
  23. endif
  24. if !exists('b:xml_indent_close')
  25. let b:xml_indent_close = '.\{-}</'
  26. " end pre tag, e.g. </address>
  27. " let b:xml_indent_close = '.\{-}</\(address\)\@!'
  28. endif
  29. let &cpo = s:keepcpo
  30. unlet s:keepcpo
  31. " [-- finish, if the function already exists --]
  32. if exists('*XmlIndentGet')
  33. finish
  34. endif
  35. let s:keepcpo= &cpo
  36. set cpo&vim
  37. fun! <SID>XmlIndentWithPattern(line, pat)
  38. let s = substitute('x'.a:line, a:pat, "\1", 'g')
  39. return strlen(substitute(s, "[^\1].*$", '', ''))
  40. endfun
  41. " [-- check if it's xml --]
  42. fun! <SID>XmlIndentSynCheck(lnum)
  43. if '' != &syntax
  44. let syn1 = synIDattr(synID(a:lnum, 1, 1), 'name')
  45. let syn2 = synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name')
  46. if '' != syn1 && syn1 !~ 'xml' && '' != syn2 && syn2 !~ 'xml'
  47. " don't indent pure non-xml code
  48. return 0
  49. elseif syn1 =~ '^xmlComment' && syn2 =~ '^xmlComment'
  50. " indent comments specially
  51. return -1
  52. endif
  53. endif
  54. return 1
  55. endfun
  56. " [-- return the sum of indents of a:lnum --]
  57. fun! <SID>XmlIndentSum(lnum, style, add)
  58. let line = getline(a:lnum)
  59. if a:style == match(line, '^\s*</')
  60. return (shiftwidth() *
  61. \ (<SID>XmlIndentWithPattern(line, b:xml_indent_open)
  62. \ - <SID>XmlIndentWithPattern(line, b:xml_indent_close)
  63. \ - <SID>XmlIndentWithPattern(line, '.\{-}/>'))) + a:add
  64. else
  65. return a:add
  66. endif
  67. endfun
  68. fun! XmlIndentGet(lnum, use_syntax_check)
  69. " Find a non-empty line above the current line.
  70. let lnum = prevnonblank(a:lnum - 1)
  71. " Hit the start of the file, use zero indent.
  72. if lnum == 0
  73. return 0
  74. endif
  75. if a:use_syntax_check
  76. let check_lnum = <SID>XmlIndentSynCheck(lnum)
  77. let check_alnum = <SID>XmlIndentSynCheck(a:lnum)
  78. if 0 == check_lnum || 0 == check_alnum
  79. return indent(a:lnum)
  80. elseif -1 == check_lnum || -1 == check_alnum
  81. return -1
  82. endif
  83. endif
  84. let ind = <SID>XmlIndentSum(lnum, -1, indent(lnum))
  85. let ind = <SID>XmlIndentSum(a:lnum, 0, ind)
  86. return ind
  87. endfun
  88. let &cpo = s:keepcpo
  89. unlet s:keepcpo
  90. " vim:ts=8