emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/colorful-mode 6080583490 07/20: Add experimental suppor


From: ELPA Syncer
Subject: [elpa] externals/colorful-mode 6080583490 07/20: Add experimental support for CSS rgb
Date: Sat, 27 Apr 2024 18:57:42 -0400 (EDT)

branch: externals/colorful-mode
commit 608058349046ce902369deba4c0077731386cfa0
Author: Elias G. B. Perez <eg642616@gmail.com>
Commit: Elias G. B. Perez <eg642616@gmail.com>

    Add experimental support for CSS rgb
    
    This only allow preview rgb colors and
    convert them to hex or emacs color names.
    Also deprecate `colorful-extra-color-keywords-hook'
    in favor a new and better alternative
    `colorful-extra-color-keyword-functions'
    * colorful-mode.el (colorful-extra-color-keywords-hook):
    Deprecate and replace it with colorful-extra-color-keyword-functions.
    * README.org: Document changes.
---
 README.org       |  67 +++++++++----
 colorful-mode.el | 281 ++++++++++++++++++++++++++++++++++++++++---------------
 2 files changed, 252 insertions(+), 96 deletions(-)

diff --git a/README.org b/README.org
index cc096c1678..ad93da48c4 100644
--- a/README.org
+++ b/README.org
@@ -1,5 +1,5 @@
 #+title: [[./assets/colorful-mode-logo.svg]]
-#+subtitle: Preview any color in your buffer
+#+subtitle: Preview any color in your buffer in real time.
 #+author: Elias G.B. Perez
 #+language: en
 #+export_file_name: colorful-mode.texi
@@ -9,15 +9,15 @@
 
 #+html: <img 
src="https://raw.githubusercontent.com/DevelopmentCool2449/emacs-svg-badges/main/elisp_logo_warning.svg";
 align="right" width="10%">
 
