Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 1 | " Vim completion script |
| 2 | " Language: XHTML 1.0 Strict |
| 3 | " Maintainer: Mikolaj Machowski ( mikmach AT wp DOT pl ) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 4 | " Last Change: 2005 Sep 19 |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 5 | |
| 6 | function! 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 Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 14 | 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 Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 25 | return start |
| 26 | else |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 27 | " 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 Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 42 | " Set attribute groups |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 43 | 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 Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 46 | \ "onmouseout", "onkeypress", "onkeydown", "onkeyup"] |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 47 | let focus = ["accesskey", "tabindex", "onfocus", "onblur"] |
| 48 | let coregroup = coreattrs + i18n + events |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 49 | let res = [] |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 50 | let res2 = [] |
| 51 | " find tags matching with "a:base" |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 52 | " If a:base contains > it means we are already outside of tag and we |
| 53 | " should abandon action |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 54 | " If a:base contains white space it is attribute. |
| 55 | " It could be also value of attribute... |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 56 | " 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 Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 61 | " 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. |
| 69 | if a:base =~ "\\(on[a-z]*\\|style\\|class\\)\\s*=\\s*[\"']" |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 70 | if a:base =~ "class\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$" |
| 71 | " Handle class name completion |
| 72 | " 1. Find lines of <link stylesheet> |
| 73 | " 1a. Check file for @import |
| 74 | " 2. Extract filename(s?) of stylesheet, |
| 75 | call cursor(1,1) |
| 76 | let head = getline(search('<head\>'), search('<\/head>')) |
| 77 | let headjoined = join(copy(head), ' ') |
| 78 | if headjoined =~ '<style' |
| 79 | let stylehead = substitute(headjoined, '+>\*[', ' ', 'g') |
| 80 | let styleheadlines = split(stylehead) |
| 81 | let headclasslines = filter(copy(styleheadlines), "v:val =~ '\\([a-zA-Z0-9:]\\+\\)\\?\\.[a-zA-Z0-9_-]\\+'") |
| 82 | let internal = 1 |
| 83 | else |
| 84 | let internal = 0 |
| 85 | endif |
| 86 | let styletable = [] |
| 87 | let secimportfiles = [] |
| 88 | let filestable = filter(copy(head), "v:val =~ '\\(@import\\|link.*stylesheet\\)'") |
| 89 | for line in filestable |
| 90 | if line =~ "@import" |
| 91 | let styletable += [matchstr(line, "import\\s\\+\\(url(\\)\\?[\"']\\?\\zs\\f\\+\\ze")] |
| 92 | elseif line =~ "<link" |
| 93 | let styletable += [matchstr(line, "href\\s*=\\s*[\"']\\zs\\f\\+\\ze")] |
| 94 | endif |
| 95 | endfor |
| 96 | for file in filestable |
| 97 | if filereadable(file) |
| 98 | let stylesheet = readfile(file) |
| 99 | let secimport = filter(copy(stylesheet), "v:val =~ '@import'") |
| 100 | if len(secimport) > 0 |
| 101 | for line in secimport |
| 102 | let secimportfiles += [matchstr(line, "import\\s\\+\\(url(\\)\\?[\"']\\?\\zs\\f\\+\\ze")] |
| 103 | endfor |
| 104 | endif |
| 105 | endif |
| 106 | endfor |
| 107 | let cssfiles = styletable + secimportfiles |
| 108 | let classes = [] |
| 109 | for file in cssfiles |
| 110 | if filereadable(file) |
| 111 | let stylesheet = readfile(file) |
| 112 | let stylefile = join(stylesheet) |
| 113 | let stylefile = substitute(stylefile, '+>\*[', ' ', 'g') |
| 114 | let stylesheet = split(stylefile) |
| 115 | let classlines = filter(copy(stylesheet), "v:val =~ '\\([a-zA-Z0-9:]\\+\\)\\?\\.[a-zA-Z0-9_-]\\+'") |
| 116 | endif |
| 117 | " We gathered classes definitions from all external files |
| 118 | let classes += classlines |
| 119 | endfor |
| 120 | if internal == 1 |
| 121 | let classes += headclasslines |
| 122 | endif |
| 123 | let elements = {} |
| 124 | for element in classes |
| 125 | if element =~ '^\.' |
| 126 | let class = matchstr(element, '^\.\zs[a-zA-Z][a-zA-Z0-9_-]*\ze') |
| 127 | let class = substitute(class, ':.*', '', '') |
| 128 | if has_key(elements, "common") |
| 129 | let elements["common"] .= " ".class |
| 130 | else |
| 131 | let elements["common"] = class |
| 132 | endif |
| 133 | else |
| 134 | let class = matchstr(element, '[a-zA-Z1-6]*\.\zs[a-zA-Z][a-zA-Z0-9_-]*\ze') |
| 135 | let tagname = tolower(matchstr(element, '[a-zA-Z1-6]*\ze.')) |
| 136 | if tagname != '' |
| 137 | if has_key(elements, tagname) |
| 138 | let elements[tagname] .= " ".class |
| 139 | else |
| 140 | let elements[tagname] = class |
| 141 | endif |
| 142 | endif |
| 143 | endif |
| 144 | endfor |
| 145 | |
| 146 | if has_key(elements, tag) && has_key(elements, "common") |
| 147 | let values = split(elements[tag]." ".elements["common"]) |
| 148 | elseif has_key(elements, tag) && !has_key(elements, "common") |
| 149 | let values = split(elements[tag]) |
| 150 | elseif !has_key(elements, tag) && has_key(elements, "common") |
| 151 | let values = split(elements["common"]) |
| 152 | else |
| 153 | return [] |
| 154 | endif |
| 155 | |
| 156 | " We need special version of sbase |
| 157 | let classbase = matchstr(a:base, ".*[\"']") |
| 158 | let classquote = matchstr(classbase, '.$') |
| 159 | |
| 160 | |
| 161 | let entered_class = matchstr(attr, ".*=\\s*[\"']\\zs.*") |
| 162 | |
| 163 | for m in sort(values) |
| 164 | if m =~? '^'.entered_class |
| 165 | call add(res, classbase . m . classquote . ' ') |
| 166 | elseif m =~? entered_class |
| 167 | call add(res2, classbase . m . classquote . ' ') |
| 168 | endif |
| 169 | endfor |
| 170 | |
| 171 | return res + res2 |
| 172 | |
| 173 | elseif a:base =~ "style\\s*=\\s*[\"'][^\"']*$" |
| 174 | return csscomplete#CompleteCSS(0, a:base) |
| 175 | |
| 176 | endif |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 177 | let stripbase = matchstr(a:base, ".*\\(on[a-z]*\\|style\\|class\\)\\s*=\\s*[\"']\\zs.*") |
| 178 | " Now we have a:base stripped from all chars up to style/class. |
| 179 | " It may fail with some strange style value combinations. |
| 180 | if stripbase !~ "[\"']" |
| 181 | return [] |
| 182 | endif |
| 183 | endif |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 184 | " If attr contains =\s*[\"'] we catched value of attribute |
| 185 | if attr =~ "=\s*[\"']" |
| 186 | " Let do attribute specific completion |
| 187 | let attrname = matchstr(attr, '.*\ze\s*=') |
| 188 | let entered_value = matchstr(attr, ".*=\\s*[\"']\\zs.*") |
| 189 | let values = [] |
| 190 | if attrname == 'media' |
| 191 | let values = ["screen", "tty", "tv", "projection", "handheld", "print", "braille", "aural", "all"] |
| 192 | elseif attrname == 'xml:space' |
| 193 | let values = ["preserve"] |
| 194 | elseif attrname == 'shape' |
| 195 | if a:base =~ '^a\>' |
| 196 | let values = ["rect"] |
| 197 | else |
| 198 | let values = ["rect", "circle", "poly", "default"] |
| 199 | endif |
| 200 | elseif attrname == 'valuetype' |
| 201 | let values = ["data", "ref", "object"] |
| 202 | elseif attrname == 'method' |
| 203 | let values = ["get", "post"] |
Bram Moolenaar | 4c903f9 | 2005-09-14 21:32:32 +0000 | [diff] [blame] | 204 | elseif attrname == 'dir' |
| 205 | let values = ["ltr", "rtl"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 206 | elseif attrname == 'frame' |
| 207 | let values = ["void", "above", "below", "hsides", "lhs", "rhs", "vsides", "box", "border"] |
| 208 | elseif attrname == 'rules' |
| 209 | let values = ["none", "groups", "rows", "all"] |
| 210 | elseif attrname == 'align' |
| 211 | let values = ["left", "center", "right", "justify", "char"] |
| 212 | elseif attrname == 'valign' |
| 213 | let values = ["top", "middle", "bottom", "baseline"] |
| 214 | elseif attrname == 'scope' |
| 215 | let values = ["row", "col", "rowgroup", "colgroup"] |
| 216 | elseif attrname == 'href' |
| 217 | " Now we are looking for local anchors defined by name or id |
| 218 | if entered_value =~ '^#' |
| 219 | let file = join(getline(1, line('$')), ' ') |
| 220 | " Split it be sure there will be one id/name element in |
| 221 | " item, it will be also first word [a-zA-Z0-9_-] in element |
| 222 | let oneelement = split(file, "\\(meta \\)\\@<!\\(name\\|id\\)\\s*=\\s*[\"']") |
| 223 | for i in oneelement |
| 224 | let values += ['#'.matchstr(i, "^[a-zA-Z][a-zA-Z0-9%_-]*")] |
| 225 | endfor |
| 226 | endif |
| 227 | elseif attrname == 'type' |
| 228 | if a:base =~ '^input' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 229 | let values = ["text", "password", "checkbox", "radio", "submit", "reset", "file", "hidden", "image", "button"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 230 | elseif a:base =~ '^button' |
| 231 | let values = ["button", "submit", "reset"] |
| 232 | endif |
| 233 | else |
| 234 | return [] |
| 235 | endif |
| 236 | |
| 237 | if len(values) == 0 |
| 238 | return [] |
| 239 | endif |
| 240 | |
| 241 | " We need special version of sbase |
| 242 | let attrbase = matchstr(a:base, ".*[\"']") |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 243 | let attrquote = matchstr(attrbase, '.$') |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 244 | |
| 245 | for m in values |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 246 | " This if is needed to not offer all completions as-is |
| 247 | " alphabetically but sort them. Those beginning with entered |
| 248 | " part will be as first choices |
| 249 | if m =~ '^'.entered_value |
| 250 | call add(res, attrbase . m . attrquote.' ') |
| 251 | elseif m =~ entered_value |
| 252 | call add(res2, attrbase . m . attrquote.' ') |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 253 | endif |
| 254 | endfor |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 255 | |
| 256 | return res + res2 |
| 257 | |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 258 | endif |
| 259 | " Shorten a:base to not include last word |
| 260 | let sbase = matchstr(a:base, '.*\ze\s.*') |
| 261 | 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 Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 262 | let attrs = coregroup |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 263 | elseif tag == 'a' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 264 | let attrs = coregroup + focus + ["charset", "type", "name", "href", "hreflang", "rel", "rev", "shape", "coords"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 265 | elseif tag == 'area' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 266 | let attrs = coregroup |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 267 | elseif tag == 'base' |
| 268 | let attrs = ["href", "id"] |
| 269 | elseif tag == 'blockquote' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 270 | let attrs = coregroup + ["cite"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 271 | elseif tag == 'body' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 272 | let attrs = coregroup + ["onload", "onunload"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 273 | elseif tag == 'br' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 274 | let attrs = coreattrs |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 275 | elseif tag == 'button' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 276 | let attrs = coreattrs + focus + ["name", "value", "type"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 277 | elseif tag == '^\(col\|colgroup\)$' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 278 | let attrs = coreattrs + ["span", "width", "align", "char", "charoff", "valign"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 279 | elseif tag =~ '^\(del\|ins\)$' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 280 | let attrs = coreattrs + ["cite", "datetime"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 281 | elseif tag == 'form' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 282 | let attrs = coreattrs + ["action", "method=\"get\" ", "method=\"post\" ", "enctype", "onsubmit", "onreset", "accept", "accept-charset"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 283 | elseif tag == 'head' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 284 | let attrs = i18n + ["id", "profile"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 285 | elseif tag == 'html' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 286 | let attrs = i18n + ["id", "xmlns"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 287 | elseif tag == 'img' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 288 | let attrs = coreattrs + ["src", "alt", "longdesc", "height", "width", "usemap", "ismap"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 289 | elseif tag == 'input' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 290 | let attrs = coreattrs + focus + ["type", "name", "value", "checked", "disabled", "readonly", "size", "maxlength", "src", "alt", "usemap", "onselect", "onchange", "accept"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 291 | elseif tag == 'label' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 292 | let attrs = coreattrs + ["for", "accesskey", "onfocus", "onblur"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 293 | elseif tag == 'legend' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 294 | let attrs = coreattrs + ["accesskey"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 295 | elseif tag == 'link' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 296 | let attrs = coreattrs + ["charset", "href", "hreflang", "type", "rel", "rev", "media"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 297 | elseif tag == 'map' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 298 | let attrs = i18n + events + ["id", "class", "style", "title", "name"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 299 | elseif tag == 'meta' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 300 | let attrs = i18n + ["id", "http-equiv", "content", "scheme", "name"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 301 | elseif tag == 'title' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 302 | let attrs = i18n + ["id"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 303 | elseif tag == 'object' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 304 | let attrs = coreattrs + ["declare", "classid", "codebase", "data", "type", "codetype", "archive", "standby", "height", "width", "usemap", "name", "tabindex"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 305 | elseif tag == 'optgroup' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 306 | let attrs = coreattrs + ["disbled", "label"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 307 | elseif tag == 'option' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 308 | let attrs = coreattrs + ["disbled", "selected", "value", "label"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 309 | elseif tag == 'param' |
| 310 | let attrs = ["id", "name", "value", "valuetype", "type"] |
| 311 | elseif tag == 'pre' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 312 | let attrs = coreattrs + ["xml:space"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 313 | elseif tag == 'q' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 314 | let attrs = coreattrs + ["cite"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 315 | elseif tag == 'script' |
| 316 | let attrs = ["id", "charset", "type", "src", "defer", "xml:space"] |
| 317 | elseif tag == 'select' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 318 | let attrs = coreattrs + ["name", "size", "multiple", "disabled", "tabindex", "onfocus", "onblur", "onchange"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 319 | elseif tag == 'style' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 320 | let attrs = coreattrs + ["id", "type", "media", "title", "xml:space"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 321 | elseif tag == 'table' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 322 | let attrs = coreattrs + ["summary", "width", "border", "frame", "rules", "cellspacing", "cellpadding"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 323 | elseif tag =~ '^\(thead\|tfoot\|tbody\|tr\)$' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 324 | let attrs = coreattrs + ["align", "char", "charoff", "valign"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 325 | elseif tag == 'textarea' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 326 | let attrs = coreattrs + focus + ["name", "rows", "cols", "disabled", "readonly", "onselect", "onchange"] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 327 | elseif tag =~ '^\(th\|td\)$' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 328 | let attrs = coreattrs + ["abbr", "headers", "scope", "rowspan", "colspan", "align", "char", "charoff", "valign"] |
| 329 | else |
| 330 | return [] |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 331 | endif |
| 332 | |
| 333 | for m in sort(attrs) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 334 | if m =~ '^'.attr |
| 335 | if m =~ '^\(ismap\|defer\|declare\|nohref\|checked\|disabled\|selected\|readonly\)$' || m =~ '=' |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 336 | call add(res, sbase.' '.m) |
| 337 | else |
| 338 | call add(res, sbase.' '.m.'="') |
| 339 | endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 340 | elseif m =~ attr |
| 341 | if m =~ '^\(ismap\|defer\|declare\|nohref\|checked\|disabled\|selected\|readonly\)$' || m =~ '=' |
| 342 | call add(res2, sbase.' '.m) |
| 343 | else |
| 344 | call add(res2, sbase.' '.m.'="') |
| 345 | endif |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 346 | endif |
| 347 | endfor |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 348 | |
| 349 | return res + res2 |
| 350 | |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 351 | endif |
Bram Moolenaar | 4c903f9 | 2005-09-14 21:32:32 +0000 | [diff] [blame] | 352 | " Close tag |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 353 | let b:unaryTagsStack = "base meta link hr br param img area input col" |
| 354 | if a:base =~ '^\/' |
| 355 | let opentag = htmlcomplete#GetLastOpenTag("b:unaryTagsStack") |
Bram Moolenaar | 4c903f9 | 2005-09-14 21:32:32 +0000 | [diff] [blame] | 356 | return ["/".opentag.">"] |
| 357 | endif |
| 358 | " Deal with tag completion. |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 359 | let opentag = htmlcomplete#GetLastOpenTag("b:unaryTagsStack") |
| 360 | " Clusters |
| 361 | let special = "br span bdo map object img" |
| 362 | let phrase = "em strong dfn code q samp kbd var cite abbr acronym sub sup" |
| 363 | let inlineforms = "input select textarea label button" |
| 364 | let miscinline = "ins del script" |
| 365 | let inline = "a ".special." ".phrase." ".inlineforms." tt i b big small" |
| 366 | let misc = "noscript ".miscinline |
| 367 | let block = "p h1 h2 h3 h4 h5 h6 div ul ol dl pre hr blockquote address fieldset table" |
Bram Moolenaar | 4c903f9 | 2005-09-14 21:32:32 +0000 | [diff] [blame] | 368 | |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 369 | if opentag == 'a' |
| 370 | let tags = split("tt i b big small ".special." ".phrase." ".inlineforms." ".miscinline) |
| 371 | 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\)$' |
| 372 | let tags = split(inline." ".miscinline) |
| 373 | elseif opentag == 'pre' |
| 374 | let tags = split("a tt i b big small br span bdo map ".phrase." ".miscinline." ".inlineforms) |
| 375 | elseif opentag == 'html' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 376 | let tags = ["head", "body"] |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 377 | elseif opentag == 'legend' |
| 378 | let tags = split(inline." ".miscinline) |
| 379 | elseif opentag == 'head' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 380 | let tags = ["title", "base", "scipt", "style", "meta", "link", "object"] |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 381 | elseif opentag =~ '^\(noscript\|body\|blockquote\)$' |
| 382 | let tags = split("form ".block." ".misc) |
| 383 | elseif opentag =~ '^\(ul\|ol\)$' |
| 384 | let tags = ["li"] |
| 385 | elseif opentag == 'dl' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 386 | let tags = ["dt", "dd"] |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 387 | elseif opentag =~ '^\(ins\|del\|th\|td\|dd\|div\|li\)$' |
| 388 | let tags = split("form ".block." ".inline." ".misc) |
| 389 | elseif opentag == 'object' |
| 390 | let tags = split("param form ".block." ".inline." ".misc) |
| 391 | elseif opentag == 'fieldset' |
| 392 | let tags = split("legend form ".block." ".inline." ".misc) |
| 393 | elseif opentag == 'map' |
| 394 | let tags = split("area form ".block." ".misc) |
| 395 | elseif opentag == 'form' |
| 396 | let tags = split(block." ".misc) |
| 397 | elseif opentag == 'select' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 398 | let tags = ["optgroup", "option"] |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 399 | elseif opentag == 'optgroup' |
| 400 | let tags = ["option"] |
| 401 | elseif opentag == 'colgroup' |
| 402 | let tags = ["col"] |
| 403 | elseif opentag == '^\(textarea\|option\|script\|style\|title\)$' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 404 | let tags = ['empty'] |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 405 | elseif opentag == 'button' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 406 | let tags = ["p", "h1", "h2", "h3", "h4", "h5", "h6", "div", "ul", "ol", "dl", "table"] |
| 407 | elseif opentag =~ '^\(thead\|tfoot\|tbody\)$' |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 408 | let tags = ["tr"] |
| 409 | elseif opentag == 'tr' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 410 | let tags = ["th", "td"] |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 411 | elseif opentag == 'table' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 412 | let tags = ["caption", "col", "colgroup", "thead", "tfoot", "tbody", "tr"] |
| 413 | else |
| 414 | return [] |
Bram Moolenaar | 4c903f9 | 2005-09-14 21:32:32 +0000 | [diff] [blame] | 415 | endif |
| 416 | |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 417 | for m in tags |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 418 | if m =~ '^'.a:base |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 419 | call add(res, m) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 420 | elseif m =~ a:base |
| 421 | call add(res2, m) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 422 | endif |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 423 | endfor |
| 424 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 425 | return res + res2 |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 426 | |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 427 | endif |
| 428 | endfunction |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 429 | |
| 430 | " MM: This is greatly reduced closetag.vim used with kind permission of Steven |
| 431 | " Mueller |
| 432 | " Changes: strip all comments; delete error messages |
| 433 | " Author: Steven Mueller <diffusor@ugcs.caltech.edu> |
| 434 | " Last Modified: Tue May 24 13:29:48 PDT 2005 |
| 435 | " Version: 0.9.1 |
| 436 | |
| 437 | function! htmlcomplete#GetLastOpenTag(unaryTagsStack) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 438 | let linenum=line('.') |
| 439 | let lineend=col('.') - 1 " start: cursor position |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 440 | let first=1 " flag for first line searched |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 441 | let b:TagStack='' " main stack of tags |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 442 | let startInComment=s:InComment() |
| 443 | |
| 444 | let tagpat='</\=\(\k\|[-:]\)\+\|/>' |
| 445 | while (linenum>0) |
| 446 | let line=getline(linenum) |
| 447 | if first |
| 448 | let line=strpart(line,0,lineend) |
| 449 | else |
| 450 | let lineend=strlen(line) |
| 451 | endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 452 | let b:lineTagStack='' |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 453 | let mpos=0 |
| 454 | let b:TagCol=0 |
| 455 | while (mpos > -1) |
| 456 | let mpos=matchend(line,tagpat) |
| 457 | if mpos > -1 |
| 458 | let b:TagCol=b:TagCol+mpos |
| 459 | let tag=matchstr(line,tagpat) |
| 460 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 461 | if exists('b:closetag_disable_synID') || startInComment==s:InCommentAt(linenum, b:TagCol) |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 462 | let b:TagLine=linenum |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 463 | call s:Push(matchstr(tag,'[^<>]\+'),'b:lineTagStack') |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 464 | endif |
| 465 | let lineend=lineend-mpos |
| 466 | let line=strpart(line,mpos,lineend) |
| 467 | endif |
| 468 | endwhile |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 469 | while (!s:EmptystackP('b:lineTagStack')) |
| 470 | let tag=s:Pop('b:lineTagStack') |
| 471 | if match(tag, '^/') == 0 "found end tag |
| 472 | call s:Push(tag,'b:TagStack') |
| 473 | elseif s:EmptystackP('b:TagStack') && !s:Instack(tag, a:unaryTagsStack) "found unclosed tag |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 474 | return tag |
| 475 | else |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 476 | let endtag=s:Peekstack('b:TagStack') |
| 477 | if endtag == '/'.tag || endtag == '/' |
| 478 | call s:Pop('b:TagStack') "found a open/close tag pair |
| 479 | elseif !s:Instack(tag, a:unaryTagsStack) "we have a mismatch error |
| 480 | return '' |
| 481 | endif |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 482 | endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 483 | endwhile |
| 484 | let linenum=linenum-1 | let first=0 |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 485 | endwhile |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 486 | return '' |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 487 | endfunction |
| 488 | |
| 489 | function! s:InComment() |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 490 | return synIDattr(synID(line('.'), col('.'), 0), 'name') =~ 'Comment' |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 491 | endfunction |
| 492 | |
| 493 | function! s:InCommentAt(line, col) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 494 | return synIDattr(synID(a:line, a:col, 0), 'name') =~ 'Comment' |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 495 | endfunction |
| 496 | |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 497 | function! s:SetKeywords() |
| 498 | let g:IsKeywordBak=&iskeyword |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 499 | let &iskeyword='33-255' |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 500 | endfunction |
| 501 | |
| 502 | function! s:RestoreKeywords() |
| 503 | let &iskeyword=g:IsKeywordBak |
| 504 | endfunction |
| 505 | |
| 506 | function! s:Push(el, sname) |
| 507 | if !s:EmptystackP(a:sname) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 508 | exe 'let '.a:sname."=a:el.' '.".a:sname |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 509 | else |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 510 | exe 'let '.a:sname.'=a:el' |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 511 | endif |
| 512 | endfunction |
| 513 | |
| 514 | function! s:EmptystackP(sname) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 515 | exe 'let stack='.a:sname |
| 516 | if match(stack,'^ *$') == 0 |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 517 | return 1 |
| 518 | else |
| 519 | return 0 |
| 520 | endif |
| 521 | endfunction |
| 522 | |
| 523 | function! s:Instack(el, sname) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 524 | exe 'let stack='.a:sname |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 525 | call s:SetKeywords() |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 526 | let m=match(stack, '\<'.a:el.'\>') |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 527 | call s:RestoreKeywords() |
| 528 | if m < 0 |
| 529 | return 0 |
| 530 | else |
| 531 | return 1 |
| 532 | endif |
| 533 | endfunction |
| 534 | |
| 535 | function! s:Peekstack(sname) |
| 536 | call s:SetKeywords() |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 537 | exe 'let stack='.a:sname |
| 538 | let top=matchstr(stack, '\<.\{-1,}\>') |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 539 | call s:RestoreKeywords() |
| 540 | return top |
| 541 | endfunction |
| 542 | |
| 543 | function! s:Pop(sname) |
| 544 | if s:EmptystackP(a:sname) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 545 | return '' |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 546 | endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 547 | exe 'let stack='.a:sname |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 548 | call s:SetKeywords() |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 549 | let loc=matchend(stack,'\<.\{-1,}\>') |
| 550 | exe 'let '.a:sname.'=strpart(stack, loc+1, strlen(stack))' |
| 551 | let top=strpart(stack, match(stack, '\<'), loc) |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 552 | call s:RestoreKeywords() |
| 553 | return top |
| 554 | endfunction |
| 555 | |
| 556 | function! s:Clearstack(sname) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame^] | 557 | exe 'let '.a:sname."=''" |
Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 558 | endfunction |