summaryrefslogtreecommitdiff
path: root/modally.el
blob: 34b5f86d9619d67346c77529700632bad8ca994a (plain)
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
;;; modally.el --- An Emacs mode-line                -*- lexical-binding: t; -*-

;; Copyright (C) 2024  Blake Romero

;; Author: Blake Romero <codeblake@outlook.com>
;; Keywords: extensions

;; 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:
;;

;;; Code:
(require 'subr-x)
(require 'cl-lib)
(require 'vc-git)
(require 'flymake)
(require 'org-timer)

(require 'modally-faces)

;; FUNCTIONS
(defun modally--buffer-name ()
  "Return a propertized buffer name string."
  (let* ((buffer-face (cond
                        ((buffer-modified-p) 'modally-buffer-modified)
                        (buffer-read-only 'modally-buffer-readonly)
                        ('modally-buffer)))
         (buffer-name (concat (if buffer-read-only "🔒 " "✏️ ")
                              (if buffer-file-name
                                  (if (string-match "^\\(/home/[^/]+\\)\\(.+\\)"
                                                    buffer-file-name)
                                      (concat "~" (match-string 2 buffer-file-name))
                                    buffer-file-name)
                                (buffer-name (current-buffer)))))
         (spath (string-split buffer-name "/")))

    ;; Format
    (if (> (length spath) 1)
        ;; if buffer has a path
        (let ((path (string-join (butlast spath) "/"))
              (filename (car (last spath))))
          (format "\s%s/%s"
                  (propertize path 'face 'modally-buffer-path)
                  (propertize filename 'face buffer-face)))
      ;; if a buffer
      (format "\s%s" (propertize buffer-name 'face buffer-face))
      )))

(defun modally--major-mode ()
  "Return a propertized major mode string."
  (let ((mode (string-remove-suffix "-mode" (symbol-name major-mode))))
    (format-mode-line (format "\s%s" (cond
                                       ((equal mode "emacs-lisp")
                                        "elisp")
                                       ((equal mode "lisp-interaction")
                                        "lisp")
                                       (mode))) 'modally-major-mode)))

(defun modally--line-column ()
  "Return a propertized line column position."
  (format-mode-line
   "\sC%C"
   `(,(let ((column (current-column)))
        (cond
         ((and (>= column (- fill-column 10))
               (< column fill-column))
          'warning)
         ((>= column fill-column)
          'modally-column-error))))))

(defun modally--git-branch ()
  "Return a propertized Git branch string."
  (when-let* ((buffer (buffer-file-name))
              (branch (vc-git--symbolic-ref buffer)))
    (format "\s%s |" (propertize branch 'face 'modally-git-branch))))

(defun modally--flymake-diagnostics ()
  "Return `flymake-mode' alert counts."
  (let ((errors 0) (warnings 0) (notes 0) (alerts))
    (dolist (d (flymake-diagnostics))
      (pcase (flymake-diagnostic-type d)
        (':error (cl-incf errors))
        (':warning (cl-incf warnings))
        (':note (cl-incf notes))))
    (when (or (> errors 0) (> warnings 0) (> notes 0))
      (when (> notes 0)
        (cl-pushnew
         (propertize (format "i%s" notes) 'face 'modus-themes-fg-blue) alerts))
      (when (> warnings 0)
        (cl-pushnew
         (propertize (format "w%s" warnings) 'face 'warning) alerts))
      (when (> errors 0)
        (cl-pushnew
         (propertize (format "e%s" errors) 'face 'error) alerts))
      (format "\s%s |" (string-join alerts "\s")))))

(defun modally--approx-time (time)
  "Return an approximation of TIME."
  (let ((time (replace-regexp-in-string "<\\|>" "" time))
        (h (string-to-number
            (nth 0 (string-split time ":"))))
        (m (string-to-number
            (nth 1 (string-split time ":"))))
        (s (string-to-number
            (nth 2 (string-split time ":")))))
    (cond
     ((and (eq h 0) (eq m 0))
      (format "%ds" s))
     ((and (eq h 0) (> m 0))
      (format "~%dm" m))
     ((and (> h 0) (> m 0))
      (format "~%dh:%dm" h m)))))

(defun modally--org-timer ()
  "Return a timer from `org-timer'."
  (when (or org-timer-mode-line-timer
            org-timer-pause-time)
    (let ((face (if org-timer-pause-time
                    'modally-org-timer-pause
                  'modally-org-timer)))
      (format "\s%s |"
              (propertize
               (modally--approx-time org-timer-mode-line-string)
               'face face)))))

;; VARIABLES
(defvar modally-display-buffer-name
  '(:eval (modally--buffer-name))
  "Modeline construct to display the buffer name.")

(defvar modally-display-major-mode
  '(:eval (modally--major-mode))
  "Modeline construct to display the major mode.")

(defvar modally-display-git-branch
  '(:eval (modally--git-branch))
  "Modeline construct to display the Git branch name.")

(defvar modally-display-line-column
  '(:eval (modally--line-column))
  "Modeline construct to display line & column position.")

(defvar modally-display-line-row
  '(:eval (format-mode-line "\sL%l"))
  "Modeline construct to display line & column position.")

(defvar modally-display-file-size
  '(:eval (format-mode-line "\s%I"))
  "Modeline construct to display file size of buffer.")

(defvar modally-display-flymake
  '(:eval (modally--flymake-diagnostics))
  "Modeline construct to display flymake info.")

(defvar modally-display-org-timer
  '(:eval (modally--org-timer))
  "Modeline construct to display `org-timer' info.")

;; set as risky local variable
(dolist (construct '(modally-display-buffer-name
                     modally-display-major-mode
                     modally-display-git-branch
                     modally-display-line-row
                     modally-display-line-column
                     modally-display-file-size
                     modally-display-flymake
                     modally-display-org-timer))
  (put construct 'risky-local-variable #'stringp))

;; FORMATS
(defvar modally-format-default
  `("%e"
    modally-display-buffer-name

    mode-line-format-right-align
    modally-display-org-timer
    modally-display-line-row
    modally-display-line-column
    " |"
    modally-display-git-branch
    modally-display-file-size
    " |"
    modally-display-flymake
    modally-display-major-mode
    )
  "Default modally format for the mode-line.")

;; HELPERS
(defun modally--set-face ()
  "Set mode-line face."
  (set-face-attribute
   'mode-line-active nil
   :background "black"
   :foreground "white"
   :box '(:line-width (20 . 2) :color "#444444"))
  (set-face-attribute
   'mode-line-inactive nil
   :background "black"
   :foreground "grey"
   :box '(:line-width (20 . 2) :color "#222222")))

(defun modally--reset-face()
  "Reset mode-line face."
  (set-face-attribute
   'mode-line nil
   :background "black"
   :foreground "white"
   :box t))

(defun modally--reset-mode-line ()
  "Reset mode-line."
  (setq-default mode-line-format (car (get 'mode-line-format 'standard-value)))
  (modally--reset-face))

(defun modally--set-mode-line ()
  "Set modally mode-line."
  (setq-default mode-line-format modally-format-default)
  (with-eval-after-load 'keycast
    (setq-default keycast-mode-line-insert-after 'modally-display-git-branch))
  (modally--set-face))

;; MODE
;;;###autoload
(define-minor-mode modally-mode
  "An Emacs mode-line."
  :group 'modally
  :global t
  :require '(subr-x vc-git flymake cl-lib)
  :lighter " MDL"
  (if modally-mode
      (modally--set-mode-line)
    (modally--reset-mode-line)))

(provide 'modally)
;;; modally.el ends here