1" Vim completion script
2" Language:	XHTML 1.0 Strict
3" Maintainer:	Mikolaj Machowski ( mikmach AT wp DOT pl )
4" Last Change:	2005 Oct 12
5
6function! htmlcomplete#CompleteTags(findstart, base)
7  if a:findstart
8    " locate the start of the word
9    let line = getline('.')
10    let start = col('.') - 1
11	let compl_begin = col('.') - 2
12    while start >= 0 && line[start - 1] =~ '\(\k\|[:.-]\)'
13		let start -= 1
14    endwhile
15	if start >= 0 && line[start - 1] =~ '&'
16		let b:entitiescompl = 1
17		let b:compl_context = ''
18		return start
19	endif
20	let stylestart = searchpair('<style\>', '', '<\/style\>', "bnW")
21	let styleend   = searchpair('<style\>', '', '<\/style\>', "nW")
22	if stylestart != 0 && styleend != 0
23		let curpos = line('.')
24		if stylestart <= curpos && styleend >= curpos
25			let start = col('.') - 1
26			let b:csscompl = 1
27			while start >= 0 && line[start - 1] =~ '\(\k\|-\)'
28				let start -= 1
29			endwhile
30		endif
31	endif
32	if !exists("b:csscompl")
33		let b:compl_context = getline('.')[0:(compl_begin)]
34		let b:compl_context = matchstr(b:compl_context, '.*<\zs.*')
35	else
36		let b:compl_context = getline('.')[0:compl_begin]
37	endif
38    return start
39  else
40	" Initialize base return lists
41    let res = []
42    let res2 = []
43	" a:base is very short - we need context
44	let context = b:compl_context
45	unlet! b:compl_context
46	" Check if we should do CSS completion inside of <style> tag
47	if exists("b:csscompl")
48		unlet! b:csscompl
49		return csscomplete#CompleteCSS(0, context)
50	endif
51	" Make entities completion
52	if exists("b:entitiescompl")
53		unlet! b:entitiescompl
54
55		" Very, very long line
56        let values = ["AElig", "Aacute", "Acirc", "Agrave", "Alpha", "Aring", "Atilde", "Auml", "Beta", "Ccedil", "Chi", "Dagger", "Delta", "ETH", "Eacute", "Ecirc", "Egrave", "Epsilon", "Eta", "Euml", "Gamma", "Iacute", "Icirc", "Igrave", "Iota", "Iuml", "Kappa", "Lambda", "Mu", "Ntilde", "Nu", "OElig", "Oacute", "Ocirc", "Ograve", "Omega", "Omicron", "Oslash", "Otilde", "Ouml", "Phi", "Pi", "Prime", "Psi", "Rho", "Scaron", "Sigma", "THORN", "TITY", "Tau", "Theta", "Uacute", "Ucirc", "Ugrave", "Upsilon", "Uuml", "Xi", "Yacute", "Yuml", "Zeta", "amp", "aacute", "acirc", "acute", "aelig", "agrave", "alefsym", "alpha", "and", "ang", "apos", "aring", "asymp", "atilde", "auml", "bdquo", "beta", "brvbar", "bull", "cap", "ccedil", "cedil", "cent", "chi", "circ", "clubs", "copy", "cong", "crarr", "cup", "curren", "dArr", "dagger", "darr", "deg", "delta", "diams", "divide", "eacute", "ecirc", "egrave", "empty", "ensp", "emsp", "epsilon", "equiv", "eta", "eth", "euro", "euml", "exist", "fnof", "forall", "frac12", "frac14", "frac34", "frasl", "gt", "gamma", "ge", "hArr", "harr", "hearts", "hellip", "iacute", "icirc", "iexcl", "igrave", "image", "infin", "int", "iota", "iquest", "isin", "iuml", "kappa", "lt", "laquo", "lArr", "lambda", "lang", "larr", "lceil", "ldquo", "le", "lfloor", "lowast", "loz", "lrm", "lsaquo", "lsquo", "macr", "mdash", "micro", "middot", "minus", "mu", "nbsp", "nabla", "ndash", "ne", "ni", "not", "notin", "nsub", "ntilde", "nu", "oacute", "ocirc", "oelig", "ograve", "oline", "omega", "omicron", "oplus", "or", "ordf", "ordm", "oslash", "otilde", "otimes", "ouml", "para", "part", "permil", "perp", "phi", "pi", "piv", "plusmn", "pound", "prime", "prod", "prop", "psi", "quot", "rArr", "raquo", "radic", "rang", "rarr", "rceil", "rdquo", "real", "reg", "rfloor", "rho", "rlm", "rsaquo", "rsquo", "sbquo", "scaron", "sdot", "sect", "shy", "sigma", "sigmaf", "sim", "spades", "sub", "sube", "sum", "sup", "sup1", "sup2", "sup3", "supe", "szlig", "tau", "there4", "theta", "thetasym", "thinsp", "thorn", "tilde", "times", "trade", "uArr", "uacute", "uarr", "ucirc", "ugrave", "uml", "upsih", "upsilon", "uuml", "weierp", "xi", "yacute", "yen", "yuml", "zeta", "zwj", "zwnj"]
57
58		for m in values
59			if m =~ '^'.a:base
60				call add(res, m.';')
61			endif
62		endfor
63
64		return res
65
66	endif
67	if context =~ '>'
68		" Generally if context contains > it means we are outside of tag and
69		" should abandon action - with one exception: <style> span { bo
70		if context =~ 'style[^>]\{-}>[^<]\{-}$'
71			return csscomplete#CompleteCSS(0, context)
72		else
73			return []
74		endif
75	endif
76
77	" Set attribute groups
78    let coreattrs = ["id", "class", "style", "title"]
79    let i18n = ["lang", "xml:lang", "dir=\"ltr\" ", "dir=\"rtl\" "]
80    let events = ["onclick", "ondblclick", "onmousedown", "onmouseup", "onmousemove",
81    			\ "onmouseover", "onmouseout", "onkeypress", "onkeydown", "onkeyup"]
82    let focus = ["accesskey", "tabindex", "onfocus", "onblur"]
83    let coregroup = coreattrs + i18n + events
84    " find tags matching with "context"
85	" If context contains > it means we are already outside of tag and we
86	" should abandon action
87	" If context contains white space it is attribute.
88	" It could be also value of attribute...
89	" We have to get first word to offer
90	" proper completions
91	if context == ''
92		let tag = ''
93	else
94		let tag = split(context)[0]
95	endif
96	" Get last word, it should be attr name
97	let attr = matchstr(context, '.*\s\zs.*')
98	" Possible situations where any prediction would be difficult:
99	" 1. Events attributes
100	if context =~ '\s'
101		" Sort out style, class, and on* cases
102		if context =~ "\\(on[a-z]*\\|id\\|style\\|class\\)\\s*=\\s*[\"']"
103			if context =~ "\\(id\\|class\\)\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
104				if context =~ "class\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
105					let search_for = "class"
106				elseif context =~ "id\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
107					let search_for = "id"
108				endif
109				" Handle class name completion
110				" 1. Find lines of <link stylesheet>
111				" 1a. Check file for @import
112				" 2. Extract filename(s?) of stylesheet,
113				call cursor(1,1)
114				let head = getline(search('<head\>'), search('<\/head>'))
115				let headjoined = join(copy(head), ' ')
116				if headjoined =~ '<style'
117					let stylehead = substitute(headjoined, '+>\*[,', ' ', 'g')
118					if search_for == 'class'
119						let styleheadlines = split(stylehead)
120						let headclasslines = filter(copy(styleheadlines), "v:val =~ '\\([a-zA-Z0-9:]\\+\\)\\?\\.[a-zA-Z0-9_-]\\+'")
121					else
122						let stylesheet = split(headjoined, '[{}]')
123						" Get all lines which fit id syntax
124						let classlines = filter(copy(stylesheet), "v:val =~ '#[a-zA-Z0-9_-]\\+'")
125						" Filter out possible color definitions
126						call filter(classlines, "v:val !~ ':\\s*#[a-zA-Z0-9_-]\\+'")
127						" Filter out complex border definitions
128						call filter(classlines, "v:val !~ '\\(none\\|hidden\\|dotted\\|dashed\\|solid\\|double\\|groove\\|ridge\\|inset\\|outset\\)\\s*#[a-zA-Z0-9_-]\\+'")
129						let templines = join(classlines, ' ')
130						let headclasslines = split(templines)
131						call filter(headclasslines, "v:val =~ '#[a-zA-Z0-9_-]\\+'")
132					endif
133					let internal = 1
134				else
135					let internal = 0
136				endif
137				let styletable = []
138				let secimportfiles = []
139				let filestable = filter(copy(head), "v:val =~ '\\(@import\\|link.*stylesheet\\)'")
140				for line in filestable
141					if line =~ "@import"
142						let styletable += [matchstr(line, "import\\s\\+\\(url(\\)\\?[\"']\\?\\zs\\f\\+\\ze")]
143					elseif line =~ "<link"
144						let styletable += [matchstr(line, "href\\s*=\\s*[\"']\\zs\\f\\+\\ze")]
145					endif
146				endfor
147				for file in styletable
148					if filereadable(file)
149						let stylesheet = readfile(file)
150						let secimport = filter(copy(stylesheet), "v:val =~ '@import'")
151						if len(secimport) > 0
152							for line in secimport
153								let secfile = matchstr(line, "import\\s\\+\\(url(\\)\\?[\"']\\?\\zs\\f\\+\\ze")
154								let secfile = fnamemodify(file, ":p:h").'/'.secfile
155								let secimportfiles += [secfile]
156							endfor
157						endif
158					endif
159				endfor
160				let cssfiles = styletable + secimportfiles
161				let classes = []
162				for file in cssfiles
163					if filereadable(file)
164						let stylesheet = readfile(file)
165						let stylefile = join(stylesheet, ' ')
166						let stylefile = substitute(stylefile, '+>\*[,', ' ', 'g')
167						if search_for == 'class'
168							let stylesheet = split(stylefile)
169							let classlines = filter(copy(stylesheet), "v:val =~ '\\([a-zA-Z0-9:]\\+\\)\\?\\.[a-zA-Z0-9_-]\\+'")
170						else
171							let stylesheet = split(stylefile, '[{}]')
172							" Get all lines which fit id syntax
173							let classlines = filter(copy(stylesheet), "v:val =~ '#[a-zA-Z0-9_-]\\+'")
174							" Filter out possible color definitions
175							call filter(classlines, "v:val !~ ':\\s*#[a-zA-Z0-9_-]\\+'")
176							" Filter out complex border definitions
177							call filter(classlines, "v:val !~ '\\(none\\|hidden\\|dotted\\|dashed\\|solid\\|double\\|groove\\|ridge\\|inset\\|outset\\)\\s*#[a-zA-Z0-9_-]\\+'")
178							let templines = join(classlines, ' ')
179							let stylelines = split(templines)
180							let classlines = filter(stylelines, "v:val =~ '#[a-zA-Z0-9_-]\\+'")
181
182						endif
183					endif
184					" We gathered classes definitions from all external files
185					let classes += classlines
186				endfor
187				if internal == 1
188					let classes += headclasslines
189				endif
190
191				if search_for == 'class'
192					let elements = {}
193					for element in classes
194						if element =~ '^\.'
195							let class = matchstr(element, '^\.\zs[a-zA-Z][a-zA-Z0-9_-]*\ze')
196							let class = substitute(class, ':.*', '', '')
197							if has_key(elements, 'common')
198								let elements['common'] .= ' '.class
199							else
200								let elements['common'] = class
201							endif
202						else
203							let class = matchstr(element, '[a-zA-Z1-6]*\.\zs[a-zA-Z][a-zA-Z0-9_-]*\ze')
204							let tagname = tolower(matchstr(element, '[a-zA-Z1-6]*\ze.'))
205							if tagname != ''
206								if has_key(elements, tagname)
207									let elements[tagname] .= ' '.class
208								else
209									let elements[tagname] = class
210								endif
211							endif
212						endif
213					endfor
214
215					if has_key(elements, tag) && has_key(elements, 'common')
216						let values = split(elements[tag]." ".elements['common'])
217					elseif has_key(elements, tag) && !has_key(elements, 'common')
218						let values = split(elements[tag])
219					elseif !has_key(elements, tag) && has_key(elements, 'common')
220						let values = split(elements['common'])
221					else
222						return []
223					endif
224
225				elseif search_for == 'id'
226					" Find used IDs
227					" 1. Catch whole file
228					let filelines = getline(1, line('$'))
229					" 2. Find lines with possible id
230					let used_id_lines = filter(filelines, 'v:val =~ "id\\s*=\\s*[\"''][a-zA-Z0-9_-]\\+"')
231					" 3a. Join all filtered lines
232					let id_string = join(used_id_lines, ' ')
233					" 3b. And split them to be sure each id is in separate item
234					let id_list = split(id_string, 'id\s*=\s*')
235					" 4. Extract id values
236					let used_id = map(id_list, 'matchstr(v:val, "[\"'']\\zs[a-zA-Z0-9_-]\\+\\ze")')
237					let joined_used_id = ','.join(used_id, ',').','
238
239					let allvalues = map(classes, 'matchstr(v:val, ".*#\\zs[a-zA-Z0-9_-]\\+")')
240
241					let values = []
242
243					for element in classes
244						if joined_used_id !~ ','.element.','
245							let values += [element]
246						endif
247
248					endfor
249
250				endif
251
252				" We need special version of sbase
253				let classbase = matchstr(context, ".*[\"']")
254				let classquote = matchstr(classbase, '.$')
255
256				let entered_class = matchstr(attr, ".*=\\s*[\"']\\zs.*")
257
258				for m in sort(values)
259					if m =~? '^'.entered_class
260						call add(res, m . classquote)
261					elseif m =~? entered_class
262						call add(res2, m . classquote)
263					endif
264				endfor
265
266				return res + res2
267
268			elseif context =~ "style\\s*=\\s*[\"'][^\"']*$"
269				return csscomplete#CompleteCSS(0, context)
270
271			endif
272			let stripbase = matchstr(context, ".*\\(on[a-z]*\\|style\\|class\\)\\s*=\\s*[\"']\\zs.*")
273			" Now we have context stripped from all chars up to style/class.
274			" It may fail with some strange style value combinations.
275			if stripbase !~ "[\"']"
276				return []
277			endif
278		endif
279		" If attr contains =\s*[\"'] we catched value of attribute
280		if attr =~ "=\s*[\"']"
281			" Let do attribute specific completion
282			let attrname = matchstr(attr, '.*\ze\s*=')
283			let entered_value = matchstr(attr, ".*=\\s*[\"']\\zs.*")
284			let values = []
285			if attrname == 'media'
286				let values = ["screen", "tty", "tv", "projection", "handheld", "print", "braille", "aural", "all"]
287			elseif attrname == 'xml:space'
288				let values = ["preserve"]
289			elseif attrname == 'shape'
290				let values = ["rect", "circle", "poly", "default"]
291			elseif attrname == 'valuetype'
292				let values = ["data", "ref", "object"]
293			elseif attrname == 'method'
294				let values = ["get", "post"]
295			elseif attrname == 'dir'
296				let values = ["ltr", "rtl"]
297			elseif attrname == 'frame'
298				let values = ["void", "above", "below", "hsides", "lhs", "rhs", "vsides", "box", "border"]
299			elseif attrname == 'rules'
300				let values = ["none", "groups", "rows", "all"]
301			elseif attrname == 'align'
302				let values = ["left", "center", "right", "justify", "char"]
303			elseif attrname == 'valign'
304				let values = ["top", "middle", "bottom", "baseline"]
305			elseif attrname == 'scope'
306				let values = ["row", "col", "rowgroup", "colgroup"]
307			elseif attrname == 'href'
308				" Now we are looking for local anchors defined by name or id
309				if entered_value =~ '^#'
310					let file = join(getline(1, line('$')), ' ')
311					" Split it be sure there will be one id/name element in
312					" item, it will be also first word [a-zA-Z0-9_-] in element
313					let oneelement = split(file, "\\(meta \\)\\@<!\\(name\\|id\\)\\s*=\\s*[\"']")
314					for i in oneelement
315						let values += ['#'.matchstr(i, "^[a-zA-Z][a-zA-Z0-9%_-]*")]
316					endfor
317				endif
318			elseif attrname == 'type'
319				if context =~ '^input'
320					let values = ["text", "password", "checkbox", "radio", "submit", "reset", "file", "hidden", "image", "button"]
321				elseif context =~ '^button'
322					let values = ["button", "submit", "reset"]
323				elseif context =~ '^style'
324					let values = ["text/css"]
325				elseif context =~ '^script'
326					let values = ["text/javascript"]
327				endif
328			else
329				return []
330			endif
331
332			if len(values) == 0
333				return []
334			endif
335
336			" We need special version of sbase
337			let attrbase = matchstr(context, ".*[\"']")
338			let attrquote = matchstr(attrbase, '.$')
339
340			for m in values
341				" This if is needed to not offer all completions as-is
342				" alphabetically but sort them. Those beginning with entered
343				" part will be as first choices
344				if m =~ '^'.entered_value
345					call add(res, m . attrquote.' ')
346				elseif m =~ entered_value
347					call add(res2, m . attrquote.' ')
348				endif
349			endfor
350
351			return res + res2
352
353		endif
354		" Shorten context to not include last word
355		let sbase = matchstr(context, '.*\ze\s.*')
356		if tag =~ '^\(abbr\|acronym\|address\|b\|bdo\|big\|caption\|cite\|code\|dd\|dfn\|div\|dl\|dt\|em\|fieldset\|h\d\|hr\|i\|kbd\|li\|noscript\|ol\|p\|samp\|small\|span\|strong\|sub\|sup\|tt\|ul\|var\)$'
357			let attrs = coregroup
358		elseif tag == 'a'
359			let attrs = coregroup + focus + ["charset", "type", "name", "href", "hreflang", "rel", "rev", "shape", "coords"]
360		elseif tag == 'area'
361			let attrs = coregroup + focus + ["shape", "coords", "href", "nohref", "alt"]
362		elseif tag == 'base'
363			let attrs = ["href", "id"]
364		elseif tag == 'blockquote'
365			let attrs = coregroup + ["cite"]
366		elseif tag == 'body'
367			let attrs = coregroup + ["onload", "onunload"]
368		elseif tag == 'br'
369			let attrs = coreattrs
370		elseif tag == 'button'
371			let attrs = coregroup + focus + ["name", "value", "type"]
372		elseif tag == '^\(col\|colgroup\)$'
373			let attrs = coregroup + ["span", "width", "align", "char", "charoff", "valign"]
374		elseif tag =~ '^\(del\|ins\)$'
375			let attrs = coregroup + ["cite", "datetime"]
376		elseif tag == 'form'
377			let attrs = coregroup + ["action", "method=\"get\" ", "method=\"post\" ", "enctype", "onsubmit", "onreset", "accept", "accept-charset"]
378		elseif tag == 'head'
379			let attrs = i18n + ["id", "profile"]
380		elseif tag == 'html'
381			let attrs = i18n + ["id", "xmlns"]
382		elseif tag == 'img'
383			let attrs = coregroup + ["src", "alt", "longdesc", "height", "width", "usemap", "ismap"]
384		elseif tag == 'input'
385			let attrs = coregroup + ["type", "name", "value", "checked", "disabled", "readonly", "size", "maxlength", "src", "alt", "usemap", "onselect", "onchange", "accept"]
386		elseif tag == 'label'
387			let attrs = coregroup + ["for", "accesskey", "onfocus", "onblur"]
388		elseif tag == 'legend'
389			let attrs = coregroup + ["accesskey"]
390		elseif tag == 'link'
391			let attrs = coregroup + ["charset", "href", "hreflang", "type", "rel", "rev", "media"]
392		elseif tag == 'map'
393			let attrs = i18n + events + ["id", "class", "style", "title", "name"]
394		elseif tag == 'meta'
395			let attrs = i18n + ["id", "http-equiv", "content", "scheme", "name"]
396		elseif tag == 'title'
397			let attrs = i18n + ["id"]
398		elseif tag == 'object'
399			let attrs = coregroup + ["declare", "classid", "codebase", "data", "type", "codetype", "archive", "standby", "height", "width", "usemap", "name", "tabindex"]
400		elseif tag == 'optgroup'
401			let attrs = coregroup + ["disbled", "label"]
402		elseif tag == 'option'
403			let attrs = coregroup + ["disbled", "selected", "value", "label"]
404		elseif tag == 'param'
405			let attrs = ["id", "name", "value", "valuetype", "type"]
406		elseif tag == 'pre'
407			let attrs = coregroup + ["xml:space"]
408		elseif tag == 'q'
409			let attrs = coregroup + ["cite"]
410		elseif tag == 'script'
411			let attrs = ["id", "charset", "type=\"text/javascript\"", "type", "src", "defer", "xml:space"]
412		elseif tag == 'select'
413			let attrs = coregroup + ["name", "size", "multiple", "disabled", "tabindex", "onfocus", "onblur", "onchange"]
414		elseif tag == 'style'
415			let attrs = coreattrs + ["id", "type=\"text/css\"", "type", "media", "title", "xml:space"]
416		elseif tag == 'table'
417			let attrs = coregroup + ["summary", "width", "border", "frame", "rules", "cellspacing", "cellpadding"]
418		elseif tag =~ '^\(thead\|tfoot\|tbody\|tr\)$'
419			let attrs = coregroup + ["align", "char", "charoff", "valign"]
420		elseif tag == 'textarea'
421			let attrs = coregroup + ["name", "rows", "cols", "disabled", "readonly", "onselect", "onchange"]
422		elseif tag =~ '^\(th\|td\)$'
423			let attrs = coregroup + ["abbr", "headers", "scope", "rowspan", "colspan", "align", "char", "charoff", "valign"]
424		else
425			return []
426		endif
427
428		for m in sort(attrs)
429			if m =~ '^'.attr
430				if m =~ '^\(ismap\|defer\|declare\|nohref\|checked\|disabled\|selected\|readonly\)$' || m =~ '='
431					call add(res, m)
432				else
433					call add(res, m.'="')
434				endif
435			elseif m =~ attr
436				if m =~ '^\(ismap\|defer\|declare\|nohref\|checked\|disabled\|selected\|readonly\)$' || m =~ '='
437					call add(res2, m)
438				else
439					call add(res2, m.'="')
440				endif
441			endif
442		endfor
443
444		return res + res2
445
446	endif
447	" Close tag
448	let b:unaryTagsStack = "base meta link hr br param img area input col"
449	if context =~ '^\/'
450		let opentag = htmlcomplete#GetLastOpenTag("b:unaryTagsStack")
451		return [opentag.">"]
452	endif
453	" Deal with tag completion.
454	let opentag = htmlcomplete#GetLastOpenTag("b:unaryTagsStack")
455	" Clusters
456	let special = "br span bdo map object img"
457	let phrase =  "em strong dfn code q samp kbd var cite abbr acronym sub sup"
458	let inlineforms = "input select textarea label button"
459	let miscinline = "ins del script"
460	let inline = "a ".special." ".phrase." ".inlineforms." tt i b big small"
461	let misc = "noscript ".miscinline
462	let block = "p h1 h2 h3 h4 h5 h6 div ul ol dl pre hr blockquote address fieldset table"
463
464	if opentag == 'a'
465		let tags = split("tt i b big small ".special." ".phrase." ".inlineforms." ".miscinline)
466	elseif opentag =~ '^\(abbr\|acronym\|address\|b\|p\|h\d\|dt\|span\|bdo\|em\|strong\|dfn\|code\|samp\|kbd\|var\|cite\|q\|sub\|sup\|tt\|i\|big\|small\|label\|caption\)$'
467		let tags = split(inline." ".miscinline)
468	elseif opentag == 'pre'
469		let tags = split("a tt i b big small br span bdo map ".phrase." ".miscinline." ".inlineforms)
470	elseif opentag == 'html'
471		let tags = ["head", "body"]
472	elseif opentag == 'legend'
473		let tags = split(inline." ".miscinline)
474	elseif opentag == 'head'
475		let tags = ["title", "base", "scipt", "style", "meta", "link", "object"]
476	elseif opentag =~ '^\(noscript\|body\|blockquote\)$'
477		let tags = split("form ".block." ".misc)
478	elseif opentag =~ '^\(ul\|ol\)$'
479		let tags = ["li"]
480	elseif opentag == 'dl'
481		let tags = ["dt", "dd"]
482	elseif opentag =~ '^\(ins\|del\|th\|td\|dd\|div\|li\)$'
483		let tags = split("form ".block." ".inline." ".misc)
484	elseif opentag == 'object'
485		let tags = split("param form ".block." ".inline." ".misc)
486	elseif opentag == 'fieldset'
487		let tags = split("legend form ".block." ".inline." ".misc)
488	elseif opentag == 'map'
489		let tags = split("area form ".block." ".misc)
490	elseif opentag == 'form'
491		let tags = split(block." ".misc)
492	elseif opentag == 'select'
493		let tags = ["optgroup", "option"]
494	elseif opentag == 'optgroup'
495		let tags = ["option"]
496	elseif opentag == 'colgroup'
497		let tags = ["col"]
498	elseif opentag == '^\(textarea\|option\|script\|style\|title\)$'
499		let tags = ['empty']
500	elseif opentag == 'button'
501		let tags = ["p", "h1", "h2", "h3", "h4", "h5", "h6", "div", "ul", "ol", "dl", "table"]
502	elseif opentag =~ '^\(thead\|tfoot\|tbody\)$'
503		let tags = ["tr"]
504	elseif opentag == 'tr'
505		let tags = ["th", "td"]
506	elseif opentag == 'table'
507		let tags = ["caption", "col", "colgroup", "thead", "tfoot", "tbody", "tr"]
508	else
509		return []
510	endif
511
512	for m in tags
513		if m =~ '^'.context
514			call add(res, m)
515		elseif m =~ context
516			call add(res2, m)
517		endif
518	endfor
519
520	return res + res2
521
522  endif
523endfunction
524
525" MM: This is greatly reduced closetag.vim used with kind permission of Steven
526"     Mueller
527"     Changes: strip all comments; delete error messages
528" Author: Steven Mueller <[email protected]>
529" Last Modified: Tue May 24 13:29:48 PDT 2005
530" Version: 0.9.1
531
532function! htmlcomplete#GetLastOpenTag(unaryTagsStack)
533	let linenum=line('.')
534	let lineend=col('.') - 1 " start: cursor position
535	let first=1              " flag for first line searched
536	let b:TagStack=''        " main stack of tags
537	let startInComment=s:InComment()
538
539	let tagpat='</\=\(\k\|[-:]\)\+\|/>'
540	while (linenum>0)
541		let line=getline(linenum)
542		if first
543			let line=strpart(line,0,lineend)
544		else
545			let lineend=strlen(line)
546		endif
547		let b:lineTagStack=''
548		let mpos=0
549		let b:TagCol=0
550		while (mpos > -1)
551			let mpos=matchend(line,tagpat)
552			if mpos > -1
553				let b:TagCol=b:TagCol+mpos
554				let tag=matchstr(line,tagpat)
555
556				if exists('b:closetag_disable_synID') || startInComment==s:InCommentAt(linenum, b:TagCol)
557					let b:TagLine=linenum
558					call s:Push(matchstr(tag,'[^<>]\+'),'b:lineTagStack')
559				endif
560				let lineend=lineend-mpos
561				let line=strpart(line,mpos,lineend)
562			endif
563		endwhile
564		while (!s:EmptystackP('b:lineTagStack'))
565			let tag=s:Pop('b:lineTagStack')
566			if match(tag, '^/') == 0		"found end tag
567				call s:Push(tag,'b:TagStack')
568			elseif s:EmptystackP('b:TagStack') && !s:Instack(tag, a:unaryTagsStack)	"found unclosed tag
569				return tag
570			else
571				let endtag=s:Peekstack('b:TagStack')
572				if endtag == '/'.tag || endtag == '/'
573					call s:Pop('b:TagStack')	"found a open/close tag pair
574				elseif !s:Instack(tag, a:unaryTagsStack) "we have a mismatch error
575					return ''
576				endif
577			endif
578		endwhile
579		let linenum=linenum-1 | let first=0
580	endwhile
581return ''
582endfunction
583
584function! s:InComment()
585	return synIDattr(synID(line('.'), col('.'), 0), 'name') =~ 'Comment'
586endfunction
587
588function! s:InCommentAt(line, col)
589	return synIDattr(synID(a:line, a:col, 0), 'name') =~ 'Comment'
590endfunction
591
592function! s:SetKeywords()
593	let g:IsKeywordBak=&iskeyword
594	let &iskeyword='33-255'
595endfunction
596
597function! s:RestoreKeywords()
598	let &iskeyword=g:IsKeywordBak
599endfunction
600
601function! s:Push(el, sname)
602	if !s:EmptystackP(a:sname)
603		exe 'let '.a:sname."=a:el.' '.".a:sname
604	else
605		exe 'let '.a:sname.'=a:el'
606	endif
607endfunction
608
609function! s:EmptystackP(sname)
610	exe 'let stack='.a:sname
611	if match(stack,'^ *$') == 0
612		return 1
613	else
614		return 0
615	endif
616endfunction
617
618function! s:Instack(el, sname)
619	exe 'let stack='.a:sname
620	call s:SetKeywords()
621	let m=match(stack, '\<'.a:el.'\>')
622	call s:RestoreKeywords()
623	if m < 0
624		return 0
625	else
626		return 1
627	endif
628endfunction
629
630function! s:Peekstack(sname)
631	call s:SetKeywords()
632	exe 'let stack='.a:sname
633	let top=matchstr(stack, '\<.\{-1,}\>')
634	call s:RestoreKeywords()
635	return top
636endfunction
637
638function! s:Pop(sname)
639	if s:EmptystackP(a:sname)
640		return ''
641	endif
642	exe 'let stack='.a:sname
643	call s:SetKeywords()
644	let loc=matchend(stack,'\<.\{-1,}\>')
645	exe 'let '.a:sname.'=strpart(stack, loc+1, strlen(stack))'
646	let top=strpart(stack, match(stack, '\<'), loc)
647	call s:RestoreKeywords()
648	return top
649endfunction
650
651function! s:Clearstack(sname)
652	exe 'let '.a:sname."=''"
653endfunction
654