-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmode-icons.el
1904 lines (1734 loc) · 81.7 KB
/
mode-icons.el
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; mode-icons.el --- Show icons for modes -*- lexical-binding: t; -*-
;; Copyright (C) 2013, 2016 Tom Willemse
;; 2016 Matthew L. Fidler
;; Author: Tom Willemse <[email protected]>
;; Keywords: multimedia
;; Version: 0.4.0
;; URL: http://ryuslash.org/projects/mode-icons.html
;; Package-Requires: ((emacs "24") (cl-lib "0.5"))
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides a globalized minor mode that replaces the
;; major mode name in your mode-line and places like Ibuffer with an
;; icon. Currently the following programming modes are supported,
;; among others:
;;
;; - C
;; - C++
;; - C#
;; - CSS
;; - Coffee
;; - Dart
;; - Emacs-Lisp
;; - HTML
;; - Haml
;; - JavaScript
;; - Lisp
;; - Lua
;; - nXML
;; - PHP
;; - Python
;; - React
;; - Ruby
;; - Rust
;; - Sass/Scss
;; - Scheme
;; - Shell-script
;; - Slim
;; - Snippet
;; - Web
;; - Yaml
;;
;; To enable this minor mode add the following line to your init file:
;;
;; (mode-icons-mode)
;;
;; As of version 0.3.0 this project includes some icons which can use icon
;; fonts instead of images. These fonts are:
;;
;; - Font Awesome, found at URL `http://fontawesome.io/'.
;; - GitHub Octicons, found at URL `https://octicons.github.com/'.
;; - Font Mfizz, found at URL `http://fizzed.com/oss/font-mfizz'.
;; - IcoMoon, found at URL `https://icomoon.io/#icons-icomoon'.
;;
;;; Code:
(declare-function comint-send-string "comint")
(declare-function emojify-set-emoji-data "emojify")
(declare-function ht-get "ht")
(declare-function powerline-minor-modes "powerline")
(declare-function powerline-raw "powerline-raw")
(declare-function pl/add-text-property "powerline")
(declare-function mode-icons--real-powerline-raw "powerline")
(declare-function mode-icons--powerline-raw "mode-icons")
(declare-function mode-icons--real-powerline-major-mode "powerline")
(declare-function mode-icons--powerline-major-mode "mode-icons")
(require 'cl-lib)
(require 'color)
(require 'emojify nil t)
(defgroup mode-icons nil
"Provide icons for major modes."
:group 'editing-basics
:group 'convenience)
(defconst mode-icons--directory
(if load-file-name
(file-name-directory load-file-name)
default-directory)
"Where mode-icons was loaded from.")
(defun mode-icons-get-icon-file (icon)
"Get the location of ICON.
ICON should be a file name with extension. The result is the
absolute path to ICON."
(expand-file-name icon (expand-file-name "icons" mode-icons--directory)))
(defmacro mode-icons-save-buffer-state (&rest body)
"Eval BODY saving buffer state.
This macro restores the buffer state under the assumption that no
significant modification has been made in BODY. A change is
considered significant if it affects the buffer text in any way
that isn't completely restored again. Changes in text properties
like `face' or `syntax-table' are considered insignificant. This
macro allows text properties to be changed, even in a read-only
buffer.
This macro should be placed around all calculations which set
\"insignificant\" text properties in a buffer, even when the buffer is
known to be writeable. That way, these text properties remain set
even if the user undoes the command which set them.
This macro should ALWAYS be placed around \"temporary\" internal buffer
changes \(like adding a newline to calculate a text-property then
deleting it again\), so that the user never sees them on his
`buffer-undo-list'.
However, any user-visible changes to the buffer \(like auto-newlines\)
must not be within a `ergoemacs-save-buffer-state', since the user then
wouldn't be able to undo them.
The return value is the value of the last form in BODY.
This was stole/modified from `c-save-buffer-state'"
`(let* ((modified (buffer-modified-p)) (buffer-undo-list t)
(inhibit-read-only t) (inhibit-point-motion-hooks t)
before-change-functions after-change-functions
deactivate-mark
buffer-file-name buffer-file-truename ; Prevent primitives checking
; for file modification
)
(unwind-protect
(progn ,@body)
(and (not modified)
(buffer-modified-p)
(set-buffer-modified-p nil)))))
(defcustom mode-icons
`(("\\`CSS\\'" "css" xpm)
("\\`Coffee\\'" "coffee" xpm-bw)
("\\`Compilation\\'" "compile" xpm)
("\\`Dart\\'" "dart" xpm)
("\\`Flutter\\'" "flutter" xpm)
("\\`Elixir\\'" "elixir" xpm)
("\\`Erlang\\'" "erlang" xpm)
("\\`Emacs-Lisp\\'" "emacs" xpm)
("\\`Lisp Interaction\\'" "emacs" xpm)
("\\`HTML\\'" "html" xpm)
("\\`Haml\\'" "haml" xpm)
("\\`Image\\[imagemagick\\]\\'" "svg" xpm)
("\\`Inf-Ruby\\'" "infruby" xpm)
("\\`Java[Ss]cript\\'" "js" xpm)
("\\`Lisp\\'" "cl" xpm)
("\\`Lua\\'" "Lua-Logo_16x16" png)
("\\`nXML\\'" "xml" xpm)
("\\`Org\\'" "org" xpm)
("\\`PHP\\(\\|/.*\\)\\'" "php" xpm)
("\\`Projectile Rails Server\\'" "rails" xpm)
("\\`Python\\'" "python" xpm)
("\\` Emmet\\'" "emmet" xpm)
("\\`RJSX\\'" "react" xpm)
("\\`Ruby\\'" "ruby" xpm)
("\\`Rust\\'" "rust" xpm)
("\\`EnhRuby\\'" "ruby" xpm)
("\\`ESS\\[S\\]\\'" "R" xpm)
("\\`ESS\\[SAS\\]\\'" "sas" xpm)
("\\`ESS\\[BUGS\\]\\'" #xf188 FontAwesome)
("\\`iESS\\'" "R" xpm)
("\\`SCSS\\'" "sass" xpm)
("\\`Sass\\'" "sass" xpm)
("\\`Scheme" "scheme" xpm-bw)
("\\`Shell-script" "bash" xpm-bw)
("\\`Slim" "slim" xpm-bw)
("\\`Snippet" "yas" xpm)
("\\`Term\\'" "term" xpm)
("\\`Web\\'" "html" xpm)
("\\`XML\\'" "xml" xpm)
("\\`YAML\\'" "yaml" xpm)
("\\` ?YASnippet\\'" "yas" xpm)
("\\` ?yas\\'" "yas" xpm)
("\\` ?hs\\'" "hs" xpm)
("\\`Markdown\\'" #xf0c9 github-octicons)
("\\`GFM\\'" #xf0c9 github-octicons)
("\\`Scala\\'" #xf15b font-mfizz)
("\\`Magit\\'" #xf1d2 FontAwesome)
("\\` Pulls\\'" #xf092 FontAwesome)
("\\`Zip-Archive\\'" #xf1c6 FontAwesome)
("\\` ARev\\'" #xf021 FontAwesome)
("\\`Calc\\(ulator\\)?\\'" #xf1ec FontAwesome)
("\\`Debug.*\\'" #xf188 FontAwesome)
("\\`Debug.*\\'" #xf188 FontAwesome)
("\\`Calendar\\'" #xf073 FontAwesome)
("\\`Help\\'" #xf059 FontAwesome)
("\\`WoMan\\'" #xf05a FontAwesome)
("\\`C\\(/.*\\|\\)\\'" "c" xpm)
("\\`Custom\\'" #xf013 FontAwesome)
("\\`Go\\'" "go" xpm)
("\\` ?Rbow\\'" "rainbow" xpm)
("\\` ?ivy\\'" "ivy" xpm) ;; Icon created by Philipp Lehmann from the Noun Project https://thenounproject.com/search/?q=ivy&i=329756
("\\` ?ICY\\'" "icy" xpm) ;; http://www.clipartpal.com/clipart_pd/weather/ice_10206.html
("\\` ?Golden\\'" "golden" xpm-bw) ;; Icon created by Arthur Shlain from Noun Project
("\\`BibTeX\\'\\'" "bibtex" xpm-bw)
("\\`C[+][+]\\(/.*\\|\\)\\'" "cpp" xpm)
("\\`C[#]\\(/.*\\|\\)\\'" "csharp" xpm)
("\\`Haskell\\'" #xf126 font-mfizz)
("\\`Clojure\\'" #xf10b font-mfizz)
("\\`Java\\(/.*\\|\\)\\'" #xf12b font-mfizz)
("\\`C?Perl\\'" #xf148 font-mfizz)
("\\`Octave\\'" "octave" xpm)
("\\`AHK\\'" "autohotkey" xpm)
("\\`Info\\'" #xf05a FontAwesome)
("\\` ?Narrow\\'" #xf066 FontAwesome)
("\\`Dockerfile\\'" "docker" xpm)
("\\`Spacemacs buffer\\'" "spacemacs" png)
("\\` ?emoji\\'" "emoji" png)
("\\`Org-Agenda" #xf046 FontAwesome)
("\\`PS\\'" "powershell" xpm)
(mode-icons-powershell-p "powershell" xpm)
(mode-icons-cmd-p "cmd" xpm-bw)
(mode-icons-msys-p "msys" xpm)
(mode-icons-cygwin-p "cygwin" xpm)
(read-only #xf023 FontAwesome)
(writable #xf09c FontAwesome)
(save #xf0c7 FontAwesome)
(saved "" nil)
(modified-outside #xf071 FontAwesome)
(steal #xf21b FontAwesome)
;; Prefer finder icon since it looks like the old mac icon
(apple #xeabf IcoMoon-Free)
(apple #xf179 FontAwesome)
(win #xf17a FontAwesome)
;; FIXME: use lsb_release to determine Linux variant and choose appropriate icon
(unix #xeabd IcoMoon-Free) ;; Clear Tux (Unlike FontAwesome)
;; This icon is clearer than FontAwesome's Linux Penguin
(unix #xf166 font-mfizz) ;; Use ubuntu, since I think it is the most common.
(unix #xf17c FontAwesome) ;; Fall Back to FontAwesome
(undecided #xf128 FontAwesome)
("Text\\'" #xf0f6 FontAwesome)
("\\` ?company\\'" #xf1ad FontAwesome)
("\\` ?AC\\'" #xf18e FontAwesome)
("\\` ?Fly\\'" #xea12 IcoMoon-Free)
;; ("\\` ?FlyC.*\\'" "flycheck" xpm)
("\\` ?SP\\(/s\\)?\\'" "smartparens" xpm)
("\\` ?Ergo" #xf11c FontAwesome)
("\\` ?drag\\'" #xf047 FontAwesome)
("\\` ?Helm\\'" "helm" xpm-bw) ;; By Noe Araujo, MX, https://thenounproject.com/term/helm/233101/
("\\`Messages\\'" #xf27b FontAwesome)
("\\`Conf" #xf1de FontAwesome)
("\\`Fundamental\\'" #xf016 FontAwesome)
("\\`Javascript-IDE\\'" "js" xpm)
("\\` Undo-Tree\\'" ":palm_tree:" emoji)
("\\`LaTeX\\'" "tex" ext)
("\\`Image\\[xpm\\]\\'" "xpm" ext)
("\\`Image\\[png\\]\\'" "png" ext)
("\\` ?AI\\'" #xf03c FontAwesome)
("\\` ?Isearch\\'" #xf002)
(default #xf059 FontAwesome)
;; Diminished modes
("\\` ?\\(?:ElDoc\\|Anzu\\|SP\\|Guide\\|PgLn\\|Undo-Tree\\|Ergo.*\\|,\\|Isearch\\|Ind\\)\\'" nil nil))
"Icons for major and minor modes.
Each specificatioun is a list with the first element being the
name of the major mode. The second the name of the icon file,
without the extension. And the third being the type of icon."
:type '(repeat
(list (choice
(string :tag "Regular Expression")
(const :tag "Read Only Indicator" read-only)
(const :tag "Writable Indicator" writable)
(const :tag "Saved" saved)
(const :tag "Save" save)
(const :tag "Modified Outside Emacs" modified-outside)
(const :tag "Locked By Someone Else" steal)
(const :tag "Apple" apple)
(const :tag "Windows" win)
(const :tag "Unix" unix)
(const :tag "Default Icon" default)
(function :tag "Enriched minor mode"))
(choice
(string :tag "Icon Name")
(integer :tag "Font Glyph Code")
(const :tag "ess" nil))
(choice
(const :tag "text" nil)
(const :tag "png" png)
(const :tag "gif" gif)
(const :tag "jpeg" jpeg)
(const :tag "jpg" jpg)
(const :tag "xbm" xbm)
(const :tag "xpm" xpm)
(const :tag "Black and White xpm that changes color to match the mode-line face" xpm-bw)
(const :tag "Emoji" emoji)
(const :tag "Mode Icons Generated file-type" ext)
(symbol :tag "Font"))))
:group 'mode-icons)
(defun mode-icons-powershell-p (&optional match)
"Is the current mode a powershell process?"
(let ((proc (get-buffer-process (current-buffer))))
(and proc (string-match-p (or match "powershell") (car (process-command proc))))))
(defun mode-icons-cmd-p ()
"Is the current mode a CMD shell?"
(mode-icons-powershell-p "cmdproxy"))
(defun mode-icons-cygwin-p ()
"Is the current mode a CMD shell?"
(mode-icons-powershell-p "cygwin"))
(defun mode-icons-msys-p ()
"Is the current mode a CMD shell?"
(mode-icons-powershell-p "msys"))
(defvar mode-icons-get-xpm-string (make-hash-table :test 'equal))
(defun mode-icons-get-xpm-string (icon-path)
"Get XPM file contents for ICON-PATH.
If ICON-PATH is a string, return that."
(or (and (file-exists-p icon-path)
(or (gethash icon-path mode-icons-get-xpm-string)
(puthash icon-path (mode-icons-save-buffer-state (with-temp-buffer (insert-file-contents icon-path) (buffer-string)))
mode-icons-get-xpm-string)))
(and (stringp icon-path) icon-path)))
(defun mode-icons-get-icon-display-xpm-replace (icon-path rep-alist &optional name)
"Get xpm image from ICON-PATH and replace REP-ALIST in file.
When NAME is non-nil, also replace the internal xpm image name."
(let ((case-fold-search t)
(img (mode-icons-get-xpm-string icon-path))
(i 0))
(dolist (c rep-alist)
(setq img (replace-regexp-in-string (regexp-quote (car c)) (format "COLOR<%d>" i) img t t)
i (1+ i)))
(let ((i 0))
(dolist (c rep-alist)
(setq img (replace-regexp-in-string (format "COLOR<%d>" i) (cdr c) img t t)
i (1+ i))))
(when name
(setq img (replace-regexp-in-string "^[ ]*static[ ]+char[ ]+[*][ ]+.*?\\[" (concat "static char * " name "[") img t t)))
img))
(defun mode-icons-interpolate (c1 c2 &optional factor)
"Interpolate between C1 and C2 by FACTOR.
If FACTOR is unspecified, use 0.5"
(let* ((factor (or factor 0.5))
(red (+ (* (nth 0 c1) factor) (* (nth 0 c2) (- 1.0 factor))))
(green (+ (* (nth 1 c1) factor) (* (nth 1 c2) (- 1.0 factor))))
(blue (+ (* (nth 2 c1) factor) (* (nth 2 c2) (- 1.0 factor)))))
(setq red (/ (round (* 256.0 red)) 256.0)
green (/ (round (* 256.0 green)) 256.0)
blue (/ (round (* 256.0 blue)) 256.0))
(color-rgb-to-hex red green blue)))
(defun mode-icons-interpolate-from-scale (foreground background)
"Interpolate black to FOREGROUND and white to BACKGROUND.
Grayscales are in between.
Assumes that FOREGROUND and BACKGROUND are (r g b) lists."
(let ((black '(0.0 0.0 0.0))
(white '(1.0 1.0 1.0))
lst tmp
(i 0))
(while (< i 256)
(setq tmp (/ i 255.0))
(push (cons (upcase (mode-icons-interpolate black white tmp))
(upcase (mode-icons-interpolate foreground background tmp))) lst)
(setq i (1+ i)))
lst))
(defvar mode-icons-get-icon-display-xpm-bw-face (make-hash-table)
"Hash table of dynamic images.")
(defun mode-icons-background-color (&optional face)
"Get the background color of FACE.
In order, will try to get the background color from:
- FACE
- `mode-line' face
- `default' face
- Assume white."
(color-name-to-rgb (or (face-background (or face 'mode-line))
(face-background 'mode-line)
(face-background 'default)
"white")))
(defun mode-icons-foreground-color (&optional face)
"Get the foreground color of FACE.
In order, will try to get the foreground color from:
- FACE
- `mode-line' face
- `default' face
- Assume black."
(color-name-to-rgb (or (face-foreground (or face 'mode-line))
(face-foreground 'mode-line)
(face-foreground 'default)
"black")))
(defun mode-icons-get-icon-display-xpm-bw-face (icon-path &optional face)
"Change xpm at ICON-PATH to match FACE.
The white is changed to the background color.
The black is changed to the foreground color.
Grayscale colors are also changed by `mode-icons-interpolate-from-scale'."
(let* ((background (mode-icons-background-color face))
(foreground (mode-icons-foreground-color face))
(lst (mode-icons-interpolate-from-scale foreground background))
(name (concat "mode_icons_bw_" (substring (mode-icons-interpolate background foreground 0.0) 1) "_"
(substring (mode-icons-interpolate background foreground 1.0) 1) "_"
(file-name-sans-extension (file-name-nondirectory icon-path))))
(sym (intern name)))
(or (gethash sym mode-icons-get-icon-display-xpm-bw-face)
(puthash sym (mode-icons-get-icon-display-xpm-replace icon-path lst name) mode-icons-get-icon-display-xpm-bw-face))))
(defun mode-icons-get-xpm-icon-colors (icon-path)
"Get a list of rgb colors based on ICON-PATH xpm icon.
ICON-PATH can be a XPM string or a XPM file."
(let (colors)
(mode-icons-save-buffer-state
(with-temp-buffer
(insert (mode-icons-get-xpm-string icon-path))
(goto-char (point-min))
(while (re-search-forward "#[0-9A-Fa-f]\\{6\\}" nil t)
(push (color-name-to-rgb (match-string 0)) colors))))
colors))
(defun mode-icons-desaturate-colors (colors &optional foreground background)
"Desaturate COLORS.
If COLORS is an icon-path of an xpm file, use the colors from
that file.
When FOREGROUND and BACKGROUND are both non-nil, use
`mode-icons-interpolate-from-scale' to change the grayscale to
match the foreground (black) and background (white) colors.
Assume that COLORS is a list of (r g b) values.
Returns a replacement list for `mode-icons-get-icon-display-xpm-replace'"
(if (and colors (stringp colors))
(mode-icons-desaturate-colors (mode-icons-get-xpm-icon-colors colors) foreground background)
(let (color-list
val tmp
(trans-alist (and foreground background (mode-icons-interpolate-from-scale foreground background))))
(dolist (color colors)
(setq val (+ (* 0.3 (nth 0 color)) (* 0.59 (nth 1 color)) (* 0.11 (nth 2 color)))
val (upcase (color-rgb-to-hex val val val)))
(when (and trans-alist (setq tmp (assoc val trans-alist)))
(setq val (cdr tmp)))
(push (cons (upcase (color-rgb-to-hex (nth 0 color) (nth 1 color) (nth 2 color))) val) color-list))
color-list)))
(defun mode-icons-desaturate-xpm (icon-path &optional face)
"Desaturate the xpm at ICON-PATH.
When FACE is non-nil, match the foreground and background colors
in FACE instead of making the image black and white."
(let* ((background (mode-icons-background-color face))
(foreground (mode-icons-foreground-color face))
(lst (mode-icons-desaturate-colors icon-path foreground background))
(name (concat "mode_icons_desaturate_"
(or (and background foreground
(substring (mode-icons-interpolate background foreground 0.0) 1))
"black") "_"
(or (and background foreground
(substring (mode-icons-interpolate background foreground 1.0) 1))
"white") "_"
(file-name-sans-extension (file-name-nondirectory icon-path))))
(sym (intern name)))
(or (gethash sym mode-icons-get-icon-display-xpm-bw-face)
(puthash sym (mode-icons-get-icon-display-xpm-replace icon-path lst name) mode-icons-get-icon-display-xpm-bw-face))))
(defcustom mode-icons-desaturate-inactive t
"Should the inactive mode-line be desaturated.
And changed to match the icon colors?
This only works with xpm files."
:type 'boolean
:group 'mode-icons)
(defcustom mode-icons-desaturate-active nil
"Should the active mode-line be desaturated.
And changed to match the icon colors?
This only works with xpm files."
:type 'boolean
:group 'mode-icons)
(defcustom mode-icons-grayscale-transform t
"Should grayscale 'xpm-bw images match mode-line colors?"
:type 'boolean
:group 'mode-icons)
(defvar mode-icons-get-icon-display (make-hash-table :test 'equal)
"Hash table of `mode-icons-get-icon-display'.")
(defun mode-icons--get-face (&optional face active)
"If FACE is unspecified, use ACTIVE to determine the face.
ACTIVE tells if current window is active."
(or face (and active 'mode-line) 'mode-line-inactive))
(defcustom mode-icons-line-height-adjust 0
"The manual adjustment of the mode-line height for images."
:type 'integer
:group 'mode-icons)
(defun mode-icons-line-height (&optional window)
"Gets the height in pixels of WINDOW's mode-line font.
This function also adjusts the line height by
`mode-icons-line-height-adjust'. If WINDOW is nil, it defaults to
the current window.
This function returns nil if the current display isn’t a graphic
one. This is to make sure that things still work when dealing
with, for example, a starting daemon."
(when (display-graphic-p)
(+ mode-icons-line-height-adjust
(aref (font-info (face-font 'mode-line) (window-frame window)) 3))))
(defun mode-icons-get-icon-display (icon type &optional face active)
"Get the value for the display property of ICON having TYPE.
ICON should be a string naming the file of the icon, without its
extension. Type should be a symbol designating the file type for
the icon.
FACE should be the face for rendering black and white xpm icons
specified by type 'xpm-bw.
ACTIVE is an indicator that the current window is active."
(let* ((face (mode-icons--get-face face active))
(key (list icon type face active
mode-icons-desaturate-inactive mode-icons-desaturate-active
mode-icons-grayscale-transform custom-enabled-themes))
tmp)
(or (gethash key mode-icons-get-icon-display)
(puthash key
(cond
((memq type '(png xpm xpm-bw gif jpeg jpg xbm xpm))
(let ((icon-path (mode-icons-get-icon-file
(concat icon "." (or (and (eq type 'xpm-bw) "xpm")
(symbol-name type))))))
(cond
((and mode-icons-grayscale-transform (eq type 'xpm-bw))
(create-image (mode-icons-get-icon-display-xpm-bw-face icon-path face)
;; Use imagemagick for rescaling...
(or (and (fboundp 'imagemagick-types)
(memq 'png (imagemagick-types)) 'imagemagick)
'xpm)
t :ascent 'center
:face face
:xpm-bw t
:height (mode-icons-line-height)
:icon icon))
((eq type 'xpm-bw)
(create-image icon-path
(or (and (fboundp 'imagemagick-types)
(memq 'png (imagemagick-types)) 'imagemagick)
'xpm)
:height (mode-icons-line-height)
:ascent 'center
:face face
:icon icon))
((and (eq type 'xpm)
(or (and active mode-icons-desaturate-active)
(and (not active) mode-icons-desaturate-inactive)))
(create-image (mode-icons-desaturate-xpm icon-path face)
(or (and (fboundp 'imagemagick-types)
(memq 'png (imagemagick-types)) 'imagemagick)
'xpm) t
:ascent 'center
:height (mode-icons-line-height)
:face face :icon icon))
(t
(create-image icon-path
(or (and (fboundp 'imagemagick-types)
(memq (or (and (eq type 'jpg) 'jpeg) type) (imagemagick-types))
'imagemagick)
(or (and (eq type 'jpg) 'jpeg) type))
nil
:height (mode-icons-line-height)
:ascent 'center :face face :icon icon)))))
((and (eq type 'emoji) (setq tmp (mode-icons--get-emoji " " (list "" icon type) face)))
(get-text-property 0 'display tmp))
;; Shouldn't get here...
((and (eq type 'ext) (setq tmp (mode-icons--ext-available-p (list "" icon type))))
(mode-icons-get-icon-display (concat "ext-" (downcase icon)) 'xpm-bw face active))
((and (image-type-available-p 'xpm)
(setq tmp (mode-icons--get-font-xpm-file (list "" icon type)))
(file-exists-p tmp))
(setq tmp nil)
(mode-icons-get-icon-display (mode-icons--get-font-xpm-file (list "" icon type) t) 'xpm-bw face active))
(t nil))
mode-icons-get-icon-display))))
(defcustom mode-icons-minor-mode-base-text-properties
'('help-echo nil
'mouse-face 'mode-line-highlight
'local-map mode-line-minor-mode-keymap)
"List of text propeties to apply to every minor mode."
:type '(repeat sexp)
:group 'mode-icons)
(defcustom mode-icons-major-mode-base-text-properties
'('help-echo "Major mode\nmouse-1: Display major mode menu\nmouse-2: Show help for major mode\nmouse-3: Toggle minor modes"
'mouse-face 'mode-line-highlight
'local-map mode-line-major-mode-keymap)
"List of text propeties to apply to every major mode."
:type '(repeat sexp)
:group 'mode-icons)
(defcustom mode-icons-narrow-text-properties
'('local-map
'(keymap
(mode-line keymap
(mouse-2 . mode-line-widen)))
'mouse-face 'mode-line-highlight 'help-echo "mouse-2: Remove narrowing from buffer")
"List of text propeties to apply to narrowing buffer indicator."
:type '(repeat sexp)
:group 'mode-icons)
(defcustom mode-icons-read-only-text-properties
'('mouse-face 'mode-line-highlight 'local-map
'(keymap
(mode-line keymap
(mouse-1 . mode-line-toggle-read-only)))
'help-echo 'mode-line-read-only-help-echo)
"List of text propeties to apply to read-only buffer indicator."
:type '(repeat sexp)
:group 'mode-icons)
(defcustom mode-icons-modified-text-properties
'('mouse-face 'mode-line-highlight
'local-map
'(keymap
(mode-line keymap
(mouse-1 . mode-icons-save-steal-or-revert-buffer)
(mouse-3 . mode-line-toggle-modified)))
'help-echo 'mode-icons-modified-help-echo)
"List of text propeties to apply to read-only buffer indicator."
:type '(repeat sexp)
:group 'mode-icons)
(defun mode-icons-save-steal-or-revert-buffer (event)
"Save buffer OR revert file from mode line.
Use EVENT to determine location."
(interactive "e")
(with-selected-window (posn-window (event-start event))
(let* ((bfn (buffer-file-name))
(revert-p (not (or (and bfn (file-remote-p buffer-file-name))
(verify-visited-file-modtime (current-buffer)))))
(steal-p (and (not (or (and bfn (file-remote-p buffer-file-name))
(member (file-locked-p bfn) '(nil t)))))))
(cond
(revert-p (revert-buffer t t))
(steal-p
(message "To steal or ignore lock, start editing the file."))
(t (call-interactively (key-binding (where-is-internal 'save-buffer global-map t))))))
(force-mode-line-update)))
(defun mode-icons-modified-help-echo (window _object _point)
"Return help text specifying WINDOW's buffer modification status."
(let* ((bfn (buffer-file-name))
(revert-p (not (or (and bfn (file-remote-p buffer-file-name))
(verify-visited-file-modtime (current-buffer)))))
(steal-p (and (not (or (and bfn (file-remote-p buffer-file-name))
(member (file-locked-p bfn) '(nil t))))))
(mod-p (buffer-modified-p (window-buffer window))))
(format "Buffer is %s\nmouse-1: %s Buffer\nmouse-3: Toggle modification state"
(cond
(steal-p
"locked for editing by another user.")
(revert-p
"modified outside of emacs!")
((buffer-modified-p (window-buffer window))
"modified")
(t "unmodified"))
(cond
(steal-p
"Echo about lock status of")
(revert-p
"Revert")
(mod-p
"Save")
(t "")))))
(defcustom mode-icons-read-only-text-properties
'('mouse-face 'mode-line-highlight 'local-map
'(keymap
(mode-line keymap
(mouse-1 . mode-line-toggle-read-only)))
'help-echo 'mode-line-read-only-help-echo)
"List of text propeties to apply to read-only buffer indicator."
:type '(repeat sexp)
:group 'mode-icons)
(defvar mode-icons-powerline-p nil)
(defun mode-icons-need-update-p ()
"Determine if the mode-icons need an update."
(not (or (and (boundp 'rich-minority-mode) rich-minority-mode)
(member 'sml/pos-id-separator mode-line-format)
(string-match-p "powerline" (prin1-to-string mode-line-format)))))
(defvar mode-icons-font-register-alist nil
"Alist of characters supported.")
(defun mode-icons-supported-font-p (char font)
"Determine if the CHAR is supported in FONT.
When DONT-REGISTER is non-nil, don't register the font.
Otherwise, register the font for use in the mode-line and
everywhere else."
(if (memq font '(ext emoji xpm xbm jpg jpeg gif png nil)) nil
(unless (boundp (intern (format "mode-icons-font-spec-%s" font)))
(set (intern (format "mode-icons-font-spec-%s" font))
(and (member (format "%s" font) (font-family-list))
(font-spec :name (format "%s" font)))))
(when (and (or (integerp char)
(and (stringp char) (= 1 (length char))))
(boundp (intern (format "mode-icons-font-spec-%s" font)))
(symbol-value (intern (format "mode-icons-font-spec-%s" font))))
(let* ((char (or (and (integerp char) char)
(and (stringp char) (= 1 (length char))
(aref (vconcat char) 0))))
(found-char-p (assoc char mode-icons-font-register-alist))
(char-font-p (and found-char-p (eq (cdr found-char-p) font))))
(cond
(char-font-p t)
(found-char-p t)
(t ;; not yet registered.
(set-fontset-font t (cons char char) (symbol-value (intern (format "mode-icons-font-spec-%s" font))))
(push (cons char font) mode-icons-font-register-alist)
t))))))
(defun mode-icons-supported-p (icon-spec)
"Determine if ICON-SPEC is suppored on your system."
(or
(and (or (eq (nth 2 icon-spec) nil) (eq (nth 1 icon-spec) nil)) t)
(and (eq (nth 2 icon-spec) 'emoji)
(or (and (image-type-available-p 'png) (featurep 'emojify))
(and (image-type-available-p 'xpm)
(file-exists-p (mode-icons--get-emoji-xpm-file icon-spec)))))
(and (eq (nth 2 icon-spec) 'jpg) (image-type-available-p 'jpeg))
(and (eq (nth 2 icon-spec) 'xpm-bw) (image-type-available-p 'xpm))
(and (eq (nth 2 icon-spec) 'ext) (image-type-available-p 'xpm)
(mode-icons--ext-available-p icon-spec))
(or (mode-icons-supported-font-p (nth 1 icon-spec) (nth 2 icon-spec))
(and (image-type-available-p 'xpm)
(mode-icons--get-font-xpm-file icon-spec)
(file-exists-p (mode-icons--get-font-xpm-file icon-spec))))
(or (image-type-available-p (nth 2 icon-spec))
(and (eq (nth 2 icon-spec) 'png)
(and (image-type-available-p 'xpm)
(file-exists-p (mode-icons--get-png-xpm-file icon-spec))))) ))
(defvar emojify-emojis)
(defvar mode-icons--gimp (executable-find "gimp")
"Gimp to convert png to xpm.")
(defvar mode-icons--gimp-inferior-args "-i -d -b -")
(defvar mode-icons--stop-gimp-after nil
"Seconds of idle time before mode-icons gimp is stopped.
When nil, don't stop the gimp inferior mode.")
(defvar mode-icons--stop-gimp-timer nil)
(defun mode-icons--start-gimp-inferior ()
"GIMP inferior process."
(interactive)
(when (file-exists-p mode-icons--gimp)
(unless (get-buffer "*mode-icons-gimp*")
(cl-letf (((symbol-function 'pop-to-buffer-same-window) (lambda(&rest _ignore))))
(save-excursion
(run-scheme (format "\"%s\" %s" mode-icons--gimp mode-icons--gimp-inferior-args))))
(with-current-buffer (get-buffer "*scheme*")
(rename-buffer "*mode-icons-gimp*")
(set-process-query-on-exit-flag (get-buffer-process (get-buffer "*mode-icons-gimp*")) nil)))))
(defvar mode-icons--gimp-ready-p nil)
(defun mode-icons--gimp-ready-p ()
"Determine if GIMP inferior process is ready."
(if (file-exists-p mode-icons--gimp)
(or mode-icons--gimp-ready-p
(let (buf)
(mode-icons--start-gimp-inferior)
(and (setq buf (get-buffer "*mode-icons-gimp*"))
(with-current-buffer buf
(goto-char (point-min))
(when (re-search-forward "ts>" nil t)
(setq mode-icons--gimp-ready-p t))))))))
(defvar mode-icons--stop-gimp-inferior nil)
(defun mode-icons--stop-gimp-inferior ()
"Stop the inferior gimp process."
(interactive)
(when (file-exists-p mode-icons--gimp)
(let ((buf (get-buffer "*mode-icons-gimp*")))
(cond
((and (mode-icons--gimp-ready-p) buf
(get-buffer-process buf))
(mode-icons--process-gimp "(gimp-quit 0)")
(setq mode-icons--gimp-ready-p nil
mode-icons--stop-gimp-inferior t)
(run-with-idle-timer 1 nil #'mode-icons--stop-gimp-inferior))
((and buf (not (get-buffer-process buf)))
(kill-buffer (get-buffer "*mode-icons-gimp*")))
(t (run-with-idle-timer 1 nil #'mode-icons--stop-gimp-inferior))))))
(defun mode-icons--process-gimp (scm)
"Process gimp SCM (scheme)."
(when mode-icons--stop-gimp-timer
(cancel-timer mode-icons--stop-gimp-timer))
(when (file-exists-p mode-icons--gimp)
(if (mode-icons--gimp-ready-p)
(progn
(comint-send-string
(with-current-buffer (get-buffer "*mode-icons-gimp*"))
(concat scm "\n"))
(when mode-icons--stop-gimp-after
(setq mode-icons--stop-gimp-timer (run-with-timer mode-icons--stop-gimp-after nil #'mode-icons--stop-gimp-inferior))))
(run-with-idle-timer 1 nil #'mode-icons--process-gimp scm))))
(defvar mode-icons--generic-type-to-xpm-gimp-script
(replace-regexp-in-string
"[ \n\t]+" " "
"(let* ((image-width 1024)
(image-height 20)
(buffer-image 1)
(text \"%s\")
(font-size 20)
(font-name \"FontAwesome\")
(xpm-image \"%s\")
(font-size-2 10)
(text-2 \"%s\")
(font-name-2 \"Haettenschweiler\")
(bg-color '(255 255 255))
(fg-color '(0 0 0))
(image (car (gimp-image-new 1024 16 0)))
(layer (car (gimp-layer-new image image-width image-height RGB-IMAGE \"layer 1\" 100 NORMAL)))
(layer2 (car (gimp-layer-new image image-width image-height RGB-IMAGE \"layer 2\" 100 NORMAL)))
(out-text)
(out-width)
(out-height)
(out-buffer)
(drawable))
(gimp-image-add-layer image layer 0)
(gimp-context-set-background bg-color)
(gimp-context-set-foreground fg-color)
(gimp-layer-add-alpha layer)
(gimp-drawable-fill layer TRANSPARENT-FILL)
(gimp-image-add-layer image layer2 0)
(gimp-layer-add-alpha layer2)
(gimp-drawable-fill layer2 TRANSPARENT-FILL)
(gimp-text-fontname image layer2 3 7 text-2 0 TRUE font-size-2 PIXELS font-name-2)
(set! out-text (car (gimp-text-fontname image layer 0 0 text 0 TRUE font-size PIXELS font-name)))
(set! out-width (car (gimp-drawable-width out-text)))
(set! out-height (car (gimp-drawable-height out-text)))
(set! out-buffer (* out-height (/ buffer-image 100)))
(set! out-height (+ out-height out-buffer out-buffer))
(set! out-width (+ out-width out-buffer out-buffer))
(gimp-image-resize image out-width out-height 0 0)
(gimp-layer-resize layer out-width out-height 0 0)
(gimp-layer-set-offsets out-text out-buffer out-buffer)
(gimp-image-flatten image)
(set! drawable (car (gimp-image-get-active-layer image)))
(file-xpm-save RUN-NONINTERACTIVE image drawable xpm-image xpm-image 127)
(gimp-image-delete image))")
"Generic Type script.")
(defvar mode-icons--font-to-xpm-gimp-script
(replace-regexp-in-string
"[ \n\t]+" " "
"(let* ((image-width 1024)
(image-height 20)
(buffer-image 1)
(text \"%s\")
(font-size 20)
(font-name \"%s\")
(xpm-image \"%s\")
(bg-color '(255 255 255))
(fg-color '(0 0 0))
(image (car (gimp-image-new 1024 16 0)))
(layer (car (gimp-layer-new image image-width image-height RGB-IMAGE \"layer 1\" 100 NORMAL)))
(out-text)
(out-width)
(out-height)
(out-buffer)
(drawable))
(gimp-image-add-layer image layer 0)
(gimp-context-set-background bg-color)
(gimp-context-set-foreground fg-color)
(gimp-layer-add-alpha layer)
(gimp-drawable-fill layer TRANSPARENT-FILL)
(set! out-text (car (gimp-text-fontname image layer 0 0 text 0 TRUE font-size PIXELS font-name)))
(set! out-width (car (gimp-drawable-width out-text)))
(set! out-height (car (gimp-drawable-height out-text)))
(set! out-buffer (* out-height (/ buffer-image 100)))
(set! out-height (+ out-height out-buffer out-buffer))
(set! out-width (+ out-width out-buffer out-buffer))
(gimp-image-resize image out-width out-height 0 0)
(gimp-layer-resize layer out-width out-height 0 0)
(gimp-layer-set-offsets out-text out-buffer out-buffer)
(gimp-image-flatten image)
(set! drawable (car (gimp-image-get-active-layer image)))
(file-xpm-save RUN-NONINTERACTIVE image drawable xpm-image xpm-image 127)
(gimp-image-delete image))")
"Gimp scheme script to convert a font character to xpm file.")
(defvar mode-icons--convert-ext-to-xpm (make-hash-table :test 'equal))
(defun mode-icons--convert-ext-to-xpm (ext)
"Convert EXT to a xpm file."
(let ((xpm (mode-icons-get-icon-file (concat "ext-" (downcase ext) ".xpm"))))
(when (and mode-icons--gimp (file-exists-p mode-icons--gimp)
xpm (not (gethash xpm mode-icons--convert-ext-to-xpm))
(not (file-exists-p xpm)))
(puthash xpm t mode-icons--convert-ext-to-xpm)
(mode-icons--process-gimp
(format mode-icons--generic-type-to-xpm-gimp-script (make-string 1 #xf016) xpm
(downcase ext))))))
(defun mode-icons--ext-available-p (icon-spec)
"Determine if ICON-SPEC's ext is availble for display.
If not, try `mode-icons--convert-ext-to-xpm'."
(when (eq (nth 2 icon-spec) 'ext)
(let ((xpm (mode-icons-get-icon-file (concat "ext-" (downcase (nth 1 icon-spec)) ".xpm"))))
(if (file-readable-p xpm)
xpm
(mode-icons--convert-ext-to-xpm (nth 1 icon-spec))
nil))))
(defcustom mode-icons-generate-font-grayscale nil
"Generate grayscale images for font icons.
This is used instead of transparancy to capure the font's
anti-aliasing. `mode-icons' will transform the colors to match
the background instead."
:type 'boolean
:group 'mode-icons)
(defvar mode-icons--convert-text-to-xpm (make-hash-table :test 'equal))
(defun mode-icons--convert-text-to-xpm (text font xpm &optional face height)
"Convert TEXT in FONT to XPM file using gimp.
When FACE is non-nil, use the face background and foreground
properties to render the font (its no longer transparent).
When HEIGHT is non-nil, use the font HEIGHT (in pixels) instead
of 20px."
(when (and mode-icons--gimp (file-exists-p mode-icons--gimp)
xpm (not (gethash xpm mode-icons--convert-text-to-xpm))
(not (file-exists-p xpm)))
(puthash xpm t mode-icons--convert-text-to-xpm)
(let ((script (format mode-icons--font-to-xpm-gimp-script text font xpm))
(background (mode-icons-background-color face))
(foreground (mode-icons-foreground-color face)))
(when face
(setq background (mapcar (lambda(x)
(round (* 255 x))) background)
foreground (mapcar (lambda(x)
(round (* 255 x))) foreground))
(setq script (replace-regexp-in-string
(regexp-quote "(bg-color '(255 255 255))")
(format "(bg-color '%s)" background)
script)
script (replace-regexp-in-string
(regexp-quote "(fg-color '(0 0 0))")
(format "(fg-color '%s)" foreground)
script)
script (replace-regexp-in-string
"TRANSPARENT-FILL" "BACKGROUND-FILL" script)
script (replace-regexp-in-string
(regexp-quote "(gimp-layer-add-alpha layer)") "" script)))
(when height
(setq script (replace-regexp-in-string
(regexp-quote "(image-height 20)")
(format "(image-height %s)" background)
script)
script (replace-regexp-in-string
(regexp-quote "(font-size 20)")
(format "(font-size %s)" background)
script)
script (replace-regexp-in-string
"TRANSPARENT-FILL" "BACKGROUND-FILL" script)
script (replace-regexp-in-string
(regexp-quote "(gimp-layer-add-alpha layer)") "" script)))
(when mode-icons-generate-font-grayscale
(setq script (replace-regexp-in-string
"TRANSPARENT-FILL" "BACKGROUND-FILL" script)
script (replace-regexp-in-string
(regexp-quote "(gimp-layer-add-alpha layer)") "" script)))
(mode-icons--process-gimp script))))
(defun mode-icons--get-font-xpm-file (icon-spec &optional icon-name)
"Get the font icon equivalent xpm file name from ICON-SPEC.
When ICON-NAME is non-nil, return the small icon name without the
extension or directory."