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 ) |
| 4 | " Last Change: 2005 Sep 13 |
| 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 |
| 14 | let g:st = start |
| 15 | return start |
| 16 | else |
| 17 | " Set attribute groups |
| 18 | let g:coreattrs = ["id", "class", "style", "title"] |
| 19 | let g:i18n = ["lang", "xml:lang", "dir"] |
| 20 | let g:events = ["onclick", "ondblclick", "onmousedown", "onmouseup", "onmousemove", |
| 21 | \ "onmouseout", "onkeypress", "onkeydown", "onkeyup"] |
| 22 | let g:focus = ["accesskey", "tabindex", "onfocus", "onblur"] |
| 23 | let g:coregroup = g:coreattrs |
| 24 | let g:coregroup = extend(g:coregroup, g:i18n) |
| 25 | let g:coregroup = extend(g:coregroup, g:events) |
| 26 | " find tags matching with "a:base" |
| 27 | let res = [] |
| 28 | " If a:base contains > it means we are already outside of tag and we |
| 29 | " should abandon action |
| 30 | if a:base =~ '>' |
| 31 | return [] |
| 32 | endif |
| 33 | " If a:base contains white space it is attribute. |
| 34 | " It could be also value of attribute... |
| 35 | " Possible situations where any prediction would be difficult: |
| 36 | " 1. Events attributes |
| 37 | if a:base =~ '\s' |
| 38 | " Sort out style, class, and on* cases |
| 39 | " Perfect solution for style would be switching for CSS completion. Is |
| 40 | " it possible? |
| 41 | " Also retrieving class names from current file and linked |
| 42 | " stylesheets. |
| 43 | if a:base =~ "\\(on[a-z]*\\|style\\|class\\)\\s*=\\s*[\"']" |
| 44 | let stripbase = matchstr(a:base, ".*\\(on[a-z]*\\|style\\|class\\)\\s*=\\s*[\"']\\zs.*") |
| 45 | " Now we have a:base stripped from all chars up to style/class. |
| 46 | " It may fail with some strange style value combinations. |
| 47 | if stripbase !~ "[\"']" |
| 48 | return [] |
| 49 | endif |
| 50 | endif |
| 51 | " We have to get first word to offer |
| 52 | " proper attributes. |
| 53 | let tag = split(a:base)[0] |
| 54 | " Get last word, it should be attr name |
| 55 | let attr = matchstr(a:base, '.*\s\zs.*') |
| 56 | " If attr contains =\s*[\"'] we catched value of attribute |
| 57 | if attr =~ "=\s*[\"']" |
| 58 | " Let do attribute specific completion |
| 59 | let attrname = matchstr(attr, '.*\ze\s*=') |
| 60 | let entered_value = matchstr(attr, ".*=\\s*[\"']\\zs.*") |
| 61 | let values = [] |
| 62 | if attrname == 'media' |
| 63 | let values = ["screen", "tty", "tv", "projection", "handheld", "print", "braille", "aural", "all"] |
| 64 | elseif attrname == 'xml:space' |
| 65 | let values = ["preserve"] |
| 66 | elseif attrname == 'shape' |
| 67 | if a:base =~ '^a\>' |
| 68 | let values = ["rect"] |
| 69 | else |
| 70 | let values = ["rect", "circle", "poly", "default"] |
| 71 | endif |
| 72 | elseif attrname == 'valuetype' |
| 73 | let values = ["data", "ref", "object"] |
| 74 | elseif attrname == 'method' |
| 75 | let values = ["get", "post"] |
| 76 | elseif attrname == 'frame' |
| 77 | let values = ["void", "above", "below", "hsides", "lhs", "rhs", "vsides", "box", "border"] |
| 78 | elseif attrname == 'rules' |
| 79 | let values = ["none", "groups", "rows", "all"] |
| 80 | elseif attrname == 'align' |
| 81 | let values = ["left", "center", "right", "justify", "char"] |
| 82 | elseif attrname == 'valign' |
| 83 | let values = ["top", "middle", "bottom", "baseline"] |
| 84 | elseif attrname == 'scope' |
| 85 | let values = ["row", "col", "rowgroup", "colgroup"] |
| 86 | elseif attrname == 'href' |
| 87 | " Now we are looking for local anchors defined by name or id |
| 88 | if entered_value =~ '^#' |
| 89 | let file = join(getline(1, line('$')), ' ') |
| 90 | " Split it be sure there will be one id/name element in |
| 91 | " item, it will be also first word [a-zA-Z0-9_-] in element |
| 92 | let oneelement = split(file, "\\(meta \\)\\@<!\\(name\\|id\\)\\s*=\\s*[\"']") |
| 93 | for i in oneelement |
| 94 | let values += ['#'.matchstr(i, "^[a-zA-Z][a-zA-Z0-9%_-]*")] |
| 95 | endfor |
| 96 | endif |
| 97 | elseif attrname == 'type' |
| 98 | if a:base =~ '^input' |
| 99 | let values = ["input-text", "password", "checkbox", "radio", "submit", "reset", "input-file", "hidden", "input-image", "input-button"] |
| 100 | elseif a:base =~ '^button' |
| 101 | let values = ["button", "submit", "reset"] |
| 102 | endif |
| 103 | else |
| 104 | return [] |
| 105 | endif |
| 106 | |
| 107 | if len(values) == 0 |
| 108 | return [] |
| 109 | endif |
| 110 | |
| 111 | " We need special version of sbase |
| 112 | let attrbase = matchstr(a:base, ".*[\"']") |
| 113 | |
| 114 | for m in values |
| 115 | if m =~ '^' . entered_value |
| 116 | call add(res, attrbase . m . '" ') |
| 117 | endif |
| 118 | endfor |
| 119 | endif |
| 120 | " Shorten a:base to not include last word |
| 121 | let sbase = matchstr(a:base, '.*\ze\s.*') |
| 122 | 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\)$' |
| 123 | let attrs = g:coregroup |
| 124 | elseif tag == 'a' |
| 125 | let tagspec = ["charset", "type", "name", "href", "hreflang", "rel", "rev", "shape", "coords"] |
| 126 | let attrs = extend(tagspec, g:coregroup) |
| 127 | let attrs = extend(attrs, g:focus) |
| 128 | elseif tag == 'area' |
| 129 | let attrs = g:coregroup |
| 130 | elseif tag == 'base' |
| 131 | let attrs = ["href", "id"] |
| 132 | elseif tag == 'blockquote' |
| 133 | let attrs = g:coregroup |
| 134 | let attrs = extend(attrs, ["cite"]) |
| 135 | elseif tag == 'body' |
| 136 | let attrs = g:coregroup |
| 137 | let attrs = extend(attrs, ["onload", "onunload"]) |
| 138 | elseif tag == 'br' |
| 139 | let attrs = g:coreattrs |
| 140 | elseif tag == 'button' |
| 141 | let attrs = g:coreattrs |
| 142 | let attrs = extend(attrs, g:focus) |
| 143 | let attrs = extend(attrs, ["name", "value", "type"]) |
| 144 | elseif tag == '^\(col\|colgroup\)$' |
| 145 | let attrs = g:coreattrs |
| 146 | let attrs = extend(attrs, ["span", "width", "align", "char", "charoff", "valign"]) |
| 147 | elseif tag =~ '^\(del\|ins\)$' |
| 148 | let attrs = g:coreattrs |
| 149 | let attrs = extend(attrs, ["cite", "datetime"]) |
| 150 | elseif tag == 'form' |
| 151 | let attrs = g:coreattrs |
| 152 | let attrs = extend(attrs, ["action", "method", "enctype", "onsubmit", "onreset", "accept", "accept-charset"]) |
| 153 | elseif tag == 'head' |
| 154 | let attrs = g:i18n |
| 155 | let attrs = extend(attrs, ["id", "profile"]) |
| 156 | elseif tag == 'html' |
| 157 | let attrs = g:i18n |
| 158 | let attrs = extend(attrs, ["id", "xmlns"]) |
| 159 | elseif tag == 'img' |
| 160 | let attrs = g:coreattrs |
| 161 | let attrs = extend(attrs, ["src", "alt", "longdesc", "height", "width", "usemap", "ismap"]) |
| 162 | elseif tag == 'input' |
| 163 | let attrs = g:coreattrs |
| 164 | let attrs = extend(attrs, g:focus) |
| 165 | let attrs = extend(attrs, ["type", "name", "value", "checked", "disabled", "readonly", "size", "maxlength", "src", "alt", "usemap", "onselect", "onchange", "accept"]) |
| 166 | elseif tag == 'label' |
| 167 | let attrs = g:coreattrs |
| 168 | let attrs = extend(attrs, ["for", "accesskey", "onfocus", "onblur"]) |
| 169 | elseif tag == 'legend' |
| 170 | let attrs = g:coreattrs |
| 171 | let attrs = extend(attrs, ["accesskey"]) |
| 172 | elseif tag == 'link' |
| 173 | let attrs = g:coreattrs |
| 174 | let attrs = extend(attrs, ["charset", "href", "hreflang", "type", "rel", "rev", "media"]) |
| 175 | elseif tag == 'map' |
| 176 | let attrs = g:i18n |
| 177 | let attrs = extend(attrs, g:events) |
| 178 | let attrs = extend(attrs, ["id", "class", "style", "title", "name"]) |
| 179 | elseif tag == 'meta' |
| 180 | let attrs = g:i18n |
| 181 | let attrs = extend(attrs, ["id", "http-equiv", "content", "scheme", "name"]) |
| 182 | elseif tag == 'title' |
| 183 | let attrs = g:i18n |
| 184 | let attrs = extend(attrs, ["id"]) |
| 185 | elseif tag == 'object' |
| 186 | let attrs = g:coreattrs |
| 187 | let attrs = extend(attrs, ["declare", "classid", "codebase", "data", "type", "codetype", "archive", "standby", "height", "width", "usemap", "name", "tabindex"]) |
| 188 | elseif tag == 'optgroup' |
| 189 | let attrs = g:coreattrs |
| 190 | let attrs = extend(attrs, ["disbled", "label"]) |
| 191 | elseif tag == 'option' |
| 192 | let attrs = g:coreattrs |
| 193 | let attrs = extend(attrs, ["disbled", "selected", "value", "label"]) |
| 194 | elseif tag == 'param' |
| 195 | let attrs = ["id", "name", "value", "valuetype", "type"] |
| 196 | elseif tag == 'pre' |
| 197 | let attrs = g:coreattrs |
| 198 | let attrs = extend(attrs, ["xml:space"]) |
| 199 | elseif tag == 'q' |
| 200 | let attrs = g:coreattrs |
| 201 | let attrs = extend(attrs, ["cite"]) |
| 202 | elseif tag == 'script' |
| 203 | let attrs = ["id", "charset", "type", "src", "defer", "xml:space"] |
| 204 | elseif tag == 'select' |
| 205 | let attrs = g:coreattrs |
| 206 | let attrs = extend(attrs, ["name", "size", "multiple", "disabled", "tabindex", "onfocus", "onblur", "onchange"]) |
| 207 | elseif tag == 'style' |
| 208 | let attrs = g:coreattrs |
| 209 | let attrs = extend(attrs, ["id", "type", "media", "title", "xml:space"]) |
| 210 | elseif tag == 'table' |
| 211 | let attrs = g:coreattrs |
| 212 | let attrs = extend(attrs, ["summary", "width", "border", "frame", "rules" "cellspacing", "cellpadding"]) |
| 213 | elseif tag =~ '^\(thead\|tfoot\|tbody\|tr\)$' |
| 214 | let attrs = g:coreattrs |
| 215 | let attrs = extend(attrs, ["align", "char", "charoff", "valign"]) |
| 216 | elseif tag == 'textarea' |
| 217 | let attrs = g:coreattrs |
| 218 | let attrs = extend(attrs, g:focus) |
| 219 | let attrs = extend(attrs, ["name", "rows", "cols", "disabled", "readonly", "onselect", "onchange"]) |
| 220 | elseif tag =~ '^\(th\|td\)$' |
| 221 | let attrs = g:coreattrs |
| 222 | let attrs = extend(attrs, ["abbr", "headers", "scope", "rowspan", "colspan", "align", "char", "charoff", "valign"]) |
| 223 | endif |
| 224 | |
| 225 | for m in sort(attrs) |
| 226 | if m =~ '^' . attr |
| 227 | if m =~ '^\(ismap\|defer\|declare\|nohref\|checked\|disabled\|selected\|readonly\)$' |
| 228 | call add(res, sbase.' '.m) |
| 229 | else |
| 230 | call add(res, sbase.' '.m.'="') |
| 231 | endif |
| 232 | endif |
| 233 | endfor |
| 234 | return res |
| 235 | endif |
| 236 | for m in split("a abbr acronym address area b base bdo big blockquote body br button caption cite code col colgroup dd del dfn div dl dt em fieldset form head h1 h2 h3 h4 h5 h6 hr html i img input ins kbd label legend li link map meta noscript object ol optgroup option p param pre q samp script select small span strong style sub sup table tbody td textarea tfoot th thead title tr tt ul var") |
| 237 | if m =~ '^' . a:base |
| 238 | call add(res, m) |
| 239 | endif |
| 240 | endfor |
| 241 | return res |
| 242 | endif |
| 243 | endfunction |