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

    Add 'colorful-add-ansi-shell-colors'
    
    * README.org (Enabling colors to specifics major-modes): Update 
documentation.
    * colorful-colors.el (colorful-add-color-names): Remove redundant ':match'.
    (colorful--ansi-fn):
    (colorful-add-ansi-shell-colors): New functions.
    * colorful-mode.el (colorful-extra-color-keyword-functions):
    (colorful-color-keywords): Update docstring.
    (colorful--conversion-choices): New internal var.
    (colorful-convert-and-change-color):
    (colorful--prompt-converter): Remove 'choices' let-var.
    (colorful--converter): Remove recursion.
    (colorful--colorize): Add 'save-match-data'.
    * test/emacs-colors.el: Add new tests.
---
 README.org           | 15 +++++++-------
 colorful-colors.el   | 34 ++++++++++++++++++++++++++++---
 colorful-mode.el     | 56 ++++++++++++++++++++++++++--------------------------
 test/css.css         | 12 +++++------
 test/emacs-colors.el |  4 ++++
 5 files changed, 77 insertions(+), 44 deletions(-)

diff --git a/README.org b/README.org
index 3ea95bb88a..a3fb8940eb 100644
--- a/README.org
+++ b/README.org
@@ -121,13 +121,14 @@ If you want also use hsl and rgb together you can use this
 colorful provides extra functions out-the-box that enable additional
 highlighting:
 
-- =colorful-add-hex-colors=: Add Hexadecimal colors highlighting.
-- =colorful-add-color-names=: Add color names highlighting.
-- =colorful-add-css-variables-colors=: Add CSS user-defined color variables 
highlighting.
-- =colorful-add-rgb-colors=: Add CSS RGB colors highlighting.
-- =colorful-add-oklab-oklch-colors=: Add CSS OkLab and OkLch colors 
highlighting.
-- =colorful-add-hsl-colors=: Add CSS HSL colors highlighting.
-- =colorful-add-latex-colors=: Add LaTeX rgb/RGB/HTML/Grey colors highlighting.
+- =colorful-add-hex-colors=: Enable Hexadecimal colors highlighting.
+- =colorful-add-color-names=: Enable 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.
+- =colorful-add-hsl-colors=: Enable CSS HSL colors highlighting.
+- =colorful-add-latex-colors=: Enable LaTeX rgb/RGB/HTML/Grey colors 
highlighting.
+- =colorful-add-ansi-shell-colors=: Enable ANSI shell color highlighting.
 
 (See: =colorful-extra-color-keyword-functions= for more details.)
 
diff --git a/colorful-colors.el b/colorful-colors.el
index aae0524fa4..250739bcb1 100644
--- a/colorful-colors.el
+++ b/colorful-colors.el
@@ -30,7 +30,8 @@
 ;; :keywords must be a regexp string which contains the keywords
 ;; to highlight
 ;;
-;; :type (optional) is a symbol which specifies the color type.
+;; :type (optional) is a symbol which specifies the color type (only
+;; used to convert between colors).
 ;;
 ;; :match (optional) must be a number which specifies the match to
 ;; use, if not set, it will use 0 instead.
@@ -63,6 +64,8 @@
 (declare-function color-hsl-to-rgb "colorful-mode")
 (declare-function colorful--find-overlay "colorful-mode")
 
+(declare-function ansi-color-apply "ansi-color")
+
 
 ;;; Hex
 
@@ -115,7 +118,6 @@ This is intended to be used with 
`colorful-extra-color-keyword-functions'."
                    (defined-colors))
                   'symbols)
       :type color-name
-      :match 0
       :case t ; HTML/CSS/Emacs color names are case insensitive.
       :function colorful--color-names-fn)
    colorful-color-keywords))
@@ -234,7 +236,7 @@ This is intended to be used with 
`colorful-extra-color-keyword-functions'."
     (colorful--oklch-to-hex match-1 match-2 match-3)))
 
 (defun colorful-add-oklab-oklch-colors ()
-  "Add CSS OKLAB and OKLCH color highlighting.
+  "Enable CSS OKLAB and OKLCH color highlighting.
 This is intended to be used with `colorful-extra-color-keyword-functions'."
   ;; OKLAB
   (cl-pushnew
@@ -358,6 +360,32 @@ This is intended to be used with 
`colorful-extra-color-keyword-functions'."
       :function colorful-latex-gray)
    colorful-color-keywords))
 
