-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcolorful-mode.el
More file actions
752 lines (660 loc) · 27 KB
/
colorful-mode.el
File metadata and controls
752 lines (660 loc) · 27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
;;; colorful-mode.el --- Preview any color in your buffer in real time -*- lexical-binding: t; -*-
;; Copyright (C) 2024-2026 Free Software Foundation, Inc
;; Author: Elias G. Perez <eg642616@gmail.com>
;; Maintainer: Shen, Jen-Chieh <jcs090218@gmail.com>
;; Elias G. Perez <eg642616@gmail.com>
;; Created: 2024-04-10
;; Package-Requires: ((emacs "28.1") (compat "30.1.0.0"))
;; Homepage: https://github.com/DevelopmentCool2449/colorful-mode
;; Keywords: faces, tools, matching, convenience
;; Version: 1.2.5
;; This file is part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; 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/inspired on `rainbow-mode.el'
;;; Code:
;;;; Libraries
(require 'compat)
(require 'color)
(require 'colorful-colors)
(eval-when-compile
(require 'subr-x)
(require 'rx)
(require 'cl-lib))
;;;; User Options
(defgroup colorful nil
"Preview color values in current buffer.."
:tag "Colorful mode"
:group 'faces
:group 'tools
:group 'matching)
(defface colorful-base
'((t (:weight bold :box (:line-width -1))))
"Face used as base for highlight color names.
Changing the background or foreground color will have no effect."
:group 'colorful)
(defcustom colorful-html-colors-alist
'(("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")
("blanchedalmond" . "#ffebcd")
("blueviolet" . "#8a2be2")
("brown" . "#a52a2a")
("burlywood" . "#deb887")
("cadetblue" . "#5f9ea0")
("chartreuse" . "#7fff00")
("chocolate" . "#d2691e")
("coral" . "#ff7f50")
("cornflowerblue" . "#6495ed")
("cornsilk" . "#fff8dc")
("crimson" . "#dc143c")
("darkblue" . "#00008b")
("darkcyan" . "#008b8b")
("darkgoldenrod" . "#b8860b")
("darkgray" . "#a9a9a9")
("darkgreen" . "#006400")
("darkgrey" . "#a9a9a9")
("darkkhaki" . "#bdb76b")
("darkmagenta" . "#8b008b")
("darkolivegreen" . "#556b2f")
("darkorange" . "#ff8c00")
("darkorchid" . "#9932cc")
("darkred" . "#8b0000")
("darksalmon" . "#e9967a")
("darkseagreen" . "#8fbc8f")
("darkslateblue" . "#483d8b")
("darkslategray" . "#2f4f4f")
("darkslategrey" . "#2f4f4f")
("darkturquoise" . "#00ced1")
("darkviolet" . "#9400d3")
("deeppink" . "#ff1493")
("deepskyblue" . "#00bfff")
("dimgray" . "#696969")
("dimgrey" . "#696969")
("dodgerblue" . "#1e90ff")
("firebrick" . "#b22222")
("floralwhite" . "#fffaf0")
("forestgreen" . "#228b22")
("gainsboro" . "#dcdcdc")
("ghostwhite" . "#f8f8ff")
("gold" . "#ffd700")
("goldenrod" . "#daa520")
("greenyellow" . "#adff2f")
("grey" . "#808080")
("honeydew" . "#f0fff0")
("hotpink" . "#ff69b4")
("indianred" . "#cd5c5c")
("indigo" . "#4b0082")
("ivory" . "#fffff0")
("khaki" . "#f0e68c")
("lavender" . "#e6e6fa")
("lavenderblush" . "#fff0f5")
("lawngreen" . "#7cfc00")
("lemonchiffon" . "#fffacd")
("lightblue" . "#add8e6")
("lightcoral" . "#f08080")
("lightcyan" . "#e0ffff")
("lightgoldenrodyellow" . "#fafad2")
("lightgray" . "#d3d3d3")
("lightgreen" . "#90ee90")
("lightgrey" . "#d3d3d3")
("lightpink" . "#ffb6c1")
("lightsalmon" . "#ffa07a")
("lightseagreen" . "#20b2aa")
("lightskyblue" . "#87cefa")
("lightslategray" . "#778899")
("lightslategrey" . "#778899")
("lightsteelblue" . "#b0c4de")
("lightyellow" . "#ffffe0")
("limegreen" . "#32cd32")
("linen" . "#faf0e6")
("mediumaquamarine" . "#66cdaa")
("mediumblue" . "#0000cd")
("mediumorchid" . "#ba55d3")
("mediumpurple" . "#9370db")
("mediumseagreen" . "#3cb371")
("mediumslateblue" . "#7b68ee")
("mediumspringgreen" . "#00fa9a")
("mediumturquoise" . "#48d1cc")
("mediumvioletred" . "#c71585")
("midnightblue" . "#191970")
("mintcream" . "#f5fffa")
("mistyrose" . "#ffe4e1")
("moccasin" . "#ffe4b5")
("navajowhite" . "#ffdead")
("oldlace" . "#fdf5e6")
("olivedrab" . "#6b8e23")
("orangered" . "#ff4500")
("orchid" . "#da70d6")
("palegoldenrod" . "#eee8aa")
("palegreen" . "#98fb98")
("paleturquoise" . "#afeeee")
("palevioletred" . "#db7093")
("papayawhip" . "#ffefd5")
("peachpuff" . "#ffdab9")
("peru" . "#cd853f")
("pink" . "#ffc0cb")
("plum" . "#dda0dd")
("powderblue" . "#b0e0e6")
("rosybrown" . "#bc8f8f")
("royalblue" . "#4169e1")
("saddlebrown" . "#8b4513")
("salmon" . "#fa8072")
("sandybrown" . "#f4a460")
("seagreen" . "#2e8b57")
("seashell" . "#fff5ee")
("sienna" . "#a0522d")
("skyblue" . "#87ceeb")
("slateblue" . "#6a5acd")
("slategray" . "#708090")
("slategrey" . "#708090")
("snow" . "#fffafa")
("springgreen" . "#00ff7f")
("steelblue" . "#4682b4")
("tan" . "#d2b48c")
("thistle" . "#d8bfd8")
("tomato" . "#ff6347")
("turquoise" . "#40e0d0")
("violet" . "#ee82ee")
("wheat" . "#f5deb3")
("whitesmoke" . "#f5f5f5")
("yellowgreen" . "#9acd32")
("rebeccapurple" . "#663399"))
"Alist of HTML colors.
Each entry should have the form (COLOR-NAME . HEXADECIMAL-COLOR)."
:type 'alist)
(defcustom colorful-extra-color-keyword-functions
'(colorful-add-hex-colors
(emacs-lisp-mode . colorful-add-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))
(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),
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-hex-colors'
- `colorful-add-color-names'
- `colorful-add-css-variables-colors'
- `colorful-add-rgb-colors'
- `colorful-add-hsl-colors'
- `colorful-add-oklab-oklch-colors'
- `colorful-add-latex-colors'"
:type '(repeat
(choice (cons (choice :tag "Mode(s)" symbol (repeat symbol))
(choice :tag "Function(s)" (repeat function)
function))
function)))
;; TODO: (define-obsolete-variable-alias colorful-extra-color-keyword-functions colorful-color-functions "1.3.0")
(defcustom colorful-allow-mouse-clicks t
"If non-nil, allow using mouse buttons to change color."
:type 'boolean)
(defcustom colorful-use-prefix nil
"If non-nil, use a prefix to preview color instead of highlighting them."
:type 'boolean)
;; (defcustom colorful-use-indicator nil
;; "If non-nil, use a string as indicator instead highlight.
;; :type 'boolean)
;; (define-obsolete-variable-alias colorful-use-prefix colorful-use-indicator "1.3.0")
(defcustom colorful-prefix-string "●"
"String to be used in highlights.
Only relevant if `colorful-use-prefix' is non-nil."
:type 'string)
;; (define-obsolete-variable-alias colorful-prefix-string colorful-indicator-string "1.3.0")
(defcustom colorful-prefix-alignment 'left
"The position to place the prefix string.
The value can be `left' or `right'.
Only relevant if `colorful-use-prefix' is non-nil."
:type '(choice (const :tag "Left" left)
(const :tag "Right" right)))
;; (defcustom colorful-indicator-alignment 'left
;; "The position to place the indicator string.
;; The value can be `left' or `right'.
;; Only relevant if `colorful-use-prefix' is non-nil."
;; :type '(choice (const :tag "Left" left)
;; (const :tag "Right" right)))
;; (define-obsolete-variable-alias colorful-prefix-alignment colorful-indicator-alignment "1.3.0")
(defcustom colorful-exclude-colors '("#define")
"List of keywords not to highlight."
:type '(repeat string))
(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."
:type '(repeat string))
(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.
Setting this to non-nil can make converted hex inaccurate."
:type 'boolean)
(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."
: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
;;;;; Base Conversion functions
(defun colorful--percentage-to-absolute (percentage)
"Return PERCENTAGE as an absolute number.
PERCENTAGE must be a string.
If PERCENTAGE is absolute, return PERCENTAGE as a number.
This will convert \"80%\" to 204, \"100%\" to 255 but not \"123\".
If PERCENTAGE is above 100%, it is converted to 100."
(if (string-suffix-p "%" percentage)
(min (max 0 (* 255 (/ (string-to-number percentage) 100.0))) 255)
(string-to-number percentage)))
(defun colorful--short-hex (hex)
"Convert a 12-digit hexadecimal color form to a 6-digit (#RRGGBB) form.
HEX should be a string in the format `#RRRRGGGGBBBB' (12-digit form).
The conversion is controlled by `colorful-short-hex-conversions'. If
`colorful-short-hex-conversions' is set to nil, then just return HEX."
(if (and colorful-short-hex-conversions
(length> hex 7))
(let ((r (substring hex 1 5))
(g (substring hex 5 9))
(b (substring hex 9 13)))
(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--hsl-to-hex (h s l)
"Return CSS H S L as hexadecimal format."
(if-let* ((h (cond
((string-suffix-p "grad" h)
(/ (string-to-number h) 400.0))
((string-suffix-p "rad" h)
(/ (string-to-number h) (* 2 float-pi)))
(t (/ (string-to-number h) 360.0))))
(s (/ (string-to-number s) 100.0))
(l (/ (string-to-number l) 100.0)))
(apply #'color-rgb-to-hex (color-hsl-to-rgb h s l))))
(defun colorful--oklab-to-hex (l a b)
"Convert OKLab color (L, A, B) to HEX format.
L A and B must be strings."
(let* ((l (if (not (seq-contains-p l ?%))
(string-to-number l)
(/ (string-to-number l) 100.0)))
(a (string-to-number a))
(b (string-to-number b))
(rgb (mapcar #'color-clamp (color-oklab-to-srgb l a b))))
(apply #'color-rgb-to-hex rgb)))
(defun colorful--oklch-to-hex (l c h)
"Convert OKLCH color (L, C, H) to HEX format.
L C and H must be strings."
(let* ((l (if (not (seq-contains-p l ?%))
(string-to-number l)
(/ (string-to-number l) 100.0)))
(c (string-to-number c))
(h (float (string-to-number h)))
;; Convert to LAB
(h-rad (* h (/ float-pi 180.0)))
(a (* c (cos h-rad)))
(b (* c (sin h-rad)))
;; Convert to RGB
(rgb (mapcar #'color-clamp (color-oklab-to-srgb l a b))))
;; Return HEX
(apply #'color-rgb-to-hex rgb)))
(defun colorful--hex-to-name (hex)
"Return HEX as color name."
(car (or (rassoc (color-values-from-color-spec hex) color-name-rgb-alist)
(rassoc hex colorful-html-colors-alist))))
(defun colorful--name-to-hex (name)
"Return color NAME as hex color format."
(if (color-defined-p name)
(apply #'format "#%04x%04x%04x" (color-values name))
(cdr (assoc-string name colorful-html-colors-alist t))))
;;;;; Overlay functions
(defun colorful--find-overlay (&optional beg)
"Return colorful overlay if found at current point.
BEG is the position to check for the overlay."
(cl-dolist (ov (overlays-at (or beg (point))))
(if (overlay-get ov 'colorful--overlay)
(cl-return ov))))
;;;; User Interactive Functions
(defun colorful-convert-and-change-color (&optional beg end)
"Convert color at point or colors in region to another format."
(interactive
(progn (barf-if-buffer-read-only)
(if (use-region-p)
(list (region-beginning) (region-end)))))
;; 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.
(choice (alist-get
(completing-read "Change colors in region: " choices nil t nil nil)
choices nil nil 'equal))
;; Define counters
(ignored-colors 0)
(changed-colors 0))
(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))
((consp result))
(range (cdr result)) ; Get the positions where it should be replaced.
(start (car range))
(end (cadr range))
(new-color (car result)))
(progn
(replace-region-contents start end new-color 0)
(setq changed-colors (1+ changed-colors)))
(setq ignored-colors (1+ ignored-colors)))))
(if (and (zerop changed-colors)
(zerop ignored-colors))
(message "No color found in region.")
(message (concat (propertize "Changed colors: %d" 'face 'success) " / "
(propertize "Ignored colors: %d" 'face 'error))
changed-colors ignored-colors)))
;; 2# Case: replace only the color at point
(if-let* ((colorful-ov (colorful--find-overlay)) ; Find colorful overlay tag at point/cursor.
;; Start prompt for color change and get new color.
(result (colorful--prompt-converter colorful-ov "Change '%s' to: "))
(new-color (car result))
;; Get the positions where it should be replaced.
(range (cdr result))
(start (car range))
(end (cadr range)))
;; Replace color at point.
(replace-region-contents start end new-color 0)
;; Otherwise throw error.
(user-error "No color found"))))
(defun colorful-convert-and-copy-color ()
"Convert color at point to another format and copy it to the kill ring."
(interactive)
(if-let* ((colorful-ov (colorful--find-overlay)) ; Find colorful overlay tag at point/cursor.
;; Start prompt for color change and get new color.
(result (car (colorful--prompt-converter colorful-ov "Copy '%s' as: ")))
;; Propertize text for message.
(color (propertize result 'face `(:foreground
,(readable-foreground-color result)
:background ,result))))
;; Copy color and notify to user it's done
(progn (kill-new color)
(message "`%s' copied." color))
;; Otherwise throw an error.
(user-error "No color found")))
(defun colorful-change-or-copy-color ()
"Change or copy color at point to another format."
(interactive)
(let* ((prompt "Please type an option: ")
(choices '(("Convert and change color." . convert)
("Convert and copy color." . copy)))
(result (alist-get
(completing-read prompt choices nil t nil nil)
choices nil nil #'equal)))
(if (eq result 'copy)
(colorful-convert-and-copy-color)
(colorful-convert-and-change-color))))
;;;;; Coloring functions
(defun colorful--prompt-converter (ov prompt &optional beg end color)
"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.
(color (or color (buffer-substring-no-properties beg end)))
(prompt (format prompt color))
(choices '(("Hexadecimal color format" . hex)
("Color name" . color-name)))
;; Get choice.
(choice (alist-get
(completing-read prompt choices nil t nil nil)
choices nil nil 'equal))
(converted-color (colorful--converter ov choice)))
(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)))
(defun colorful--converter (ov choice)
"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.
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
(if (eq kind 'color-name)
(colorful--name-to-hex color-value)
color-value))
beg end))
('color-name ; color to NAME
(if-let* ((color (colorful--hex-to-name color-value)))
(list color beg end))))))
(defun colorful--colorize-match (color beg end kind face map)
"Overlay match with a face from BEG to END.
The background uses COLOR color value. The foreground is obtained
from `readable-foreground-color'."
(let ((ov (make-overlay beg end)))
(overlay-put ov 'colorful--overlay t) ; Define colorful overlay tag
(overlay-put ov 'colorful--color-kind kind) ; Set kind tag
(overlay-put ov 'colorful--color color) ; Set color value as tag
(overlay-put ov 'evaporate t) ; Enable auto deletion.
(cond
(colorful-use-prefix
(overlay-put ov
(if (eq colorful-prefix-alignment 'left)
'before-string
'after-string)
(apply #'propertize
colorful-prefix-string
`( face ,face
,@(when colorful-allow-mouse-clicks
`( mouse-face highlight
keymap ,map)))))
;; Use no face for matched color
(overlay-put ov 'face nil))
(t
(when colorful-allow-mouse-clicks
(overlay-put ov 'mouse-face 'highlight)
(overlay-put ov 'keymap map))
(overlay-put ov 'face face)))))
(defun colorful--colorize (kind color beg end function)
"Helper function to colorize each KIND of MATCH with itself.
KIND is the color type.
COLOR is the string which contains the color matched.
BEG and END are color match positions.
FUNCTION is a function to call to get the respective color
and positions to colorize."
(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* ((return (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))
(end (if (consp return) (nth 2 return) end))
(face (if colorful-use-prefix
(list :foreground color)
(list
:foreground (readable-foreground-color color)
:background color
:inherit 'colorful-base)))
;; Make the function for the mouse clicks
(map (when colorful-allow-mouse-clicks
`(keymap
(mouse-1
. ,(lambda ()
(interactive)
(save-excursion
(goto-char beg)
(call-interactively
(if buffer-read-only
#'colorful-convert-and-copy-color
#'colorful-change-or-copy-color)))))))))
(colorful--colorize-match color beg end kind face map))))))
;;; Fontify functions
(defun colorful-mode-fontify-region (start end)
;; Clean up colorful overlays if found
(setq start (progn (goto-char start) (line-beginning-position))
end (progn (goto-char end) (line-end-position)))
(remove-overlays start end 'colorful--overlay t)
(dolist (el colorful-color-keywords)
(let* ((keywords (plist-get el :keywords))
(type (plist-get el :type))
(match (or (plist-get el :match) 0))
(ignore-case (plist-get el :case))
(function (plist-get el :function)))
(goto-char start)
(cond
((stringp keywords)
(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)
(colorful--colorize type (match-string-no-properties match)
(match-beginning match) (match-end match)
function)))))))
`(jit-lock-bounds ,start . ,end))
;;;; Minor mode definitions
(defun colorful--turn-on ()
"Helper function to turn on `colorful-mode'."
;; Run functions from `colorful-extra-color-keyword-functions' list
;; for add keywords to `colorful-color-keywords'.
(dolist (fn colorful-extra-color-keyword-functions)
(cond
;; Enable some highlighting for some major modes defined in
;; `colorful-extra-color-keyword-functions'
((and (listp fn)
;; For emacs < 28.1 compatibility (see: github#19)
(seq-some #'derived-mode-p (ensure-list (car fn))))
(dolist (fn-list (ensure-list (cdr fn)))
(funcall fn-list)))
;; If current major mode is not derived from any mode defined in
;; `colorful-extra-color-keyword-functions', use the
;; functions without the cons.
((functionp fn)
(funcall fn))))
(jit-lock-register #'colorful-mode-fontify-region))
(defun colorful--turn-off ()
"Helper function to clear colorful overlays."
(jit-lock-unregister #'colorful-mode-fontify-region)
(setq-local colorful-color-keywords nil) ; Clear list
(remove-overlays nil nil 'colorful--overlay t))
;;;; Keymap
(defvar-keymap colorful-mode-map
:doc "Keymap for `colorful-mode'."
"C-x c x" #'colorful-change-or-copy-color
"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
(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
;; NOTE: This bug is already fixed in emacs-30
(defvar global-colorful-modes)
;;;###autoload
(defun turn-on-colorful-mode ()
"Turn on `colorful-mode' mode in the current buffer."
(unless colorful-mode
(colorful-mode t)))
;;;###autoload
(define-globalized-minor-mode global-colorful-mode
colorful-mode turn-on-colorful-mode
:predicate '(prog-mode help-mode html-mode css-mode latex-mode))
(provide 'colorful-mode)
;;; colorful-mode.el ends here