Subject: bug#25525: 25.1.90; add color highlighting to css mode

From: Andreas Schwab <[email-masked]>
Date: Fri, 21 Apr 2017 11:33:42 +0200

On Apr 20 2017, Tom Tromey <[email-masked]> wrote:

> +(defun css--named-color (str)
> +  "Check whether STR, seen at point, is CSS named color.
> +Returns STR if it is a valid color.  Special care is taken
> +to exclude some SCSS contructs."
> +  (when-let ((color (assoc str css--color-map)))
> +    (save-excursion
> +      ;; We still have the match from the caller of
> +      ;; css--compute-color.
> +      (goto-char (match-beginning 0))
> +      (forward-comment (- (point)))
> +      (skip-chars-backward "@[:alpha:]")
> +      (unless (looking-at-p "@\\(mixin\\|include\\)")
> +        (cdr color)))))
> +
> +(defun css--compute-color ()
> +  "Return the CSS color at point.
> +Point should be just after the start of a CSS color, as recognized
> +by `css--colors-regexp'.  This function will either return the color,
> +as a hex RGB string; or `nil' if no color could be recognized.  When
> +this function returns, point will be at the end of the recognized
> +color."
> +  (let ((match (downcase (match-string 0))))
> +    (cond
> +     ((eq (aref match 0) ?#)
> +      (css--hex-color match))
> +     ((member match '("rgb(" "rgba("))
> +      (css--rgb-color))
> +     ((member match '("hsl(" "hsla("))
> +      (css--hsl-color))
> +     ;; Evaluate to the color if the name is found.
> +     ((css--named-color match)))))

It would be better to pass the match string as an argument instead of
depending on the caller to establish a match.

Andreas.

-- 
Andreas Schwab, [email-masked]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."