summaryrefslogtreecommitdiff
path: root/lisp/textmodes/css-mode.el
diff options
context:
space:
mode:
authorSimen Heggestøyl <simenheg@gmail.com>2018-01-28 15:35:46 +0100
committerSimen Heggestøyl <simenheg@gmail.com>2018-01-28 15:38:27 +0100
commit69a30e8b87fac5888daa26c63663351570e3d533 (patch)
treef7c4b0a665556c86dcaa4e744f1693bda393375c /lisp/textmodes/css-mode.el
parent97defdfc36d9a83a3081c5f76e249f908645f7ec (diff)
downloademacs-69a30e8b87fac5888daa26c63663351570e3d533.tar.gz
Shorten CSS hex colors when possible
* lisp/textmodes/css-mode.el (css--format-hex): New function for shortening CSS hex colors when possible. (css--named-color-to-hex, css--rgb-to-named-color-or-hex): Use it. * test/lisp/textmodes/css-mode-tests.el (css-test-format-hex): New tests for 'css--format-hex'. (css-test-named-color-to-hex, css-test-cycle-color-format): Adjust for the changes to 'css--named-color-to-hex' and 'css--rgb-to-named-color-or-hex'.
Diffstat (limited to 'lisp/textmodes/css-mode.el')
-rw-r--r--lisp/textmodes/css-mode.el15
1 files changed, 13 insertions, 2 deletions
diff --git a/lisp/textmodes/css-mode.el b/lisp/textmodes/css-mode.el
index 135c0d5f928..55c21f8acb0 100644
--- a/lisp/textmodes/css-mode.el
+++ b/lisp/textmodes/css-mode.el
@@ -1413,6 +1413,15 @@ should not be mixed with those in color.el."
(apply-partially #'make-list (if six-digits 2 4))
(seq-partition (seq-drop hex 1) (if six-digits 2 1)))))))
+(defun css--format-hex (hex)
+ "Format a CSS hex color by shortening it if possible."
+ (let ((parts (seq-partition (seq-drop hex 1) 2)))
+ (if (and (>= (length hex) 6)
+ (seq-every-p (lambda (p) (eq (elt p 0) (elt p 1))) parts))
+ (apply #'string
+ (cons ?# (mapcar (lambda (p) (elt p 0)) parts)))
+ hex)))
+
(defun css--named-color-to-hex ()
"Convert named CSS color at point to hex format.
Return non-nil if a conversion was made.
@@ -1426,7 +1435,7 @@ should not be mixed with those in color.el."
(when (member (word-at-point) (mapcar #'car css--color-map))
(looking-at css--colors-regexp)
(let ((color (css--compute-color (point) (match-string 0))))
- (replace-match color))
+ (replace-match (css--format-hex color)))
t)))
(defun css--format-rgba-alpha (alpha)
@@ -1490,7 +1499,9 @@ should not be mixed with those in color.el."
(kill-sexp)
(let ((named-color (seq-find (lambda (x) (equal (cdr x) color))
css--color-map)))
- (insert (if named-color (car named-color) color)))
+ (insert (if named-color
+ (car named-color)
+ (css--format-hex color))))
t)))))
(defun css-cycle-color-format ()