vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

htmlcomplete.vim (25482B)


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