-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
jcs-modeline.el
766 lines (662 loc) · 27.7 KB
/
jcs-modeline.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
;;; jcs-modeline.el --- A modeline for jcs-emacs -*- lexical-binding: t; -*-
;; Copyright (C) 2022-2024 Shen, Jen-Chieh
;; Author: Shen, Jen-Chieh <jcs090218@gmail.com>
;; Maintainer: Shen, Jen-Chieh <jcs090218@gmail.com>
;; URL: https://github.com/jcs-emacs/jcs-modeline
;; Version: 0.1.1
;; Package-Requires: ((emacs "29.1") (moody "0.7.1") (minions "0.3.7") (elenv "0.1.0") (nerd-icons "0.0.1") (reveal-in-folder "0.1.2"))
;; Keywords: faces mode-line
;; 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:
;;
;; A modeline for jcs-emacs
;;
;;; Code:
(require 'cl-lib)
(require 'subr-x)
(require 'elenv)
(require 'moody)
(require 'minions)
(require 'nerd-icons)
(defgroup jcs-modeline nil
"A modeline for jcs-emacs."
:prefix "jcs-modeline-"
:group 'faces
:link '(url-link :tag "Github" "https://github.com/jcs-emacs/jcs-modeline"))
(defcustom jcs-modeline-left
`((:eval (jcs-modeline--render-front-spaces))
(:eval (jcs-modeline--render-buffer-identification))
(:eval (jcs-modeline--render-modes))
(:eval (moody-tab (jcs-modeline--render-vc-project)))
(:eval (jcs-modeline--render-read-only))
(:eval (jcs-modeline--render-mode-line-process)))
"List of item to render on the left."
:type 'list
:group 'jcs-modeline)
(defcustom jcs-modeline-right
`((:eval (jcs-modeline--render-nov))
(:eval (jcs-modeline--render-csv))
(:eval (jcs-modeline--render-text-scale))
(:eval (jcs-modeline--render-flymake))
(:eval (jcs-modeline--render-flycheck))
(:eval (jcs-modeline--render-undo-tree-buffer-name))
(:eval (jcs-modeline--render-undo-tree-status))
(:eval (jcs-modeline--render-vc-info))
(:eval (moody-tab (jcs-modeline--render-line-columns) 0 'up))
(:eval (jcs-modeline--render-percent-position))
(:eval (jcs-modeline--render-end-spaces)))
"List of item to render on the right."
:type 'list
:group 'jcs-modeline)
(defcustom jcs-modeline-checker-colors '((error . "#FB4933")
(warning . "#FABD2F")
(info . "#83A598")
(note . "#83A598"))
"Alist of colors for checkers."
:type 'list
:group 'jcs-modeline)
;;
;; (@* "Externals" )
;;
(defvar buffer-undo-tree)
(defvar undo-tree-visualizer-buffer-name)
(defvar undo-tree-visualizer-parent-buffer)
(declare-function undo-tree-current "ext:undo-tree.el")
(declare-function undo-tree-root "ext:undo-tree.el")
(declare-function undo-tree-node-next "ext:undo-tree.el")
(declare-function undo-tree-node-previous "ext:undo-tree.el")
(declare-function undo-tree-node-branch "ext:undo-tree.el")
(declare-function undo-tree-count "ext:undo-tree.el")
(declare-function undo-tree-size "ext:undo-tree.el")
(declare-function undo-tree-node-timestamp "ext:undo-tree.el")
(declare-function undo-tree-timestamp-to-string "ext:undo-tree.el")
(defvar flymake--state)
(declare-function flymake-running-backends "ext:flymake.el")
(declare-function flymake-disabled-backends "ext:flymake.el")
(declare-function flymake-reporting-backends "ext:flymake.el")
(declare-function flymake--diag-type "ext:flymake.el")
(declare-function flymake--state-diags "ext:flymake.el")
(defvar flycheck-current-errors)
(defvar flycheck-last-status-change)
(declare-function flycheck-has-current-errors-p "ext:flycheck.el")
(declare-function flycheck-count-errors "ext:flycheck.el")
(declare-function magit-branch "ext:magit-branch.el")
;;
;; (@* "Entry" )
;;
(defvar jcs-modeline--render-left nil)
(defvar jcs-modeline--render-right nil)
(defvar jcs-modeline--default-mode-line nil
"Default modeline value to revert back.")
(defun jcs-modeline--enable ()
"Enable function `jcs-modeline-mode'."
(unless elenv-graphic-p
(advice-add 'moody-tab :override #'jcs-modeline--moody-tab))
(add-hook 'window-size-change-functions #'jcs-modeline--window-resize)
(jcs-modeline--window-resize) ; call it manually once
(setq jcs-modeline--default-mode-line mode-line-format)
(setq-default mode-line-format
'((:eval (jcs-modeline-render jcs-modeline--render-left
jcs-modeline--render-right))))
(minions-mode 1))
(defun jcs-modeline--disable ()
"Disable function `jcs-modeline-mode'."
(unless elenv-graphic-p
(advice-remove 'moody-tab #'jcs-modeline--moody-tab))
(remove-hook 'window-size-change-functions #'jcs-modeline--window-resize)
(setq-default mode-line-format jcs-modeline--default-mode-line))
;;;###autoload
(define-minor-mode jcs-modeline-mode
"Minor mode `jcs-modeline-mode'."
:global t
:require 'jcs-modeline-mode
:group 'jcs-modeline
:lighter nil
(if jcs-modeline-mode (jcs-modeline--enable) (jcs-modeline--disable)))
;;
;; (@* "Util" )
;;
(defun jcs-modeline-2str (obj)
"Convert OBJ to string."
(format "%s" obj))
(defvar jcs-modeline--char-displayable-cache (make-hash-table :test 'equal)
"Cache the displable character.")
(defun jcs-modeline--char-displayable-p (str-or-char)
"Check if STR-OR-CHAR is displayable."
(let* ((char (if (stringp str-or-char)
(string-to-char str-or-char)
str-or-char))
(result (or (gethash char jcs-modeline--char-displayable-cache)
(char-displayable-p char))))
(puthash char result jcs-modeline--char-displayable-cache)
(and result str-or-char)))
(defun jcs-modeline--str-width (str)
"Calculate STR in pixel width."
(let ((width (frame-char-width))
(len (string-pixel-width str)))
(+ (/ len width)
(if (zerop (% len width)) 0 1)))) ; add one if exceeed
(defun jcs-modeline--light-color-p (hex-code)
"Return non-nil if HEX-CODE is in light tone."
(when elenv-graphic-p
(let ((gray (nth 0 (color-values "gray")))
(color (nth 0 (color-values hex-code))))
(< gray color))))
(defun jcs-modeline--light-theme-p ()
"Return non-nil if current theme is light theme."
(ignore-errors (jcs-modeline--light-color-p (face-background 'default))))
(defun jcs-modeline-format (format &optional face window buffer)
"Wrapper for function `format-mode-line'.
Arguments FORMAT, FACE, WINDOW and BUFFER are parameters from the
`format-mode-line' function."
(string-trim (format-mode-line format face window buffer)))
(defun jcs-modeline--moody-tab (arg0 &rest _)
"Override the function `moody-tab' when inside the terminal.
Position argument ARG0."
(concat " " arg0 " "))
;;
;; (@* "Core" )
;;
(defun jcs-modeline--window-resize (&rest _)
"Window resize hook."
(let ((count 0) (current-width 0)
(is-left t)
(left-index 0) (right-index 0)
;; Let's iterate it from outer to inner, so we must flip the right list.
(right-list (reverse jcs-modeline-right)))
(setq jcs-modeline--render-left nil
jcs-modeline--render-right nil) ; reset
;; Loop through the entire `left' + `right' list
(while (< count (length (append jcs-modeline-left jcs-modeline-right)))
;; Check if the index exceed it's length, then flip the left/right
;; toggle variable `is-left' to ensure we iterate through the whole
;; render list!
(when (<= (length (if is-left jcs-modeline-left jcs-modeline-right))
(if is-left left-index right-index))
(setq is-left (not is-left)))
;; Select the item, check the length and add it to the render list!
(let* ((item (nth (if is-left left-index right-index)
(if is-left jcs-modeline-left right-list)))
(format (format-mode-line item))
(width (jcs-modeline--str-width format))
(new-width (+ current-width width)))
;; Can the new item added to the list?
(when (<= new-width (window-width)) ; can be displayed properly!
(setq current-width new-width) ; update string/display width
;; Add the item to render list!
(push item (if is-left jcs-modeline--render-left
jcs-modeline--render-right))))
(cl-incf (if is-left left-index right-index)) ; increment index
(cl-incf count)
(setq is-left (not is-left)))) ; flip `left' and `right'
(setq jcs-modeline--render-left (reverse jcs-modeline--render-left)
;; Since we iterate it from the edge, we don't need to reverse the right
jcs-modeline--render-right jcs-modeline--render-right))
(defun jcs-modeline--adjust-pad ()
"Adjust padding for external packages."
(let ((delta 0))
(when vertical-scroll-bar
(when-let* ((data (window-scroll-bars))
(shown (nth 2 data))
(width (nth 1 data)))
(setq delta (+ delta width))))
delta))
(defun jcs-modeline-render (left right)
"Render mode line with LEFT and RIGHT alignment."
(let* ((len-left (jcs-modeline--str-width (format-mode-line left)))
(len-right (jcs-modeline--str-width (format-mode-line right)))
(available-width (- (window-width) (+ len-left len-right)))
(available-width (+ available-width (jcs-modeline--adjust-pad))))
(append left
(list (format (format "%%%ds" available-width) ""))
right)))
;;
;; (@* "Padding" )
;;
(defun jcs-modeline--render-front-spaces ()
"Return the front spaces."
" ")
(defun jcs-modeline--render-end-spaces ()
"Return the end spaces."
(unless elenv-graphic-p " "))
;;
;; (@* "Plugins" )
;;
;;
;;; Buffe Identification
(defun jcs-modeline--render-buffer-identification ()
"Render buffer identification."
(unless (bound-and-true-p centaur-tabs-mode)
(moody-tab (jcs-modeline-format mode-line-buffer-identification))))
(defun jcs-modeline--render-mode-line-process ()
"Render `mode-line-process'."
(when mode-line-process
(concat " " (jcs-modeline-format mode-line-process))))
;;
;;; Modes
(defcustom jcs-modeline-show-mode-icons nil
"Non-nil to display mode icons."
:type 'boolean
:group 'jcs-modeline)
(defcustom jcs-modeline-show-mode-name t
"Non-nil to display mode name."
:type 'boolean
:group 'jcs-modeline)
(defun jcs-modeline--icon-for-buffer ()
"Return icon for buffer."
(or (ignore-errors (nerd-icons-icon-for-file (buffer-file-name)))
(ignore-errors (nerd-icons-icon-for-mode major-mode))))
(defun jcs-modeline--render-modes ()
"Render line modes."
(let* ((icon (and jcs-modeline-show-mode-icons
(when-let* ((icon (jcs-modeline--icon-for-buffer))
(icon (if (or (null icon) (symbolp icon))
(nerd-icons-faicon "nf-fa-file_o")
icon))
((jcs-modeline--char-displayable-p icon)))
icon)))
(line-modes (and (or jcs-modeline-show-mode-name
(null icon))
(jcs-modeline-format (if minions-mode
minions-mode-line-modes
mode-line-modes))))
(lst (cl-remove-if #'null (list icon line-modes)))
(ind (mapconcat #'identity lst " ")))
(concat (propertize ind
'mouse-face 'mode-line-highlight
'help-echo "Major and minor modes
mouse-1: Toggle display of major mode name"
'local-map (let ((map (make-sparse-keymap)))
(define-key map (vector 'mode-line 'mouse-1)
(lambda (&rest _)
(interactive)
(setq jcs-modeline-show-mode-name
(not jcs-modeline-show-mode-name))
(force-mode-line-update t)))
map))
" ")))
;;
;;; Line and Columns
(defcustom jcs-modeline-show-point nil
"If non-nil, also shows point information."
:type 'boolean
:group 'jcs-modeline)
(defun jcs-modeline--render-line-columns ()
"Render current line number and column."
(let* ((ind-line (propertize (jcs-modeline-format "%l")
'mouse-face 'mode-line-highlight
'help-echo "Line"))
(ind-column (propertize (jcs-modeline-format "%c")
'mouse-face 'mode-line-highlight
'help-echo "Column"))
(ind-point (concat "("
(propertize (jcs-modeline-2str (point))
'mouse-face 'mode-line-highlight
'help-echo "Point")
")"))
(lst (if jcs-modeline-show-point
(list ind-line ind-column ind-point)
(list ind-line ind-column))))
(mapconcat #'identity lst " ")))
;;
;;; Scroll
(defun jcs-modeline--render-percent-position ()
"Render current scroll."
(concat " " (propertize "%p"
'mouse-face 'mode-line-highlight
'help-echo "Percent position")))
;;
;;; Project
(defcustom jcs-modeline-project-indicator "-*-"
"String indicator when project root is not found."
:type 'string
:group 'jcs-modeline)
(defun jcs-modeline--project-root ()
"Return project directory path."
(when-let ((current (project-current))) (project-root current)))
(defcustom jcs-modeline-show-project-name-virutal-buffer nil
"If non-nil, display project's name in the virutal buffer."
:type 'boolean
:group 'jcs-modeline)
(defun jcs-modeline--render-vc-project ()
"Return the project name."
(if-let* (((or (buffer-file-name) jcs-modeline-show-project-name-virutal-buffer))
(project (jcs-modeline--project-root))
(ind (file-name-nondirectory (directory-file-name project))))
(concat
(propertize ind
'mouse-face 'mode-line-highlight
'help-echo (format "Project Name
path: %s
mouse-1: Reveal project in folder" project)
'local-map
(let ((map (make-sparse-keymap)))
(define-key map (vector 'mode-line 'mouse-1)
(lambda (&rest _)
(interactive)
(reveal-in-folder-open project)))
map)))
(propertize jcs-modeline-project-indicator
'mouse-face 'mode-line-highlight
'help-echo "Project Name
mouse-1: Switch project"
'local-map
(let ((map (make-sparse-keymap)))
(define-key map (vector 'mode-line 'mouse-1) #'project-switch-project)
map))))
;;
;;; Version Control
(defcustom jcs-modeline-vc-unknown-icon
(ignore-errors
(nerd-icons-codicon "nf-cod-workspace_unknown" :face 'jcs-modeline-vc-face))
"The default unknown VC icon."
:type 'string
:group 'jcs-modeline)
(defface jcs-modeline-vc-face
'((t :foreground "#9BBC6D"))
"Face to use for VC."
:group 'jcs-modeline)
(defcustom jcs-modeline-vc-backends
`((RCS . ,jcs-modeline-vc-unknown-icon)
(CVS . ,jcs-modeline-vc-unknown-icon)
(SVN . ,jcs-modeline-vc-unknown-icon)
(SCCS . ,jcs-modeline-vc-unknown-icon)
(SRC . ,jcs-modeline-vc-unknown-icon)
(Bzr . ,jcs-modeline-vc-unknown-icon)
(Git . ,(ignore-errors
(nerd-icons-devicon "nf-dev-git_branch" :face 'jcs-modeline-vc-face)))
(Hg . ,jcs-modeline-vc-unknown-icon))
"Alist of vc backends to icon."
:type 'list
:group 'jcs-modeline)
(declare-function vc-git--symbolic-ref "vc-git.el")
(defun jcs-modeline--render-vc-info ()
"Return `vc-mode' information."
(when-let* ((bfn (buffer-file-name))
(backend (vc-backend bfn))
(backend-icon (alist-get backend jcs-modeline-vc-backends))
(backend-icon (if (jcs-modeline--char-displayable-p backend-icon)
backend-icon
""))
(branch (or (vc-git--symbolic-ref bfn) ""))
(separator (if (and backend-icon
(not (string-empty-p backend-icon)))
" "
""))
(tip (jcs-modeline-2str backend)))
(concat (propertize backend-icon
'mouse-face 'mode-line-highlight
'help-echo tip
'local-map
(let ((map (make-sparse-keymap)))
(define-key map (vector 'mode-line 'mouse-1) #'magit-branch)
map))
(propertize (concat
separator
branch)
'face 'jcs-modeline-vc-face
'mouse-face 'mode-line-highlight
'help-echo tip
'local-map
(let ((map (make-sparse-keymap)))
(define-key map (vector 'mode-line 'mouse-1) #'magit-branch)
map))
" ")))
;;
;;; Read-Only
(defun jcs-modeline--render-read-only ()
"Render read-only indicator."
(concat " "
(propertize (if buffer-read-only
(or (jcs-modeline--char-displayable-p "🔒")
"&L")
(or (jcs-modeline--char-displayable-p "🔓")
"&U"))
'mouse-face 'mode-line-highlight
'help-echo (format "Buffer read-only: %s"
(if buffer-read-only "ON" "OFF"))
'local-map
(let ((map (make-sparse-keymap)))
(define-key map (vector 'mode-line 'mouse-1) #'read-only-mode)
map))))
;;
;;; Text Scale
(defvar text-scale-mode-amount)
(defvar text-scale-mode-lighter)
(defun jcs-modeline--render-text-scale ()
"Render text-scale amount."
(when (and (boundp 'text-scale-mode-lighter)
(/= text-scale-mode-amount 0))
(format "(%s) " (propertize text-scale-mode-lighter
'mouse-face 'mode-line-highlight
'help-echo (concat "Text scale " text-scale-mode-lighter)))))
;;
;;; Undo
(defun jcs-modeline--render-undo-tree-buffer-name ()
"Render text-scale amount."
(when-let (((featurep 'undo-tree))
((equal (buffer-name) undo-tree-visualizer-buffer-name))
(ind (buffer-name undo-tree-visualizer-parent-buffer)))
(concat " "
(propertize ind
'mouse-face 'mode-line-highlight
'help-echo "UndoTree Parent Buffer"))))
(defun jcs-modeline--undo-tree-branch-height (root)
"Return the total height of the current branch from ROOT."
(let ((count 0))
(while (setq root (nth (undo-tree-node-branch root)
(undo-tree-node-next root)))
(cl-incf count))
count))
(defun jcs-modeline--undo-tree-height (node)
"Return NODE's current height."
(let ((count 0))
(while (setq node (undo-tree-node-previous node))
(cl-incf count))
count))
(defun jcs-modeline--render-undo-tree-status ()
"Render text-scale amount."
(when-let* (((and (featurep 'undo-tree) buffer-undo-tree
(equal (buffer-name) undo-tree-visualizer-buffer-name)))
(root (undo-tree-root buffer-undo-tree))
(node (undo-tree-current buffer-undo-tree))
(timestamp (undo-tree-node-timestamp node))
(tree-height (jcs-modeline--undo-tree-height node))
(tree-branch-height (jcs-modeline--undo-tree-branch-height root))
(tree-count (undo-tree-count buffer-undo-tree))
(tree-size (undo-tree-size buffer-undo-tree))
(timestamp (string-trim (undo-tree-timestamp-to-string timestamp))))
(concat " "
(propertize (jcs-modeline-2str tree-height)
'mouse-face 'mode-line-highlight
'help-echo "UndoTree Height")
"/"
(propertize (jcs-modeline-2str tree-branch-height)
'mouse-face 'mode-line-highlight
'help-echo "UndoTree Branch Height")
"/"
(propertize (jcs-modeline-2str tree-count)
'mouse-face 'mode-line-highlight
'help-echo "UndoTree Count")
" ("
(propertize (jcs-modeline-2str tree-size)
'mouse-face 'mode-line-highlight
'help-echo "UndoTree Size")
") "
(propertize (jcs-modeline-2str timestamp)
'mouse-face 'mode-line-highlight
'help-echo "UndoTree Timestamp"))))
;;
;;; Flymake
(defun jcs-modeline--flymake-lighter (diags-by-type state running)
"Return flycheck lighter by given DIAGS-BY-TYPE and STATE.
If argument RUNNING is non-nil, we turn lighter into question mark."
(let* ((c-state (cl-case state
(error :error)
(warning :warning)
(note :note)))
(counts (length (gethash c-state diags-by-type)))
(color (cdr (assoc state jcs-modeline-checker-colors)))
(lighter (format "%s" (if running "?" counts))))
(propertize lighter 'face `(:foreground ,color))))
(defun jcs-modeline--render-flymake ()
"Render for flymake."
(when (bound-and-true-p flymake-mode)
(let* ((known (hash-table-keys flymake--state))
(running (flymake-running-backends))
(disabled (flymake-disabled-backends))
(reported (flymake-reporting-backends))
(diags-by-type (make-hash-table))
(all-disabled (and disabled (null running)))
(some-waiting (cl-set-difference running reported)))
(maphash (lambda (_b state)
(mapc (lambda (diag)
(push diag
(gethash (flymake--diag-type diag)
diags-by-type)))
(flymake--state-diags state)))
flymake--state)
(concat
(let* ((states '(error warning note))
(last (car (last states)))
result)
(dolist (state states)
(when-let ((lighter (jcs-modeline--flymake-lighter
diags-by-type state
(or some-waiting (null known) all-disabled))))
(setq result (concat result lighter
(unless (equal state last) "/")))))
(propertize result
'mouse-face 'mode-line-highlight
'help-echo "flymake"))
" "))))
;;
;;; Flycheck
(defun jcs-modeline--flycheck-lighter (state running)
"Return flycheck lighter by given STATE.
If argument RUNNING is non-nil, we turn lighter into question mark."
(let* ((counts (flycheck-count-errors flycheck-current-errors))
(err (or (cdr (assq state counts)) "0"))
(color (cdr (assoc state jcs-modeline-checker-colors)))
(lighter (format "%s" (if running "?" err))))
(propertize lighter 'face `(:foreground ,color))))
(defun jcs-modeline--render-flycheck ()
"Render for flycheck."
(when (bound-and-true-p flycheck-mode)
(concat
(let* ((states '(error warning info))
(last (car (last states)))
(running (eq 'running flycheck-last-status-change))
result)
(dolist (state states)
(when-let ((lighter (jcs-modeline--flycheck-lighter state running)))
(setq result (concat result lighter
(unless (equal state last) "/")))))
(propertize result
'mouse-face 'mode-line-highlight
'help-echo "flycheck"))
" ")))
;;
;;; CSV
(defvar csv-mode-line-format)
(defun jcs-modeline--render-csv ()
"Render for `csv-mode'."
(when-let (((memq major-mode '(csv-mode tsv-mode)))
(ind (format-mode-line csv-mode-line-format)))
(concat (propertize ind
'mouse-face 'mode-line-highlight
'help-echo "csv")
" ")))
;;
;;; Nov
(defvar nov-documents)
(defvar nov-documents-index)
(defun jcs-modeline--render-nov ()
"Render for nov."
(when-let (((eq major-mode 'nov-mode))
(ind (format "[%s/%s]" (1+ nov-documents-index) (length nov-documents))))
(propertize ind
'mouse-face 'mode-line-highlight
'help-echo "[current page/totla page]")))
;;
;; (@* "Themes" )
;;
(defun jcs-modeline--set-color (ac-lst inac-lst)
"Set `mode-line' theme faces with AC-LST and INAC-LST."
(let ((state (frame-focus-state))
(ac-0 (nth 0 ac-lst)) (ac-1 (nth 1 ac-lst))
(ic-0 (nth 0 inac-lst)) (ic-1 (nth 1 inac-lst)))
(set-face-foreground 'mode-line (if state ac-0 ic-0))
(set-face-background 'mode-line (if state ac-1 ic-1))
(set-face-foreground 'mode-line-inactive ic-0)
(set-face-background 'mode-line-inactive ic-1)))
(defun jcs-modeline--set-border-color (color)
"Set the border color with COLOR."
(set-face-foreground 'vertical-border color)
(set-face-foreground 'window-divider color))
(defun jcs-modeline--set-theme (ml-ac-lst ml-inac-lst bc)
"Set the mode line theme.
ML-AC-LST : mode-line active list. ML-INAC-LST : mode-line inactive list.
BC : border color."
(jcs-modeline--set-color ml-ac-lst ml-inac-lst)
(jcs-modeline--set-border-color bc))
(defun jcs-modeline-gray ()
"Gray mode line."
(interactive)
(if (jcs-modeline--light-theme-p)
(jcs-modeline--set-theme
'("#1C1C1C" "#E5E5E5") '("#000000" "#D7D7D7") "#989898")
(jcs-modeline--set-theme
'("#D2D2D2" "#4D4D4D") '("#CCCCCC" "#333333") "#7E7E7E")))
(defun jcs-modeline-dark-green ()
"Dark green mode line."
(interactive)
(if (jcs-modeline--light-theme-p)
(jcs-modeline--set-theme
'("#000" "#B7D3D3") '("#000" "#99C2C2") "#B7D3D3")
(jcs-modeline--set-theme
'("#CCCCCC" "#3C6A69") '("#CCCCCC" "#2B4D4D") "#3C6A69")))
(defun jcs-modeline-dark-blue ()
"Dark blue mode line."
(interactive)
(if (jcs-modeline--light-theme-p)
(jcs-modeline--set-theme
'("#000" "#92B9DF") '("#000" "#7AA0C6") "#92B9DF")
(jcs-modeline--set-theme
'("#CCCCCC" "#205386") '("#CCCCCC" "#0C3765") "#246AAF")))
(defun jcs-modeline-dark-orange ()
"Dark orange mode line."
(interactive)
(if (jcs-modeline--light-theme-p)
(jcs-modeline--set-theme
'("#1C1C1C" "#CC6633") '("#000000" "#682B12") "#CC6633")
(jcs-modeline--set-theme
'("#D2D2D2" "#CC6633") '("#CCCCCC" "#A4532A") "#CC6633")))
(defun jcs-modeline-red ()
"Red mode line."
(interactive)
(if (jcs-modeline--light-theme-p)
(jcs-modeline--set-theme
'("#CCCCCC" "#FF0000") '("#CCCCCC" "#6A0101") "#FF0000")
(jcs-modeline--set-theme
'("#CCCCCC" "#FF0000") '("#CCCCCC" "#6A0101") "#FF0000")))
(defun jcs-modeline-purple ()
"Purple mode line."
(interactive)
(if (jcs-modeline--light-theme-p)
(jcs-modeline--set-theme
'("#CCCCCC" "#B100EB") '("#CCCCCC" "#650286") "#B100EB")
(jcs-modeline--set-theme
'("#CCCCCC" "#B100EB") '("#CCCCCC" "#650286") "#B100EB")))
(provide 'jcs-modeline)
;;; jcs-modeline.el ends here