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