blob: b3dae2d3081945883798d484e0824b2f2438020e [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 Moolenaard5cdbeb2005-10-10 20:59:28 +00004" Last Change: 2005 Oct 9
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
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
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000071 " should abandon action - with one exception: <style> span { bo
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000072 if context =~ 'style[^>]\{-}>[^<]\{-}$'
73 return csscomplete#CompleteCSS(0, context)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000074 else
75 return []
76 endif
77 endif
78
Bram Moolenaarf75a9632005-09-13 21:20:47 +000079 " Set attribute groups
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000080 let coreattrs = ["id", "class", "style", "title"]
81 let i18n = ["lang", "xml:lang", "dir=\"ltr\" ", "dir=\"rtl\" "]
82 let events = ["onclick", "ondblclick", "onmousedown", "onmouseup", "onmousemove",
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000083 \ "onmouseover", "onmouseout", "onkeypress", "onkeydown", "onkeyup"]
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000084 let focus = ["accesskey", "tabindex", "onfocus", "onblur"]
85 let coregroup = coreattrs + i18n + events
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000086 " find tags matching with "context"
87 " If context contains > it means we are already outside of tag and we
Bram Moolenaarf75a9632005-09-13 21:20:47 +000088 " should abandon action
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000089 " If context contains white space it is attribute.
Bram Moolenaarf75a9632005-09-13 21:20:47 +000090 " It could be also value of attribute...
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000091 " We have to get first word to offer
92 " proper completions
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000093 if context == ''
94 let tag = ''
95 else
96 let tag = split(context)[0]
97 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000098 " Get last word, it should be attr name
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000099 let attr = matchstr(context, '.*\s\zs.*')
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000100 " Possible situations where any prediction would be difficult:
101 " 1. Events attributes
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000102 if context =~ '\s'
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000103 " Sort out style, class, and on* cases
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000104 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_ -]*$"
Bram Moolenaar1e015462005-09-25 22:16:38 +0000107 let search_for = "class"
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000108 elseif context =~ "id\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
Bram Moolenaar1e015462005-09-25 22:16:38 +0000109 let search_for = "id"
110 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000111 " 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'
Bram Moolenaar1e015462005-09-25 22:16:38 +0000119 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
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000135 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
Bram Moolenaar1e015462005-09-25 22:16:38 +0000149 for file in styletable
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000150 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
Bram Moolenaar1e015462005-09-25 22:16:38 +0000155 let secfile = matchstr(line, "import\\s\\+\\(url(\\)\\?[\"']\\?\\zs\\f\\+\\ze")
156 let secfile = fnamemodify(file, ":p:h").'/'.secfile
157 let secimportfiles += [secfile]
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000158 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)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000167 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
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000185 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
Bram Moolenaar1e015462005-09-25 22:16:38 +0000192
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
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000201 else
Bram Moolenaar1e015462005-09-25 22:16:38 +0000202 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
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000213 endif
214 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000215 endfor
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000216
Bram Moolenaar1e015462005-09-25 22:16:38 +0000217 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
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000252 endif
253
254 " We need special version of sbase
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000255 let classbase = matchstr(context, ".*[\"']")
Bram Moolenaar1e015462005-09-25 22:16:38 +0000256 let classquote = matchstr(classbase, '.$')
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000257
258 let entered_class = matchstr(attr, ".*=\\s*[\"']\\zs.*")
259
260 for m in sort(values)
261 if m =~? '^'.entered_class
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000262 call add(res, m . classquote)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000263 elseif m =~? entered_class
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000264 call add(res2, m . classquote)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000265 endif
266 endfor
267
268 return res + res2
269
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000270 elseif context =~ "style\\s*=\\s*[\"'][^\"']*$"
271 return csscomplete#CompleteCSS(0, context)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000272
273 endif
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000274 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.
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000276 " It may fail with some strange style value combinations.
277 if stripbase !~ "[\"']"
278 return []
279 endif
280 endif
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000281 " 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'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000292 if context =~ '^a\>'
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000293 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"]
Bram Moolenaar4c903f92005-09-14 21:32:32 +0000301 elseif attrname == 'dir'
302 let values = ["ltr", "rtl"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000303 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'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000325 if context =~ '^input'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000326 let values = ["text", "password", "checkbox", "radio", "submit", "reset", "file", "hidden", "image", "button"]
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000327 elseif context =~ '^button'
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000328 let values = ["button", "submit", "reset"]
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000329 elseif context =~ '^style'
Bram Moolenaar1e015462005-09-25 22:16:38 +0000330 let values = ["text/css"]
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000331 elseif context =~ '^script'
Bram Moolenaar1e015462005-09-25 22:16:38 +0000332 let values = ["text/javascript"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000333 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
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000343 let attrbase = matchstr(context, ".*[\"']")
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000344 let attrquote = matchstr(attrbase, '.$')
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000345
346 for m in values
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000347 " 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
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000351 call add(res, m . attrquote.' ')
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000352 elseif m =~ entered_value
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000353 call add(res2, m . attrquote.' ')
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000354 endif
355 endfor
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000356
357 return res + res2
358
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000359 endif
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000360 " 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\)$'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000363 let attrs = coregroup
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000364 elseif tag == 'a'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000365 let attrs = coregroup + focus + ["charset", "type", "name", "href", "hreflang", "rel", "rev", "shape", "coords"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000366 elseif tag == 'area'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000367 let attrs = coregroup + focus + ["shape", "coords", "href", "nohref", "alt"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000368 elseif tag == 'base'
369 let attrs = ["href", "id"]
370 elseif tag == 'blockquote'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000371 let attrs = coregroup + ["cite"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000372 elseif tag == 'body'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000373 let attrs = coregroup + ["onload", "onunload"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000374 elseif tag == 'br'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000375 let attrs = coreattrs
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000376 elseif tag == 'button'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000377 let attrs = coregroup + focus + ["name", "value", "type"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000378 elseif tag == '^\(col\|colgroup\)$'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000379 let attrs = coregroup + ["span", "width", "align", "char", "charoff", "valign"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000380 elseif tag =~ '^\(del\|ins\)$'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000381 let attrs = coregroup + ["cite", "datetime"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000382 elseif tag == 'form'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000383 let attrs = coregroup + ["action", "method=\"get\" ", "method=\"post\" ", "enctype", "onsubmit", "onreset", "accept", "accept-charset"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000384 elseif tag == 'head'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000385 let attrs = i18n + ["id", "profile"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000386 elseif tag == 'html'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000387 let attrs = i18n + ["id", "xmlns"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000388 elseif tag == 'img'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000389 let attrs = coregroup + ["src", "alt", "longdesc", "height", "width", "usemap", "ismap"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000390 elseif tag == 'input'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000391 let attrs = coregroup + ["type", "name", "value", "checked", "disabled", "readonly", "size", "maxlength", "src", "alt", "usemap", "onselect", "onchange", "accept"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000392 elseif tag == 'label'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000393 let attrs = coregroup + ["for", "accesskey", "onfocus", "onblur"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000394 elseif tag == 'legend'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000395 let attrs = coregroup + ["accesskey"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000396 elseif tag == 'link'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000397 let attrs = coregroup + ["charset", "href", "hreflang", "type", "rel", "rev", "media"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000398 elseif tag == 'map'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000399 let attrs = i18n + events + ["id", "class", "style", "title", "name"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000400 elseif tag == 'meta'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000401 let attrs = i18n + ["id", "http-equiv", "content", "scheme", "name"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000402 elseif tag == 'title'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000403 let attrs = i18n + ["id"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000404 elseif tag == 'object'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000405 let attrs = coregroup + ["declare", "classid", "codebase", "data", "type", "codetype", "archive", "standby", "height", "width", "usemap", "name", "tabindex"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000406 elseif tag == 'optgroup'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000407 let attrs = coregroup + ["disbled", "label"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000408 elseif tag == 'option'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000409 let attrs = coregroup + ["disbled", "selected", "value", "label"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000410 elseif tag == 'param'
411 let attrs = ["id", "name", "value", "valuetype", "type"]
412 elseif tag == 'pre'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000413 let attrs = coregroup + ["xml:space"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000414 elseif tag == 'q'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000415 let attrs = coregroup + ["cite"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000416 elseif tag == 'script'
Bram Moolenaar1e015462005-09-25 22:16:38 +0000417 let attrs = ["id", "charset", "type=\"text/javascript\"", "type", "src", "defer", "xml:space"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000418 elseif tag == 'select'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000419 let attrs = coregroup + ["name", "size", "multiple", "disabled", "tabindex", "onfocus", "onblur", "onchange"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000420 elseif tag == 'style'
Bram Moolenaar1e015462005-09-25 22:16:38 +0000421 let attrs = coreattrs + ["id", "type=\"text/css\"", "type", "media", "title", "xml:space"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000422 elseif tag == 'table'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000423 let attrs = coregroup + ["summary", "width", "border", "frame", "rules", "cellspacing", "cellpadding"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000424 elseif tag =~ '^\(thead\|tfoot\|tbody\|tr\)$'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000425 let attrs = coregroup + ["align", "char", "charoff", "valign"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000426 elseif tag == 'textarea'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000427 let attrs = coregroup + ["name", "rows", "cols", "disabled", "readonly", "onselect", "onchange"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000428 elseif tag =~ '^\(th\|td\)$'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000429 let attrs = coregroup + ["abbr", "headers", "scope", "rowspan", "colspan", "align", "char", "charoff", "valign"]
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000430 else
431 return []
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000432 endif
433
434 for m in sort(attrs)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000435 if m =~ '^'.attr
436 if m =~ '^\(ismap\|defer\|declare\|nohref\|checked\|disabled\|selected\|readonly\)$' || m =~ '='
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000437 call add(res, m)
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000438 else
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000439 call add(res, m.'="')
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000440 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000441 elseif m =~ attr
442 if m =~ '^\(ismap\|defer\|declare\|nohref\|checked\|disabled\|selected\|readonly\)$' || m =~ '='
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000443 call add(res2, m)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000444 else
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000445 call add(res2, m.'="')
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000446 endif
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000447 endif
448 endfor
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000449
450 return res + res2
451
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000452 endif
Bram Moolenaar4c903f92005-09-14 21:32:32 +0000453 " Close tag
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000454 let b:unaryTagsStack = "base meta link hr br param img area input col"
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000455 if context =~ '^\/'
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000456 let opentag = htmlcomplete#GetLastOpenTag("b:unaryTagsStack")
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000457 return [opentag.">"]
Bram Moolenaar4c903f92005-09-14 21:32:32 +0000458 endif
459 " Deal with tag completion.
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000460 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"
Bram Moolenaar4c903f92005-09-14 21:32:32 +0000469
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000470 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'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000477 let tags = ["head", "body"]
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000478 elseif opentag == 'legend'
479 let tags = split(inline." ".miscinline)
480 elseif opentag == 'head'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000481 let tags = ["title", "base", "scipt", "style", "meta", "link", "object"]
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000482 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'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000487 let tags = ["dt", "dd"]
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000488 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'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000499 let tags = ["optgroup", "option"]
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000500 elseif opentag == 'optgroup'
501 let tags = ["option"]
502 elseif opentag == 'colgroup'
503 let tags = ["col"]
504 elseif opentag == '^\(textarea\|option\|script\|style\|title\)$'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000505 let tags = ['empty']
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000506 elseif opentag == 'button'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000507 let tags = ["p", "h1", "h2", "h3", "h4", "h5", "h6", "div", "ul", "ol", "dl", "table"]
508 elseif opentag =~ '^\(thead\|tfoot\|tbody\)$'
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000509 let tags = ["tr"]
510 elseif opentag == 'tr'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000511 let tags = ["th", "td"]
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000512 elseif opentag == 'table'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000513 let tags = ["caption", "col", "colgroup", "thead", "tfoot", "tbody", "tr"]
514 else
515 return []
Bram Moolenaar4c903f92005-09-14 21:32:32 +0000516 endif
517
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000518 for m in tags
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000519 if m =~ '^'.context
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000520 call add(res, m)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000521 elseif m =~ context
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000522 call add(res2, m)
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000523 endif
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000524 endfor
525
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000526 return res + res2
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000527
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000528 endif
529endfunction
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000530
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 <diffusor@ugcs.caltech.edu>
535" Last Modified: Tue May 24 13:29:48 PDT 2005
536" Version: 0.9.1
537
538function! htmlcomplete#GetLastOpenTag(unaryTagsStack)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000539 let linenum=line('.')
540 let lineend=col('.') - 1 " start: cursor position
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000541 let first=1 " flag for first line searched
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000542 let b:TagStack='' " main stack of tags
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000543 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
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000553 let b:lineTagStack=''
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000554 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
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000562 if exists('b:closetag_disable_synID') || startInComment==s:InCommentAt(linenum, b:TagCol)
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000563 let b:TagLine=linenum
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000564 call s:Push(matchstr(tag,'[^<>]\+'),'b:lineTagStack')
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000565 endif
566 let lineend=lineend-mpos
567 let line=strpart(line,mpos,lineend)
568 endif
569 endwhile
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000570 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
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000575 return tag
576 else
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000577 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
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000583 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000584 endwhile
585 let linenum=linenum-1 | let first=0
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000586 endwhile
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000587return ''
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000588endfunction
589
590function! s:InComment()
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000591 return synIDattr(synID(line('.'), col('.'), 0), 'name') =~ 'Comment'
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000592endfunction
593
594function! s:InCommentAt(line, col)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000595 return synIDattr(synID(a:line, a:col, 0), 'name') =~ 'Comment'
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000596endfunction
597
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000598function! s:SetKeywords()
599 let g:IsKeywordBak=&iskeyword
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000600 let &iskeyword='33-255'
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000601endfunction
602
603function! s:RestoreKeywords()
604 let &iskeyword=g:IsKeywordBak
605endfunction
606
607function! s:Push(el, sname)
608 if !s:EmptystackP(a:sname)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000609 exe 'let '.a:sname."=a:el.' '.".a:sname
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000610 else
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000611 exe 'let '.a:sname.'=a:el'
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000612 endif
613endfunction
614
615function! s:EmptystackP(sname)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000616 exe 'let stack='.a:sname
617 if match(stack,'^ *$') == 0
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000618 return 1
619 else
620 return 0
621 endif
622endfunction
623
624function! s:Instack(el, sname)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000625 exe 'let stack='.a:sname
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000626 call s:SetKeywords()
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000627 let m=match(stack, '\<'.a:el.'\>')
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000628 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()
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000638 exe 'let stack='.a:sname
639 let top=matchstr(stack, '\<.\{-1,}\>')
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000640 call s:RestoreKeywords()
641 return top
642endfunction
643
644function! s:Pop(sname)
645 if s:EmptystackP(a:sname)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000646 return ''
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000647 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000648 exe 'let stack='.a:sname
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000649 call s:SetKeywords()
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000650 let loc=matchend(stack,'\<.\{-1,}\>')
651 exe 'let '.a:sname.'=strpart(stack, loc+1, strlen(stack))'
652 let top=strpart(stack, match(stack, '\<'), loc)
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000653 call s:RestoreKeywords()
654 return top
655endfunction
656
657function! s:Clearstack(sname)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000658 exe 'let '.a:sname."=''"
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000659endfunction