home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / vim60.zip / vim60rt2.zip / vim / vim60 / indent / xml.vim < prev    next >
Text File  |  2001-09-26  |  3KB  |  90 lines

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