+
+;;; Shell colors
+
+(defun colorful--ansi-fn (&rest _)
+  (let* ((match (format "\033%sX" (match-string-no-properties 2)))
+         (face-property (get-text-property
+                         0 'font-lock-face
+                         (ansi-color-apply match))))
+    (or (plist-get face-property :foreground)
+        (plist-get face-property :background)
+        (cadr (or (assq :background face-property)
+                  (assq :foreground face-property))))))
+
+(defun colorful-add-ansi-shell-colors ()
+  "Enable ANSI shell color highlighting.
+This is intended to be used with `colorful-extra-color-keyword-functions'."
+  (require 'ansi-color)
+  (cl-pushnew
+   `( :keywords ,(rx (group (or (seq "\\" (any "Ee"))
+                                "\\033"
+                                (seq "\\x1" (any "Bb"))
+                                "\033"))
+                     (group "[" (zero-or-more (any "0-9" ";")) "m"))
+      :type ansi
+      :function colorful--ansi-fn)
+   colorful-color-keywords))
 
 (provide 'colorful-colors)
 ;;; colorful-colors.el ends here
diff --git a/colorful-mode.el b/colorful-mode.el
index 96a8026884..883d1024c3 100644
--- a/colorful-mode.el
+++ b/colorful-mode.el
@@ -243,7 +243,8 @@ Available functions are:
  - `colorful-add-rgb-colors'
  - `colorful-add-hsl-colors'
  - `colorful-add-oklab-oklch-colors'
- - `colorful-add-latex-colors'"
+ - `colorful-add-latex-colors'
+ - `colorful-add-ansi-shell-colors'"
   :type '(repeat
           (choice (cons (choice :tag "Mode(s)" symbol (repeat symbol))
                         (choice :tag "Function(s)" (repeat function)
@@ -322,11 +323,16 @@ comments, including color names, which can be annoying."
 ;;;; Internal variables
 
 (defvar-local colorful-color-keywords nil
-  "Font-lock colors keyword to highlight.")
+  "List of colors keywords to highlight.")
 
 (defvar-local colorful--highlight nil
   "Internal variable used for check when the highlighting must be done.")
 
+(defvar colorful--conversion-choices
+  '(("Hexadecimal color format" . hex)
+    ("Color name" . color-name))
+  "Alist with all supported conversions formats.")
+
 
 ;;;; Internal Functions
 
@@ -431,12 +437,13 @@ BEG is the position to check for the overlay."
 
   ;; 1# Case: replace all the colors in an active region.
   (if (and beg end)
-      (let* ((choices '(("Hexadecimal color format" . hex)
-                        ("Color name" . name)))
-             ;; Start prompt.
+      (let* (;; Start prompt.
              (choice (alist-get
-                      (completing-read "Change colors in region: " choices nil 
t nil nil)
-                      choices nil nil 'equal))
+                      (completing-read "Change colors in region: "
+                                       colorful--conversion-choices
+                                       nil t nil nil)
+                      colorful--conversion-choices
+                      nil nil 'equal))
              ;; Define counters
              (ignored-colors 0)
              (changed-colors 0))
@@ -444,7 +451,8 @@ BEG is the position to check for the overlay."
         (dolist (ov (overlays-in beg end))
           ;; Ensure we are in colorful--overlay
           (when (overlay-get ov 'colorful--overlay)
-            (if-let* ((result (colorful--converter ov choice))
+            (if-let* ((kind (overlay-get ov 'colorful--color-kind))
+                      (result (colorful--converter ov choice kind))
                       ((consp result))
                       (range (cdr result)) ; Get the positions where it should 
be replaced.
                       (start (car range))
@@ -514,38 +522,29 @@ BEG is the position to check for the overlay."
          ;; If not COLOR string then get it from buffer.
          (color (or color (buffer-substring-no-properties beg end)))
          (prompt (format prompt color))
-         (choices '(("Hexadecimal color format" . hex)
-                    ("Color name" . color-name)))
+         (kind (overlay-get ov 'colorful--color-kind))
          ;; Get choice.
          (choice (alist-get
-                  (completing-read prompt choices nil t nil nil)
-                  choices nil nil 'equal))
-         (converted-color (colorful--converter ov choice)))
+                  (completing-read prompt colorful--conversion-choices
+                                   (lambda (elt) (not (eq (cdr elt) kind)))
+                                   t nil nil)
+                  colorful--conversion-choices nil nil 'equal))
+         (converted-color (colorful--converter ov choice kind)))
 
     (unless converted-color
       (user-error "No color available"))
 
-    ;; If choice is the same type as the color at point
-    ;; run again this function and send a message saying the color
-    ;; is the same type.
-    (if (stringp converted-color)
-        (colorful--prompt-converter ov converted-color beg end color)
+    converted-color))
 
-      converted-color)))
-
-(defun colorful--converter (ov choice)
+(defun colorful--converter (ov choice kind)
   "Convert color from OV to other format.
-Return a list which contains the new color and the positions to replace,
-otherwise return a string for message error.
-
+Return a list which contains the new color and the positions to replace.
+KIND (a symbol) is the kind of color.
 CHOICE is used for get kind of color."
   (let* ((beg (overlay-start ov)) ; Find positions.
          (end (overlay-end ov))
-         (kind (overlay-get ov 'colorful--color-kind))
          (color-value (overlay-get ov 'colorful--color)))
     (pcase choice ; Check and convert color to any of the options:
-      ((pred (eq kind))
-       (format "%s is already a %s. Try again: " color-value kind))
       ('hex ; color to HEX
        (list
         (colorful--short-hex
@@ -613,7 +612,8 @@ and positions to colorize."
                  (nth 3 (syntax-ppss))))
              (eq colorful--highlight t)))
 
-    (let* ((return (funcall function color beg end))
+    (let* ((return (save-match-data
+                     (funcall function color beg end)))
            (color (or (car-safe return) return)))
       (when (and color (color-defined-p color))
         (let ((beg (if (consp return) (nth 1 return) beg))
diff --git a/test/css.css b/test/css.css
index f246b74a70..a4c8c3f764 100644
--- a/test/css.css
+++ b/test/css.css
@@ -26,14 +26,14 @@ vars
 /*                           oklab and oklch colors                          */
 /*****************************************************************************/
 
-oklch(89.32% 0.12 248.9); /* #b5e1ff */
-oklab(89.32% -0.04 -0.11); /* #b5e1ff */
+oklch(89.32% 0.12 248.9); /* #9ce3ff */
+oklab(89.32% -0.04 -0.11); /* #9ce3ff */
 
-oklch(64.56% 0.2146 0 / 20%); /* #ed4188 */
-oklab(64.56% 0.21 0 / 20%); /* #ed4188 */
+oklch(64.56% 0.2146 0 / 20%); /* #ed4088 */
+oklab(64.56% 0.21 0 / 20%); /* #ec4389 */
 
-oklab(0.50 -0.06 -0.24); /* #0054EB */
-oklch(0.50 0.25 255.2); /* #0054EB */
+oklab(0.50 -0.06 -0.24); /* #0054eb */
+oklch(0.50 0.25 255.2); /* #0054eb */
 
 /*****************************************************************************/
 /*                          rgb and hsl (with alpha)                         */
diff --git a/test/emacs-colors.el b/test/emacs-colors.el
index 9ed2ac766b..aff67ee14c 100644
--- a/test/emacs-colors.el
+++ b/test/emacs-colors.el
@@ -40,3 +40,7 @@ Crimson     "Crimson"
 Red         "Red"
 Lime        "Lime"
 Green       "Green"
+
+;;; ANSI Colors
+"\e[0;41m test_red_color \e[0m"
+"\033[0;46m test_blue_color \033[0m"

Reply via email to