robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
ab20681c52
master
1" Vim completion script 2" Language: HTML and XHTML 3" Maintainer: Mikolaj Machowski ( mikmach AT wp DOT pl ) 4" Last Change: 2019 Sep 27 5 6" Distinguish between HTML versions. 7" To use with other HTML versions add another "elseif" condition to match 8" proper DOCTYPE. 9function ! htmlcomplete#DetectOmniFlavor () 10if & filetype == 'xhtml' 11let b: html_omni_flavor = 'xhtml10s' 12else 13let b: html_omni_flavor = 'html401t' 14endif 15let i = 1 16let line = "" 17while i < 10 && i < line ( "$" ) 18let line = getline ( i ) 19if line =~ '<!DOCTYPE.*\<DTD ' 20break 21endif 22let i += 1 23endwhile 24if line =~ '<!DOCTYPE.*\<DTD ' " doctype line found above 25if line =~ ' HTML 3\.2' 26let b: html_omni_flavor = 'html32' 27elseif line =~ ' XHTML 1\.1' 28let b: html_omni_flavor = 'xhtml11' 29else " two-step detection with strict/frameset/transitional 30if line =~ ' XHTML 1\.0' 31let b: html_omni_flavor = 'xhtml10' 32elseif line =~ ' HTML 4\.01' 33let b: html_omni_flavor = 'html401' 34elseif line =~ ' HTML 4.0\>' 35let b: html_omni_flavor = 'html40' 36endif 37if line =~ '\<Transitional\>' 38let b: html_omni_flavor .= 't' 39elseif line =~ '\<Frameset\>' 40let b: html_omni_flavor .= 'f' 41else 42let b: html_omni_flavor .= 's' 43endif 44endif 45endif 46endfunction 47 48function ! htmlcomplete#CompleteTags ( findstart , base ) 49if a: findstart 50" locate the start of the word 51let line = getline ( '.' ) 52let start = col ( '.' ) - 1 53let curline = line ( '.' ) 54let compl_begin = col ( '.' ) - 2 55while start >= 0 && line [ start - 1 ] =~ '\(\k\|[!:.-]\)' 56let start -= 1 57endwhile 58" Handling of entities {{{ 59if start >= 0 && line [ start - 1 ] =~ '&' 60let b: entitiescompl = 1 61let b: compl_context = '' 62return start 63endif 64" }}} 65" Handling of <style> tag {{{ 66let stylestart = searchpair ( '<style\>' , '' , '<\/style\>' , "bnW" ) 67let styleend = searchpair ( '<style\>' , '' , '<\/style\>' , "nW" ) 68if stylestart != 0 && styleend != 0 69if stylestart <= curline && styleend >= curline 70let start = col ( '.' ) - 1 71let b: csscompl = 1 72while start >= 0 && line [ start - 1 ] =~ '\(\k\|-\)' 73let start -= 1 74endwhile 75endif 76endif 77" }}} 78" Handling of <script> tag {{{ 79let scriptstart = searchpair ( '<script\>' , '' , '<\/script\>' , "bnW" ) 80let scriptend = searchpair ( '<script\>' , '' , '<\/script\>' , "nW" ) 81if scriptstart != 0 && scriptend != 0 82if scriptstart <= curline && scriptend >= curline 83let start = col ( '.' ) - 1 84let b: jscompl = 1 85let b: jsrange = [ scriptstart , scriptend ] 86while start >= 0 && line [ start - 1 ] =~ '\k' 87let start -= 1 88endwhile 89" We are inside of <script> tag. But we should also get contents 90" of all linked external files and (secondary, less probably) other <script> tags 91" This logic could possible be done in separate function - may be 92" reused in events scripting (also with option could be reused for 93" CSS 94let b: js_extfiles = [] 95let l = line ( '.' ) 96let c = col ( '.' ) 97call cursor ( 1 , 1 ) 98while search ( '<\@<=script\>' , 'W' ) && line ( '.' ) <= l 99if synIDattr ( synID ( line ( '.' ), col ( '.' ) - 1 , 0 ), "name" ) !~? 'comment' 100let sname = matchstr ( getline ( '.' ), '<script[^>]*src\s*=\s*\([''"]\)\zs.\{-}\ze\1' ) 101if filereadable ( sname ) 102let b: js_extfiles += readfile ( sname ) 103endif 104endif 105endwhile 106call cursor ( 1 , 1 ) 107let js_scripttags = [] 108while search ( '<script\>' , 'W' ) && line ( '.' ) < l 109if matchstr ( getline ( '.' ), '<script[^>]*src' ) == '' 110let js_scripttag = getline ( line ( '.' ), search ( '</script>' , 'W' )) 111let js_scripttags += js_scripttag 112endif 113endwhile 114let b: js_extfiles += js_scripttags 115call cursor ( l , c ) 116unlet ! l c 117endif 118endif 119" }}} 120if ! exists ( "b:csscompl" ) && ! exists ( "b:jscompl" ) 121let b: compl_context = getline ( '.' )[ 0 :( compl_begin )] 122if b: compl_context !~ '<[^>]*$' 123" Look like we may have broken tag. Check previous lines. 124let i = 1 125while 1 126let context_line = getline ( curline - i ) 127if context_line =~ '<[^>]*$' 128" Yep, this is this line 129let context_lines = getline ( curline - i , curline - 1 ) + [ b: compl_context ] 130let b: compl_context = join ( context_lines , ' ' ) 131break 132elseif context_line =~ '>[^<]*$' || i == curline 133" We are in normal tag line, no need for completion at all 134" OR reached first line without tag at all 135let b: compl_context = '' 136break 137endif 138let i += 1 139endwhile 140" Make sure we don't have counter 141unlet ! i 142endif 143let b: compl_context = matchstr ( b: compl_context , '.*\zs<.*' ) 144 145" Return proper start for on-events. Without that beginning of 146" completion will be badly reported 147if b: compl_context =~? 'on[a-z]*\s*=\s*\(''[^'']*\|"[^"]*\)$' 148let start = col ( '.' ) - 1 149while start >= 0 && line [ start - 1 ] =~ '\k' 150let start -= 1 151endwhile 152endif 153" If b:compl_context begins with <? we are inside of PHP code. It 154" wasn't closed so PHP completion passed it to HTML 155if & filetype =~? 'php' && b: compl_context =~ '^<?' 156let b: phpcompl = 1 157let start = col ( '.' ) - 1 158while start >= 0 && line [ start - 1 ] =~ '[a-zA-Z_0-9\x7f-\xff$]' 159let start -= 1 160endwhile 161endif 162else 163let b: compl_context = getline ( '.' )[ 0 : compl_begin ] 164endif 165return start 166else 167" Initialize base return lists 168let res = [] 169let res2 = [] 170" a:base is very short - we need context 171let context = b: compl_context 172" Check if we should do CSS completion inside of <style> tag 173" or JS completion inside of <script> tag or PHP completion in case of <? 174" tag AND &ft==php 175if exists ( "b:csscompl" ) 176unlet ! b: csscompl 177let context = b: compl_context 178unlet ! b: compl_context 179return csscomplete#CompleteCSS ( 0 , context ) 180elseif exists ( "b:jscompl" ) 181unlet ! b: jscompl 182return javascriptcomplete#CompleteJS ( 0 , a: base ) 183elseif exists ( "b:phpcompl" ) 184unlet ! b: phpcompl 185let context = b: compl_context 186return phpcomplete#CompletePHP ( 0 , a: base ) 187else 188if len ( b: compl_context ) == 0 && ! exists ( "b:entitiescompl" ) 189return [] 190endif 191let context = matchstr ( b: compl_context , '.\zs.*' ) 192endif 193unlet ! b: compl_context 194" Entities completion {{{ 195if exists ( "b:entitiescompl" ) 196unlet ! b: entitiescompl 197 198if ! exists ( "b:html_doctype" ) 199call htmlcomplete#CheckDoctype () 200endif 201if ! exists ( "b:html_omni" ) 202"runtime! autoload/xml/xhtml10s.vim 203call htmlcomplete#LoadData () 204endif 205 206let entities = b: html_omni [ 'vimxmlentities' ] 207 208if len ( a: base ) == 1 209for m in entities 210if m =~ '^' . a: base 211call add ( res , m . ';' ) 212endif 213endfor 214return res 215else 216for m in entities 217if m =~? '^' . a: base 218call add ( res , m . ';' ) 219elseif m =~? a: base 220call add ( res2 , m . ';' ) 221endif 222endfor 223 224return res + res2 225endif 226 227 228endif 229" }}} 230if context =~ '>' 231" Generally if context contains > it means we are outside of tag and 232" should abandon action - with one exception: <style> span { bo 233if context =~ 'style[^>]\{-}>[^<]\{-}$' 234return csscomplete#CompleteCSS ( 0 , context ) 235elseif context =~ 'script[^>]\{-}>[^<]\{-}$' 236let b: jsrange = [ line ( '.' ), search ( '<\/script\>' , 'nW' )] 237return javascriptcomplete#CompleteJS ( 0 , context ) 238else 239return [] 240endif 241endif 242 243" If context contains > it means we are already outside of tag and we 244" should abandon action 245" If context contains white space it is attribute. 246" It can be also value of attribute. 247" We have to get first word to offer proper completions 248if context =~ '^\s*$' 249" empty or whitespace line 250let tag = '' 251else 252let tag = split ( context )[ 0 ] 253" Detect if tag is uppercase to return in proper case, 254" we need to make it lowercase for processing 255if tag =~ '^[A-Z]*$' 256let uppercase_tag = 1 257let tag = tolower ( tag ) 258else 259let uppercase_tag = 0 260endif 261endif 262" Get last word, it should be attr name 263let attr = matchstr ( context , '.*\s\zs.*' ) 264" Possible situations where any prediction would be difficult: 265" 1. Events attributes 266if context =~ '\s' 267" Sort out style, class, and on* cases 268if context =~? "\\(on[a-z]*\\|id\\|style\\|class\\)\\s*=\\s*[\"']" 269" Id, class completion {{{ 270if context =~? "\\(id\\|class\\)\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$" 271if context =~? "class\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$" 272let search_for = "class" 273elseif context =~? "id\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$" 274let search_for = "id" 275endif 276" Handle class name completion 277" 1. Find lines of <link stylesheet> 278" 1a. Check file for @import 279" 2. Extract filename(s?) of stylesheet, 280call cursor ( 1 , 1 ) 281let head = getline ( search ( '<head\>' ), search ( '<\/head>' )) 282let headjoined = join ( copy ( head ), ' ' ) 283if headjoined =~ '<style' 284" Remove possibly confusing CSS operators 285let stylehead = substitute ( headjoined , '+>\*[,' , ' ' , 'g' ) 286if search_for == 'class' 287let styleheadlines = split ( stylehead ) 288let headclasslines = filter ( copy ( styleheadlines ), "v:val =~ '\\([a-zA-Z0-9:]\\+\\)\\?\\.[a-zA-Z0-9_-]\\+'" ) 289else 290let stylesheet = split ( headjoined , '[{}]' ) 291" Get all lines which fit id syntax 292let classlines = filter ( copy ( stylesheet ), "v:val =~ '#[a-zA-Z0-9_-]\\+'" ) 293" Filter out possible color definitions 294call filter ( classlines , "v:val !~ ':\\s*#[a-zA-Z0-9_-]\\+'" ) 295" Filter out complex border definitions 296call filter ( classlines , "v:val !~ '\\(none\\|hidden\\|dotted\\|dashed\\|solid\\|double\\|groove\\|ridge\\|inset\\|outset\\)\\s*#[a-zA-Z0-9_-]\\+'" ) 297let templines = join ( classlines , ' ' ) 298let headclasslines = split ( templines ) 299call filter ( headclasslines , "v:val =~ '#[a-zA-Z0-9_-]\\+'" ) 300endif 301let internal = 1 302else 303let internal = 0 304endif 305let styletable = [] 306let secimportfiles = [] 307let filestable = filter ( copy ( head ), "v:val =~ '\\(@import\\|link.*stylesheet\\)'" ) 308for line in filestable 309if line =~ "@import" 310let styletable += [ matchstr ( line , "import\\s\\+\\(url(\\)\\?[\"']\\?\\zs\\f\\+\\ze" )] 311elseif line =~ "<link" 312let styletable += [ matchstr ( line , "href\\s*=\\s*[\"']\\zs\\f\\+\\ze" )] 313endif 314endfor 315for file in styletable 316if filereadable ( file ) 317let stylesheet = readfile ( file ) 318let secimport = filter ( copy ( stylesheet ), "v:val =~ '@import'" ) 319if len ( secimport ) > 0 320for line in secimport 321let secfile = matchstr ( line , "import\\s\\+\\(url(\\)\\?[\"']\\?\\zs\\f\\+\\ze" ) 322let secfile = fnamemodify ( file , ":p:h" ) . '/' . secfile 323let secimportfiles += [ secfile ] 324endfor 325endif 326endif 327endfor 328let cssfiles = styletable + secimportfiles 329let classes = [] 330for file in cssfiles 331let classlines = [] 332if filereadable ( file ) 333let stylesheet = readfile ( file ) 334let stylefile = join ( stylesheet , ' ' ) 335let stylefile = substitute ( stylefile , '+>\*[,' , ' ' , 'g' ) 336if search_for == 'class' 337let stylesheet = split ( stylefile ) 338let classlines = filter ( copy ( stylesheet ), "v:val =~ '\\([a-zA-Z0-9:]\\+\\)\\?\\.[a-zA-Z0-9_-]\\+'" ) 339else 340let stylesheet = split ( stylefile , '[{}]' ) 341" Get all lines which fit id syntax 342let classlines = filter ( copy ( stylesheet ), "v:val =~ '#[a-zA-Z0-9_-]\\+'" ) 343" Filter out possible color definitions 344call filter ( classlines , "v:val !~ ':\\s*#[a-zA-Z0-9_-]\\+'" ) 345" Filter out complex border definitions 346call filter ( classlines , "v:val !~ '\\(none\\|hidden\\|dotted\\|dashed\\|solid\\|double\\|groove\\|ridge\\|inset\\|outset\\)\\s*#[a-zA-Z0-9_-]\\+'" ) 347let templines = join ( classlines , ' ' ) 348let stylelines = split ( templines ) 349let classlines = filter ( stylelines , "v:val =~ '#[a-zA-Z0-9_-]\\+'" ) 350 351endif 352endif 353" We gathered classes definitions from all external files 354let classes += classlines 355endfor 356if internal == 1 357let classes += headclasslines 358endif 359 360if search_for == 'class' 361let elements = {} 362for element in classes 363if element =~ '^\.' 364let class = matchstr ( element , '^\.\zs[a-zA-Z][a-zA-Z0-9_-]*\ze' ) 365let class = substitute ( class , ':.*' , '' , '' ) 366if has_key ( elements , 'common' ) 367let elements [ 'common' ] .= ' ' . class 368else 369let elements [ 'common' ] = class 370endif 371else 372let class = matchstr ( element , '[a-zA-Z1-6]*\.\zs[a-zA-Z][a-zA-Z0-9_-]*\ze' ) 373let tagname = tolower ( matchstr ( element , '[a-zA-Z1-6]*\ze.' )) 374if tagname != '' 375if has_key ( elements , tagname ) 376let elements [ tagname ] .= ' ' . class 377else 378let elements [ tagname ] = class 379endif 380endif 381endif 382endfor 383 384if has_key ( elements , tag ) && has_key ( elements , 'common' ) 385let values = split ( elements [ tag ] . " " . elements [ 'common' ]) 386elseif has_key ( elements , tag ) && ! has_key ( elements , 'common' ) 387let values = split ( elements [ tag ]) 388elseif ! has_key ( elements , tag ) && has_key ( elements , 'common' ) 389let values = split ( elements [ 'common' ]) 390else 391return [] 392endif 393 394elseif search_for == 'id' 395" Find used IDs 396" 1. Catch whole file 397let filelines = getline ( 1 , line ( '$' )) 398" 2. Find lines with possible id 399let used_id_lines = filter ( filelines , 'v:val =~ "id\\s*=\\s*[\"''][a-zA-Z0-9_-]\\+"' ) 400" 3a. Join all filtered lines 401let id_string = join ( used_id_lines , ' ' ) 402" 3b. And split them to be sure each id is in separate item 403let id_list = split ( id_string , 'id\s*=\s*' ) 404" 4. Extract id values 405let used_id = map ( id_list , 'matchstr(v:val, "[\"'']\\zs[a-zA-Z0-9_-]\\+\\ze")' ) 406let joined_used_id = ',' . join ( used_id , ',' ) . ',' 407 408let allvalues = map ( classes , 'matchstr(v:val, ".*#\\zs[a-zA-Z0-9_-]\\+")' ) 409 410let values = [] 411 412for element in classes 413if joined_used_id !~ ',' . element . ',' 414let values += [ element ] 415endif 416 417endfor 418 419endif 420 421" We need special version of sbase 422let classbase = matchstr ( context , ".*[\"']" ) 423let classquote = matchstr ( classbase , '.$' ) 424 425let entered_class = matchstr ( attr , ".*=\\s*[\"']\\zs.*" ) 426 427for m in sort ( values ) 428if m =~? '^' . entered_class 429call add ( res , m . classquote ) 430elseif m =~? entered_class 431call add ( res2 , m . classquote ) 432endif 433endfor 434 435return res + res2 436 437elseif context =~? "style\\s*=\\s*[\"'][^\"']*$" 438return csscomplete#CompleteCSS ( 0 , context ) 439 440endif 441" }}} 442" Complete on-events {{{ 443if context =~? 'on[a-z]*\s*=\s*\(''[^'']*\|"[^"]*\)$' 444" We have to: 445" 1. Find external files 446let b: js_extfiles = [] 447let l = line ( '.' ) 448let c = col ( '.' ) 449call cursor ( 1 , 1 ) 450while search ( '<\@<=script\>' , 'W' ) && line ( '.' ) <= l 451if synIDattr ( synID ( line ( '.' ), col ( '.' ) - 1 , 0 ), "name" ) !~? 'comment' 452let sname = matchstr ( getline ( '.' ), '<script[^>]*src\s*=\s*\([''"]\)\zs.\{-}\ze\1' ) 453if filereadable ( sname ) 454let b: js_extfiles += readfile ( sname ) 455endif 456endif 457endwhile 458" 2. Find at least one <script> tag 459call cursor ( 1 , 1 ) 460let js_scripttags = [] 461while search ( '<script\>' , 'W' ) && line ( '.' ) < l 462if matchstr ( getline ( '.' ), '<script[^>]*src' ) == '' 463let js_scripttag = getline ( line ( '.' ), search ( '</script>' , 'W' )) 464let js_scripttags += js_scripttag 465endif 466endwhile 467let b: js_extfiles += js_scripttags 468 469" 3. Proper call for javascriptcomplete#CompleteJS 470call cursor ( l , c ) 471let js_context = matchstr ( a: base , '\k\+$' ) 472let js_shortcontext = substitute ( a: base , js_context . '$' , '' , '' ) 473let b: compl_context = context 474let b: jsrange = [ l , l ] 475unlet ! l c 476return javascriptcomplete#CompleteJS ( 0 , js_context ) 477 478endif 479 480" }}} 481let stripbase = matchstr ( context , ".*\\(on[a-zA-Z]*\\|style\\|class\\)\\s*=\\s*[\"']\\zs.*" ) 482" Now we have context stripped from all chars up to style/class. 483" It may fail with some strange style value combinations. 484if stripbase !~ "[\"']" 485return [] 486endif 487endif 488" Value of attribute completion {{{ 489" If attr contains =\s*[\"'] we match value of attribute 490if attr =~ "=\s*[\"']" || attr =~ "=\s*$" 491" Let do attribute specific completion 492let attrname = matchstr ( attr , '.*\ze\s*=' ) 493let entered_value = matchstr ( attr , ".*=\\s*[\"']\\?\\zs.*" ) 494let values = [] 495" Load data {{{ 496if ! exists ( "b:html_doctype" ) 497call htmlcomplete#CheckDoctype () 498endif 499if ! exists ( "b:html_omni" ) 500"runtime! autoload/xml/xhtml10s.vim 501call htmlcomplete#LoadData () 502endif 503" }}} 504if attrname == 'href' 505" Now we are looking for local anchors defined by name or id 506if entered_value =~ '^#' 507let file = join ( getline ( 1 , line ( '$' )), ' ' ) 508" Split it be sure there will be one id/name element in 509" item, it will be also first word [a-zA-Z0-9_-] in element 510let oneelement = split ( file , "\\(meta \\)\\@<!\\(name\\|id\\)\\s*=\\s*[\"']" ) 511for i in oneelement 512let values += [ '#' . matchstr ( i , "^[a-zA-Z][a-zA-Z0-9%_-]*" )] 513endfor 514endif 515else 516if has_key ( b: html_omni , tag ) && has_key ( b: html_omni [ tag ][ 1 ], attrname ) 517let values = b: html_omni [ tag ][ 1 ][ attrname ] 518else 519return [] 520endif 521endif 522 523if len ( values ) == 0 524return [] 525endif 526 527" We need special version of sbase 528let attrbase = matchstr ( context , ".*[\"']" ) 529let attrquote = matchstr ( attrbase , '.$' ) 530if attrquote !~ "['\"]" 531let attrquoteopen = '"' 532let attrquote = '"' 533else 534let attrquoteopen = '' 535endif 536 537for m in values 538" This if is needed to not offer all completions as-is 539" alphabetically but sort them. Those beginning with entered 540" part will be as first choices 541if m =~ '^' . entered_value 542call add ( res , attrquoteopen . m . attrquote ) 543elseif m =~ entered_value 544call add ( res2 , attrquoteopen . m . attrquote ) 545endif 546endfor 547 548return res + res2 549 550endif 551" }}} 552" Attribute completion {{{ 553" Shorten context to not include last word 554let sbase = matchstr ( context , '.*\ze\s.*' ) 555 556" Load data {{{ 557if ! exists ( "b:html_doctype" ) 558call htmlcomplete#CheckDoctype () 559endif 560if ! exists ( "b:html_omni" ) 561call htmlcomplete#LoadData () 562endif 563" }}} 564 565if has_key ( b: html_omni , tag ) 566let attrs = keys ( b: html_omni [ tag ][ 1 ]) 567else 568return [] 569endif 570 571for m in sort ( attrs ) 572if m =~ '^' . attr 573call add ( res , m ) 574elseif m =~ attr 575call add ( res2 , m ) 576endif 577endfor 578let menu = res + res2 579if has_key ( b: html_omni , 'vimxmlattrinfo' ) 580let final_menu = [] 581for i in range ( len ( menu )) 582let item = menu [ i ] 583if has_key ( b: html_omni [ 'vimxmlattrinfo' ], item ) 584let m_menu = b: html_omni [ 'vimxmlattrinfo' ][ item ][ 0 ] 585let m_info = b: html_omni [ 'vimxmlattrinfo' ][ item ][ 1 ] 586else 587let m_menu = '' 588let m_info = '' 589endif 590if len ( b: html_omni [ tag ][ 1 ][ item ]) > 0 && b: html_omni [ tag ][ 1 ][ item ][ 0 ] =~ '^\(BOOL\|' . item . '\)$' 591let item = item 592let m_menu = 'Bool' 593else 594let item .= '="' 595endif 596let final_menu += [{ 'word' : item , 'menu' : m_menu , 'info' : m_info }] 597endfor 598else 599let final_menu = [] 600for i in range ( len ( menu )) 601let item = menu [ i ] 602if len ( b: html_omni [ tag ][ 1 ][ item ]) > 0 && b: html_omni [ tag ][ 1 ][ item ][ 0 ] =~ '^\(BOOL\|' . item . '\)$' 603let item = item 604else 605let item .= '="' 606endif 607let final_menu += [ item ] 608endfor 609return final_menu 610 611endif 612return final_menu 613 614endif 615" }}} 616" Close tag {{{ 617let b: unaryTagsStack = "base meta link hr br param img area input col" 618if context =~ '^\/' 619if context =~ '^\/.' 620return [] 621else 622let opentag = xmlcomplete#GetLastOpenTag ( "b:unaryTagsStack" ) 623return [ opentag . ">" ] 624endif 625endif 626" }}} 627" Load data {{{ 628if ! exists ( "b:html_doctype" ) 629call htmlcomplete#CheckDoctype () 630endif 631if ! exists ( "b:html_omni" ) 632"runtime! autoload/xml/xhtml10s.vim 633call htmlcomplete#LoadData () 634endif 635" }}} 636" Tag completion {{{ 637" Deal with tag completion. 638let opentag = tolower ( xmlcomplete#GetLastOpenTag ( "b:unaryTagsStack" )) 639" MM: TODO: GLOT works always the same but with some weird situation it 640" behaves as intended in HTML but screws in PHP 641if opentag == '' || & filetype == 'php' && ! has_key ( b: html_omni , opentag ) 642" Hack for sometimes failing GetLastOpenTag. 643" As far as I tested fail isn't GLOT fault but problem 644" of invalid document - not properly closed tags and other mish-mash. 645" Also when document is empty. Return list of *all* tags. 646let tags = keys ( b: html_omni ) 647call filter ( tags , 'v:val !~ "^vimxml"' ) 648else 649if has_key ( b: html_omni , opentag ) 650let tags = b: html_omni [ opentag ][ 0 ] 651else 652return [] 653endif 654endif 655" }}} 656 657if exists ( "uppercase_tag" ) && uppercase_tag == 1 658let context = tolower ( context ) 659endif 660" Handle XML keywords: DOCTYPE 661if opentag == '' 662let tags += [ 663 \'!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">' , 664 \'!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">' , 665 \'!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">' , 666 \'!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN" "http://www.w3.org/TR/REC-html40/frameset.dtd">' , 667 \'!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">' , 668 \'!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">' , 669 \'!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">' , 670 \'!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' , 671 \'!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' , 672 \'!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">' , 673 \'!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/1999/xhtml">' 674 \] 675endif 676 677for m in sort ( tags ) 678if m =~ '^' . context 679call add ( res , m ) 680elseif m =~ context 681call add ( res2 , m ) 682endif 683endfor 684let menu = res + res2 685if has_key ( b: html_omni , 'vimxmltaginfo' ) 686let final_menu = [] 687for i in range ( len ( menu )) 688let item = menu [ i ] 689if has_key ( b: html_omni [ 'vimxmltaginfo' ], item ) 690let m_menu = b: html_omni [ 'vimxmltaginfo' ][ item ][ 0 ] 691let m_info = b: html_omni [ 'vimxmltaginfo' ][ item ][ 1 ] 692else 693let m_menu = '' 694let m_info = '' 695endif 696if & filetype == 'html' && exists ( "uppercase_tag" ) && uppercase_tag == 1 && item !~ 'DOCTYPE' 697let item = toupper ( item ) 698endif 699if item =~ 'DOCTYPE' 700let abbr = 'DOCTYPE ' . matchstr ( item , 'DTD \zsX\?HTML .\{-}\ze\/\/' ) 701else 702let abbr = item 703endif 704let final_menu += [{ 'abbr' : abbr , 'word' : item , 'menu' : m_menu , 'info' : m_info }] 705endfor 706else 707let final_menu = menu 708endif 709return final_menu 710 711" }}} 712endif 713endfunction 714 715function ! htmlcomplete#LoadData () " {{{ 716if ! exists ( "b:html_omni_flavor" ) 717if & filetype == 'html' 718let b: html_omni_flavor = 'html401t' 719else 720let b: html_omni_flavor = 'xhtml10s' 721endif 722endif 723" With that if we still have bloated memory but create new buffer 724" variables only by linking to existing g:variable, not sourcing whole 725" file. 726if exists ( 'g:xmldata_' . b: html_omni_flavor ) 727exe 'let b:html_omni = g:xmldata_' . b: html_omni_flavor 728else 729exe 'runtime! autoload/xml/' . b: html_omni_flavor . '.vim' 730exe 'let b:html_omni = g:xmldata_' . b: html_omni_flavor 731endif 732endfunction 733" }}} 734function ! htmlcomplete#CheckDoctype () " {{{ 735if exists ( 'b:html_omni_flavor' ) 736let old_flavor = b: html_omni_flavor 737else 738let old_flavor = '' 739endif 740let i = 1 741while i < 10 && i < line ( "$" ) 742let line = getline ( i ) 743if line =~ '<!DOCTYPE.*\<DTD HTML 3\.2' 744let b: html_omni_flavor = 'html32' 745let b: html_doctype = 1 746break 747elseif line =~ '<!DOCTYPE.*\<DTD HTML 4\.0 Transitional' 748let b: html_omni_flavor = 'html40t' 749let b: html_doctype = 1 750break 751elseif line =~ '<!DOCTYPE.*\<DTD HTML 4\.0 Frameset' 752let b: html_omni_flavor = 'html40f' 753let b: html_doctype = 1 754break 755elseif line =~ '<!DOCTYPE.*\<DTD HTML 4\.0' 756let b: html_omni_flavor = 'html40s' 757let b: html_doctype = 1 758break 759elseif line =~ '<!DOCTYPE.*\<DTD HTML 4\.01 Transitional' 760let b: html_omni_flavor = 'html401t' 761let b: html_doctype = 1 762break 763elseif line =~ '<!DOCTYPE.*\<DTD HTML 4\.01 Frameset' 764let b: html_omni_flavor = 'html401f' 765let b: html_doctype = 1 766break 767elseif line =~ '<!DOCTYPE.*\<DTD HTML 4\.01' 768let b: html_omni_flavor = 'html401s' 769let b: html_doctype = 1 770break 771elseif line =~ '<!DOCTYPE.*\<DTD XHTML 1\.0 Transitional' 772let b: html_omni_flavor = 'xhtml10t' 773let b: html_doctype = 1 774break 775elseif line =~ '<!DOCTYPE.*\<DTD XHTML 1\.0 Frameset' 776let b: html_omni_flavor = 'xhtml10f' 777let b: html_doctype = 1 778break 779elseif line =~ '<!DOCTYPE.*\<DTD XHTML 1\.0 Strict' 780let b: html_omni_flavor = 'xhtml10s' 781let b: html_doctype = 1 782break 783elseif line =~ '<!DOCTYPE.*\<DTD XHTML 1\.1' 784let b: html_omni_flavor = 'xhtml11' 785let b: html_doctype = 1 786break 787endif 788let i += 1 789endwhile 790if ! exists ( "b:html_doctype" ) 791return 792else 793" Tie g:xmldata with b:html_omni this way we need to sourca data file only 794" once, not every time per buffer. 795if old_flavor == b: html_omni_flavor 796return 797else 798if exists ( 'g:xmldata_' . b: html_omni_flavor ) 799exe 'let b:html_omni = g:xmldata_' . b: html_omni_flavor 800else 801exe 'runtime! autoload/xml/' . b: html_omni_flavor . '.vim' 802exe 'let b:html_omni = g:xmldata_' . b: html_omni_flavor 803endif 804return 805endif 806endif 807endfunction 808" }}} 809" vim:set foldmethod=marker: