Merge branch 'master' into comment-cache
[emacs.git] / lisp / textmodes / css-mode.el
blob0c7d76f792477592743926be744a70aeab6a53a5
1 ;;; css-mode.el --- Major mode to edit CSS files -*- lexical-binding: t -*-
3 ;; Copyright (C) 2006-2017 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 ;; - filling code with auto-fill-mode
31 ;; - fix font-lock errors with multi-line selectors
33 ;;; Code:
35 (require 'eww)
36 (require 'seq)
37 (require 'sgml-mode)
38 (require 'smie)
39 (require 'subr-x)
41 (defgroup css nil
42 "Cascading Style Sheets (CSS) editing mode."
43 :group 'languages)
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.")
53 (defconst css-pseudo-element-ids
54 '("after" "before" "first-letter" "first-line")
55 "Identifiers for pseudo-elements.")
57 (defconst css-at-ids
58 '("charset" "font-face" "import" "keyframes" "media" "namespace"
59 "page" "supports")
60 "Identifiers that appear in the form @foo.")
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.")
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)
72 (defconst css-bang-ids
73 '("important")
74 "Identifiers that appear in the form !foo.")
76 (defconst scss-bang-ids
77 '("default" "global" "optional")
78 "Additional identifiers that appear in the form !foo in SCSS.")
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)
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.")
91 (defconst css-media-ids
92 '("all" "aural" "bitmap" "continuous" "grid" "paged" "static" "tactile"
93 "visual")
94 "Identifiers for types of media.")
96 (defconst css-property-alist
97 ;; CSS 2.1 properties (http://www.w3.org/TR/CSS21/propidx.html).
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)
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)
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)
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)
268 ;; CSS Color Module Level 3
269 ;; (http://www.w3.org/TR/css3-color/#property)
270 ("color" color)
271 ("opacity" alphavalue)
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)
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")
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)
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)
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")
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")
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")
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")
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)
433 ;; CSS Will Change Module Level 1
434 ;; (https://www.w3.org/TR/css-will-change-1/#property-index)
435 ("will-change" "auto" animateable-feature)
437 ;; Filter Effects Module Level 1
438 ;; (http://www.w3.org/TR/filter-effects/#property-index)
439 ("color-interpolation-filters" "auto" "sRGB" "linearRGB")
440 ("filter" "none" filter-function-list)
441 ("flood-color" color)
442 ("flood-opacity" number percentage)
443 ("lighting-color" color))
444 "Identifiers for properties and their possible values.
445 The CAR of each entry is the name of a property, while the CDR is
446 a list of possible values for that property. String values in
447 the CDRs represent literal values, while symbols represent one of
448 the value classes found in `css-value-class-alist'. If a symbol
449 is not found in `css-value-class-alist', it's interpreted as a
450 reference back to one of the properties in this list. Some
451 symbols, such as `number' or `identifier', don't produce any
452 further value candidates, since that list would be infinite.")
454 (defconst css-property-ids
455 (mapcar #'car css-property-alist)
456 "Identifiers for properties.")
458 (defconst css-value-class-alist
459 '((absolute-size
460 "xx-small" "x-small" "small" "medium" "large" "x-large"
461 "xx-large")
462 (alphavalue number)
463 (angle "calc()")
464 (animateable-feature "scroll-position" "contents" custom-ident)
465 (attachment "scroll" "fixed" "local")
466 (bg-image image "none")
467 (bg-layer bg-image position repeat-style attachment box)
468 (bg-size length percentage "auto" "cover" "contain")
469 (box "border-box" "padding-box" "content-box")
470 (color
471 "rgb()" "rgba()" "hsl()" "hsla()" named-color "transparent"
472 "currentColor")
473 (common-lig-values "common-ligatures" "no-common-ligatures")
474 (contextual-alt-values "contextual" "no-contextual")
475 (counter "counter()" "counters()")
476 (discretionary-lig-values
477 "discretionary-ligatures" "no-discretionary-ligatures")
478 (east-asian-variant-values
479 "jis78" "jis83" "jis90" "jis04" "simplified" "traditional")
480 (east-asian-width-values "full-width" "proportional-width")
481 (family-name "Courier" "Helvetica" "Times")
482 (feature-tag-value string integer "on" "off")
483 (filter-function
484 "blur()" "brightness()" "contrast()" "drop-shadow()"
485 "grayscale()" "hue-rotate()" "invert()" "opacity()" "sepia()"
486 "saturate()")
487 (filter-function-list filter-function uri)
488 (final-bg-layer
489 bg-image position repeat-style attachment box color)
490 (font-variant-css21 "normal" "small-caps")
491 (frequency "calc()")
492 (generic-family
493 "serif" "sans-serif" "cursive" "fantasy" "monospace")
494 (generic-voice "male" "female" "child")
495 (gradient
496 linear-gradient radial-gradient repeating-linear-gradient
497 repeating-radial-gradient)
498 (historical-lig-values
499 "historical-ligatures" "no-historical-ligatures")
500 (image uri image-list element-reference gradient)
501 (image-list "image()")
502 (integer "calc()")
503 (length "calc()" number)
504 (line-height "normal" number length percentage)
505 (line-style
506 "none" "hidden" "dotted" "dashed" "solid" "double" "groove"
507 "ridge" "inset" "outset")
508 (line-width length "thin" "medium" "thick")
509 (linear-gradient "linear-gradient()")
510 (margin-width "auto" length percentage)
511 (named-color
512 "aliceblue" "antiquewhite" "aqua" "aquamarine" "azure" "beige"
513 "bisque" "black" "blanchedalmond" "blue" "blueviolet" "brown"
514 "burlywood" "cadetblue" "chartreuse" "chocolate" "coral"
515 "cornflowerblue" "cornsilk" "crimson" "cyan" "darkblue"
516 "darkcyan" "darkgoldenrod" "darkgray" "darkgreen" "darkkhaki"
517 "darkmagenta" "darkolivegreen" "darkorange" "darkorchid"
518 "darkred" "darksalmon" "darkseagreen" "darkslateblue"
519 "darkslategray" "darkturquoise" "darkviolet" "deeppink"
520 "deepskyblue" "dimgray" "dodgerblue" "firebrick" "floralwhite"
521 "forestgreen" "fuchsia" "gainsboro" "ghostwhite" "gold"
522 "goldenrod" "gray" "green" "greenyellow" "honeydew" "hotpink"
523 "indianred" "indigo" "ivory" "khaki" "lavender" "lavenderblush"
524 "lawngreen" "lemonchiffon" "lightblue" "lightcoral" "lightcyan"
525 "lightgoldenrodyellow" "lightgray" "lightgreen" "lightpink"
526 "lightsalmon" "lightseagreen" "lightskyblue" "lightslategray"
527 "lightsteelblue" "lightyellow" "lime" "limegreen" "linen"
528 "magenta" "maroon" "mediumaquamarine" "mediumblue" "mediumorchid"
529 "mediumpurple" "mediumseagreen" "mediumslateblue"
530 "mediumspringgreen" "mediumturquoise" "mediumvioletred"
531 "midnightblue" "mintcream" "mistyrose" "moccasin" "navajowhite"
532 "navy" "oldlace" "olive" "olivedrab" "orange" "orangered"
533 "orchid" "palegoldenrod" "palegreen" "paleturquoise"
534 "palevioletred" "papayawhip" "peachpuff" "peru" "pink" "plum"
535 "powderblue" "purple" "rebeccapurple" "red" "rosybrown"
536 "royalblue" "saddlebrown" "salmon" "sandybrown" "seagreen"
537 "seashell" "sienna" "silver" "skyblue" "slateblue" "slategray"
538 "snow" "springgreen" "steelblue" "tan" "teal" "thistle" "tomato"
539 "turquoise" "violet" "wheat" "white" "whitesmoke" "yellow"
540 "yellowgreen")
541 (number "calc()")
542 (numeric-figure-values "lining-nums" "oldstyle-nums")
543 (numeric-fraction-values "diagonal-fractions" "stacked-fractions")
544 (numeric-spacing-values "proportional-nums" "tabular-nums")
545 (padding-width length percentage)
546 (position
547 "left" "center" "right" "top" "bottom" percentage length)
548 (radial-gradient "radial-gradient()")
549 (relative-size "larger" "smaller")
550 (repeat-style
551 "repeat-x" "repeat-y" "repeat" "space" "round" "no-repeat")
552 (repeating-linear-gradient "repeating-linear-gradient()")
553 (repeating-radial-gradient "repeating-radial-gradient()")
554 (shadow "inset" length color)
555 (shape "rect()")
556 (single-animation-direction
557 "normal" "reverse" "alternate" "alternate-reverse")
558 (single-animation-fill-mode "none" "forwards" "backwards" "both")
559 (single-animation-iteration-count "infinite" number)
560 (single-animation-name "none" identifier)
561 (single-animation-play-state "running" "paused")
562 (single-timing-function single-transition-timing-function)
563 (single-transition
564 "none" single-transition-property time
565 single-transition-timing-function)
566 (single-transition-property "all" identifier)
567 (single-transition-timing-function
568 "ease" "linear" "ease-in" "ease-out" "ease-in-out" "step-start"
569 "step-end" "steps()" "cubic-bezier()")
570 (specific-voice identifier)
571 (target-name string)
572 (time "calc()")
573 (transform-list
574 "matrix()" "translate()" "translateX()" "translateY()" "scale()"
575 "scaleX()" "scaleY()" "rotate()" "skew()" "skewX()" "skewY()"
576 "matrix3d()" "translate3d()" "translateZ()" "scale3d()"
577 "scaleZ()" "rotate3d()" "rotateX()" "rotateY()" "rotateZ()"
578 "perspective()")
579 (uri "url()")
580 (width length percentage "auto")
581 (x number)
582 (y number))
583 "Property value classes and their values.
584 The format is similar to that of `css-property-alist', except
585 that the CARs aren't actual CSS properties, but rather a name for
586 a class of values, and that symbols in the CDRs always refer to
587 other entries in this list, not to properties.
589 The following classes have been left out above because they
590 cannot be completed sensibly: `custom-ident',
591 `element-reference', `id', `identifier', `percentage', and
592 `string'.")
594 (defcustom css-electric-keys '(?\} ?\;) ;; '()
595 "Self inserting keys which should trigger re-indentation."
596 :version "22.2"
597 :type '(repeat character)
598 :options '((?\} ?\;))
599 :group 'css)
601 (defvar css-mode-syntax-table
602 (let ((st (make-syntax-table)))
603 ;; C-style comments.
604 (modify-syntax-entry ?/ ". 14" st)
605 (modify-syntax-entry ?* ". 23b" st)
606 ;; Strings.
607 (modify-syntax-entry ?\" "\"" st)
608 (modify-syntax-entry ?\' "\"" st)
609 ;; Blocks.
610 (modify-syntax-entry ?\{ "(}" st)
611 (modify-syntax-entry ?\} "){" st)
612 ;; Args in url(...) thingies and other "function calls".
613 (modify-syntax-entry ?\( "()" st)
614 (modify-syntax-entry ?\) ")(" st)
615 ;; To match attributes in selectors.
616 (modify-syntax-entry ?\[ "(]" st)
617 (modify-syntax-entry ?\] ")[" st)
618 ;; Special chars that sometimes come at the beginning of words.
619 (modify-syntax-entry ?@ "'" st)
620 ;; (modify-syntax-entry ?: "'" st)
621 (modify-syntax-entry ?# "'" st)
622 ;; Distinction between words and symbols.
623 (modify-syntax-entry ?- "_" st)
624 st))
626 (defvar css-mode-map
627 (let ((map (make-sparse-keymap)))
628 (define-key map [remap info-lookup-symbol] 'css-lookup-symbol)
629 map)
630 "Keymap used in `css-mode'.")
632 (eval-and-compile
633 (defconst css--uri-re
634 (concat
635 "url\\((\\)[[:space:]]*\\(?:\\\\.\\|[^()[:space:]\n'\"]\\)+"
636 "[[:space:]]*\\()\\)")))
638 (defconst css-syntax-propertize-function
639 (syntax-propertize-rules
640 (css--uri-re (1 "|") (2 "|"))))
642 (defconst css-escapes-re
643 "\\\\\\(?:[^\000-\037\177]\\|[0-9a-fA-F]+[ \n\t\r\f]?\\)")
644 (defconst css-nmchar-re (concat "\\(?:[-[:alnum:]]\\|" css-escapes-re "\\)"))
645 (defconst css-nmstart-re (concat "\\(?:[[:alpha:]]\\|" css-escapes-re "\\)"))
646 (defconst css-ident-re ;; (concat css-nmstart-re css-nmchar-re "*")
647 ;; Apparently, "at rules" names can start with a dash, e.g. @-moz-keyframes.
648 (concat css-nmchar-re "+"))
649 (defconst css-proprietary-nmstart-re ;; Vendor-specific properties.
650 (concat "[-_]" (regexp-opt '("ms" "moz" "o" "khtml" "webkit")) "-"))
651 (defconst css-name-re (concat css-nmchar-re "+"))
653 (defconst scss--hash-re "#\\(?:{[$-_[:alnum:]]+}\\|[[:alnum:]]+\\)")
655 (defface css-selector '((t :inherit font-lock-function-name-face))
656 "Face to use for selectors."
657 :group 'css)
658 (defface css-property '((t :inherit font-lock-variable-name-face))
659 "Face to use for properties."
660 :group 'css)
661 (defface css-proprietary-property '((t :inherit (css-property italic)))
662 "Face to use for vendor-specific properties.")
664 (defun css--font-lock-keywords (&optional sassy)
665 `((,(concat "!\\s-*" (regexp-opt css--bang-ids))
666 (0 font-lock-builtin-face))
667 ;; Atrules keywords. IDs not in css-at-ids are valid (ignored).
668 ;; In fact the regexp should probably be
669 ;; (,(concat "\\(@" css-ident-re "\\)\\([ \t\n][^;{]*\\)[;{]")
670 ;; (1 font-lock-builtin-face))
671 ;; Since "An at-rule consists of everything up to and including the next
672 ;; semicolon (;) or the next block, whichever comes first."
673 (,(concat "@" css-ident-re) (0 font-lock-builtin-face))
674 ;; Variables.
675 (,(concat "--" css-ident-re) (0 font-lock-variable-name-face))
676 ;; Selectors.
677 ;; Allow plain ":root" as a selector.
678 ("^[ \t]*\\(:root\\)\\(?:[\n \t]*\\)*{" (1 'css-selector keep))
679 ;; FIXME: attribute selectors don't work well because they may contain
680 ;; strings which have already been highlighted as f-l-string-face and
681 ;; thus prevent this highlighting from being applied (actually now that
682 ;; I use `keep' this should work better). But really the part of the
683 ;; selector between [...] should simply not be highlighted.
684 (,(concat
685 "^[ \t]*\\("
686 (if (not sassy)
687 ;; We don't allow / as first char, so as not to
688 ;; take a comment as the beginning of a selector.
689 "[^@/:{}() \t\n][^:{}()]+"
690 ;; Same as for non-sassy except we do want to allow { and }
691 ;; chars in selectors in the case of #{$foo}
692 ;; variable interpolation!
693 (concat "\\(?:" scss--hash-re
694 "\\|[^@/:{}() \t\n#]\\)"
695 "[^:{}()#]*\\(?:" scss--hash-re "[^:{}()#]*\\)*"))
696 ;; Even though pseudo-elements should be prefixed by ::, a
697 ;; single colon is accepted for backward compatibility.
698 "\\(?:\\(:" (regexp-opt (append css-pseudo-class-ids
699 css-pseudo-element-ids) t)
700 "\\|\\::" (regexp-opt css-pseudo-element-ids t) "\\)"
701 "\\(?:([^)]+)\\)?"
702 (if (not sassy)
703 "[^:{}()\n]*"
704 (concat "[^:{}()\n#]*\\(?:" scss--hash-re "[^:{}()\n#]*\\)*"))
705 "\\)*"
706 "\\)\\(?:\n[ \t]*\\)*{")
707 (1 'css-selector keep))
708 ;; In the above rule, we allow the open-brace to be on some subsequent
709 ;; line. This will only work if we properly mark the intervening text
710 ;; as being part of a multiline element (and even then, this only
711 ;; ensures proper refontification, but not proper discovery).
712 ("^[ \t]*{" (0 (save-excursion
713 (goto-char (match-beginning 0))
714 (skip-chars-backward " \n\t")
715 (put-text-property (point) (match-end 0)
716 'font-lock-multiline t)
717 ;; No face.
718 nil)))
719 ;; Properties. Again, we don't limit ourselves to css-property-ids.
720 (,(concat "\\(?:[{;]\\|^\\)[ \t]*\\("
721 "\\(?:\\(" css-proprietary-nmstart-re "\\)\\|"
722 css-nmstart-re "\\)" css-nmchar-re "*"
723 "\\)\\s-*:")
724 (1 (if (match-end 2) 'css-proprietary-property 'css-property)))
725 ;; Make sure the parens in a url(...) expression receive the
726 ;; default face. This is done because the parens may sometimes
727 ;; receive generic string delimiter syntax (see
728 ;; `css-syntax-propertize-function').
729 (,css--uri-re
730 (1 'default t) (2 'default t))))
732 (defvar css-font-lock-keywords (css--font-lock-keywords))
734 (defvar css-font-lock-defaults
735 '(css-font-lock-keywords nil t))
737 (defcustom css-indent-offset 4
738 "Basic size of one indentation step."
739 :version "22.2"
740 :type 'integer
741 :safe 'integerp)
743 (defconst css-smie-grammar
744 (smie-prec2->grammar
745 (smie-precs->prec2
746 '((assoc ";")
747 ;; Colons that belong to a CSS property. These get a higher
748 ;; precedence than other colons, such as colons in selectors,
749 ;; which are represented by a plain ":" token.
750 (left ":-property")
751 (assoc ",")
752 (assoc ":")))))
754 (defun css--colon-inside-selector-p ()
755 "Return t if point looks to be inside a CSS selector.
756 This function is intended to be good enough to help SMIE during
757 tokenization, but should not be regarded as a reliable function
758 for determining whether point is within a selector."
759 (save-excursion
760 (re-search-forward "[{};)]" nil t)
761 (eq (char-before) ?\{)))
763 (defun css--colon-inside-funcall ()
764 "Return t if point is inside a function call."
765 (when-let (opening-paren-pos (nth 1 (syntax-ppss)))
766 (save-excursion
767 (goto-char opening-paren-pos)
768 (eq (char-after) ?\())))
770 (defun css-smie--forward-token ()
771 (cond
772 ((and (eq (char-before) ?\})
773 (scss-smie--not-interpolation-p)
774 ;; FIXME: If the next char is not whitespace, what should we do?
775 (or (memq (char-after) '(?\s ?\t ?\n))
776 (looking-at comment-start-skip)))
777 (if (memq (char-after) '(?\s ?\t ?\n))
778 (forward-char 1) (forward-comment 1))
779 ";")
780 ((progn (forward-comment (point-max))
781 (looking-at "[;,:]"))
782 (forward-char 1)
783 (if (equal (match-string 0) ":")
784 (if (or (css--colon-inside-selector-p)
785 (css--colon-inside-funcall))
787 ":-property")
788 (match-string 0)))
789 (t (smie-default-forward-token))))
791 (defun css-smie--backward-token ()
792 (let ((pos (point)))
793 (forward-comment (- (point)))
794 (cond
795 ;; FIXME: If the next char is not whitespace, what should we do?
796 ((and (eq (char-before) ?\}) (scss-smie--not-interpolation-p)
797 (> pos (point))) ";")
798 ((memq (char-before) '(?\; ?\, ?\:))
799 (forward-char -1)
800 (if (eq (char-after) ?\:)
801 (if (or (css--colon-inside-selector-p)
802 (css--colon-inside-funcall))
804 ":-property")
805 (string (char-after))))
806 (t (smie-default-backward-token)))))
808 (defun css-smie-rules (kind token)
809 (pcase (cons kind token)
810 (`(:elem . basic) css-indent-offset)
811 (`(:elem . arg) 0)
812 (`(:list-intro . ,(or `";" `"")) t) ;"" stands for BOB (bug#15467).
813 (`(:before . "{")
814 (when (or (smie-rule-hanging-p) (smie-rule-bolp))
815 (smie-backward-sexp ";")
816 (smie-indent-virtual)))
817 (`(:before . ,(or "{" "("))
818 (if (smie-rule-hanging-p) (smie-rule-parent 0)))))
820 ;;; Completion
822 (defun css--complete-property ()
823 "Complete property at point."
824 (save-excursion
825 (let ((pos (point)))
826 (skip-chars-backward "-[:alnum:]")
827 (let ((start (point)))
828 (skip-chars-backward " \t\r\n")
829 (when (memq (char-before) '(?\{ ?\;))
830 (list start pos css-property-ids))))))
832 (defun css--complete-bang-rule ()
833 "Complete bang-rule at point."
834 (save-excursion
835 (let ((pos (point)))
836 (skip-chars-backward "-[:alnum:]")
837 (when (eq (char-before) ?\!)
838 (list (point) pos css--bang-ids)))))
840 (defun css--complete-pseudo-element-or-class ()
841 "Complete pseudo-element or pseudo-class at point."
842 (save-excursion
843 (let ((pos (point)))
844 (skip-chars-backward "-[:alnum:]")
845 (when (eq (char-before) ?\:)
846 (list (point) pos
847 (if (eq (char-before (- (point) 1)) ?\:)
848 css-pseudo-element-ids
849 css-pseudo-class-ids))))))
851 (defun css--complete-at-rule ()
852 "Complete at-rule (statement beginning with `@') at point."
853 (save-excursion
854 (let ((pos (point)))
855 (skip-chars-backward "-[:alnum:]")
856 (when (eq (char-before) ?\@)
857 (list (point) pos css--at-ids)))))
859 (defvar css--property-value-cache
860 (make-hash-table :test 'equal :size (length css-property-alist))
861 "Cache of previously completed property values.")
863 (defun css--value-class-lookup (value-class)
864 "Return a list of value completion candidates for VALUE-CLASS.
865 Completion candidates are looked up in `css-value-class-alist' by
866 the symbol VALUE-CLASS."
867 (seq-uniq
868 (seq-mapcat
869 (lambda (value)
870 (if (stringp value)
871 (list value)
872 (css--value-class-lookup value)))
873 (cdr (assq value-class css-value-class-alist)))))
875 (defun css--property-values (property)
876 "Return a list of value completion candidates for PROPERTY.
877 Completion candidates are looked up in `css-property-alist' by
878 the string PROPERTY."
879 (or (gethash property css--property-value-cache)
880 (let ((values
881 (seq-uniq
882 (seq-mapcat
883 (lambda (value)
884 (if (stringp value)
885 (list value)
886 (or (css--value-class-lookup value)
887 (css--property-values (symbol-name value)))))
888 (cdr (assoc property css-property-alist))))))
889 (puthash property values css--property-value-cache))))
891 (defun css--complete-property-value ()
892 "Complete property value at point."
893 (let ((property
894 (save-excursion
895 (re-search-backward ":[^/]" (line-beginning-position) t)
896 (let ((property-end (point)))
897 (skip-chars-backward "-[:alnum:]")
898 (let ((property (buffer-substring (point) property-end)))
899 (car (member property css-property-ids)))))))
900 (when property
901 (let ((end (point)))
902 (save-excursion
903 (skip-chars-backward "[:graph:]")
904 (list (point) end
905 (append '("inherit" "initial" "unset")
906 (css--property-values property))))))))
908 (defvar css--html-tags (mapcar #'car html-tag-alist)
909 "List of HTML tags.
910 Used to provide completion of HTML tags in selectors.")
912 (defvar css--nested-selectors-allowed nil
913 "Non-nil if nested selectors are allowed in the current mode.")
914 (make-variable-buffer-local 'css--nested-selectors-allowed)
916 (defvar css-class-list-function #'ignore
917 "Called to provide completions of class names.
918 This can be bound by buffers that are able to suggest class name
919 completions, such as HTML mode buffers.")
921 (defvar css-id-list-function #'ignore
922 "Called to provide completions of IDs.
923 This can be bound by buffers that are able to suggest ID
924 completions, such as HTML mode buffers.")
926 (defun css--foreign-completions (extractor)
927 "Return a list of completions provided by other buffers.
928 EXTRACTOR should be the name of a function that may be defined in
929 one or more buffers. In each of the buffers where EXTRACTOR is
930 defined, EXTRACTOR is called and the results are accumulated into
931 a list of completions."
932 (delete-dups
933 (seq-mapcat
934 (lambda (buf)
935 (with-current-buffer buf
936 (funcall (symbol-value extractor))))
937 (buffer-list))))
939 (defun css--complete-selector ()
940 "Complete part of a CSS selector at point."
941 (when (or (= (nth 0 (syntax-ppss)) 0) css--nested-selectors-allowed)
942 (let ((end (point)))
943 (save-excursion
944 (skip-chars-backward "-[:alnum:]")
945 (let ((start-char (char-before)))
946 (list
947 (point) end
948 (completion-table-dynamic
949 (lambda (_)
950 (cond
951 ((eq start-char ?.)
952 (css--foreign-completions 'css-class-list-function))
953 ((eq start-char ?#)
954 (css--foreign-completions 'css-id-list-function))
955 (t css--html-tags))))))))))
957 (defun css-completion-at-point ()
958 "Complete current symbol at point.
959 Currently supports completion of CSS properties, property values,
960 pseudo-elements, pseudo-classes, at-rules, and bang-rules."
961 (or (css--complete-bang-rule)
962 (css--complete-property-value)
963 (css--complete-pseudo-element-or-class)
964 (css--complete-at-rule)
965 (seq-let (prop-beg prop-end prop-table) (css--complete-property)
966 (seq-let (sel-beg sel-end sel-table) (css--complete-selector)
967 (when (or prop-table sel-table)
968 `(,@(if prop-table
969 (list prop-beg prop-end)
970 (list sel-beg sel-end))
971 ,(completion-table-merge prop-table sel-table)))))))
973 ;;;###autoload
974 (define-derived-mode css-mode prog-mode "CSS"
975 "Major mode to edit Cascading Style Sheets."
976 (setq-local font-lock-defaults css-font-lock-defaults)
977 (setq-local comment-start "/*")
978 (setq-local comment-start-skip "/\\*+[ \t]*")
979 (setq-local comment-end "*/")
980 (setq-local comment-end-skip "[ \t]*\\*+/")
981 (setq-local syntax-propertize-function
982 css-syntax-propertize-function)
983 (setq-local fill-paragraph-function #'css-fill-paragraph)
984 (setq-local adaptive-fill-function #'css-adaptive-fill)
985 (setq-local add-log-current-defun-function #'css-current-defun-name)
986 (smie-setup css-smie-grammar #'css-smie-rules
987 :forward-token #'css-smie--forward-token
988 :backward-token #'css-smie--backward-token)
989 (setq-local electric-indent-chars
990 (append css-electric-keys electric-indent-chars))
991 (add-hook 'completion-at-point-functions
992 #'css-completion-at-point nil 'local))
994 (defvar comment-continue)
996 (defun css-fill-paragraph (&optional justify)
997 (save-excursion
998 ;; Fill succeeding comment when invoked right before a multi-line
999 ;; comment.
1000 (when (save-excursion
1001 (beginning-of-line)
1002 (comment-search-forward (point-at-eol) t))
1003 (goto-char (match-end 0)))
1004 (let ((ppss (syntax-ppss))
1005 (eol (line-end-position)))
1006 (cond
1007 ((and (nth 4 ppss)
1008 (save-excursion
1009 (goto-char (nth 8 ppss))
1010 (forward-comment 1)
1011 (prog1 (not (bolp))
1012 (setq eol (point)))))
1013 ;; Filling inside a comment whose comment-end marker is not \n.
1014 ;; This code is meant to be generic, so that it works not only for
1015 ;; css-mode but for all modes.
1016 (save-restriction
1017 (narrow-to-region (nth 8 ppss) eol)
1018 (comment-normalize-vars) ;Will define comment-continue.
1019 (let ((fill-paragraph-function nil)
1020 (paragraph-separate
1021 (if (and comment-continue
1022 (string-match "[^ \t]" comment-continue))
1023 (concat "\\(?:[ \t]*\\(?:"
1024 (regexp-quote comment-continue) "\\|"
1025 comment-start-skip "\\|"
1026 comment-end-skip "\\)\\)?"
1027 "\\(?:" paragraph-separate "\\)")
1028 paragraph-separate))
1029 (paragraph-start
1030 (if (and comment-continue
1031 (string-match "[^ \t]" comment-continue))
1032 (concat "\\(?:[ \t]*" (regexp-quote comment-continue)
1033 "\\)?\\(?:" paragraph-start "\\)")
1034 paragraph-start)))
1035 (fill-paragraph justify)
1036 ;; Don't try filling again.
1037 t)))
1039 ((and (null (nth 8 ppss))
1040 (or (nth 1 ppss)
1041 (and (ignore-errors
1042 (down-list 1)
1043 (when (<= (point) eol)
1044 (setq ppss (syntax-ppss)))))))
1045 (goto-char (nth 1 ppss))
1046 (let ((end (save-excursion
1047 (ignore-errors (forward-sexp 1) (copy-marker (point) t)))))
1048 (when end
1049 (while (re-search-forward "[{;}]" end t)
1050 (cond
1051 ;; This is a false positive inside a string or comment.
1052 ((nth 8 (syntax-ppss)) nil)
1053 ;; This is a false positive when encountering an
1054 ;; interpolated variable (bug#19751).
1055 ((eq (char-before (- (point) 1)) ?#) nil)
1056 ((eq (char-before) ?\})
1057 (save-excursion
1058 (forward-char -1)
1059 (skip-chars-backward " \t")
1060 (when (and (not (bolp))
1061 (scss-smie--not-interpolation-p))
1062 (newline))))
1064 (while
1065 (progn
1066 (setq eol (line-end-position))
1067 (and (forward-comment 1)
1068 (> (point) eol)
1069 ;; A multi-line comment should be on its own line.
1070 (save-excursion (forward-comment -1)
1071 (when (< (point) eol)
1072 (newline)
1073 t)))))
1074 (if (< (point) eol) (newline)))))
1075 (goto-char (nth 1 ppss))
1076 (indent-region (line-beginning-position 2) end)
1077 ;; Don't use the default filling code.
1078 t)))))))
1080 (defun css-adaptive-fill ()
1081 (when (looking-at "[ \t]*/\\*[ \t]*")
1082 (let ((str (match-string 0)))
1083 (and (string-match "/\\*" str)
1084 (replace-match " *" t t str)))))
1086 (defun css-current-defun-name ()
1087 "Return the name of the CSS section at point, or nil."
1088 (save-excursion
1089 (let ((max (max (point-min) (- (point) 1600)))) ; approx 20 lines back
1090 (when (search-backward "{" max t)
1091 (skip-chars-backward " \t\r\n")
1092 (beginning-of-line)
1093 (if (looking-at "^[ \t]*\\([^{\r\n]*[^ {\t\r\n]\\)")
1094 (match-string-no-properties 1))))))
1096 ;;; SCSS mode
1098 (defvar scss-mode-syntax-table
1099 (let ((st (make-syntax-table css-mode-syntax-table)))
1100 (modify-syntax-entry ?/ ". 124" st)
1101 (modify-syntax-entry ?\n ">" st)
1102 ;; Variable names are prefixed by $.
1103 (modify-syntax-entry ?$ "'" st)
1104 st))
1106 (defun scss-font-lock-keywords ()
1107 (append `((,(concat "$" css-ident-re) (0 font-lock-variable-name-face)))
1108 (css--font-lock-keywords 'sassy)
1109 `((,(concat "@mixin[ \t]+\\(" css-ident-re "\\)[ \t]*(")
1110 (1 font-lock-function-name-face)))))
1112 (defun scss-smie--not-interpolation-p ()
1113 (save-excursion
1114 (forward-char -1)
1115 (or (zerop (skip-chars-backward "-[:alnum:]"))
1116 (not (looking-back "#{\\$" (- (point) 3))))))
1118 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.scss\\'" . scss-mode))
1119 ;;;###autoload
1120 (define-derived-mode scss-mode css-mode "SCSS"
1121 "Major mode to edit \"Sassy CSS\" files."
1122 (setq-local comment-start "// ")
1123 (setq-local comment-end "")
1124 (setq-local comment-continue " *")
1125 (setq-local comment-start-skip "/[*/]+[ \t]*")
1126 (setq-local comment-end-skip "[ \t]*\\(?:\n\\|\\*+/\\)")
1127 (setq-local css--at-ids (append css-at-ids scss-at-ids))
1128 (setq-local css--bang-ids (append css-bang-ids scss-bang-ids))
1129 (setq-local css--nested-selectors-allowed t)
1130 (setq-local font-lock-defaults
1131 (list (scss-font-lock-keywords) nil t)))
1135 (defvar css--mdn-lookup-history nil)
1137 (defcustom css-lookup-url-format
1138 "https://developer.mozilla.org/en-US/docs/Web/CSS/%s?raw&macros"
1139 "Format for a URL where CSS documentation can be found.
1140 The format should include a single \"%s\" substitution.
1141 The name of the CSS property, @-id, pseudo-class, or pseudo-element
1142 to look up will be substituted there."
1143 :version "26.1"
1144 :type 'string
1145 :group 'css)
1147 (defun css--mdn-after-render ()
1148 (setf header-line-format nil)
1149 (goto-char (point-min))
1150 (let ((window (get-buffer-window (current-buffer) 'visible)))
1151 (when window
1152 (when (re-search-forward "^Summary" nil 'move)
1153 (beginning-of-line)
1154 (set-window-start window (point))))))
1156 (defconst css--mdn-symbol-regexp
1157 (concat "\\("
1158 ;; @-ids.
1159 "\\(@" (regexp-opt css-at-ids) "\\)"
1160 "\\|"
1161 ;; ;; Known properties.
1162 (regexp-opt css-property-ids t)
1163 "\\|"
1164 ;; Pseudo-classes.
1165 "\\(:" (regexp-opt css-pseudo-class-ids) "\\)"
1166 "\\|"
1167 ;; Pseudo-elements with either one or two ":"s.
1168 "\\(::?" (regexp-opt css-pseudo-element-ids) "\\)"
1169 "\\)")
1170 "Regular expression to match the CSS symbol at point.")
1172 (defconst css--mdn-property-regexp
1173 (concat "\\_<" (regexp-opt css-property-ids t) "\\s-*\\(?:\\=\\|:\\)")
1174 "Regular expression to match a CSS property.")
1176 (defconst css--mdn-completion-list
1177 (nconc
1178 ;; @-ids.
1179 (mapcar (lambda (atrule) (concat "@" atrule)) css-at-ids)
1180 ;; Pseudo-classes.
1181 (mapcar (lambda (class) (concat ":" class)) css-pseudo-class-ids)
1182 ;; Pseudo-elements with either one or two ":"s.
1183 (mapcar (lambda (elt) (concat ":" elt)) css-pseudo-element-ids)
1184 (mapcar (lambda (elt) (concat "::" elt)) css-pseudo-element-ids)
1185 ;; Properties.
1186 css-property-ids)
1187 "List of all symbols available for lookup via MDN.")
1189 (defun css--mdn-find-symbol ()
1190 "A helper for `css-lookup-symbol' that finds the symbol at point.
1191 Returns the symbol, a string, or nil if none found."
1192 (save-excursion
1193 ;; Skip backward over a word first.
1194 (skip-chars-backward "-[:alnum:] \t")
1195 ;; Now skip ":" or "@" to see if it's a pseudo-element or at-id.
1196 (skip-chars-backward "@:")
1197 (if (looking-at css--mdn-symbol-regexp)
1198 (match-string-no-properties 0)
1199 (let ((bound (save-excursion
1200 (beginning-of-line)
1201 (point))))
1202 (when (re-search-backward css--mdn-property-regexp bound t)
1203 (match-string-no-properties 1))))))
1205 ;;;###autoload
1206 (defun css-lookup-symbol (symbol)
1207 "Display the CSS documentation for SYMBOL, as found on MDN.
1208 When this command is used interactively, it picks a default
1209 symbol based on the CSS text before point -- either an @-keyword,
1210 a property name, a pseudo-class, or a pseudo-element, depending
1211 on what is seen near point."
1212 (interactive
1213 (list
1214 (let* ((sym (css--mdn-find-symbol))
1215 (enable-recursive-minibuffers t)
1216 (value (completing-read
1217 (if sym
1218 (format "Describe CSS symbol (default %s): " sym)
1219 "Describe CSS symbol: ")
1220 css--mdn-completion-list nil nil nil
1221 'css--mdn-lookup-history sym)))
1222 (if (equal value "") sym value))))
1223 (when symbol
1224 ;; If we see a single-colon pseudo-element like ":after", turn it
1225 ;; into "::after".
1226 (when (and (eq (aref symbol 0) ?:)
1227 (member (substring symbol 1) css-pseudo-element-ids))
1228 (setq symbol (concat ":" symbol)))
1229 (let ((url (format css-lookup-url-format symbol))
1230 (buffer (get-buffer-create "*MDN CSS*")))
1231 (save-selected-window
1232 ;; Make sure to display the buffer before calling `eww', as
1233 ;; that calls `pop-to-buffer-same-window'.
1234 (switch-to-buffer-other-window buffer)
1235 (with-current-buffer buffer
1236 (eww-mode)
1237 (add-hook 'eww-after-render-hook #'css--mdn-after-render nil t)
1238 (eww url))))))
1240 (provide 'css-mode)
1241 ;;; css-mode.el ends here