blob: 9dd5830a1206a867508e2ef03063d7b67971ab4d [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 Moolenaar1e015462005-09-25 22:16:38 +00004" Last Change: 2005 Sep 23
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
11 while start >= 0 && line[start - 1] !~ '<'
12 let start -= 1
13 endwhile
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014 if start < 0
15 let curpos = line('.')
16 let stylestart = searchpair('<style\>', '', '<\/style\>', "bnW")
17 let styleend = searchpair('<style\>', '', '<\/style\>', "nW")
18 if stylestart != 0 && styleend != 0
19 if stylestart <= curpos && styleend >= curpos
20 let b:csscompl = 1
21 let start = 0
22 endif
23 endif
24 endif
Bram Moolenaarf75a9632005-09-13 21:20:47 +000025 return start
26 else
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000027 " Check if we should do CSS completion inside of <style> tag
28 if exists("b:csscompl")
29 unlet! b:csscompl
30 return csscomplete#CompleteCSS(0, a:base)
31 endif
32 if a:base =~ '>'
33 " Generally if a:base contains > it means we are outside of tag and
34 " should abandon action - with one exception: <style> span { bo
35 if a:base =~ 'style[^>]\{-}>[^<]\{-}$'
36 return csscomplete#CompleteCSS(0, a:base)
37 else
38 return []
39 endif
40 endif
41
Bram Moolenaarf75a9632005-09-13 21:20:47 +000042 " Set attribute groups
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000043 let coreattrs = ["id", "class", "style", "title"]
44 let i18n = ["lang", "xml:lang", "dir=\"ltr\" ", "dir=\"rtl\" "]
45 let events = ["onclick", "ondblclick", "onmousedown", "onmouseup", "onmousemove",
Bram Moolenaarf75a9632005-09-13 21:20:47 +000046 \ "onmouseout", "onkeypress", "onkeydown", "onkeyup"]
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000047 let focus = ["accesskey", "tabindex", "onfocus", "onblur"]
48 let coregroup = coreattrs + i18n + events
Bram Moolenaarf75a9632005-09-13 21:20:47 +000049 let res = []
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000050 let res2 = []
51 " find tags matching with "a:base"
Bram Moolenaarf75a9632005-09-13 21:20:47 +000052 " If a:base contains > it means we are already outside of tag and we
53 " should abandon action
Bram Moolenaarf75a9632005-09-13 21:20:47 +000054 " If a:base contains white space it is attribute.
55 " It could be also value of attribute...
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000056 " We have to get first word to offer
57 " proper completions
58 let tag = split(a:base)[0]
59 " Get last word, it should be attr name
60 let attr = matchstr(a:base, '.*\s\zs.*')
Bram Moolenaarf75a9632005-09-13 21:20:47 +000061 " Possible situations where any prediction would be difficult:
62 " 1. Events attributes
63 if a:base =~ '\s'
64 " Sort out style, class, and on* cases
65 " Perfect solution for style would be switching for CSS completion. Is
66 " it possible?
67 " Also retrieving class names from current file and linked
68 " stylesheets.
Bram Moolenaar1e015462005-09-25 22:16:38 +000069 if a:base =~ "\\(on[a-z]*\\|id\\|style\\|class\\)\\s*=\\s*[\"']"
70 if a:base =~ "\\(id\\|class\\)\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
71 if a:base =~ "class\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
72 let search_for = "class"
73 elseif a:base =~ "id\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
74 let search_for = "id"
75 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000076 " Handle class name completion
77 " 1. Find lines of <link stylesheet>
78 " 1a. Check file for @import
79 " 2. Extract filename(s?) of stylesheet,
80 call cursor(1,1)
81 let head = getline(search('<head\>'), search('<\/head>'))
82 let headjoined = join(copy(head), ' ')
83 if headjoined =~ '<style'
Bram Moolenaar1e015462005-09-25 22:16:38 +000084 let stylehead = substitute(headjoined, '+>\*[,', ' ', 'g')
85 if search_for == 'class'
86 let styleheadlines = split(stylehead)
87 let headclasslines = filter(copy(styleheadlines), "v:val =~ '\\([a-zA-Z0-9:]\\+\\)\\?\\.[a-zA-Z0-9_-]\\+'")
88 else
89 let stylesheet = split(headjoined, '[{}]')
90 " Get all lines which fit id syntax
91 let classlines = filter(copy(stylesheet), "v:val =~ '#[a-zA-Z0-9_-]\\+'")
92 " Filter out possible color definitions
93 call filter(classlines, "v:val !~ ':\\s*#[a-zA-Z0-9_-]\\+'")
94 " Filter out complex border definitions
95 call filter(classlines, "v:val !~ '\\(none\\|hidden\\|dotted\\|dashed\\|solid\\|double\\|groove\\|ridge\\|inset\\|outset\\)\\s*#[a-zA-Z0-9_-]\\+'")
96 let templines = join(classlines, ' ')
97 let headclasslines = split(templines)
98 call filter(headclasslines, "v:val =~ '#[a-zA-Z0-9_-]\\+'")
99 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000100 let internal = 1
101 else
102 let internal = 0
103 endif
104 let styletable = []
105 let secimportfiles = []
106 let filestable = filter(copy(head), "v:val =~ '\\(@import\\|link.*stylesheet\\)'")
107 for line in filestable
108 if line =~ "@import"
109 let styletable += [matchstr(line, "import\\s\\+\\(url(\\)\\?[\"']\\?\\zs\\f\\+\\ze")]
110 elseif line =~ "<link"
111 let styletable += [matchstr(line, "href\\s*=\\s*[\"']\\zs\\f\\+\\ze")]
112 endif
113 endfor
Bram Moolenaar1e015462005-09-25 22:16:38 +0000114 for file in styletable
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000115 if filereadable(file)
116 let stylesheet = readfile(file)
117 let secimport = filter(copy(stylesheet), "v:val =~ '@import'")
118 if len(secimport) > 0
119 for line in secimport
Bram Moolenaar1e015462005-09-25 22:16:38 +0000120 let secfile = matchstr(line, "import\\s\\+\\(url(\\)\\?[\"']\\?\\zs\\f\\+\\ze")
121 let secfile = fnamemodify(file, ":p:h").'/'.secfile
122 let secimportfiles += [secfile]
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000123 endfor
124 endif
125 endif
126 endfor
127 let cssfiles = styletable + secimportfiles
128 let classes = []
129 for file in cssfiles
130 if filereadable(file)
131 let stylesheet = readfile(file)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000132 let stylefile = join(stylesheet, ' ')
133 let stylefile = substitute(stylefile, '+>\*[,', ' ', 'g')
134 if search_for == 'class'
135 let stylesheet = split(stylefile)
136 let classlines = filter(copy(stylesheet), "v:val =~ '\\([a-zA-Z0-9:]\\+\\)\\?\\.[a-zA-Z0-9_-]\\+'")
137 else
138 let stylesheet = split(stylefile, '[{}]')
139 " Get all lines which fit id syntax
140 let classlines = filter(copy(stylesheet), "v:val =~ '#[a-zA-Z0-9_-]\\+'")
141 " Filter out possible color definitions
142 call filter(classlines, "v:val !~ ':\\s*#[a-zA-Z0-9_-]\\+'")
143 " Filter out complex border definitions
144 call filter(classlines, "v:val !~ '\\(none\\|hidden\\|dotted\\|dashed\\|solid\\|double\\|groove\\|ridge\\|inset\\|outset\\)\\s*#[a-zA-Z0-9_-]\\+'")
145 let templines = join(classlines, ' ')
146 let stylelines = split(templines)
147 let classlines = filter(stylelines, "v:val =~ '#[a-zA-Z0-9_-]\\+'")
148
149 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000150 endif
151 " We gathered classes definitions from all external files
152 let classes += classlines
153 endfor
154 if internal == 1
155 let classes += headclasslines
156 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000157
158 if search_for == 'class'
159 let elements = {}
160 for element in classes
161 if element =~ '^\.'
162 let class = matchstr(element, '^\.\zs[a-zA-Z][a-zA-Z0-9_-]*\ze')
163 let class = substitute(class, ':.*', '', '')
164 if has_key(elements, 'common')
165 let elements['common'] .= ' '.class
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000166 else
Bram Moolenaar1e015462005-09-25 22:16:38 +0000167 let elements['common'] = class
168 endif
169 else
170 let class = matchstr(element, '[a-zA-Z1-6]*\.\zs[a-zA-Z][a-zA-Z0-9_-]*\ze')
171 let tagname = tolower(matchstr(element, '[a-zA-Z1-6]*\ze.'))
172 if tagname != ''
173 if has_key(elements, tagname)
174 let elements[tagname] .= ' '.class
175 else
176 let elements[tagname] = class
177 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000178 endif
179 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000180 endfor
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000181
Bram Moolenaar1e015462005-09-25 22:16:38 +0000182 if has_key(elements, tag) && has_key(elements, 'common')
183 let values = split(elements[tag]." ".elements['common'])
184 elseif has_key(elements, tag) && !has_key(elements, 'common')
185 let values = split(elements[tag])
186 elseif !has_key(elements, tag) && has_key(elements, 'common')
187 let values = split(elements['common'])
188 else
189 return []
190 endif
191
192 elseif search_for == 'id'
193 " Find used IDs
194 " 1. Catch whole file
195 let filelines = getline(1, line('$'))
196 " 2. Find lines with possible id
197 let used_id_lines = filter(filelines, 'v:val =~ "id\\s*=\\s*[\"''][a-zA-Z0-9_-]\\+"')
198 " 3a. Join all filtered lines
199 let id_string = join(used_id_lines, ' ')
200 " 3b. And split them to be sure each id is in separate item
201 let id_list = split(id_string, 'id\s*=\s*')
202 " 4. Extract id values
203 let used_id = map(id_list, 'matchstr(v:val, "[\"'']\\zs[a-zA-Z0-9_-]\\+\\ze")')
204 let joined_used_id = ','.join(used_id, ',').','
205
206 let allvalues = map(classes, 'matchstr(v:val, ".*#\\zs[a-zA-Z0-9_-]\\+")')
207
208 let values = []
209
210 for element in classes
211 if joined_used_id !~ ','.element.','
212 let values += [element]
213 endif
214
215 endfor
216
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000217 endif
218
219 " We need special version of sbase
220 let classbase = matchstr(a:base, ".*[\"']")
Bram Moolenaar1e015462005-09-25 22:16:38 +0000221 let classquote = matchstr(classbase, '.$')
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000222
223 let entered_class = matchstr(attr, ".*=\\s*[\"']\\zs.*")
224
225 for m in sort(values)
226 if m =~? '^'.entered_class
227 call add(res, classbase . m . classquote . ' ')
228 elseif m =~? entered_class
229 call add(res2, classbase . m . classquote . ' ')
230 endif
231 endfor
232
233 return res + res2
234
235 elseif a:base =~ "style\\s*=\\s*[\"'][^\"']*$"
236 return csscomplete#CompleteCSS(0, a:base)
237
238 endif
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000239 let stripbase = matchstr(a:base, ".*\\(on[a-z]*\\|style\\|class\\)\\s*=\\s*[\"']\\zs.*")
240 " Now we have a:base stripped from all chars up to style/class.
241 " It may fail with some strange style value combinations.
242 if stripbase !~ "[\"']"
243 return []
244 endif
245 endif
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000246 " If attr contains =\s*[\"'] we catched value of attribute
247 if attr =~ "=\s*[\"']"
248 " Let do attribute specific completion
249 let attrname = matchstr(attr, '.*\ze\s*=')
250 let entered_value = matchstr(attr, ".*=\\s*[\"']\\zs.*")
251 let values = []
252 if attrname == 'media'
253 let values = ["screen", "tty", "tv", "projection", "handheld", "print", "braille", "aural", "all"]
254 elseif attrname == 'xml:space'
255 let values = ["preserve"]
256 elseif attrname == 'shape'
257 if a:base =~ '^a\>'
258 let values = ["rect"]
259 else
260 let values = ["rect", "circle", "poly", "default"]
261 endif
262 elseif attrname == 'valuetype'
263 let values = ["data", "ref", "object"]
264 elseif attrname == 'method'
265 let values = ["get", "post"]
Bram Moolenaar4c903f92005-09-14 21:32:32 +0000266 elseif attrname == 'dir'
267 let values = ["ltr", "rtl"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000268 elseif attrname == 'frame'
269 let values = ["void", "above", "below", "hsides", "lhs", "rhs", "vsides", "box", "border"]
270 elseif attrname == 'rules'
271 let values = ["none", "groups", "rows", "all"]
272 elseif attrname == 'align'
273 let values = ["left", "center", "right", "justify", "char"]
274 elseif attrname == 'valign'
275 let values = ["top", "middle", "bottom", "baseline"]
276 elseif attrname == 'scope'
277 let values = ["row", "col", "rowgroup", "colgroup"]
278 elseif attrname == 'href'
279 " Now we are looking for local anchors defined by name or id
280 if entered_value =~ '^#'
281 let file = join(getline(1, line('$')), ' ')
282 " Split it be sure there will be one id/name element in
283 " item, it will be also first word [a-zA-Z0-9_-] in element
284 let oneelement = split(file, "\\(meta \\)\\@<!\\(name\\|id\\)\\s*=\\s*[\"']")
285 for i in oneelement
286 let values += ['#'.matchstr(i, "^[a-zA-Z][a-zA-Z0-9%_-]*")]
287 endfor
288 endif
289 elseif attrname == 'type'
290 if a:base =~ '^input'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000291 let values = ["text", "password", "checkbox", "radio", "submit", "reset", "file", "hidden", "image", "button"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000292 elseif a:base =~ '^button'
293 let values = ["button", "submit", "reset"]
Bram Moolenaar1e015462005-09-25 22:16:38 +0000294 elseif a:base =~ '^style'
295 let values = ["text/css"]
296 elseif a:base =~ '^script'
297 let values = ["text/javascript"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000298 endif
299 else
300 return []
301 endif
302
303 if len(values) == 0
304 return []
305 endif
306
307 " We need special version of sbase
308 let attrbase = matchstr(a:base, ".*[\"']")
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000309 let attrquote = matchstr(attrbase, '.$')
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000310
311 for m in values
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000312 " This if is needed to not offer all completions as-is
313 " alphabetically but sort them. Those beginning with entered
314 " part will be as first choices
315 if m =~ '^'.entered_value
316 call add(res, attrbase . m . attrquote.' ')
317 elseif m =~ entered_value
318 call add(res2, attrbase . m . attrquote.' ')
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000319 endif
320 endfor
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000321
322 return res + res2
323
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000324 endif
325 " Shorten a:base to not include last word
326 let sbase = matchstr(a:base, '.*\ze\s.*')
327 if tag =~ '^\(abbr\|acronym\|b\|bdo\|big\|caption\|cite\|code\|dd\|dfn\|div\|dl\|dt\|em\|fieldset\|h\d\|kbd\|li\|noscript\|ol\|p\|samp\|small\|span\|strong\|sub\|sup\|tt\|ul\|var\)$'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000328 let attrs = coregroup
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000329 elseif tag == 'a'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000330 let attrs = coregroup + focus + ["charset", "type", "name", "href", "hreflang", "rel", "rev", "shape", "coords"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000331 elseif tag == 'area'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000332 let attrs = coregroup
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000333 elseif tag == 'base'
334 let attrs = ["href", "id"]
335 elseif tag == 'blockquote'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000336 let attrs = coregroup + ["cite"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000337 elseif tag == 'body'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000338 let attrs = coregroup + ["onload", "onunload"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000339 elseif tag == 'br'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000340 let attrs = coreattrs
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000341 elseif tag == 'button'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000342 let attrs = coreattrs + focus + ["name", "value", "type"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000343 elseif tag == '^\(col\|colgroup\)$'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000344 let attrs = coreattrs + ["span", "width", "align", "char", "charoff", "valign"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000345 elseif tag =~ '^\(del\|ins\)$'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000346 let attrs = coreattrs + ["cite", "datetime"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000347 elseif tag == 'form'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000348 let attrs = coreattrs + ["action", "method=\"get\" ", "method=\"post\" ", "enctype", "onsubmit", "onreset", "accept", "accept-charset"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000349 elseif tag == 'head'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000350 let attrs = i18n + ["id", "profile"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000351 elseif tag == 'html'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000352 let attrs = i18n + ["id", "xmlns"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000353 elseif tag == 'img'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000354 let attrs = coreattrs + ["src", "alt", "longdesc", "height", "width", "usemap", "ismap"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000355 elseif tag == 'input'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000356 let attrs = coreattrs + focus + ["type", "name", "value", "checked", "disabled", "readonly", "size", "maxlength", "src", "alt", "usemap", "onselect", "onchange", "accept"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000357 elseif tag == 'label'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000358 let attrs = coreattrs + ["for", "accesskey", "onfocus", "onblur"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000359 elseif tag == 'legend'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000360 let attrs = coreattrs + ["accesskey"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000361 elseif tag == 'link'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000362 let attrs = coreattrs + ["charset", "href", "hreflang", "type", "rel", "rev", "media"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000363 elseif tag == 'map'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000364 let attrs = i18n + events + ["id", "class", "style", "title", "name"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000365 elseif tag == 'meta'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000366 let attrs = i18n + ["id", "http-equiv", "content", "scheme", "name"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000367 elseif tag == 'title'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000368 let attrs = i18n + ["id"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000369 elseif tag == 'object'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000370 let attrs = coreattrs + ["declare", "classid", "codebase", "data", "type", "codetype", "archive", "standby", "height", "width", "usemap", "name", "tabindex"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000371 elseif tag == 'optgroup'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000372 let attrs = coreattrs + ["disbled", "label"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000373 elseif tag == 'option'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000374 let attrs = coreattrs + ["disbled", "selected", "value", "label"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000375 elseif tag == 'param'
376 let attrs = ["id", "name", "value", "valuetype", "type"]
377 elseif tag == 'pre'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000378 let attrs = coreattrs + ["xml:space"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000379 elseif tag == 'q'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000380 let attrs = coreattrs + ["cite"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000381 elseif tag == 'script'
Bram Moolenaar1e015462005-09-25 22:16:38 +0000382 let attrs = ["id", "charset", "type=\"text/javascript\"", "type", "src", "defer", "xml:space"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000383 elseif tag == 'select'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000384 let attrs = coreattrs + ["name", "size", "multiple", "disabled", "tabindex", "onfocus", "onblur", "onchange"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000385 elseif tag == 'style'
Bram Moolenaar1e015462005-09-25 22:16:38 +0000386 let attrs = coreattrs + ["id", "type=\"text/css\"", "type", "media", "title", "xml:space"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000387 elseif tag == 'table'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000388 let attrs = coreattrs + ["summary", "width", "border", "frame", "rules", "cellspacing", "cellpadding"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000389 elseif tag =~ '^\(thead\|tfoot\|tbody\|tr\)$'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000390 let attrs = coreattrs + ["align", "char", "charoff", "valign"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000391 elseif tag == 'textarea'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000392 let attrs = coreattrs + focus + ["name", "rows", "cols", "disabled", "readonly", "onselect", "onchange"]
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000393 elseif tag =~ '^\(th\|td\)$'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000394 let attrs = coreattrs + ["abbr", "headers", "scope", "rowspan", "colspan", "align", "char", "charoff", "valign"]
395 else
396 return []
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000397 endif
398
399 for m in sort(attrs)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000400 if m =~ '^'.attr
401 if m =~ '^\(ismap\|defer\|declare\|nohref\|checked\|disabled\|selected\|readonly\)$' || m =~ '='
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000402 call add(res, sbase.' '.m)
403 else
404 call add(res, sbase.' '.m.'="')
405 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000406 elseif m =~ attr
407 if m =~ '^\(ismap\|defer\|declare\|nohref\|checked\|disabled\|selected\|readonly\)$' || m =~ '='
408 call add(res2, sbase.' '.m)
409 else
410 call add(res2, sbase.' '.m.'="')
411 endif
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000412 endif
413 endfor
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000414
415 return res + res2
416
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000417 endif
Bram Moolenaar4c903f92005-09-14 21:32:32 +0000418 " Close tag
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000419 let b:unaryTagsStack = "base meta link hr br param img area input col"
420 if a:base =~ '^\/'
421 let opentag = htmlcomplete#GetLastOpenTag("b:unaryTagsStack")
Bram Moolenaar4c903f92005-09-14 21:32:32 +0000422 return ["/".opentag.">"]
423 endif
424 " Deal with tag completion.
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000425 let opentag = htmlcomplete#GetLastOpenTag("b:unaryTagsStack")
426 " Clusters
427 let special = "br span bdo map object img"
428 let phrase = "em strong dfn code q samp kbd var cite abbr acronym sub sup"
429 let inlineforms = "input select textarea label button"
430 let miscinline = "ins del script"
431 let inline = "a ".special." ".phrase." ".inlineforms." tt i b big small"
432 let misc = "noscript ".miscinline
433 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 +0000434
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000435 if opentag == 'a'
436 let tags = split("tt i b big small ".special." ".phrase." ".inlineforms." ".miscinline)
437 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\)$'
438 let tags = split(inline." ".miscinline)
439 elseif opentag == 'pre'
440 let tags = split("a tt i b big small br span bdo map ".phrase." ".miscinline." ".inlineforms)
441 elseif opentag == 'html'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000442 let tags = ["head", "body"]
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000443 elseif opentag == 'legend'
444 let tags = split(inline." ".miscinline)
445 elseif opentag == 'head'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000446 let tags = ["title", "base", "scipt", "style", "meta", "link", "object"]
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000447 elseif opentag =~ '^\(noscript\|body\|blockquote\)$'
448 let tags = split("form ".block." ".misc)
449 elseif opentag =~ '^\(ul\|ol\)$'
450 let tags = ["li"]
451 elseif opentag == 'dl'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000452 let tags = ["dt", "dd"]
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000453 elseif opentag =~ '^\(ins\|del\|th\|td\|dd\|div\|li\)$'
454 let tags = split("form ".block." ".inline." ".misc)
455 elseif opentag == 'object'
456 let tags = split("param form ".block." ".inline." ".misc)
457 elseif opentag == 'fieldset'
458 let tags = split("legend form ".block." ".inline." ".misc)
459 elseif opentag == 'map'
460 let tags = split("area form ".block." ".misc)
461 elseif opentag == 'form'
462 let tags = split(block." ".misc)
463 elseif opentag == 'select'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000464 let tags = ["optgroup", "option"]
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000465 elseif opentag == 'optgroup'
466 let tags = ["option"]
467 elseif opentag == 'colgroup'
468 let tags = ["col"]
469 elseif opentag == '^\(textarea\|option\|script\|style\|title\)$'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000470 let tags = ['empty']
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000471 elseif opentag == 'button'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000472 let tags = ["p", "h1", "h2", "h3", "h4", "h5", "h6", "div", "ul", "ol", "dl", "table"]
473 elseif opentag =~ '^\(thead\|tfoot\|tbody\)$'
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000474 let tags = ["tr"]
475 elseif opentag == 'tr'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000476 let tags = ["th", "td"]
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000477 elseif opentag == 'table'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000478 let tags = ["caption", "col", "colgroup", "thead", "tfoot", "tbody", "tr"]
479 else
480 return []
Bram Moolenaar4c903f92005-09-14 21:32:32 +0000481 endif
482
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000483 for m in tags
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000484 if m =~ '^'.a:base
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000485 call add(res, m)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000486 elseif m =~ a:base
487 call add(res2, m)
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000488 endif
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000489 endfor
490
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000491 return res + res2
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000492
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000493 endif
494endfunction
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000495
496" MM: This is greatly reduced closetag.vim used with kind permission of Steven
497" Mueller
498" Changes: strip all comments; delete error messages
499" Author: Steven Mueller <diffusor@ugcs.caltech.edu>
500" Last Modified: Tue May 24 13:29:48 PDT 2005
501" Version: 0.9.1
502
503function! htmlcomplete#GetLastOpenTag(unaryTagsStack)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000504 let linenum=line('.')
505 let lineend=col('.') - 1 " start: cursor position
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000506 let first=1 " flag for first line searched
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000507 let b:TagStack='' " main stack of tags
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000508 let startInComment=s:InComment()
509
510 let tagpat='</\=\(\k\|[-:]\)\+\|/>'
511 while (linenum>0)
512 let line=getline(linenum)
513 if first
514 let line=strpart(line,0,lineend)
515 else
516 let lineend=strlen(line)
517 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000518 let b:lineTagStack=''
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000519 let mpos=0
520 let b:TagCol=0
521 while (mpos > -1)
522 let mpos=matchend(line,tagpat)
523 if mpos > -1
524 let b:TagCol=b:TagCol+mpos
525 let tag=matchstr(line,tagpat)
526
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000527 if exists('b:closetag_disable_synID') || startInComment==s:InCommentAt(linenum, b:TagCol)
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000528 let b:TagLine=linenum
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000529 call s:Push(matchstr(tag,'[^<>]\+'),'b:lineTagStack')
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000530 endif
531 let lineend=lineend-mpos
532 let line=strpart(line,mpos,lineend)
533 endif
534 endwhile
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000535 while (!s:EmptystackP('b:lineTagStack'))
536 let tag=s:Pop('b:lineTagStack')
537 if match(tag, '^/') == 0 "found end tag
538 call s:Push(tag,'b:TagStack')
539 elseif s:EmptystackP('b:TagStack') && !s:Instack(tag, a:unaryTagsStack) "found unclosed tag
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000540 return tag
541 else
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000542 let endtag=s:Peekstack('b:TagStack')
543 if endtag == '/'.tag || endtag == '/'
544 call s:Pop('b:TagStack') "found a open/close tag pair
545 elseif !s:Instack(tag, a:unaryTagsStack) "we have a mismatch error
546 return ''
547 endif
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000548 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000549 endwhile
550 let linenum=linenum-1 | let first=0
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000551 endwhile
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000552return ''
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000553endfunction
554
555function! s:InComment()
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000556 return synIDattr(synID(line('.'), col('.'), 0), 'name') =~ 'Comment'
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000557endfunction
558
559function! s:InCommentAt(line, col)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000560 return synIDattr(synID(a:line, a:col, 0), 'name') =~ 'Comment'
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000561endfunction
562
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000563function! s:SetKeywords()
564 let g:IsKeywordBak=&iskeyword
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000565 let &iskeyword='33-255'
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000566endfunction
567
568function! s:RestoreKeywords()
569 let &iskeyword=g:IsKeywordBak
570endfunction
571
572function! s:Push(el, sname)
573 if !s:EmptystackP(a:sname)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000574 exe 'let '.a:sname."=a:el.' '.".a:sname
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000575 else
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000576 exe 'let '.a:sname.'=a:el'
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000577 endif
578endfunction
579
580function! s:EmptystackP(sname)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000581 exe 'let stack='.a:sname
582 if match(stack,'^ *$') == 0
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000583 return 1
584 else
585 return 0
586 endif
587endfunction
588
589function! s:Instack(el, sname)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000590 exe 'let stack='.a:sname
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000591 call s:SetKeywords()
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000592 let m=match(stack, '\<'.a:el.'\>')
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000593 call s:RestoreKeywords()
594 if m < 0
595 return 0
596 else
597 return 1
598 endif
599endfunction
600
601function! s:Peekstack(sname)
602 call s:SetKeywords()
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000603 exe 'let stack='.a:sname
604 let top=matchstr(stack, '\<.\{-1,}\>')
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000605 call s:RestoreKeywords()
606 return top
607endfunction
608
609function! s:Pop(sname)
610 if s:EmptystackP(a:sname)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000611 return ''
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000612 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000613 exe 'let stack='.a:sname
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000614 call s:SetKeywords()
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000615 let loc=matchend(stack,'\<.\{-1,}\>')
616 exe 'let '.a:sname.'=strpart(stack, loc+1, strlen(stack))'
617 let top=strpart(stack, match(stack, '\<'), loc)
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000618 call s:RestoreKeywords()
619 return top
620endfunction
621
622function! s:Clearstack(sname)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000623 exe 'let '.a:sname."=''"
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000624endfunction