1 ;;; company-css.el --- A company-mode completion back-end for css-mode
3 ;; Copyright (C) 2009, 2011 Free Software Foundation, Inc.
5 ;; Author: Nikolaj Schumacher
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'cl))
29 (defconst company-css-property-alist
30 ;; see http://www.w3.org/TR/CSS21/propidx.html
31 '(("azimuth" angle "left-side" "far-left" "left" "center-left" "center"
32 "center-right" "right" "far-right" "right-side" "behind" "leftwards"
34 ("background" background-color background-image background-repeat
35 background-attachment background-position)
36 ("background-attachment" "scroll" "fixed")
37 ("background-color" color "transparent")
38 ("background-image" uri "none")
39 ("background-position" percentage length "left" "center" "right" percentage
40 length "top" "center" "bottom" "left" "center" "right" "top" "center"
42 ("background-repeat" "repeat" "repeat-x" "repeat-y" "no-repeat")
43 ("border" border-width border-style border-color)
44 ("border-bottom" border)
45 ("border-bottom-color" border-color)
46 ("border-bottom-style" border-style)
47 ("border-bottom-width" border-width)
48 ("border-collapse" "collapse" "separate")
49 ("border-color" color "transparent")
50 ("border-left" border)
51 ("border-left-color" border-color)
52 ("border-left-style" border-style)
53 ("border-left-width" border-width)
54 ("border-right" border)
55 ("border-right-color" border-color)
56 ("border-right-style" border-style)
57 ("border-right-width" border-width)
58 ("border-spacing" length length)
59 ("border-style" border-style)
61 ("border-top-color" border-color)
62 ("border-top-style" border-style)
63 ("border-top-width" border-width)
64 ("border-width" border-width)
65 ("bottom" length percentage "auto")
66 ("caption-side" "top" "bottom")
67 ("clear" "none" "left" "right" "both")
70 ("content" "normal" "none" string uri counter "attr()" "open-quote"
71 "close-quote" "no-open-quote" "no-close-quote")
72 ("counter-increment" identifier integer "none")
73 ("counter-reset" identifier integer "none")
74 ("cue" cue-before cue-after)
75 ("cue-after" uri "none")
76 ("cue-before" uri "none")
77 ("cursor" uri "*" "auto" "crosshair" "default" "pointer" "move" "e-resize"
78 "ne-resize" "nw-resize" "n-resize" "se-resize" "sw-resize" "s-resize"
79 "w-resize" "text" "wait" "help" "progress")
80 ("direction" "ltr" "rtl")
81 ("display" "inline" "block" "list-item" "run-in" "inline-block" "table"
82 "inline-table" "table-row-group" "table-header-group" "table-footer-group"
83 "table-row" "table-column-group" "table-column" "table-cell"
84 "table-caption" "none")
85 ("elevation" angle "below" "level" "above" "higher" "lower")
86 ("empty-cells" "show" "hide")
87 ("float" "left" "right" "none")
88 ("font" font-style font-variant font-weight font-size "/" line-height
89 font-family "caption" "icon" "menu" "message-box" "small-caption"
91 ("font-family" family-name generic-family)
92 ("font-size" absolute-size relative-size length percentage)
93 ("font-style" "normal" "italic" "oblique")
94 ("font-variant" "normal" "small-caps")
95 ("font-weight" "normal" "bold" "bolder" "lighter" "100" "200" "300" "400"
96 "500" "600" "700" "800" "900")
97 ("height" length percentage "auto")
98 ("left" length percentage "auto")
99 ("letter-spacing" "normal" length)
100 ("line-height" "normal" number length percentage)
101 ("list-style" list-style-type list-style-position list-style-image)
102 ("list-style-image" uri "none")
103 ("list-style-position" "inside" "outside")
104 ("list-style-type" "disc" "circle" "square" "decimal" "decimal-leading-zero"
105 "lower-roman" "upper-roman" "lower-greek" "lower-latin" "upper-latin"
106 "armenian" "georgian" "lower-alpha" "upper-alpha" "none")
107 ("margin" margin-width)
108 ("margin-bottom" margin-width)
109 ("margin-left" margin-width)
110 ("margin-right" margin-width)
111 ("margin-top" margin-width)
112 ("max-height" length percentage "none")
113 ("max-width" length percentage "none")
114 ("min-height" length percentage)
115 ("min-width" length percentage)
117 ("outline" outline-color outline-style outline-width)
118 ("outline-color" color "invert")
119 ("outline-style" border-style)
120 ("outline-width" border-width)
121 ("overflow" "visible" "hidden" "scroll" "auto")
122 ("padding" padding-width)
123 ("padding-bottom" padding-width)
124 ("padding-left" padding-width)
125 ("padding-right" padding-width)
126 ("padding-top" padding-width)
127 ("page-break-after" "auto" "always" "avoid" "left" "right")
128 ("page-break-before" "auto" "always" "avoid" "left" "right")
129 ("page-break-inside" "avoid" "auto")
130 ("pause" time percentage)
131 ("pause-after" time percentage)
132 ("pause-before" time percentage)
133 ("pitch" frequency "x-low" "low" "medium" "high" "x-high")
134 ("pitch-range" number)
135 ("play-during" uri "mix" "repeat" "auto" "none")
136 ("position" "static" "relative" "absolute" "fixed")
137 ("quotes" string string "none")
139 ("right" length percentage "auto")
140 ("speak" "normal" "none" "spell-out")
141 ("speak-header" "once" "always")
142 ("speak-numeral" "digits" "continuous")
143 ("speak-punctuation" "code" "none")
144 ("speech-rate" number "x-slow" "slow" "medium" "fast" "x-fast" "faster"
147 ("table-layout" "auto" "fixed")
148 ("text-align" "left" "right" "center" "justify")
149 ("text-decoration" "none" "underline" "overline" "line-through" "blink")
150 ("text-indent" length percentage)
151 ("text-transform" "capitalize" "uppercase" "lowercase" "none")
152 ("top" length percentage "auto")
153 ("unicode-bidi" "normal" "embed" "bidi-override")
154 ("vertical-align" "baseline" "sub" "super" "top" "text-top" "middle"
155 "bottom" "text-bottom" percentage length)
156 ("visibility" "visible" "hidden" "collapse")
157 ("voice-family" specific-voice generic-voice "*" specific-voice
159 ("volume" number percentage "silent" "x-soft" "soft" "medium" "loud"
161 ("white-space" "normal" "pre" "nowrap" "pre-wrap" "pre-line")
163 ("width" length percentage "auto")
164 ("word-spacing" "normal" length)
165 ("z-index" "auto" integer))
166 "A list of CSS properties and their possible values.")
168 (defconst company-css-value-classes
169 '((absolute-size "xx-small" "x-small" "small" "medium" "large" "x-large"
171 (border-style "none" "hidden" "dotted" "dashed" "solid" "double" "groove"
172 "ridge" "inset" "outset")
173 (color "aqua" "black" "blue" "fuchsia" "gray" "green" "lime" "maroon" "navy"
174 "olive" "orange" "purple" "red" "silver" "teal" "white" "yellow")
175 (counter "counter(,)")
176 (family-name "Courier" "Helvetica" "Times")
177 (generic-family "serif" "sans-serif" "cursive" "fantasy" "monospace")
178 (generic-voice "male" "female" "child")
179 (margin-width "auto") ;; length percentage
180 (relative-size "larger" "smaller")
183 "A list of CSS property value classes and their contents.")
184 ;; missing, because not completable
185 ;; <angle><frequency><identifier><integer><length><number><padding-width>
186 ;; <percentage><specific-voice><string><time><uri>
188 (defconst company-css-html-tags
189 '("a" "abbr" "acronym" "address" "applet" "area" "b" "base" "basefont" "bdo"
190 "big" "blockquote" "body" "br" "button" "caption" "center" "cite" "code"
191 "col" "colgroup" "dd" "del" "dfn" "dir" "div" "dl" "dt" "em" "fieldset"
192 "font" "form" "frame" "frameset" "h1" "h2" "h3" "h4" "h5" "h6" "head" "hr"
193 "html" "i" "iframe" "img" "input" "ins" "isindex" "kbd" "label" "legend"
194 "li" "link" "map" "menu" "meta" "noframes" "noscript" "object" "ol"
195 "optgroup" "option" "p" "param" "pre" "q" "s" "samp" "script" "select"
196 "small" "span" "strike" "strong" "style" "sub" "sup" "table" "tbody" "td"
197 "textarea" "tfoot" "th" "thead" "title" "tr" "tt" "u" "ul" "var")
198 "A list of HTML tags for use in CSS completion.")
200 (defconst company-css-pseudo-classes
201 '("active" "after" "before" "first" "first-child" "first-letter" "first-line"
202 "focus" "hover" "lang" "left" "link" "right" "visited")
203 "Identifiers for CSS pseudo-elements and pseudo-classes.")
205 (defconst company-css-property-cache (make-hash-table :size 115 :test 'equal))
207 (defun company-css-property-values (attribute)
208 "Access the `company-css-property-alist' cached and flattened."
209 (or (gethash attribute company-css-property-cache)
211 (dolist (value (cdr (assoc attribute company-css-property-alist)))
213 (dolist (child (or (cdr (assoc value company-css-value-classes))
214 (company-css-property-values
215 (symbol-name value))))
216 (add-to-list 'results child))
217 (add-to-list 'results value)))
218 (setq results (sort results 'string<))
219 (puthash attribute results company-css-property-cache)
222 ;;; bracket detection
224 (defconst company-css-braces-syntax-table
225 (let ((table (make-syntax-table)))
226 (setf (aref table ?{) '(4 . 125))
227 (setf (aref table ?}) '(5 . 123))
229 "A syntax table giving { and } paren syntax.")
231 (defun company-css-inside-braces-p ()
232 "Return non-nil, if point is within matched { and }."
234 (with-syntax-table company-css-braces-syntax-table
235 (let ((parse-sexp-ignore-comments t))
236 (scan-lists (point) -1 1)))))
239 (defconst company-css-tag-regexp
240 (concat "\\(?:\\`\\|}\\)[[:space:]]*"
244 "\\(?:#\\|\\_<[[:alpha:]]\\)[[:alnum:]-#]*\\(?:\\[[^]]*\\]\\)?"
245 ;; space or selectors
246 "\\(?:[[:space:]]+\\|[[:space:]]*[+,>][[:space:]]*\\)"
248 "\\(\\(?:#\\|\\_<[[:alpha:]]\\)\\(?:[[:alnum:]-#]*\\_>\\)?\\_>\\|\\)"
250 "A regular expression matching CSS tags.")
253 (defconst company-css-pseudo-regexp
254 (concat "\\(?:\\`\\|}\\)[[:space:]]*"
258 "\\(?:#\\|\\_<[[:alpha:]]\\)[[:alnum:]-#]*\\(?:\\[[^]]*\\]\\)?"
259 ;; space or delimiters
260 "\\(?:[[:space:]]+\\|[[:space:]]*[+,>][[:space:]]*\\)"
262 "\\(?:\\(?:\\#\\|\\_<[[:alpha:]]\\)[[:alnum:]-#]*\\):"
263 "\\([[:alpha:]-]+\\_>\\|\\)\\_>\\=")
264 "A regular expression matching CSS pseudo classes.")
268 (defun company-css-grab-property ()
269 "Return the CSS property before point, if any.
270 Returns \"\" if no property found, but feasible at this position."
271 (when (company-css-inside-braces-p)
272 (company-grab-symbol)))
275 (defconst company-css-property-value-regexp
276 "\\_<\\([[:alpha:]-]+\\):\\(?:[^};]*[[:space:]]+\\)?\\([^};]*\\_>\\|\\)\\="
277 "A regular expression matching CSS tags.")
280 (defun company-css (command &optional arg &rest ignored)
281 "A `company-mode' completion back-end for `css-mode'."
282 (interactive (list 'interactive))
284 (interactive (company-begin-backend 'company-css))
285 (prefix (and (derived-mode-p 'css-mode)
286 (or (company-grab company-css-tag-regexp 1)
287 (company-grab company-css-pseudo-regexp 1)
288 (company-grab company-css-property-value-regexp 2)
289 (company-css-grab-property))))
292 ((company-grab company-css-tag-regexp 1)
293 (all-completions arg company-css-html-tags))
294 ((company-grab company-css-pseudo-regexp 1)
295 (all-completions arg company-css-pseudo-classes))
296 ((company-grab company-css-property-value-regexp 2)
298 (company-css-property-values
299 (company-grab company-css-property-value-regexp 1))))
300 ((company-css-grab-property)
301 (all-completions arg company-css-property-alist))))
304 (provide 'company-css)
305 ;;; company-css.el ends here