forked from seagle0128/doom-modeline
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdoom-modeline-core.el
967 lines (820 loc) · 36 KB
/
doom-modeline-core.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
;;; doom-modeline-core.el --- The core libraries for doom-modeline -*- lexical-binding: t; -*-
;; Copyright (C) 2018-2019 Vincent Zhang
;; This file is not 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 2, 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;; Commentary:
;;
;; The core libraries for doom-modeline.
;;
;;; Code:
(require 'cl-lib)
(require 'all-the-icons)
(require 'shrink-path)
(require 'subr-x)
;;
;; Compatibilities
;;
(eval-and-compile
(unless (>= emacs-major-version 26)
;; Define `if-let*' and `when-let*' variants for 25 users.
(unless (fboundp 'if-let*) (defalias 'if-let* #'if-let))
(unless (fboundp 'when-let*) (defalias 'when-let* #'when-let))))
;; Don’t compact font caches during GC.
(if (eq system-type 'windows-nt)
(setq inhibit-compacting-font-caches t))
;;`file-local-name' is introduced in 25.2.2.
(unless (fboundp 'file-local-name)
(defun file-local-name (file)
"Return the local name component of FILE.
It returns a file name which can be used directly as argument of
`process-file', `start-file-process', or `shell-command'."
(or (file-remote-p file 'localname) file)))
;; Set correct font width for `all-the-icons' for appropriate mode-line width.
;; @see https://emacs.stackexchange.com/questions/14420/how-can-i-fix-incorrect-character-width
(defun doom-modeline--set-char-widths (alist)
"Set correct widths of icons characters in ALIST."
(while (char-table-parent char-width-table)
(setq char-width-table (char-table-parent char-width-table)))
(dolist (pair alist)
(let ((width (car pair))
(chars (cdr pair))
(table (make-char-table nil)))
(dolist (char chars)
(set-char-table-range table char width))
(optimize-char-table table)
(set-char-table-parent table char-width-table)
(setq char-width-table table))))
(defun doom-moddeline--set-font-widths (alist)
(let (fonts)
(dolist (pair alist)
(push (string-to-char (cdr pair)) fonts))
(doom-modeline--set-char-widths
`((2 . ,fonts)))))
(defconst doom-modeline-rhs-icons-alist
'(;; vcs
("git-compare" . "\xf0ac")
("git-merge" . "\xf023")
("arrow-down" . "\xf03f")
("alert" . "\xf02d")
("git-branch" . "\xf020")
;; checker: flycheck/flymake
("do_not_disturb_alt" . "\xe611")
("check" . "\xe5ca")
("access_time" . "\xe192")
("sim_card_alert" . "\xe624")
("pause" . "\xe034")
("priority_high" . "\xe645")
;; Persp
("aspect_ratio" . "\xe85b")
;; Preview
("pageview" . "\xe8a0")
;; LSP
("rocket" . "\xf135")
;; GitHub
("github" . "\xf09b")
;; debug
("bug" . "\xf188")
;; mu4e
("email" . "\xe0be")
;; irc
("message" . "\xe0c9")
;; Battery
("battery-charging" . "\xe939")
("battery-empty" . "\xf244")
("battery-full" . "\xf240")
("battery-half" . "\xf242")
("battery-quarter" . "\xf243")
("battery-three-quarters" . "\xf241")))
(doom-moddeline--set-font-widths doom-modeline-rhs-icons-alist)
;;
;; Customizations
;;
(defgroup doom-modeline nil
"A minimal and modern mode-line."
:group 'mode-line
:link '(url-link :tag "Homepage" "https://github.com/seagle0128/doom-modeline"))
(defcustom doom-modeline-height 25
"How tall the mode-line should be. It's only respected in GUI.
If the actual char height is larger, it respects the actual char height."
:type 'integer
:set (lambda (sym val)
(set sym (if (> val 0) val 1)))
:group 'doom-modeline)
(defcustom doom-modeline-bar-width (if (eq system-type 'darwin) 3 6)
"How wide the mode-line bar should be. It's only respected in GUI."
:type 'integer
:set (lambda (sym val)
(set sym (if (> val 0) val 1)))
:group 'doom-modeline)
(defcustom doom-modeline-project-detection
(cond ((fboundp 'ffip-get-project-root-directory) 'ffip)
((fboundp 'projectile-project-root) 'projectile)
((fboundp 'project-current) 'project)
(t nil))
"How to detect the project root.
The default priority is `ffip' > `projectile' > `project'.
nil means to use `default-directory'.
The project management packages have some issues on detecting project root.
e.g. `projectile' doesn't handle symlink folders well, while `project' is
unable to hanle sub-projects.
Specify another one if you encounter the issue."
:type '(choice
(const :tag "Find File in Project" ffip)
(const :tag "Projectile" projectile)
(const :tag "Built-in Project" project)
(const :tag "Disable" nil))
:group 'doom-modeline)
(defcustom doom-modeline-buffer-file-name-style 'truncate-upto-project
"Determines the style used by `doom-modeline-buffer-file-name'.
Given ~/Projects/FOSS/emacs/lisp/comint.el
truncate-upto-project => ~/P/F/emacs/lisp/comint.el
truncate-from-project => ~/Projects/FOSS/emacs/l/comint.el
truncate-with-project => emacs/l/comint.el
truncate-except-project => ~/P/F/emacs/l/comint.el
truncate-upto-root => ~/P/F/e/lisp/comint.el
truncate-all => ~/P/F/e/l/comint.el
relative-from-project => emacs/lisp/comint.el
relative-to-project => lisp/comint.el
file-name => comint.el
buffer-name => comint.el<2> (uniquify buffer name)"
:type '(choice (const truncate-upto-project)
(const truncate-upto-project)
(const truncate-from-project)
(const truncate-with-project)
(const truncate-except-project)
(const truncate-upto-root)
(const truncate-all)
(const relative-from-project)
(const relative-to-project)
(const file-name)
(const buffer-name))
:group'doom-modeline)
(defcustom doom-modeline-icon (display-graphic-p)
"Whether display icons in mode-line.
It respects `all-the-icons-color-icons'."
:type 'boolean
:group 'doom-modeline)
(defcustom doom-modeline-major-mode-icon t
"Whether display the icon for `major-mode'.
It respects `doom-modeline-icon'."
:type 'boolean
:group'doom-modeline)
(defcustom doom-modeline-major-mode-color-icon t
"Whether display the colorful icon for `major-mode'.
It respects `doom-modeline-major-mode-icon'."
:type 'boolean
:group'doom-modeline)
(defcustom doom-modeline-buffer-state-icon t
"Whether display the icon for the buffer state.
It respects `doom-modeline-icon'."
:type 'boolean
:group 'doom-modeline)
(defcustom doom-modeline-buffer-modification-icon t
"Whether display the modification icon for the buffer.
It respects `doom-modeline-icon' and `doom-modeline-buffer-state-icon'."
:type 'boolean
:group 'doom-modeline)
(defcustom doom-modeline-unicode-fallback t
"Whether to use unicode as a fallback (instead of ASCII) when not using icons."
:type 'boolean
:group 'doom-modeline)
(defcustom doom-modeline-minor-modes (featurep 'minions)
"Whether display minor modes in mode-line."
:type 'boolean
:group 'doom-modeline)
(defcustom doom-modeline-enable-word-count nil
"If non-nil, a word count will be added to the selection-info modeline segment."
:type 'boolean
:group 'doom-modeline)
(defcustom doom-modeline-buffer-encoding t
"Whether display buffer encoding."
:type 'boolean
:group 'doom-modeline)
(defcustom doom-modeline-indent-info nil
"Whether display indentation information."
:type 'boolean
:group 'doom-modeline)
(defcustom doom-modeline-checker-simple-format t
"If non-nil, only display one number for checker information if applicable."
:type 'boolean
:group 'doom-modeline)
(defcustom doom-modeline-number-limit 99
"The maximum number displayed for notifications."
:type 'integer
:group 'doom-modeline)
(defcustom doom-modeline-vcs-max-length 12
"The maximum displayed length of the branch name of version control."
:type 'integer
:group 'doom-modeline)
(defcustom doom-modeline-persp-name t
"Whether display perspective name.
Non-nil to display in mode-line."
:type 'boolean
:group 'doom-modeline)
(defcustom doom-modeline-lsp t
"Whether display `lsp' state.
Non-nil to display in mode-line."
:type 'boolean
:group 'doom-modeline)
(defcustom doom-modeline-github nil
"Whether display GitHub notifications/
It requires `ghub' and `async' packages."
:type 'boolean
:group 'doom-modeline)
(defcustom doom-modeline-github-interval 1800 ; (* 30 60)
"The interval of checking GitHub."
:type 'integer
:group 'doom-modeline)
(defcustom doom-modeline-env-version t
"Whether display environment version."
:type 'boolean
:group 'doom-modeline)
(defcustom doom-modeline-mu4e t
"Whether display mu4e notifications.
It requires `mu4e-alert' package."
:type 'boolean
:group 'doom-modeline)
(defcustom doom-modeline-irc t
"Whether display irc notifications.
It requires `circe' package."
:type 'boolean
:group 'doom-modeline)
(defcustom doom-modeline-irc-stylize 'identity
"Function to stylize the irc buffer names."
:type 'function
:group 'doom-modeline)
;;
;; Faces
;;
(defgroup doom-modeline-faces nil
"The faces of `doom-modeline'."
:group 'doom-modeline
:group 'faces
:link '(url-link :tag "Homepage" "https://github.com/seagle0128/doom-modeline"))
(defface doom-modeline-buffer-path
'((t (:inherit (mode-line-emphasis bold))))
"Face used for the dirname part of the buffer path."
:group 'doom-modeline-faces)
(defface doom-modeline-buffer-file
'((t (:inherit (mode-line-buffer-id bold))))
"Face used for the filename part of the mode-line buffer path."
:group 'doom-modeline-faces)
(defface doom-modeline-buffer-modified
'((t (:inherit (error bold) :background nil)))
"Face used for the 'unsaved' symbol in the mode-line."
:group 'doom-modeline-faces)
(defface doom-modeline-buffer-major-mode
'((t (:inherit (mode-line-emphasis bold))))
"Face used for the major-mode segment in the mode-line."
:group 'doom-modeline-faces)
(defface doom-modeline-buffer-minor-mode
'((t (:inherit (mode-line-buffer-id bold))))
"Face used for the minor-modes segment in the mode-line."
:group 'doom-modeline-faces)
(defface doom-modeline-project-parent-dir
'((t (:inherit (font-lock-comment-face bold))))
"Face used for the project parent directory of the mode-line buffer path."
:group 'doom-modeline-faces)
(defface doom-modeline-project-dir
'((t (:inherit (font-lock-string-face bold))))
"Face used for the project directory of the mode-line buffer path."
:group 'doom-modeline-faces)
(defface doom-modeline-project-root-dir
'((t (:inherit (mode-line-emphasis bold))))
"Face used for the project part of the mode-line buffer path."
:group 'doom-modeline-faces)
(defface doom-modeline-highlight
'((t (:inherit mode-line-emphasis)))
"Face for bright segments of the mode-line."
:group 'doom-modeline-faces)
(defface doom-modeline-panel
'((t (:inherit mode-line-highlight)))
"Face for 'X out of Y' segments, such as `anzu', `evil-substitute' and`iedit', etc."
:group 'doom-modeline-faces)
(defface doom-modeline-debug
'((t (:inherit (font-lock-doc-face bold))))
"Face for debug-level messages in the modeline. Used by `*checker'."
:group 'doom-modeline-faces)
(defface doom-modeline-info
'((t (:inherit (success bold))))
"Face for info-level messages in the modeline. Used by `*vc' and `*chcker'."
:group 'doom-modeline-faces)
(defface doom-modeline-warning
'((t (:inherit (warning bold))))
"Face for warnings in the modeline. Used by `*checker'."
:group 'doom-modeline-faces)
(defface doom-modeline-urgent
'((t (:inherit (error bold))))
"Face for errors in the modeline. Used by `*checker'."
:group 'doom-modeline-faces)
(defface doom-modeline-unread-number
'((t (:inherit italic)))
"Face for unread number in the modeline. Used by `github', `mu4e', etc."
:group 'doom-modeline-faces)
(defface doom-modeline-bar
'((t (:inherit highlight)))
"The face used for the left-most bar on the mode-line of an active window."
:group 'doom-modeline-faces)
(defface doom-modeline-inactive-bar
`((t (:background ,(face-foreground 'mode-line-inactive))))
"The face used for the left-most bar on the mode-line of an inactive window."
:group 'doom-modeline-faces)
(defface doom-modeline-evil-emacs-state
'((t (:inherit doom-modeline-warning)))
"Face for the Emacs state tag in evil state indicator."
:group 'doom-modeline-faces)
(defface doom-modeline-evil-insert-state
'((t (:inherit doom-modeline-urgent)))
"Face for the insert state tag in evil state indicator."
:group 'doom-modeline-faces)
(defface doom-modeline-evil-motion-state
'((t :inherit doom-modeline-buffer-path))
"Face for the motion state tag in evil state indicator."
:group 'doom-modeline-faces)
(defface doom-modeline-evil-normal-state
'((t (:inherit doom-modeline-info)))
"Face for the normal state tag in evil state indicator."
:group 'doom-modeline-faces)
(defface doom-modeline-evil-operator-state
'((t (:inherit doom-modeline-buffer-path)))
"Face for the operator state tag in evil state indicator."
:group 'doom-modeline-faces)
(defface doom-modeline-evil-visual-state
'((t (:inherit doom-modeline-buffer-file)))
"Face for the visual state tag in evil state indicator."
:group 'doom-modeline-faces)
(defface doom-modeline-evil-replace-state
'((t (:inherit doom-modeline-buffer-modified)))
"Face for the replace state tag in evil state indicator."
:group 'doom-modeline-faces)
(defface doom-modeline-persp-name
'((t (:inherit (font-lock-comment-face italic))))
"Face for the replace state tag in evil state indicator."
:group 'doom-modeline-faces)
(defface doom-modeline-persp-buffer-not-in-persp
'((t (:inherit (font-lock-doc-face bold italic))))
"Face for the replace state tag in evil state indicator."
:group 'doom-modeline-faces)
(defface doom-modeline-lsp-success
'((t (:inherit success)))
"Face for success state in LSP."
:group 'doom-modeline-faces)
(defface doom-modeline-lsp-warning
'((t (:inherit warning)))
"Face for warning state in LSP."
:group 'doom-modeline-faces)
(defface doom-modeline-lsp-error
'((t (:inherit error)))
"Face for error state in LSP."
:group 'doom-modeline-faces)
;;
;; Externals
;;
(declare-function face-remap-remove-relative 'face-remap)
(declare-function ffip-get-project-root-directory 'find-file-in-project)
(declare-function project-roots 'project)
(declare-function projectile-project-root 'projectile)
;;
;; Modeline library
;;
(defvar doom-modeline-fn-alist ())
(defvar doom-modeline-var-alist ())
(defmacro doom-modeline-def-segment (name &rest body)
"Defines a modeline segment NAME with BODY and byte compiles it."
(declare (indent defun) (doc-string 2))
(let ((sym (intern (format "doom-modeline-segment--%s" name)))
(docstring (if (stringp (car body))
(pop body)
(format "%s modeline segment" name))))
(cond ((and (symbolp (car body))
(not (cdr body)))
(add-to-list 'doom-modeline-var-alist (cons name (car body)))
`(add-to-list 'doom-modeline-var-alist (cons ',name ',(car body))))
(t
(add-to-list 'doom-modeline-fn-alist (cons name sym))
`(progn
(fset ',sym (lambda () ,docstring ,@body))
(add-to-list 'doom-modeline-fn-alist (cons ',name ',sym))
,(unless (bound-and-true-p byte-compile-current-file)
`(let (byte-compile-warnings)
(byte-compile #',sym))))))))
(defun doom-modeline--prepare-segments (segments)
"Prepare mode-line `SEGMENTS'."
(let (forms it)
(dolist (seg segments)
(cond ((stringp seg)
(push seg forms))
((symbolp seg)
(cond ((setq it (cdr (assq seg doom-modeline-fn-alist)))
(push (list :eval (list it)) forms))
((setq it (cdr (assq seg doom-modeline-var-alist)))
(push it forms))
((error "%s is not a defined segment" seg))))
((error "%s is not a valid segment" seg))))
(nreverse forms)))
(defvar doom-modeline--width-cache nil)
(defun doom-modeline--window-font-width ()
"Cache the font width."
(let ((attributes (face-all-attributes 'default)))
(or (cdr (assoc attributes doom-modeline--width-cache))
(let ((width (window-font-width nil 'mode-line)))
(push (cons attributes width) doom-modeline--width-cache)
width))))
(defun doom-modeline-def-modeline (name lhs &optional rhs)
"Defines a modeline format and byte-compiles it.
NAME is a symbol to identify it (used by `doom-modeline' for retrieval).
LHS and RHS are lists of symbols of modeline segments defined with
`doom-modeline-def-segment'.
Example:
(doom-modeline-def-modeline 'minimal
'(bar matches \" \" buffer-info)
'(media-info major-mode))
(doom-modeline-set-modeline 'minimal t)"
(let ((sym (intern (format "doom-modeline-format--%s" name)))
(lhs-forms (doom-modeline--prepare-segments lhs))
(rhs-forms (doom-modeline--prepare-segments rhs)))
(defalias sym
(lambda ()
(list lhs-forms
(propertize
" "
'face (if (doom-modeline--active) 'mode-line 'mode-line-inactive)
'display `((space :align-to (- (+ right right-fringe right-margin)
,(* (if (number-or-marker-p (face-attribute 'mode-line :height))
(/ (doom-modeline--window-font-width)
(frame-char-width) 1.0)
1)
(string-width
(format-mode-line
(cons "" rhs-forms))))))))
rhs-forms))
(concat "Modeline:\n"
(format " %s\n %s"
(prin1-to-string lhs)
(prin1-to-string rhs))))))
(put 'doom-modeline-def-modeline 'lisp-indent-function 'defun)
(defun doom-modeline (key)
"Return a mode-line configuration associated with KEY (a symbol).
Throws an error if it doesn't exist."
(let ((fn (intern-soft (format "doom-modeline-format--%s" key))))
(when (functionp fn)
`(:eval (,fn)))))
(defun doom-modeline-set-modeline (key &optional default)
"Set the modeline format. Does nothing if the modeline KEY doesn't exist.
If DEFAULT is non-nil, set the default mode-line for all buffers."
(when-let ((modeline (doom-modeline key)))
(setf (if default
(default-value 'mode-line-format)
(buffer-local-value 'mode-line-format (current-buffer)))
(list "%e" modeline))))
;;
;; Plugins
;;
(defun doom-modeline-redisplay (&rest _)
"Call `redisplay' to trigger mode-line height calculations.
Certain functions, including e.g. `fit-window-to-buffer', base
their size calculations on values which are incorrect if the
mode-line has a height different from that of the `default' face
and certain other calculations have not yet taken place for the
window in question.
These calculations can be triggered by calling `redisplay'
explicitly at the appropriate time and this functions purpose
is to make it easier to do so.
This function is like `redisplay' with non-nil FORCE argument.
It accepts an arbitrary number of arguments making it suitable
as a `:before' advice for any function."
(redisplay t))
(advice-add #'fit-window-to-buffer :before #'doom-modeline-redisplay)
(advice-add #'resize-temp-buffer-window :before #'doom-modeline-redisplay)
;; Keep `doom-modeline-current-window' up-to-date
(defun doom-modeline--get-current-window ()
"Get the current window but should exclude the child windows."
(if (and (fboundp 'frame-parent) (frame-parent))
(frame-selected-window (frame-parent))
(frame-selected-window)))
(defvar doom-modeline-current-window (doom-modeline--get-current-window))
(defun doom-modeline-set-selected-window (&rest _)
"Set `doom-modeline-current-window' appropriately."
(when-let ((win (doom-modeline--get-current-window)))
(unless (minibuffer-window-active-p win)
(setq doom-modeline-current-window win)
(force-mode-line-update))))
(defun doom-modeline-unset-selected-window ()
"Unset `doom-modeline-current-window' appropriately."
(setq doom-modeline-current-window nil)
(force-mode-line-update))
(add-hook 'window-configuration-change-hook #'doom-modeline-set-selected-window)
(add-hook 'buffer-list-update-hook #'doom-modeline-set-selected-window)
(add-hook 'after-make-frame-functions #'doom-modeline-set-selected-window)
(add-hook 'delete-frame-functions #'doom-modeline-set-selected-window)
(advice-add #'handle-switch-frame :after #'doom-modeline-set-selected-window)
(with-no-warnings
(cond ((not (boundp 'after-focus-change-function))
(add-hook 'focus-in-hook #'doom-modeline-set-selected-window)
(add-hook 'focus-out-hook #'doom-modeline-unset-selected-window))
((defun doom-modeline-refresh-frame ()
(setq doom-modeline-current-window nil)
(cl-loop for frame in (frame-list)
if (eq (frame-focus-state frame) t)
return (setq doom-modeline-current-window (frame-selected-window frame)))
(force-mode-line-update))
(add-function :after after-focus-change-function #'doom-modeline-refresh-frame))))
;; Ensure modeline is inactive when Emacs is unfocused (and active otherwise)
(defvar doom-modeline-remap-face-cookie nil)
(defun doom-modeline-focus ()
"Focus mode-line."
(when doom-modeline-remap-face-cookie
(require 'face-remap)
(face-remap-remove-relative doom-modeline-remap-face-cookie)))
(defun doom-modeline-unfocus ()
"Unfocus mode-line."
(setq doom-modeline-remap-face-cookie (face-remap-add-relative 'mode-line 'mode-line-inactive)))
(with-no-warnings
(if (boundp 'after-focus-change-function)
(progn
(defun doom-modeline-focus-change ()
(if (frame-focus-state)
(doom-modeline-focus)
(doom-modeline-unfocus)))
(add-function :after after-focus-change-function #'doom-modeline-focus-change))
(progn
(add-hook 'focus-in-hook #'doom-modeline-focus)
(add-hook 'focus-out-hook #'doom-modeline-unfocus))))
;;
;; Modeline helpers
;;
(defun doom-modeline--active ()
"Whether is an active window."
(eq (selected-window) doom-modeline-current-window))
(defsubst doom-modeline-vspc ()
"Text style with icons in mode-line."
(propertize " " 'face (if (doom-modeline--active)
'variable-pitch
'(:inherit (variable-pitch mode-line-inactive)))))
(defsubst doom-modeline-spc ()
"Text style with whitespace."
(propertize " " 'face (if (doom-modeline--active)
'mode-line
'mode-line-inactive)))
(defun doom-modeline--font-height ()
"Calculate the actual char height of the mode-line."
(let ((height (face-attribute 'mode-line :height)))
(round (* 1.68 (cond ((integerp height) (/ height 10))
((floatp height) (* height (frame-char-height)))
(t (frame-char-height)))))))
(defun doom-modeline-icon (icon-set icon-name unicode text face &rest args)
"Display icon of ICON-NAME with FACE and ARGS in mode-line.
ICON-SET includes `octicon', `faicon', `material', `alltheicons' and `fileicon'.
UNICODE is the unicode char fallback. TEXT is the ASCII char fallback."
(let ((face (or face 'mode-line)))
(or (when (and icon-name (not (string-empty-p icon-name)))
(pcase icon-set
('octicon
(apply #'doom-modeline-icon-octicon icon-name :face face args))
('faicon
(apply #'doom-modeline-icon-faicon icon-name :face face args))
('material
(apply #'doom-modeline-icon-material icon-name :face face args))
('alltheicon
(apply #'doom-modeline-icon-alltheicon icon-name :face face args))
('fileicon
(apply #'doom-modeline-icon-fileicon icon-name :face face args))))
(when (and doom-modeline-unicode-fallback
unicode
(not (string-empty-p unicode))
(char-displayable-p (string-to-char unicode)))
(propertize unicode 'face face))
(when text (propertize text 'face face)))))
(defun doom-modeline-icon-octicon (icon-name &rest args)
"Display octicon with ARGS."
(when doom-modeline-icon
(apply #'all-the-icons-octicon icon-name args)))
(defun doom-modeline-icon-faicon (icon-name &rest args)
"Display font awesome icon with ARGS."
(when doom-modeline-icon
(apply #'all-the-icons-faicon icon-name args)))
(defun doom-modeline-icon-material (icon-name &rest args)
"Display material icon with ARGS."
(when doom-modeline-icon
(apply #'all-the-icons-material icon-name args)))
(defun doom-modeline-icon-alltheicon (icon-name &rest args)
"Display alltheicon with ARGS."
(when doom-modeline-icon
(apply #'all-the-icons-alltheicon icon-name args)))
(defun doom-modeline-icon-fileicon (icon-name &rest args)
"Display fileicon with ARGS."
(when doom-modeline-icon
(apply #'all-the-icons-fileicon icon-name args)))
(defun doom-modeline-icon-for-mode (&rest args)
"Display icon for major mode with ARGS."
(when doom-modeline-icon
(apply #'all-the-icons-icon-for-mode args)))
(defun doom-modeline-icon-for-file (&rest args)
"Display icon for major mode with ARGS."
(when doom-modeline-icon
(apply #'all-the-icons-icon-for-file args)))
(defvar-local doom-modeline-project-root nil)
(defun doom-modeline-project-root ()
"Get the path to the root of your project.
Return `default-directory' if no project was found."
(setq doom-modeline-project-root
(or doom-modeline-project-root
(pcase doom-modeline-project-detection
('ffip
(when (fboundp 'ffip-get-project-root-directory)
(let ((inhibit-message t))
(ffip-get-project-root-directory))))
('projectile
(when (fboundp 'projectile-project-root)
(projectile-project-root)))
('project
(when (fboundp 'project-current)
(when-let ((project (project-current)))
(car (project-roots project))))))
default-directory)))
(defun doom-modeline--make-xpm (face width height)
"Create an XPM bitmap via FACE, WIDTH and HEIGHT. Inspired by `powerline''s `pl/make-xpm'."
(when (and (display-graphic-p)
(image-type-available-p 'xpm))
(propertize
" " 'display
(let ((data (make-list height (make-list width 1)))
(color (or (face-background face nil t) "None")))
(ignore-errors
(create-image
(concat
(format
"/* XPM */\nstatic char * percent[] = {\n\"%i %i 2 1\",\n\". c %s\",\n\" c %s\","
(length (car data)) (length data) color color)
(apply #'concat
(cl-loop with idx = 0
with len = (length data)
for dl in data
do (cl-incf idx)
collect
(concat
"\""
(cl-loop for d in dl
if (= d 0) collect (string-to-char " ")
else collect (string-to-char "."))
(if (eq idx len) "\"};" "\",\n")))))
'xpm t :ascent 'center))))))
;; Fix: invalid-regexp "Trailing backslash" while handling $HOME on Windows
(defun doom-modeline-shrink-path--dirs-internal (full-path &optional truncate-all)
"Return fish-style truncated string based on FULL-PATH.
Optional parameter TRUNCATE-ALL will cause the function to truncate the last
directory too."
(let* ((home (expand-file-name "~"))
(path (replace-regexp-in-string
(s-concat "^" home) "~" full-path))
(split (s-split "/" path 'omit-nulls))
(split-len (length split))
shrunk)
(->> split
(--map-indexed (if (= it-index (1- split-len))
(if truncate-all (shrink-path--truncate it) it)
(shrink-path--truncate it)))
(s-join "/")
(setq shrunk))
(s-concat (unless (s-matches? (rx bos (or "~" "/")) shrunk) "/")
shrunk
(unless (s-ends-with? "/" shrunk) "/"))))
(advice-add #'shrink-path--dirs-internal :override #'doom-modeline-shrink-path--dirs-internal)
(defun doom-modeline-buffer-file-name ()
"Propertized variable `buffer-file-name' based on `doom-modeline-buffer-file-name-style'."
(let* ((buffer-file-name (file-local-name (or (buffer-file-name (buffer-base-buffer)) "")))
(buffer-file-truename (file-local-name (or buffer-file-truename (file-truename buffer-file-name) ""))))
(propertize
(pcase doom-modeline-buffer-file-name-style
(`truncate-upto-project
(doom-modeline--buffer-file-name buffer-file-name buffer-file-truename 'shrink))
(`truncate-from-project
(doom-modeline--buffer-file-name buffer-file-name buffer-file-truename nil 'shrink))
(`truncate-with-project
(doom-modeline--buffer-file-name buffer-file-name buffer-file-truename 'shrink 'shink 'hide))
(`truncate-except-project
(doom-modeline--buffer-file-name buffer-file-name buffer-file-truename 'shrink 'shink))
(`truncate-upto-root
(doom-modeline--buffer-file-name-truncate buffer-file-name buffer-file-truename))
(`truncate-all
(doom-modeline--buffer-file-name-truncate buffer-file-name buffer-file-truename t))
(`relative-to-project
(doom-modeline--buffer-file-name-relative buffer-file-name buffer-file-truename))
(`relative-from-project
(doom-modeline--buffer-file-name buffer-file-name buffer-file-truename nil nil 'hide))
(style
(propertize
(pcase style
(`file-name (file-name-nondirectory buffer-file-name))
(`buffer-name (buffer-name)))
'face
(let ((face (or (and (buffer-modified-p)
'doom-modeline-buffer-modified)
(and (doom-modeline--active)
'doom-modeline-buffer-file))))
(when face `(:inherit ,face))))))
'help-echo (concat buffer-file-truename
(unless (string= (file-name-nondirectory buffer-file-truename)
(buffer-name))
(concat "\n" (buffer-name)))
"\nmouse-1: Previous buffer\nmouse-3: Next buffer")
'local-map mode-line-buffer-identification-keymap)))
(defun doom-modeline--buffer-file-name-truncate (file-path true-file-path &optional truncate-tail)
"Propertized variable `buffer-file-name' that truncates every dir along path.
If TRUNCATE-TAIL is t also truncate the parent directory of the file."
(let ((dirs (shrink-path-prompt (file-name-directory true-file-path)))
(active (doom-modeline--active)))
(if (null dirs)
(propertize "%b" 'face (if active 'doom-modeline-buffer-file))
(let ((modified-faces (if (buffer-modified-p) 'doom-modeline-buffer-modified)))
(let ((dirname (car dirs))
(basename (cdr dirs))
(dir-faces (or modified-faces (if active 'doom-modeline-project-root-dir)))
(file-faces (or modified-faces (if active 'doom-modeline-buffer-file))))
(concat (propertize (concat dirname
(if truncate-tail (substring basename 0 1) basename)
"/")
'face (if dir-faces `(:inherit ,dir-faces)))
(propertize (file-name-nondirectory file-path)
'face (if file-faces `(:inherit ,file-faces)))))))))
(defun doom-modeline--buffer-file-name-relative (_file-path true-file-path &optional include-project)
"Propertized variable `buffer-file-name' showing directories relative to project's root only."
(let ((root (file-local-name (doom-modeline-project-root)))
(active (doom-modeline--active)))
(if (null root)
(propertize "%b" 'face (if active 'doom-modeline-buffer-file))
(let* ((modified-faces (if (buffer-modified-p) 'doom-modeline-buffer-modified))
(relative-dirs (file-relative-name (file-name-directory true-file-path)
(if include-project (concat root "../") root)))
(relative-faces (or modified-faces (if active 'doom-modeline-buffer-path)))
(file-faces (or modified-faces (if active 'doom-modeline-buffer-file))))
(if (equal "./" relative-dirs) (setq relative-dirs ""))
(concat (propertize relative-dirs 'face (if relative-faces `(:inherit ,relative-faces)))
(propertize (file-name-nondirectory true-file-path)
'face (if file-faces `(:inherit ,file-faces))))))))
(defun doom-modeline--buffer-file-name (file-path _true-file-path &optional truncate-project-root-parent truncate-project-relative-path hide-project-root-parent)
"Propertized variable `buffer-file-name' given by FILE-PATH.
If TRUNCATE-PROJECT-ROOT-PARENT is non-nil will be saved by truncating project
root parent down fish-shell style.
Example:
~/Projects/FOSS/emacs/lisp/comint.el => ~/P/F/emacs/lisp/comint.el
If TRUNCATE-PROJECT-RELATIVE-PATH is non-nil will be saved by truncating project
relative path down fish-shell style.
Example:
~/Projects/FOSS/emacs/lisp/comint.el => ~/Projects/FOSS/emacs/l/comint.el
If HIDE-PROJECT-ROOT-PARENT is non-nil will hide project root parent.
Example:
~/Projects/FOSS/emacs/lisp/comint.el => emacs/lisp/comint.el"
(let ((project-root (file-local-name (doom-modeline-project-root)))
(active (doom-modeline--active))
(modified-faces (if (buffer-modified-p) 'doom-modeline-buffer-modified)))
(let ((sp-faces (or modified-faces (if active 'doom-modeline-project-parent-dir)))
(project-faces (or modified-faces (if active 'doom-modeline-project-dir)))
(relative-faces (or modified-faces (if active 'doom-modeline-buffer-path)))
(file-faces (or modified-faces (if active 'doom-modeline-buffer-file))))
(concat
;; project root parent
(unless hide-project-root-parent
(when-let (root-path-parent
(file-name-directory (directory-file-name project-root)))
(propertize
(if (and truncate-project-root-parent
(not (string-empty-p root-path-parent))
(not (string= root-path-parent "/")))
(shrink-path--dirs-internal root-path-parent t)
(abbreviate-file-name root-path-parent))
'face sp-faces)))
;; project
(propertize
(concat (file-name-nondirectory (directory-file-name project-root)) "/")
'face project-faces)
;; relative path
(propertize
(when-let (relative-path (file-relative-name
(or (file-name-directory file-path) "./")
project-root))
(if (string= relative-path "./")
""
(if truncate-project-relative-path
(substring (shrink-path--dirs-internal relative-path t) 1)
relative-path)))
'face relative-faces)
;; file name
(propertize (file-name-nondirectory file-path) 'face file-faces)))))
(provide 'doom-modeline-core)
;;; doom-modeline-core.el ends here