blob: 5420321b6e7503d4e58be53e0cd8affa87af5058 [file] [log] [blame]
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001" Vim completion script
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00002" Language: HTML and XHTML
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003" Maintainer: Mikolaj Machowski ( mikmach AT wp DOT pl )
Bram Moolenaard5ab34b2007-05-05 17:15:44 +00004" Last Change: 2006 Oct 19
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
Bram Moolenaar4a85b412006-04-23 22:40:29 +000013 while start >= 0 && line[start - 1] =~ '\(\k\|[!:.-]\)'
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000014 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")
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000026 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")
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000039 if scriptstart != 0 && scriptend != 0
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000040 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
Bram Moolenaard5ab34b2007-05-05 17:15:44 +000087 let context_lines = getline(curline-i, curline-1) + [b:compl_context]
Bram Moolenaar28c258f2006-01-25 22:02:51 +000088 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
97 endwhile
98 " Make sure we don't have counter
99 unlet! i
100 endif
Bram Moolenaar09df3122006-01-23 22:23:09 +0000101 let b:compl_context = matchstr(b:compl_context, '.*\zs<.*')
Bram Moolenaare0fa5602006-03-19 22:08:37 +0000102
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000103 " Return proper start for on-events. Without that beginning of
104 " completion will be badly reported
105 if b:compl_context =~? 'on[a-z]*\s*=\s*\(''[^'']*\|"[^"]*\)$'
106 let start = col('.') - 1
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000107 while start >= 0 && line[start - 1] =~ '\k'
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000108 let start -= 1
109 endwhile
110 endif
Bram Moolenaare0fa5602006-03-19 22:08:37 +0000111 " If b:compl_context begins with <? we are inside of PHP code. It
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000112 " wasn't closed so PHP completion passed it to HTML
Bram Moolenaare0fa5602006-03-19 22:08:37 +0000113 if &filetype =~? 'php' && b:compl_context =~ '^<?'
114 let b:phpcompl = 1
115 let start = col('.') - 1
116 while start >= 0 && line[start - 1] =~ '[a-zA-Z_0-9\x7f-\xff$]'
117 let start -= 1
118 endwhile
119 endif
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000120 else
121 let b:compl_context = getline('.')[0:compl_begin]
122 endif
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000123 return start
124 else
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000125 " Initialize base return lists
126 let res = []
127 let res2 = []
128 " a:base is very short - we need context
129 let context = b:compl_context
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000130 " Check if we should do CSS completion inside of <style> tag
Bram Moolenaare0fa5602006-03-19 22:08:37 +0000131 " or JS completion inside of <script> tag or PHP completion in case of <?
132 " tag AND &ft==php
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000133 if exists("b:csscompl")
134 unlet! b:csscompl
Bram Moolenaar09df3122006-01-23 22:23:09 +0000135 let context = b:compl_context
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000136 unlet! b:compl_context
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000137 return csscomplete#CompleteCSS(0, context)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000138 elseif exists("b:jscompl")
139 unlet! b:jscompl
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000140 return javascriptcomplete#CompleteJS(0, a:base)
Bram Moolenaare0fa5602006-03-19 22:08:37 +0000141 elseif exists("b:phpcompl")
142 unlet! b:phpcompl
143 let context = b:compl_context
144 return phpcomplete#CompletePHP(0, a:base)
Bram Moolenaar09df3122006-01-23 22:23:09 +0000145 else
146 if len(b:compl_context) == 0 && !exists("b:entitiescompl")
147 return []
148 endif
149 let context = matchstr(b:compl_context, '.\zs.*')
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000150 endif
Bram Moolenaar09df3122006-01-23 22:23:09 +0000151 unlet! b:compl_context
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000152 " Entities completion {{{
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000153 if exists("b:entitiescompl")
154 unlet! b:entitiescompl
155
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000156 if !exists("b:html_doctype")
157 call htmlcomplete#CheckDoctype()
158 endif
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000159 if !exists("b:html_omni")
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000160 "runtime! autoload/xml/xhtml10s.vim
161 call htmlcomplete#LoadData()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000162 endif
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000163
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000164 let entities = b:html_omni['vimxmlentities']
Bram Moolenaara5792f52005-11-23 21:25:05 +0000165
Bram Moolenaar09df3122006-01-23 22:23:09 +0000166 if len(a:base) == 1
167 for m in entities
168 if m =~ '^'.a:base
169 call add(res, m.';')
170 endif
171 endfor
172 return res
173 else
174 for m in entities
175 if m =~? '^'.a:base
176 call add(res, m.';')
177 elseif m =~? a:base
178 call add(res2, m.';')
179 endif
180 endfor
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000181
Bram Moolenaar09df3122006-01-23 22:23:09 +0000182 return res + res2
183 endif
184
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000185
186 endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000187 " }}}
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000188 if context =~ '>'
189 " Generally if context contains > it means we are outside of tag and
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000190 " should abandon action - with one exception: <style> span { bo
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000191 if context =~ 'style[^>]\{-}>[^<]\{-}$'
192 return csscomplete#CompleteCSS(0, context)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +0000193 elseif context =~ 'script[^>]\{-}>[^<]\{-}$'
194 let b:jsrange = [line('.'), search('<\/script\>', 'nW')]
195 return javascriptcomplete#CompleteJS(0, context)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000196 else
197 return []
198 endif
199 endif
200
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000201 " If context contains > it means we are already outside of tag and we
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000202 " should abandon action
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000203 " If context contains white space it is attribute.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000204 " It can be also value of attribute.
205 " We have to get first word to offer proper completions
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000206 if context == ''
207 let tag = ''
208 else
209 let tag = split(context)[0]
Bram Moolenaar4a85b412006-04-23 22:40:29 +0000210 " Detect if tag is uppercase to return in proper case,
211 " we need to make it lowercase for processing
212 if tag =~ '^[A-Z]*$'
Bram Moolenaar8424a622006-04-19 21:23:36 +0000213 let uppercase_tag = 1
214 let tag = tolower(tag)
215 else
216 let uppercase_tag = 0
217 endif
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000218 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000219 " Get last word, it should be attr name
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000220 let attr = matchstr(context, '.*\s\zs.*')
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000221 " Possible situations where any prediction would be difficult:
222 " 1. Events attributes
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000223 if context =~ '\s'
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000224 " Sort out style, class, and on* cases
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000225 if context =~? "\\(on[a-z]*\\|id\\|style\\|class\\)\\s*=\\s*[\"']"
226 " Id, class completion {{{
227 if context =~? "\\(id\\|class\\)\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
228 if context =~? "class\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
Bram Moolenaar1e015462005-09-25 22:16:38 +0000229 let search_for = "class"
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000230 elseif context =~? "id\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
Bram Moolenaar1e015462005-09-25 22:16:38 +0000231 let search_for = "id"
232 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000233 " Handle class name completion
234 " 1. Find lines of <link stylesheet>
235 " 1a. Check file for @import
236 " 2. Extract filename(s?) of stylesheet,
237 call cursor(1,1)
238 let head = getline(search('<head\>'), search('<\/head>'))
239 let headjoined = join(copy(head), ' ')
240 if headjoined =~ '<style'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000241 " Remove possibly confusing CSS operators
Bram Moolenaar1e015462005-09-25 22:16:38 +0000242 let stylehead = substitute(headjoined, '+>\*[,', ' ', 'g')
243 if search_for == 'class'
244 let styleheadlines = split(stylehead)
245 let headclasslines = filter(copy(styleheadlines), "v:val =~ '\\([a-zA-Z0-9:]\\+\\)\\?\\.[a-zA-Z0-9_-]\\+'")
246 else
247 let stylesheet = split(headjoined, '[{}]')
248 " Get all lines which fit id syntax
249 let classlines = filter(copy(stylesheet), "v:val =~ '#[a-zA-Z0-9_-]\\+'")
250 " Filter out possible color definitions
251 call filter(classlines, "v:val !~ ':\\s*#[a-zA-Z0-9_-]\\+'")
252 " Filter out complex border definitions
253 call filter(classlines, "v:val !~ '\\(none\\|hidden\\|dotted\\|dashed\\|solid\\|double\\|groove\\|ridge\\|inset\\|outset\\)\\s*#[a-zA-Z0-9_-]\\+'")
254 let templines = join(classlines, ' ')
255 let headclasslines = split(templines)
256 call filter(headclasslines, "v:val =~ '#[a-zA-Z0-9_-]\\+'")
257 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000258 let internal = 1
259 else
260 let internal = 0
261 endif
262 let styletable = []
263 let secimportfiles = []
264 let filestable = filter(copy(head), "v:val =~ '\\(@import\\|link.*stylesheet\\)'")
265 for line in filestable
266 if line =~ "@import"
267 let styletable += [matchstr(line, "import\\s\\+\\(url(\\)\\?[\"']\\?\\zs\\f\\+\\ze")]
268 elseif line =~ "<link"
269 let styletable += [matchstr(line, "href\\s*=\\s*[\"']\\zs\\f\\+\\ze")]
270 endif
271 endfor
Bram Moolenaar1e015462005-09-25 22:16:38 +0000272 for file in styletable
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000273 if filereadable(file)
274 let stylesheet = readfile(file)
275 let secimport = filter(copy(stylesheet), "v:val =~ '@import'")
276 if len(secimport) > 0
277 for line in secimport
Bram Moolenaar1e015462005-09-25 22:16:38 +0000278 let secfile = matchstr(line, "import\\s\\+\\(url(\\)\\?[\"']\\?\\zs\\f\\+\\ze")
279 let secfile = fnamemodify(file, ":p:h").'/'.secfile
280 let secimportfiles += [secfile]
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000281 endfor
282 endif
283 endif
284 endfor
285 let cssfiles = styletable + secimportfiles
286 let classes = []
287 for file in cssfiles
288 if filereadable(file)
289 let stylesheet = readfile(file)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000290 let stylefile = join(stylesheet, ' ')
291 let stylefile = substitute(stylefile, '+>\*[,', ' ', 'g')
292 if search_for == 'class'
293 let stylesheet = split(stylefile)
294 let classlines = filter(copy(stylesheet), "v:val =~ '\\([a-zA-Z0-9:]\\+\\)\\?\\.[a-zA-Z0-9_-]\\+'")
295 else
296 let stylesheet = split(stylefile, '[{}]')
297 " Get all lines which fit id syntax
298 let classlines = filter(copy(stylesheet), "v:val =~ '#[a-zA-Z0-9_-]\\+'")
299 " Filter out possible color definitions
300 call filter(classlines, "v:val !~ ':\\s*#[a-zA-Z0-9_-]\\+'")
301 " Filter out complex border definitions
302 call filter(classlines, "v:val !~ '\\(none\\|hidden\\|dotted\\|dashed\\|solid\\|double\\|groove\\|ridge\\|inset\\|outset\\)\\s*#[a-zA-Z0-9_-]\\+'")
303 let templines = join(classlines, ' ')
304 let stylelines = split(templines)
305 let classlines = filter(stylelines, "v:val =~ '#[a-zA-Z0-9_-]\\+'")
306
307 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000308 endif
309 " We gathered classes definitions from all external files
310 let classes += classlines
311 endfor
312 if internal == 1
313 let classes += headclasslines
314 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000315
316 if search_for == 'class'
317 let elements = {}
318 for element in classes
319 if element =~ '^\.'
320 let class = matchstr(element, '^\.\zs[a-zA-Z][a-zA-Z0-9_-]*\ze')
321 let class = substitute(class, ':.*', '', '')
322 if has_key(elements, 'common')
323 let elements['common'] .= ' '.class
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000324 else
Bram Moolenaar1e015462005-09-25 22:16:38 +0000325 let elements['common'] = class
326 endif
327 else
328 let class = matchstr(element, '[a-zA-Z1-6]*\.\zs[a-zA-Z][a-zA-Z0-9_-]*\ze')
329 let tagname = tolower(matchstr(element, '[a-zA-Z1-6]*\ze.'))
330 if tagname != ''
331 if has_key(elements, tagname)
332 let elements[tagname] .= ' '.class
333 else
334 let elements[tagname] = class
335 endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000336 endif
337 endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000338 endfor
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000339
Bram Moolenaar1e015462005-09-25 22:16:38 +0000340 if has_key(elements, tag) && has_key(elements, 'common')
341 let values = split(elements[tag]." ".elements['common'])
342 elseif has_key(elements, tag) && !has_key(elements, 'common')
343 let values = split(elements[tag])
344 elseif !has_key(elements, tag) && has_key(elements, 'common')
345 let values = split(elements['common'])
346 else
347 return []
348 endif
349
350 elseif search_for == 'id'
351 " Find used IDs
352 " 1. Catch whole file
353 let filelines = getline(1, line('$'))
354 " 2. Find lines with possible id
355 let used_id_lines = filter(filelines, 'v:val =~ "id\\s*=\\s*[\"''][a-zA-Z0-9_-]\\+"')
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000356 " 3a. Join all filtered lines
Bram Moolenaar1e015462005-09-25 22:16:38 +0000357 let id_string = join(used_id_lines, ' ')
358 " 3b. And split them to be sure each id is in separate item
359 let id_list = split(id_string, 'id\s*=\s*')
360 " 4. Extract id values
361 let used_id = map(id_list, 'matchstr(v:val, "[\"'']\\zs[a-zA-Z0-9_-]\\+\\ze")')
362 let joined_used_id = ','.join(used_id, ',').','
363
364 let allvalues = map(classes, 'matchstr(v:val, ".*#\\zs[a-zA-Z0-9_-]\\+")')
365
366 let values = []
367
368 for element in classes
369 if joined_used_id !~ ','.element.','
370 let values += [element]
371 endif
372
373 endfor
374
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000375 endif
376
377 " We need special version of sbase
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000378 let classbase = matchstr(context, ".*[\"']")
Bram Moolenaar1e015462005-09-25 22:16:38 +0000379 let classquote = matchstr(classbase, '.$')
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000380
381 let entered_class = matchstr(attr, ".*=\\s*[\"']\\zs.*")
382
383 for m in sort(values)
384 if m =~? '^'.entered_class
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000385 call add(res, m . classquote)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000386 elseif m =~? entered_class
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000387 call add(res2, m . classquote)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000388 endif
389 endfor
390
391 return res + res2
392
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000393 elseif context =~? "style\\s*=\\s*[\"'][^\"']*$"
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000394 return csscomplete#CompleteCSS(0, context)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000395
396 endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000397 " }}}
398 " Complete on-events {{{
399 if context =~? 'on[a-z]*\s*=\s*\(''[^'']*\|"[^"]*\)$'
400 " We have to:
401 " 1. Find external files
402 let b:js_extfiles = []
403 let l = line('.')
404 let c = col('.')
405 call cursor(1,1)
406 while search('<\@<=script\>', 'W') && line('.') <= l
407 if synIDattr(synID(line('.'),col('.')-1,0),"name") !~? 'comment'
408 let sname = matchstr(getline('.'), '<script[^>]*src\s*=\s*\([''"]\)\zs.\{-}\ze\1')
409 if filereadable(sname)
410 let b:js_extfiles += readfile(sname)
411 endif
412 endif
413 endwhile
414 " 2. Find at least one <script> tag
415 call cursor(1,1)
416 let js_scripttags = []
417 while search('<script\>', 'W') && line('.') < l
418 if matchstr(getline('.'), '<script[^>]*src') == ''
419 let js_scripttag = getline(line('.'), search('</script>', 'W'))
420 let js_scripttags += js_scripttag
421 endif
422 endwhile
423 let b:js_extfiles += js_scripttags
424
425 " 3. Proper call for javascriptcomplete#CompleteJS
426 call cursor(l,c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000427 let js_context = matchstr(a:base, '\k\+$')
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000428 let js_shortcontext = substitute(a:base, js_context.'$', '', '')
429 let b:compl_context = context
430 let b:jsrange = [l, l]
431 unlet! l c
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000432 return javascriptcomplete#CompleteJS(0, js_context)
433
434 endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000435
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000436 " }}}
437 let stripbase = matchstr(context, ".*\\(on[a-zA-Z]*\\|style\\|class\\)\\s*=\\s*[\"']\\zs.*")
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000438 " Now we have context stripped from all chars up to style/class.
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000439 " It may fail with some strange style value combinations.
440 if stripbase !~ "[\"']"
441 return []
442 endif
443 endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000444 " Value of attribute completion {{{
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000445 " If attr contains =\s*[\"'] we catched value of attribute
Bram Moolenaar8424a622006-04-19 21:23:36 +0000446 if attr =~ "=\s*[\"']" || attr =~ "=\s*$"
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000447 " Let do attribute specific completion
448 let attrname = matchstr(attr, '.*\ze\s*=')
Bram Moolenaar8424a622006-04-19 21:23:36 +0000449 let entered_value = matchstr(attr, ".*=\\s*[\"']\\?\\zs.*")
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000450 let values = []
Bram Moolenaard5ab34b2007-05-05 17:15:44 +0000451 " Load data {{{
452 if !exists("b:html_doctype")
453 call htmlcomplete#CheckDoctype()
454 endif
455 if !exists("b:html_omni")
456 "runtime! autoload/xml/xhtml10s.vim
457 call htmlcomplete#LoadData()
458 endif
459 " }}}
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000460 if attrname == 'href'
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000461 " Now we are looking for local anchors defined by name or id
462 if entered_value =~ '^#'
463 let file = join(getline(1, line('$')), ' ')
464 " Split it be sure there will be one id/name element in
465 " item, it will be also first word [a-zA-Z0-9_-] in element
466 let oneelement = split(file, "\\(meta \\)\\@<!\\(name\\|id\\)\\s*=\\s*[\"']")
467 for i in oneelement
468 let values += ['#'.matchstr(i, "^[a-zA-Z][a-zA-Z0-9%_-]*")]
469 endfor
470 endif
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000471 else
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000472 if has_key(b:html_omni, tag) && has_key(b:html_omni[tag][1], attrname)
473 let values = b:html_omni[tag][1][attrname]
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000474 else
475 return []
476 endif
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000477 endif
478
479 if len(values) == 0
480 return []
481 endif
482
483 " We need special version of sbase
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000484 let attrbase = matchstr(context, ".*[\"']")
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000485 let attrquote = matchstr(attrbase, '.$')
Bram Moolenaar8424a622006-04-19 21:23:36 +0000486 if attrquote !~ "['\"]"
487 let attrquoteopen = '"'
488 let attrquote = '"'
489 else
490 let attrquoteopen = ''
491 endif
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000492
493 for m in values
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000494 " This if is needed to not offer all completions as-is
495 " alphabetically but sort them. Those beginning with entered
496 " part will be as first choices
497 if m =~ '^'.entered_value
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000498 call add(res, attrquoteopen . m . attrquote)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000499 elseif m =~ entered_value
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000500 call add(res2, attrquoteopen . m . attrquote)
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000501 endif
502 endfor
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000503
504 return res + res2
505
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000506 endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000507 " }}}
508 " Attribute completion {{{
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000509 " Shorten context to not include last word
510 let sbase = matchstr(context, '.*\ze\s.*')
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000511
512 " Load data {{{
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000513 if !exists("b:html_doctype")
514 call htmlcomplete#CheckDoctype()
515 endif
516 if !exists("b:html_omni")
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000517 call htmlcomplete#LoadData()
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000518 endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000519 " }}}
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000520
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000521 if has_key(b:html_omni, tag)
522 let attrs = keys(b:html_omni[tag][1])
Bram Moolenaar8424a622006-04-19 21:23:36 +0000523 else
524 return []
525 endif
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000526
527 for m in sort(attrs)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000528 if m =~ '^'.attr
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000529 call add(res, m)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000530 elseif m =~ attr
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000531 call add(res2, m)
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000532 endif
533 endfor
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000534 let menu = res + res2
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000535 if has_key(b:html_omni, 'vimxmlattrinfo')
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000536 let final_menu = []
537 for i in range(len(menu))
538 let item = menu[i]
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000539 if has_key(b:html_omni['vimxmlattrinfo'], item)
540 let m_menu = b:html_omni['vimxmlattrinfo'][item][0]
541 let m_info = b:html_omni['vimxmlattrinfo'][item][1]
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000542 else
543 let m_menu = ''
544 let m_info = ''
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000545 endif
546 if len(b:html_omni[tag][1][item]) > 0 && b:html_omni[tag][1][item][0] =~ '^\(BOOL\|'.item.'\)$'
547 let item = item
548 let m_menu = 'Bool'
549 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000550 let item .= '="'
551 endif
552 let final_menu += [{'word':item, 'menu':m_menu, 'info':m_info}]
553 endfor
554 else
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000555 let final_menu = []
556 for i in range(len(menu))
557 let item = menu[i]
558 if len(b:html_omni[tag][1][item]) > 0 && b:html_omni[tag][1][item][0] =~ '^\(BOOL\|'.item.'\)$'
559 let item = item
560 else
561 let item .= '="'
562 endif
563 let final_menu += [item]
564 endfor
565 return final_menu
566
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000567 endif
568 return final_menu
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000569
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000570 endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000571 " }}}
572 " Close tag {{{
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000573 let b:unaryTagsStack = "base meta link hr br param img area input col"
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000574 if context =~ '^\/'
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000575 if context =~ '^\/.'
576 return []
577 else
578 let opentag = xmlcomplete#GetLastOpenTag("b:unaryTagsStack")
579 return [opentag.">"]
580 endif
Bram Moolenaar4c903f92005-09-14 21:32:32 +0000581 endif
Bram Moolenaar8424a622006-04-19 21:23:36 +0000582 " }}}
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000583 " Load data {{{
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000584 if !exists("b:html_doctype")
585 call htmlcomplete#CheckDoctype()
586 endif
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000587 if !exists("b:html_omni")
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000588 "runtime! autoload/xml/xhtml10s.vim
589 call htmlcomplete#LoadData()
Bram Moolenaar4c903f92005-09-14 21:32:32 +0000590 endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000591 " }}}
592 " Tag completion {{{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000593 " Deal with tag completion.
Bram Moolenaar8424a622006-04-19 21:23:36 +0000594 let opentag = tolower(xmlcomplete#GetLastOpenTag("b:unaryTagsStack"))
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000595 " MM: TODO: GLOT works always the same but with some weird situation it
596 " behaves as intended in HTML but screws in PHP
Bram Moolenaar4a85b412006-04-23 22:40:29 +0000597 if opentag == '' || &filetype == 'php' && !has_key(b:html_omni, opentag)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000598 " Hack for sometimes failing GetLastOpenTag.
599 " As far as I tested fail isn't GLOT fault but problem
600 " of invalid document - not properly closed tags and other mish-mash.
601 " Also when document is empty. Return list of *all* tags.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000602 let tags = keys(b:html_omni)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000603 call filter(tags, 'v:val !~ "^vimxml"')
604 else
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000605 if has_key(b:html_omni, opentag)
606 let tags = b:html_omni[opentag][0]
Bram Moolenaar8424a622006-04-19 21:23:36 +0000607 else
608 return []
609 endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000610 endif
611 " }}}
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000612
Bram Moolenaar8424a622006-04-19 21:23:36 +0000613 if exists("uppercase_tag") && uppercase_tag == 1
614 let context = tolower(context)
615 endif
Bram Moolenaard5ab34b2007-05-05 17:15:44 +0000616 " Handle XML keywords: DOCTYPE
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000617 if opentag == ''
Bram Moolenaar4a85b412006-04-23 22:40:29 +0000618 let tags += [
Bram Moolenaard5ab34b2007-05-05 17:15:44 +0000619 \ '!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">',
620 \ '!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">',
621 \ '!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">',
622 \ '!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN" "http://www.w3.org/TR/REC-html40/frameset.dtd">',
623 \ '!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
624 \ '!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
625 \ '!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
626 \ '!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
627 \ '!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
628 \ '!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
629 \ '!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/1999/xhtml">'
Bram Moolenaar4a85b412006-04-23 22:40:29 +0000630 \ ]
631 endif
Bram Moolenaar4c903f92005-09-14 21:32:32 +0000632
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000633 for m in sort(tags)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000634 if m =~ '^'.context
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000635 call add(res, m)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000636 elseif m =~ context
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000637 call add(res2, m)
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000638 endif
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000639 endfor
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000640 let menu = res + res2
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000641 if has_key(b:html_omni, 'vimxmltaginfo')
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000642 let final_menu = []
643 for i in range(len(menu))
644 let item = menu[i]
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000645 if has_key(b:html_omni['vimxmltaginfo'], item)
646 let m_menu = b:html_omni['vimxmltaginfo'][item][0]
647 let m_info = b:html_omni['vimxmltaginfo'][item][1]
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000648 else
649 let m_menu = ''
650 let m_info = ''
651 endif
Bram Moolenaar4a85b412006-04-23 22:40:29 +0000652 if &filetype == 'html' && exists("uppercase_tag") && uppercase_tag == 1 && item !~ 'DOCTYPE'
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000653 let item = toupper(item)
654 endif
Bram Moolenaar4a85b412006-04-23 22:40:29 +0000655 if item =~ 'DOCTYPE'
656 let abbr = 'DOCTYPE '.matchstr(item, 'DTD \zsX\?HTML .\{-}\ze\/\/')
657 else
658 let abbr = item
659 endif
660 let final_menu += [{'abbr':abbr, 'word':item, 'menu':m_menu, 'info':m_info}]
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000661 endfor
662 else
663 let final_menu = menu
664 endif
665 return final_menu
Bram Moolenaar6b730e12005-09-16 21:47:57 +0000666
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000667 " }}}
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000668 endif
669endfunction
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000670
671function! htmlcomplete#LoadData() " {{{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000672 if !exists("b:html_omni_flavor")
Bram Moolenaar4a85b412006-04-23 22:40:29 +0000673 if &filetype == 'html'
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000674 let b:html_omni_flavor = 'html401t'
675 else
676 let b:html_omni_flavor = 'xhtml10s'
677 endif
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000678 endif
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000679 " With that if we still have bloated memory but create new buffer
680 " variables only by linking to existing g:variable, not sourcing whole
681 " file.
682 if exists('g:xmldata_'.b:html_omni_flavor)
683 exe 'let b:html_omni = g:xmldata_'.b:html_omni_flavor
684 else
685 exe 'runtime! autoload/xml/'.b:html_omni_flavor.'.vim'
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000686 exe 'let b:html_omni = g:xmldata_'.b:html_omni_flavor
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000687 endif
688endfunction
689" }}}
690function! htmlcomplete#CheckDoctype() " {{{
691 if exists('b:html_omni_flavor')
692 let old_flavor = b:html_omni_flavor
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000693 else
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000694 let old_flavor = ''
695 endif
696 let i = 1
697 while i < 10 && i < line("$")
698 let line = getline(i)
699 if line =~ '<!DOCTYPE.*\<DTD HTML 3\.2'
700 let b:html_omni_flavor = 'html32'
701 let b:html_doctype = 1
702 break
703 elseif line =~ '<!DOCTYPE.*\<DTD HTML 4\.0 Transitional'
704 let b:html_omni_flavor = 'html40t'
705 let b:html_doctype = 1
706 break
707 elseif line =~ '<!DOCTYPE.*\<DTD HTML 4\.0 Frameset'
708 let b:html_omni_flavor = 'html40f'
709 let b:html_doctype = 1
710 break
711 elseif line =~ '<!DOCTYPE.*\<DTD HTML 4\.0'
712 let b:html_omni_flavor = 'html40s'
713 let b:html_doctype = 1
714 break
715 elseif line =~ '<!DOCTYPE.*\<DTD HTML 4\.01 Transitional'
716 let b:html_omni_flavor = 'html401t'
717 let b:html_doctype = 1
718 break
719 elseif line =~ '<!DOCTYPE.*\<DTD HTML 4\.01 Frameset'
720 let b:html_omni_flavor = 'html401f'
721 let b:html_doctype = 1
722 break
723 elseif line =~ '<!DOCTYPE.*\<DTD HTML 4\.01'
724 let b:html_omni_flavor = 'html401s'
725 let b:html_doctype = 1
726 break
727 elseif line =~ '<!DOCTYPE.*\<DTD XHTML 1\.0 Transitional'
728 let b:html_omni_flavor = 'xhtml10t'
729 let b:html_doctype = 1
730 break
731 elseif line =~ '<!DOCTYPE.*\<DTD XHTML 1\.0 Frameset'
732 let b:html_omni_flavor = 'xhtml10f'
733 let b:html_doctype = 1
734 break
735 elseif line =~ '<!DOCTYPE.*\<DTD XHTML 1\.0 Strict'
736 let b:html_omni_flavor = 'xhtml10s'
737 let b:html_doctype = 1
738 break
739 elseif line =~ '<!DOCTYPE.*\<DTD XHTML 1\.1'
740 let b:html_omni_flavor = 'xhtml11'
741 let b:html_doctype = 1
742 break
743 endif
744 let i += 1
745 endwhile
746 if !exists("b:html_doctype")
747 return
748 else
749 " Tie g:xmldata with b:html_omni this way we need to sourca data file only
750 " once, not every time per buffer.
751 if old_flavor == b:html_omni_flavor
752 return
753 else
754 if exists('g:xmldata_'.b:html_omni_flavor)
755 exe 'let b:html_omni = g:xmldata_'.b:html_omni_flavor
756 else
757 exe 'runtime! autoload/xml/'.b:html_omni_flavor.'.vim'
758 exe 'let b:html_omni = g:xmldata_'.b:html_omni_flavor
759 endif
760 return
761 endif
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000762 endif
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000763endfunction
764" }}}
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000765" vim:set foldmethod=marker: