blob: e307698a2852e987b52b6783febcdd7ad71f02f6 [file] [log] [blame]
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001" Vim completion script
Bram Moolenaarc1e37902006-04-18 21:55:01 +00002" Language: HTML (XHTML 1.0 Strict by default)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003" Maintainer: Mikolaj Machowski ( mikmach AT wp DOT pl )
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004" Last Change: 2006 Apr 17
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 Moolenaar28c258f2006-01-25 22:02:51 +000011 let curline = line('.')
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000012 let compl_begin = col('.') - 2
13 while start >= 0 && line[start - 1] =~ '\(\k\|[:.-]\)'
14 let start -= 1
Bram Moolenaarf75a9632005-09-13 21:20:47 +000015 endwhile
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000016 " Handling of entities {{{
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000017 if start >= 0 && line[start - 1] =~ '&'
18 let b:entitiescompl = 1
19 let b:compl_context = ''
20 return start
21 endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000022 " }}}
23 " Handling of <style> tag {{{
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000024 let stylestart = searchpair('<style\>', '', '<\/style\>', "bnW")
25 let styleend = searchpair('<style\>', '', '<\/style\>', "nW")
26 if stylestart != 0 && styleend != 0
Bram Moolenaar28c258f2006-01-25 22:02:51 +000027 if stylestart <= curline && styleend >= curline
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000028 let start = col('.') - 1
29 let b:csscompl = 1
30 while start >= 0 && line[start - 1] =~ '\(\k\|-\)'
31 let start -= 1
32 endwhile
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000033 endif
34 endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000035 " }}}
36 " Handling of <script> tag {{{
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000037 let scriptstart = searchpair('<script\>', '', '<\/script\>', "bnW")
38 let scriptend = searchpair('<script\>', '', '<\/script\>', "nW")
39 if scriptstart != 0 && scriptend != 0
40 if scriptstart <= curline && scriptend >= curline
41 let start = col('.') - 1
42 let b:jscompl = 1
43 let b:jsrange = [scriptstart, scriptend]
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000044 while start >= 0 && line[start - 1] =~ '\k'
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000045 let start -= 1
46 endwhile
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000047 " We are inside of <script> tag. But we should also get contents
48 " of all linked external files and (secondary, less probably) other <script> tags
49 " This logic could possible be done in separate function - may be
50 " reused in events scripting (also with option could be reused for
51 " CSS
52 let b:js_extfiles = []
53 let l = line('.')
54 let c = col('.')
55 call cursor(1,1)
56 while search('<\@<=script\>', 'W') && line('.') <= l
57 if synIDattr(synID(line('.'),col('.')-1,0),"name") !~? 'comment'
58 let sname = matchstr(getline('.'), '<script[^>]*src\s*=\s*\([''"]\)\zs.\{-}\ze\1')
59 if filereadable(sname)
60 let b:js_extfiles += readfile(sname)
61 endif
62 endif
63 endwhile
64 call cursor(1,1)
65 let js_scripttags = []
66 while search('<script\>', 'W') && line('.') < l
67 if matchstr(getline('.'), '<script[^>]*src') == ''
68 let js_scripttag = getline(line('.'), search('</script>', 'W'))
69 let js_scripttags += js_scripttag
70 endif
71 endwhile
72 let b:js_extfiles += js_scripttags
73 call cursor(l,c)
74 unlet! l c
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000075 endif
76 endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000077 " }}}
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000078 if !exists("b:csscompl") && !exists("b:jscompl")
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000079 let b:compl_context = getline('.')[0:(compl_begin)]
Bram Moolenaar28c258f2006-01-25 22:02:51 +000080 if b:compl_context !~ '<[^>]*$'
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000081 " Look like we may have broken tag. Check previous lines.
Bram Moolenaar28c258f2006-01-25 22:02:51 +000082 let i = 1
83 while 1
84 let context_line = getline(curline-i)
85 if context_line =~ '<[^>]*$'
86 " Yep, this is this line
87 let context_lines = getline(curline-i, curline)
88 let b:compl_context = join(context_lines, ' ')
89 break
Bram Moolenaare0fa5602006-03-19 22:08:37 +000090 elseif context_line =~ '>[^<]*$' || i == curline
91 " We are in normal tag line, no need for completion at all
92 " OR reached first line without tag at all
Bram Moolenaar28c258f2006-01-25 22:02:51 +000093 let b:compl_context = ''
94 break
95 endif
96 let i += 1
Bram Moolenaare0fa5602006-03-19 22:08:37 +000097 " We reached first line and no tag approached
98 " Prevents endless loop
99 "if i > curline
100 "let b:compl_context = ''
101 "break
102 "endif
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000103 endwhile
104 " Make sure we don't have counter
105 unlet! i
106 endif
Bram Moolenaar09df3122006-01-23 22:23:09 +0000107 let b:compl_context = matchstr(b:compl_context, '.*\zs<.*')
Bram Moolenaare0fa5602006-03-19 22:08:37 +0000108
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000109 " Return proper start for on-events. Without that beginning of
110 " completion will be badly reported
111 if b:compl_context =~? 'on[a-z]*\s*=\s*\(''[^'']*\|"[^"]*\)$'
112 let start = col('.') - 1
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000113 while start >= 0 && line[start - 1] =~ '\k'
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000114 let start -= 1
115 endwhile
116 endif
Bram Moolenaare0fa5602006-03-19 22:08:37 +0000117 " If b:compl_context begins with <? we are inside of PHP code. It
118 " wasn't closed so PHP completion passed it to HTML
119 if &filetype =~? 'php' && b:compl_context =~ '^<?'
120 let b:phpcompl = 1
121 let start = col('.') - 1
122 while start >= 0 && line[start - 1] =~ '[a-zA-Z_0-9\x7f-\xff$]'
123 let start -= 1
124 endwhile
125 endif
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000126 else
127 let b:compl_context = getline('.')[0:compl_begin]
128 endif
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000129 return start
130 else
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000131 " Initialize base return lists
132 let res = []
133 let res2 = []
134 " a:base is very short - we need context
135 let context = b:compl_context
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000136 " Check if we should do CSS completion inside of <style> tag
Bram Moolenaare0fa5602006-03-19 22:08:37 +0000137 " or JS completion inside of <script> tag or PHP completion in case of <?
138 " tag AND &ft==php
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000139 if exists("b:csscompl")
140 unlet! b:csscompl
Bram Moolenaar09df3122006-01-23 22:23:09 +0000141 let context = b:compl_context
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000142 unlet! b:compl_context
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000143 return csscomplete#CompleteCSS(0, context)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000144 elseif exists("b:jscompl")
145 unlet! b:jscompl
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000146 return javascriptcomplete#CompleteJS(0, a:base)
Bram Moolenaare0fa5602006-03-19 22:08:37 +0000147 elseif exists("b:phpcompl")
148 unlet! b:phpcompl
149 let context = b:compl_context
150 return phpcomplete#CompletePHP(0, a:base)
Bram Moolenaar09df3122006-01-23 22:23:09 +0000151 else
152 if len(b:compl_context) == 0 && !exists("b:entitiescompl")
153 return []
154 endif
155 let context = matchstr(b:compl_context, '.\zs.*')
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000156 endif
Bram Moolenaar09df3122006-01-23 22:23:09 +0000157 unlet! b:compl_context
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000158 " Entities completion {{{
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000159 if exists("b:entitiescompl")
160 unlet! b:entitiescompl
161
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000162 if !exists("g:html_omni")
163 "runtime! autoload/xml/xhtml10s.vim
164 call htmlcomplete#LoadData()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000165 endif
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000166
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000167 let entities = g:html_omni['vimxmlentities']
Bram Moolenaara5792f52005-11-23 21:25:05 +0000168
Bram Moolenaar09df3122006-01-23 22:23:09 +0000169 if len(a:base) == 1
170 for m in entities
171 if m =~ '^'.a:base
172 call add(res, m.';')
173 endif
174 endfor
175 return res
176 else
177 for m in entities
178 if m =~? '^'.a:base
179 call add(res, m.';')
180 elseif m =~? a:base
181 call add(res2, m.';')
182 endif
183 endfor
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000184
Bram Moolenaar09df3122006-01-23 22:23:09 +0000185 return res + res2
186 endif
187
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000188
189 endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000190 " }}}
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000191 if context =~ '>'
192 " Generally if context contains > it means we are outside of tag and
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000193 " should abandon action - with one exception: <style> span { bo
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000194 if context =~ 'style[^>]\{-}>[^<]\{-}$'
195 return csscomplete#CompleteCSS(0, context)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000196 elseif context =~ 'script[^>]\{-}>[^<]\{-}$'
197 let b:jsrange = [line('.'), search('<\/script\>', 'nW')]
198 return javascriptcomplete#CompleteJS(0, context)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000199 else
200 return []
201 endif
202 endif
203
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000204 " If context contains > it means we are already outside of tag and we
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000205 " should abandon action
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000206 " If context contains white space it is attribute.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000207 " It can be also value of attribute.
208 " We have to get first word to offer proper completions
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000209 if context == ''
210 let tag = ''
211 else
212 let tag = split(context)[0]
Bram Moolenaar8424a622006-04-19 21:23:36 +0000213 if tag =~ '[A-Z]'
214 let uppercase_tag = 1
215 let tag = tolower(tag)
216 else
217 let uppercase_tag = 0
218 endif
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000219 endif
Bram Moolenaar8424a622006-04-19 21:23:36 +0000220 let g:ta = tag
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000221 " Get last word, it should be attr name
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000222 let attr = matchstr(context, '.*\s\zs.*')
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000223 " Possible situations where any prediction would be difficult:
224 " 1. Events attributes
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000225 if context =~ '\s'
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000226 " Sort out style, class, and on* cases
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000227 if context =~? "\\(on[a-z]*\\|id\\|style\\|class\\)\\s*=\\s*[\"']"
228 " Id, class completion {{{
229 if context =~? "\\(id\\|class\\)\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
230 if context =~? "class\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
Bram Moolenaar1e015462005-09-25 22:16:38 +0000231 let search_for = "class"
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000232 elseif context =~? "id\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
Bram Moolenaar1e015462005-09-25 22:16:38 +0000233 let search_for = "id"
234 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000235 " Handle class name completion
236 " 1. Find lines of <link stylesheet>
237 " 1a. Check file for @import
238 " 2. Extract filename(s?) of stylesheet,
239 call cursor(1,1)
240 let head = getline(search('<head\>'), search('<\/head>'))
241 let headjoined = join(copy(head), ' ')
242 if headjoined =~ '<style'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000243 " Remove possibly confusing CSS operators
Bram Moolenaar1e015462005-09-25 22:16:38 +0000244 let stylehead = substitute(headjoined, '+>\*[,', ' ', 'g')
245 if search_for == 'class'
246 let styleheadlines = split(stylehead)
247 let headclasslines = filter(copy(styleheadlines), "v:val =~ '\\([a-zA-Z0-9:]\\+\\)\\?\\.[a-zA-Z0-9_-]\\+'")
248 else
249 let stylesheet = split(headjoined, '[{}]')
250 " Get all lines which fit id syntax
251 let classlines = filter(copy(stylesheet), "v:val =~ '#[a-zA-Z0-9_-]\\+'")
252 " Filter out possible color definitions
253 call filter(classlines, "v:val !~ ':\\s*#[a-zA-Z0-9_-]\\+'")
254 " Filter out complex border definitions
255 call filter(classlines, "v:val !~ '\\(none\\|hidden\\|dotted\\|dashed\\|solid\\|double\\|groove\\|ridge\\|inset\\|outset\\)\\s*#[a-zA-Z0-9_-]\\+'")
256 let templines = join(classlines, ' ')
257 let headclasslines = split(templines)
258 call filter(headclasslines, "v:val =~ '#[a-zA-Z0-9_-]\\+'")
259 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000260 let internal = 1
261 else
262 let internal = 0
263 endif
264 let styletable = []
265 let secimportfiles = []
266 let filestable = filter(copy(head), "v:val =~ '\\(@import\\|link.*stylesheet\\)'")
267 for line in filestable
268 if line =~ "@import"
269 let styletable += [matchstr(line, "import\\s\\+\\(url(\\)\\?[\"']\\?\\zs\\f\\+\\ze")]
270 elseif line =~ "<link"
271 let styletable += [matchstr(line, "href\\s*=\\s*[\"']\\zs\\f\\+\\ze")]
272 endif
273 endfor
Bram Moolenaar1e015462005-09-25 22:16:38 +0000274 for file in styletable
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000275 if filereadable(file)
276 let stylesheet = readfile(file)
277 let secimport = filter(copy(stylesheet), "v:val =~ '@import'")
278 if len(secimport) > 0
279 for line in secimport
Bram Moolenaar1e015462005-09-25 22:16:38 +0000280 let secfile = matchstr(line, "import\\s\\+\\(url(\\)\\?[\"']\\?\\zs\\f\\+\\ze")
281 let secfile = fnamemodify(file, ":p:h").'/'.secfile
282 let secimportfiles += [secfile]
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000283 endfor
284 endif
285 endif
286 endfor
287 let cssfiles = styletable + secimportfiles
288 let classes = []
289 for file in cssfiles
290 if filereadable(file)
291 let stylesheet = readfile(file)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000292 let stylefile = join(stylesheet, ' ')
293 let stylefile = substitute(stylefile, '+>\*[,', ' ', 'g')
294 if search_for == 'class'
295 let stylesheet = split(stylefile)
296 let classlines = filter(copy(stylesheet), "v:val =~ '\\([a-zA-Z0-9:]\\+\\)\\?\\.[a-zA-Z0-9_-]\\+'")
297 else
298 let stylesheet = split(stylefile, '[{}]')
299 " Get all lines which fit id syntax
300 let classlines = filter(copy(stylesheet), "v:val =~ '#[a-zA-Z0-9_-]\\+'")
301 " Filter out possible color definitions
302 call filter(classlines, "v:val !~ ':\\s*#[a-zA-Z0-9_-]\\+'")
303 " Filter out complex border definitions
304 call filter(classlines, "v:val !~ '\\(none\\|hidden\\|dotted\\|dashed\\|solid\\|double\\|groove\\|ridge\\|inset\\|outset\\)\\s*#[a-zA-Z0-9_-]\\+'")
305 let templines = join(classlines, ' ')
306 let stylelines = split(templines)
307 let classlines = filter(stylelines, "v:val =~ '#[a-zA-Z0-9_-]\\+'")
308
309 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000310 endif
311 " We gathered classes definitions from all external files
312 let classes += classlines
313 endfor
314 if internal == 1
315 let classes += headclasslines
316 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000317
318 if search_for == 'class'
319 let elements = {}
320 for element in classes
321 if element =~ '^\.'
322 let class = matchstr(element, '^\.\zs[a-zA-Z][a-zA-Z0-9_-]*\ze')
323 let class = substitute(class, ':.*', '', '')
324 if has_key(elements, 'common')
325 let elements['common'] .= ' '.class
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000326 else
Bram Moolenaar1e015462005-09-25 22:16:38 +0000327 let elements['common'] = class
328 endif
329 else
330 let class = matchstr(element, '[a-zA-Z1-6]*\.\zs[a-zA-Z][a-zA-Z0-9_-]*\ze')
331 let tagname = tolower(matchstr(element, '[a-zA-Z1-6]*\ze.'))
332 if tagname != ''
333 if has_key(elements, tagname)
334 let elements[tagname] .= ' '.class
335 else
336 let elements[tagname] = class
337 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000338 endif
339 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000340 endfor
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000341
Bram Moolenaar1e015462005-09-25 22:16:38 +0000342 if has_key(elements, tag) && has_key(elements, 'common')
343 let values = split(elements[tag]." ".elements['common'])
344 elseif has_key(elements, tag) && !has_key(elements, 'common')
345 let values = split(elements[tag])
346 elseif !has_key(elements, tag) && has_key(elements, 'common')
347 let values = split(elements['common'])
348 else
349 return []
350 endif
351
352 elseif search_for == 'id'
353 " Find used IDs
354 " 1. Catch whole file
355 let filelines = getline(1, line('$'))
356 " 2. Find lines with possible id
357 let used_id_lines = filter(filelines, 'v:val =~ "id\\s*=\\s*[\"''][a-zA-Z0-9_-]\\+"')
358 " 3a. Join all filtered lines
359 let id_string = join(used_id_lines, ' ')
360 " 3b. And split them to be sure each id is in separate item
361 let id_list = split(id_string, 'id\s*=\s*')
362 " 4. Extract id values
363 let used_id = map(id_list, 'matchstr(v:val, "[\"'']\\zs[a-zA-Z0-9_-]\\+\\ze")')
364 let joined_used_id = ','.join(used_id, ',').','
365
366 let allvalues = map(classes, 'matchstr(v:val, ".*#\\zs[a-zA-Z0-9_-]\\+")')
367
368 let values = []
369
370 for element in classes
371 if joined_used_id !~ ','.element.','
372 let values += [element]
373 endif
374
375 endfor
376
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000377 endif
378
379 " We need special version of sbase
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000380 let classbase = matchstr(context, ".*[\"']")
Bram Moolenaar1e015462005-09-25 22:16:38 +0000381 let classquote = matchstr(classbase, '.$')
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000382
383 let entered_class = matchstr(attr, ".*=\\s*[\"']\\zs.*")
384
385 for m in sort(values)
386 if m =~? '^'.entered_class
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000387 call add(res, m . classquote)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000388 elseif m =~? entered_class
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000389 call add(res2, m . classquote)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000390 endif
391 endfor
392
393 return res + res2
394
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000395 elseif context =~? "style\\s*=\\s*[\"'][^\"']*$"
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000396 return csscomplete#CompleteCSS(0, context)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000397
398 endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000399 " }}}
400 " Complete on-events {{{
401 if context =~? 'on[a-z]*\s*=\s*\(''[^'']*\|"[^"]*\)$'
402 " We have to:
403 " 1. Find external files
404 let b:js_extfiles = []
405 let l = line('.')
406 let c = col('.')
407 call cursor(1,1)
408 while search('<\@<=script\>', 'W') && line('.') <= l
409 if synIDattr(synID(line('.'),col('.')-1,0),"name") !~? 'comment'
410 let sname = matchstr(getline('.'), '<script[^>]*src\s*=\s*\([''"]\)\zs.\{-}\ze\1')
411 if filereadable(sname)
412 let b:js_extfiles += readfile(sname)
413 endif
414 endif
415 endwhile
416 " 2. Find at least one <script> tag
417 call cursor(1,1)
418 let js_scripttags = []
419 while search('<script\>', 'W') && line('.') < l
420 if matchstr(getline('.'), '<script[^>]*src') == ''
421 let js_scripttag = getline(line('.'), search('</script>', 'W'))
422 let js_scripttags += js_scripttag
423 endif
424 endwhile
425 let b:js_extfiles += js_scripttags
426
427 " 3. Proper call for javascriptcomplete#CompleteJS
428 call cursor(l,c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000429 let js_context = matchstr(a:base, '\k\+$')
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000430 let js_shortcontext = substitute(a:base, js_context.'$', '', '')
431 let b:compl_context = context
432 let b:jsrange = [l, l]
433 unlet! l c
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000434 return javascriptcomplete#CompleteJS(0, js_context)
435
436 endif
437
438 " }}}
439 let stripbase = matchstr(context, ".*\\(on[a-zA-Z]*\\|style\\|class\\)\\s*=\\s*[\"']\\zs.*")
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000440 " Now we have context stripped from all chars up to style/class.
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000441 " It may fail with some strange style value combinations.
442 if stripbase !~ "[\"']"
443 return []
444 endif
445 endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000446 " Value of attribute completion {{{
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000447 " If attr contains =\s*[\"'] we catched value of attribute
Bram Moolenaar8424a622006-04-19 21:23:36 +0000448 if attr =~ "=\s*[\"']" || attr =~ "=\s*$"
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000449 " Let do attribute specific completion
450 let attrname = matchstr(attr, '.*\ze\s*=')
Bram Moolenaar8424a622006-04-19 21:23:36 +0000451 let entered_value = matchstr(attr, ".*=\\s*[\"']\\?\\zs.*")
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000452 let values = []
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000453 if attrname == 'href'
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000454 " Now we are looking for local anchors defined by name or id
455 if entered_value =~ '^#'
456 let file = join(getline(1, line('$')), ' ')
457 " Split it be sure there will be one id/name element in
458 " item, it will be also first word [a-zA-Z0-9_-] in element
459 let oneelement = split(file, "\\(meta \\)\\@<!\\(name\\|id\\)\\s*=\\s*[\"']")
460 for i in oneelement
461 let values += ['#'.matchstr(i, "^[a-zA-Z][a-zA-Z0-9%_-]*")]
462 endfor
463 endif
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000464 else
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000465 if has_key(g:html_omni, tag) && has_key(g:html_omni[tag][1], attrname)
466 let values = g:html_omni[tag][1][attrname]
467 else
468 return []
469 endif
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000470 endif
471
472 if len(values) == 0
473 return []
474 endif
475
476 " We need special version of sbase
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000477 let attrbase = matchstr(context, ".*[\"']")
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000478 let attrquote = matchstr(attrbase, '.$')
Bram Moolenaar8424a622006-04-19 21:23:36 +0000479 if attrquote !~ "['\"]"
480 let attrquoteopen = '"'
481 let attrquote = '"'
482 else
483 let attrquoteopen = ''
484 endif
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000485
486 for m in values
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000487 " This if is needed to not offer all completions as-is
488 " alphabetically but sort them. Those beginning with entered
489 " part will be as first choices
490 if m =~ '^'.entered_value
Bram Moolenaar8424a622006-04-19 21:23:36 +0000491 call add(res, attrquoteopen . m . attrquote.' ')
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000492 elseif m =~ entered_value
Bram Moolenaar8424a622006-04-19 21:23:36 +0000493 call add(res2, attrquoteopen . m . attrquote.' ')
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000494 endif
495 endfor
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000496
497 return res + res2
498
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000499 endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000500 " }}}
501 " Attribute completion {{{
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000502 " Shorten context to not include last word
503 let sbase = matchstr(context, '.*\ze\s.*')
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000504
505 " Load data {{{
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000506 if !exists("g:html_omni_gen")
507 call htmlcomplete#LoadData()
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000508 endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000509 " }}}
Bram Moolenaar8424a622006-04-19 21:23:36 +0000510
511 if has_key(g:html_omni, tag)
512 let attrs = keys(g:html_omni[tag][1])
513 else
514 return []
515 endif
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000516
517 for m in sort(attrs)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000518 if m =~ '^'.attr
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000519 call add(res, m)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000520 elseif m =~ attr
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000521 call add(res2, m)
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000522 endif
523 endfor
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000524 let menu = res + res2
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000525 if has_key(g:html_omni, 'vimxmlattrinfo')
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000526 let final_menu = []
527 for i in range(len(menu))
528 let item = menu[i]
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000529 if has_key(g:html_omni['vimxmlattrinfo'], item)
530 let m_menu = g:html_omni['vimxmlattrinfo'][item][0]
531 let m_info = g:html_omni['vimxmlattrinfo'][item][1]
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000532 if m_menu !~ 'Bool'
533 let item .= '="'
534 endif
535 else
536 let m_menu = ''
537 let m_info = ''
538 let item .= '="'
539 endif
540 let final_menu += [{'word':item, 'menu':m_menu, 'info':m_info}]
541 endfor
542 else
543 let final_menu = map(menu, 'v:val."=\""')
544 endif
545 return final_menu
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000546
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000547 endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000548 " }}}
549 " Close tag {{{
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000550 let b:unaryTagsStack = "base meta link hr br param img area input col"
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000551 if context =~ '^\/'
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000552 if context =~ '^\/.'
553 return []
554 else
555 let opentag = xmlcomplete#GetLastOpenTag("b:unaryTagsStack")
556 return [opentag.">"]
557 endif
Bram Moolenaar4c903f92005-09-14 21:32:32 +0000558 endif
Bram Moolenaar8424a622006-04-19 21:23:36 +0000559 " }}}
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000560 " Load data {{{
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000561 if !exists("g:html_omni")
562 "runtime! autoload/xml/xhtml10s.vim
563 call htmlcomplete#LoadData()
Bram Moolenaar4c903f92005-09-14 21:32:32 +0000564 endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000565 " }}}
566 " Tag completion {{{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000567 " Deal with tag completion.
Bram Moolenaar8424a622006-04-19 21:23:36 +0000568 let opentag = tolower(xmlcomplete#GetLastOpenTag("b:unaryTagsStack"))
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000569 " MM: TODO: GLOT works always the same but with some weird situation it
570 " behaves as intended in HTML but screws in PHP
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000571 if opentag == '' || &ft == 'php' && !has_key(g:html_omni, opentag)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000572 " Hack for sometimes failing GetLastOpenTag.
573 " As far as I tested fail isn't GLOT fault but problem
574 " of invalid document - not properly closed tags and other mish-mash.
575 " Also when document is empty. Return list of *all* tags.
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000576 let tags = keys(g:html_omni)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000577 call filter(tags, 'v:val !~ "^vimxml"')
578 else
Bram Moolenaar8424a622006-04-19 21:23:36 +0000579 if has_key(g:html_omni, opentag)
580 let tags = g:html_omni[opentag][0]
581 else
582 return []
583 endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000584 endif
585 " }}}
Bram Moolenaar8424a622006-04-19 21:23:36 +0000586
587 if exists("uppercase_tag") && uppercase_tag == 1
588 let context = tolower(context)
589 endif
Bram Moolenaar4c903f92005-09-14 21:32:32 +0000590
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000591 for m in sort(tags)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000592 if m =~ '^'.context
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000593 call add(res, m)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000594 elseif m =~ context
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000595 call add(res2, m)
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000596 endif
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000597 endfor
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000598 let menu = res + res2
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000599 if has_key(g:html_omni, 'vimxmltaginfo')
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000600 let final_menu = []
601 for i in range(len(menu))
602 let item = menu[i]
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000603 if has_key(g:html_omni['vimxmltaginfo'], item)
604 let m_menu = g:html_omni['vimxmltaginfo'][item][0]
605 let m_info = g:html_omni['vimxmltaginfo'][item][1]
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000606 else
607 let m_menu = ''
608 let m_info = ''
609 endif
610 let final_menu += [{'word':item, 'menu':m_menu, 'info':m_info}]
611 endfor
612 else
613 let final_menu = menu
614 endif
615 return final_menu
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000616
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000617 " }}}
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000618 endif
619endfunction
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000620
621function! htmlcomplete#LoadData() " {{{
622 if !exists("g:html_omni_flavor")
623 let g:html_omni_flavor = 'xhtml10s'
624 endif
625 exe 'runtime! autoload/xml/'.g:html_omni_flavor.'.vim'
626 " This one is necessary because we don't know if
627 " g:html_omni_flavor file exists and was sourced
628 " Proper checking for files would require iterating through 'rtp'
629 " and could introduce OS dependent mess.
630 if !exists("g:xmldata_".g:html_omni_flavor)
631 let g:html_omni_flavor = 'xhtml10s'
632 runtime! autoload/xml/xhtml10s.vim
633 endif
634
635 exe 'let g:html_omni = g:xmldata_'.g:html_omni_flavor
636
637 " Free some memory
638 exe 'unlet! g:xmldata_'.g:html_omni_flavor
639
640 "call htmlcomplete#LoadData()
641endfunction
642" }}}
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000643" vim:set foldmethod=marker: