1" Vim completion script
2" Language:	HTML and XHTML
3" Maintainer:	Mikolaj Machowski ( mikmach AT wp DOT pl )
4" Last Change:	2014 Jun 20
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()
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
46endfunction
47
48function! 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 == ''
249		let tag = ''
250	else
251		let tag = split(context)[0]
252		" Detect if tag is uppercase to return in proper case,
253		" we need to make it lowercase for processing
254		if tag =~ '^[A-Z]*$'
255			let uppercase_tag = 1
256			let tag = tolower(tag)
257		else
258			let uppercase_tag = 0
259		endif
260	endif
261	" Get last word, it should be attr name
262	let attr = matchstr(context, '.*\s\zs.*')
263	" Possible situations where any prediction would be difficult:
264	" 1. Events attributes
265	if context =~ '\s'
266		" Sort out style, class, and on* cases
267		if context =~? "\\(on[a-z]*\\|id\\|style\\|class\\)\\s*=\\s*[\"']"
268			" Id, class completion {{{
269			if context =~? "\\(id\\|class\\)\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
270				if context =~? "class\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
271					let search_for = "class"
272				elseif context =~? "id\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
273					let search_for = "id"
274				endif
275				" Handle class name completion
276				" 1. Find lines of <link stylesheet>
277				" 1a. Check file for @import
278				" 2. Extract filename(s?) of stylesheet,
279				call cursor(1,1)
280				let head = getline(search('<head\>'), search('<\/head>'))
281				let headjoined = join(copy(head), ' ')
282				if headjoined =~ '<style'
283					" Remove possibly confusing CSS operators
284					let stylehead = substitute(headjoined, '+>\*[,', ' ', 'g')
285					if search_for == 'class'
286						let styleheadlines = split(stylehead)
287						let headclasslines = filter(copy(styleheadlines), "v:val =~ '\\([a-zA-Z0-9:]\\+\\)\\?\\.[a-zA-Z0-9_-]\\+'")
288					else
289						let stylesheet = split(headjoined, '[{}]')
290						" Get all lines which fit id syntax
291						let classlines = filter(copy(stylesheet), "v:val =~ '#[a-zA-Z0-9_-]\\+'")
292						" Filter out possible color definitions
293						call filter(classlines, "v:val !~ ':\\s*#[a-zA-Z0-9_-]\\+'")
294						" Filter out complex border definitions
295						call filter(classlines, "v:val !~ '\\(none\\|hidden\\|dotted\\|dashed\\|solid\\|double\\|groove\\|ridge\\|inset\\|outset\\)\\s*#[a-zA-Z0-9_-]\\+'")
296						let templines = join(classlines, ' ')
297						let headclasslines = split(templines)
298						call filter(headclasslines, "v:val =~ '#[a-zA-Z0-9_-]\\+'")
299					endif
300					let internal = 1
301				else
302					let internal = 0
303				endif
304				let styletable = []
305				let secimportfiles = []
306				let filestable = filter(copy(head), "v:val =~ '\\(@import\\|link.*stylesheet\\)'")
307				for line in filestable
308					if line =~ "@import"
309						let styletable += [matchstr(line, "import\\s\\+\\(url(\\)\\?[\"']\\?\\zs\\f\\+\\ze")]
310					elseif line =~ "<link"
311						let styletable += [matchstr(line, "href\\s*=\\s*[\"']\\zs\\f\\+\\ze")]
312					endif
313				endfor
314				for file in styletable
315					if filereadable(file)
316						let stylesheet = readfile(file)
317						let secimport = filter(copy(stylesheet), "v:val =~ '@import'")
318						if len(secimport) > 0
319							for line in secimport
320								let secfile = matchstr(line, "import\\s\\+\\(url(\\)\\?[\"']\\?\\zs\\f\\+\\ze")
321								let secfile = fnamemodify(file, ":p:h").'/'.secfile
322								let secimportfiles += [secfile]
323							endfor
324						endif
325					endif
326				endfor
327				let cssfiles = styletable + secimportfiles
328				let classes = []
329				for file in cssfiles
330				  	let classlines = []
331					if filereadable(file)
332						let stylesheet = readfile(file)
333						let stylefile = join(stylesheet, ' ')
334						let stylefile = substitute(stylefile, '+>\*[,', ' ', 'g')
335						if search_for == 'class'
336							let stylesheet = split(stylefile)
337							let classlines = filter(copy(stylesheet), "v:val =~ '\\([a-zA-Z0-9:]\\+\\)\\?\\.[a-zA-Z0-9_-]\\+'")
338						else
339							let stylesheet = split(stylefile, '[{}]')
340							" Get all lines which fit id syntax
341							let classlines = filter(copy(stylesheet), "v:val =~ '#[a-zA-Z0-9_-]\\+'")
342							" Filter out possible color definitions
343							call filter(classlines, "v:val !~ ':\\s*#[a-zA-Z0-9_-]\\+'")
344							" Filter out complex border definitions
345							call filter(classlines, "v:val !~ '\\(none\\|hidden\\|dotted\\|dashed\\|solid\\|double\\|groove\\|ridge\\|inset\\|outset\\)\\s*#[a-zA-Z0-9_-]\\+'")
346							let templines = join(classlines, ' ')
347							let stylelines = split(templines)
348							let classlines = filter(stylelines, "v:val =~ '#[a-zA-Z0-9_-]\\+'")
349
350						endif
351					endif
352					" We gathered classes definitions from all external files
353					let classes += classlines
354				endfor
355				if internal == 1
356					let classes += headclasslines
357				endif
358
359				if search_for == 'class'
360					let elements = {}
361					for element in classes
362						if element =~ '^\.'
363							let class = matchstr(element, '^\.\zs[a-zA-Z][a-zA-Z0-9_-]*\ze')
364							let class = substitute(class, ':.*', '', '')
365							if has_key(elements, 'common')
366								let elements['common'] .= ' '.class
367							else
368								let elements['common'] = class
369							endif
370						else
371							let class = matchstr(element, '[a-zA-Z1-6]*\.\zs[a-zA-Z][a-zA-Z0-9_-]*\ze')
372							let tagname = tolower(matchstr(element, '[a-zA-Z1-6]*\ze.'))
373							if tagname != ''
374								if has_key(elements, tagname)
375									let elements[tagname] .= ' '.class
376								else
377									let elements[tagname] = class
378								endif
379							endif
380						endif
381					endfor
382
383					if has_key(elements, tag) && has_key(elements, 'common')
384						let values = split(elements[tag]." ".elements['common'])
385					elseif has_key(elements, tag) && !has_key(elements, 'common')
386						let values = split(elements[tag])
387					elseif !has_key(elements, tag) && has_key(elements, 'common')
388						let values = split(elements['common'])
389					else
390						return []
391					endif
392
393				elseif search_for == 'id'
394					" Find used IDs
395					" 1. Catch whole file
396					let filelines = getline(1, line('$'))
397					" 2. Find lines with possible id
398					let used_id_lines = filter(filelines, 'v:val =~ "id\\s*=\\s*[\"''][a-zA-Z0-9_-]\\+"')
399					" 3a. Join all filtered lines
400					let id_string = join(used_id_lines, ' ')
401					" 3b. And split them to be sure each id is in separate item
402					let id_list = split(id_string, 'id\s*=\s*')
403					" 4. Extract id values
404					let used_id = map(id_list, 'matchstr(v:val, "[\"'']\\zs[a-zA-Z0-9_-]\\+\\ze")')
405					let joined_used_id = ','.join(used_id, ',').','
406
407					let allvalues = map(classes, 'matchstr(v:val, ".*#\\zs[a-zA-Z0-9_-]\\+")')
408
409					let values = []
410
411					for element in classes
412						if joined_used_id !~ ','.element.','
413							let values += [element]
414						endif
415
416					endfor
417
418				endif
419
420				" We need special version of sbase
421				let classbase = matchstr(context, ".*[\"']")
422				let classquote = matchstr(classbase, '.$')
423
424				let entered_class = matchstr(attr, ".*=\\s*[\"']\\zs.*")
425
426				for m in sort(values)
427					if m =~? '^'.entered_class
428						call add(res, m . classquote)
429					elseif m =~? entered_class
430						call add(res2, m . classquote)
431					endif
432				endfor
433
434				return res + res2
435
436			elseif context =~? "style\\s*=\\s*[\"'][^\"']*$"
437				return csscomplete#CompleteCSS(0, context)
438
439			endif
440			" }}}
441			" Complete on-events {{{
442			if context =~? 'on[a-z]*\s*=\s*\(''[^'']*\|"[^"]*\)$'
443				" We have to:
444				" 1. Find external files
445				let b:js_extfiles = []
446				let l = line('.')
447				let c = col('.')
448				call cursor(1,1)
449				while search('<\@<=script\>', 'W') && line('.') <= l
450					if synIDattr(synID(line('.'),col('.')-1,0),"name") !~? 'comment'
451						let sname = matchstr(getline('.'), '<script[^>]*src\s*=\s*\([''"]\)\zs.\{-}\ze\1')
452						if filereadable(sname)
453							let b:js_extfiles += readfile(sname)
454						endif
455					endif
456				endwhile
457				" 2. Find at least one <script> tag
458				call cursor(1,1)
459				let js_scripttags = []
460				while search('<script\>', 'W') && line('.') < l
461					if matchstr(getline('.'), '<script[^>]*src') == ''
462						let js_scripttag = getline(line('.'), search('</script>', 'W'))
463						let js_scripttags += js_scripttag
464					endif
465				endwhile
466				let b:js_extfiles += js_scripttags
467
468				" 3. Proper call for javascriptcomplete#CompleteJS
469				call cursor(l,c)
470				let js_context = matchstr(a:base, '\k\+$')
471				let js_shortcontext = substitute(a:base, js_context.'$', '', '')
472				let b:compl_context = context
473				let b:jsrange = [l, l]
474				unlet! l c
475				return javascriptcomplete#CompleteJS(0, js_context)
476
477			endif
478
479			" }}}
480			let stripbase = matchstr(context, ".*\\(on[a-zA-Z]*\\|style\\|class\\)\\s*=\\s*[\"']\\zs.*")
481			" Now we have context stripped from all chars up to style/class.
482			" It may fail with some strange style value combinations.
483			if stripbase !~ "[\"']"
484				return []
485			endif
486		endif
487		" Value of attribute completion {{{
488		" If attr contains =\s*[\"'] we catched value of attribute
489		if attr =~ "=\s*[\"']" || attr =~ "=\s*$"
490			" Let do attribute specific completion
491			let attrname = matchstr(attr, '.*\ze\s*=')
492			let entered_value = matchstr(attr, ".*=\\s*[\"']\\?\\zs.*")
493			let values = []
494			" Load data {{{
495			if !exists("b:html_doctype")
496				call htmlcomplete#CheckDoctype()
497			endif
498			if !exists("b:html_omni")
499				"runtime! autoload/xml/xhtml10s.vim
500				call htmlcomplete#LoadData()
501			endif
502			" }}}
503			if attrname == 'href'
504				" Now we are looking for local anchors defined by name or id
505				if entered_value =~ '^#'
506					let file = join(getline(1, line('$')), ' ')
507					" Split it be sure there will be one id/name element in
508					" item, it will be also first word [a-zA-Z0-9_-] in element
509					let oneelement = split(file, "\\(meta \\)\\@<!\\(name\\|id\\)\\s*=\\s*[\"']")
510					for i in oneelement
511						let values += ['#'.matchstr(i, "^[a-zA-Z][a-zA-Z0-9%_-]*")]
512					endfor
513				endif
514			else
515				if has_key(b:html_omni, tag) && has_key(b:html_omni[tag][1], attrname)
516					let values = b:html_omni[tag][1][attrname]
517				else
518					return []
519				endif
520			endif
521
522			if len(values) == 0
523				return []
524			endif
525
526			" We need special version of sbase
527			let attrbase = matchstr(context, ".*[\"']")
528			let attrquote = matchstr(attrbase, '.$')
529			if attrquote !~ "['\"]"
530				let attrquoteopen = '"'
531				let attrquote = '"'
532			else
533				let attrquoteopen = ''
534			endif
535
536			for m in values
537				" This if is needed to not offer all completions as-is
538				" alphabetically but sort them. Those beginning with entered
539				" part will be as first choices
540				if m =~ '^'.entered_value
541					call add(res, attrquoteopen . m . attrquote)
542				elseif m =~ entered_value
543					call add(res2, attrquoteopen . m . attrquote)
544				endif
545			endfor
546
547			return res + res2
548
549		endif
550		" }}}
551		" Attribute completion {{{
552		" Shorten context to not include last word
553		let sbase = matchstr(context, '.*\ze\s.*')
554
555		" Load data {{{
556		if !exists("b:html_doctype")
557			call htmlcomplete#CheckDoctype()
558		endif
559		if !exists("b:html_omni")
560			call htmlcomplete#LoadData()
561		endif
562		" }}}
563
564		if has_key(b:html_omni, tag)
565			let attrs = keys(b:html_omni[tag][1])
566		else
567			return []
568		endif
569
570		for m in sort(attrs)
571			if m =~ '^'.attr
572				call add(res, m)
573			elseif m =~ attr
574				call add(res2, m)
575			endif
576		endfor
577		let menu = res + res2
578		if has_key(b:html_omni, 'vimxmlattrinfo')
579			let final_menu = []
580			for i in range(len(menu))
581				let item = menu[i]
582				if has_key(b:html_omni['vimxmlattrinfo'], item)
583					let m_menu = b:html_omni['vimxmlattrinfo'][item][0]
584					let m_info = b:html_omni['vimxmlattrinfo'][item][1]
585				else
586					let m_menu = ''
587					let m_info = ''
588				endif
589				if len(b:html_omni[tag][1][item]) > 0 && b:html_omni[tag][1][item][0] =~ '^\(BOOL\|'.item.'\)$'
590					let item = item
591					let m_menu = 'Bool'
592				else
593					let item .= '="'
594				endif
595				let final_menu += [{'word':item, 'menu':m_menu, 'info':m_info}]
596			endfor
597		else
598			let final_menu = []
599			for i in range(len(menu))
600				let item = menu[i]
601				if len(b:html_omni[tag][1][item]) > 0 && b:html_omni[tag][1][item][0] =~ '^\(BOOL\|'.item.'\)$'
602					let item = item
603				else
604					let item .= '="'
605				endif
606				let final_menu += [item]
607			endfor
608			return final_menu
609
610		endif
611		return final_menu
612
613	endif
614	" }}}
615	" Close tag {{{
616	let b:unaryTagsStack = "base meta link hr br param img area input col"
617	if context =~ '^\/'
618		if context =~ '^\/.'
619			return []
620		else
621			let opentag = xmlcomplete#GetLastOpenTag("b:unaryTagsStack")
622			return [opentag.">"]
623		endif
624	endif
625	" }}}
626	" Load data {{{
627	if !exists("b:html_doctype")
628		call htmlcomplete#CheckDoctype()
629	endif
630	if !exists("b:html_omni")
631		"runtime! autoload/xml/xhtml10s.vim
632		call htmlcomplete#LoadData()
633	endif
634	" }}}
635	" Tag completion {{{
636	" Deal with tag completion.
637	let opentag = tolower(xmlcomplete#GetLastOpenTag("b:unaryTagsStack"))
638	" MM: TODO: GLOT works always the same but with some weird situation it
639	" behaves as intended in HTML but screws in PHP
640	if opentag == '' || &filetype == 'php' && !has_key(b:html_omni, opentag)
641		" Hack for sometimes failing GetLastOpenTag.
642		" As far as I tested fail isn't GLOT fault but problem
643		" of invalid document - not properly closed tags and other mish-mash.
644		" Also when document is empty. Return list of *all* tags.
645	    let tags = keys(b:html_omni)
646		call filter(tags, 'v:val !~ "^vimxml"')
647	else
648		if has_key(b:html_omni, opentag)
649			let tags = b:html_omni[opentag][0]
650		else
651			return []
652		endif
653	endif
654	" }}}
655
656	if exists("uppercase_tag") && uppercase_tag == 1
657		let context = tolower(context)
658	endif
659	" Handle XML keywords: DOCTYPE
660	if opentag == ''
661		let tags += [
662				\ '!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">',
663				\ '!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">',
664				\ '!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">',
665				\ '!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN" "http://www.w3.org/TR/REC-html40/frameset.dtd">',
666				\ '!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
667				\ '!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
668				\ '!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
669				\ '!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
670				\ '!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
671				\ '!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
672				\ '!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/1999/xhtml">'
673				\ ]
674	endif
675
676	for m in sort(tags)
677		if m =~ '^'.context
678			call add(res, m)
679		elseif m =~ context
680			call add(res2, m)
681		endif
682	endfor
683	let menu = res + res2
684	if has_key(b:html_omni, 'vimxmltaginfo')
685		let final_menu = []
686		for i in range(len(menu))
687			let item = menu[i]
688			if has_key(b:html_omni['vimxmltaginfo'], item)
689				let m_menu = b:html_omni['vimxmltaginfo'][item][0]
690				let m_info = b:html_omni['vimxmltaginfo'][item][1]
691			else
692				let m_menu = ''
693				let m_info = ''
694			endif
695			if &filetype == 'html' && exists("uppercase_tag") && uppercase_tag == 1 && item !~ 'DOCTYPE'
696				let item = toupper(item)
697			endif
698			if item =~ 'DOCTYPE'
699				let abbr = 'DOCTYPE '.matchstr(item, 'DTD \zsX\?HTML .\{-}\ze\/\/')
700			else
701				let abbr = item
702			endif
703			let final_menu += [{'abbr':abbr, 'word':item, 'menu':m_menu, 'info':m_info}]
704		endfor
705	else
706		let final_menu = menu
707	endif
708	return final_menu
709
710	" }}}
711  endif
712endfunction
713
714function! htmlcomplete#LoadData() " {{{
715	if !exists("b:html_omni_flavor")
716		if &filetype == 'html'
717			let b:html_omni_flavor = 'html401t'
718		else
719			let b:html_omni_flavor = 'xhtml10s'
720		endif
721	endif
722	" With that if we still have bloated memory but create new buffer
723	" variables only by linking to existing g:variable, not sourcing whole
724	" file.
725	if exists('g:xmldata_'.b:html_omni_flavor)
726		exe 'let b:html_omni = g:xmldata_'.b:html_omni_flavor
727	else
728		exe 'runtime! autoload/xml/'.b:html_omni_flavor.'.vim'
729		exe 'let b:html_omni = g:xmldata_'.b:html_omni_flavor
730	endif
731endfunction
732" }}}
733function! htmlcomplete#CheckDoctype() " {{{
734	if exists('b:html_omni_flavor')
735		let old_flavor = b:html_omni_flavor
736	else
737		let old_flavor = ''
738	endif
739	let i = 1
740	while i < 10 && i < line("$")
741		let line = getline(i)
742		if line =~ '<!DOCTYPE.*\<DTD HTML 3\.2'
743			let b:html_omni_flavor = 'html32'
744			let b:html_doctype = 1
745			break
746		elseif line =~ '<!DOCTYPE.*\<DTD HTML 4\.0 Transitional'
747			let b:html_omni_flavor = 'html40t'
748			let b:html_doctype = 1
749			break
750		elseif line =~ '<!DOCTYPE.*\<DTD HTML 4\.0 Frameset'
751			let b:html_omni_flavor = 'html40f'
752			let b:html_doctype = 1
753			break
754		elseif line =~ '<!DOCTYPE.*\<DTD HTML 4\.0'
755			let b:html_omni_flavor = 'html40s'
756			let b:html_doctype = 1
757			break
758		elseif line =~ '<!DOCTYPE.*\<DTD HTML 4\.01 Transitional'
759			let b:html_omni_flavor = 'html401t'
760			let b:html_doctype = 1
761			break
762		elseif line =~ '<!DOCTYPE.*\<DTD HTML 4\.01 Frameset'
763			let b:html_omni_flavor = 'html401f'
764			let b:html_doctype = 1
765			break
766		elseif line =~ '<!DOCTYPE.*\<DTD HTML 4\.01'
767			let b:html_omni_flavor = 'html401s'
768			let b:html_doctype = 1
769			break
770		elseif line =~ '<!DOCTYPE.*\<DTD XHTML 1\.0 Transitional'
771			let b:html_omni_flavor = 'xhtml10t'
772			let b:html_doctype = 1
773			break
774		elseif line =~ '<!DOCTYPE.*\<DTD XHTML 1\.0 Frameset'
775			let b:html_omni_flavor = 'xhtml10f'
776			let b:html_doctype = 1
777			break
778		elseif line =~ '<!DOCTYPE.*\<DTD XHTML 1\.0 Strict'
779			let b:html_omni_flavor = 'xhtml10s'
780			let b:html_doctype = 1
781			break
782		elseif line =~ '<!DOCTYPE.*\<DTD XHTML 1\.1'
783			let b:html_omni_flavor = 'xhtml11'
784			let b:html_doctype = 1
785			break
786		endif
787		let i += 1
788	endwhile
789	if !exists("b:html_doctype")
790		return
791	else
792		" Tie g:xmldata with b:html_omni this way we need to sourca data file only
793		" once, not every time per buffer.
794		if old_flavor == b:html_omni_flavor
795			return
796		else
797			if exists('g:xmldata_'.b:html_omni_flavor)
798				exe 'let b:html_omni = g:xmldata_'.b:html_omni_flavor
799			else
800				exe 'runtime! autoload/xml/'.b:html_omni_flavor.'.vim'
801				exe 'let b:html_omni = g:xmldata_'.b:html_omni_flavor
802			endif
803			return
804		endif
805	endif
806endfunction
807" }}}
808" vim:set foldmethod=marker:
809