Guest User

Untitled

a guest
Mar 22nd, 2018
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.44 KB | None | 0 0
  1. " Description: html indenter
  2. " Author: Johannes Zellner <johannes@zellner.org>
  3. " Last Change: Tue, 27 Apr 2004 10:28:39 CEST
  4. " Restoring 'cpo' and 'ic' added by Bram 2006 May 5
  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>,<>>,{,}
  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('colgroup')
  50. call <SID>HtmlIndentPush('del')
  51. call <SID>HtmlIndentPush('dfn')
  52. call <SID>HtmlIndentPush('dir')
  53. call <SID>HtmlIndentPush('div')
  54. call <SID>HtmlIndentPush('dl')
  55. call <SID>HtmlIndentPush('em')
  56. call <SID>HtmlIndentPush('fieldset')
  57. call <SID>HtmlIndentPush('font')
  58. call <SID>HtmlIndentPush('form')
  59. call <SID>HtmlIndentPush('frameset')
  60. call <SID>HtmlIndentPush('h1')
  61. call <SID>HtmlIndentPush('h2')
  62. call <SID>HtmlIndentPush('h3')
  63. call <SID>HtmlIndentPush('h4')
  64. call <SID>HtmlIndentPush('h5')
  65. call <SID>HtmlIndentPush('h6')
  66. call <SID>HtmlIndentPush('i')
  67. call <SID>HtmlIndentPush('iframe')
  68. call <SID>HtmlIndentPush('ins')
  69. call <SID>HtmlIndentPush('kbd')
  70. call <SID>HtmlIndentPush('label')
  71. call <SID>HtmlIndentPush('legend')
  72. call <SID>HtmlIndentPush('map')
  73. call <SID>HtmlIndentPush('menu')
  74. call <SID>HtmlIndentPush('noframes')
  75. call <SID>HtmlIndentPush('noscript')
  76. call <SID>HtmlIndentPush('object')
  77. call <SID>HtmlIndentPush('ol')
  78. call <SID>HtmlIndentPush('optgroup')
  79. " call <SID>HtmlIndentPush('pre')
  80. call <SID>HtmlIndentPush('q')
  81. call <SID>HtmlIndentPush('s')
  82. call <SID>HtmlIndentPush('samp')
  83. call <SID>HtmlIndentPush('script')
  84. call <SID>HtmlIndentPush('select')
  85. call <SID>HtmlIndentPush('small')
  86. call <SID>HtmlIndentPush('span')
  87. call <SID>HtmlIndentPush('strong')
  88. call <SID>HtmlIndentPush('style')
  89. call <SID>HtmlIndentPush('sub')
  90. call <SID>HtmlIndentPush('sup')
  91. call <SID>HtmlIndentPush('table')
  92. call <SID>HtmlIndentPush('textarea')
  93. call <SID>HtmlIndentPush('title')
  94. call <SID>HtmlIndentPush('tt')
  95. call <SID>HtmlIndentPush('u')
  96. call <SID>HtmlIndentPush('ul')
  97. call <SID>HtmlIndentPush('var')
  98.  
  99.  
  100. " [-- <ELEMENT ? O O ...> --]
  101. if !exists('g:html_indent_strict')
  102. call <SID>HtmlIndentPush('body')
  103. call <SID>HtmlIndentPush('head')
  104. call <SID>HtmlIndentPush('html')
  105. call <SID>HtmlIndentPush('tbody')
  106. endif
  107.  
  108.  
  109. " [-- <ELEMENT ? O - ...> --]
  110. if !exists('g:html_indent_strict_table')
  111. call <SID>HtmlIndentPush('th')
  112. call <SID>HtmlIndentPush('td')
  113. call <SID>HtmlIndentPush('tr')
  114. call <SID>HtmlIndentPush('tfoot')
  115. call <SID>HtmlIndentPush('thead')
  116. endif
  117.  
  118. delfun <SID>HtmlIndentPush
  119.  
  120. let s:cpo_save = &cpo
  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! s:getSyntaxName(lnum, re)
  172. return synIDattr(synID(a:lnum, match(getline(a:lnum), a:re) + 1, 0), "name")
  173. endfun
  174.  
  175. fun! s:isSyntaxElem(lnum, re, elem)
  176. if getline(a:lnum) =~ a:re && s:getSyntaxName(a:lnum, a:re) == a:elem
  177. return 1
  178. endif
  179. return 0
  180. endfun
  181.  
  182. fun! HtmlIndentGet(lnum)
  183. " Find a non-empty line above the current line.
  184. let lnum = prevnonblank(a:lnum - 1)
  185.  
  186. " Hit the start of the file, use zero indent.
  187. if lnum == 0
  188. return 0
  189. endif
  190.  
  191. let restore_ic = &ic
  192. setlocal ic " ignore case
  193.  
  194. " [-- special handling for <pre>: no indenting --]
  195. if getline(a:lnum) =~ 'c</pre>'
  196. || 0 < searchpair('c<pre>', '', 'c</pre>', 'nWb')
  197. || 0 < searchpair('c<pre>', '', 'c</pre>', 'nW')
  198. " we're in a line with </pre> or inside <pre> ... </pre>
  199. if restore_ic == 0
  200. setlocal noic
  201. endif
  202. return -1
  203. endif
  204.  
  205. " [-- special handling for <javascript>: use cindent --]
  206. let js = '<script.*types*=s*.*java'
  207. if 0 < searchpair(js, '', '</script>', 'nWb')
  208. || 0 < searchpair(js, '', '</script>', 'nW')
  209. " we're inside javascript
  210. if getline(lnum) !~ js && getline(a:lnum) !~ js
  211. if restore_ic == 0
  212. setlocal noic
  213. endif
  214. " Open and close bracket:
  215. if s:isSyntaxElem(lnum, '{', "javaScriptBraces") && s:isSyntaxElem(a:lnum, '}', "javaScriptBraces")
  216. return indent(lnum)
  217. elseif s:isSyntaxElem(lnum, '{', "javaScriptBraces")
  218. return indent(lnum) + &sw
  219. elseif s:isSyntaxElem(a:lnum, '}', "javaScriptBraces")
  220. if s:isSyntaxElem(lnum, 'break', 'javaScriptBranch') && ! s:isSyntaxElem(lnum, '(case|default)', "javaScriptLabel")
  221. return indent(lnum) - 2 * &sw
  222. endif
  223. return indent(lnum) - &sw
  224. endif
  225.  
  226. " cases:
  227. if s:isSyntaxElem(lnum, '(case|default)', "javaScriptLabel") && s:isSyntaxElem(a:lnum, '(case|default)', "javaScriptLabel")
  228. return indent(lnum)
  229. elseif s:isSyntaxElem(lnum, '(case|default)', "javaScriptLabel")
  230. return indent(lnum) + &sw
  231. elseif s:isSyntaxElem(a:lnum, '(case|default)', "javaScriptLabel")
  232. return indent(lnum) - &sw
  233. endif
  234.  
  235. if getline(a:lnum) =~ 'c</script>'
  236. let scriptline = prevnonblank(search(js, 'bW'))
  237. if scriptline > 0
  238. return indent(scriptline)
  239. endif
  240. endif
  241. return indent(lnum)
  242. endif
  243. endif
  244.  
  245. if getline(a:lnum) =~ 'c</?body' || getline(a:lnum) =~ 'c</?html' || getline(a:lnum) =~ 'c</?head'
  246. return 0
  247. endif
  248. if getline(lnum) =~ 'c</?body' || getline(lnum) =~ 'c</?html' || getline(lnum) =~ 'c</?head'
  249. return 0
  250. endif
  251.  
  252. if getline(lnum) =~ 'c</pre>'
  253. " line before the current line a:lnum contains
  254. " a closing </pre>. --> search for line before
  255. " starting <pre> to restore the indent.
  256. let preline = prevnonblank(search('c<pre>', 'bW') - 1)
  257. if preline > 0
  258. if restore_ic == 0
  259. setlocal noic
  260. endif
  261. return indent(preline)
  262. endif
  263. endif
  264.  
  265. let ind = <SID>HtmlIndentSum(lnum, -1)
  266. let ind = ind + <SID>HtmlIndentSum(a:lnum, 0)
  267.  
  268. if restore_ic == 0
  269. setlocal noic
  270. endif
  271.  
  272. return indent(lnum) + (&sw * ind)
  273. endfun
  274.  
  275. let &cpo = s:cpo_save
  276. unlet s:cpo_save
  277.  
  278. " [-- EOF <runtime>/indent/html.vim --]
Add Comment
Please, Sign In to add comment