2
2
" Header: "{{{
3
3
" Maintainer: Bram Moolenaar
4
4
" Original Author: Andy Wokula <anwoku@yahoo.de>
5
- " Last Change: 2017 Jun 13
5
+ " Last Change: 2018 Mar 09
6
6
" Version: 1.0
7
7
" Description: HTML indent script with cached state for faster indenting on a
8
8
" range of lines.
@@ -56,6 +56,9 @@ let s:cpo_save = &cpo
56
56
set cpo -= C
57
57
" }}}
58
58
59
+ " Pattern to match the name of a tag, including custom elements.
60
+ let s: tagname = ' \w\+\(-\w\+\)*'
61
+
59
62
" Check and process settings from b:html_indent and g:html_indent... variables.
60
63
" Prefer using buffer-local settings over global settings, so that there can
61
64
" be defaults for all HTML files and exceptions for specific types of HTML
@@ -280,7 +283,7 @@ func! s:CountITags(text)
280
283
let s: nextrel = 0 " relative indent steps for next line [unit &sw]:
281
284
let s: block = 0 " assume starting outside of a block
282
285
let s: countonly = 1 " don't change state
283
- call substitute (a: text , ' <\zs/\=\w\+\(-\w\+\)* \>\|<!--\[\|\[endif\]-->\|<!--\|-->' , ' \=s:CheckTag(submatch(0))' , ' g' )
286
+ call substitute (a: text , ' <\zs/\=' . s: tagname . ' \>\|<!--\[\|\[endif\]-->\|<!--\|-->' , ' \=s:CheckTag(submatch(0))' , ' g' )
284
287
let s: countonly = 0
285
288
endfunc " }}}
286
289
@@ -292,7 +295,7 @@ func! s:CountTagsAndState(text)
292
295
let s: nextrel = 0 " relative indent steps for next line [unit &sw]:
293
296
294
297
let s: block = b: hi_newstate .block
295
- let tmp = substitute (a: text , ' <\zs/\=\w\+\(-\w\+\)* \>\|<!--\[\|\[endif\]-->\|<!--\|-->' , ' \=s:CheckTag(submatch(0))' , ' g' )
298
+ let tmp = substitute (a: text , ' <\zs/\=' . s: tagname . ' \>\|<!--\[\|\[endif\]-->\|<!--\|-->' , ' \=s:CheckTag(submatch(0))' , ' g' )
296
299
if s: block == 3
297
300
let b: hi_newstate .scripttype = s: GetScriptType (matchstr (tmp, ' \C.*<SCRIPT\>\zs[^>]*' ))
298
301
endif
@@ -530,7 +533,7 @@ func! s:FreshState(lnum)
530
533
let swendtag = match (text, ' ^\s*</' ) >= 0
531
534
532
535
" If previous line ended in a closing tag, line up with the opening tag.
533
- if ! swendtag && text = ~ ' </\w\+ \s*>\s*$'
536
+ if ! swendtag && text = ~ ' </' . s: tagname . ' \s*>\s*$'
534
537
call cursor (state .lnum, 99999 )
535
538
normal ! F<
536
539
let start_lnum = HtmlIndent_FindStartTag ()
@@ -860,7 +863,7 @@ func! HtmlIndent_FindStartTag()
860
863
" The cursor must be on or before a closing tag.
861
864
" If found, positions the cursor at the match and returns the line number.
862
865
" Otherwise returns 0.
863
- let tagname = matchstr (getline (' .' )[col (' .' ) - 1 :], ' </\zs\w\+ \ze' )
866
+ let tagname = matchstr (getline (' .' )[col (' .' ) - 1 :], ' </\zs' . s: tagname . ' \ze' )
864
867
let start_lnum = searchpair (' <' . tagname . ' \>' , ' ' , ' </' . tagname . ' \>' , ' bW' )
865
868
if start_lnum > 0
866
869
return start_lnum
@@ -876,7 +879,7 @@ func! HtmlIndent_FindTagEnd()
876
879
" a self-closing tag, to the matching ">".
877
880
" Limited to look up to b:html_indent_line_limit lines away.
878
881
let text = getline (' .' )
879
- let tagname = matchstr (text, ' \w\+ \|!--' , col (' .' ))
882
+ let tagname = matchstr (text, s: tagname . ' \|!--' , col (' .' ))
880
883
if tagname == ' !--'
881
884
call search (' --\zs>' )
882
885
elseif s: get_tag (' /' . tagname) != 0
@@ -921,9 +924,22 @@ func! s:InsideTag(foundHtmlString)
921
924
else
922
925
let idx = match (text, ' \s\zs[_a-zA-Z0-9-]\+="' )
923
926
endif
927
+ if idx == -1
928
+ " try <tag attr
929
+ let idx = match (text, ' <' . s: tagname . ' \s\+\zs\w' )
930
+ endif
931
+ if idx == -1
932
+ " after just <tag indent one level more
933
+ let idx = match (text, ' <' . s: tagname . ' $' )
934
+ if idx >= 0
935
+ call cursor (lnum, idx)
936
+ return virtcol (' .' ) + shiftwidth ()
937
+ endif
938
+ endif
924
939
if idx > 0
925
- " Found the attribute. TODO: assumes spaces, no Tabs.
926
- return idx
940
+ " Found the attribute to align with.
941
+ call cursor (lnum, idx)
942
+ return virtcol (' .' )
927
943
endif
928
944
endwhile
929
945
return -1
0 commit comments