branch: externals/colorful-mode commit 0ebfa75fe114199ee6c7b4f1346e13fcb103a71e Author: Elías Gabriel Pérez <eg642...@gmail.com> Commit: Elías Gabriel Pérez <eg642...@gmail.com>
Use jit-lock instead font-lock-add-keywords. font-lock overrides buffer text properties, using a custom jit-lock function should fix this. * README.org (Disable colorful in regions): Revert previous changes. This should remove complete overlays from overlay beginning and end instead only the region position. * colorful-mode.el (colorful-excluded-buffers): Set to nil (this option may be deprecated) (colorful--delete-overlays): Remove unused function. (colorful--shorten-hex, colorful--converter, colorful--colorize-match): Remove alpha code. (colorful-mode-fontify-region): New function. (colorful--colorize, colorful-hex-font-lock-keywords) (colorful-color-name-font-lock-keywords) (colorful-css-variables-keywords, colorful-rgb-font-lock-keywords) (colorful-oklab-oklch-font-lock-keywords) (colorful-hsl-font-lock-keywords, colorful-latex-keywords): Rewrite for jit-lock use. (colorful--turn-on, colorful--turn-off): Use jit-lock register functions. (colorful-mode): Remove 'font-lock-flush'. --- README.org | 4 +- colorful-mode.el | 433 +++++++++++++++++++------------------------------------ 2 files changed, 152 insertions(+), 285 deletions(-) diff --git a/README.org b/README.org index 43cc282faf..71ef058a91 100644 --- a/README.org +++ b/README.org @@ -207,9 +207,11 @@ for you: (when-let* (colorful-mode (beg (use-region-beginning)) (end (use-region-end))) + ;; Remove full colorful overlay instead only the part where + ;; the region is. (dolist (ov (overlays-in beg end)) (when (overlay-get ov 'colorful--overlay) - (remove-overlays beg end 'colorful--overlay t)))))) + (remove-overlays (overlay-start ov) (overlay-end ov) 'colorful--overlay t)))))) (add-hook 'deactivate-mark-hook (lambda () diff --git a/colorful-mode.el b/colorful-mode.el index e554a5b7ad..5f24c30884 100644 --- a/colorful-mode.el +++ b/colorful-mode.el @@ -6,7 +6,7 @@ ;; Maintainer: Shen, Jen-Chieh <jcs090...@gmail.com> ;; Elias G. Perez <eg642...@gmail.com> ;; Created: 2024-04-10 -;; Package-Requires: ((emacs "28.1") (compat "30.0.2.0")) +;; Package-Requires: ((emacs "28.1") (compat "30")) ;; Homepage: https://github.com/DevelopmentCool2449/colorful-mode ;; Keywords: faces, tools, matching, convenience ;; Version: 1.2.3 @@ -29,7 +29,7 @@ ;;; Commentary: ;; Minor mode for coloring color names, hex values, or rgb/hsl values ;; (CSS), and more inside your buffer in real time, -;; developer-friendly and effective based on `rainbow-mode.el' +;; developer-friendly and effective based/inspired on `rainbow-mode.el' ;;; Code: @@ -60,16 +60,32 @@ Changing the background or foreground color will have no effect." :group 'colorful) (defcustom colorful-html-colors-alist - '(("aliceblue" . "#f0f8ff") - ("antiquewhite" . "#faebd7") + '(("black" . "#000000") + ("silver" . "#c0c0c0") + ("gray" . "#808080") + ("white" . "#ffffff") + ("maroon" . "#800000") + ("red" . "#ff0000") + ("purple" . "#800080") + ("fuchsia" . "#ff00ff") + ("magenta" . "#ff00ff") + ("green" . "#008000") + ("lime" . "#00ff00") + ("olive" . "#808000") + ("yellow" . "#ffff00") + ("navy" . "#000080") + ("blue" . "#0000ff") + ("teal" . "#008080") ("aqua" . "#00ffff") + ("cyan" . "#00ffff") + ("orange" . "#ffa500") + ("aliceblue" . "#f0f8ff") + ("antiquewhite" . "#faebd7") ("aquamarine" . "#7fffd4") ("azure" . "#f0ffff") ("beige" . "#f5f5dc") ("bisque" . "#ffe4c4") - ("black" . "#000000") ("blanchedalmond" . "#ffebcd") - ("blue" . "#0000ff") ("blueviolet" . "#8a2be2") ("brown" . "#a52a2a") ("burlywood" . "#deb887") @@ -79,13 +95,13 @@ Changing the background or foreground color will have no effect." ("coral" . "#ff7f50") ("cornflowerblue" . "#6495ed") ("cornsilk" . "#fff8dc") - ("cyan" . "#00ffff") + ("crimson" . "#dc143c") ("darkblue" . "#00008b") ("darkcyan" . "#008b8b") ("darkgoldenrod" . "#b8860b") ("darkgray" . "#a9a9a9") - ("darkgrey" . "#a9a9a9") ("darkgreen" . "#006400") + ("darkgrey" . "#a9a9a9") ("darkkhaki" . "#bdb76b") ("darkmagenta" . "#8b008b") ("darkolivegreen" . "#556b2f") @@ -107,15 +123,12 @@ Changing the background or foreground color will have no effect." ("firebrick" . "#b22222") ("floralwhite" . "#fffaf0") ("forestgreen" . "#228b22") - ("fuchsia" . "#ff00ff") ("gainsboro" . "#dcdcdc") ("ghostwhite" . "#f8f8ff") ("gold" . "#ffd700") ("goldenrod" . "#daa520") - ("gray" . "#808080") - ("grey" . "#808080") - ("green" . "#008000") ("greenyellow" . "#adff2f") + ("grey" . "#808080") ("honeydew" . "#f0fff0") ("hotpink" . "#ff69b4") ("indianred" . "#cd5c5c") @@ -131,8 +144,8 @@ Changing the background or foreground color will have no effect." ("lightcyan" . "#e0ffff") ("lightgoldenrodyellow" . "#fafad2") ("lightgray" . "#d3d3d3") - ("lightgrey" . "#d3d3d3") ("lightgreen" . "#90ee90") + ("lightgrey" . "#d3d3d3") ("lightpink" . "#ffb6c1") ("lightsalmon" . "#ffa07a") ("lightseagreen" . "#20b2aa") @@ -141,15 +154,12 @@ Changing the background or foreground color will have no effect." ("lightslategrey" . "#778899") ("lightsteelblue" . "#b0c4de") ("lightyellow" . "#ffffe0") - ("lime" . "#00ff00") ("limegreen" . "#32cd32") ("linen" . "#faf0e6") - ("magenta" . "#ff00ff") - ("maroon" . "#800000") ("mediumaquamarine" . "#66cdaa") ("mediumblue" . "#0000cd") ("mediumorchid" . "#ba55d3") - ("mediumpurple" . "#9370d8") + ("mediumpurple" . "#9370db") ("mediumseagreen" . "#3cb371") ("mediumslateblue" . "#7b68ee") ("mediumspringgreen" . "#00fa9a") @@ -160,25 +170,20 @@ Changing the background or foreground color will have no effect." ("mistyrose" . "#ffe4e1") ("moccasin" . "#ffe4b5") ("navajowhite" . "#ffdead") - ("navy" . "#000080") ("oldlace" . "#fdf5e6") - ("olive" . "#808000") ("olivedrab" . "#6b8e23") - ("orange" . "#ffa500") ("orangered" . "#ff4500") ("orchid" . "#da70d6") ("palegoldenrod" . "#eee8aa") ("palegreen" . "#98fb98") ("paleturquoise" . "#afeeee") - ("palevioletred" . "#d87093") + ("palevioletred" . "#db7093") ("papayawhip" . "#ffefd5") ("peachpuff" . "#ffdab9") ("peru" . "#cd853f") ("pink" . "#ffc0cb") ("plum" . "#dda0dd") ("powderblue" . "#b0e0e6") - ("purple" . "#800080") - ("red" . "#ff0000") ("rosybrown" . "#bc8f8f") ("royalblue" . "#4169e1") ("saddlebrown" . "#8b4513") @@ -187,7 +192,6 @@ Changing the background or foreground color will have no effect." ("seagreen" . "#2e8b57") ("seashell" . "#fff5ee") ("sienna" . "#a0522d") - ("silver" . "#c0c0c0") ("skyblue" . "#87ceeb") ("slateblue" . "#6a5acd") ("slategray" . "#708090") @@ -196,115 +200,14 @@ Changing the background or foreground color will have no effect." ("springgreen" . "#00ff7f") ("steelblue" . "#4682b4") ("tan" . "#d2b48c") - ("teal" . "#008080") ("thistle" . "#d8bfd8") ("tomato" . "#ff6347") ("turquoise" . "#40e0d0") ("violet" . "#ee82ee") ("wheat" . "#f5deb3") - ("white" . "#ffffff") ("whitesmoke" . "#f5f5f5") - ("yellow" . "#ffff00") - ("yellowgreen" . "#9acd32") - ("lightsalmon" . "#ffa07a") - ("darksalmon" . "#e9967a") - ("lightcoral" . "#f08080") - ("indianred" . "#cd5c5c") - ("crimson" . "#dc143c") - ("darkred" . "#8b0000") - ("orangered" . "#ff4500") - ("darkorange" . "#ff8c00") - ("lightyellow" . "#ffffe0") - ("lemonchiffon" . "#fffacd") - ("lightgoldenrodyellow" . "#fafad2") - ("papayawhip" . "#ffefd5") - ("peachpuff" . "#ffdab9") - ("palegoldenrod" . "#eee8aa") - ("darkkhaki" . "#bdb76b") - ("lawngreen" . "#7cfc00") - ("limegreen" . "#32cd32") - ("lime" . "#00ff00") - ("forestgreen" . "#228b22") - ("darkgreen" . "#006400") - ("greenyellow" . "#adff2f") ("yellowgreen" . "#9acd32") - ("springgreen" . "#00ff7f") - ("mediumspringgreen" . "#00fa9a") - ("lightgreen" . "#90ee90") - ("palegreen" . "#98fb98") - ("darkseagreen" . "#8fbc8f") - ("mediumseagreen" . "#3cb371") - ("seagreen" . "#2e8b57") - ("olive" . "#808000") - ("darkolivegreen" . "#556b2f") - ("olivedrab" . "#6b8e23") - ("lightcyan" . "#e0ffff") - ("aqua" . "#00ffff") - ("mediumaquamarine" . "#66cdaa") - ("paleturquoise" . "#afeeee") - ("mediumturquoise" . "#48d1cc") - ("darkturquoise" . "#00ced1") - ("lightseagreen" . "#20b2aa") - ("cadetblue" . "#5f9ea0") - ("darkcyan" . "#008b8b") - ("teal" . "#008080") - ("powderblue" . "#b0e0e6") - ("lightblue" . "#add8e6") - ("lightskyblue" . "#87cefa") - ("skyblue" . "#87ceeb") - ("deepskyblue" . "#00bfff") - ("lightsteelblue" . "#b0c4de") - ("dodgerblue" . "#1e90ff") - ("cornflowerblue" . "#6495ed") - ("steelblue" . "#4682b4") - ("royalblue" . "#4169e1") - ("mediumblue" . "#0000cd") - ("darkblue" . "#00008b") - ("midnightblue" . "#191970") - ("mediumslateblue" . "#7b68ee") - ("slateblue" . "#6a5acd") - ("darkslateblue" . "#483d8b") - ("fuchsia" . "#ff00ff") - ("mediumorchid" . "#ba55d3") - ("rebeccapurple" . "#663399") - ("mediumpurple" . "#9370db") - ("blueviolet" . "#8a2be2") - ("darkviolet" . "#9400d3") - ("darkorchid" . "#9932cc") - ("darkmagenta" . "#8b008b") - ("indigo" . "#4b0082") - ("lightpink" . "#ffb6c1") - ("hotpink" . "#ff69b4") - ("deeppink" . "#ff1493") - ("palevioletred" . "#db7093") - ("mediumvioletred" . "#c71585") - ("mintcream" . "#f5fffa") - ("aliceblue" . "#f0f8ff") - ("ghostwhite" . "#f8f8ff") - ("whitesmoke" . "#f5f5f5") - ("oldlace" . "#fdf5e6") - ("floralwhite" . "#fffaf0") - ("antiquewhite" . "#faebd7") - ("lavenderblush" . "#fff0f5") - ("mistyrose" . "#ffe4e1") - ("lightgray" . "#d3d3d3") - ("lightgrey" . "#d3d3d3") - ("silver" . "#c0c0c0") - ("darkgray" . "#a9a9a9") - ("darkgrey" . "#a9a9a9") - ("dimgray" . "#696969") - ("dimgrey" . "#696969") - ("lightslategray" . "#778899") - ("lightslategrey" . "#778899") - ("slategray" . "#708090") - ("slategrey" . "#708090") - ("darkslategray" . "#2f4f4f") - ("darkslategrey" . "#2f4f4f") - ("blanchedalmond" . "#ffebcd") - ("navajowhite" . "#ffdead") - ("rosybrown" . "#bc8f8f") - ("sandybrown" . "#f4a460") - ("saddlebrown" . "#8b4513")) + ("rebeccapurple" . "#663399")) "Alist of HTML colors. Each entry should have the form (COLOR-NAME . HEXADECIMAL-COLOR)." :type 'alist) @@ -370,7 +273,8 @@ Only relevant if `colorful-use-prefix' is non-nil." "List of keywords not to highlight." :type '(repeat string)) -(defcustom colorful-excluded-buffers '("*Faces*" "*Colors*") +;; XXX: deprecate? +(defcustom colorful-excluded-buffers nil "Do not activate colorful in these buffers. In case colorful breaks a buffer, such as a buffer derived from `help-mode', this option can be useful for you." @@ -394,12 +298,6 @@ major mode is derived from `prog-mode'." (defvar-local colorful-color-keywords nil "Font-lock colors keyword to highlight.") -(defvar colorful--color-names-regexp - (regexp-opt (append - (defined-colors) - (mapcar #'car colorful-html-colors-alist)) - 'symbols)) - ;;;; Internal Functions @@ -415,7 +313,7 @@ If PERCENTAGE is above 100%, it is converted to 100." (/ (* (min (string-to-number percentage) 100) 255) 100) (string-to-number percentage))) -(defun colorful--shorten-hex (hex &optional alpha) +(defun colorful--shorten-hex (hex) "Convert a 6-digit hexadecimal color representation to a 3-digit representation. HEX should be a string in the format `#RRGGBB' (6-digit form). If ALPHA is non-nil then use `#RRGGBBAA' format" @@ -423,16 +321,10 @@ If ALPHA is non-nil then use `#RRGGBBAA' format" (let ((r (substring hex 1 5)) (g (substring hex 5 9)) (b (substring hex 9 13))) - (if alpha - (format "#%02x%02x%02x%02x" - (/ (string-to-number r 16) 256) - (/ (string-to-number g 16) 256) - (/ (string-to-number b 16) 256) - (round (* alpha 255))) - (format "#%02x%02x%02x" - (/ (string-to-number r 16) 256) - (/ (string-to-number g 16) 256) - (/ (string-to-number b 16) 256)))) + (format "#%02x%02x%02x" + (/ (string-to-number r 16) 256) + (/ (string-to-number g 16) 256) + (/ (string-to-number b 16) 256))) hex)) (defun colorful--rgb-to-hex (r g b) @@ -493,12 +385,6 @@ BEG is the position to check for the overlay." (if (overlay-get ov 'colorful--overlay) (cl-return ov)))) -(defun colorful--delete-overlays (limit) - "Font-lock matcher that flushes our overlays before we install new ones." - (remove-overlays (point) limit 'colorful--overlay t) - ;; Tell font-lock we did not find any "match", so it doesn't call us back. - nil) - ;;;; User Interactive Functions @@ -528,8 +414,8 @@ If region is active, convert colors in mark." (range (cdr result)) ; Get the positions where it should be replaced. (new-color (car result))) (save-excursion - (goto-char (car range)) (apply #'delete-region range) + (goto-char (car range)) (insert new-color) (setq changed-colors (1+ changed-colors))) (setq ignored-colors (1+ ignored-colors))))) @@ -537,8 +423,7 @@ If region is active, convert colors in mark." (if (and (= changed-colors 0) (= ignored-colors 0)) (message "No color found in region.") - (message (concat (propertize "Changed colors: %d" 'face 'success) - ", " + (message (concat (propertize "Changed colors: %d" 'face 'success) " / " (propertize "Ignored colors: %d" 'face 'error)) changed-colors ignored-colors))) @@ -587,7 +472,7 @@ If region is active, convert colors in mark." ;;;;; Coloring functions (defun colorful--prompt-converter (ov prompt &optional beg end color) - "PROMPT user to convert color to other format." + "Prompt user to convert color to other format." (let* ((beg (or beg (overlay-start ov))) ; Find positions. (end (or end (overlay-end ov))) ;; If not COLOR string then get it from buffer. @@ -604,6 +489,9 @@ If region is active, convert colors in mark." (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) @@ -615,45 +503,38 @@ Return a list which contains the new color and the positions to replace, otherwise return a formatted string for message error. CHOICE is used for get kind of color." - (let* ((beg (overlay-start ov)) ; Find positions. (end (overlay-end ov)) (kind (overlay-get ov 'colorful--overlay-kind)) - (color-value (overlay-get ov 'colorful--overlay-color)) - (_alpha-value (overlay-get ov 'colorful--overlay-alpha))) + (color-value (overlay-get ov 'colorful--overlay-color))) (pcase choice ; Check and convert color to any of the options: ('hex ; COLOR to HEX (pcase kind - ('hex - "%s is already a Hex color. Try again: ") + ('hex "%s is already a Hex color. Try again: ") ;; Is COLOR a Name? - ('color-name - (list (colorful--shorten-hex color-value) beg end)) + ('color-name (list (colorful--shorten-hex color-value) beg end)) ;; Is COLOR a CSS rgb? - ('css-rgb - (list (colorful--shorten-hex color-value) beg end)) + ('css-rgb (list (colorful--shorten-hex color-value) beg end)) ;; Is COLOR a HSL? - ('css-hsl - (list (colorful--shorten-hex color-value) beg end)))) + ('css-hsl (list (colorful--shorten-hex color-value) beg end)))) ('name ; COLOR to NAME (pcase kind - ('color-name - "%s is already a color name. Try again: ") + ('color-name "%s is already a color name. Try again: ") ;; Is COLOR a Hex? ('hex - (if-let* ((rep (colorful--hex-to-name color-value))) - (list rep beg end))) + (if-let* ((color (colorful--hex-to-name color-value))) + (list color beg end))) ;; Is COLOR a CSS rgb? ('css-rgb - (if-let* ((rep (colorful--hex-to-name color-value))) - (list rep beg end))) + (if-let* ((color (colorful--hex-to-name color-value))) + (list color beg end))) ;; Is COLOR a HSL? ('css-hsl - (if-let* ((rep (colorful--hex-to-name color-value))) - (list rep beg end)))))))) + (if-let* ((color (colorful--hex-to-name color-value))) + (list color beg end)))))))) -(defun colorful--colorize-match (color beg end kind &optional _alpha-value) +(defun colorful--colorize-match (color beg end kind) "Overlay match with a face from BEG to END. The background uses COLOR color value. The foreground is obtained from `readable-foreground-color'." @@ -675,11 +556,6 @@ from `readable-foreground-color'." (colorful--name-to-hex color) color)) - ;; Set alpha value (if exists) as tag - ;; (when alpha-value - ;; (setq alpha-value (/ (string-to-number alpha-value) 100.0)) - ;; (overlay-put ov 'colorful--overlay-alpha alpha-value)) - ;; Enable auto deletion. (overlay-put ov 'evaporate t) @@ -708,8 +584,9 @@ from `readable-foreground-color'." (:background ,color) (:inherit colorful-base))))))) +;; FIXME: SLOW? (defmacro colorful--get-css-variable-color (regexp) - "Get color value from CSS variable matching REGEXP. + "Get CSS variable color value matching REGEXP from end to beginning. REGEXP must have a group that contains the color value." (declare (indent 1) (debug t)) `(save-excursion @@ -723,11 +600,10 @@ REGEXP must have a group that contains the color value." 'colorful--overlay-color)) (match-string-no-properties 1)))))) -(defun colorful--colorize (kind &optional match) +;; NOTE: Modify this functions for handle new colors added to this package. +(defun colorful--colorize (kind color beg end) "Helper function to colorize each KIND of MATCH with itself." - (when-let* ((match (or match 0)) - (color (match-string-no-properties match)) - ;; Check if match isn't blacklisted and is not in a comment ... + (when-let* (;; Check if match isn't blacklisted and is not in a comment ... ((and (not (member color colorful-exclude-colors)) (not (nth 4 (syntax-ppss))) ;; ... or is in a string ... @@ -742,38 +618,16 @@ REGEXP must have a group that contains the color value." (let* ((match-1 (match-string-no-properties 1)) (match-2 (match-string-no-properties 2)) - (match-3 (match-string-no-properties 3)) - ;; NOTE: There are no plans to support alpha coloring - ;; at the moment due to the problems it may cause with - ;; emacs background color. - ;; However, the code is ready in case there is motivation - ;; to do it. If so, uncomment this: - ;; (match-alpha (match-string-no-properties 4)) - (beg (match-beginning match)) - (end (match-end match))) + (match-3 (match-string-no-properties 3))) (pcase kind + ('hex + (setq beg (match-beginning 0)) + (setq end (match-end 0)) + (setq color (string-replace "0x" "#" color))) + ('color-name (setq color (colorful--name-to-hex color))) - ('css-rgb - (setq color (colorful--rgb-to-hex (colorful--percentage-to-absolute match-1) ; r - (colorful--percentage-to-absolute match-2) ; g - (colorful--percentage-to-absolute match-3)))) ; b - - ((and 'css-hsl - (guard (<= (string-to-number match-1) 360))) ; Ensure Hue is not greater than 360. - (setq color (colorful--hsl-to-hex match-1 match-2 match-3))) ; h s l - - ('css-oklab - (setq color (colorful--oklab-to-hex (/ (string-to-number match-1) 100.0) ; l - (string-to-number match-2) ; a - (string-to-number match-3)))) ; b - - ('css-oklch - (setq color (colorful--oklch-to-hex (/ (string-to-number match-1) 100.0) ; l - (string-to-number match-2) ; c - (float (string-to-number match-3))))) ; h - ('latex-rgb (setq color (if (string-prefix-p "{R" color) ; Check if it's RGB (shorted as "{R") @@ -793,25 +647,29 @@ REGEXP must have a group that contains the color value." (setq color (apply #'color-rgb-to-hex (color-hsl-to-rgb 0 0 (string-to-number match-1))))) - ('hex - (setq color (cond - ;; Check if hex is #RRGGBBAA or #RGBA and then - ;; ignore their Alpha hex values. - ((and (length= color 9) - (not (string-prefix-p "0x" color))) ; For #RRGGBBAA - (substring color 0 7)) - ((and (length= color 5) - (not (string-prefix-p "0x" color))) ; For #RGBA - (substring color 0 4)) - ;; Otherwise, just pass it. - (t (string-replace "0x" "#" color))))) + ('css-rgb + (setq color (colorful--rgb-to-hex (colorful--percentage-to-absolute match-1) ; r + (colorful--percentage-to-absolute match-2) ; g + (colorful--percentage-to-absolute match-3)))) ; b + + ((and 'css-hsl + (guard (<= (string-to-number match-1) 360))) ; Ensure Hue is not greater than 360. + (setq color (colorful--hsl-to-hex match-1 match-2 match-3))) ; h s l + + ('css-oklab + (setq color (colorful--oklab-to-hex (/ (string-to-number match-1) 100.0) ; l + (string-to-number match-2) ; a + (string-to-number match-3)))) ; b + + ('css-oklch + (setq color (colorful--oklch-to-hex (/ (string-to-number match-1) 100.0) ; l + (string-to-number match-2) ; c + (float (string-to-number match-3))))) ; h ('css-color-variable (cond ((and (string= match-1 "@") (not (string= match-2 "define_color"))) - ;; Find whole buffer for last @define-color match-1 found - ;; and get its color value. (colorful--get-css-variable-color (rx (seq "@define_color" (one-or-more space) @@ -825,54 +683,58 @@ REGEXP must have a group that contains the color value." ;; Ensure that string is a valid color and that string is non-nil (if (and color (color-defined-p color)) - (colorful--colorize-match color beg end kind)))) - - ;; The return value is not ignored, so be mindful what we return. - nil) + (colorful--colorize-match color beg end kind))))) + +;;; Fontify functions +(defun colorful-mode-fontify-region (start end) + ;; Clean up colorful overlays if found + (setq start (progn (goto-char start) (line-beginning-position))) + (setq end (progn (goto-char end) (line-end-position))) + + (save-excursion + (remove-overlays (or start (point-min)) (or end (point-max)) 'colorful--overlay t) + + (dolist (el colorful-color-keywords) + (let* ((keywords (car el)) + (type (nth 1 el)) + (match (or (nth 2 el) 0))) + (goto-char start) + (cond + ((stringp keywords) + (while (re-search-forward keywords end t) + (funcall #'colorful--colorize type (match-string-no-properties match) + (match-beginning match) (match-end match)))) + ((functionp keywords) + (while (funcall keywords end) + (funcall #'colorful--colorize type (match-string-no-properties match) + (match-beginning match) (match-end match))))))) + `(jit-lock-bounds ,start . ,end))) ;;;; Extra coloring definitions +;; Each element of these lists must be in the form: +;; (KEYWORDS TYPE MATCH) +;; +;; KEYWORDS must be a regexp string which contains the keywords +;; to highlight +;; +;; TYPE is a symbol which specifies the color type. +;; +;; MATCH is optional, must be a number which specifies the match to +;; use, if not set, it will use 0 instead. + ;;; Hex (defvar colorful-hex-font-lock-keywords - `((,(rx (seq (or bol (not "&")) - (group (or "#" "0x") (repeat 1 14 hex)) + `((,(rx (seq (group (or "#" "0x") (= 3 hex)) (opt hex) word-boundary)) - (1 (colorful--colorize 'hex 1))) - - (,(rx (seq (any "Rr") (any "Gg") (any "Bb") (opt (any "Ii")) ":" - (repeat 1 4 hex) "/" - (repeat 1 4 hex) "/" - (repeat 1 4 hex))) - (0 (colorful--colorize 'hex))) - - (,(rx (seq (or (seq (any "Cc") (any "Ii") (any "Ee") - (or (seq (any "Xx") (any "Yy") (any "Zz")) - (seq (any "Uu") (any "Vv") (any "Yy")) - (seq (any "Xx") (any "Yy") (any "Yy")) - (seq (any "Ll") (any "Aa") (any "Bb")) - (seq (any "Ll") (any "Uu") (any "Vv")))) - (seq (any "Tt") (any "Ee") (any "Kk") (any "Hh") - (any "Vv") (any "Cc"))) - ":" - (opt (any "+-")) - (one-or-more (any digit ".")) - (opt (any "Ee") - (opt (any "+-")) - (one-or-more digit)) - "/" - (opt (any "+-")) - (one-or-more (any digit ".")) - (opt (any "Ee") - (opt (any "+-")) - (one-or-more digit)) - "/" - (opt (any "+-")) - (one-or-more (any digit ".")) - (opt (any "Ee") - (opt (any "+-")) - (one-or-more digit)))) - (0 (colorful--colorize 'hex)))) - "Font-lock keywords to colorize.") + hex 1) + (,(rx (seq (group (or "#" "0x") (= 3 hex hex)) (opt hex hex) + word-boundary)) + hex 1) + (,(rx (seq (group "#" (= 12 hex)) + word-boundary)) + hex 1)) + "Font-lock keywords to add Hex colors (with alpha).") (defun colorful-add-hex-colors () "Add hex color highlighting. @@ -882,11 +744,17 @@ This is intended to be used with `colorful-extra-color-keyword-functions'." ;;; Color names +(defvar colorful--color-names-regexp + (regexp-opt (append + (mapcar #'car colorful-html-colors-alist) + (defined-colors)) + 'symbols)) + (defvar colorful-color-name-font-lock-keywords `((,(lambda (limit) (let ((case-fold-search t)) (re-search-forward colorful--color-names-regexp limit t))) - (0 (colorful--colorize 'color-name)))) + color-name)) "Font-lock keywords to add color names.") (defun colorful-add-color-names () @@ -900,11 +768,11 @@ This is intended to be used with `colorful-extra-color-keyword-functions'." (defvar colorful-css-variables-keywords `((,(rx (group "@") (group (one-or-more (any alphabetic "_")))) - (0 (colorful--colorize 'css-color-variable))) + css-color-variable) (,(rx (group "var") "(" (zero-or-more space) (group (one-or-more (any alphanumeric "-"))) (zero-or-more space) ")") - (0 (colorful--colorize 'css-color-variable)))) + css-color-variable)) "Font-lock keywords to add css user-defined colors.") (defun colorful-add-css-variables-colors () @@ -936,7 +804,7 @@ This is intended to be used with `colorful-extra-color-keyword-functions'." digit) (opt (or "%" (zero-or-more " "))))) ")")) - (0 (colorful--colorize 'css-rgb)))) + css-rgb)) "Font-lock keywords to add RGB colors.") (defun colorful-add-rgb-colors () @@ -968,7 +836,7 @@ This is intended to be used with `colorful-extra-color-keyword-functions'." digit) (opt (or "%" (zero-or-more " "))))) ")")) - (0 (colorful--colorize 'css-oklab))) + css-oklab) (,(rx (seq "oklch" "(" (zero-or-more " ") (group (repeat 1 3 digit) (opt "." (1+ digit)) @@ -987,7 +855,7 @@ This is intended to be used with `colorful-extra-color-keyword-functions'." digit) (opt (or "%" (zero-or-more " "))))) ")")) - (0 (colorful--colorize 'css-oklch)))) + css-oklch)) "Font-lock keywords to add OKLAB and OKLCH colors.") (defun colorful-add-oklab-oklch-colors () @@ -1013,7 +881,7 @@ This is intended to be used with `colorful-extra-color-keyword-functions'." digit) (opt (or "%" (zero-or-more " "))))) ")")) - (0 (colorful--colorize 'css-hsl)))) + css-hsl)) "Font-lock keywords to add HSL colors.") (defun colorful-add-hsl-colors () @@ -1029,11 +897,11 @@ This is intended to be used with `colorful-extra-color-keyword-functions'." (group (one-or-more (any digit "."))) (zero-or-more " ") "," (zero-or-more " ") (group (one-or-more (any digit "."))) (zero-or-more " ") "," (zero-or-more " ") (group (one-or-more (any digit "."))) (zero-or-more " ") "}")) - (0 (colorful--colorize 'latex-rgb))) + latex-rgb) (,(rx (seq "{HTML}{" (group (= 6 hex)) "}")) - (0 (colorful--colorize 'latex-HTML))) + latex-HTML) (,(rx (seq "{gray}{" (group (one-or-more (any digit "."))) "}")) - (0 (colorful--colorize 'latex-gray)))) + latex-gray)) "Font-lock keywords to add LaTeX rgb/RGB/HTML/Grey colors.") (defun colorful-add-latex-colors () @@ -1059,12 +927,11 @@ This is intended to be used with `colorful-extra-color-keyword-functions'." ((functionp fn) (funcall fn)))) - (push '(colorful--delete-overlays) colorful-color-keywords) - (font-lock-add-keywords nil colorful-color-keywords)) + (jit-lock-register #'colorful-mode-fontify-region)) (defun colorful--turn-off () "Helper function to clear colorful overlays." - (font-lock-remove-keywords nil colorful-color-keywords) + (jit-lock-unregister #'colorful-mode-fontify-region) (setq-local colorful-color-keywords nil) ; Clear list (remove-overlays nil nil 'colorful--overlay t)) @@ -1085,9 +952,7 @@ This is intended to be used with `colorful-extra-color-keyword-functions'." (unless (member (buffer-name) colorful-excluded-buffers) (if colorful-mode (colorful--turn-on) - (colorful--turn-off)) - ;; Refresh font-lock - (font-lock-flush))) + (colorful--turn-off)))) ;; Silence a byte-compile warning about global-colorful-modes not ;; being defined