blob: a773ff20496fd3ed5553d2dc3b61e159e8e7038e [file] [log] [blame]
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001" Vim completion script
2" Language: XHTML 1.0 Strict
3" Maintainer: Mikolaj Machowski ( mikmach AT wp DOT pl )
Bram Moolenaar8349fd72005-10-12 20:52:20 +00004" Last Change: 2005 Oct 12
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005
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
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000011 let compl_begin = col('.') - 2
12 while start >= 0 && line[start - 1] =~ '\(\k\|[:.-]\)'
13 let start -= 1
Bram Moolenaarf75a9632005-09-13 21:20:47 +000014 endwhile
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000015 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
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000023 let curpos = line('.')
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000024 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
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000030 endif
31 endif
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000032 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
Bram Moolenaarf75a9632005-09-13 21:20:47 +000038 return start
39 else
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000040 " 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
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000046 " Check if we should do CSS completion inside of <style> tag
47 if exists("b:csscompl")
48 unlet! b:csscompl
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000049 return csscomplete#CompleteCSS(0, context)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000050 endif
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000051 " Make entities completion
52 if exists("b:entitiescompl")
53 unlet! b:entitiescompl
54
55 " Very, very long line
Bram Moolenaar8349fd72005-10-12 20:52:20 +000056 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"]
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000057
Bram Moolenaar8349fd72005-10-12 20:52:20 +000058 for m in values
59 if m =~ '^'.a:base
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000060 call add(res, m.';')
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000061 endif
62 endfor
63
Bram Moolenaar8349fd72005-10-12 20:52:20 +000064 return res
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000065
66 endif
67 if context =~ '>'
68 " Generally if context contains > it means we are outside of tag and
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000069 " should abandon action - with one exception: <style> span { bo
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000070 if context =~ 'style[^>]\{-}>[^<]\{-}$'
71 return csscomplete#CompleteCSS(0, context)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000072 else
73 return []
74 endif
75 endif
76
Bram Moolenaarf75a9632005-09-13 21:20:47 +000077 " Set attribute groups
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000078 let coreattrs = ["id", "class", "style", "title"]
79 let i18n = ["lang", "xml:lang", "dir=\"ltr\" ", "dir=\"rtl\" "]
80 let events = ["onclick", "ondblclick", "onmousedown", "onmouseup", "onmousemove",
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000081 \ "onmouseover", "onmouseout", "onkeypress", "onkeydown", "onkeyup"]
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000082 let focus = ["accesskey", "tabindex", "onfocus", "onblur"]
83 let coregroup = coreattrs + i18n + events
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000084 " find tags matching with "context"
85 " If context contains > it means we are already outside of tag and we
Bram Moolenaarf75a9632005-09-13 21:20:47 +000086 " should abandon action
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000087 " If context contains white space it is attribute.
Bram Moolenaarf75a9632005-09-13 21:20:47 +000088 " It could be also value of attribute...
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000089 " We have to get first word to offer
90 " proper completions
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000091 if context == ''
92 let tag = ''
93 else
94 let tag = split(context)[0]
95 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000096 " Get last word, it should be attr name
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000097 let attr = matchstr(context, '.*\s\zs.*')
Bram Moolenaarf75a9632005-09-13 21:20:47 +000098 " Possible situations where any prediction would be difficult:
99 " 1. Events attributes
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000100 if context =~ '\s'
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000101 " Sort out style, class, and on* cases
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000102 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_ -]*$"
Bram Moolenaar1e015462005-09-25 22:16:38 +0000105 let search_for = "class"
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000106 elseif context =~ "id\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
Bram Moolenaar1e015462005-09-25 22:16:38 +0000107 let search_for = "id"
108 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000109 " 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'
Bram Moolenaar1e015462005-09-25 22:16:38 +0000117 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
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000133 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
Bram Moolenaar1e015462005-09-25 22:16:38 +0000147 for file in styletable
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000148 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
Bram Moolenaar1e015462005-09-25 22:16:38 +0000153 let secfile = matchstr(line, "import\\s\\+\\(url(\\)\\?[\"']\\?\\zs\\f\\+\\ze")
154 let secfile = fnamemodify(file, ":p:h").'/'.secfile
155 let secimportfiles += [secfile]
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000156 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)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000165 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
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000183 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
Bram Moolenaar1e015462005-09-25 22:16:38 +0000190
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
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000199 else
Bram Moolenaar1e015462005-09-25 22:16:38 +0000200 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
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000211 endif
212 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000213 endfor
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000214
Bram Moolenaar1e015462005-09-25 22:16:38 +0000215 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
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000250 endif
251
252 " We need special version of sbase
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000253 let classbase = matchstr(context, ".*[\"']")
Bram Moolenaar1e015462005-09-25 22:16:38 +0000254 let classquote = matchstr(classbase, '.$')
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000255
256 let entered_class = matchstr(attr, ".*=\\s*[\"']\\zs.*")
257
258 for m in sort(values)
259 if m =~? '^'.entered_class
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000260 call add(res, m . classquote)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000261 elseif m =~? entered_class
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000262 call add(res2, m . classquote)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000263 endif
264 endfor
265
266 return res + res2
267
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000268 elseif context =~ "style\\s*=\\s*[\"'][^\"']*$"
269 return csscomplete#CompleteCSS(0, context)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000270
271 endif
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000272 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.
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000274 " It may fail with some strange style value combinations.
275 if stripbase !~ "[\"']"
276 return []
277 endif
278 endif
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000279 " 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'
Bram Moolenaar8349fd72005-10-12 20:52:20 +0000290 let values = ["rect", "circle", "poly", "default"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000291 elseif attrname == 'valuetype'
292 let values = ["data", "ref", "object"]
293 elseif attrname == 'method'
294 let values = ["get", "post"]
Bram Moolenaar4c903f92005-09-14 21:32:32 +0000295 elseif attrname == 'dir'
296 let values = ["ltr", "rtl"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000297 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'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000319 if context =~ '^input'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000320 let values = ["text", "password", "checkbox", "radio", "submit", "reset", "file", "hidden", "image", "button"]
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000321 elseif context =~ '^button'
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000322 let values = ["button", "submit", "reset"]
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000323 elseif context =~ '^style'
Bram Moolenaar1e015462005-09-25 22:16:38 +0000324 let values = ["text/css"]
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000325 elseif context =~ '^script'
Bram Moolenaar1e015462005-09-25 22:16:38 +0000326 let values = ["text/javascript"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000327 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
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000337 let attrbase = matchstr(context, ".*[\"']")
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000338 let attrquote = matchstr(attrbase, '.$')
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000339
340 for m in values
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000341 " 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
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000345 call add(res, m . attrquote.' ')
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000346 elseif m =~ entered_value
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000347 call add(res2, m . attrquote.' ')
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000348 endif
349 endfor
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000350
351 return res + res2
352
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000353 endif
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000354 " 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\)$'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000357 let attrs = coregroup
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000358 elseif tag == 'a'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000359 let attrs = coregroup + focus + ["charset", "type", "name", "href", "hreflang", "rel", "rev", "shape", "coords"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000360 elseif tag == 'area'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000361 let attrs = coregroup + focus + ["shape", "coords", "href", "nohref", "alt"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000362 elseif tag == 'base'
363 let attrs = ["href", "id"]
364 elseif tag == 'blockquote'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000365 let attrs = coregroup + ["cite"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000366 elseif tag == 'body'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000367 let attrs = coregroup + ["onload", "onunload"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000368 elseif tag == 'br'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000369 let attrs = coreattrs
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000370 elseif tag == 'button'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000371 let attrs = coregroup + focus + ["name", "value", "type"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000372 elseif tag == '^\(col\|colgroup\)$'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000373 let attrs = coregroup + ["span", "width", "align", "char", "charoff", "valign"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000374 elseif tag =~ '^\(del\|ins\)$'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000375 let attrs = coregroup + ["cite", "datetime"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000376 elseif tag == 'form'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000377 let attrs = coregroup + ["action", "method=\"get\" ", "method=\"post\" ", "enctype", "onsubmit", "onreset", "accept", "accept-charset"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000378 elseif tag == 'head'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000379 let attrs = i18n + ["id", "profile"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000380 elseif tag == 'html'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000381 let attrs = i18n + ["id", "xmlns"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000382 elseif tag == 'img'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000383 let attrs = coregroup + ["src", "alt", "longdesc", "height", "width", "usemap", "ismap"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000384 elseif tag == 'input'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000385 let attrs = coregroup + ["type", "name", "value", "checked", "disabled", "readonly", "size", "maxlength", "src", "alt", "usemap", "onselect", "onchange", "accept"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000386 elseif tag == 'label'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000387 let attrs = coregroup + ["for", "accesskey", "onfocus", "onblur"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000388 elseif tag == 'legend'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000389 let attrs = coregroup + ["accesskey"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000390 elseif tag == 'link'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000391 let attrs = coregroup + ["charset", "href", "hreflang", "type", "rel", "rev", "media"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000392 elseif tag == 'map'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000393 let attrs = i18n + events + ["id", "class", "style", "title", "name"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000394 elseif tag == 'meta'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000395 let attrs = i18n + ["id", "http-equiv", "content", "scheme", "name"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000396 elseif tag == 'title'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000397 let attrs = i18n + ["id"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000398 elseif tag == 'object'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000399 let attrs = coregroup + ["declare", "classid", "codebase", "data", "type", "codetype", "archive", "standby", "height", "width", "usemap", "name", "tabindex"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000400 elseif tag == 'optgroup'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000401 let attrs = coregroup + ["disbled", "label"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000402 elseif tag == 'option'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000403 let attrs = coregroup + ["disbled", "selected", "value", "label"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000404 elseif tag == 'param'
405 let attrs = ["id", "name", "value", "valuetype", "type"]
406 elseif tag == 'pre'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000407 let attrs = coregroup + ["xml:space"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000408 elseif tag == 'q'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000409 let attrs = coregroup + ["cite"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000410 elseif tag == 'script'
Bram Moolenaar1e015462005-09-25 22:16:38 +0000411 let attrs = ["id", "charset", "type=\"text/javascript\"", "type", "src", "defer", "xml:space"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000412 elseif tag == 'select'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000413 let attrs = coregroup + ["name", "size", "multiple", "disabled", "tabindex", "onfocus", "onblur", "onchange"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000414 elseif tag == 'style'
Bram Moolenaar1e015462005-09-25 22:16:38 +0000415 let attrs = coreattrs + ["id", "type=\"text/css\"", "type", "media", "title", "xml:space"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000416 elseif tag == 'table'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000417 let attrs = coregroup + ["summary", "width", "border", "frame", "rules", "cellspacing", "cellpadding"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000418 elseif tag =~ '^\(thead\|tfoot\|tbody\|tr\)$'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000419 let attrs = coregroup + ["align", "char", "charoff", "valign"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000420 elseif tag == 'textarea'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000421 let attrs = coregroup + ["name", "rows", "cols", "disabled", "readonly", "onselect", "onchange"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000422 elseif tag =~ '^\(th\|td\)$'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000423 let attrs = coregroup + ["abbr", "headers", "scope", "rowspan", "colspan", "align", "char", "charoff", "valign"]
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000424 else
425 return []
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000426 endif
427
428 for m in sort(attrs)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000429 if m =~ '^'.attr
430 if m =~ '^\(ismap\|defer\|declare\|nohref\|checked\|disabled\|selected\|readonly\)$' || m =~ '='
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000431 call add(res, m)
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000432 else
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000433 call add(res, m.'="')
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000434 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000435 elseif m =~ attr
436 if m =~ '^\(ismap\|defer\|declare\|nohref\|checked\|disabled\|selected\|readonly\)$' || m =~ '='
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000437 call add(res2, m)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000438 else
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000439 call add(res2, m.'="')
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000440 endif
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000441 endif
442 endfor
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000443
444 return res + res2
445
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000446 endif
Bram Moolenaar4c903f92005-09-14 21:32:32 +0000447 " Close tag
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000448 let b:unaryTagsStack = "base meta link hr br param img area input col"
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000449 if context =~ '^\/'
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000450 let opentag = htmlcomplete#GetLastOpenTag("b:unaryTagsStack")
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000451 return [opentag.">"]
Bram Moolenaar4c903f92005-09-14 21:32:32 +0000452 endif
453 " Deal with tag completion.
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000454 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"
Bram Moolenaar4c903f92005-09-14 21:32:32 +0000463
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000464 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'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000471 let tags = ["head", "body"]
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000472 elseif opentag == 'legend'
473 let tags = split(inline." ".miscinline)
474 elseif opentag == 'head'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000475 let tags = ["title", "base", "scipt", "style", "meta", "link", "object"]
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000476 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'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000481 let tags = ["dt", "dd"]
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000482 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'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000493 let tags = ["optgroup", "option"]
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000494 elseif opentag == 'optgroup'
495 let tags = ["option"]
496 elseif opentag == 'colgroup'
497 let tags = ["col"]
498 elseif opentag == '^\(textarea\|option\|script\|style\|title\)$'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000499 let tags = ['empty']
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000500 elseif opentag == 'button'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000501 let tags = ["p", "h1", "h2", "h3", "h4", "h5", "h6", "div", "ul", "ol", "dl", "table"]
502 elseif opentag =~ '^\(thead\|tfoot\|tbody\)$'
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000503 let tags = ["tr"]
504 elseif opentag == 'tr'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000505 let tags = ["th", "td"]
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000506 elseif opentag == 'table'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000507 let tags = ["caption", "col", "colgroup", "thead", "tfoot", "tbody", "tr"]
508 else
509 return []
Bram Moolenaar4c903f92005-09-14 21:32:32 +0000510 endif
511
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000512 for m in tags
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000513 if m =~ '^'.context
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000514 call add(res, m)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000515 elseif m =~ context
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000516 call add(res2, m)
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000517 endif
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000518 endfor
519
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000520 return res + res2
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000521
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000522 endif
523endfunction
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000524
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 <diffusor@ugcs.caltech.edu>
529" Last Modified: Tue May 24 13:29:48 PDT 2005
530" Version: 0.9.1
531
532function! htmlcomplete#GetLastOpenTag(unaryTagsStack)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000533 let linenum=line('.')
534 let lineend=col('.') - 1 " start: cursor position
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000535 let first=1 " flag for first line searched
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000536 let b:TagStack='' " main stack of tags
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000537 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
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000547 let b:lineTagStack=''
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000548 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
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000556 if exists('b:closetag_disable_synID') || startInComment==s:InCommentAt(linenum, b:TagCol)
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000557 let b:TagLine=linenum
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000558 call s:Push(matchstr(tag,'[^<>]\+'),'b:lineTagStack')
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000559 endif
560 let lineend=lineend-mpos
561 let line=strpart(line,mpos,lineend)
562 endif
563 endwhile
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000564 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
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000569 return tag
570 else
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000571 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
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000577 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000578 endwhile
579 let linenum=linenum-1 | let first=0
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000580 endwhile
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000581return ''
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000582endfunction
583
584function! s:InComment()
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000585 return synIDattr(synID(line('.'), col('.'), 0), 'name') =~ 'Comment'
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000586endfunction
587
588function! s:InCommentAt(line, col)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000589 return synIDattr(synID(a:line, a:col, 0), 'name') =~ 'Comment'
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000590endfunction
591
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000592function! s:SetKeywords()
593 let g:IsKeywordBak=&iskeyword
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000594 let &iskeyword='33-255'
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000595endfunction
596
597function! s:RestoreKeywords()
598 let &iskeyword=g:IsKeywordBak
599endfunction
600
601function! s:Push(el, sname)
602 if !s:EmptystackP(a:sname)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000603 exe 'let '.a:sname."=a:el.' '.".a:sname
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000604 else
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000605 exe 'let '.a:sname.'=a:el'
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000606 endif
607endfunction
608
609function! s:EmptystackP(sname)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000610 exe 'let stack='.a:sname
611 if match(stack,'^ *$') == 0
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000612 return 1
613 else
614 return 0
615 endif
616endfunction
617
618function! s:Instack(el, sname)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000619 exe 'let stack='.a:sname
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000620 call s:SetKeywords()
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000621 let m=match(stack, '\<'.a:el.'\>')
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000622 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()
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000632 exe 'let stack='.a:sname
633 let top=matchstr(stack, '\<.\{-1,}\>')
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000634 call s:RestoreKeywords()
635 return top
636endfunction
637
638function! s:Pop(sname)
639 if s:EmptystackP(a:sname)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000640 return ''
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000641 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000642 exe 'let stack='.a:sname
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000643 call s:SetKeywords()
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000644 let loc=matchend(stack,'\<.\{-1,}\>')
645 exe 'let '.a:sname.'=strpart(stack, loc+1, strlen(stack))'
646 let top=strpart(stack, match(stack, '\<'), loc)
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000647 call s:RestoreKeywords()
648 return top
649endfunction
650
651function! s:Clearstack(sname)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000652 exe 'let '.a:sname."=''"
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000653endfunction