Add support for :winpos
[MacVim.git] / runtime / indent / html.vim
blob6f016ad16ce1cfcd84551ca22aca96bb5df60278
1 " Description:  html indenter
2 " Author:       Johannes Zellner <johannes@zellner.org>
3 " Last Change:  Mo, 05 Jun 2006 22:32:41 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
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
16 " [-- local settings (must come before aborting the script) --]
17 setlocal indentexpr=HtmlIndentGet(v:lnum)
18 setlocal indentkeys=o,O,*<Return>,<>>,{,}
21 if exists('g:html_indent_tags')
22     unlet g:html_indent_tags
23 endif
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
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')
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
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
118 delfun <SID>HtmlIndentPush
120 let s:cpo_save = &cpo
121 set cpo-=C
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
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
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
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
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
171 fun! HtmlIndentGet(lnum)
172     " Find a non-empty line above the current line.
173     let lnum = prevnonblank(a:lnum - 1)
175     " Hit the start of the file, use zero indent.
176     if lnum == 0
177         return 0
178     endif
180     let restore_ic = &ic
181     setlocal ic " ignore case
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         if restore_ic == 0
189           setlocal noic
190         endif
191         return -1
192     endif
194     " [-- special handling for <javascript>: use cindent --]
195     let js = '<script.*type\s*=\s*.*java'
197     """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
198     " by Tye Zdrojewski <zdro@yahoo.com>, 05 Jun 2006
199     " ZDR: This needs to be an AND (we are 'after the start of the pair' AND
200     "      we are 'before the end of the pair').  Otherwise, indentation
201     "      before the start of the script block will be affected; the end of
202     "      the pair will still match if we are before the beginning of the
203     "      pair.
204     "
205     if   0 < searchpair(js, '', '</script>', 'nWb')
206     \ && 0 < searchpair(js, '', '</script>', 'nW')
207         " we're inside javascript
208         if getline(lnum) !~ js && getline(a:lnum) != '</script>'
209             if restore_ic == 0
210               setlocal noic
211             endif
212             return cindent(a:lnum)
213         endif
214     endif
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
229     let ind = <SID>HtmlIndentSum(lnum, -1)
230     let ind = ind + <SID>HtmlIndentSum(a:lnum, 0)
232     if restore_ic == 0
233         setlocal noic
234     endif
236     return indent(lnum) + (&sw * ind)
237 endfun
239 let &cpo = s:cpo_save
240 unlet s:cpo_save
242 " [-- EOF <runtime>/indent/html.vim --]