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