My dotfiles. Period.

html.vim 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. " Description: HTML5 and inline SVG indenter
  2. " Changed By: HT de Beer <H.T.de.Beer@gmail.com>
  3. " Last Change: 20121013
  4. " Added the SVG elements to the list of indenting element. SVG elements
  5. " taken from http://www.w3.org/TR/SVG/eltindex.html
  6. "
  7. " Description: html5 (and html4) indenter
  8. " Changed By: Brian Gershon <brian.five@gmail.com>
  9. " Last Change: 30 Jan 2011
  10. "
  11. " 1. Started with vim72 html indent file authored by Johannes Zellner (below)
  12. " 2. Added html5 list as described here:
  13. " http://stackoverflow.com/questions/3232518/how-to-update-vim-to-color-code-new-html-elements
  14. " 3. Added this to a fork of https://github.com/othree/html5.vim
  15. " which already provides nice html5 syntax highlighting.
  16. "
  17. " Description: html indenter
  18. " Author: Johannes Zellner <johannes@zellner.org>
  19. " Last Change: Mo, 05 Jun 2006 22:32:41 CEST
  20. " Restoring 'cpo' and 'ic' added by Bram 2006 May 5
  21. " Globals:
  22. " let g:html_indent_tags = 'html\|p\|time'
  23. " let g:html_exclude_tags = ['html', 'style', 'script', 'body']
  24. " Only load this indent file when no other was loaded.
  25. if exists("b:did_indent")
  26. finish
  27. endif
  28. runtime! indent/javascript.vim
  29. let s:jsindent = &indentexpr
  30. unlet b:did_indent
  31. runtime! indent/css.vim
  32. let s:cssindent = &indentexpr
  33. let b:did_indent = 1
  34. " [-- local settings (must come before aborting the script) --]
  35. setlocal indentexpr=HtmlIndentGet(v:lnum)
  36. setlocal indentkeys=o,O,*<Return>,<>>,{,}
  37. let s:tags = []
  38. " [-- <ELEMENT ? - - ...> --]
  39. call add(s:tags, 'a')
  40. call add(s:tags, 'abbr')
  41. call add(s:tags, 'acronym')
  42. call add(s:tags, 'address')
  43. call add(s:tags, 'b')
  44. call add(s:tags, 'bdo')
  45. call add(s:tags, 'big')
  46. call add(s:tags, 'blockquote')
  47. call add(s:tags, 'button')
  48. call add(s:tags, 'caption')
  49. call add(s:tags, 'center')
  50. call add(s:tags, 'cite')
  51. call add(s:tags, 'code')
  52. call add(s:tags, 'colgroup')
  53. call add(s:tags, 'del')
  54. call add(s:tags, 'dfn')
  55. call add(s:tags, 'dir')
  56. call add(s:tags, 'div')
  57. call add(s:tags, 'dl')
  58. call add(s:tags, 'em')
  59. call add(s:tags, 'fieldset')
  60. call add(s:tags, 'font')
  61. call add(s:tags, 'form')
  62. call add(s:tags, 'frameset')
  63. call add(s:tags, 'h1')
  64. call add(s:tags, 'h2')
  65. call add(s:tags, 'h3')
  66. call add(s:tags, 'h4')
  67. call add(s:tags, 'h5')
  68. call add(s:tags, 'h6')
  69. call add(s:tags, 'i')
  70. call add(s:tags, 'iframe')
  71. call add(s:tags, 'ins')
  72. call add(s:tags, 'kbd')
  73. call add(s:tags, 'label')
  74. call add(s:tags, 'legend')
  75. call add(s:tags, 'li')
  76. call add(s:tags, 'map')
  77. call add(s:tags, 'menu')
  78. call add(s:tags, 'noframes')
  79. call add(s:tags, 'noscript')
  80. call add(s:tags, 'object')
  81. call add(s:tags, 'ol')
  82. call add(s:tags, 'optgroup')
  83. call add(s:tags, 'p')
  84. " call add(s:tags, 'pre')
  85. call add(s:tags, 'q')
  86. call add(s:tags, 's')
  87. call add(s:tags, 'samp')
  88. call add(s:tags, 'script')
  89. call add(s:tags, 'select')
  90. call add(s:tags, 'small')
  91. call add(s:tags, 'span')
  92. call add(s:tags, 'strong')
  93. call add(s:tags, 'style')
  94. call add(s:tags, 'sub')
  95. call add(s:tags, 'sup')
  96. call add(s:tags, 'table')
  97. call add(s:tags, 'textarea')
  98. call add(s:tags, 'title')
  99. call add(s:tags, 'tt')
  100. call add(s:tags, 'u')
  101. call add(s:tags, 'ul')
  102. call add(s:tags, 'var')
  103. " New HTML 5 elements
  104. call add(s:tags, 'article')
  105. call add(s:tags, 'aside')
  106. call add(s:tags, 'audio')
  107. call add(s:tags, 'canvas')
  108. call add(s:tags, 'datalist')
  109. call add(s:tags, 'details')
  110. call add(s:tags, 'figcaption')
  111. call add(s:tags, 'figure')
  112. call add(s:tags, 'footer')
  113. call add(s:tags, 'header')
  114. call add(s:tags, 'hgroup')
  115. call add(s:tags, 'mark')
  116. call add(s:tags, 'meter')
  117. call add(s:tags, 'nav')
  118. call add(s:tags, 'output')
  119. call add(s:tags, 'progress')
  120. call add(s:tags, 'rp')
  121. call add(s:tags, 'rt')
  122. call add(s:tags, 'ruby')
  123. call add(s:tags, 'section')
  124. call add(s:tags, 'summary')
  125. call add(s:tags, 'time')
  126. call add(s:tags, 'video')
  127. call add(s:tags, 'bdi')
  128. " Common inline used SVG elements
  129. call add(s:tags, 'clipPath')
  130. call add(s:tags, 'defs')
  131. call add(s:tags, 'desc')
  132. call add(s:tags, 'filter')
  133. call add(s:tags, 'foreignObject')
  134. call add(s:tags, 'g')
  135. call add(s:tags, 'linearGradient')
  136. call add(s:tags, 'marker')
  137. call add(s:tags, 'mask')
  138. call add(s:tags, 'pattern')
  139. call add(s:tags, 'radialGradient')
  140. call add(s:tags, 'svg')
  141. call add(s:tags, 'switch')
  142. call add(s:tags, 'symbol')
  143. call add(s:tags, 'text')
  144. call add(s:tags, 'textPath')
  145. call add(s:tags, 'tref')
  146. call add(s:tags, 'tspan')
  147. call add(s:tags, 'html')
  148. call add(s:tags, 'head')
  149. call add(s:tags, 'body')
  150. call add(s:tags, 'thead')
  151. call add(s:tags, 'tbody')
  152. call add(s:tags, 'tfoot')
  153. call add(s:tags, 'tr')
  154. call add(s:tags, 'th')
  155. call add(s:tags, 'td')
  156. if exists('g:html_exclude_tags')
  157. for tag in g:html_exclude_tags
  158. call remove(s:tags, index(s:tags, tag))
  159. endfor
  160. endif
  161. let s:html_indent_tags = join(s:tags, '\|')
  162. if exists('g:html_indent_tags')
  163. let s:html_indent_tags = s:html_indent_tags.'\|'.g:html_indent_tags
  164. endif
  165. let s:cpo_save = &cpo
  166. set cpo-=C
  167. " [-- count indent-increasing tags of line a:lnum --]
  168. fun! <SID>HtmlIndentOpen(lnum, pattern)
  169. let s = substitute('x'.getline(a:lnum),
  170. \ '.\{-}\(\(<\)\('.a:pattern.'\)\>\)', "\1", 'g')
  171. let s = substitute(s, "[^\1].*$", '', '')
  172. return strlen(s)
  173. endfun
  174. " [-- count indent-decreasing tags of line a:lnum --]
  175. fun! <SID>HtmlIndentClose(lnum, pattern)
  176. let s = substitute('x'.getline(a:lnum),
  177. \ '.\{-}\(\(<\)/\('.a:pattern.'\)\>>\)', "\1", 'g')
  178. let s = substitute(s, "[^\1].*$", '', '')
  179. return strlen(s)
  180. endfun
  181. " [-- count indent-increasing '{' of (java|css) line a:lnum --]
  182. fun! <SID>HtmlIndentOpenAlt(lnum)
  183. return strlen(substitute(getline(a:lnum), '[^{]\+', '', 'g'))
  184. endfun
  185. " [-- count indent-decreasing '}' of (java|css) line a:lnum --]
  186. fun! <SID>HtmlIndentCloseAlt(lnum)
  187. return strlen(substitute(getline(a:lnum), '[^}]\+', '', 'g'))
  188. endfun
  189. " [-- return the sum of indents respecting the syntax of a:lnum --]
  190. fun! <SID>HtmlIndentSum(lnum, style)
  191. if a:style == match(getline(a:lnum), '^\s*</')
  192. if a:style == match(getline(a:lnum), '^\s*</\<\('.s:html_indent_tags.'\)\>')
  193. let open = <SID>HtmlIndentOpen(a:lnum, s:html_indent_tags)
  194. let close = <SID>HtmlIndentClose(a:lnum, s:html_indent_tags)
  195. if 0 != open || 0 != close
  196. return open - close
  197. endif
  198. endif
  199. endif
  200. if '' != &syntax &&
  201. \ synIDattr(synID(a:lnum, 1, 1), 'name') =~ '\(css\|java\).*' &&
  202. \ synIDattr(synID(a:lnum, strlen(getline(a:lnum)), 1), 'name')
  203. \ =~ '\(css\|java\).*'
  204. if a:style == match(getline(a:lnum), '^\s*}')
  205. return <SID>HtmlIndentOpenAlt(a:lnum) - <SID>HtmlIndentCloseAlt(a:lnum)
  206. endif
  207. endif
  208. return 0
  209. endfun
  210. fun! HtmlIndentGet(lnum)
  211. " Find a non-empty line above the current line.
  212. let lnum = prevnonblank(a:lnum - 1)
  213. " Hit the start of the file, use zero indent.
  214. if lnum == 0
  215. return 0
  216. endif
  217. let restore_ic = &ic
  218. setlocal ic " ignore case
  219. " [-- special handling for <pre>: no indenting --]
  220. if getline(a:lnum) =~ '\c</pre>'
  221. \ || 0 < searchpair('\c<pre>', '', '\c</pre>', 'nWb')
  222. \ || 0 < searchpair('\c<pre>', '', '\c</pre>', 'nW')
  223. " we're in a line with </pre> or inside <pre> ... </pre>
  224. if restore_ic == 0
  225. setlocal noic
  226. endif
  227. return -1
  228. endif
  229. " [-- special handling for <javascript>: use cindent --]
  230. let js = '<script'
  231. let jse = '</script>'
  232. """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  233. " by Tye Zdrojewski <zdro@yahoo.com>, 05 Jun 2006
  234. " ZDR: This needs to be an AND (we are 'after the start of the pair' AND
  235. " we are 'before the end of the pair'). Otherwise, indentation
  236. " before the start of the script block will be affected; the end of
  237. " the pair will still match if we are before the beginning of the
  238. " pair.
  239. "
  240. if 0 < searchpair(js, '', jse, 'nWb')
  241. \ && 0 < searchpair(js, '', jse, 'nW')
  242. " we're inside javascript
  243. if getline(searchpair(js, '', '</script>', 'nWb')) !~ '<script [^>]*type=["'']\?text\/\(html\|template\)'
  244. \ && getline(lnum) !~ js && getline(a:lnum) !~ jse
  245. if restore_ic == 0
  246. setlocal noic
  247. endif
  248. if s:jsindent == ''
  249. return cindent(a:lnum)
  250. else
  251. execute 'let ind = ' . s:jsindent
  252. return ind
  253. endif
  254. endif
  255. if getline(a:lnum) =~ jse
  256. return indent(searchpair(js, '', jse, 'nWb'))
  257. endif
  258. endif
  259. let css = '<style'
  260. let csse = '</style>'
  261. if 0 < searchpair(css, '', csse, 'nWb')
  262. \ && 0 < searchpair(css, '', csse, 'nW')
  263. " we're inside style
  264. if getline(lnum) !~ css && getline(a:lnum) !~ csse
  265. if restore_ic == 0
  266. setlocal noic
  267. endif
  268. if s:cssindent == ''
  269. return cindent(a:lnum)
  270. else
  271. execute 'let ind = ' . s:cssindent
  272. return ind
  273. endif
  274. endif
  275. if getline(a:lnum) =~ csse
  276. return indent(searchpair(css, '', csse, 'nWb'))
  277. endif
  278. endif
  279. if getline(lnum) =~ '\c</pre>'
  280. " line before the current line a:lnum contains
  281. " a closing </pre>. --> search for line before
  282. " starting <pre> to restore the indent.
  283. let preline = prevnonblank(search('\c<pre>', 'bW') - 1)
  284. if preline > 0
  285. if restore_ic == 0
  286. setlocal noic
  287. endif
  288. if 0 == match(getline(a:lnum), '^\s*</')
  289. return indent(preline) - (1*&sw)
  290. else
  291. return indent(preline)
  292. endif
  293. endif
  294. endif
  295. let ind = <SID>HtmlIndentSum(lnum, -1)
  296. let ind = ind + <SID>HtmlIndentSum(a:lnum, 0)
  297. " Fix for conditional comment
  298. if getline(a:lnum) =~ '\c<!--.*<\(html\|body\).*-->'
  299. let ind = ind - 1
  300. endif
  301. if restore_ic == 0
  302. setlocal noic
  303. endif
  304. return indent(lnum) + (&sw * ind)
  305. endfun
  306. let &cpo = s:cpo_save
  307. unlet s:cpo_save
  308. " [-- EOF <runtime>/indent/html.vim --]