summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElías Gabriel Pérez <eg642616@gmail.com>2025-05-08 20:01:05 -0600
committerElías Gabriel Pérez <eg642616@gmail.com>2025-05-27 20:26:32 -0600
commit106731d545a71fb06408e2f2b39e8b70dcacc68e (patch)
treed7b2081c165dffd0c1daeb10367987c597d9209d
parent3ddd2286fddaaba48eefc7ce873c8a777984376a (diff)
Move coloring in strings to minor mode definition
* colorful-mode.el (colorful-highlight-in-comments): New user option. (github #18) (colorful--highlight): New buffer-local variable. (colorful--colorize): Use `when' instead `when-let*' and move whether coloring must be done in strings... (colorful-mode): ... to here.
-rw-r--r--colorful-mode.el70
1 files changed, 48 insertions, 22 deletions
diff --git a/colorful-mode.el b/colorful-mode.el
index eac1f40..b0e1ef3 100644
--- a/colorful-mode.el
+++ b/colorful-mode.el
@@ -297,7 +297,7 @@ In case colorful breaks a buffer, such as a buffer
derived from `help-mode', this option can be useful for you."
:type '(repeat string))
-;; (make-obsolete-variable colorful-excluded-buffers nil "1.3.0")
+;; (make-obsolete-variable colorful-excluded-buffers nil "1.2.4")
(defcustom colorful-short-hex-conversions t
"If non-nil, colorful will converted long hex colors to \"#RRGGBB\" format.
@@ -307,16 +307,25 @@ Setting this to non-nil can make converted hex inaccurate."
(defcustom colorful-only-strings nil
"If non-nil, colorful will only highlight colors inside strings.
If set to `only-prog', the colors in `prog-mode' will be highlighted
-only if they are inside a string.
-This doesn't include `css-mode' and derived."
+only if they are inside a string, this doesn't include `css-mode' and
+derived."
:type '(choice boolean (const :tag "Only in prog-modes" only-prog)))
+(defcustom colorful-highlight-in-comments nil
+ "If non-nil, colorful will highlight colors inside comments.
+NOTE: If this is set, this will highlight any keyword within the
+comments, including color names, which can be annoying."
+ :type 'boolean)
+
;;;; Internal variables
(defvar-local colorful-color-keywords nil
"Font-lock colors keyword to highlight.")
+(defvar-local colorful--highlight nil
+ "Internal variable used for check when the highlighting must be done.")
+
;;;; Internal Functions
@@ -603,20 +612,23 @@ POS is the position where start the search."
KIND is the color type.
COLOR is the string which contains the color matched.
BEG and END are color match positions."
- (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)))
- ;; ... and wheter color is in a string according colorful-only-strings...
- (or (not colorful-only-strings)
- (and colorful-only-strings
- (nth 3 (syntax-ppss)))
- (and (eq colorful-only-strings 'only-prog)
- (or (not (derived-mode-p 'prog-mode))
- ;; CSS is prog-mode derived so ignore only-strings
- ;; in CSS derived modes.
- (derived-mode-p 'css-mode)
- (and (derived-mode-p 'prog-mode)
- (nth 3 (syntax-ppss)))))))))
+ (when
+ (and
+ ;; Check if match isn't blacklisted ...
+ (not (member color colorful-exclude-colors))
+ ;; ... and color is in a comment according
+ ;; colorful-highlight-in-comments ...
+ (or colorful-highlight-in-comments (not (nth 4 (syntax-ppss))))
+ ;; ... and wheter color is in a string according colorful-only-strings.
+ (or (not colorful-only-strings)
+ (when (or (eq colorful--highlight 'prog)
+ (eq colorful-only-strings t))
+ (if colorful-highlight-in-comments
+ ;; Highlight only for strings and comments
+ (syntax-ppss-context (syntax-ppss))
+ ;; Highlight only for strings
+ (nth 3 (syntax-ppss))))
+ (eq colorful--highlight t)))
(let* ((match-1 (match-string-no-properties 1))
(match-2 (match-string-no-properties 2))
@@ -963,15 +975,29 @@ This is intended to be used with `colorful-extra-color-keyword-functions'."
"C-x c c" #'colorful-convert-and-copy-color
"C-x c r" #'colorful-convert-and-change-color)
+
+;;;; Minor mode definition
+
;;;###autoload
(define-minor-mode colorful-mode
"Preview any color in your buffer such as hex, color names, CSS rgb in real time."
:global nil
- ;; Do not activate it in these buffers.
- (unless (member (buffer-name) colorful-excluded-buffers)
- (if colorful-mode
- (colorful--turn-on)
- (colorful--turn-off))))
+ (if colorful-mode
+ (progn
+ ;; If `colorful-only-strings' is set to `only-prog', check if
+ ;; the current major mode is derived from prog-mode for
+ ;; highlight in strings, otherwise highlight wherever.
+ (if (eq colorful-only-strings 'only-prog)
+ (cond
+ ;; CSS is prog-mode derived so ignore only-strings
+ ;; in CSS derived modes.
+ ((or (derived-mode-p 'css-mode)
+ (not (derived-mode-p 'prog-mode)))
+ (setq colorful--highlight t))
+ ((derived-mode-p 'prog-mode)
+ (setq colorful--highlight 'prog))))
+ (colorful--turn-on))
+ (colorful--turn-off)))
;; Silence a byte-compile warning about global-colorful-modes not
;; being defined