1" Vim completion script
2" Language:	XHTML 1.0 Strict
3" Maintainer:	Mikolaj Machowski ( mikmach AT wp DOT pl )
4" Last Change:	2005 Oct 9
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", "aacute", "acirc", "acute", "aelig", "agrave", "alefsym", "alpha", "amp", "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 sort(values)
59			if m =~? '^'.a:base
60				call add(res, m.';')
61			elseif m =~? a:base
62				call add(res2, m.';')
63			endif
64		endfor
65
66		return res + res2
67
68	endif
69	if context =~ '>'
70		" Generally if context contains > it means we are outside of tag and
71		" should abandon action - with one exception: <style> span { bo
72		if context =~ 'style[^>]\{-}>[^<]\{-}$'
73			return csscomplete#CompleteCSS(0, context)
74		else
75			return []
76		endif
77	endif
78
79	" Set attribute groups
80    let coreattrs = ["id", "class", "style", "title"]
81    let i18n = ["lang", "xml:lang", "dir=\"ltr\" ", "dir=\"rtl\" "]
82    let events = ["onclick", "ondblclick", "onmousedown", "onmouseup", "onmousemove",
83    			\ "onmouseover", "onmouseout", "onkeypress", "onkeydown", "onkeyup"]
84    let focus = ["accesskey", "tabindex", "onfocus", "onblur"]
85    let coregroup = coreattrs + i18n + events
86    " find tags matching with "context"
87	" If context contains > it means we are already outside of tag and we
88	" should abandon action
89	" If context contains white space it is attribute.
90	" It could be also value of attribute...
91	" We have to get first word to offer
92	" proper completions
93	if context == ''
94		let tag = ''
95	else
96		let tag = split(context)[0]
97	endif
98	" Get last word, it should be attr name
99	let attr = matchstr(context, '.*\s\zs.*')
100	" Possible situations where any prediction would be difficult:
101	" 1. Events attributes
102	if context =~ '\s'
103		" Sort out style, class, and on* cases
104		if context =~ "\\(on[a-z]*\\|id\\|style\\|class\\)\\s*=\\s*[\"']"
105			if context =~ "\\(id\\|class\\)\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
106				if context =~ "class\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
107					let search_for = "class"
108				elseif context =~ "id\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
109					let search_for = "id"
110				endif
111				" Handle class name completion
112				" 1. Find lines of <link stylesheet>
113				" 1a. Check file for @import
114				" 2. Extract filename(s?) of stylesheet,
115				call cursor(1,1)
116				let head = getline(search('<head\>'), search('<\/head>'))
117				let headjoined = join(copy(head), ' ')
118				if headjoined =~ '<style'
119					let stylehead = substitute(headjoined, '+>\*[,', ' ', 'g')
120					if search_for == 'class'
121						let styleheadlines = split(stylehead)
122						let headclasslines = filter(copy(styleheadlines), "v:val =~ '\\([a-zA-Z0-9:]\\+\\)\\?\\.[a-zA-Z0-9_-]\\+'")
123					else
124						let stylesheet = split(headjoined, '[{}]')
125						" Get all lines which fit id syntax
126						let classlines = filter(copy(stylesheet), "v:val =~ '#[a-zA-Z0-9_-]\\+'")
127						" Filter out possible color definitions
128						call filter(classlines, "v:val !~ ':\\s*#[a-zA-Z0-9_-]\\+'")
129						" Filter out complex border definitions
130						call filter(classlines, "v:val !~ '\\(none\\|hidden\\|dotted\\|dashed\\|solid\\|double\\|groove\\|ridge\\|inset\\|outset\\)\\s*#[a-zA-Z0-9_-]\\+'")
131						let templines = join(classlines, ' ')
132						let headclasslines = split(templines)
133						call filter(headclasslines, "v:val =~ '#[a-zA-Z0-9_-]\\+'")
134					endif
135					let internal = 1
136				else
137					let internal = 0
138				endif
139				let styletable = []
140				let secimportfiles = []
141				let filestable = filter(copy(head), "v:val =~ '\\(@import\\|link.*stylesheet\\)'")
142				for line in filestable
143					if line =~ "@import"
144						let styletable += [matchstr(line, "import\\s\\+\\(url(\\)\\?[\"']\\?\\zs\\f\\+\\ze")]
145					elseif line =~ "<link"
146						let styletable += [matchstr(line, "href\\s*=\\s*[\"']\\zs\\f\\+\\ze")]
147					endif
148				endfor
149				for file in styletable
150					if filereadable(file)
151						let stylesheet = readfile(file)
152						let secimport = filter(copy(stylesheet), "v:val =~ '@import'")
153						if len(secimport) > 0
154							for line in secimport
155								let secfile = matchstr(line, "import\\s\\+\\(url(\\)\\?[\"']\\?\\zs\\f\\+\\ze")
156								let secfile = fnamemodify(file, ":p:h").'/'.secfile
157								let secimportfiles += [secfile]
158							endfor
159						endif
160					endif
161				endfor
162				let cssfiles = styletable + secimportfiles
163				let classes = []
164				for file in cssfiles
165					if filereadable(file)
166						let stylesheet = readfile(file)
167						let stylefile = join(stylesheet, ' ')
168						let stylefile = substitute(stylefile, '+>\*[,', ' ', 'g')
169						if search_for == 'class'
170							let stylesheet = split(stylefile)
171							let classlines = filter(copy(stylesheet), "v:val =~ '\\([a-zA-Z0-9:]\\+\\)\\?\\.[a-zA-Z0-9_-]\\+'")
172						else
173							let stylesheet = split(stylefile, '[{}]')
174							" Get all lines which fit id syntax
175							let classlines = filter(copy(stylesheet), "v:val =~ '#[a-zA-Z0-9_-]\\+'")
176							" Filter out possible color definitions
177							call filter(classlines, "v:val !~ ':\\s*#[a-zA-Z0-9_-]\\+'")
178							" Filter out complex border definitions
179							call filter(classlines, "v:val !~ '\\(none\\|hidden\\|dotted\\|dashed\\|solid\\|double\\|groove\\|ridge\\|inset\\|outset\\)\\s*#[a-zA-Z0-9_-]\\+'")
180							let templines = join(classlines, ' ')
181							let stylelines = split(templines)
182							let classlines = filter(stylelines, "v:val =~ '#[a-zA-Z0-9_-]\\+'")
183
184						endif
185					endif
186					" We gathered classes definitions from all external files
187					let classes += classlines
188				endfor
189				if internal == 1
190					let classes += headclasslines
191				endif
192
193				if search_for == 'class'
194					let elements = {}
195					for element in classes
196						if element =~ '^\.'
197							let class = matchstr(element, '^\.\zs[a-zA-Z][a-zA-Z0-9_-]*\ze')
198							let class = substitute(class, ':.*', '', '')
199							if has_key(elements, 'common')
200								let elements['common'] .= ' '.class
201							else
202								let elements['common'] = class
203							endif
204						else
205							let class = matchstr(element, '[a-zA-Z1-6]*\.\zs[a-zA-Z][a-zA-Z0-9_-]*\ze')
206							let tagname = tolower(matchstr(element, '[a-zA-Z1-6]*\ze.'))
207							if tagname != ''
208								if has_key(elements, tagname)
209									let elements[tagname] .= ' '.class
210								else
211									let elements[tagname] = class
212								endif
213							endif
214						endif
215					endfor
216
217					if has_key(elements, tag) && has_key(elements, 'common')
218						let values = split(elements[tag]." ".elements['common'])
219					elseif has_key(elements, tag) && !has_key(elements, 'common')
220						let values = split(elements[tag])
221					elseif !has_key(elements, tag) && has_key(elements, 'common')
222						let values = split(elements['common'])
223					else
224						return []
225					endif
226
227				elseif search_for == 'id'
228					" Find used IDs
229					" 1. Catch whole file
230					let filelines = getline(1, line('$'))
231					" 2. Find lines with possible id
232					let used_id_lines = filter(filelines, 'v:val =~ "id\\s*=\\s*[\"''][a-zA-Z0-9_-]\\+"')
233					" 3a. Join all filtered lines
234					let id_string = join(used_id_lines, ' ')
235					" 3b. And split them to be sure each id is in separate item
236					let id_list = split(id_string, 'id\s*=\s*')
237					" 4. Extract id values
238					let used_id = map(id_list, 'matchstr(v:val, "[\"'']\\zs[a-zA-Z0-9_-]\\+\\ze")')
239					let joined_used_id = ','.join(used_id, ',').','
240
241					let allvalues = map(classes, 'matchstr(v:val, ".*#\\zs[a-zA-Z0-9_-]\\+")')
242
243					let values = []
244
245					for element in classes
246						if joined_used_id !~ ','.element.','
247							let values += [element]
248						endif
249
250					endfor
251
252				endif
253
254				" We need special version of sbase
255				let classbase = matchstr(context, ".*[\"']")
256				let classquote = matchstr(classbase, '.$')
257
258				let entered_class = matchstr(attr, ".*=\\s*[\"']\\zs.*")
259
260				for m in sort(values)
261					if m =~? '^'.entered_class
262						call add(res, m . classquote)
263					elseif m =~? entered_class
264						call add(res2, m . classquote)
265					endif
266				endfor
267
268				return res + res2
269
270			elseif context =~ "style\\s*=\\s*[\"'][^\"']*$"
271				return csscomplete#CompleteCSS(0, context)
272
273			endif
274			let stripbase = matchstr(context, ".*\\(on[a-z]*\\|style\\|class\\)\\s*=\\s*[\"']\\zs.*")
275			" Now we have context stripped from all chars up to style/class.
276			" It may fail with some strange style value combinations.
277			if stripbase !~ "[\"']"
278				return []
279			endif
280		endif
281		" If attr contains =\s*[\"'] we catched value of attribute
282		if attr =~ "=\s*[\"']"
283			" Let do attribute specific completion
284			let attrname = matchstr(attr, '.*\ze\s*=')
285			let entered_value = matchstr(attr, ".*=\\s*[\"']\\zs.*")
286			let values = []
287			if attrname == 'media'
288				let values = ["screen", "tty", "tv", "projection", "handheld", "print", "braille", "aural", "all"]
289			elseif attrname == 'xml:space'
290				let values = ["preserve"]
291			elseif attrname == 'shape'
292				if context =~ '^a\>'
293					let values = ["rect"]
294				else
295					let values = ["rect", "circle", "poly", "default"]
296				endif
297			elseif attrname == 'valuetype'
298				let values = ["data", "ref", "object"]
299			elseif attrname == 'method'
300				let values = ["get", "post"]
301			elseif attrname == 'dir'
302				let values = ["ltr", "rtl"]
303			elseif attrname == 'frame'
304				let values = ["void", "above", "below", "hsides", "lhs", "rhs", "vsides", "box", "border"]
305			elseif attrname == 'rules'
306				let values = ["none", "groups", "rows", "all"]
307			elseif attrname == 'align'
308				let values = ["left", "center", "right", "justify", "char"]
309			elseif attrname == 'valign'
310				let values = ["top", "middle", "bottom", "baseline"]
311			elseif attrname == 'scope'
312				let values = ["row", "col", "rowgroup", "colgroup"]
313			elseif attrname == 'href'
314				" Now we are looking for local anchors defined by name or id
315				if entered_value =~ '^#'
316					let file = join(getline(1, line('$')), ' ')
317					" Split it be sure there will be one id/name element in
318					" item, it will be also first word [a-zA-Z0-9_-] in element
319					let oneelement = split(file, "\\(meta \\)\\@<!\\(name\\|id\\)\\s*=\\s*[\"']")
320					for i in oneelement
321						let values += ['#'.matchstr(i, "^[a-zA-Z][a-zA-Z0-9%_-]*")]
322					endfor
323				endif
324			elseif attrname == 'type'
325				if context =~ '^input'
326					let values = ["text", "password", "checkbox", "radio", "submit", "reset", "file", "hidden", "image", "button"]
327				elseif context =~ '^button'
328					let values = ["button", "submit", "reset"]
329				elseif context =~ '^style'
330					let values = ["text/css"]
331				elseif context =~ '^script'
332					let values = ["text/javascript"]
333				endif
334			else
335				return []
336			endif
337
338			if len(values) == 0
339				return []
340			endif
341
342			" We need special version of sbase
343			let attrbase = matchstr(context, ".*[\"']")
344			let attrquote = matchstr(attrbase, '.$')
345
346			for m in values
347				" This if is needed to not offer all completions as-is
348				" alphabetically but sort them. Those beginning with entered
349				" part will be as first choices
350				if m =~ '^'.entered_value
351					call add(res, m . attrquote.' ')
352				elseif m =~ entered_value
353					call add(res2, m . attrquote.' ')
354				endif
355			endfor
356
357			return res + res2
358
359		endif
360		" Shorten context to not include last word
361		let sbase = matchstr(context, '.*\ze\s.*')
362		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\)$'
363			let attrs = coregroup
364		elseif tag == 'a'
365			let attrs = coregroup + focus + ["charset", "type", "name", "href", "hreflang", "rel", "rev", "shape", "coords"]
366		elseif tag == 'area'
367			let attrs = coregroup + focus + ["shape", "coords", "href", "nohref", "alt"]
368		elseif tag == 'base'
369			let attrs = ["href", "id"]
370		elseif tag == 'blockquote'
371			let attrs = coregroup + ["cite"]
372		elseif tag == 'body'
373			let attrs = coregroup + ["onload", "onunload"]
374		elseif tag == 'br'
375			let attrs = coreattrs
376		elseif tag == 'button'
377			let attrs = coregroup + focus + ["name", "value", "type"]
378		elseif tag == '^\(col\|colgroup\)$'
379			let attrs = coregroup + ["span", "width", "align", "char", "charoff", "valign"]
380		elseif tag =~ '^\(del\|ins\)$'
381			let attrs = coregroup + ["cite", "datetime"]
382		elseif tag == 'form'
383			let attrs = coregroup + ["action", "method=\"get\" ", "method=\"post\" ", "enctype", "onsubmit", "onreset", "accept", "accept-charset"]
384		elseif tag == 'head'
385			let attrs = i18n + ["id", "profile"]
386		elseif tag == 'html'
387			let attrs = i18n + ["id", "xmlns"]
388		elseif tag == 'img'
389			let attrs = coregroup + ["src", "alt", "longdesc", "height", "width", "usemap", "ismap"]
390		elseif tag == 'input'
391			let attrs = coregroup + ["type", "name", "value", "checked", "disabled", "readonly", "size", "maxlength", "src", "alt", "usemap", "onselect", "onchange", "accept"]
392		elseif tag == 'label'
393			let attrs = coregroup + ["for", "accesskey", "onfocus", "onblur"]
394		elseif tag == 'legend'
395			let attrs = coregroup + ["accesskey"]
396		elseif tag == 'link'
397			let attrs = coregroup + ["charset", "href", "hreflang", "type", "rel", "rev", "media"]
398		elseif tag == 'map'
399			let attrs = i18n + events + ["id", "class", "style", "title", "name"]
400		elseif tag == 'meta'
401			let attrs = i18n + ["id", "http-equiv", "content", "scheme", "name"]
402		elseif tag == 'title'
403			let attrs = i18n + ["id"]
404		elseif tag == 'object'
405			let attrs = coregroup + ["declare", "classid", "codebase", "data", "type", "codetype", "archive", "standby", "height", "width", "usemap", "name", "tabindex"]
406		elseif tag == 'optgroup'
407			let attrs = coregroup + ["disbled", "label"]
408		elseif tag == 'option'
409			let attrs = coregroup + ["disbled", "selected", "value", "label"]
410		elseif tag == 'param'
411			let attrs = ["id", "name", "value", "valuetype", "type"]
412		elseif tag == 'pre'
413			let attrs = coregroup + ["xml:space"]
414		elseif tag == 'q'
415			let attrs = coregroup + ["cite"]
416		elseif tag == 'script'
417			let attrs = ["id", "charset", "type=\"text/javascript\"", "type", "src", "defer", "xml:space"]
418		elseif tag == 'select'
419			let attrs = coregroup + ["name", "size", "multiple", "disabled", "tabindex", "onfocus", "onblur", "onchange"]
420		elseif tag == 'style'
421			let attrs = coreattrs + ["id", "type=\"text/css\"", "type", "media", "title", "xml:space"]
422		elseif tag == 'table'
423			let attrs = coregroup + ["summary", "width", "border", "frame", "rules", "cellspacing", "cellpadding"]
424		elseif tag =~ '^\(thead\|tfoot\|tbody\|tr\)$'
425			let attrs = coregroup + ["align", "char", "charoff", "valign"]
426		elseif tag == 'textarea'
427			let attrs = coregroup + ["name", "rows", "cols", "disabled", "readonly", "onselect", "onchange"]
428		elseif tag =~ '^\(th\|td\)$'
429			let attrs = coregroup + ["abbr", "headers", "scope", "rowspan", "colspan", "align", "char", "charoff", "valign"]
430		else
431			return []
432		endif
433
434		for m in sort(attrs)
435			if m =~ '^'.attr
436				if m =~ '^\(ismap\|defer\|declare\|nohref\|checked\|disabled\|selected\|readonly\)$' || m =~ '='
437					call add(res, m)
438				else
439					call add(res, m.'="')
440				endif
441			elseif m =~ attr
442				if m =~ '^\(ismap\|defer\|declare\|nohref\|checked\|disabled\|selected\|readonly\)$' || m =~ '='
443					call add(res2, m)
444				else
445					call add(res2, m.'="')
446				endif
447			endif
448		endfor
449
450		return res + res2
451
452	endif
453	" Close tag
454	let b:unaryTagsStack = "base meta link hr br param img area input col"
455	if context =~ '^\/'
456		let opentag = htmlcomplete#GetLastOpenTag("b:unaryTagsStack")
457		return [opentag.">"]
458	endif
459	" Deal with tag completion.
460	let opentag = htmlcomplete#GetLastOpenTag("b:unaryTagsStack")
461	" Clusters
462	let special = "br span bdo map object img"
463	let phrase =  "em strong dfn code q samp kbd var cite abbr acronym sub sup"
464	let inlineforms = "input select textarea label button"
465	let miscinline = "ins del script"
466	let inline = "a ".special." ".phrase." ".inlineforms." tt i b big small"
467	let misc = "noscript ".miscinline
468	let block = "p h1 h2 h3 h4 h5 h6 div ul ol dl pre hr blockquote address fieldset table"
469
470	if opentag == 'a'
471		let tags = split("tt i b big small ".special." ".phrase." ".inlineforms." ".miscinline)
472	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\)$'
473		let tags = split(inline." ".miscinline)
474	elseif opentag == 'pre'
475		let tags = split("a tt i b big small br span bdo map ".phrase." ".miscinline." ".inlineforms)
476	elseif opentag == 'html'
477		let tags = ["head", "body"]
478	elseif opentag == 'legend'
479		let tags = split(inline." ".miscinline)
480	elseif opentag == 'head'
481		let tags = ["title", "base", "scipt", "style", "meta", "link", "object"]
482	elseif opentag =~ '^\(noscript\|body\|blockquote\)$'
483		let tags = split("form ".block." ".misc)
484	elseif opentag =~ '^\(ul\|ol\)$'
485		let tags = ["li"]
486	elseif opentag == 'dl'
487		let tags = ["dt", "dd"]
488	elseif opentag =~ '^\(ins\|del\|th\|td\|dd\|div\|li\)$'
489		let tags = split("form ".block." ".inline." ".misc)
490	elseif opentag == 'object'
491		let tags = split("param form ".block." ".inline." ".misc)
492	elseif opentag == 'fieldset'
493		let tags = split("legend form ".block." ".inline." ".misc)
494	elseif opentag == 'map'
495		let tags = split("area form ".block." ".misc)
496	elseif opentag == 'form'
497		let tags = split(block." ".misc)
498	elseif opentag == 'select'
499		let tags = ["optgroup", "option"]
500	elseif opentag == 'optgroup'
501		let tags = ["option"]
502	elseif opentag == 'colgroup'
503		let tags = ["col"]
504	elseif opentag == '^\(textarea\|option\|script\|style\|title\)$'
505		let tags = ['empty']
506	elseif opentag == 'button'
507		let tags = ["p", "h1", "h2", "h3", "h4", "h5", "h6", "div", "ul", "ol", "dl", "table"]
508	elseif opentag =~ '^\(thead\|tfoot\|tbody\)$'
509		let tags = ["tr"]
510	elseif opentag == 'tr'
511		let tags = ["th", "td"]
512	elseif opentag == 'table'
513		let tags = ["caption", "col", "colgroup", "thead", "tfoot", "tbody", "tr"]
514	else
515		return []
516	endif
517
518	for m in tags
519		if m =~ '^'.context
520			call add(res, m)
521		elseif m =~ context
522			call add(res2, m)
523		endif
524	endfor
525
526	return res + res2
527
528  endif
529endfunction
530
531" MM: This is greatly reduced closetag.vim used with kind permission of Steven
532"     Mueller
533"     Changes: strip all comments; delete error messages
534" Author: Steven Mueller <[email protected]>
535" Last Modified: Tue May 24 13:29:48 PDT 2005
536" Version: 0.9.1
537
538function! htmlcomplete#GetLastOpenTag(unaryTagsStack)
539	let linenum=line('.')
540	let lineend=col('.') - 1 " start: cursor position
541	let first=1              " flag for first line searched
542	let b:TagStack=''        " main stack of tags
543	let startInComment=s:InComment()
544
545	let tagpat='</\=\(\k\|[-:]\)\+\|/>'
546	while (linenum>0)
547		let line=getline(linenum)
548		if first
549			let line=strpart(line,0,lineend)
550		else
551			let lineend=strlen(line)
552		endif
553		let b:lineTagStack=''
554		let mpos=0
555		let b:TagCol=0
556		while (mpos > -1)
557			let mpos=matchend(line,tagpat)
558			if mpos > -1
559				let b:TagCol=b:TagCol+mpos
560				let tag=matchstr(line,tagpat)
561
562				if exists('b:closetag_disable_synID') || startInComment==s:InCommentAt(linenum, b:TagCol)
563					let b:TagLine=linenum
564					call s:Push(matchstr(tag,'[^<>]\+'),'b:lineTagStack')
565				endif
566				let lineend=lineend-mpos
567				let line=strpart(line,mpos,lineend)
568			endif
569		endwhile
570		while (!s:EmptystackP('b:lineTagStack'))
571			let tag=s:Pop('b:lineTagStack')
572			if match(tag, '^/') == 0		"found end tag
573				call s:Push(tag,'b:TagStack')
574			elseif s:EmptystackP('b:TagStack') && !s:Instack(tag, a:unaryTagsStack)	"found unclosed tag
575				return tag
576			else
577				let endtag=s:Peekstack('b:TagStack')
578				if endtag == '/'.tag || endtag == '/'
579					call s:Pop('b:TagStack')	"found a open/close tag pair
580				elseif !s:Instack(tag, a:unaryTagsStack) "we have a mismatch error
581					return ''
582				endif
583			endif
584		endwhile
585		let linenum=linenum-1 | let first=0
586	endwhile
587return ''
588endfunction
589
590function! s:InComment()
591	return synIDattr(synID(line('.'), col('.'), 0), 'name') =~ 'Comment'
592endfunction
593
594function! s:InCommentAt(line, col)
595	return synIDattr(synID(a:line, a:col, 0), 'name') =~ 'Comment'
596endfunction
597
598function! s:SetKeywords()
599	let g:IsKeywordBak=&iskeyword
600	let &iskeyword='33-255'
601endfunction
602
603function! s:RestoreKeywords()
604	let &iskeyword=g:IsKeywordBak
605endfunction
606
607function! s:Push(el, sname)
608	if !s:EmptystackP(a:sname)
609		exe 'let '.a:sname."=a:el.' '.".a:sname
610	else
611		exe 'let '.a:sname.'=a:el'
612	endif
613endfunction
614
615function! s:EmptystackP(sname)
616	exe 'let stack='.a:sname
617	if match(stack,'^ *$') == 0
618		return 1
619	else
620		return 0
621	endif
622endfunction
623
624function! s:Instack(el, sname)
625	exe 'let stack='.a:sname
626	call s:SetKeywords()
627	let m=match(stack, '\<'.a:el.'\>')
628	call s:RestoreKeywords()
629	if m < 0
630		return 0
631	else
632		return 1
633	endif
634endfunction
635
636function! s:Peekstack(sname)
637	call s:SetKeywords()
638	exe 'let stack='.a:sname
639	let top=matchstr(stack, '\<.\{-1,}\>')
640	call s:RestoreKeywords()
641	return top
642endfunction
643
644function! s:Pop(sname)
645	if s:EmptystackP(a:sname)
646		return ''
647	endif
648	exe 'let stack='.a:sname
649	call s:SetKeywords()
650	let loc=matchend(stack,'\<.\{-1,}\>')
651	exe 'let '.a:sname.'=strpart(stack, loc+1, strlen(stack))'
652	let top=strpart(stack, match(stack, '\<'), loc)
653	call s:RestoreKeywords()
654	return top
655endfunction
656
657function! s:Clearstack(sname)
658	exe 'let '.a:sname."=''"
659endfunction
660