-🟒colorful-mode is a minor mode that allow you preview *color hex* and
-*color names*, in your current buffer in real time and in a user
-friendly way based on 
🌈[[https://elpa.gnu.org/packages/rainbow-mode.html][rainbow-mode.el]].
+🟒colorful-mode is a minor mode that allow you preview any color
+format such as *color hex* and *color names*, in your current buffer
+in real time and in a user friendly way based on 
🌈[[https://elpa.gnu.org/packages/rainbow-mode.html][rainbow-mode.el]].
 
-*WARNING: THIS PACKAGE STILL IN A EARLY STATE, ABRUPT CHANGES SUCH AS DELETE 
USER FUNCTIONS, OPTIONS AND KEYBINDINGS OR UNEXPECTED BREAKS ARE USUALLY COMMON*
+*WARNING: THIS PACKAGE STILL IN A EXPERIMENTAL STATE, ABRUPT CHANGES SUCH AS 
DELETE USER FUNCTIONS, OPTIONS AND KEYBINDINGS OR UNEXPECTED BREAKS ARE USUALLY 
COMMON*
 
 * Features ✨
-- Preview emacs colors names and hex in your current buffer in real
-  time /(soon support for CSS rgb)/.
+- Preview emacs colors names, hexadecimal colors and CSS rgb functions
+  in your current buffer in real time.
 - Convert color to other formats or copy them without replace current
   color.
 - Preview using highlight or a prefix/suffix string.
@@ -26,7 +26,7 @@ friendly way based on 
🌈[[https://elpa.gnu.org/packages/rainbow-mode.html][rai
 
 * Screenshots and animated GIFs πŸ“·
 [[./assets/gif1.gif]]
-/With prefix instead highliht./
+/With prefix instead highliht/.
 
 [[./assets/gif2.gif]]
 [[./assets/gif3.gif]]
@@ -35,7 +35,7 @@ friendly way based on 
🌈[[https://elpa.gnu.org/packages/rainbow-mode.html][rai
 [[./assets/screenshot3.png]]
 
 [[./assets/screenshot4.png]]
-/With a custom prefix (in this example a non-ASCII/Unicode character)./
+/With a custom prefix (in this example a non-ASCII/Unicode character)/.
 
 * User Options, Setups and Guides πŸ“–
 ** Customizable User options
@@ -43,13 +43,36 @@ friendly way based on 
🌈[[https://elpa.gnu.org/packages/rainbow-mode.html][rai
   for change color.
 - =colorful-use-prefix (default: nil)= If non-nil, use prefix for preview color
   instead highlight them.
+  *NOTE: css-mode and css-ts-mode by default colorize rgb and hex colors, this 
may interfere with colorful prefix, you can disable this setting 
=css-fontify-colors= to nil*
 - =colorful-prefix-string (default: "●")= Prefix symbol to be used according
   =colorful-use-prefix=.
 - =colorful-prefix-alignment (default: 'left)= The position to put prefix 
string.
   The value can be left or right.
-- =colorful-extra-color-keywords-hook (default: nil)= Hook used for add extra 
color
-  keywords to =colorful-color-keywords=.
-  Available functions are: =colorful-add-color-names=.
+- =colorful-extra-color-keyword-functions=
+  default:
+#+begin_src emacs-lisp
+  '((emacs-lisp-mode . (colorful-add-color-names colorful-add-rgb-colors))
+    ((css-mode css-ts-mode) . colorful-add-rgb-colors))
+#+end_src
+  List of functions to add extra color keywords to colorful-color-keywords.
+
+  It can be a cons cell specifing the mode (or a list of modes)
+  e.g:
+#+begin_src emacs-lisp
+  (((css-mode css-ts-mode) . colorful-add-rgb-colors)
+    (emacs-lisp-mode . (colorful-add-color-names colorful-add-rgb-colors))
+    ((text-mode html-mode) . (colorful-add-color-names
+                              colorful-add-rgb-colors))
+    ...)
+#+end_src
+  Or a simple list of functions for executing wherever colorful is active:
+  (colorful-add-color-names
+    colorful-add-rgb-colors)
+
+  Available functions are:
+   - colorful-add-color-names.
+   - colorful-add-rgb-colors.
+
 - =colorful-exclude-colors (default: '("#def"))= List of keyword to don't 
highlight.
 - =colorful-short-hex-convertions (default: t)= If non nil, hex
   values converted by coloful should be as short as possible.
@@ -57,7 +80,7 @@ friendly way based on 
🌈[[https://elpa.gnu.org/packages/rainbow-mode.html][rai
   and can make them inaccurate.
 - =colorful-only-strings (default: nil)= If non-nil colorful will only
   highlight colors inside strings.
-- =global-colorful-modes (default: '(mhtml-mode html-ts-mode css-mode 
css-ts-mode emacs-lisp-mode))= Which major modes colorful-mode is switched on 
in (globally).
+- =global-colorful-modes (default: '(mhtml-mode html-ts-mode css-mode 
css-ts-mode emacs-lisp-mode))= Which major modes global-colorful-mode is 
switched on in (globally).
 
 ** Interactive User Functions.
 - =colorful-change-or-copy-color= Change or copy color to a converted
@@ -80,19 +103,21 @@ Colorful by default provides extra functions that 
highlight additional
 colors:
 
 - =colorful-add-color-names= Add emacs color names to colorful-color-keywords.
+- =colorful-add-rgb-colors= Add CSS rgb color to colorful-color-keywords.
 
-For use them add it to =colorful-extra-color-keywords-hook=:
+For use them add it to:
 #+begin_src emacs-lisp
 ;; In this example add emacs color names to colorful-keywords.
-(add-hook 'colorful-extra-color-keywords-hook #'colorful-add-color-names)
+(add-to-list 'colorful-extra-color-keyword-functions 
#'colorful-add-color-names)
 #+end_src
 
+See: =colorful-extra-color-keyword-functions= for more details.
+
 * Installation πŸ“¦
-It's recommended that you must use emacs-29.3 or higher,
-however you can use it in emacs-28.1.
+It's recommended that you must use emacs-28.X or higher.
 
 Currently colorful-mode is not available in any elisp package archive,
-however you can install it with =package-vc-install=
+however you can install it with =package-vc-install= in emacs-29.X
 
 Run =M-x package-vc-install=
 And type =https://github.com/DevelopmentCool2449/colorful-mode.git=
@@ -114,11 +139,12 @@ and fixing some /(and old)/ bugs:
 
|---------------------------------------------------------+------------------+-----------------|
 | Compatible with hl-line and other overlays?             | βœ“                | 
❌              |
 | Convert color to other formats?                         | βœ“                | 
❌              |
-| Insert open color hex                                   | not yet          | 
❌(?)           |
+| Insert open color hex                                   | Work in progress | 
❌              |
 | Opcionally use string preffix/suffix instead highlight  | βœ“                | 
❌              |
 | Use emacs built-in functions instead define custom ones | βœ“^{1}            | 
βœ“^{2}           |
 | Exclude keywords/colors to highlight                    | βœ“                | 
❌^{3}          |
 | Highlight only in strings and docstrings                | βœ“                | 
❌              |
+| Allow highlight some colors only in specific modes      | βœ“                | 
❌              |
 | No performance issues?^{4}                              | ❌               | 
βœ“               |
 
 1. However colorful defines some helper funtions for some cases and
@@ -154,3 +180,6 @@ submit it to ELPA, later that *I won't be able to be the 
mantainer* of
 this package, However I would like if someone would be able to be the
 mantainer (if you are interested please email me
 <eg642616@gmail.com>).
+
+#+html: <img 
src="https://raw.githubusercontent.com/DevelopmentCool2449/emacs-svg-badges/main/powered_by_emacs.svg";
 align="right" width="10%">
+#+html: <img 
src="https://raw.githubusercontent.com/DevelopmentCool2449/emacs-svg-badges/main/powered_by_org_mode.svg";
 align="right" width="10%">
diff --git a/colorful-mode.el b/colorful-mode.el
index bdeb4fef78..fd6d5a0c95 100644
--- a/colorful-mode.el
+++ b/colorful-mode.el
@@ -1,4 +1,4 @@
-;;; colorful-mode.el --- Preview any color in your buffer  -*- 
lexical-binding: t; -*-
+;;; colorful-mode.el --- Preview any color in your buffer in real time.  -*- 
lexical-binding: t; -*-
 
 ;; Copyright (C) 2024 Elias G.B. Perez
 
@@ -8,7 +8,7 @@
 ;; Package-Requires: ((emacs "28.1") (compat "29.1.4.5"))
 ;; Homepage: https://github.com/DevelopmentCool2449/colorful-mode
 ;; Keywords: faces
-;; Version: 0.1.2
+;; Version: 0.1.3
 
 ;; This file is not part of GNU Emacs.
 
@@ -26,21 +26,27 @@
 ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
 ;;; Commentary:
-;;  Minor mode that sets background color to color names, hex values
-;;  or rgb values (CSS) found in your current buffer in a friendly and
-;;  effective way based on rainbow-mode.
+;;  Minor mode for coloring color names, hex values or rgb values (CSS)
+;;  found in your current buffer in a friendly and effective way based
+;;  on rainbow-mode.
 
 ;;; Code:
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;                                  Libraries                                 ;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
-;; Importing Libraries.
-(require 'compat)
+(require 'compat) ; This should add compatibility for emacs-28.2 and higher.
 
-(require 'faces)
 (require 'color)
-(require 'rx)
+(require 'subr-x)
+;; (require 'rx) ; Not sure if this should be added.
 
 
-;; Buffer-local variables
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;                           Buffer-local variables                           ;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
 (defvar-local colorful-color-keywords
     `((,(rx (seq (not (any "&"))
                  (group "#" (repeat 1 4 (= 3 (any "0-9A-Fa-f"))))
@@ -91,20 +97,55 @@
 Must be a list containing regex strings.")
 
 
-;; Customizable User options.
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;                          Customizable User Options                         ;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
 (defgroup colorful nil
   "Preview hex colors values in current buffer.."
   :tag "Colorful mode"
   :group 'help)
 
 (defface colorful-base
-  '((t (:box (:line-width (1 . 1) :color "grey75" :style flat-button))))
+  '((t (:box (:line-width -1 :color "grey75" :style flat-button))))
   "Face used as base for highlight color names.")
 
-(defcustom colorful-extra-color-keywords-hook nil
+(defcustom colorful-extra-color-keyword-functions
+  '((emacs-lisp-mode . (colorful-add-color-names
+                        colorful-add-rgb-colors))
+    ((css-mode css-ts-mode) . colorful-add-rgb-colors))
+  "List of functions to add extra color keywords to `colorful-color-keywords'.
+It can be a cons cell specifing the mode (or a list of modes)
+e.g:
+\(((`css-mode' `css-ts-mode') . `colorful-add-rgb-colors')
+  (`emacs-lisp-mode' . (`colorful-add-color-names'
+                        `colorful-add-rgb-colors'))
+  ((`text-mode' `html-mode') . (`colorful-add-color-names'
+                                `colorful-add-rgb-colors'))
+  ...)
+Or a simple list of functions for executing wherever colorful is active:
+\(`colorful-add-color-names'
+  `colorful-add-rgb-colors')
+
+Available functions are:
+ - `colorful-add-color-names'.
+ - `colorful-add-rgb-colors'."
+  :type '(repeat
+          (choice
+           (cons (choice :tag "Mode(s)"
+                         symbol
+                         (repeat symbol))
+                 (choice :tag "Functions(s)"
+                         (repeat function)
+                         function))
+           function)))
+
+(defvar colorful-extra-color-keywords-hook nil
   "Hook used for add extra color keywords to `colorful-color-keywords'.
-Available functions are: `colorful-add-color-names'."
-  :type 'hook)
+Available functions are: `colorful-add-color-names'.")
+
+(make-obsolete-variable 'colorful-extra-color-keywords-hook
+                        'colorful-extra-color-keyword-functions "0.1.3")
 
 (defcustom colorful-allow-mouse-clicks t
   "If non-nil, allow using mouse buttons for change color."
@@ -139,7 +180,10 @@ and can make them inaccurate."
   :type 'boolean)
 
 
-;; Keymaps
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;                                   Keymaps                                  ;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
 (defvar-keymap colorful-mode-map
   :doc "Keymap when `colorful-mode' is active."
   "C-c c c" #'colorful-change-or-copy-color
@@ -147,27 +191,9 @@ and can make them inaccurate."
   "C-c c r" #'colorful-convert-and-change-color)
 
 
-;; Internal Functions
-
-;; (defun colorful--hex-to-rgb (hex)
-;;   "Return HEX color as CSS rgb format."
-;;   ;; (query-replace)
-;;   )
-
-(defun colorful--hex-to-name (hex)
-  "Return HEX as Emacs color name."
-  (let ((color (color-values hex))
-        name)
-    (dolist (color-list color-name-rgb-alist)
-      (if (equal (cdr color-list) color)
-          (setq name (car color-list))))
-    name))
-
-(defun colorful--name-to-hex (name)
-  "Return Emacs color NAME as hex color format."
-  (let* ((short (if colorful-short-hex-convertions 2 1))
-         (color (append (color-name-to-rgb name) `(,short))))
-    (apply #'color-rgb-to-hex color)))
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;                             Internal Functions                             ;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 ;; FIXME: THIS MACRO WORKS FINE, HOWEVER IT DOESN'T WORK WITH
 ;;       MOUSE CLICKS, IF ANYONE KNOW WHY, PLEASE OPEN AN ISSUE.
@@ -190,6 +216,49 @@ and can make them inaccurate."
 ;;        ,(macroexp-progn then)
 ;;      (user-error "No color found")))
 
+;; Base Convertion functions.
+;; (defun colorful--hex-to-rgb (hex)
+;;   "Return HEX color as CSS rgb format."
+;;   ;; (query-replace)
+;;   )
+
+(defun colorful--percentage-to-absolute (percentage)
+  "Convert PERCENTAGE to a absolute number.
+If NUMBER is absolute, return NUMBER.
+This will convert \"80 %\" to 204, \"100 %\" to 255 but \"123\" to \"123\".
+If the percentage value is above 100, it's converted to 100."
+  (let ((string-length (- (length percentage) 1)))
+    ;; Is this a number with %?
+    (if (eq (elt percentage string-length) ?%)
+        (/ (* (min (string-to-number (substring percentage 0 string-length)) 
100) 255) 100)
+      (string-to-number percentage))))
+
+(defun colorful--rgb-to-hex (rgb)
+  "Return CSS RGB as hexadecimal format.
+RGB must be a string."
+  (let* ((rgb (string-split (string-remove-prefix "rgb(" rgb) ","))
+            (r (colorful--percentage-to-absolute (nth 0 rgb)))
+         (g (colorful--percentage-to-absolute (nth 1 rgb)))
+         (b (colorful--percentage-to-absolute (nth 2 rgb))))
+    (format "#%02X%02X%02X" r g b)))
+
+(defun colorful--hex-to-name (hex)
+  "Return HEX as Emacs color name."
+  (let ((color (color-values hex))
+        name)
+    (dolist (color-list color-name-rgb-alist)
+      (if (equal (cdr color-list) color)
+          (setq name (car color-list))))
+    name))
+
+(defun colorful--name-to-hex (name)
+  "Return Emacs color NAME as hex color format."
+  (let* ((short (if colorful-short-hex-convertions 2 1))
+         (color (append (color-name-to-rgb name) `(,short))))
+    (apply #'color-rgb-to-hex color)))
+
+;;;;;;;;;; User Interactive Functions ;;;;;;;;;;
+
 ;;;###autoload
 (defun colorful-convert-and-change-color ()
   "Convert color to a valid format and replace color at current cursor 
position."
@@ -198,7 +267,7 @@ and can make them inaccurate."
                            (dolist (ov (overlays-at (point)))
                              (if (overlay-get ov 'colorful--overlay)
                                  (throw 'val ov)))))
-            (result (colorful--change-color colorful-ov "Change %s to: "))
+            (result (colorful--change-color colorful-ov "Change '%s' to: "))
             (range (cdr result))
             (text (car result)))
       (save-excursion
@@ -214,7 +283,7 @@ and can make them inaccurate."
                            (dolist (ov (overlays-at (point)))
                              (if (overlay-get ov 'colorful--overlay)
                                  (throw 'val ov)))))
-            (result (car (colorful--change-color colorful-ov "Copy %s as: ")))
+            (result (car (colorful--change-color colorful-ov "Copy '%s' as: 
")))
             (color (if (color-defined-p result)
                        (propertize result 'face
                                    `(:background
@@ -243,11 +312,15 @@ and can make them inaccurate."
         (colorful-convert-and-copy-color)
       (colorful-convert-and-change-color))))
 
+;;;;;;;;;; Coloring functions ;;;;;;;;;;
+
 (defun colorful--change-color (ov &optional prompt color beg end)
   "Return COLOR as other color format.
+This return a list which contain the text to be replaced,
+beginning and end where should be inserted.
 COLOR, BEG, and END are only used as internal values, not intended to
-be used externally.  OV must be an overlay
-PROMPT must be a string with 1 format control (generally a sring argument)."
+be used externally.  OV must be an overlay.
+PROMPT must be a string with 1 format control (generally a string argument)."
   (let* ((beg (or beg (overlay-start ov)))
          (end (or end (overlay-end ov)))
          (color (or color (buffer-substring-no-properties beg end)))
@@ -261,40 +334,48 @@ PROMPT must be a string with 1 format control (generally 
a sring argument)."
     (pcase choice ; Convert to...
       ('hex
        (if (not (string-prefix-p "#" color)) ; Ensure is not already a hex
-           (cond ((member color (defined-colors)) ; Name
-                  (list (colorful--name-to-hex color) beg end))
-                 ;; TODO: () ; rgb
-                 )
+           (cond
+            ;; Is a Name?
+            ((member color (defined-colors))
+             (list (colorful--name-to-hex color) beg end))
+            ;; Is a CSS rgb?
+            ((string-prefix-p "rgb(" color)
+             (list (colorful--rgb-to-hex color) beg end)))
+
          (colorful--change-color ov "%s is already a Hex color. Try again: "
                                  color beg end)))
+
       ;; ('rgb (unless (string-prefix-p "rgb" color)))
+
       ('name
        (if (not (assoc color color-name-rgb-alist))
-           (cond ((string-prefix-p "#" color) ; Hex
-                  (if-let ((rep (colorful--hex-to-name color)))
-                      (list rep beg end)
-                    (user-error "No color name available")
-                    nil))
-                 ;; ((string-prefix-p "rgb" color) ; CSS rgb
-                 ;; (let ((rep (colorful--hex-to-name color)))
-                 ;; (list rep beg end)))
-                 )
-         (colorful--change-color ov "%s is already a color name. Try again: "
-                                 color beg end))))))
+           (cond
+            ;; Is a Hex?
+            ((string-prefix-p "#" color)
+             (if-let ((rep (colorful--hex-to-name color)))
+                 (list rep beg end)
+               (user-error "No color name available")
+               nil))
+            ;; Is a CSS rgb?
+            ((string-prefix-p "rgb(" color)
+             (if-let ((rep (colorful--rgb-to-hex color))
+                      (rep (colorful--hex-to-name rep)))
+                 (list rep beg end)
+               (user-error "No color name available"))))
+
+         (colorful--change-color
+          ov "%s is already a color name. Try again: " color beg end))))))
 
 (defun colorful--delete-overlay (overlay &rest _)
   "Helper function for delete OVERLAY."
   (delete-overlay overlay))
 
-(defun colorful--colorize-match (color &optional match)
-  "Overlay MATCH string with a face.
+(defun colorful--colorize-match (color beg end)
+  "Overlay match with a face from BEG to END.
 The background uses COLOR color value.  The foreground is obtained
 converting COLOR to a Emacs RGB value and determined with `color-dark-p',
 it can be white or black."
-  (let* ((match (or match 0))
-         (start (match-beginning match))
-         (end (match-end match))
-         (ov (make-overlay start end nil t t)))
+  (let* ((ov (make-overlay beg end nil t t)))
 
     ;; Define colorful overlay tag
     (overlay-put ov 'colorful--overlay t)
@@ -343,36 +424,82 @@ it can be white or black."
 
 (defun colorful-colorize-itself (&optional match)
   "Helper function for Colorize MATCH with itself."
-  (let* ((match1 (or match 0))
-         (string (match-string-no-properties match1)))
-    (when (and (not (member string colorful-exclude-colors))
-               (or (and colorful-only-strings (nth 3 (syntax-ppss)))
-                   (not colorful-only-strings)))
-      ;; Delete duplicates overlays found
-      (dolist (ov (overlays-in (match-beginning match1) (match-end match1)))
-        (if (overlay-get ov 'colorful--overlay)
-            (colorful--delete-overlay ov)))
-
-      (colorful--colorize-match string match))))
+  (when-let* ((match (or match 0))
+              (string (match-string-no-properties match))
+              ((and (not (member string colorful-exclude-colors))
+                    (or (and colorful-only-strings (nth 3 (syntax-ppss)))
+                        (not colorful-only-strings))))
+              (beg (match-beginning match))
+              (end (match-end match)))
+    (cond
+     ((string-prefix-p "rgb(" string)
+      (setq string (colorful--rgb-to-hex string))))
+
+    ;; Delete duplicates overlays found
+    (dolist (ov (overlays-in beg end))
+      (if (overlay-get ov 'colorful--overlay)
+          (colorful--delete-overlay ov)))
+
+    (colorful--colorize-match string beg end)))
 
 
-;; Extras color regex functions and variables.
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;                        Functions for Extra Coloring                        ;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
 (defvar colorful-color-name-font-lock-keywords
   `((,(regexp-opt (defined-colors) 'words)
      (0 (colorful-colorize-itself))))
   "Font-lock keywords to add for `colorful-color-keywords'.")
 
+(defvar colorful-rgb-font-lock-keywords
+  `((,(rx (seq "rgb(" (zero-or-more " ")
+               (group (repeat 1 3 (any "0-9"))
+                      (opt nonl (any "0-9"))
+                      (opt (zero-or-more " ") "%"))
+               (zero-or-more " ") "," (zero-or-more " ")
+               (group (repeat 1 3 (any "0-9"))
+                      (opt "." (any "0-9"))
+                      (opt (zero-or-more " ") "%"))
+               (zero-or-more " ") "," (zero-or-more " ")
+               (group (repeat 1 3 (any "0-9"))
+                      (opt "." (any "0-9"))
+                      (opt (zero-or-more " ") "%"))
+               (zero-or-more " ") ")"))
+     (0 (colorful-colorize-itself))))
+
+  "Font-lock keywords to add for RGB colors.")
 (defun colorful-add-color-names ()
   "Function for add Emacs color names to `colorful-color-keywords'.
-This is intended to be used with `colorful-extra-color-keywords-hook'."
+This is intended to be used with `colorful-extra-color-keyword-functions'."
   (dolist (colors colorful-color-name-font-lock-keywords)
     (add-to-list 'colorful-color-keywords colors t)))
 
+(defun colorful-add-rgb-colors ()
+  "Function for add CSS RGB colors keywords to `colorful-color-keywords'.
+This is intended to be used with `colorful-extra-color-keyword-functions'."
+  (dolist (colors colorful-rgb-font-lock-keywords)
+    (add-to-list 'colorful-color-keywords colors t)))
+
 
-;; Minor mode definitions.
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;                           Minor mode defintinions                          ;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
 (defun colorful--turn-on ()
   "Helper function for turn on `colorful-mode'."
-  (run-hooks 'colorful-extra-color-keywords-hook)
+  ;; Execute keyword functions before add them to font-lock keywords.
+  (dolist (fn colorful-extra-color-keyword-functions)
+    (cond
+     ((and (listp fn)
+           (derived-mode-p (car fn)))
+      (if (listp (cdr fn))
+          (dolist (fn-list (cdr fn))
+            (funcall fn-list))
+        (funcall (cdr fn))))
+
+     ((functionp fn)
+      (funcall fn))))
 
   (font-lock-add-keywords nil colorful-color-keywords)
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]