Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 1 | " Vim indent script for HTML |
| 2 | " General: "{{{ |
| 3 | " File: html.vim (Vimscript #2075) |
| 4 | " Author: Andy Wokula <anwoku@yahoo.de> |
Bram Moolenaar | 946e27a | 2014-06-25 18:50:27 +0200 | [diff] [blame] | 5 | " Last Change: 2014 Jun 19 |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 6 | " Rev Days: 13 |
| 7 | " Version: 0.9 |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 8 | " Vim Version: Vim7 |
| 9 | " Description: |
| 10 | " Improved version of the distributed html indent script, faster on a |
| 11 | " range of lines. |
| 12 | " |
| 13 | " Credits: |
| 14 | " indent/html.vim (2006 Jun 05) from J. Zellner |
| 15 | " indent/css.vim (2006 Dec 20) from N. Weibull |
| 16 | " |
| 17 | " History: |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 18 | " 2012 Oct 21 (v0.9) added support for shiftwidth() |
| 19 | " 2011 Sep 09 (v0.8) added HTML5 tags (thx to J. Zuckerman) |
| 20 | " 2008 Apr 28 (v0.6) revised customization |
| 21 | " 2008 Mar 09 (v0.5) fixed 'indk' issue (thx to C.J. Robinson) |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 22 | " }}} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 23 | |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 24 | " Init Folklore, check user settings (2nd time ++) "{{{ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 25 | if exists("b:did_indent") |
| 26 | finish |
| 27 | endif |
| 28 | let b:did_indent = 1 |
| 29 | |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 30 | setlocal indentexpr=HtmlIndent() |
| 31 | setlocal indentkeys=o,O,<Return>,<>>,{,},!^F |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 32 | |
Bram Moolenaar | 946e27a | 2014-06-25 18:50:27 +0200 | [diff] [blame] | 33 | " Needed for % to work when finding start of a tag. |
| 34 | setlocal matchpairs+=<:> |
| 35 | |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 36 | let b:indent = {"lnum": -1} |
| 37 | let b:undo_indent = "set inde< indk<| unlet b:indent" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 38 | |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 39 | " Load Once: |
| 40 | if exists("*HtmlIndent") |
| 41 | call HtmlIndent_CheckUserSettings() |
| 42 | finish |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 43 | endif |
| 44 | |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 45 | " Patch 7.3.694 |
| 46 | if exists('*shiftwidth') |
| 47 | let s:ShiftWidth = function('shiftwidth') |
| 48 | else |
| 49 | func! s:ShiftWidth() |
| 50 | return &shiftwidth |
| 51 | endfunc |
| 52 | endif |
| 53 | |
Bram Moolenaar | 91170f8 | 2006-05-05 21:15:17 +0000 | [diff] [blame] | 54 | let s:cpo_save = &cpo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 55 | set cpo-=C |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 56 | "}}} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 57 | |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 58 | func! HtmlIndent_CheckUserSettings() "{{{ |
| 59 | if exists("g:html_indent_inctags") |
| 60 | call s:AddITags(split(g:html_indent_inctags, ",")) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 61 | endif |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 62 | if exists("g:html_indent_autotags") |
| 63 | call s:RemoveITags(split(g:html_indent_autotags, ",")) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 64 | endif |
| 65 | |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 66 | let indone = {"zero": 0 |
| 67 | \,"auto": "indent(prevnonblank(v:lnum-1))" |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 68 | \,"inc": "b:indent.blocktagind + s:ShiftWidth()"} |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 69 | if exists("g:html_indent_script1") |
| 70 | let s:js1indent = get(indone, g:html_indent_script1, indone.zero) |
| 71 | endif |
| 72 | if exists("g:html_indent_style1") |
| 73 | let s:css1indent = get(indone, g:html_indent_style1, indone.zero) |
| 74 | endif |
| 75 | endfunc "}}} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 76 | |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 77 | " Init Script Vars "{{{ |
Bram Moolenaar | 946e27a | 2014-06-25 18:50:27 +0200 | [diff] [blame] | 78 | let s:lasttick = 0 |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 79 | let s:css1indent = 0 |
| 80 | let s:js1indent = 0 |
| 81 | " not to be changed: |
| 82 | let s:endtags = [0,0,0,0,0,0,0,0] " some places unused |
| 83 | let s:newstate = {} |
| 84 | let s:countonly = 0 |
| 85 | "}}} |
| 86 | func! s:AddITags(taglist) "{{{ |
| 87 | for itag in a:taglist |
| 88 | let s:indent_tags[itag] = 1 |
| 89 | let s:indent_tags['/'.itag] = -1 |
| 90 | endfor |
| 91 | endfunc "}}} |
| 92 | func! s:AddBlockTag(tag, id, ...) "{{{ |
| 93 | if !(a:id >= 2 && a:id < 2+len(s:endtags)) |
| 94 | return |
| 95 | endif |
| 96 | let s:indent_tags[a:tag] = a:id |
| 97 | if a:0 == 0 |
| 98 | let s:indent_tags['/'.a:tag] = -a:id |
| 99 | let s:endtags[a:id-2] = "</".a:tag.">" |
| 100 | else |
| 101 | let s:indent_tags[a:1] = -a:id |
| 102 | let s:endtags[a:id-2] = a:1 |
| 103 | endif |
| 104 | endfunc "}}} |
| 105 | func! s:RemoveITags(taglist) "{{{ |
| 106 | " remove itags (protect blocktags from being removed) |
| 107 | for itag in a:taglist |
| 108 | if !has_key(s:indent_tags, itag) || s:indent_tags[itag] != 1 |
| 109 | continue |
Bram Moolenaar | 91170f8 | 2006-05-05 21:15:17 +0000 | [diff] [blame] | 110 | endif |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 111 | unlet s:indent_tags[itag] |
| 112 | if itag =~ '^\w\+$' |
| 113 | unlet s:indent_tags["/".itag] |
| 114 | endif |
| 115 | endfor |
| 116 | endfunc "}}} |
| 117 | " Add Indent Tags: {{{ |
| 118 | if !exists("s:indent_tags") |
| 119 | let s:indent_tags = {} |
| 120 | endif |
| 121 | |
| 122 | " old tags: |
| 123 | call s:AddITags(['a', 'abbr', 'acronym', 'address', 'b', 'bdo', 'big', |
| 124 | \ 'blockquote', 'button', 'caption', 'center', 'cite', 'code', 'colgroup', |
| 125 | \ 'del', 'dfn', 'dir', 'div', 'dl', 'em', 'fieldset', 'font', 'form', |
| 126 | \ 'frameset', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'iframe', 'ins', 'kbd', |
| 127 | \ 'label', 'legend', 'map', 'menu', 'noframes', 'noscript', 'object', 'ol', |
| 128 | \ 'optgroup', 'q', 's', 'samp', 'select', 'small', 'span', 'strong', 'sub', |
| 129 | \ 'sup', 'table', 'textarea', 'title', 'tt', 'u', 'ul', 'var', 'th', 'td', |
| 130 | \ 'tr', 'tfoot', 'thead']) |
| 131 | |
| 132 | " tags added 2011 Sep 09 (especially HTML5 tags): |
| 133 | call s:AddITags(['area', 'article', 'aside', 'audio', 'bdi', 'canvas', |
| 134 | \ 'command', 'datalist', 'details', 'embed', 'figure', 'footer', |
| 135 | \ 'header', 'group', 'keygen', 'mark', 'math', 'meter', 'nav', 'output', |
| 136 | \ 'progress', 'ruby', 'section', 'svg', 'texture', 'time', 'video', |
| 137 | \ 'wbr', 'text']) |
| 138 | |
| 139 | "}}} |
| 140 | " Add Block Tags: contain alien content "{{{ |
| 141 | call s:AddBlockTag('pre', 2) |
| 142 | call s:AddBlockTag('script', 3) |
| 143 | call s:AddBlockTag('style', 4) |
| 144 | call s:AddBlockTag('<!--', 5, '-->') |
| 145 | "}}} |
| 146 | |
| 147 | func! s:CountITags(...) "{{{ |
| 148 | |
| 149 | " relative indent steps for current line [unit &sw]: |
| 150 | let s:curind = 0 |
| 151 | " relative indent steps for next line [unit &sw]: |
| 152 | let s:nextrel = 0 |
| 153 | |
| 154 | if a:0==0 |
| 155 | let s:block = s:newstate.block |
Bram Moolenaar | 946e27a | 2014-06-25 18:50:27 +0200 | [diff] [blame] | 156 | let tmpline = substitute(s:curline, '<\zs/\=\w\+\>\|<!--\|-->', '\=s:CheckTag(submatch(0))', 'g') |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 157 | if s:block == 3 |
| 158 | let s:newstate.scripttype = s:GetScriptType(matchstr(tmpline, '\C.*<SCRIPT\>\zs[^>]*')) |
| 159 | endif |
| 160 | let s:newstate.block = s:block |
| 161 | else |
| 162 | let s:block = 0 " assume starting outside of a block |
| 163 | let s:countonly = 1 " don't change state |
Bram Moolenaar | 946e27a | 2014-06-25 18:50:27 +0200 | [diff] [blame] | 164 | let tmpline = substitute(s:altline, '<\zs/\=\w\+\>\|<!--\|-->', '\=s:CheckTag(submatch(0))', 'g') |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 165 | let s:countonly = 0 |
| 166 | endif |
| 167 | endfunc "}}} |
| 168 | func! s:CheckTag(itag) "{{{ |
| 169 | " "tag" or "/tag" or "<!--" or "-->" |
| 170 | let ind = get(s:indent_tags, a:itag) |
| 171 | if ind == -1 |
| 172 | " closing tag |
| 173 | if s:block != 0 |
| 174 | " ignore itag within a block |
| 175 | return "foo" |
| 176 | endif |
| 177 | if s:nextrel == 0 |
| 178 | let s:curind -= 1 |
| 179 | else |
| 180 | let s:nextrel -= 1 |
| 181 | endif |
| 182 | " if s:curind >= 1 |
| 183 | " let s:curind -= 1 |
| 184 | " else |
| 185 | " let s:nextrel -= 1 |
| 186 | " endif |
| 187 | elseif ind == 1 |
| 188 | " opening tag |
| 189 | if s:block != 0 |
| 190 | return "foo" |
| 191 | endif |
| 192 | let s:nextrel += 1 |
| 193 | elseif ind != 0 |
| 194 | " block-tag (opening or closing) |
| 195 | return s:Blocktag(a:itag, ind) |
| 196 | endif |
| 197 | " else ind==0 (other tag found): keep indent |
| 198 | return "foo" " no matter |
| 199 | endfunc "}}} |
| 200 | func! s:Blocktag(blocktag, ind) "{{{ |
| 201 | if a:ind > 0 |
| 202 | " a block starts here |
| 203 | if s:block != 0 |
| 204 | " already in a block (nesting) - ignore |
| 205 | " especially ignore comments after other blocktags |
| 206 | return "foo" |
| 207 | endif |
| 208 | let s:block = a:ind " block type |
| 209 | if s:countonly |
| 210 | return "foo" |
| 211 | endif |
| 212 | let s:newstate.blocklnr = v:lnum |
| 213 | " save allover indent for the endtag |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 214 | let s:newstate.blocktagind = b:indent.baseindent + (s:nextrel + s:curind) * s:ShiftWidth() |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 215 | if a:ind == 3 |
| 216 | return "SCRIPT" " all except this must be lowercase |
| 217 | " line is to be checked again for the type attribute |
| 218 | endif |
| 219 | else |
| 220 | let s:block = 0 |
| 221 | " we get here if starting and closing block-tag on same line |
| 222 | endif |
| 223 | return "foo" |
| 224 | endfunc "}}} |
| 225 | func! s:GetScriptType(str) "{{{ |
| 226 | if a:str == "" || a:str =~ "java" |
| 227 | return "javascript" |
| 228 | else |
| 229 | return "" |
| 230 | endif |
| 231 | endfunc "}}} |
| 232 | |
| 233 | func! s:FreshState(lnum) "{{{ |
| 234 | " Look back in the file (lines 1 to a:lnum-1) to calc a state for line |
| 235 | " a:lnum. A state is to know ALL relevant details about the lines |
| 236 | " 1..a:lnum-1, initial calculating (here!) can be slow, but updating is |
| 237 | " fast (incremental). |
| 238 | " State: |
| 239 | " lnum last indented line == prevnonblank(a:lnum - 1) |
| 240 | " block = 0 a:lnum located within special tag: 0:none, 2:<pre>, |
| 241 | " 3:<script>, 4:<style>, 5:<!-- |
| 242 | " baseindent use this indent for line a:lnum as a start - kind of |
| 243 | " autoindent (if block==0) |
| 244 | " scripttype = '' type attribute of a script tag (if block==3) |
| 245 | " blocktagind indent for current opening (get) and closing (set) |
| 246 | " blocktag (if block!=0) |
| 247 | " blocklnr lnum of starting blocktag (if block!=0) |
| 248 | " inattr line {lnum} starts with attributes of a tag |
| 249 | let state = {} |
| 250 | let state.lnum = prevnonblank(a:lnum - 1) |
| 251 | let state.scripttype = "" |
| 252 | let state.blocktagind = -1 |
| 253 | let state.block = 0 |
| 254 | let state.baseindent = 0 |
| 255 | let state.blocklnr = 0 |
| 256 | let state.inattr = 0 |
| 257 | |
| 258 | if state.lnum == 0 |
| 259 | return state |
| 260 | endif |
| 261 | |
| 262 | " Heuristic: |
| 263 | " remember startline state.lnum |
| 264 | " look back for <pre, </pre, <script, </script, <style, </style tags |
| 265 | " remember stopline |
| 266 | " if opening tag found, |
| 267 | " assume a:lnum within block |
| 268 | " else |
| 269 | " look back in result range (stopline, startline) for comment |
| 270 | " \ delimiters (<!--, -->) |
| 271 | " if comment opener found, |
| 272 | " assume a:lnum within comment |
| 273 | " else |
| 274 | " assume usual html for a:lnum |
| 275 | " if a:lnum-1 has a closing comment |
| 276 | " look back to get indent of comment opener |
| 277 | " FI |
| 278 | |
| 279 | " look back for blocktag |
| 280 | call cursor(a:lnum, 1) |
| 281 | let [stopline, stopcol] = searchpos('\c<\zs\/\=\%(pre\>\|script\>\|style\>\)', "bW") |
| 282 | " fugly ... why isn't there searchstr() |
| 283 | let tagline = tolower(getline(stopline)) |
| 284 | let blocktag = matchstr(tagline, '\/\=\%(pre\>\|script\>\|style\>\)', stopcol-1) |
| 285 | if stopline > 0 && blocktag[0] != "/" |
| 286 | " opening tag found, assume a:lnum within block |
| 287 | let state.block = s:indent_tags[blocktag] |
| 288 | if state.block == 3 |
| 289 | let state.scripttype = s:GetScriptType(matchstr(tagline, '\>[^>]*', stopcol)) |
| 290 | endif |
| 291 | let state.blocklnr = stopline |
| 292 | " check preceding tags in the line: |
| 293 | let s:altline = tagline[: stopcol-2] |
| 294 | call s:CountITags(1) |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 295 | let state.blocktagind = indent(stopline) + (s:curind + s:nextrel) * s:ShiftWidth() |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 296 | return state |
| 297 | elseif stopline == state.lnum |
| 298 | " handle special case: previous line (= state.lnum) contains a |
| 299 | " closing blocktag which is preceded by line-noise; |
| 300 | " blocktag == "/..." |
| 301 | let swendtag = match(tagline, '^\s*</') >= 0 |
| 302 | if !swendtag |
| 303 | let [bline, bcol] = searchpos('<'.blocktag[1:].'\>', "bW") |
| 304 | let s:altline = tolower(getline(bline)[: bcol-2]) |
| 305 | call s:CountITags(1) |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 306 | let state.baseindent = indent(bline) + (s:nextrel+s:curline) * s:ShiftWidth() |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 307 | return state |
| 308 | endif |
| 309 | endif |
| 310 | |
| 311 | " else look back for comment |
| 312 | call cursor(a:lnum, 1) |
| 313 | let [comline, comcol, found] = searchpos('\(<!--\)\|-->', 'bpW', stopline) |
| 314 | if found == 2 |
| 315 | " comment opener found, assume a:lnum within comment |
| 316 | let state.block = 5 |
| 317 | let state.blocklnr = comline |
| 318 | " check preceding tags in the line: |
| 319 | let s:altline = tolower(getline(comline)[: comcol-2]) |
| 320 | call s:CountITags(1) |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 321 | let state.blocktagind = indent(comline) + (s:curind + s:nextrel) * s:ShiftWidth() |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 322 | return state |
| 323 | endif |
| 324 | |
| 325 | " else within usual html |
| 326 | let s:altline = tolower(getline(state.lnum)) |
Bram Moolenaar | 946e27a | 2014-06-25 18:50:27 +0200 | [diff] [blame] | 327 | |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 328 | " check a:lnum-1 for closing comment (we need indent from the opening line) |
| 329 | let comcol = stridx(s:altline, '-->') |
| 330 | if comcol >= 0 |
| 331 | call cursor(state.lnum, comcol+1) |
| 332 | let [comline, comcol] = searchpos('<!--', 'bW') |
| 333 | if comline == state.lnum |
| 334 | let s:altline = s:altline[: comcol-2] |
| 335 | else |
| 336 | let s:altline = tolower(getline(comline)[: comcol-2]) |
| 337 | endif |
| 338 | call s:CountITags(1) |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 339 | let state.baseindent = indent(comline) + (s:nextrel+s:curline) * s:ShiftWidth() |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 340 | return state |
| 341 | " TODO check tags that follow "-->" |
| 342 | endif |
| 343 | |
Bram Moolenaar | 946e27a | 2014-06-25 18:50:27 +0200 | [diff] [blame] | 344 | " Check if the previous line starts with end tag. |
| 345 | let swendtag = match(s:altline, '^\s*</') >= 0 |
| 346 | |
| 347 | " If previous line ended in a closing tag, line up with the opening tag. |
| 348 | " Avoids aligning with continuation lines. |
| 349 | " TODO: this assumes the start tag is at the start of a line. |
| 350 | if !swendtag && s:altline =~ '</\w\+\s*>\s*$' |
| 351 | call cursor(state.lnum, 99999) |
| 352 | normal F<h |
| 353 | " TODO: doesn't work for nested <br> and the like |
| 354 | let slnum = searchpair('<\w\+', '', '</\w', 'bW') |
| 355 | if slnum > 0 |
| 356 | let state.baseindent = indent(slnum) |
| 357 | return state |
| 358 | endif |
| 359 | endif |
| 360 | |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 361 | " else no comments |
Bram Moolenaar | 946e27a | 2014-06-25 18:50:27 +0200 | [diff] [blame] | 362 | let state.lnum = s:FindTagStart(state.lnum) |
| 363 | let s:altline = tolower(getline(state.lnum)) |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 364 | call s:CountITags(1) |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 365 | let state.baseindent = indent(state.lnum) + s:nextrel * s:ShiftWidth() |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 366 | if !swendtag |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 367 | let state.baseindent += s:curind * s:ShiftWidth() |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 368 | endif |
| 369 | return state |
| 370 | endfunc "}}} |
| 371 | |
| 372 | func! s:Alien2() "{{{ |
| 373 | " <pre> block |
| 374 | return -1 |
| 375 | endfunc "}}} |
| 376 | func! s:Alien3() "{{{ |
| 377 | " <script> javascript |
| 378 | if prevnonblank(v:lnum-1) == b:indent.blocklnr |
| 379 | " indent for the first line after <script> |
| 380 | return eval(s:js1indent) |
| 381 | endif |
| 382 | if b:indent.scripttype == "javascript" |
| 383 | return cindent(v:lnum) |
| 384 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 385 | return -1 |
| 386 | endif |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 387 | endfunc "}}} |
| 388 | func! s:Alien4() "{{{ |
| 389 | " <style> |
| 390 | if prevnonblank(v:lnum-1) == b:indent.blocklnr |
| 391 | " indent for first content line |
| 392 | return eval(s:css1indent) |
| 393 | endif |
| 394 | return s:CSSIndent() |
| 395 | endfunc |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 396 | |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 397 | func! s:CSSIndent() "{{{ |
| 398 | " adopted $VIMRUNTIME/indent/css.vim |
| 399 | if getline(v:lnum) =~ '^\s*[*}]' |
| 400 | return cindent(v:lnum) |
| 401 | endif |
| 402 | let minline = b:indent.blocklnr |
| 403 | let pnum = s:css_prevnoncomment(v:lnum - 1, minline) |
Bram Moolenaar | 946e27a | 2014-06-25 18:50:27 +0200 | [diff] [blame] | 404 | let pnum = s:FindTagStart(pnum) |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 405 | if pnum <= minline |
| 406 | " < is to catch errors |
| 407 | " indent for first content line after comments |
| 408 | return eval(s:css1indent) |
| 409 | endif |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 410 | let ind = indent(pnum) + s:css_countbraces(pnum, 1) * s:ShiftWidth() |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 411 | let pline = getline(pnum) |
| 412 | if pline =~ '}\s*$' |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 413 | let ind -= (s:css_countbraces(pnum, 0) - (pline =~ '^\s*}')) * s:ShiftWidth() |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 414 | endif |
| 415 | return ind |
| 416 | endfunc "}}} |
| 417 | func! s:css_prevnoncomment(lnum, stopline) "{{{ |
| 418 | " caller starts from a line a:lnum-1 that is not a comment |
| 419 | let lnum = prevnonblank(a:lnum) |
| 420 | let ccol = match(getline(lnum), '\*/') |
| 421 | if ccol < 0 |
| 422 | return lnum |
| 423 | endif |
| 424 | call cursor(lnum, ccol+1) |
| 425 | let lnum = search('/\*', 'bW', a:stopline) |
| 426 | if indent(".") == virtcol(".")-1 |
| 427 | return prevnonblank(lnum-1) |
| 428 | else |
| 429 | return lnum |
| 430 | endif |
| 431 | endfunc "}}} |
| 432 | func! s:css_countbraces(lnum, count_open) "{{{ |
| 433 | let brs = substitute(getline(a:lnum),'[''"].\{-}[''"]\|/\*.\{-}\*/\|/\*.*$\|[^{}]','','g') |
| 434 | let n_open = 0 |
| 435 | let n_close = 0 |
| 436 | for brace in split(brs, '\zs') |
| 437 | if brace == "{" |
| 438 | let n_open += 1 |
| 439 | elseif brace == "}" |
| 440 | if n_open > 0 |
| 441 | let n_open -= 1 |
| 442 | else |
| 443 | let n_close += 1 |
Bram Moolenaar | 91170f8 | 2006-05-05 21:15:17 +0000 | [diff] [blame] | 444 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 445 | endif |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 446 | endfor |
| 447 | return a:count_open ? n_open : n_close |
| 448 | endfunc "}}} |
| 449 | |
| 450 | "}}} |
| 451 | func! s:Alien5() "{{{ |
| 452 | " <!-- --> |
| 453 | return -1 |
| 454 | endfunc "}}} |
| 455 | |
Bram Moolenaar | 946e27a | 2014-06-25 18:50:27 +0200 | [diff] [blame] | 456 | " When the "lnum" line ends in ">" find the line containing the |
| 457 | " matching "<". Avoids using the indent of a continuation line. |
| 458 | " Moves the cursor. |
| 459 | " Return the matching line number or "lnum". |
| 460 | func! s:FindTagStart(lnum) "{{{ |
| 461 | if getline(a:lnum) =~ '>\s*$' |
| 462 | call cursor(a:lnum, 99999) |
| 463 | normal! % |
| 464 | return line('.') |
| 465 | endif |
| 466 | return a:lnum |
| 467 | endfunc "}}} |
| 468 | |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 469 | func! HtmlIndent() "{{{ |
| 470 | let s:curline = tolower(getline(v:lnum)) |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 471 | let indentunit = s:ShiftWidth() |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 472 | |
| 473 | let s:newstate = {} |
| 474 | let s:newstate.lnum = v:lnum |
| 475 | |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 476 | " does the line start with a closing tag? |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 477 | let swendtag = match(s:curline, '^\s*</') >= 0 |
| 478 | |
Bram Moolenaar | 946e27a | 2014-06-25 18:50:27 +0200 | [diff] [blame] | 479 | if prevnonblank(v:lnum-1) == b:indent.lnum && s:lasttick == b:changedtick - 1 |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 480 | " use state (continue from previous line) |
| 481 | else |
| 482 | " start over (know nothing) |
| 483 | let b:indent = s:FreshState(v:lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 484 | endif |
| 485 | |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 486 | if b:indent.block >= 2 |
| 487 | " within block |
| 488 | let endtag = s:endtags[b:indent.block-2] |
| 489 | let blockend = stridx(s:curline, endtag) |
| 490 | if blockend >= 0 |
| 491 | " block ends here |
| 492 | let s:newstate.block = 0 |
| 493 | " calc indent for REST OF LINE (may start more blocks): |
| 494 | let s:curline = strpart(s:curline, blockend+strlen(endtag)) |
| 495 | call s:CountITags() |
| 496 | if swendtag && b:indent.block != 5 |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 497 | let indent = b:indent.blocktagind + s:curind * indentunit |
| 498 | let s:newstate.baseindent = indent + s:nextrel * indentunit |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 499 | else |
| 500 | let indent = s:Alien{b:indent.block}() |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 501 | let s:newstate.baseindent = b:indent.blocktagind + s:nextrel * indentunit |
Bram Moolenaar | 91170f8 | 2006-05-05 21:15:17 +0000 | [diff] [blame] | 502 | endif |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 503 | else |
| 504 | " block continues |
| 505 | " indent this line with alien method |
| 506 | let indent = s:Alien{b:indent.block}() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 507 | endif |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 508 | else |
| 509 | " not within a block - within usual html |
| 510 | " if < 2 then always 0 |
| 511 | let s:newstate.block = b:indent.block |
| 512 | call s:CountITags() |
| 513 | if swendtag |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 514 | let indent = b:indent.baseindent + s:curind * indentunit |
| 515 | let s:newstate.baseindent = indent + s:nextrel * indentunit |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 516 | else |
| 517 | let indent = b:indent.baseindent |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 518 | let s:newstate.baseindent = indent + (s:curind + s:nextrel) * indentunit |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 519 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 520 | endif |
Bram Moolenaar | 946e27a | 2014-06-25 18:50:27 +0200 | [diff] [blame] | 521 | let s:lasttick = b:changedtick |
| 522 | call extend(b:indent, s:newstate, "force") |
| 523 | return indent |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 524 | |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 525 | endfunc "}}} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 526 | |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 527 | " check user settings (first time), clear cpo, Modeline: {{{1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 528 | |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 529 | " DEBUG: |
| 530 | com! -nargs=* IndHtmlLocal <args> |
| 531 | |
| 532 | call HtmlIndent_CheckUserSettings() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 533 | |
Bram Moolenaar | 91170f8 | 2006-05-05 21:15:17 +0000 | [diff] [blame] | 534 | let &cpo = s:cpo_save |
| 535 | unlet s:cpo_save |
| 536 | |
Bram Moolenaar | ec7944a | 2013-06-12 21:29:15 +0200 | [diff] [blame] | 537 | " vim:set fdm=marker ts=8: |