-
Notifications
You must be signed in to change notification settings - Fork 8
/
lambda-line.el
1840 lines (1557 loc) · 70.2 KB
/
lambda-line.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
;;; lambda-line.el --- A custom status line -*- lexical-binding: t -*-
;; Author: Colin McLear
;; Maintainer: Colin McLear
;; Version: 0.2.0
;; Package-Requires: ((emacs "27.1") (all-the-icons "5.0.0"))
;; Homepage: https://github.com/Lambda-Emacs/lambda-line
;; Keywords: mode-line faces
;; 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:
;; lambda-line is a minimal, though opinionated, status-line (i.e. in Emacs the
;; information display either in the mode-line and/or header-line) for use as
;; either header or footer in a buffer. The structure of the status-line takes
;; the following form: [ status | name (primary) tertiary | secondary ]
;; Usage: M-x lambda-line-mode
;;; Code:
(require 'face-remap)
(require 'cl-lib)
(require 'all-the-icons)
;;;; Group
(defgroup lambda-line nil
"lambda-line group"
:group 'mode-line
:link '(url-link :tag "Homepage" "https://github.com/Lambda-Emacs/lambda-line"))
;;;; Custom Variable Settings
(defcustom lambda-line-window-width-limit 0.25
"The limit of the window width.
If `window-width' is smaller than the limit, some information won't be
displayed. It can be an integer or a float number. `nil' means no limit."
:type '(choice integer
float
(const :tag "Disable" nil))
:group 'lambda-line)
(defcustom lambda-line-position 'bottom
"Default modeline position (top or bottom)"
:type '(choice
(const :tag "Nil" nil)
(const :tag "Top" top)
(const :tag "Bottom" bottom))
:group 'lambda-line)
(defcustom lambda-line-prefix t
"Include a prefix icon to indicate buffer status in the status-line."
:type 'boolean
:group 'lambda-line)
(defcustom lambda-line-prefix-padding t
"Include prefix padding."
:type 'boolean
:group 'lambda-line)
(defcustom lambda-line-user-mode nil
"User supplied mode to be evaluated for modeline."
:type '(choice (const nil) function)
:group 'lambda-line)
(defcustom lambda-line-abbrev nil
"If t then show abbreviated mode symbol in modeline.
Default is nil. To change the values of the major-mode symbols
see the value of `lambda-line-abbrev-alist'"
:group 'lambda-line
:type 'boolean)
(defcustom lambda-line-git-diff-mode-line t
"If t then show diff lines in modeline."
:group 'lambda-line
:type 'boolean)
(defcustom lambda-line-vc-symbol ""
"Symbol to use in buffers visiting files under version control"
:group 'lambda-line
:type 'string)
;; Visual Bell
(defcustom lambda-line-visual-bell t
"If t then use `lambda-line-visual-bell'."
:group 'lambda-line
:type 'boolean)
;; Invert status faces
;; This make lambda-line look more like nano-modeline
(defcustom lambda-line-status-invert nil
"If t then invert the colors to get a box effect for the corner of the status line."
:group 'lambda-line
:type 'boolean)
;; Mode line symbols
(defcustom lambda-line-gui-ro-symbol " ⨂" ;; ⬤◯⨂
"Modeline gui read-only symbol."
:group 'lambda-line
:type 'string)
(defcustom lambda-line-gui-mod-symbol " ⬤" ;; ⨀⬤
"Modeline gui modified symbol."
:group 'lambda-line
:type 'string)
(defcustom lambda-line-gui-rw-symbol " ◯" ; λ ◉ ◎ ⬤◯
"Modeline gui read-write symbol."
:group 'lambda-line
:type 'string)
(defcustom lambda-line-tty-ro-symbol " λ "
"Modeline tty read-only symbol."
:group 'lambda-line
:type 'string)
(defcustom lambda-line-tty-mod-symbol " λ "
"Modeline tty read-only symbol."
:group 'lambda-line
:type 'string)
(defcustom lambda-line-tty-rw-symbol " λ "
"Modeline tty read-write symbol."
:group 'lambda-line
:type 'string)
(defcustom lambda-line-truncate-value 30
"Value of modeline truncate-length function."
:group 'lambda-line
:type 'integer)
(defcustom lambda-line-hspace " "
"Space adjustment for right end of modeline."
:type 'string
:group 'lambda-line)
(defcustom lambda-line-space-top +.35
"Space adjustment for top of status-line.
Positive is upwards"
:type 'float
:group 'lambda-line)
(defcustom lambda-line-space-bottom -.5
"Space adjustment for bottom of status-line.
Negative is downwards."
:type 'float
:group 'lambda-line)
(defcustom lambda-line-symbol-position .067
"Space adjustment for symbol in status-line.
Negative is downwards."
:type 'float
:group 'lambda-line)
(defcustom lambda-line-syntax t
"Show flycheck/flymake report in status-line."
:type 'boolean
:group 'lambda-line)
(defcustom lambda-line-which-func nil
"Show `which-function-mode' display in status-line."
:type 'boolean
:group 'lambda-line)
(defcustom lambda-line-flycheck-label "Issues: "
"Show with flycheck/flymake issues count."
:type 'string
:group 'lambda-line)
(defcustom lambda-line-icon-time nil
"When set to non-nil show the time as an icon clock.
Time info is only shown `display-time-mode' is non-nil"
:type 'boolean
:group 'lambda-line)
(defcustom lambda-line-time-day-and-date-format " %H:%M %Y-%m-%e "
"`format-time-string'."
:type 'string
:group 'lambda-line)
(defcustom lambda-line-time-format " %H:%M "
"`format-time-string'."
:type 'string
:group 'lambda-line)
(defcustom lambda-line-time-icon-format " %s"
"`format-time-string'."
:type 'string
:group 'lambda-line)
(defcustom lambda-line-display-group-start "("
"Modeline display group start indicator."
:group 'lambda-line
:type 'string)
(defcustom lambda-line-display-group-end ")"
"Modeline display group end indicator."
:group 'lambda-line
:type 'string)
(defcustom lambda-line-mode-formats
'(;; with :mode-p first
(imenu-list-mode :mode-p lambda-line-imenu-list-mode-p
:format lambda-line-imenu-list-mode)
(org-capture-mode :mode-p lambda-line-org-capture-mode-p
:format lambda-line-org-capture-mode
:on-activate lambda-line-org-capture-activate
:on-deactivate lambda-line-org-capture-deactivate)
(prog-mode :mode-p lambda-line-prog-mode-p
:format lambda-line-prog-mode
:on-activate lambda-line-prog-activate
:on-deactivate lambda-line-prog-deactivate)
(mu4e-dashboard-mode :mode-p lambda-line-mu4e-dashboard-mode-p
:format lambda-line-mu4e-dashboard-mode)
(messages-mode :mode-p lambda-line-messages-mode-p
:format lambda-line-messages-mode)
(message-mode :mode-p lambda-line-message-mode-p
:format lambda-line-message-mode)
(term-mode :mode-p lambda-line-term-mode-p
:format lambda-line-term-mode)
(vterm-mode :mode-p lambda-line-vterm-mode-p
:format lambda-line-term-mode)
(eshell-mode :mode-p lambda-line-eshell-mode-p
:format lambda-line-eshell-mode)
(buffer-menu-mode :mode-p lambda-line-buffer-menu-mode-p
:format lambda-line-buffer-menu-mode
:on-activate lambda-line-buffer-menu-activate
:on-deactivate lambda-line-buffer-menu-deactivate)
(calendar-mode :mode-p lambda-line-calendar-mode-p
:format lambda-line-calendar-mode
:on-activate lambda-line-calendar-activate
:on-deactivate lambda-line-calendar-deactivate)
(completion-list-mode :mode-p lambda-line-completion-list-mode-p
:format lambda-line-completion-list-mode)
(deft-mode :mode-p lambda-line-deft-mode-p
:format lambda-line-deft-mode)
(doc-view-mode :mode-p lambda-line-doc-view-mode-p
:format lambda-line-doc-view-mode)
(elfeed-search-mode :mode-p lambda-line-elfeed-search-mode-p
:format lambda-line-elfeed-search-mode
:on-activate lambda-line-elfeed-search-activate
:on-deactivate lambda-line-elfeed-search-deactivate)
(elfeed-show-mode :mode-p lambda-line-elfeed-show-mode-p
:format lambda-line-elfeed-show-mode)
(elpher-mode :mode-p lambda-line-elpher-mode-p
:format lambda-line-elpher-mode
:on-activate lambda-line-elpher-activate)
(help-mode :mode-p lambda-line-help-mode-p
:format lambda-line-help-mode)
(helpful-mode :mode-p lambda-line-helpful-mode-p
:format lambda-line-help-mode)
(info-mode :mode-p lambda-line-info-mode-p
:format lambda-line-info-mode
:on-activate lambda-line-info-activate
:on-deactivate lambda-line-info-deactivate)
(magit-mode :mode-p lambda-line-magit-mode-p
:format lambda-line-magit-mode)
(mu4e-compose-mode :mode-p lambda-line-mu4e-compose-mode-p
:format lambda-line-mu4e-compose-mode)
(mu4e-headers-mode :mode-p lambda-line-mu4e-headers-mode-p
:format lambda-line-mu4e-headers-mode)
(mu4e-loading-mode :mode-p lambda-line-mu4e-loading-mode-p
:format lambda-line-mu4e-loading-mode)
(mu4e-main-mode :mode-p lambda-line-mu4e-main-mode-p
:format lambda-line-mu4e-main-mode)
(mu4e-view-mode :mode-p lambda-line-mu4e-view-mode-p
:format lambda-line-mu4e-view-mode)
(org-agenda-mode :mode-p lambda-line-org-agenda-mode-p
:format lambda-line-org-agenda-mode)
(org-clock-mode :mode-p lambda-line-org-clock-mode-p
:format lambda-line-org-clock-mode
:on-activate lambda-line-org-clock-activate
:on-deactivate lambda-line-org-clock-deactivate)
(pdf-view-mode :mode-p lambda-line-pdf-view-mode-p
:format lambda-line-pdf-view-mode)
(fundamental-mode :mode-p lambda-line-fundamental-mode-p
:format lambda-line-fundamental-mode)
(text-mode :mode-p lambda-line-text-mode-p
:format lambda-line-text-mode)
;; hooks only go last
(ein-notebook-mode :on-activate lambda-line-ein-notebook-activate
:on-deactivate lambda-line-ein-notebook-deactivate)
(esh-mode :on-activate lambda-line-esh-activate
:on-deactivate lambda-line-esh-deactivate)
(ispell-mode :on-activate lambda-line-ispell-activate
:on-deactivate lambda-line-ispell-deactivate)
(mu4e-mode :on-activate lambda-line-mu4e-activate
:on-deactivate lambda-line-mu4e-deactivate))
"Modes to be evalued for modeline.
KEY mode name, for reference only. Easier to do lookups and/or replacements.
:MODE-P the function to check if :FORMAT needs to be used, first one wins.
:ON-ACTIVATE and :ON-DEACTIVATE do hook magic on enabling/disabling the mode.
"
:type '(alist :key-type symbol
:value-type (plist :key-type (choice (const :mode-p)
(const :format)
(const :on-activate)
(const :on-deactivate))
:value-type function))
:group 'lambda-line)
(defcustom lambda-line-mode-format-activate-hook nil
"Add hooks on activation of the mode.
This is for those modes that define their own status-line."
:type 'hook
:options '(turn-on-auto-fill flyspell-mode)
:group 'lambda-line)
(defcustom lambda-line-mode-format-deactivate-hook nil
"Remove hooks on de-activation of the mode.
This is for those modes that define their own status-line."
:type 'hook
:options '(turn-on-auto-fill flyspell-mode)
:group 'lambda-line)
(defcustom lambda-line-default-mode-format 'lambda-line-default-mode
"Default mode to evaluate.
This is if no match could be found in `lambda-lines-mode-formats'"
:type 'function
:group 'lambda-line)
;;;; Faces
;;;;; Line Faces
(defface lambda-line-active
'((t (:inherit (mode-line))))
"Modeline face for active modeline."
:group 'lambda-line-active)
(defface lambda-line-inactive
'((t (:inherit (mode-line-inactive))))
"Modeline face for inactive line."
:group 'lambda-line-inactive)
(defface lambda-line-hspace-active
'((t (:invisible t :family "Monospace" :inherit (mode-line))))
"Face for vertical spacer in active line.")
(defface lambda-line-hspace-inactive
'((t (:invisible t :family "Monospace" :inherit (mode-line-inactive))))
"Face for vertical spacer in inactive line.")
(defface lambda-line-active-name
'((t (:inherit (mode-line))))
"Modeline face for active name element."
:group 'lambda-line-active)
(defface lambda-line-inactive-name
'((t (:inherit (mode-line-inactive))))
"Modeline face for inactive name element."
:group 'lambda-line-inactive)
(defface lambda-line-active-primary
'((t (:weight light :inherit (mode-line))))
"Modeline face for active primary element."
:group 'lambda-line-active)
(defface lambda-line-inactive-primary
'((t (:inherit (mode-line-inactive))))
"Modeline face for inactive primary element."
:group 'lambda-line-inactive)
(defface lambda-line-active-secondary
'((t (:inherit mode-line)))
"Modeline face for active secondary element."
:group 'lambda-line-active)
(defface lambda-line-inactive-secondary
'((t (:inherit (mode-line-inactive))))
"Modeline face for inactive secondary element."
:group 'lambda-line-inactive)
(defface lambda-line-active-tertiary
'((t (:inherit mode-line)))
"Modeline face for active tertiary element."
:group 'lambda-line-active)
(defface lambda-line-inactive-tertiary
'((t (:inherit (mode-line-inactive))))
"Modeline face for inactive tertiary element."
:group 'lambda-line-inactive)
;;;;; Status Bar Faces
;; lambda-line uses a colored symbol to indicate the status of the buffer
(defface lambda-line-active-status-RO
'((t (:inherit (mode-line) :foreground "yellow" (when lambda-line-status-invert :inverse-video t))))
"Modeline face for active READ-ONLY element."
:group 'lambda-line-active)
(defface lambda-line-inactive-status-RO
'((t (:inherit (mode-line-inactive) :foreground "light gray" (when lambda-line-status-invert :inverse-video t))))
"Modeline face for inactive READ-ONLY element."
:group 'lambda-line-inactive)
(defface lambda-line-active-status-RW
'((t (:inherit (mode-line) :foreground "green" (when lambda-line-status-invert :inverse-video t))))
"Modeline face for active READ-WRITE element."
:group 'lambda-line-active)
(defface lambda-line-inactive-status-RW
'((t (:inherit (mode-line-inactive) :foreground "light gray" (when lambda-line-status-invert :inverse-video t))))
"Modeline face for inactive READ-WRITE element."
:group 'lambda-line-inactive)
(defface lambda-line-active-status-MD
'((t (:inherit (mode-line) :foreground "red" (when lambda-line-status-invert :inverse-video t))))
"Modeline face for active MODIFIED element."
:group 'lambda-line-active)
(defface lambda-line-inactive-status-MD
'((t (:inherit (mode-line-inactive) :foreground "light gray" (when lambda-line-status-invert :inverse-video t))))
"Modeline face for inactive MODIFIED element."
:group 'lambda-line-inactive)
;;;;; Bell Faces
(defface lambda-line-visual-bell '((t (:background "red3")))
"Face to use for the mode-line when `lambda-line-visual-bell-config' is used."
:group 'lambda-line)
;;;; Setup Functions
;;;;; Visual bell for mode line
;; See https://github.com/hlissner/emacs-doom-themes for the basic idea
(defun lambda-line-visual-bell-fn ()
"Blink the status-line red briefly. Set `ring-bell-function' to this to use it."
(let ((lambda-line--bell-cookie (if (eq lambda-line-position 'bottom)
(face-remap-add-relative 'mode-line 'lambda-line-visual-bell)
(face-remap-add-relative 'header-line 'lambda-line-visual-bell)))
(force-mode-line-update t))
(run-with-timer 0.15 nil
(lambda (cookie buf)
(with-current-buffer buf
(face-remap-remove-relative cookie)
(force-mode-line-update t)))
lambda-line--bell-cookie
(current-buffer))))
(defun lambda-line-visual-bell-config ()
"Enable flashing the status-line on error."
(setq ring-bell-function #'lambda-line-visual-bell-fn
visible-bell t))
;;;;; Abbreviate Major-Mode
;; Source: https://www.masteringemacs.org/article/hiding-replacing-modeline-strings
(defcustom lambda-line-abbrev-alist
`((dired-mode . "Dir")
(emacs-lisp-mode . "λ")
(fundamental-mode . "F")
(helpful-mode . "?")
(help-mode . "?")
(lisp-interaction-mode . "λΙ")
(markdown-mode . "MD")
(magit-mode . "MG")
(nxhtml-mode . "NX")
(prog-mode . "PR")
(python-mode . "PY")
(text-mode . "TX"))
"Alist for `lambda-line--abbrev'.
When you add a new element to the alist, keep in mind that you
must pass the correct minor/major mode symbol and a string you
want to use in the modeline *as substitute for* the original."
:type '(alist
:key-type (symbol :tag "Major mode")
:value-type (string :tag "Abbreviation"))
:group 'lambda-line)
(defun lambda-line--abbrev ()
(cl-loop for abbrev in lambda-line-abbrev-alist
do (let* ((mode (car abbrev))
(mode-str (cdr abbrev))
(old-mode-str (cdr (assq mode minor-mode-alist))))
(when old-mode-str
(setcar old-mode-str mode-str))
;; major mode
(when (eq mode major-mode)
(setq mode-name mode-str)))))
;; Set abbrev (default is nil)
(when lambda-line-abbrev
(add-hook 'after-change-major-mode-hook #'lambda-line--abbrev))
;;;;; Mode Name
(defun lambda-line-user-mode-p ()
"Should the user supplied mode be called for modeline?"
lambda-line-user-mode)
(defun lambda-line-mode-name ()
"Return current major mode name."
(format-mode-line mode-name))
;;;;; String Truncate
(defun lambda-line-truncate (str size &optional ellipsis)
"If STR is longer than SIZE, truncate it and add ELLIPSIS."
(let ((ellipsis (or ellipsis "…")))
(if (> (length str) size)
(format "%s%s" (substring str 0 (- size (length ellipsis))) ellipsis)
str)))
;;;;; Branch display
;; -------------------------------------------------------------------
(defun lambda-line-project-name ()
"Return name of project without path."
(file-name-nondirectory (directory-file-name (if (vc-root-dir) (vc-root-dir) "-"))))
(defun lambda-line-vc-project-branch ()
"Show project and branch name for file.
Otherwise show '-'."
(let ((backend (vc-backend buffer-file-name)))
(concat
(if buffer-file-name
(if vc-mode
(let ((project-name (lambda-line-project-name)))
;; Project name
(unless (string= "-" project-name)
(concat
;; Divider
(propertize " •" 'face `(:inherit fringe))
(format " %s" project-name))))))
;; Show branch
(if vc-mode
(concat
lambda-line-vc-symbol (substring-no-properties vc-mode ;
(+ (if (eq backend 'Hg) 2 3) 2))) nil))))
;;;;; Dir display
;; From https://amitp.blogspot.com/2011/08/emacs-custom-mode-line.html
(defun lambda-line-shorten-directory (dir max-length)
"Show up to `max-length' characters of a directory name `dir'."
(let ((path (reverse (split-string (abbreviate-file-name dir) "/")))
(output ""))
(when (and path (equal "" (car path)))
(setq path (cdr path)))
(while (and path (< (length output) (- max-length 4)))
(setq output (concat (car path) "/" output))
(setq path (cdr path)))
(when path
(setq output (concat "…/" output)))
output))
;;;;; Git diff in modeline
;; https://cocktailmake.github.io/posts/emacs-modeline-enhancement-for-git-diff/
(when lambda-line-git-diff-mode-line
(defadvice vc-git-mode-line-string (after plus-minus (file) compile activate)
"Show the information of git diff in status-line"
(setq ad-return-value
(concat ad-return-value
(let ((plus-minus (vc-git--run-command-string
file "diff" "--numstat" "--")))
(if (and plus-minus
(string-match "^\\([0-9]+\\)\t\\([0-9]+\\)\t" plus-minus))
(concat
" "
(format "+%s" (match-string 1 plus-minus))
(format "-%s" (match-string 2 plus-minus)))
""))))))
;;;;; Git Parse Repo Status
;; See https://kitchingroup.cheme.cmu.edu/blog/2014/09/19/A-git-status-Emacs-modeline/
(defun lambda-line-git-parse-status ()
"Display the status of the repo."
(interactive)
(let ((U 0) ; untracked files
(M 0) ; modified files
(O 0) ; other files
(U-files "")
(M-files "")
(O-files ""))
(dolist (line (split-string
(shell-command-to-string "git status --porcelain")
"\n"))
(cond
;; ignore empty line at end
((string= "" line) nil)
((string-match "^\\?\\?" line)
(setq U (+ 1 U))
(setq U-files (concat U-files "\n" line)))
((string-match "^ M" line)
(setq M (+ 1 M))
(setq M-files (concat M-files "\n" line))
)))
;; construct propertized string
(concat
(propertize
(format "M:%d" M)
'face (if (> M 0)
'error
'success)
'help-echo M-files)
(propertize "|" 'face 'magit-dimmed)
(propertize
(format "?:%d" U)
'face (if (> U 0)
'warning
'success)
'help-echo U-files))))
;;;;; Flycheck/Flymake Segment
(defvar-local lambda-line--flycheck-text nil)
(defun lambda-line--update-flycheck-segment (&optional status)
"Update `lambda-line--flycheck-text' against the reported flycheck STATUS."
(setq lambda-line--flycheck-text
(pcase status
('finished (if flycheck-current-errors
(let-alist (flycheck-count-errors flycheck-current-errors)
(let ((sum (+ (or .error 0) (or .warning 0))))
(propertize (concat lambda-line-flycheck-label
(number-to-string sum)
" ")
'face (if .error
'error
'warning))))
(propertize "Good " 'face 'success)))
('running (propertize "Checking " 'face 'info))
('errored (propertize "Error " 'face 'error))
('interrupted (propertize "Paused " 'face 'fringe))
('no-checker ""))))
(defun lambda-line-check-syntax ()
"Display syntax-checking information from flymake/flycheck in the mode-line (if available)."
(if (and (>= emacs-major-version 28)
(boundp 'flymake-mode)
flymake-mode)
(concat (format-mode-line flymake-mode-line-format) " ")
lambda-line--flycheck-text))
(defun lambda-line-show-func ()
"Display `which-function-mode' output in mode-line."
(if (and (boundp 'which-function-mode)
(default-value 'which-function-mode))
(concat (format-mode-line which-func-format) " ")
""))
;;;;; Display-time-mode
(defun lambda-line-install-clockface-fonts ()
"Install ClockFace fonts on the local system.
Thanks to the Doom Emacs project, for the basis of this
cross-platform font dowload/install code."
(interactive)
(let ((on-mac (eq system-type 'darwin))
(on-linux (memq system-type '(gnu gnu/linux gnu/kfreebsd berkeley-unix)))
(on-windows (memq system-type '(cygwin windows-nt ms-dos)))
(name "ClockFace")
(url-format "https://ocodo.github.io/ClockFace-font/%s")
(fonts-list '("ClockFace-Regular.ttf"
"ClockFaceRect-Regular.ttf"
"ClockFaceSolid-Regular.ttf"
"ClockFaceRectSolid-Regular.ttf")))
(unless (yes-or-no-p
(format
"Download%sthe ClockFace fonts, continue?"
(if on-windows
" "
" and install ")))
(user-error "Aborted Download of ClockFace fonts"))
(let* ((font-dest
(cond (on-linux
(expand-file-name
"fonts/" (or (getenv "XDG_DATA_HOME")
"~/.local/share")))
(on-mac
(expand-file-name "~/Library/Fonts/"))))
(known-dest-p (stringp font-dest))
(font-dest (or font-dest (read-directory-name "Font installation directory: " "~/"))))
(unless (file-directory-p font-dest)
(mkdir font-dest t))
(dolist (font fonts-list)
(url-copy-file (format url-format font)
(expand-file-name font font-dest)
t))
(when known-dest-p
(message "Font downloaded, updating font cache... Using <fc-cache -f -v> ")
(shell-command-to-string "fc-cache -f -v"))
(if on-windows
(when (y-or-n-p "The %S font was downloaded, Windows users must install manually.\n\nOpen windows explorer?")
(call-process "explorer.exe" nil nil nil font-dest))
(message "Successfully %s %S fonts to %S!"
(if known-dest-p
"installed"
"downloaded")
name font-dest)))))
(defun lambda-line-clockface-select-font ()
"Select clockface icon font."
(interactive)
(let ((font (completing-read
"Select clockface icon font: "
'("ClockFace"
"ClockFaceSolid"
"ClockFaceRect"
"ClockFaceRectSolid"))))
(lambda-line-clockface-update-fontset font)))
(defun lambda-line-clockface-update-fontset (&optional font)
"Use ClockFace font for unicode #xF0000..F008F.
Optionally use another clockface font."
(set-fontset-font
"fontset-default"
(cons (decode-char 'ucs #xF0000)
(decode-char 'ucs #xF008F))
(or font "ClockFace")))
;; Usage example for testing
;; - exal each one after font installation to test.
;; (uses the complete font name now)
;;
;; [x] (lambda-line-clockface-update-fontset "ClockFace")
;; [x] (lambda-line-clockface-update-fontset "ClockFaceRect")
;; [x] (lambda-line-clockface-update-fontset "ClockFaceRectSolid")
;; [x] (lambda-line-clockface-update-fontset "ClockFaceSolid")
;; Need to add some note about Doom Emacs font-set modification for the user:
;;
;; E.g.
;;
;; Doom Emacs will reset fontset-default when fonts are resized
;; (e.g. after `doom/increase-font-size' or `doom/decrease-font-size')
;;
;; So it's necessary to use `lambda-line-clockface-update-fontset' after such events have
;; completed.
;;
;; (I haven't found a working solution, i.e. using the Doom hook `after-setting-font-hook' doesn't work.)
(defun lambda-line-clockface-icons-unicode (hours minutes)
"Return ClockFace icon unicode for HOURS and MINUTES."
(let* ((minute (- minutes (% minutes 5)))
(offset (round (+ (* (% hours 12) 12) (* 12 (/ minute 60.0))))))
(+ offset #xF0000)))
(defun lambda-line-time ()
"Display the time when `display-time-mode' is non-nil."
(when display-time-mode
(let* ((time-unicode
(cl-destructuring-bind (_ minute hour &rest n) (decode-time)
(lambda-line-clockface-icons-unicode hour minute))))
(concat
(unless lambda-line-icon-time
(if display-time-day-and-date
(propertize (format-time-string lambda-line-time-day-and-date-format))
(propertize (format-time-string lambda-line-time-format ) 'face `(:height 0.9))))
(propertize
(format lambda-line-time-icon-format (char-to-string time-unicode)
'display '(raise 0)))))))
;;;;; Status
(defun lambda-line-status ()
"Return buffer status, one of 'read-only, 'modified or 'read-write."
(let ((read-only (when (not (or (derived-mode-p 'vterm-mode)
(derived-mode-p 'term-mode)
(derived-mode-p 'Info-mode)
(derived-mode-p 'help-mode)
(derived-mode-p 'helpful-mode)
(derived-mode-p 'elfeed-search)
(derived-mode-p 'elfeed-show)))
buffer-read-only))
(modified (and buffer-file-name (buffer-modified-p))))
(cond (modified 'modified)
(read-only 'read-only)
(t 'read-write))))
;;;;; Compose Status-Line
(defun lambda-line-compose (status name primary tertiary secondary)
"Compose a string with provided information.
Each section is first defined, along with a measure of the width of the status-line.
STATUS, NAME, PRIMARY, and SECONDARY are always displayed. TERTIARY is displayed only in some modes."
(let* ((window (get-buffer-window (current-buffer)))
(name-max-width (max 12
(- (window-body-width)
(round (* 0.8 (length primary)))
(length tertiary)
(length secondary))))
(name (if (and (stringp name) (> (length name) name-max-width))
(format "…%s" (substring name (- (length name) name-max-width -1)))
name))
(status (or status (lambda-line-status)))
(active (eq window lambda-line--selected-window))
(prefix (cond ((eq lambda-line-prefix nil) "")
(t
(cond ((derived-mode-p 'term-mode) " >_")
((derived-mode-p 'vterm-mode) " >_")
((derived-mode-p 'eshell-mode) " λ:")
((derived-mode-p 'Info-mode) " ℹ")
((derived-mode-p 'help-mode) " ?")
((derived-mode-p 'helpful-mode) " ?")
((eq status 'read-only)
(if (display-graphic-p) lambda-line-gui-ro-symbol
lambda-line-tty-ro-symbol))
((eq status 'read-write) (if (display-graphic-p) lambda-line-gui-rw-symbol
lambda-line-tty-rw-symbol))
((eq status 'modified) (if (display-graphic-p) lambda-line-gui-mod-symbol
lambda-line-tty-mod-symbol))
((window-dedicated-p) (if (display-graphic-p) " ––" " --"))
;; otherwise just use rw symbol
(t (if (display-graphic-p) lambda-line-gui-rw-symbol
lambda-line-tty-rw-symbol))))))
(face-modeline (if active
'lambda-line-active
'lambda-line-inactive))
(face-prefix (if (not prefix) face-modeline
(if active
(cond ((eq status 'read-only) 'lambda-line-active-status-RO)
((eq status 'read-write) 'lambda-line-active-status-RW)
((eq status 'modified) 'lambda-line-active-status-MD)
((or (derived-mode-p 'term-mode)
(derived-mode-p 'vterm-mode)
(derived-mode-p 'eshell-mode))
'lambda-line-active-status-MD)
((or (derived-mode-p 'Info-mode)
(derived-mode-p 'help-mode)
(derived-mode-p 'helpful-mode))
'lambda-line-active-status-RO)
(t 'lambda-line-active))
(cond ((eq status 'read-only) 'lambda-line-inactive-status-RO)
((eq status 'read-write) 'lambda-line-inactive-status-RW)
((eq status 'modified) 'lambda-line-inactive-status-MD)
((or (derived-mode-p 'term-mode)
(derived-mode-p 'vterm-mode)
(derived-mode-p 'eshell-mode)
(derived-mode-p 'Info-mode)
(derived-mode-p 'help-mode)
(derived-mode-p 'helpful-mode))
'lambda-line-inactive-status-RW)
(t 'lambda-line-inactive)))))
(face-name (if active
'lambda-line-active-name
'lambda-line-inactive-name))
(face-primary (if active
'lambda-line-active-primary
'lambda-line-inactive-primary))
(face-secondary (if active
'lambda-line-active-secondary
'lambda-line-inactive-secondary))
(face-tertiary (if active
'lambda-line-active-tertiary
'lambda-line-inactive-tertiary))
(left
;; special face for special mode prefixes
(concat
(propertize prefix 'face face-prefix 'display `(raise ,lambda-line-symbol-position))
;; this matters for inverse-video!
(propertize " " 'face face-prefix 'display `(raise ,lambda-line-space-top))
(when lambda-line-prefix-padding
(propertize " " 'face face-modeline))
(propertize name 'face face-name)
(propertize " " 'face (if active 'lambda-line-active
'lambda-line-inactive)
'display `(raise ,lambda-line-space-bottom))
(propertize primary 'face face-primary)))
(right (concat tertiary (propertize secondary 'face face-secondary) (propertize lambda-line-hspace 'face face-modeline)))
(right-len (length (format-mode-line right))))
(concat
left
(propertize " " 'face face-modeline 'display `(space :align-to (- right ,right-len)))
right)))
;;;; Mode Functions
;;;; Default display
(defun lambda-line-default-mode ()
"Compose the default status line."
(let ((buffer-name (format-mode-line (if buffer-file-name
(file-name-nondirectory (buffer-file-name))
"%b")))
(mode-name (lambda-line-mode-name))
(branch (lambda-line-vc-project-branch))
(position (format-mode-line "%l:%c:%o")))
(lambda-line-compose (lambda-line-status)
(lambda-line-truncate buffer-name lambda-line-truncate-value)
(concat lambda-line-display-group-start
mode-name
(when branch
branch)
lambda-line-display-group-end)
""
;; Narrowed buffer
(concat (if (buffer-narrowed-p)
(concat
(propertize "⇥ " 'face `(:inherit lambda-line-inactive-secondary))
position " ")
position)
(lambda-line-time)))))
;;;;; Prog Mode
;; ---------------------------------------------------------------------
(defun lambda-line-prog-mode-p ()
(derived-mode-p 'prog-mode))
(defun lambda-line-prog-mode ()
(let ((buffer-name (format-mode-line (if buffer-file-name (file-name-nondirectory (buffer-file-name)) "%b")))
(mode-name (lambda-line-mode-name))
(branch (lambda-line-vc-project-branch))
(position (format-mode-line "%l:%c:%o")))
(lambda-line-compose (lambda-line-status)
(lambda-line-truncate buffer-name lambda-line-truncate-value)
(concat lambda-line-display-group-start mode-name
(when branch branch)
lambda-line-display-group-end)
(concat
(if lambda-line-which-func
(lambda-line-show-func) "")
(if lambda-line-syntax
(lambda-line-check-syntax) ""))
(concat
;; Narrowed buffer
(when (buffer-narrowed-p)
(propertize "⇥ " 'face `(:inherit lambda-line-inactive-secondary)))
(if lambda-line-syntax
(if (or (boundp 'flycheck-mode)
(boundp 'flymake-mode))
;; (concat position lambda-line-hspace)
position)
position)
(lambda-line-time)))))
(defun lambda-line-prog-activate ()
"Setup flycheck hooks."
(add-hook 'flycheck-status-changed-functions #'lambda-line--update-flycheck-segment)
(add-hook 'flycheck-mode-hook #'lambda-line--update-flycheck-segment)
(when lambda-line-git-diff-mode-line
(add-hook 'after-save-hook #'vc-refresh-state)))
(defun lambda-line-prog-deactivate ()
"Remove flycheck hooks."
(remove-hook 'flycheck-status-changed-functions #'lambda-line--update-flycheck-segment)
(remove-hook 'flycheck-mode-hook #'lambda-line--update-flycheck-segment)
(when lambda-line-git-diff-mode-line
(remove-hook 'after-save-hook #'vc-refresh-state)))
;;;;; Fundamental Mode
(defun lambda-line-fundamental-mode-p ()
(derived-mode-p 'fundamental-mode))
(defun lambda-line-fundamental-mode ()
(lambda-line-default-mode))
;;;;; Text Mode
(defun lambda-line-text-mode-p ()
(derived-mode-p 'text-mode))
(defun lambda-line-text-mode ()
(lambda-line-default-mode))
;;;;; Help (& Helpful) Mode
(defun lambda-line-help-mode-p ()
(derived-mode-p 'help-mode))
(defun lambda-line-helpful-mode-p ()
(derived-mode-p 'helpful-mode))