Fix Bug#24962
[emacs.git] / lisp / textmodes / css-mode.el
blob9e36a881a3e7142ef971c6b062a8f333eb926269
1 ;;; css-mode.el --- Major mode to edit CSS files -*- lexical-binding: t -*-
3 ;; Copyright (C) 2006-2016 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Maintainer: Simen Heggestøyl <simenheg@gmail.com>
7 ;; Keywords: hypermedia
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Yet another CSS mode.
28 ;;; Todo:
30 ;; - electric ; and }
31 ;; - filling code with auto-fill-mode
32 ;; - fix font-lock errors with multi-line selectors
34 ;;; Code:
36 (require 'seq)
37 (require 'sgml-mode)
38 (require 'smie)
40 (defgroup css nil
41 "Cascading Style Sheets (CSS) editing mode."
42 :group 'languages)
44 (defconst css-pseudo-class-ids
45 '("active" "checked" "disabled" "empty" "enabled" "first"
46 "first-child" "first-of-type" "focus" "hover" "indeterminate" "lang"
47 "last-child" "last-of-type" "left" "link" "not" "nth-child"
48 "nth-last-child" "nth-last-of-type" "nth-of-type" "only-child"
49 "only-of-type" "right" "root" "target" "visited")
50 "Identifiers for pseudo-classes.")
52 (defconst css-pseudo-element-ids
53 '("after" "before" "first-letter" "first-line")
54 "Identifiers for pseudo-elements.")
56 (defconst css-at-ids
57 '("charset" "font-face" "import" "keyframes" "media" "namespace"
58 "page" "supports")
59 "Identifiers that appear in the form @foo.")
61 (defconst scss-at-ids
62 '("at-root" "content" "debug" "each" "else" "else if" "error" "extend"
63 "for" "function" "if" "import" "include" "mixin" "return" "warn"
64 "while")
65 "Additional identifiers that appear in the form @foo in SCSS.")
67 (defvar css--at-ids css-at-ids
68 "List of at-rules for the current mode.")
69 (make-variable-buffer-local 'css--at-ids)
71 (defconst css-bang-ids
72 '("important")
73 "Identifiers that appear in the form !foo.")
75 (defconst scss-bang-ids
76 '("default" "global" "optional")
77 "Additional identifiers that appear in the form !foo in SCSS.")
79 (defvar css--bang-ids css-bang-ids
80 "List of bang-rules for the current mode.")
81 (make-variable-buffer-local 'css--bang-ids)
83 (defconst css-descriptor-ids
84 '("ascent" "baseline" "bbox" "cap-height" "centerline" "definition-src"
85 "descent" "font-family" "font-size" "font-stretch" "font-style"
86 "font-variant" "font-weight" "mathline" "panose-1" "slope" "src" "stemh"
87 "stemv" "topline" "unicode-range" "units-per-em" "widths" "x-height")
88 "Identifiers for font descriptors.")
90 (defconst css-media-ids
91 '("all" "aural" "bitmap" "continuous" "grid" "paged" "static" "tactile"
92 "visual")
93 "Identifiers for types of media.")
95 (defconst css-property-alist
96 ;; CSS 2.1 properties (http://www.w3.org/TR/CSS21/propidx.html).
98 ;; Properties duplicated by any of the CSS3 modules below have been
99 ;; removed.
100 '(("azimuth" angle "left-side" "far-left" "left" "center-left"
101 "center" "center-right" "right" "far-right" "right-side" "behind"
102 "leftwards" "rightwards")
103 ("border-collapse" "collapse" "separate")
104 ("border-spacing" length)
105 ("bottom" length percentage "auto")
106 ("caption-side" "top" "bottom")
107 ("clear" "none" "left" "right" "both")
108 ("clip" shape "auto")
109 ("content" "normal" "none" string uri counter "attr()"
110 "open-quote" "close-quote" "no-open-quote" "no-close-quote")
111 ("counter-increment" identifier integer "none")
112 ("counter-reset" identifier integer "none")
113 ("cue" cue-before cue-after)
114 ("cue-after" uri "none")
115 ("cue-before" uri "none")
116 ("direction" "ltr" "rtl")
117 ("display" "inline" "block" "list-item" "inline-block" "table"
118 "inline-table" "table-row-group" "table-header-group"
119 "table-footer-group" "table-row" "table-column-group"
120 "table-column" "table-cell" "table-caption" "none"
121 ;; CSS Flexible Box Layout Module Level 1
122 ;; (https://www.w3.org/TR/css3-flexbox/#valdef-display-flex)
123 "flex" "inline-flex")
124 ("elevation" angle "below" "level" "above" "higher" "lower")
125 ("empty-cells" "show" "hide")
126 ("float" "left" "right" "none")
127 ("height" length percentage "auto")
128 ("left" length percentage "auto")
129 ("line-height" "normal" number length percentage)
130 ("list-style" list-style-type list-style-position
131 list-style-image)
132 ("list-style-image" uri "none")
133 ("list-style-position" "inside" "outside")
134 ("list-style-type" "disc" "circle" "square" "decimal"
135 "decimal-leading-zero" "lower-roman" "upper-roman" "lower-greek"
136 "lower-latin" "upper-latin" "armenian" "georgian" "lower-alpha"
137 "upper-alpha" "none")
138 ("margin" margin-width)
139 ("margin-bottom" margin-width)
140 ("margin-left" margin-width)
141 ("margin-right" margin-width)
142 ("margin-top" margin-width)
143 ("max-height" length percentage "none")
144 ("max-width" length percentage "none")
145 ("min-height" length percentage)
146 ("min-width" length percentage)
147 ("padding" padding-width)
148 ("padding-bottom" padding-width)
149 ("padding-left" padding-width)
150 ("padding-right" padding-width)
151 ("padding-top" padding-width)
152 ("page-break-after" "auto" "always" "avoid" "left" "right")
153 ("page-break-before" "auto" "always" "avoid" "left" "right")
154 ("page-break-inside" "avoid" "auto")
155 ("pause" time percentage)
156 ("pause-after" time percentage)
157 ("pause-before" time percentage)
158 ("pitch" frequency "x-low" "low" "medium" "high" "x-high")
159 ("pitch-range" number)
160 ("play-during" uri "mix" "repeat" "auto" "none")
161 ("position" "static" "relative" "absolute" "fixed")
162 ("quotes" string "none")
163 ("richness" number)
164 ("right" length percentage "auto")
165 ("speak" "normal" "none" "spell-out")
166 ("speak-header" "once" "always")
167 ("speak-numeral" "digits" "continuous")
168 ("speak-punctuation" "code" "none")
169 ("speech-rate" number "x-slow" "slow" "medium" "fast" "x-fast"
170 "faster" "slower")
171 ("stress" number)
172 ("table-layout" "auto" "fixed")
173 ("top" length percentage "auto")
174 ("unicode-bidi" "normal" "embed" "bidi-override")
175 ("vertical-align" "baseline" "sub" "super" "top" "text-top"
176 "middle" "bottom" "text-bottom" percentage length)
177 ("visibility" "visible" "hidden" "collapse")
178 ("voice-family" specific-voice generic-voice specific-voice
179 generic-voice)
180 ("volume" number percentage "silent" "x-soft" "soft" "medium"
181 "loud" "x-loud")
182 ("width" length percentage "auto")
183 ("z-index" "auto" integer)
185 ;; CSS Animations
186 ;; (http://www.w3.org/TR/css3-animations/#property-index)
187 ("animation" single-animation-name time single-timing-function
188 single-animation-iteration-count single-animation-direction
189 single-animation-fill-mode single-animation-play-state)
190 ("animation-delay" time)
191 ("animation-direction" single-animation-direction)
192 ("animation-duration" time)
193 ("animation-fill-mode" single-animation-fill-mode)
194 ("animation-iteration-count" single-animation-iteration-count)
195 ("animation-name" single-animation-name)
196 ("animation-play-state" single-animation-play-state)
197 ("animation-timing-function" single-timing-function)
199 ;; CSS Backgrounds and Borders Module Level 3
200 ;; (http://www.w3.org/TR/css3-background/#property-index)
201 ("background" bg-layer final-bg-layer)
202 ("background-attachment" attachment)
203 ("background-clip" box)
204 ("background-color" color)
205 ("background-image" bg-image)
206 ("background-origin" box)
207 ("background-position" position)
208 ("background-repeat" repeat-style)
209 ("background-size" bg-size)
210 ("border" line-width line-style color)
211 ("border-bottom" line-width line-style color)
212 ("border-bottom-color" color)
213 ("border-bottom-left-radius" length percentage)
214 ("border-bottom-right-radius" length percentage)
215 ("border-bottom-style" line-style)
216 ("border-bottom-width" line-width)
217 ("border-color" color)
218 ("border-image" border-image-source border-image-slice
219 border-image-width border-image-outset border-image-repeat)
220 ("border-image-outset" length number)
221 ("border-image-repeat" "stretch" "repeat" "round" "space")
222 ("border-image-slice" number percentage "fill")
223 ("border-image-source" "none" image)
224 ("border-image-width" length percentage number "auto")
225 ("border-left" line-width line-style color)
226 ("border-left-color" color)
227 ("border-left-style" line-style)
228 ("border-left-width" line-width)
229 ("border-radius" length percentage)
230 ("border-right" line-width line-style color)
231 ("border-right-color" color)
232 ("border-right-style" line-style)
233 ("border-right-width" line-width)
234 ("border-style" line-style)
235 ("border-top" line-width line-style color)
236 ("border-top-color" color)
237 ("border-top-left-radius" length percentage)
238 ("border-top-right-radius" length percentage)
239 ("border-top-style" line-style)
240 ("border-top-width" line-width)
241 ("border-width" line-width)
242 ("box-shadow" "none" shadow)
244 ;; CSS Basic User Interface Module Level 3 (CSS3 UI)
245 ;; (http://www.w3.org/TR/css3-ui/#property-index)
246 ("box-sizing" "content-box" "border-box")
247 ("caret-color" "auto" color)
248 ("cursor" uri x y "auto" "default" "none" "context-menu" "help"
249 "pointer" "progress" "wait" "cell" "crosshair" "text"
250 "vertical-text" "alias" "copy" "move" "no-drop" "not-allowed"
251 "grab" "grabbing" "e-resize" "n-resize" "ne-resize" "nw-resize"
252 "s-resize" "se-resize" "sw-resize" "w-resize" "ew-resize"
253 "ns-resize" "nesw-resize" "nwse-resize" "col-resize" "row-resize"
254 "all-scroll" "zoom-in" "zoom-out")
255 ("nav-down" "auto" id "current" "root" target-name)
256 ("nav-left" "auto" id "current" "root" target-name)
257 ("nav-right" "auto" id "current" "root" target-name)
258 ("nav-up" "auto" id "current" "root" target-name)
259 ("outline" outline-color outline-style outline-width)
260 ("outline-color" color "invert")
261 ("outline-offset" length)
262 ("outline-style" "auto" border-style)
263 ("outline-width" border-width)
264 ("resize" "none" "both" "horizontal" "vertical")
265 ("text-overflow" "clip" "ellipsis" string)
267 ;; CSS Color Module Level 3
268 ;; (http://www.w3.org/TR/css3-color/#property)
269 ("color" color)
270 ("opacity" alphavalue)
272 ;; CSS Flexible Box Layout Module Level 1
273 ;; (http://www.w3.org/TR/css-flexbox-1/#property-index)
274 ("align-content" "flex-start" "flex-end" "center" "space-between"
275 "space-around" "stretch")
276 ("align-items" "flex-start" "flex-end" "center" "baseline"
277 "stretch")
278 ("align-self" "auto" "flex-start" "flex-end" "center" "baseline"
279 "stretch")
280 ("flex" "none" flex-grow flex-shrink flex-basis)
281 ("flex-basis" "auto" "content" width)
282 ("flex-direction" "row" "row-reverse" "column" "column-reverse")
283 ("flex-flow" flex-direction flex-wrap)
284 ("flex-grow" number)
285 ("flex-shrink" number)
286 ("flex-wrap" "nowrap" "wrap" "wrap-reverse")
287 ("justify-content" "flex-start" "flex-end" "center"
288 "space-between" "space-around")
289 ("order" integer)
291 ;; CSS Fonts Module Level 3
292 ;; (http://www.w3.org/TR/css3-fonts/#property-index)
293 ("font" font-style font-variant-css21 font-weight font-stretch
294 font-size line-height font-family "caption" "icon" "menu"
295 "message-box" "small-caption" "status-bar")
296 ("font-family" family-name generic-family)
297 ("font-feature-settings" "normal" feature-tag-value)
298 ("font-kerning" "auto" "normal" "none")
299 ("font-language-override" "normal" string)
300 ("font-size" absolute-size relative-size length percentage)
301 ("font-size-adjust" "none" number)
302 ("font-stretch" "normal" "ultra-condensed" "extra-condensed"
303 "condensed" "semi-condensed" "semi-expanded" "expanded"
304 "extra-expanded" "ultra-expanded")
305 ("font-style" "normal" "italic" "oblique")
306 ("font-synthesis" "none" "weight" "style")
307 ("font-variant" "normal" "none" common-lig-values
308 discretionary-lig-values historical-lig-values
309 contextual-alt-values "stylistic()" "historical-forms"
310 "styleset()" "character-variant()" "swash()" "ornaments()"
311 "annotation()" "small-caps" "all-small-caps" "petite-caps"
312 "all-petite-caps" "unicase" "titling-caps" numeric-figure-values
313 numeric-spacing-values numeric-fraction-values "ordinal"
314 "slashed-zero" east-asian-variant-values east-asian-width-values
315 "ruby")
316 ("font-variant-alternates" "normal" "stylistic()"
317 "historical-forms" "styleset()" "character-variant()" "swash()"
318 "ornaments()" "annotation()")
319 ("font-variant-caps" "normal" "small-caps" "all-small-caps"
320 "petite-caps" "all-petite-caps" "unicase" "titling-caps")
321 ("font-variant-east-asian" "normal" east-asian-variant-values
322 east-asian-width-values "ruby")
323 ("font-variant-ligatures" "normal" "none" common-lig-values
324 discretionary-lig-values historical-lig-values
325 contextual-alt-values)
326 ("font-variant-numeric" "normal" numeric-figure-values
327 numeric-spacing-values numeric-fraction-values "ordinal"
328 "slashed-zero")
329 ("font-variant-position" "normal" "sub" "super")
330 ("font-weight" "normal" "bold" "bolder" "lighter" "100" "200"
331 "300" "400" "500" "600" "700" "800" "900")
333 ;; CSS Fragmentation Module Level 3
334 ;; (https://www.w3.org/TR/css-break-3/#property-index)
335 ("box-decoration-break" "slice" "clone")
336 ("break-after" "auto" "avoid" "avoid-page" "page" "left" "right"
337 "recto" "verso" "avoid-column" "column" "avoid-region" "region")
338 ("break-before" "auto" "avoid" "avoid-page" "page" "left" "right"
339 "recto" "verso" "avoid-column" "column" "avoid-region" "region")
340 ("break-inside" "auto" "avoid" "avoid-page" "avoid-column"
341 "avoid-region")
342 ("orphans" integer)
343 ("widows" integer)
345 ;; CSS Multi-column Layout Module
346 ;; (https://www.w3.org/TR/css3-multicol/#property-index)
347 ;; "break-after", "break-before", and "break-inside" are left out
348 ;; below, because they're already included in CSS Fragmentation
349 ;; Module Level 3.
350 ("column-count" integer "auto")
351 ("column-fill" "auto" "balance")
352 ("column-gap" length "normal")
353 ("column-rule" column-rule-width column-rule-style
354 column-rule-color "transparent")
355 ("column-rule-color" color)
356 ("column-rule-style" border-style)
357 ("column-rule-width" border-width)
358 ("column-span" "none" "all")
359 ("column-width" length "auto")
360 ("columns" column-width column-count)
362 ;; CSS Overflow Module Level 3
363 ;; (http://www.w3.org/TR/css-overflow-3/#property-index)
364 ("max-lines" "none" integer)
365 ("overflow" "visible" "hidden" "scroll" "auto" "paged-x" "paged-y"
366 "paged-x-controls" "paged-y-controls" "fragments")
367 ("overflow-x" "visible" "hidden" "scroll" "auto" "paged-x"
368 "paged-y" "paged-x-controls" "paged-y-controls" "fragments")
369 ("overflow-y" "visible" "hidden" "scroll" "auto" "paged-x"
370 "paged-y" "paged-x-controls" "paged-y-controls" "fragments")
372 ;; CSS Text Decoration Module Level 3
373 ;; (http://dev.w3.org/csswg/css-text-decor-3/#property-index)
374 ("text-decoration" text-decoration-line text-decoration-style
375 text-decoration-color)
376 ("text-decoration-color" color)
377 ("text-decoration-line" "none" "underline" "overline"
378 "line-through" "blink")
379 ("text-decoration-skip" "none" "objects" "spaces" "ink" "edges"
380 "box-decoration")
381 ("text-decoration-style" "solid" "double" "dotted" "dashed"
382 "wavy")
383 ("text-emphasis" text-emphasis-style text-emphasis-color)
384 ("text-emphasis-color" color)
385 ("text-emphasis-position" "over" "under" "right" "left")
386 ("text-emphasis-style" "none" "filled" "open" "dot" "circle"
387 "double-circle" "triangle" "sesame" string)
388 ("text-shadow" "none" length color)
389 ("text-underline-position" "auto" "under" "left" "right")
391 ;; CSS Text Module Level 3
392 ;; (http://www.w3.org/TR/css3-text/#property-index)
393 ("hanging-punctuation" "none" "first" "force-end" "allow-end"
394 "last")
395 ("hyphens" "none" "manual" "auto")
396 ("letter-spacing" "normal" length)
397 ("line-break" "auto" "loose" "normal" "strict")
398 ("overflow-wrap" "normal" "break-word")
399 ("tab-size" integer length)
400 ("text-align" "start" "end" "left" "right" "center" "justify"
401 "match-parent")
402 ("text-align-last" "auto" "start" "end" "left" "right" "center"
403 "justify")
404 ("text-indent" length percentage)
405 ("text-justify" "auto" "none" "inter-word" "distribute")
406 ("text-transform" "none" "capitalize" "uppercase" "lowercase"
407 "full-width")
408 ("white-space" "normal" "pre" "nowrap" "pre-wrap" "pre-line")
409 ("word-break" "normal" "keep-all" "break-all")
410 ("word-spacing" "normal" length percentage)
411 ("word-wrap" "normal" "break-word")
413 ;; CSS Transforms Module Level 1
414 ;; (http://www.w3.org/TR/css3-2d-transforms/#property-index)
415 ("backface-visibility" "visible" "hidden")
416 ("perspective" "none" length)
417 ("perspective-origin" "left" "center" "right" "top" "bottom"
418 percentage length)
419 ("transform" "none" transform-list)
420 ("transform-origin" "left" "center" "right" "top" "bottom"
421 percentage length)
422 ("transform-style" "flat" "preserve-3d")
424 ;; CSS Transitions
425 ;; (http://www.w3.org/TR/css3-transitions/#property-index)
426 ("transition" single-transition)
427 ("transition-delay" time)
428 ("transition-duration" time)
429 ("transition-property" "none" single-transition-property "all")
430 ("transition-timing-function" single-transition-timing-function)
432 ;; CSS Will Change Module Level 1
433 ;; (https://www.w3.org/TR/css-will-change-1/#property-index)
434 ("will-change" "auto" animateable-feature)
436 ;; Filter Effects Module Level 1
437 ;; (http://www.w3.org/TR/filter-effects/#property-index)
438 ("color-interpolation-filters" "auto" "sRGB" "linearRGB")
439 ("filter" "none" filter-function-list)
440 ("flood-color" color)
441 ("flood-opacity" number percentage)
442 ("lighting-color" color))
443 "Identifiers for properties and their possible values.
444 The CAR of each entry is the name of a property, while the CDR is
445 a list of possible values for that property. String values in
446 the CDRs represent literal values, while symbols represent one of
447 the value classes found in `css-value-class-alist'. If a symbol
448 is not found in `css-value-class-alist', it's interpreted as a
449 reference back to one of the properties in this list. Some
450 symbols, such as `number' or `identifier', don't produce any
451 further value candidates, since that list would be infinite.")
453 (defconst css-property-ids
454 (mapcar #'car css-property-alist)
455 "Identifiers for properties.")
457 (defconst css-value-class-alist
458 '((absolute-size
459 "xx-small" "x-small" "small" "medium" "large" "x-large"
460 "xx-large")
461 (alphavalue number)
462 (angle "calc()")
463 (animateable-feature "scroll-position" "contents" custom-ident)
464 (attachment "scroll" "fixed" "local")
465 (bg-image image "none")
466 (bg-layer bg-image position repeat-style attachment box)
467 (bg-size length percentage "auto" "cover" "contain")
468 (box "border-box" "padding-box" "content-box")
469 (color
470 "rgb()" "rgba()" "hsl()" "hsla()" named-color "transparent"
471 "currentColor")
472 (common-lig-values "common-ligatures" "no-common-ligatures")
473 (contextual-alt-values "contextual" "no-contextual")
474 (counter "counter()" "counters()")
475 (discretionary-lig-values
476 "discretionary-ligatures" "no-discretionary-ligatures")
477 (east-asian-variant-values
478 "jis78" "jis83" "jis90" "jis04" "simplified" "traditional")
479 (east-asian-width-values "full-width" "proportional-width")
480 (family-name "Courier" "Helvetica" "Times")
481 (feature-tag-value string integer "on" "off")
482 (filter-function
483 "blur()" "brightness()" "contrast()" "drop-shadow()"
484 "grayscale()" "hue-rotate()" "invert()" "opacity()" "sepia()"
485 "saturate()")
486 (filter-function-list filter-function uri)
487 (final-bg-layer
488 bg-image position repeat-style attachment box color)
489 (font-variant-css21 "normal" "small-caps")
490 (frequency "calc()")
491 (generic-family
492 "serif" "sans-serif" "cursive" "fantasy" "monospace")
493 (generic-voice "male" "female" "child")
494 (gradient
495 linear-gradient radial-gradient repeating-linear-gradient
496 repeating-radial-gradient)
497 (historical-lig-values
498 "historical-ligatures" "no-historical-ligatures")
499 (image uri image-list element-reference gradient)
500 (image-list "image()")
501 (integer "calc()")
502 (length "calc()" number)
503 (line-height "normal" number length percentage)
504 (line-style
505 "none" "hidden" "dotted" "dashed" "solid" "double" "groove"
506 "ridge" "inset" "outset")
507 (line-width length "thin" "medium" "thick")
508 (linear-gradient "linear-gradient()")
509 (margin-width "auto" length percentage)
510 (named-color
511 "aliceblue" "antiquewhite" "aqua" "aquamarine" "azure" "beige"
512 "bisque" "black" "blanchedalmond" "blue" "blueviolet" "brown"
513 "burlywood" "cadetblue" "chartreuse" "chocolate" "coral"
514 "cornflowerblue" "cornsilk" "crimson" "cyan" "darkblue"
515 "darkcyan" "darkgoldenrod" "darkgray" "darkgreen" "darkkhaki"
516 "darkmagenta" "darkolivegreen" "darkorange" "darkorchid"
517 "darkred" "darksalmon" "darkseagreen" "darkslateblue"
518 "darkslategray" "darkturquoise" "darkviolet" "deeppink"
519 "deepskyblue" "dimgray" "dodgerblue" "firebrick" "floralwhite"
520 "forestgreen" "fuchsia" "gainsboro" "ghostwhite" "gold"
521 "goldenrod" "gray" "green" "greenyellow" "honeydew" "hotpink"
522 "indianred" "indigo" "ivory" "khaki" "lavender" "lavenderblush"
523 "lawngreen" "lemonchiffon" "lightblue" "lightcoral" "lightcyan"
524 "lightgoldenrodyellow" "lightgray" "lightgreen" "lightpink"
525 "lightsalmon" "lightseagreen" "lightskyblue" "lightslategray"
526 "lightsteelblue" "lightyellow" "lime" "limegreen" "linen"
527 "magenta" "maroon" "mediumaquamarine" "mediumblue" "mediumorchid"
528 "mediumpurple" "mediumseagreen" "mediumslateblue"
529 "mediumspringgreen" "mediumturquoise" "mediumvioletred"
530 "midnightblue" "mintcream" "mistyrose" "moccasin" "navajowhite"
531 "navy" "oldlace" "olive" "olivedrab" "orange" "orangered"
532 "orchid" "palegoldenrod" "palegreen" "paleturquoise"
533 "palevioletred" "papayawhip" "peachpuff" "peru" "pink" "plum"
534 "powderblue" "purple" "rebeccapurple" "red" "rosybrown"
535 "royalblue" "saddlebrown" "salmon" "sandybrown" "seagreen"
536 "seashell" "sienna" "silver" "skyblue" "slateblue" "slategray"
537 "snow" "springgreen" "steelblue" "tan" "teal" "thistle" "tomato"
538 "turquoise" "violet" "wheat" "white" "whitesmoke" "yellow"
539 "yellowgreen")
540 (number "calc()")
541 (numeric-figure-values "lining-nums" "oldstyle-nums")
542 (numeric-fraction-values "diagonal-fractions" "stacked-fractions")
543 (numeric-spacing-values "proportional-nums" "tabular-nums")
544 (padding-width length percentage)
545 (position
546 "left" "center" "right" "top" "bottom" percentage length)
547 (radial-gradient "radial-gradient()")
548 (relative-size "larger" "smaller")
549 (repeat-style
550 "repeat-x" "repeat-y" "repeat" "space" "round" "no-repeat")
551 (repeating-linear-gradient "repeating-linear-gradient()")
552 (repeating-radial-gradient "repeating-radial-gradient()")
553 (shadow "inset" length color)
554 (shape "rect()")
555 (single-animation-direction
556 "normal" "reverse" "alternate" "alternate-reverse")
557 (single-animation-fill-mode "none" "forwards" "backwards" "both")
558 (single-animation-iteration-count "infinite" number)
559 (single-animation-name "none" identifier)
560 (single-animation-play-state "running" "paused")
561 (single-timing-function single-transition-timing-function)
562 (single-transition
563 "none" single-transition-property time
564 single-transition-timing-function)
565 (single-transition-property "all" identifier)
566 (single-transition-timing-function
567 "ease" "linear" "ease-in" "ease-out" "ease-in-out" "step-start"
568 "step-end" "steps()" "cubic-bezier()")
569 (specific-voice identifier)
570 (target-name string)
571 (time "calc()")
572 (transform-list
573 "matrix()" "translate()" "translateX()" "translateY()" "scale()"
574 "scaleX()" "scaleY()" "rotate()" "skew()" "skewX()" "skewY()"
575 "matrix3d()" "translate3d()" "translateZ()" "scale3d()"
576 "scaleZ()" "rotate3d()" "rotateX()" "rotateY()" "rotateZ()"
577 "perspective()")
578 (uri "url()")
579 (width length percentage "auto")
580 (x number)
581 (y number))
582 "Property value classes and their values.
583 The format is similar to that of `css-property-alist', except
584 that the CARs aren't actual CSS properties, but rather a name for
585 a class of values, and that symbols in the CDRs always refer to
586 other entries in this list, not to properties.
588 The following classes have been left out above because they
589 cannot be completed sensibly: `custom-ident',
590 `element-reference', `id', `identifier', `percentage', and
591 `string'.")
593 (defcustom css-electric-keys '(?\} ?\;) ;; '()
594 "Self inserting keys which should trigger re-indentation."
595 :version "22.2"
596 :type '(repeat character)
597 :options '((?\} ?\;))
598 :group 'css)
600 (defvar css-mode-syntax-table
601 (let ((st (make-syntax-table)))
602 ;; C-style comments.
603 (modify-syntax-entry ?/ ". 14" st)
604 (modify-syntax-entry ?* ". 23b" st)
605 ;; Strings.
606 (modify-syntax-entry ?\" "\"" st)
607 (modify-syntax-entry ?\' "\"" st)
608 ;; Blocks.
609 (modify-syntax-entry ?\{ "(}" st)
610 (modify-syntax-entry ?\} "){" st)
611 ;; Args in url(...) thingies and other "function calls".
612 (modify-syntax-entry ?\( "()" st)
613 (modify-syntax-entry ?\) ")(" st)
614 ;; To match attributes in selectors.
615 (modify-syntax-entry ?\[ "(]" st)
616 (modify-syntax-entry ?\] ")[" st)
617 ;; Special chars that sometimes come at the beginning of words.
618 (modify-syntax-entry ?@ "'" st)
619 ;; (modify-syntax-entry ?: "'" st)
620 (modify-syntax-entry ?# "'" st)
621 ;; Distinction between words and symbols.
622 (modify-syntax-entry ?- "_" st)
623 st))
625 (eval-and-compile
626 (defconst css--uri-re
627 (concat
628 "url\\((\\)[[:space:]]*\\(?:\\\\.\\|[^()[:space:]\n'\"]\\)+"
629 "[[:space:]]*\\()\\)")))
631 (defconst css-syntax-propertize-function
632 (syntax-propertize-rules
633 (css--uri-re (1 "|") (2 "|"))))
635 (defconst css-escapes-re
636 "\\\\\\(?:[^\000-\037\177]\\|[0-9a-fA-F]+[ \n\t\r\f]?\\)")
637 (defconst css-nmchar-re (concat "\\(?:[-[:alnum:]]\\|" css-escapes-re "\\)"))
638 (defconst css-nmstart-re (concat "\\(?:[[:alpha:]]\\|" css-escapes-re "\\)"))
639 (defconst css-ident-re ;; (concat css-nmstart-re css-nmchar-re "*")
640 ;; Apparently, "at rules" names can start with a dash, e.g. @-moz-keyframes.
641 (concat css-nmchar-re "+"))
642 (defconst css-proprietary-nmstart-re ;; Vendor-specific properties.
643 (concat "[-_]" (regexp-opt '("ms" "moz" "o" "khtml" "webkit")) "-"))
644 (defconst css-name-re (concat css-nmchar-re "+"))
646 (defconst scss--hash-re "#\\(?:{[$-_[:alnum:]]+}\\|[[:alnum:]]+\\)")
648 (defface css-selector '((t :inherit font-lock-function-name-face))
649 "Face to use for selectors."
650 :group 'css)
651 (defface css-property '((t :inherit font-lock-variable-name-face))
652 "Face to use for properties."
653 :group 'css)
654 (defface css-proprietary-property '((t :inherit (css-property italic)))
655 "Face to use for vendor-specific properties.")
657 (defun css--font-lock-keywords (&optional sassy)
658 `((,(concat "!\\s-*" (regexp-opt css--bang-ids))
659 (0 font-lock-builtin-face))
660 ;; Atrules keywords. IDs not in css-at-ids are valid (ignored).
661 ;; In fact the regexp should probably be
662 ;; (,(concat "\\(@" css-ident-re "\\)\\([ \t\n][^;{]*\\)[;{]")
663 ;; (1 font-lock-builtin-face))
664 ;; Since "An at-rule consists of everything up to and including the next
665 ;; semicolon (;) or the next block, whichever comes first."
666 (,(concat "@" css-ident-re) (0 font-lock-builtin-face))
667 ;; Variables.
668 (,(concat "--" css-ident-re) (0 font-lock-variable-name-face))
669 ;; Selectors.
670 ;; FIXME: attribute selectors don't work well because they may contain
671 ;; strings which have already been highlighted as f-l-string-face and
672 ;; thus prevent this highlighting from being applied (actually now that
673 ;; I use `keep' this should work better). But really the part of the
674 ;; selector between [...] should simply not be highlighted.
675 (,(concat
676 "^[ \t]*\\("
677 (if (not sassy)
678 ;; We don't allow / as first char, so as not to
679 ;; take a comment as the beginning of a selector.
680 "[^@/:{}() \t\n][^:{}()]+"
681 ;; Same as for non-sassy except we do want to allow { and }
682 ;; chars in selectors in the case of #{$foo}
683 ;; variable interpolation!
684 (concat "\\(?:" scss--hash-re
685 "\\|[^@/:{}() \t\n#]\\)"
686 "[^:{}()#]*\\(?:" scss--hash-re "[^:{}()#]*\\)*"))
687 ;; Even though pseudo-elements should be prefixed by ::, a
688 ;; single colon is accepted for backward compatibility.
689 "\\(?:\\(:" (regexp-opt (append css-pseudo-class-ids
690 css-pseudo-element-ids) t)
691 "\\|\\::" (regexp-opt css-pseudo-element-ids t) "\\)"
692 "\\(?:([^)]+)\\)?"
693 (if (not sassy)
694 "[^:{}()\n]*"
695 (concat "[^:{}()\n#]*\\(?:" scss--hash-re "[^:{}()\n#]*\\)*"))
696 "\\)*"
697 "\\)\\(?:\n[ \t]*\\)*{")
698 (1 'css-selector keep))
699 ;; In the above rule, we allow the open-brace to be on some subsequent
700 ;; line. This will only work if we properly mark the intervening text
701 ;; as being part of a multiline element (and even then, this only
702 ;; ensures proper refontification, but not proper discovery).
703 ("^[ \t]*{" (0 (save-excursion
704 (goto-char (match-beginning 0))
705 (skip-chars-backward " \n\t")
706 (put-text-property (point) (match-end 0)
707 'font-lock-multiline t)
708 ;; No face.
709 nil)))
710 ;; Properties. Again, we don't limit ourselves to css-property-ids.
711 (,(concat "\\(?:[{;]\\|^\\)[ \t]*\\("
712 "\\(?:\\(" css-proprietary-nmstart-re "\\)\\|"
713 css-nmstart-re "\\)" css-nmchar-re "*"
714 "\\)\\s-*:")
715 (1 (if (match-end 2) 'css-proprietary-property 'css-property)))
716 ;; Make sure the parens in a url(...) expression receive the
717 ;; default face. This is done because the parens may sometimes
718 ;; receive generic string delimiter syntax (see
719 ;; `css-syntax-propertize-function').
720 (,css--uri-re
721 (1 'default t) (2 'default t))))
723 (defvar css-font-lock-keywords (css--font-lock-keywords))
725 (defvar css-font-lock-defaults
726 '(css-font-lock-keywords nil t))
728 (defcustom css-indent-offset 4
729 "Basic size of one indentation step."
730 :version "22.2"
731 :type 'integer
732 :safe 'integerp)
734 (defconst css-smie-grammar
735 (smie-prec2->grammar
736 (smie-precs->prec2 '((assoc ";") (assoc ",") (left ":")))))
738 (defun css-smie--forward-token ()
739 (cond
740 ((and (eq (char-before) ?\})
741 (scss-smie--not-interpolation-p)
742 ;; FIXME: If the next char is not whitespace, what should we do?
743 (or (memq (char-after) '(?\s ?\t ?\n))
744 (looking-at comment-start-skip)))
745 (if (memq (char-after) '(?\s ?\t ?\n))
746 (forward-char 1) (forward-comment 1))
747 ";")
748 ((progn (forward-comment (point-max))
749 (looking-at "[;,:]"))
750 (forward-char 1) (match-string 0))
751 (t (smie-default-forward-token))))
753 (defun css-smie--backward-token ()
754 (let ((pos (point)))
755 (forward-comment (- (point)))
756 (cond
757 ;; FIXME: If the next char is not whitespace, what should we do?
758 ((and (eq (char-before) ?\}) (scss-smie--not-interpolation-p)
759 (> pos (point))) ";")
760 ((memq (char-before) '(?\; ?\, ?\:))
761 (forward-char -1) (string (char-after)))
762 (t (smie-default-backward-token)))))
764 (defun css-smie-rules (kind token)
765 (pcase (cons kind token)
766 (`(:elem . basic) css-indent-offset)
767 (`(:elem . arg) 0)
768 (`(:list-intro . ,(or `";" `"")) t) ;"" stands for BOB (bug#15467).
769 (`(:before . "{")
770 (when (or (smie-rule-hanging-p) (smie-rule-bolp))
771 (smie-backward-sexp ";")
772 (smie-indent-virtual)))
773 (`(:before . ,(or "{" "("))
774 (if (smie-rule-hanging-p) (smie-rule-parent 0)))))
776 ;;; Completion
778 (defun css--complete-property ()
779 "Complete property at point."
780 (save-excursion
781 (let ((pos (point)))
782 (skip-chars-backward "-[:alnum:]")
783 (let ((start (point)))
784 (skip-chars-backward " \t\r\n")
785 (when (memq (char-before) '(?\{ ?\;))
786 (list start pos css-property-ids))))))
788 (defun css--complete-bang-rule ()
789 "Complete bang-rule at point."
790 (save-excursion
791 (let ((pos (point)))
792 (skip-chars-backward "-[:alnum:]")
793 (when (eq (char-before) ?\!)
794 (list (point) pos css--bang-ids)))))
796 (defun css--complete-pseudo-element-or-class ()
797 "Complete pseudo-element or pseudo-class at point."
798 (save-excursion
799 (let ((pos (point)))
800 (skip-chars-backward "-[:alnum:]")
801 (when (eq (char-before) ?\:)
802 (list (point) pos
803 (if (eq (char-before (- (point) 1)) ?\:)
804 css-pseudo-element-ids
805 css-pseudo-class-ids))))))
807 (defun css--complete-at-rule ()
808 "Complete at-rule (statement beginning with `@') at point."
809 (save-excursion
810 (let ((pos (point)))
811 (skip-chars-backward "-[:alnum:]")
812 (when (eq (char-before) ?\@)
813 (list (point) pos css--at-ids)))))
815 (defvar css--property-value-cache
816 (make-hash-table :test 'equal :size (length css-property-alist))
817 "Cache of previously completed property values.")
819 (defun css--value-class-lookup (value-class)
820 "Return a list of value completion candidates for VALUE-CLASS.
821 Completion candidates are looked up in `css-value-class-alist' by
822 the symbol VALUE-CLASS."
823 (seq-uniq
824 (seq-mapcat
825 (lambda (value)
826 (if (stringp value)
827 (list value)
828 (css--value-class-lookup value)))
829 (cdr (assq value-class css-value-class-alist)))))
831 (defun css--property-values (property)
832 "Return a list of value completion candidates for PROPERTY.
833 Completion candidates are looked up in `css-property-alist' by
834 the string PROPERTY."
835 (or (gethash property css--property-value-cache)
836 (let ((values
837 (seq-uniq
838 (seq-mapcat
839 (lambda (value)
840 (if (stringp value)
841 (list value)
842 (or (css--value-class-lookup value)
843 (css--property-values (symbol-name value)))))
844 (cdr (assoc property css-property-alist))))))
845 (puthash property values css--property-value-cache))))
847 (defun css--complete-property-value ()
848 "Complete property value at point."
849 (let ((property
850 (save-excursion
851 (re-search-backward ":[^/]" (line-beginning-position) t)
852 (let ((property-end (point)))
853 (skip-chars-backward "-[:alnum:]")
854 (let ((property (buffer-substring (point) property-end)))
855 (car (member property css-property-ids)))))))
856 (when property
857 (let ((end (point)))
858 (save-excursion
859 (skip-chars-backward "[:graph:]")
860 (list (point) end
861 (append '("inherit" "initial" "unset")
862 (css--property-values property))))))))
864 (defvar css--html-tags (mapcar #'car html-tag-alist)
865 "List of HTML tags.
866 Used to provide completion of HTML tags in selectors.")
868 (defvar css--nested-selectors-allowed nil
869 "Non-nil if nested selectors are allowed in the current mode.")
870 (make-variable-buffer-local 'css--nested-selectors-allowed)
872 (defvar css-class-list-function #'ignore
873 "Called to provide completions of class names.
874 This can be bound by buffers that are able to suggest class name
875 completions, such as HTML mode buffers.")
877 (defvar css-id-list-function #'ignore
878 "Called to provide completions of IDs.
879 This can be bound by buffers that are able to suggest ID
880 completions, such as HTML mode buffers.")
882 (defun css--foreign-completions (extractor)
883 "Return a list of completions provided by other buffers.
884 EXTRACTOR should be the name of a function that may be defined in
885 one or more buffers. In each of the buffers where EXTRACTOR is
886 defined, EXTRACTOR is called and the results are accumulated into
887 a list of completions."
888 (delete-dups
889 (seq-mapcat
890 (lambda (buf)
891 (with-current-buffer buf
892 (funcall (symbol-value extractor))))
893 (buffer-list))))
895 (defun css--complete-selector ()
896 "Complete part of a CSS selector at point."
897 (when (or (= (nth 0 (syntax-ppss)) 0) css--nested-selectors-allowed)
898 (let ((end (point)))
899 (save-excursion
900 (skip-chars-backward "-[:alnum:]")
901 (let ((start-char (char-before)))
902 (list
903 (point) end
904 (completion-table-dynamic
905 (lambda (_)
906 (cond
907 ((eq start-char ?.)
908 (css--foreign-completions 'css-class-list-function))
909 ((eq start-char ?#)
910 (css--foreign-completions 'css-id-list-function))
911 (t css--html-tags))))))))))
913 (defun css-completion-at-point ()
914 "Complete current symbol at point.
915 Currently supports completion of CSS properties, property values,
916 pseudo-elements, pseudo-classes, at-rules, and bang-rules."
917 (or (css--complete-bang-rule)
918 (css--complete-property-value)
919 (css--complete-pseudo-element-or-class)
920 (css--complete-at-rule)
921 (seq-let (prop-beg prop-end prop-table) (css--complete-property)
922 (seq-let (sel-beg sel-end sel-table) (css--complete-selector)
923 (when (or prop-table sel-table)
924 `(,@(if prop-table
925 (list prop-beg prop-end)
926 (list sel-beg sel-end))
927 ,(completion-table-merge prop-table sel-table)))))))
929 ;;;###autoload
930 (define-derived-mode css-mode prog-mode "CSS"
931 "Major mode to edit Cascading Style Sheets."
932 (setq-local font-lock-defaults css-font-lock-defaults)
933 (setq-local comment-start "/*")
934 (setq-local comment-start-skip "/\\*+[ \t]*")
935 (setq-local comment-end "*/")
936 (setq-local comment-end-skip "[ \t]*\\*+/")
937 (setq-local syntax-propertize-function
938 css-syntax-propertize-function)
939 (setq-local fill-paragraph-function #'css-fill-paragraph)
940 (setq-local adaptive-fill-function #'css-adaptive-fill)
941 (setq-local add-log-current-defun-function #'css-current-defun-name)
942 (smie-setup css-smie-grammar #'css-smie-rules
943 :forward-token #'css-smie--forward-token
944 :backward-token #'css-smie--backward-token)
945 (setq-local electric-indent-chars
946 (append css-electric-keys electric-indent-chars))
947 (add-hook 'completion-at-point-functions
948 #'css-completion-at-point nil 'local))
950 (defvar comment-continue)
952 (defun css-fill-paragraph (&optional justify)
953 (save-excursion
954 ;; Fill succeeding comment when invoked right before a multi-line
955 ;; comment.
956 (when (save-excursion
957 (beginning-of-line)
958 (comment-search-forward (point-at-eol) t))
959 (goto-char (match-end 0)))
960 (let ((ppss (syntax-ppss))
961 (eol (line-end-position)))
962 (cond
963 ((and (nth 4 ppss)
964 (save-excursion
965 (goto-char (nth 8 ppss))
966 (forward-comment 1)
967 (prog1 (not (bolp))
968 (setq eol (point)))))
969 ;; Filling inside a comment whose comment-end marker is not \n.
970 ;; This code is meant to be generic, so that it works not only for
971 ;; css-mode but for all modes.
972 (save-restriction
973 (narrow-to-region (nth 8 ppss) eol)
974 (comment-normalize-vars) ;Will define comment-continue.
975 (let ((fill-paragraph-function nil)
976 (paragraph-separate
977 (if (and comment-continue
978 (string-match "[^ \t]" comment-continue))
979 (concat "\\(?:[ \t]*\\(?:"
980 (regexp-quote comment-continue) "\\|"
981 comment-start-skip "\\|"
982 comment-end-skip "\\)\\)?"
983 "\\(?:" paragraph-separate "\\)")
984 paragraph-separate))
985 (paragraph-start
986 (if (and comment-continue
987 (string-match "[^ \t]" comment-continue))
988 (concat "\\(?:[ \t]*" (regexp-quote comment-continue)
989 "\\)?\\(?:" paragraph-start "\\)")
990 paragraph-start)))
991 (fill-paragraph justify)
992 ;; Don't try filling again.
993 t)))
995 ((and (null (nth 8 ppss))
996 (or (nth 1 ppss)
997 (and (ignore-errors
998 (down-list 1)
999 (when (<= (point) eol)
1000 (setq ppss (syntax-ppss)))))))
1001 (goto-char (nth 1 ppss))
1002 (let ((end (save-excursion
1003 (ignore-errors (forward-sexp 1) (copy-marker (point) t)))))
1004 (when end
1005 (while (re-search-forward "[{;}]" end t)
1006 (cond
1007 ;; This is a false positive inside a string or comment.
1008 ((nth 8 (syntax-ppss)) nil)
1009 ;; This is a false positive when encountering an
1010 ;; interpolated variable (bug#19751).
1011 ((eq (char-before (- (point) 1)) ?#) nil)
1012 ((eq (char-before) ?\})
1013 (save-excursion
1014 (forward-char -1)
1015 (skip-chars-backward " \t")
1016 (when (and (not (bolp))
1017 (scss-smie--not-interpolation-p))
1018 (newline))))
1020 (while
1021 (progn
1022 (setq eol (line-end-position))
1023 (and (forward-comment 1)
1024 (> (point) eol)
1025 ;; A multi-line comment should be on its own line.
1026 (save-excursion (forward-comment -1)
1027 (when (< (point) eol)
1028 (newline)
1029 t)))))
1030 (if (< (point) eol) (newline)))))
1031 (goto-char (nth 1 ppss))
1032 (indent-region (line-beginning-position 2) end)
1033 ;; Don't use the default filling code.
1034 t)))))))
1036 (defun css-adaptive-fill ()
1037 (when (looking-at "[ \t]*/\\*[ \t]*")
1038 (let ((str (match-string 0)))
1039 (and (string-match "/\\*" str)
1040 (replace-match " *" t t str)))))
1042 (defun css-current-defun-name ()
1043 "Return the name of the CSS section at point, or nil."
1044 (save-excursion
1045 (let ((max (max (point-min) (- (point) 1600)))) ; approx 20 lines back
1046 (when (search-backward "{" max t)
1047 (skip-chars-backward " \t\r\n")
1048 (beginning-of-line)
1049 (if (looking-at "^[ \t]*\\([^{\r\n]*[^ {\t\r\n]\\)")
1050 (match-string-no-properties 1))))))
1052 ;;; SCSS mode
1054 (defvar scss-mode-syntax-table
1055 (let ((st (make-syntax-table css-mode-syntax-table)))
1056 (modify-syntax-entry ?/ ". 124" st)
1057 (modify-syntax-entry ?\n ">" st)
1058 ;; Variable names are prefixed by $.
1059 (modify-syntax-entry ?$ "'" st)
1060 st))
1062 (defun scss-font-lock-keywords ()
1063 (append `((,(concat "$" css-ident-re) (0 font-lock-variable-name-face)))
1064 (css--font-lock-keywords 'sassy)
1065 `((,(concat "@mixin[ \t]+\\(" css-ident-re "\\)[ \t]*(")
1066 (1 font-lock-function-name-face)))))
1068 (defun scss-smie--not-interpolation-p ()
1069 (save-excursion
1070 (forward-char -1)
1071 (or (zerop (skip-chars-backward "-[:alnum:]"))
1072 (not (looking-back "#{\\$" (- (point) 3))))))
1074 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.scss\\'" . scss-mode))
1075 ;;;###autoload
1076 (define-derived-mode scss-mode css-mode "SCSS"
1077 "Major mode to edit \"Sassy CSS\" files."
1078 (setq-local comment-start "// ")
1079 (setq-local comment-end "")
1080 (setq-local comment-continue " *")
1081 (setq-local comment-start-skip "/[*/]+[ \t]*")
1082 (setq-local comment-end-skip "[ \t]*\\(?:\n\\|\\*+/\\)")
1083 (setq-local css--at-ids (append css-at-ids scss-at-ids))
1084 (setq-local css--bang-ids (append css-bang-ids scss-bang-ids))
1085 (setq-local css--nested-selectors-allowed t)
1086 (setq-local font-lock-defaults
1087 (list (scss-font-lock-keywords) nil t)))
1089 (provide 'css-mode)
1090 ;;; css-mode.el ends here