Skip to content

Commit 3286328

Browse files
author
lepture
committed
add indent for html
1 parent 97e663f commit 3286328

File tree

1 file changed

+242
-0
lines changed

1 file changed

+242
-0
lines changed

indent/html.vim

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

0 commit comments

Comments
 (0)