home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / unix / vim-6.2.tar.bz2 / vim-6.2.tar / vim62 / runtime / indent / html.vim < prev    next >
Encoding:
Text File  |  2003-05-11  |  6.1 KB  |  212 lines

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