branch: externals/colorful-mode
commit 13622ae3deeb911aecf827f51aa30679de15e773
Author: Elias Gabriel Perez <[email protected]>
Commit: Elias Gabriel Perez <[email protected]>

    Split 'colorful-add-color-names'.
    
    * colorful-colors.el (colorful--color-names-fn): Remove.
    (colorful-add-web-color-names):
    (colorful-add-emacs-color-names): New functions.
    (colorful-add-color-names): Mark as obsolete.
    * colorful-mode.el (colorful-extra-color-keyword-functions): Change
    default value.
    (colorful-mode-fontify-region): Simplify code.
    * README.org: Update doc.
---
 README.org           |  3 ++-
 colorful-colors.el   | 31 +++++++++++++++++++++++++------
 colorful-mode.el     | 29 +++++++++++++++--------------
 test/emacs-colors.el |  6 ++++--
 4 files changed, 46 insertions(+), 23 deletions(-)

diff --git a/README.org b/README.org
index a3fb8940eb..bec4468911 100644
--- a/README.org
+++ b/README.org
@@ -122,7 +122,8 @@ colorful provides extra functions out-the-box that enable 
additional
 highlighting:
 
 - =colorful-add-hex-colors=: Enable Hexadecimal colors highlighting.
-- =colorful-add-color-names=: Enable color names highlighting.
+- =colorful-add-emacs-color-names=: Enable Emacs specific color names 
highlighting. (Useful for theme creators)
+- =colorful-add-web-color-names=: Enable CSS/HTML color names highlighting.
 - =colorful-add-css-variables-colors=: Enable CSS user-defined color variables 
highlighting.
 - =colorful-add-rgb-colors=: Enable CSS RGB colors highlighting.
 - =colorful-add-oklab-oklch-colors=: Enable CSS OkLab and OkLch colors 
highlighting.
diff --git a/colorful-colors.el b/colorful-colors.el
index e91dede44a..20fd2c486f 100644
--- a/colorful-colors.el
+++ b/colorful-colors.el
@@ -255,16 +255,32 @@ Each entry should have the form (COLOR-NAME . 
HEXADECIMAL-COLOR)."
   :type 'alist
   :group 'colorful)
 
-(defun colorful--color-names-fn (color &rest _)
-  (if (color-defined-p color)
-      color
-    (cdr (assoc-string color colorful-html-colors-alist t))))
+(defun colorful-add-web-color-names ()
+  "Enable web (CSS/HTML) color name highlighting.
+This is intended to be used with `colorful-extra-color-keyword-functions'."
+  (cl-pushnew
+   `( :keywords ,(regexp-opt (mapcar #'car colorful-html-colors-alist) 
'symbols)
+      :type color-name
+      :case t ; color names are case insensitive.
+      :function (lambda (color &rest _)
+                  (cdr (assoc-string color colorful-html-colors-alist t))))
+   colorful-color-keywords))
+
+(defun colorful-add-emacs-color-names ()
+  "Enable Emacs color name highlighting.
+This is intended to be used with `colorful-extra-color-keyword-functions'."
+  (cl-pushnew
+   `( :keywords ,(regexp-opt (defined-colors) 'symbols)
+      :type color-name
+      :case t ; color names are case insensitive.
+      :function (lambda (color &rest _) color))
+   colorful-color-keywords))
 
 (defun colorful-add-color-names ()
   "Enable color name highlighting.
 This includes CSS and Emacs color names.
-
 This is intended to be used with `colorful-extra-color-keyword-functions'."
+  (declare (obsolete "Use `colorful-add-emacs-color-names' or 
`colorful-add-web-color-names' or both" "1.2.6"))
   (cl-pushnew
    `( :keywords ,(regexp-opt
                   (append
@@ -273,7 +289,10 @@ This is intended to be used with 
`colorful-extra-color-keyword-functions'."
                   'symbols)
       :type color-name
       :case t ; HTML/CSS/Emacs color names are case insensitive.
-      :function colorful--color-names-fn)
+      :function (lambda (color &rest _)
+                  (if (color-defined-p color)
+                      color
+                    (cdr (assoc-string color colorful-html-colors-alist t)))))
    colorful-color-keywords))
 
 
diff --git a/colorful-mode.el b/colorful-mode.el
index 474e87f38c..0353947db8 100644
--- a/colorful-mode.el
+++ b/colorful-mode.el
@@ -62,13 +62,13 @@ Changing the background or foreground color will have no 
effect."
 
 (defcustom colorful-extra-color-keyword-functions
   '(colorful-add-hex-colors
-    (emacs-lisp-mode . colorful-add-color-names)
+    (emacs-lisp-mode . colorful-add-emacs-color-names)
     ((html-mode css-mode) .
      (colorful-add-css-variables-colors
       colorful-add-rgb-colors
       colorful-add-hsl-colors
       colorful-add-oklab-oklch-colors
-      colorful-add-color-names))
+      colorful-add-web-color-names))
     (latex-mode . colorful-add-latex-colors))
   "List of functions to add color highlighting to `colorful-color-keywords'.
 It can be a cons cell specifying the mode (or a list of modes),
@@ -85,13 +85,20 @@ Or a simple list of functions for executing wherever 
colorful is active:
 
 Available functions are:
  - `colorful-add-hex-colors'
- - `colorful-add-color-names'
+ - `colorful-add-emacs-color-names'
+ - `colorful-add-web-color-names'
  - `colorful-add-css-variables-colors'
  - `colorful-add-rgb-colors'
  - `colorful-add-hsl-colors'
  - `colorful-add-oklab-oklch-colors'
  - `colorful-add-latex-colors'
- - `colorful-add-ansi-shell-colors'"
+ - `colorful-add-ansi-shell-colors'
+
+WARNING: The order of the functions specifies the priority they
+should have to be highlighted first, for example, if you enable
+`colorful-add-emacs-color-names' and `colorful-add-web-color-names' in
+the same major mode, depending on which one was called last, it will
+overwrite the highlighting of the previous call."
   :type '(repeat
           (choice (cons (choice :tag "Mode(s)" symbol (repeat symbol))
                         (choice :tag "Function(s)" (repeat function)
@@ -500,19 +507,13 @@ and positions to colorize."
            (ignore-case (plist-get el :case))
            (function (plist-get el :function)))
       (goto-char start)
-      (cond
-       ((stringp keywords)
+      (let ((case-fold-search ignore-case))
         (while (re-search-forward keywords end t)
-          (colorful--colorize type (match-string-no-properties match)
-                              (match-beginning match) (match-end match)
-                              function)))
-       (ignore-case
-        (let ((case-fold-search t))
-          (while (re-search-forward keywords end t)
+          ;; Check if it is not already highlighted
+          (unless (colorful--find-overlay (match-beginning match))
             (colorful--colorize type (match-string-no-properties match)
                                 (match-beginning match) (match-end match)
-                                function)))))))
-
+                                function))))))
   `(jit-lock-bounds ,start . ,end))
 
 
diff --git a/test/emacs-colors.el b/test/emacs-colors.el
index aff67ee14c..a7d5e1c952 100644
--- a/test/emacs-colors.el
+++ b/test/emacs-colors.el
@@ -36,10 +36,12 @@ green       "green"
 
 
 Chocolate   "Chocolate"
-Crimson     "Crimson"
 Red         "Red"
-Lime        "Lime"
 Green       "Green"
+;; These are not available as valid Emacs color names
+;; thus these should not be highlighted
+Crimson     "Crimson"
+Lime        "Lime"
 
 ;;; ANSI Colors
 "\e[0;41m test_red_color \e[0m"

Reply via email to