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

  1. " Description:    html indenter
  2. " Author:    Johannes Zellner <johannes@zellner.org>
  3. " URL:        http://www.zellner.org/vim/indent/html.vim
  4. " Last Change:    Tue, 25 Sep 2001 13:22:27 +0200
  5. " Globals:    g:html_indent_tags         -- indenting tags
  6. "        g:html_indent_strict       -- inhibit 'O O' elements
  7. "        g:html_indent_strict_table -- inhibit 'O -' elements
  8.  
  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.  
  15.  
  16. " [-- local settings (must come before aborting the script) --]
  17. setlocal indentexpr=HtmlIndentGet(v:lnum)
  18. setlocal indentkeys=o,O,*<Return>,<>>,<bs>,{,}
  19.  
  20.  
  21. if exists('g:html_indent_tags')
  22.     unlet g:html_indent_tags
  23. endif
  24.  
  25. " [-- helper function to assemble tag list --]
  26. fun! <SID>HtmlIndentPush(tag)
  27.     if exists('g:html_indent_tags')
  28.     let g:html_indent_tags = g:html_indent_tags.'\|'.a:tag
  29.     else
  30.     let g:html_indent_tags = a:tag
  31.     endif
  32. endfun
  33.  
  34.  
  35. " [-- <ELEMENT ? - - ...> --]
  36. call <SID>HtmlIndentPush('a')
  37. call <SID>HtmlIndentPush('abbr')
  38. call <SID>HtmlIndentPush('acronym')
  39. call <SID>HtmlIndentPush('address')
  40. call <SID>HtmlIndentPush('b')
  41. call <SID>HtmlIndentPush('bdo')
  42. call <SID>HtmlIndentPush('big')
  43. call <SID>HtmlIndentPush('blockquote')
  44. call <SID>HtmlIndentPush('button')
  45. call <SID>HtmlIndentPush('caption')
  46. call <SID>HtmlIndentPush('center')
  47. call <SID>HtmlIndentPush('cite')
  48. call <SID>HtmlIndentPush('code')
  49. call <SID>HtmlIndentPush('del')
  50. call <SID>HtmlIndentPush('dfn')
  51. call <SID>HtmlIndentPush('dir')
  52. call <SID>HtmlIndentPush('div')
  53. call <SID>HtmlIndentPush('dl')
  54. call <SID>HtmlIndentPush('em')
  55. call <SID>HtmlIndentPush('fieldset')
  56. call <SID>HtmlIndentPush('font')
  57. call <SID>HtmlIndentPush('form')
  58. call <SID>HtmlIndentPush('frameset')
  59. call <SID>HtmlIndentPush('h1')
  60. call <SID>HtmlIndentPush('h2')
  61. call <SID>HtmlIndentPush('h3')
  62. call <SID>HtmlIndentPush('h4')
  63. call <SID>HtmlIndentPush('h5')
  64. call <SID>HtmlIndentPush('h6')
  65. call <SID>HtmlIndentPush('i')
  66. call <SID>HtmlIndentPush('iframe')
  67. call <SID>HtmlIndentPush('ins')
  68. call <SID>HtmlIndentPush('kbd')
  69. call <SID>HtmlIndentPush('label')
  70. call <SID>HtmlIndentPush('legend')
  71. call <SID>HtmlIndentPush('map')
  72. call <SID>HtmlIndentPush('menu')
  73. call <SID>HtmlIndentPush('noframes')
  74. call <SID>HtmlIndentPush('noscript')
  75. call <SID>HtmlIndentPush('object')
  76. call <SID>HtmlIndentPush('ol')
  77. call <SID>HtmlIndentPush('optgroup')
  78. call <SID>HtmlIndentPush('pre')
  79. call <SID>HtmlIndentPush('q')
  80. call <SID>HtmlIndentPush('s')
  81. call <SID>HtmlIndentPush('samp')
  82. call <SID>HtmlIndentPush('script')
  83. call <SID>HtmlIndentPush('select')
  84. call <SID>HtmlIndentPush('small')
  85. call <SID>HtmlIndentPush('span')
  86. call <SID>HtmlIndentPush('strong')
  87. call <SID>HtmlIndentPush('style')
  88. call <SID>HtmlIndentPush('sub')
  89. call <SID>HtmlIndentPush('sup')
  90. call <SID>HtmlIndentPush('table')
  91. call <SID>HtmlIndentPush('textarea')
  92. call <SID>HtmlIndentPush('title')
  93. call <SID>HtmlIndentPush('tt')
  94. call <SID>HtmlIndentPush('u')
  95. call <SID>HtmlIndentPush('ul')
  96. call <SID>HtmlIndentPush('var')
  97.  
  98.  
  99. " [-- <ELEMENT ? O O ...> --]
  100. if !exists('g:html_indent_strict')
  101.     call <SID>HtmlIndentPush('body')
  102.     call <SID>HtmlIndentPush('head')
  103.     call <SID>HtmlIndentPush('html')
  104.     call <SID>HtmlIndentPush('tbody')
  105. endif
  106.  
  107.  
  108. " [-- <ELEMENT ? O - ...> --]
  109. if !exists('g:html_indent_strict_table')
  110.     call <SID>HtmlIndentPush('th')
  111.     call <SID>HtmlIndentPush('td')
  112.     call <SID>HtmlIndentPush('tr')
  113.     call <SID>HtmlIndentPush('tfoot')
  114.     call <SID>HtmlIndentPush('thead')
  115. endif
  116.  
  117. delfun <SID>HtmlIndentPush
  118.  
  119. set cpo-=C
  120.  
  121. " [-- count indent-increasing tags of line a:lnum --]
  122. fun! <SID>HtmlIndentOpen(lnum)
  123.     let s = substitute('x'.getline(a:lnum),
  124.     \ '.\{-}\(\(<\)\('.g:html_indent_tags.'\)\>\)', 'º', 'g')
  125.     let s = substitute(s, '[^º].*$', '', '')
  126.     return strlen(s)
  127. endfun
  128.  
  129. " [-- count indent-decreasing tags of line a:lnum --]
  130. fun! <SID>HtmlIndentClose(lnum)
  131.     let s = substitute('x'.getline(a:lnum),
  132.     \ '.\{-}\(\(<\)/\('.g:html_indent_tags.'\)\>>\)', 'º', 'g')
  133.     let s = substitute(s, '[^º].*$', '', '')
  134.     return strlen(s)
  135. endfun
  136.  
  137. " [-- count indent-increasing '{' of (java|css) line a:lnum --]
  138. fun! <SID>HtmlIndentOpenAlt(lnum)
  139.     return strlen(substitute(getline(a:lnum), '[^{]\+', '', 'g'))
  140. endfun
  141.  
  142. " [-- count indent-decreasing '}' of (java|css) line a:lnum --]
  143. fun! <SID>HtmlIndentCloseAlt(lnum)
  144.     return strlen(substitute(getline(a:lnum), '[^}]\+', '', 'g'))
  145. endfun
  146.  
  147. " [-- return the sum of indents respecting the syntax of a:lnum --]
  148. fun! <SID>HtmlIndentSum(lnum, style)
  149.     if a:style == match(getline(a:lnum), '^\s*</')
  150.     if a:style == match(getline(a:lnum), '^\s*</\<\('.g:html_indent_tags.'\)\>') 
  151.         let open = <SID>HtmlIndentOpen(a:lnum)
  152.         let close = <SID>HtmlIndentClose(a:lnum)
  153.         if 0 != open || 0 != close
  154.         return open - close
  155.         endif
  156.     endif
  157.     endif
  158.     if '' != &syntax &&
  159.     \ synIDattr(synID(a:lnum, 1, 1), 'name') =~ '\(css\|java\).*' &&
  160.     \ synIDattr(synID(a:lnum, strlen(getline(a:lnum)) - 1, 1), 'name')
  161.     \ =~ '\(css\|java\).*'
  162.     if a:style == match(getline(a:lnum), '^\s*}')
  163.         return <SID>HtmlIndentOpenAlt(a:lnum) - <SID>HtmlIndentCloseAlt(a:lnum)
  164.     endif
  165.     endif
  166.     return 0
  167. endfun
  168.  
  169. fun! HtmlIndentGet(lnum)
  170.     " Find a non-empty line above the current line.
  171.     let lnum = prevnonblank(a:lnum - 1)
  172.  
  173.     " Hit the start of the file, use zero indent.
  174.     if lnum == 0
  175.     return 0
  176.     endif
  177.  
  178.     let restore_ic=&ic
  179.     let &ic=1 " ignore case
  180.  
  181.     let ind = <SID>HtmlIndentSum(lnum, -1)
  182.     let ind = ind + <SID>HtmlIndentSum(a:lnum, 0)
  183.  
  184.     let &ic=restore_ic
  185.  
  186.     return indent(lnum) + (&sw * ind)
  187. endfun
  188.  
  189. " [-- EOF <runtime>/indent/html.vim --]
  190.