forked from seagle0128/doom-modeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doom-modeline-segments.el
3176 lines (2891 loc) · 132 KB
/
doom-modeline-segments.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
;;; doom-modeline-segments.el --- The segments for doom-modeline -*- lexical-binding: t; -*-
;; Copyright (C) 2018-2023 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 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:
;;
;; The segments for doom-modeline.
;; Use `doom-modeline-def-segment' to create a new segment.
;;
;;; Code:
(require 'doom-modeline-core)
(require 'doom-modeline-env)
(eval-when-compile
(require 'cl-lib)
(require 'seq)
(require 'subr-x))
;;
;; Externals
;;
(defvar Info-current-file)
(defvar Info-current-node)
(defvar Info-mode-line-node-keymap)
(defvar anzu--cached-count)
(defvar anzu--current-position)
(defvar anzu--overflow-p)
(defvar anzu--state)
(defvar anzu--total-matched)
(defvar anzu-cons-mode-line-p)
(defvar aw-keys)
(defvar battery-echo-area-format)
(defvar battery-load-critical)
(defvar battery-mode-line-format)
(defvar battery-mode-line-limit)
(defvar battery-status-function)
(defvar boon-command-state)
(defvar boon-insert-state)
(defvar boon-off-state)
(defvar boon-special-state)
(defvar display-time-string)
(defvar edebug-execution-mode)
(defvar eglot--managed-mode)
(defvar erc-modified-channels-alist)
(defvar evil-ex-active-highlights-alist)
(defvar evil-ex-argument)
(defvar evil-ex-range)
(defvar evil-mc-frozen)
(defvar evil-state)
(defvar evil-visual-beginning)
(defvar evil-visual-end)
(defvar evil-visual-selection)
(defvar flycheck--automatically-enabled-checkers)
(defvar flycheck-current-errors)
(defvar flycheck-mode-menu-map)
(defvar flymake--mode-line-format)
(defvar flymake--state)
(defvar flymake-menu)
(defvar gnus-newsrc-alist)
(defvar gnus-newsrc-hashtb)
(defvar grip--process)
(defvar helm--mode-line-display-prefarg)
(defvar iedit-occurrences-overlays)
(defvar meow--indicator)
(defvar minions-mode-line-lighter)
(defvar minions-mode-line-minor-modes-map)
(defvar mlscroll-minimum-current-width)
(defvar mlscroll-right-align)
(defvar mu4e--modeline-item)
(defvar mu4e-alert-mode-line)
(defvar mu4e-alert-modeline-formatter)
(defvar mu4e-modeline-mode)
(defvar nyan-minimum-window-width)
(defvar objed--obj-state)
(defvar objed--object)
(defvar objed-modeline-setup-func)
(defvar persp-nil-name)
(defvar phi-replace--mode-line-format)
(defvar phi-search--overlays)
(defvar phi-search--selection)
(defvar phi-search-mode-line-format)
(defvar poke-line-minimum-window-width)
(defvar rcirc-activity)
(defvar sml-modeline-len)
(defvar symbol-overlay-keywords-alist)
(defvar symbol-overlay-temp-symbol)
(defvar text-scale-mode-amount)
(defvar tracking-buffers)
(defvar winum-auto-setup-mode-line)
(defvar xah-fly-insert-state-p)
(declare-function anzu--reset-status "ext:anzu")
(declare-function anzu--where-is-here "ext:anzu")
(declare-function async-inject-variables "ext:async")
(declare-function async-start "ext:async")
(declare-function avy-traverse "ext:avy")
(declare-function avy-tree "ext:avy")
(declare-function aw-update "ext:ace-window")
(declare-function aw-window-list "ext:ace-window")
(declare-function battery-format "battery")
(declare-function battery-update "battery")
(declare-function boon-modeline-string "ext:boon")
(declare-function boon-state-string "ext:boon")
(declare-function cider--connection-info "ext:cider")
(declare-function cider-connected-p "ext:cider")
(declare-function cider-current-repl "ext:cider")
(declare-function cider-jack-in "ext:cider")
(declare-function cider-quit "ext:cider")
(declare-function citre-mode "ext:citre-basic-tools")
(declare-function compilation-goto-in-progress-buffer "compile")
(declare-function dap--cur-session "ext:dap-mode")
(declare-function dap--debug-session-name "ext:dap-mode")
(declare-function dap--debug-session-state "ext:dap-mode")
(declare-function dap--session-running "ext:dap-mode")
(declare-function dap-debug-recent "ext:dap-mode")
(declare-function dap-disconnect "ext:dap-mode")
(declare-function dap-hydra "ext:dap-hydra")
(declare-function edebug-help "edebug")
(declare-function edebug-next-mode "edebug")
(declare-function edebug-stop "edebug")
(declare-function eglot "ext:eglot")
(declare-function eglot--major-modes "ext:eglot" t t)
(declare-function eglot--project-nickname "ext:eglot" t t)
(declare-function eglot-clear-status "ext:eglot")
(declare-function eglot-current-server "ext:eglot")
(declare-function eglot-events-buffer "ext:eglot")
(declare-function eglot-forget-pending-continuations "ext:eglot")
(declare-function eglot-managed-p "ext:glot")
(declare-function eglot-reconnect "ext:eglot")
(declare-function eglot-shutdown "ext:eglot")
(declare-function eglot-stderr-buffer "ext:eglot")
(declare-function erc-switch-to-buffer "erc")
(declare-function erc-track-switch-buffer "erc-track")
(declare-function evil-delimited-arguments "ext:evil-common")
(declare-function evil-emacs-state-p "ext:evil-states" t t)
(declare-function evil-force-normal-state "ext:evil-commands" t t)
(declare-function evil-insert-state-p "ext:evil-states" t t)
(declare-function evil-motion-state-p "ext:evil-states" t t)
(declare-function evil-normal-state-p "ext:evil-states" t t)
(declare-function evil-operator-state-p "ext:evil-states" t t)
(declare-function evil-replace-state-p "ext:evil-states" t t)
(declare-function evil-state-property "ext:evil-common")
(declare-function evil-visual-state-p "ext:evil-states" t t)
(declare-function eyebrowse--get "ext:eyebrowse")
(declare-function face-remap-remove-relative "face-remap")
(declare-function fancy-narrow-active-p "ext:fancy-narrow")
(declare-function flycheck-buffer "ext:flycheck")
(declare-function flycheck-count-errors "ext:flycheck")
(declare-function flycheck-error-level-compilation-level "ext:flycheck")
(declare-function flycheck-list-errors "ext:flycheck")
(declare-function flycheck-next-error "ext:flycheck")
(declare-function flycheck-previous-error "ext:flycheck")
(declare-function flymake--diag-type "ext:flymake" t t)
(declare-function flymake--handle-report "ext:flymake")
(declare-function flymake--lookup-type-property "ext:flymake")
(declare-function flymake--state-diags "ext:flymake" t t)
(declare-function flymake-disabled-backends "ext:flymake")
(declare-function flymake-goto-next-error "ext:flymake")
(declare-function flymake-goto-prev-error "ext:flymake")
(declare-function flymake-reporting-backends "ext:flymake")
(declare-function flymake-running-backends "ext:flymake")
(declare-function flymake-show-buffer-diagnostics "ext:flymake")
(declare-function flymake-show-buffer-diagnostics "ext:flymake")
(declare-function flymake-start "ext:flymake")
(declare-function follow-all-followers "follow")
(declare-function gnus-demon-add-handler "gnus-demon")
(declare-function grip--preview-url "ext:grip-mode")
(declare-function grip-browse-preview "ext:grip-mode")
(declare-function grip-restart-preview "ext:grip-mode")
(declare-function grip-stop-preview "ext:grip-mode")
(declare-function helm-candidate-number-at-point "ext:helm-core")
(declare-function helm-get-candidate-number "ext:helm-core")
(declare-function iedit-find-current-occurrence-overlay "ext:iedit-lib")
(declare-function iedit-prev-occurrence "ext:iedit-lib")
(declare-function image-get-display-property "image-mode")
(declare-function jsonrpc--request-continuations "ext:jsonrpc" t t)
(declare-function jsonrpc-last-error "ext:jsonrpc" t t)
(declare-function lsp--workspace-print "ext:lsp-mode")
(declare-function lsp-describe-session "ext:lsp-mode")
(declare-function lsp-workspace-folders-open "ext:lsp-mode")
(declare-function lsp-workspace-restart "ext:lsp-mode")
(declare-function lsp-workspace-shutdown "ext:lsp-mode")
(declare-function lsp-workspaces "ext:lsp-mode")
(declare-function lv-message "ext:lv")
(declare-function mc/num-cursors "ext:multiple-cursors-core")
(declare-function minions--prominent-modes "ext:minions")
(declare-function mlscroll-mode-line "ext:mlscroll")
(declare-function mu4e--modeline-string "ext:mu4e-modeline")
(declare-function mu4e-alert-default-mode-line-formatter "ext:mu4e-alert")
(declare-function mu4e-alert-enable-mode-line-display "ext:mu4e-alert")
(declare-function nyan-create "ext:nyan-mode")
(declare-function org-edit-src-save "ext:org-src")
(declare-function parrot-create "ext:parrot")
(declare-function pdf-cache-number-of-pages "ext:pdf-cache" t t)
(declare-function persp-add-buffer "ext:persp-mode")
(declare-function persp-contain-buffer-p "ext:persp-mode")
(declare-function persp-switch "ext:persp-mode")
(declare-function phi-search--initialize "ext:phi-search")
(declare-function poke-line-create "ext:poke-line")
(declare-function popup-create "ext:popup")
(declare-function popup-delete "ext:popup")
(declare-function rcirc-next-active-buffer "rcirc")
(declare-function rcirc-short-buffer-name "rcirc")
(declare-function rcirc-switch-to-server-buffer "rcirc")
(declare-function rcirc-window-configuration-change "rcirc")
(declare-function rime--should-enable-p "ext:rime")
(declare-function rime--should-inline-ascii-p "ext:rime")
(declare-function sml-modeline-create "ext:sml-modeline")
(declare-function symbol-overlay-assoc "ext:symbol-overlay")
(declare-function symbol-overlay-get-list "ext:symbol-overlay")
(declare-function symbol-overlay-get-symbol "ext:symbol-overlay")
(declare-function symbol-overlay-rename "ext:symbol-overlay")
(declare-function tab-bar--current-tab "tab-bar")
(declare-function tab-bar--current-tab-index "tab-bar")
(declare-function tracking-next-buffer "ext:tracking")
(declare-function tracking-previous-buffer "ext:tracking")
(declare-function tracking-shorten "ext:tracking")
(declare-function undo-tree-redo-1 "ext:undo-tree")
(declare-function undo-tree-undo-1 "ext:undo-tree")
(declare-function warning-numeric-level "warnings")
(declare-function window-numbering-clear-mode-line "ext:window-numbering")
(declare-function window-numbering-get-number-string "ext:window-numbering")
(declare-function window-numbering-install-mode-line "ext:window-numbering")
(declare-function winum--clear-mode-line "ext:winum")
(declare-function winum--install-mode-line "ext:winum")
(declare-function winum-get-number-string "ext:winum")
;;
;; Buffer information
;;
(defvar-local doom-modeline--buffer-file-icon nil)
(defun doom-modeline-update-buffer-file-icon (&rest _)
"Update file icon in mode-line."
(setq doom-modeline--buffer-file-icon
(when (and doom-modeline-major-mode-icon
(doom-modeline-icon-displayable-p))
(let ((icon (doom-modeline-icon-for-buffer)))
(propertize (if (or (null icon) (symbolp icon))
(doom-modeline-icon 'faicon "nf-fa-file_o" nil nil
:face 'nerd-icons-dsilver)
(doom-modeline-propertize-icon icon))
'help-echo (format "Major-mode: %s" (format-mode-line mode-name)))))))
(add-hook 'find-file-hook #'doom-modeline-update-buffer-file-icon)
(add-hook 'after-change-major-mode-hook #'doom-modeline-update-buffer-file-icon)
(add-hook 'clone-indirect-buffer-hook #'doom-modeline-update-buffer-file-icon)
(doom-modeline-add-variable-watcher
'doom-modeline-icon
(lambda (_sym val op _where)
(when (eq op 'set)
(setq doom-modeline-icon val)
(dolist (buf (buffer-list))
(with-current-buffer buf
(doom-modeline-update-buffer-file-icon))))))
(defun doom-modeline-buffer-file-state-icon (icon unicode text face)
"Displays an ICON of buffer state with FACE.
UNICODE and TEXT are the alternatives if it is not applicable.
Uses `nerd-icons-mdicon' to fetch the icon."
(doom-modeline-icon 'mdicon icon unicode text :face face))
(defvar-local doom-modeline--buffer-file-state-icon nil)
(defun doom-modeline-update-buffer-file-state-icon (&rest _)
"Update the buffer or file state in mode-line."
(setq doom-modeline--buffer-file-state-icon
(when doom-modeline-buffer-state-icon
(ignore-errors
(concat
(cond (buffer-read-only
(doom-modeline-buffer-file-state-icon
"nf-md-lock" "🔒" "%1*"
'doom-modeline-warning))
((and buffer-file-name (buffer-modified-p)
doom-modeline-buffer-modification-icon)
(doom-modeline-buffer-file-state-icon
"nf-md-content_save_edit" "💾" "%1*"
'doom-modeline-warning))
((and buffer-file-name
;; Avoid freezing while connection is lost
(not (file-remote-p buffer-file-name))
(not (file-exists-p buffer-file-name)))
(doom-modeline-buffer-file-state-icon
"nf-md-cancel" "🚫" "!"
'doom-modeline-urgent))
(t ""))
(when (or (buffer-narrowed-p)
(and (bound-and-true-p fancy-narrow-mode)
(fancy-narrow-active-p))
(bound-and-true-p dired-narrow-mode))
(doom-modeline-buffer-file-state-icon
"nf-md-unfold_less_horizontal" "↕" "><"
'doom-modeline-warning)))))))
(defvar-local doom-modeline--buffer-file-name nil)
(defun doom-modeline-update-buffer-file-name (&rest _)
"Update buffer file name in mode-line."
(setq doom-modeline--buffer-file-name
(ignore-errors
(save-match-data
(if buffer-file-name
(doom-modeline-buffer-file-name)
(propertize "%b"
'face 'doom-modeline-buffer-file
'mouse-face 'doom-modeline-highlight
'help-echo "Buffer name
mouse-1: Previous buffer\nmouse-3: Next buffer"
'local-map mode-line-buffer-identification-keymap))))))
(add-hook 'find-file-hook #'doom-modeline-update-buffer-file-name)
(add-hook 'after-save-hook #'doom-modeline-update-buffer-file-name)
(add-hook 'clone-indirect-buffer-hook #'doom-modeline-update-buffer-file-name)
(add-hook 'evil-insert-state-exit-hook #'doom-modeline-update-buffer-file-name)
(add-hook 'Info-selection-hook #'doom-modeline-update-buffer-file-name)
(advice-add #'rename-buffer :after #'doom-modeline-update-buffer-file-name)
(advice-add #'set-visited-file-name :after #'doom-modeline-update-buffer-file-name)
(advice-add #'pop-to-buffer :after #'doom-modeline-update-buffer-file-name)
(advice-add #'popup-create :after #'doom-modeline-update-buffer-file-name)
(advice-add #'popup-delete :after #'doom-modeline-update-buffer-file-name)
;; (advice-add #'primitive-undo :after #'doom-modeline-update-buffer-file-name)
;; (advice-add #'set-buffer-modified-p :after #'doom-modeline-update-buffer-file-name)
(with-no-warnings
(if (boundp 'after-focus-change-function)
(progn
(advice-add #'handle-switch-frame :after #'doom-modeline-update-buffer-file-name)
(add-function :after after-focus-change-function #'doom-modeline-update-buffer-file-name))
(progn
(add-hook 'focus-in-hook #'doom-modeline-update-buffer-file-name)
(add-hook 'focus-out-hook #'doom-modeline-update-buffer-file-name))))
(doom-modeline-add-variable-watcher
'doom-modeline-buffer-file-name-style
(lambda (_sym val op _where)
(when (eq op 'set)
(setq doom-modeline-buffer-file-name-style val)
(dolist (buf (buffer-list))
(with-current-buffer buf
(when buffer-file-name
(doom-modeline-update-buffer-file-name)))))))
(defsubst doom-modeline--buffer-mode-icon ()
"The icon of the current major mode."
(when (and doom-modeline-icon doom-modeline-major-mode-icon)
(when-let ((icon (or doom-modeline--buffer-file-icon
(doom-modeline-update-buffer-file-icon))))
(unless (string-empty-p icon)
(concat
(if doom-modeline-major-mode-color-icon
(doom-modeline-display-icon icon)
(doom-modeline-propertize-icon
icon
(doom-modeline-face)))
(doom-modeline-vspc))))))
(defsubst doom-modeline--buffer-state-icon ()
"The icon of the current buffer state."
(when doom-modeline-buffer-state-icon
(when-let ((icon (doom-modeline-update-buffer-file-state-icon)))
(unless (string-empty-p icon)
(concat
(doom-modeline-display-icon icon)
(doom-modeline-vspc))))))
(defsubst doom-modeline--buffer-simple-name ()
"The buffer simple name."
(propertize "%b"
'face (doom-modeline-face
(if (and doom-modeline-highlight-modified-buffer-name
(buffer-modified-p))
'doom-modeline-buffer-modified
'doom-modeline-buffer-file))
'mouse-face 'doom-modeline-highlight
'help-echo "Buffer name
mouse-1: Previous buffer\nmouse-3: Next buffer"
'local-map mode-line-buffer-identification-keymap))
(defsubst doom-modeline--buffer-name ()
"The current buffer name."
(when doom-modeline-buffer-name
(if (and (not (eq doom-modeline-buffer-file-name-style 'file-name))
doom-modeline--limited-width-p)
;; Only display the buffer name if the window is small, and doesn't
;; need to respect file-name style.
(doom-modeline--buffer-simple-name)
(when-let ((name (or doom-modeline--buffer-file-name
(doom-modeline-update-buffer-file-name))))
;; Check if the buffer is modified
(if (and doom-modeline-highlight-modified-buffer-name
(buffer-modified-p))
(propertize name 'face (doom-modeline-face 'doom-modeline-buffer-modified))
(doom-modeline-display-text name))))))
(doom-modeline-def-segment buffer-info
"Combined information about the current buffer.
Including the current working directory, the file name, and its state (modified,
read-only or non-existent)."
(concat
(doom-modeline-spc)
(doom-modeline--buffer-mode-icon)
(doom-modeline--buffer-state-icon)
(doom-modeline--buffer-name)))
(doom-modeline-def-segment buffer-info-simple
"Display only the current buffer's name, but with fontification."
(concat
(doom-modeline-spc)
(doom-modeline--buffer-mode-icon)
(doom-modeline--buffer-state-icon)
(doom-modeline--buffer-simple-name)))
(doom-modeline-def-segment calc
"Display calculator icons and info."
(concat
(doom-modeline-spc)
(when-let ((icon (doom-modeline-icon 'faicon "nf-fa-calculator" "🖩" "")))
(concat
(doom-modeline-display-icon icon)
(doom-modeline-vspc)))
(doom-modeline--buffer-simple-name)))
(doom-modeline-def-segment buffer-default-directory
"Displays `default-directory' with the icon and state.
This is for special buffers like the scratch buffer where knowing the current
project directory is important."
(let ((face (doom-modeline-face
(if (and buffer-file-name (buffer-modified-p))
'doom-modeline-buffer-modified
'doom-modeline-buffer-path))))
(concat
(doom-modeline-spc)
(and doom-modeline-major-mode-icon
(concat
(doom-modeline-icon
'octicon "nf-oct-file_directory_fill" "🖿" "" :face face)
(doom-modeline-vspc)))
(doom-modeline--buffer-state-icon)
(propertize (abbreviate-file-name default-directory) 'face face))))
(doom-modeline-def-segment buffer-default-directory-simple
"Displays `default-directory'.
This is for special buffers like the scratch buffer where knowing the current
project directory is important."
(let ((face (doom-modeline-face 'doom-modeline-buffer-path)))
(concat
(doom-modeline-spc)
(and doom-modeline-major-mode-icon
(concat
(doom-modeline-icon
'octicon "nf-oct-file_directory_fill" "🖿" "" :face face)
(doom-modeline-vspc)))
(propertize (abbreviate-file-name default-directory) 'face face))))
;;
;; Encoding
;;
(doom-modeline-def-segment buffer-encoding
"Displays the eol and the encoding style of the buffer."
(when doom-modeline-buffer-encoding
(let ((mouse-face 'doom-modeline-highlight))
(concat
(doom-modeline-spc)
;; eol type
(let ((eol (coding-system-eol-type buffer-file-coding-system)))
(when (or (eq doom-modeline-buffer-encoding t)
(and (eq doom-modeline-buffer-encoding 'nondefault)
(not (equal eol doom-modeline-default-eol-type))))
(propertize
(pcase eol
(0 "LF ")
(1 "CRLF ")
(2 "CR ")
(_ ""))
'face (doom-modeline-face)
'mouse-face mouse-face
'help-echo (format "End-of-line style: %s\nmouse-1: Cycle"
(pcase eol
(0 "Unix-style LF")
(1 "DOS-style CRLF")
(2 "Mac-style CR")
(_ "Undecided")))
'local-map (let ((map (make-sparse-keymap)))
(define-key map [mode-line mouse-1] 'mode-line-change-eol)
map))))
;; coding system
(let* ((sys (coding-system-plist buffer-file-coding-system))
(cat (plist-get sys :category))
(sym (if (memq cat
'(coding-category-undecided coding-category-utf-8))
'utf-8
(plist-get sys :name))))
(when (or (eq doom-modeline-buffer-encoding t)
(and (eq doom-modeline-buffer-encoding 'nondefault)
(not (eq cat 'coding-category-undecided))
(not (eq sym doom-modeline-default-coding-system))))
(propertize
(upcase (symbol-name sym))
'face (doom-modeline-face)
'mouse-face mouse-face
'help-echo 'mode-line-mule-info-help-echo
'local-map mode-line-coding-system-map)))
(doom-modeline-spc)))))
;;
;; Indentation
;;
(doom-modeline-def-segment indent-info
"Displays the indentation information."
(when doom-modeline-indent-info
(let ((do-propertize
(lambda (mode size)
(propertize
(format " %s %d " mode size)
'face (doom-modeline-face)))))
(if indent-tabs-mode
(funcall do-propertize "TAB" tab-width)
(let ((lookup-var
(seq-find (lambda (var)
(and var (boundp var) (symbol-value var)))
(cdr (assoc major-mode doom-modeline-indent-alist)) nil)))
(funcall do-propertize "SPC"
(if lookup-var
(symbol-value lookup-var)
tab-width)))))))
;;
;; Remote host
;;
(doom-modeline-def-segment remote-host
"Hostname for remote buffers."
(when default-directory
(when-let ((host (file-remote-p default-directory 'host)))
(propertize
(concat "@" host)
'face (doom-modeline-face 'doom-modeline-host)))))
;;
;; Major mode
;;
(doom-modeline-def-segment major-mode
"The major mode, including environment and text-scale info."
(propertize
(concat
(doom-modeline-spc)
(propertize (format-mode-line
(or (and (boundp 'delighted-modes)
(cadr (assq major-mode delighted-modes)))
mode-name))
'help-echo "Major mode\n\
mouse-1: Display major mode menu\n\
mouse-2: Show help for major mode\n\
mouse-3: Toggle minor modes"
'mouse-face 'doom-modeline-highlight
'local-map mode-line-major-mode-keymap)
(when (and doom-modeline-env-version doom-modeline-env--version)
(format "%s%s" (doom-modeline-vspc) doom-modeline-env--version))
(and (boundp 'text-scale-mode-amount)
(/= text-scale-mode-amount 0)
(format
(if (> text-scale-mode-amount 0)
" (%+d)"
" (%-d)")
text-scale-mode-amount))
(doom-modeline-spc))
'face (doom-modeline-face 'doom-modeline-buffer-major-mode)))
;;
;; Process
;;
(doom-modeline-def-segment process
"The process info."
(doom-modeline-display-text
(format-mode-line mode-line-process)))
;;
;; Minor modes
;;
(doom-modeline-def-segment minor-modes
(when doom-modeline-minor-modes
(let ((face (doom-modeline-face 'doom-modeline-buffer-minor-mode))
(mouse-face 'doom-modeline-highlight)
(help-echo "Minor mode
mouse-1: Display minor mode menu
mouse-2: Show help for minor mode
mouse-3: Toggle minor modes"))
(if (bound-and-true-p minions-mode)
`((:propertize ("" ,(minions--prominent-modes))
face ,face
mouse-face ,mouse-face
help-echo ,help-echo
local-map ,mode-line-minor-mode-keymap)
,(doom-modeline-spc)
(:propertize ("" ,(doom-modeline-icon 'octicon "nf-oct-gear" "⚙"
minions-mode-line-lighter
:face face))
mouse-face ,mouse-face
help-echo "Minions
mouse-1: Display minor modes menu"
local-map ,minions-mode-line-minor-modes-map)
,(doom-modeline-spc))
`((:propertize ("" minor-mode-alist)
face ,face
mouse-face ,mouse-face
help-echo ,help-echo
local-map ,mode-line-minor-mode-keymap)
,(doom-modeline-spc))))))
;;
;; VCS
;;
(defun doom-modeline-vcs-icon (icon &optional unicode text face)
"Displays the vcs ICON with FACE and VOFFSET.
UNICODE and TEXT are fallbacks.
Uses `nerd-icons-octicon' to fetch the icon."
(doom-modeline-icon 'devicon icon unicode text :face face))
(defvar-local doom-modeline--vcs-icon nil)
(defun doom-modeline-update-vcs-icon (&rest _)
"Update icon of vcs state in mode-line."
(setq doom-modeline--vcs-icon
(when (and vc-mode buffer-file-name)
(let* ((backend (vc-backend buffer-file-name))
(state (vc-state buffer-file-name backend)))
(cond ((memq state '(edited added))
(doom-modeline-vcs-icon "nf-dev-git_compare" "🔃" "*" 'doom-modeline-info))
((eq state 'needs-merge)
(doom-modeline-vcs-icon "nf-dev-git_merge" "🔀" "?" 'doom-modeline-info))
((eq state 'needs-update)
(doom-modeline-vcs-icon "nf-dev-git_pull_request" "⬇" "!" 'doom-modeline-warning))
((memq state '(removed conflict unregistered))
(doom-modeline-icon 'octicon "nf-oct-alert" "⚠" "!" :face 'doom-modeline-urgent))
(t
(doom-modeline-vcs-icon "nf-dev-git_branch" "" "@" 'doom-modeline-info)))))))
(add-hook 'find-file-hook #'doom-modeline-update-vcs-icon)
(add-hook 'after-save-hook #'doom-modeline-update-vcs-icon)
(advice-add #'vc-refresh-state :after #'doom-modeline-update-vcs-icon)
(doom-modeline-add-variable-watcher
'doom-modeline-icon
(lambda (_sym val op _where)
(when (eq op 'set)
(setq doom-modeline-icon val)
(dolist (buf (buffer-list))
(with-current-buffer buf
(doom-modeline-update-vcs-icon))))))
(doom-modeline-add-variable-watcher
'doom-modeline-unicode-fallback
(lambda (_sym val op _where)
(when (eq op 'set)
(setq doom-modeline-unicode-fallback val)
(dolist (buf (buffer-list))
(with-current-buffer buf
(doom-modeline-update-vcs-icon))))))
(defvar-local doom-modeline--vcs-text nil)
(defun doom-modeline-update-vcs-text (&rest _)
"Update text of vcs state in mode-line."
(setq doom-modeline--vcs-text
(when (and vc-mode buffer-file-name)
(let* ((backend (vc-backend buffer-file-name))
(state (vc-state buffer-file-name backend))
(str (if vc-display-status
(substring vc-mode (+ (if (eq backend 'Hg) 2 3) 2))
""))
(face (cond ((eq state 'needs-update)
'doom-modeline-warning)
((memq state '(removed conflict unregistered))
'doom-modeline-urgent)
(t 'doom-modeline-info))))
(propertize (if (length> str doom-modeline-vcs-max-length)
(concat
(substring str 0 (- doom-modeline-vcs-max-length 3))
doom-modeline-ellipsis)
str)
'mouse-face 'doom-modeline-highlight
'face `(:inherit (,face bold)))))))
(add-hook 'find-file-hook #'doom-modeline-update-vcs-text)
(add-hook 'after-save-hook #'doom-modeline-update-vcs-text)
(advice-add #'vc-refresh-state :after #'doom-modeline-update-vcs-text)
(doom-modeline-def-segment vcs
"Displays the current branch, colored based on its state."
(when-let ((icon doom-modeline--vcs-icon)
(text doom-modeline--vcs-text))
(concat
(doom-modeline-spc)
(propertize (concat
(doom-modeline-display-icon icon)
(doom-modeline-vspc)
(doom-modeline-display-text text))
'mouse-face 'doom-modeline-highlight
'help-echo (get-text-property 1 'help-echo vc-mode)
'local-map (get-text-property 1 'local-map vc-mode))
(doom-modeline-spc))))
;;
;; Checker
;;
(defun doom-modeline-checker-icon (icon unicode text face)
"Displays the checker ICON with FACE.
UNICODE and TEXT are fallbacks.
Uses `nerd-icons-mdicon' to fetch the icon."
(doom-modeline-icon 'mdicon icon unicode text :face face))
(defun doom-modeline-checker-text (text &optional face)
"Displays TEXT with FACE."
(propertize text 'face (or face 'mode-line)))
;; Flycheck
(defun doom-modeline--flycheck-count-errors ()
"Count the number of ERRORS, grouped by level.
Return an alist, where each ITEM is a cons cell whose `car' is an
error level, and whose `cdr' is the number of errors of that
level."
(let ((info 0) (warning 0) (error 0))
(mapc
(lambda (item)
(let ((count (cdr item)))
(pcase (flycheck-error-level-compilation-level (car item))
(0 (cl-incf info count))
(1 (cl-incf warning count))
(2 (cl-incf error count)))))
(flycheck-count-errors flycheck-current-errors))
`((info . ,info) (warning . ,warning) (error . ,error))))
(defvar-local doom-modeline--flycheck-icon nil)
(defun doom-modeline-update-flycheck-icon (&optional status)
"Update flycheck icon via STATUS."
(setq doom-modeline--flycheck-icon
(when-let
((icon
(pcase status
('finished (if flycheck-current-errors
(let-alist (doom-modeline--flycheck-count-errors)
(doom-modeline-checker-icon
"nf-md-alert_circle_outline" "❗" "!"
(cond ((> .error 0) 'doom-modeline-urgent)
((> .warning 0) 'doom-modeline-warning)
(t 'doom-modeline-info))))
(doom-modeline-checker-icon "nf-md-check_circle_outline" "✔" "" 'doom-modeline-info)))
('running (doom-modeline-checker-icon "nf-md-timer_sand" "⏳" "*" 'doom-modeline-debug))
('no-checker (doom-modeline-checker-icon "nf-md-alert_box_outline" "⚠" "-" 'doom-modeline-debug))
('errored (doom-modeline-checker-icon "nf-md-alert_circle_outline" "⚠" "!" 'doom-modeline-urgent))
('interrupted (doom-modeline-checker-icon "nf-md-pause_circle_outline" "⦷" "." 'doom-modeline-debug))
('suspicious (doom-modeline-checker-icon "nf-md-information_outline" "❓" "?" 'doom-modeline-debug))
(_ nil))))
(propertize icon
'help-echo (concat "Flycheck\n"
(pcase status
('finished "mouse-1: Display minor mode menu
mouse-2: Show help for minor mode")
('running "Checking...")
('no-checker "No Checker")
('errored "Error")
('interrupted "Interrupted")
('suspicious "Suspicious")))
'mouse-face 'doom-modeline-highlight
'local-map (let ((map (make-sparse-keymap)))
(define-key map [mode-line down-mouse-1]
flycheck-mode-menu-map)
(define-key map [mode-line mouse-2]
(lambda ()
(interactive)
(describe-function 'flycheck-mode)))
map)))))
(add-hook 'flycheck-status-changed-functions #'doom-modeline-update-flycheck-icon)
(add-hook 'flycheck-mode-hook #'doom-modeline-update-flycheck-icon)
(doom-modeline-add-variable-watcher
'doom-modeline-icon
(lambda (_sym val op _where)
(when (eq op 'set)
(setq doom-modeline-icon val)
(dolist (buf (buffer-list))
(with-current-buffer buf
(when (bound-and-true-p flycheck-mode)
(doom-modeline-update-flycheck-icon)))))))
(doom-modeline-add-variable-watcher
'doom-modeline-unicode-fallback
(lambda (_sym val op _where)
(when (eq op 'set)
(setq doom-modeline-unicode-fallback val)
(dolist (buf (buffer-list))
(with-current-buffer buf
(when (bound-and-true-p flycheck-mode)
(doom-modeline-update-flycheck-icon)))))))
(defvar-local doom-modeline--flycheck-text nil)
(defun doom-modeline-update-flycheck-text (&optional status)
"Update flycheck text via STATUS."
(setq doom-modeline--flycheck-text
(when-let
((text
(pcase status
('finished (when flycheck-current-errors
(let-alist (doom-modeline--flycheck-count-errors)
(if doom-modeline-checker-simple-format
(doom-modeline-checker-text
(number-to-string (+ .error .warning .info))
(cond ((> .error 0) 'doom-modeline-urgent)
((> .warning 0) 'doom-modeline-warning)
(t 'doom-modeline-info)))
(format "%s/%s/%s"
(doom-modeline-checker-text (number-to-string .error)
'doom-modeline-urgent)
(doom-modeline-checker-text (number-to-string .warning)
'doom-modeline-warning)
(doom-modeline-checker-text (number-to-string .info)
'doom-modeline-info))))))
('running (and doom-modeline--flycheck-text
(propertize doom-modeline--flycheck-text 'face 'doom-modeline-debug)))
;; ('no-checker nil)
;; ('errored (doom-modeline-checker-text "Error" 'doom-modeline-urgent))
;; ('interrupted (doom-modeline-checker-text "Interrupted" 'doom-modeline-debug))
;; ('suspicious (doom-modeline-checker-text "Suspicious" 'doom-modeline-urgent))
(_ nil))))
(propertize
text
'help-echo (pcase status
('finished
(concat
(when flycheck-current-errors
(let-alist (doom-modeline--flycheck-count-errors)
(format "error: %d, warning: %d, info: %d\n" .error .warning .info)))
"mouse-1: Show all errors
mouse-3: Next error"
(when (doom-modeline-mwheel-available-p)
"\nwheel-up/wheel-down: Previous/next error")))
('running "Checking...")
('no-checker "No Checker")
('errored "Error")
('interrupted "Interrupted")
('suspicious "Suspicious"))
'mouse-face 'doom-modeline-highlight
'local-map (let ((map (make-sparse-keymap)))
(define-key map [mode-line mouse-1]
#'flycheck-list-errors)
(define-key map [mode-line mouse-3]
#'flycheck-next-error)
(when (doom-modeline-mwheel-available-p)
(define-key map [mode-line mouse-wheel-down-event]
(lambda (event)
(interactive "e")
(with-selected-window (posn-window (event-start event))
(flycheck-previous-error 1))))
(define-key map [mode-line mouse-wheel-up-event]
(lambda (event)
(interactive "e")
(with-selected-window (posn-window (event-start event))
(flycheck-next-error 1))))
map))))))
(add-hook 'flycheck-status-changed-functions #'doom-modeline-update-flycheck-text)
(add-hook 'flycheck-mode-hook #'doom-modeline-update-flycheck-text)
(doom-modeline-add-variable-watcher
'doom-modeline-checker-simple-format
(lambda (_sym val op _where)
(when (eq op 'set)
(setq doom-modeline-checker-simple-format val)
(dolist (buf (buffer-list))
(with-current-buffer buf
(when (bound-and-true-p flycheck-mode)
(doom-modeline-update-flycheck-text)))))))
;; Flymake
;; Compatibility
;; @see https://github.com/emacs-mirror/emacs/commit/6e100869012da9244679696634cab6b9cac96303.
(with-eval-after-load 'flymake
(unless (boundp 'flymake--state)
(defvaralias 'flymake--state 'flymake--backend-state))
(unless (fboundp 'flymake--state-diags)
(defalias 'flymake--state-diags 'flymake--backend-state-diags)))
(defvar-local doom-modeline--flymake-icon nil)
(defun doom-modeline-update-flymake-icon (&rest _)
"Update flymake icon."
(setq flymake--mode-line-format nil) ; remove the lighter of minor mode
(setq doom-modeline--flymake-icon
(let* ((known (hash-table-keys flymake--state))
(running (flymake-running-backends))
(disabled (flymake-disabled-backends))
(reported (flymake-reporting-backends))
(all-disabled (and disabled (null running)))
(some-waiting (cl-set-difference running reported)))
(when-let
((icon
(cond
(some-waiting (doom-modeline-checker-icon "nf-md-timer_sand" "⏳" "*" 'doom-modeline-debug))
((null known) (doom-modeline-checker-icon "nf-md-alert_box_outline" "⚠" "!" 'doom-modeline-urgent))
(all-disabled (doom-modeline-checker-icon "nf-md-alert_outline" "⚠" "!" 'doom-modeline-warning))
(t (let ((.error 0)
(.warning 0)
(.note 0))
(progn
(cl-loop
with warning-level = (warning-numeric-level :warning)
with note-level = (warning-numeric-level :debug)
for state being the hash-values of flymake--state
do (cl-loop
with diags = (flymake--state-diags state)
for diag in diags do
(let ((severity (flymake--lookup-type-property (flymake--diag-type diag) 'severity
(warning-numeric-level :error))))
(cond ((> severity warning-level) (cl-incf .error))
((> severity note-level) (cl-incf .warning))
(t (cl-incf .note))))))
(if (> (+ .error .warning .note) 0)
(doom-modeline-checker-icon "nf-md-alert_circle_outline" "❗" "!"
(cond ((> .error 0) 'doom-modeline-urgent)
((> .warning 0) 'doom-modeline-warning)
(t 'doom-modeline-info)))
(doom-modeline-checker-icon "nf-md-check_circle_outline" "✔" "-" 'doom-modeline-info))))))))
(propertize
icon
'help-echo (concat "Flymake\n"
(cond
(some-waiting "Checking...")
((null known) "No Checker")
(all-disabled "All Checkers Disabled")
(t (format "%d/%d backends running
mouse-1: Display minor mode menu
mouse-2: Show help for minor mode"
(length running) (length known)))))
'mouse-face 'doom-modeline-highlight
'local-map (let ((map (make-sparse-keymap)))
(define-key map [mode-line down-mouse-1]
flymake-menu)
(define-key map [mode-line mouse-2]
(lambda ()
(interactive)
(describe-function 'flymake-mode)))
map))))))
(advice-add #'flymake--handle-report :after #'doom-modeline-update-flymake-icon)
(doom-modeline-add-variable-watcher
'doom-modeline-icon
(lambda (_sym val op _where)
(when (eq op 'set)
(setq doom-modeline-icon val)
(dolist (buf (buffer-list))
(with-current-buffer buf
(when (bound-and-true-p flymake-mode)
(doom-modeline-update-flymake-icon)))))))
(doom-modeline-add-variable-watcher
'doom-modeline-unicode-fallback
(lambda (_sym val op _where)
(when (eq op 'set)
(setq doom-modeline-unicode-fallback val)
(dolist (buf (buffer-list))
(with-current-buffer buf