|
| 1 | + |
| 2 | +" Description: html indenter |
| 3 | +" Author: Johannes Zellner <johannes@zellner.org> |
| 4 | +" Last Change: Mo, 05 Jun 2006 22:32:41 CEST |
| 5 | +" Restoring 'cpo' and 'ic' added by Bram 2006 May 5 |
| 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 | +if exists("g:js_indent") |
| 17 | + so g:js_indent |
| 18 | +else |
| 19 | + ru! indent/javascript.vim |
| 20 | +endif |
| 21 | + |
| 22 | +echo "Sourcing html indent" |
| 23 | + |
| 24 | + |
| 25 | +" [-- local settings (must come before aborting the script) --] |
| 26 | +setlocal indentexpr=HtmlIndentGetter(v:lnum) |
| 27 | +setlocal indentkeys=o,O,*<Return>,<>>,{,} |
| 28 | + |
| 29 | + |
| 30 | +if exists('g:html_indent_tags') |
| 31 | + unlet g:html_indent_tags |
| 32 | +endif |
| 33 | + |
| 34 | +" [-- helper function to assemble tag list --] |
| 35 | +fun! <SID>HtmlIndentPush(tag) |
| 36 | + if exists('g:html_indent_tags') |
| 37 | + let g:html_indent_tags = g:html_indent_tags.'\|'.a:tag |
| 38 | + else |
| 39 | + let g:html_indent_tags = a:tag |
| 40 | + endif |
| 41 | +endfun |
| 42 | + |
| 43 | + |
| 44 | +" [-- <ELEMENT ? - - ...> --] |
| 45 | +call <SID>HtmlIndentPush('a') |
| 46 | +call <SID>HtmlIndentPush('abbr') |
| 47 | +call <SID>HtmlIndentPush('acronym') |
| 48 | +call <SID>HtmlIndentPush('address') |
| 49 | +call <SID>HtmlIndentPush('b') |
| 50 | +call <SID>HtmlIndentPush('bdo') |
| 51 | +call <SID>HtmlIndentPush('big') |
| 52 | +call <SID>HtmlIndentPush('blockquote') |
| 53 | +call <SID>HtmlIndentPush('button') |
| 54 | +call <SID>HtmlIndentPush('caption') |
| 55 | +call <SID>HtmlIndentPush('center') |
| 56 | +call <SID>HtmlIndentPush('cite') |
| 57 | +call <SID>HtmlIndentPush('code') |
| 58 | +call <SID>HtmlIndentPush('colgroup') |
| 59 | +call <SID>HtmlIndentPush('del') |
| 60 | +call <SID>HtmlIndentPush('dfn') |
| 61 | +call <SID>HtmlIndentPush('dir') |
| 62 | +call <SID>HtmlIndentPush('div') |
| 63 | +call <SID>HtmlIndentPush('dl') |
| 64 | +call <SID>HtmlIndentPush('em') |
| 65 | +call <SID>HtmlIndentPush('fieldset') |
| 66 | +call <SID>HtmlIndentPush('font') |
| 67 | +call <SID>HtmlIndentPush('form') |
| 68 | +call <SID>HtmlIndentPush('frameset') |
| 69 | +call <SID>HtmlIndentPush('h1') |
| 70 | +call <SID>HtmlIndentPush('h2') |
| 71 | +call <SID>HtmlIndentPush('h3') |
| 72 | +call <SID>HtmlIndentPush('h4') |
| 73 | +call <SID>HtmlIndentPush('h5') |
| 74 | +call <SID>HtmlIndentPush('h6') |
| 75 | +call <SID>HtmlIndentPush('i') |
| 76 | +call <SID>HtmlIndentPush('iframe') |
| 77 | +call <SID>HtmlIndentPush('ins') |
| 78 | +call <SID>HtmlIndentPush('kbd') |
| 79 | +call <SID>HtmlIndentPush('label') |
| 80 | +call <SID>HtmlIndentPush('legend') |
| 81 | +call <SID>HtmlIndentPush('map') |
| 82 | +call <SID>HtmlIndentPush('menu') |
| 83 | +call <SID>HtmlIndentPush('noframes') |
| 84 | +call <SID>HtmlIndentPush('noscript') |
| 85 | +call <SID>HtmlIndentPush('object') |
| 86 | +call <SID>HtmlIndentPush('ol') |
| 87 | +call <SID>HtmlIndentPush('optgroup') |
| 88 | +" call <SID>HtmlIndentPush('pre') |
| 89 | +call <SID>HtmlIndentPush('q') |
| 90 | +call <SID>HtmlIndentPush('s') |
| 91 | +call <SID>HtmlIndentPush('samp') |
| 92 | +call <SID>HtmlIndentPush('script') |
| 93 | +call <SID>HtmlIndentPush('select') |
| 94 | +call <SID>HtmlIndentPush('small') |
| 95 | +call <SID>HtmlIndentPush('span') |
| 96 | +call <SID>HtmlIndentPush('strong') |
| 97 | +call <SID>HtmlIndentPush('style') |
| 98 | +call <SID>HtmlIndentPush('sub') |
| 99 | +call <SID>HtmlIndentPush('sup') |
| 100 | +call <SID>HtmlIndentPush('table') |
| 101 | +call <SID>HtmlIndentPush('textarea') |
| 102 | +call <SID>HtmlIndentPush('title') |
| 103 | +call <SID>HtmlIndentPush('tt') |
| 104 | +call <SID>HtmlIndentPush('u') |
| 105 | +call <SID>HtmlIndentPush('ul') |
| 106 | +call <SID>HtmlIndentPush('var') |
| 107 | + |
| 108 | + |
| 109 | +" [-- <ELEMENT ? O O ...> --] |
| 110 | +if !exists('g:html_indent_strict') |
| 111 | + call <SID>HtmlIndentPush('body') |
| 112 | + call <SID>HtmlIndentPush('head') |
| 113 | + call <SID>HtmlIndentPush('html') |
| 114 | + call <SID>HtmlIndentPush('tbody') |
| 115 | +endif |
| 116 | + |
| 117 | + |
| 118 | +" [-- <ELEMENT ? O - ...> --] |
| 119 | +if !exists('g:html_indent_strict_table') |
| 120 | + call <SID>HtmlIndentPush('th') |
| 121 | + call <SID>HtmlIndentPush('td') |
| 122 | + call <SID>HtmlIndentPush('tr') |
| 123 | + call <SID>HtmlIndentPush('tfoot') |
| 124 | + call <SID>HtmlIndentPush('thead') |
| 125 | +endif |
| 126 | + |
| 127 | +delfun <SID>HtmlIndentPush |
| 128 | + |
| 129 | +let s:cpo_save = &cpo |
| 130 | +set cpo-=C |
| 131 | + |
| 132 | +" [-- count indent-increasing tags of line a:lnum --] |
| 133 | +fun! <SID>HtmlIndentOpen(lnum, pattern) |
| 134 | + let s = substitute('x'.getline(a:lnum), |
| 135 | + \ '.\{-}\(\(<\)\('.a:pattern.'\)\>\)', "\1", 'g') |
| 136 | + let s = substitute(s, "[^\1].*$", '', '') |
| 137 | + return strlen(s) |
| 138 | +endfun |
| 139 | + |
| 140 | +" [-- count indent-decreasing tags of line a:lnum --] |
| 141 | +fun! <SID>HtmlIndentClose(lnum, pattern) |
| 142 | + let s = substitute('x'.getline(a:lnum), |
| 143 | + \ '.\{-}\(\(<\)/\('.a:pattern.'\)\>>\)', "\1", 'g') |
| 144 | + let s = substitute(s, "[^\1].*$", '', '') |
| 145 | + return strlen(s) |
| 146 | +endfun |
| 147 | + |
| 148 | +" [-- count indent-increasing '{' of (java|css) line a:lnum --] |
| 149 | +fun! <SID>HtmlIndentOpenAlt(lnum) |
| 150 | + return strlen(substitute(getline(a:lnum), '[^{]\+', '', 'g')) |
| 151 | +endfun |
| 152 | + |
| 153 | +" [-- count indent-decreasing '}' of (java|css) line a:lnum --] |
| 154 | +fun! <SID>HtmlIndentCloseAlt(lnum) |
| 155 | + return strlen(substitute(getline(a:lnum), '[^}]\+', '', 'g')) |
| 156 | +endfun |
| 157 | + |
| 158 | +" [-- return the sum of indents respecting the syntax of a:lnum --] |
| 159 | +fun! <SID>HtmlIndentSum(lnum, style) |
| 160 | + if a:style == match(getline(a:lnum), '^\s*</') |
| 161 | + if a:style == match(getline(a:lnum), '^\s*</\<\('.g:html_indent_tags.'\)\>') |
| 162 | + let open = <SID>HtmlIndentOpen(a:lnum, g:html_indent_tags) |
| 163 | + let close = <SID>HtmlIndentClose(a:lnum, g:html_indent_tags) |
| 164 | + if 0 != open || 0 != close |
| 165 | + return open - close |
| 166 | + endif |
| 167 | + endif |
| 168 | + endif |
| 169 | + if '' != &syntax && |
| 170 | + \ synIDattr(synID(a:lnum, 1, 1), 'name') =~ '\(css\|java\).*' && |
| 171 | + \ synIDattr(synID(a:lnum, strlen(getline(a:lnum)), 1), 'name') |
| 172 | + \ =~ '\(css\|java\).*' |
| 173 | + if a:style == match(getline(a:lnum), '^\s*}') |
| 174 | + return <SID>HtmlIndentOpenAlt(a:lnum) - <SID>HtmlIndentCloseAlt(a:lnum) |
| 175 | + endif |
| 176 | + endif |
| 177 | + return 0 |
| 178 | +endfun |
| 179 | + |
| 180 | +fun! HtmlIndentGetter(lnum) |
| 181 | + |
| 182 | + echo "Grabbing html indent for line: " . a: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.*type\s*=.*javascript' |
| 207 | + |
| 208 | + """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" |
| 209 | + " by Tye Zdrojewski <zdro@yahoo.com>, 05 Jun 2006 |
| 210 | + " ZDR: This needs to be an AND (we are 'after the start of the pair' AND |
| 211 | + " we are 'before the end of the pair'). Otherwise, indentation |
| 212 | + " before the start of the script block will be affected; the end of |
| 213 | + " the pair will still match if we are before the beginning of the |
| 214 | + " pair. |
| 215 | + " |
| 216 | + if 0 < searchpair(js, '', '</script>', 'nWb') |
| 217 | + \ && 0 < searchpair(js, '', '</script>', 'nW') |
| 218 | + " we're inside javascript |
| 219 | + |
| 220 | + if getline(lnum) !~ js && getline(a:lnum) !~ '</script>' |
| 221 | + if restore_ic == 0 |
| 222 | + setlocal noic |
| 223 | + endif |
| 224 | + return GetJsIndent(a:lnum) |
| 225 | + endif |
| 226 | + endif |
| 227 | + |
| 228 | + if getline(lnum) =~ '\c</pre>' |
| 229 | + " line before the current line a:lnum contains |
| 230 | + " a closing </pre>. --> search for line before |
| 231 | + " starting <pre> to restore the indent. |
| 232 | + let preline = prevnonblank(search('\c<pre>', 'bW') - 1) |
| 233 | + if preline > 0 |
| 234 | + if restore_ic == 0 |
| 235 | + setlocal noic |
| 236 | + endif |
| 237 | + return indent(preline) |
| 238 | + endif |
| 239 | + endif |
| 240 | + |
| 241 | + let ind = <SID>HtmlIndentSum(lnum, -1) |
| 242 | + let ind = ind + <SID>HtmlIndentSum(a:lnum, 0) |
| 243 | + |
| 244 | + if restore_ic == 0 |
| 245 | + setlocal noic |
| 246 | + endif |
| 247 | + |
| 248 | + return indent(lnum) + (&sw * ind) |
| 249 | +endfun |
| 250 | + |
| 251 | +let &cpo = s:cpo_save |
| 252 | +unlet s:cpo_save |
| 253 | + |
| 254 | +" [-- EOF <runtime>/indent/html.vim --] |
0 commit comments