-
Notifications
You must be signed in to change notification settings - Fork 12
/
mood-line.el
370 lines (312 loc) · 13.6 KB
/
mood-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
;;; mood-line.el --- A minimal mode-line inspired by doom-modeline -*- lexical-binding: t; -*-
;; Author: Jessie Hildebrandt <jessieh.net>
;; Homepage: https://gitlab.com/jessieh/mood-line
;; Keywords: mode-line faces
;; Version: 1.2.4
;; Package-Requires: ((emacs "25.1"))
;; This file is not part of GNU Emacs.
;;; Commentary:
;;
;; mood-line is a minimal mode-line configuration that aims to replicate
;; some of the features of the doom-modeline package.
;;
;; Features offered:
;; * Clean, minimal design
;; * Anzu and multiple-cursors counter
;; * Version control status indicator
;; * Flycheck status indicator
;; * Flymake support
;; * Lightweight with no dependencies
;;
;; To enable mood-line:
;; (mood-line-mode)
;;; License:
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Code:
;;
;; Variable declarations
;;
(defvar flycheck-current-errors)
(defvar flymake--mode-line-format)
(defvar anzu--state)
(defvar anzu--cached-count)
(defvar anzu--overflow-p)
(defvar anzu--current-position)
(defvar anzu--total-matched)
(defvar multiple-cursors-mode)
;;
;; Function prototypes
;;
(declare-function flycheck-count-errors "flycheck" (errors))
(declare-function mc/num-cursors "multiple-cursors" ())
;;
;; Config
;;
(defgroup mood-line nil
"A minimal mode-line configuration inspired by doom-modeline."
:group 'mode-line)
(defcustom mood-line-show-eol-style nil
"If t, the EOL style of the current buffer will be displayed in the mode-line."
:group 'mood-line
:type 'boolean)
(defcustom mood-line-show-encoding-information nil
"If t, the encoding format of the current buffer will be displayed in the mode-line."
:group 'mood-line
:type 'boolean)
(defcustom mood-line-show-cursor-point nil
"If t, the value of `point' will be displayed next to the cursor position in the mode-line."
:group 'mood-line
:type 'boolean)
(defface mood-line-buffer-name
'((t (:inherit (mode-line-buffer-id))))
"Face used for major mode indicator in the mode-line."
:group 'mood-line)
(defface mood-line-major-mode
'((t (:inherit (bold))))
"Face used for major mode indicator in the mode-line."
:group 'mood-line)
(defface mood-line-status-neutral
'((t (:inherit (shadow))))
"Face used for neutral or inactive status indicators in the mode-line."
:group 'mood-line)
(defface mood-line-status-info
'((t (:inherit (font-lock-keyword-face))))
"Face used for generic status indicators in the mode-line."
:group 'mood-line)
(defface mood-line-status-success
'((t (:inherit (success))))
"Face used for success status indicators in the mode-line."
:group 'mood-line)
(defface mood-line-status-warning
'((t (:inherit (warning))))
"Face for warning status indicators in the mode-line."
:group 'mood-line)
(defface mood-line-status-error
'((t (:inherit (error))))
"Face for error stauts indicators in the mode-line."
:group 'mood-line)
(defface mood-line-unimportant
'((t (:inherit (shadow))))
"Face used for less important mode-line elements."
:group 'mood-line)
(defface mood-line-modified
'((t (:inherit (error))))
"Face used for the 'modified' indicator symbol in the mode-line."
:group 'mood-line)
;;
;; Helper functions
;;
(defun mood-line--string-trim-left (string)
"Remove whitespace at the beginning of STRING."
(if (string-match "\\`[ \t\n\r]+" string)
(replace-match "" t t string)
string))
(defun mood-line--string-trim-right (string)
"Remove whitespace at the end of STRING."
(if (string-match "[ \t\n\r]+\\'" string)
(replace-match "" t t string)
string))
(defun mood-line--string-trim (string)
"Remove whitespace at the beginning and end of STRING."
(mood-line--string-trim-left (mood-line--string-trim-right string)))
(defun mood-line--format (left right)
"Return a string of `window-width' length containing LEFT and RIGHT, aligned respectively."
(let ((reserve (length right)))
(concat left
" "
(propertize " "
'display `((space :align-to (- right ,reserve))))
right)))
;;
;; Update functions
;;
(defvar-local mood-line--vc-text nil)
(defun mood-line--update-vc-segment (&rest _)
"Update `mood-line--vc-text' against the current VCS state."
(setq mood-line--vc-text
(when (and vc-mode buffer-file-name)
(let ((backend (vc-backend buffer-file-name))
(state (vc-state buffer-file-name (vc-backend buffer-file-name))))
(let ((face 'mode-line-neutral))
(concat (cond ((memq state '(edited added))
(setq face 'mood-line-status-info)
(propertize "+ " 'face face))
((eq state 'needs-merge)
(setq face 'mood-line-status-warning)
(propertize "⟷ " 'face face))
((eq state 'needs-update)
(setq face 'mood-line-status-warning)
(propertize "↑ " 'face face))
((memq state '(removed conflict unregistered))
(setq face 'mood-line-status-error)
(propertize "✖ " 'face face))
(t
(setq face 'mood-line-status-neutral)
(propertize "✔ " 'face face)))
(propertize (substring vc-mode (+ (if (eq backend 'Hg) 2 3) 2))
'face face
'mouse-face face)
" "))))))
(defvar-local mood-line--flycheck-text nil)
(defun mood-line--update-flycheck-segment (&optional status)
"Update `mood-line--flycheck-text' against the reported flycheck STATUS."
(setq mood-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 "⚑ Issues: "
(number-to-string sum)
" ")
'face (if .error
'mood-line-status-error
'mood-line-status-warning))))
(propertize "✔ Good " 'face 'mood-line-status-success)))
('running (propertize "Δ Checking " 'face 'mood-line-status-info))
('errored (propertize "✖ Error " 'face 'mood-line-status-error))
('interrupted (propertize "⏸ Paused " 'face 'mood-line-status-neutral))
('no-checker ""))))
;;
;; Segments
;;
(defun mood-line-segment-modified ()
"Displays a color-coded buffer modification/read-only indicator in the mode-line."
(if (not (string-match-p "\\*.*\\*" (buffer-name)))
(if (buffer-modified-p)
(propertize "● " 'face 'mood-line-modified)
(if (and buffer-read-only (buffer-file-name))
(propertize "■ " 'face 'mood-line-unimportant)
" "))
" "))
(defun mood-line-segment-buffer-name ()
"Displays the name of the current buffer in the mode-line."
(propertize "%b " 'face 'mood-line-buffer-name))
(defun mood-line-segment-anzu ()
"Displays color-coded anzu status information in the mode-line (if available)."
(when (and (boundp 'anzu--state) anzu--state)
(cond ((eq anzu--state 'replace-query)
(format #("Replace: %d " 0 11 (face mood-line-status-warning)) anzu--cached-count))
(anzu--overflow-p
(format #("%d/%d+ " 0 3 (face mood-line-status-info) 3 6 (face mood-line-status-error)) anzu--current-position anzu--total-matched))
(t
(format #("%d/%d " 0 5 (face mood-line-status-info)) anzu--current-position anzu--total-matched)))))
(defun mood-line-segment-multiple-cursors ()
"Displays the number of active multiple-cursors in the mode-line (if available)."
(when (and (boundp 'multiple-cursors-mode) multiple-cursors-mode)
(concat "MC"
(format #("×%d " 0 3 (face mood-line-status-warning)) (mc/num-cursors)))))
(defun mood-line-segment-position ()
"Displays the current cursor position in the mode-line."
(concat "%l:%c"
(when mood-line-show-cursor-point (propertize (format ":%d" (point)) 'face 'mood-line-unimportant))
(propertize " %p%% " 'face 'mood-line-unimportant)))
(defun mood-line-segment-eol ()
"Displays the EOL style of the current buffer in the mode-line."
(when mood-line-show-eol-style
(pcase (coding-system-eol-type buffer-file-coding-system)
(0 "LF ")
(1 "CRLF ")
(2 "CR "))))
(defun mood-line-segment-encoding ()
"Displays the encoding and EOL style of the buffer in the mode-line."
(when mood-line-show-encoding-information
(concat (let ((sys (coding-system-plist buffer-file-coding-system)))
(cond ((memq (plist-get sys :category) '(coding-category-undecided coding-category-utf-8))
"UTF-8")
(t (upcase (symbol-name (plist-get sys :name))))))
" ")))
(defun mood-line-segment-vc ()
"Displays color-coded version control information in the mode-line."
mood-line--vc-text)
(defun mood-line-segment-major-mode ()
"Displays the current major mode in the mode-line."
(concat (format-mode-line mode-name 'mood-line-major-mode) " "))
(defun mood-line-segment-misc-info ()
"Displays the current value of `mode-line-misc-info' in the mode-line."
(let ((misc-info (format-mode-line mode-line-misc-info 'mood-line-unimportant)))
(unless (string= (mood-line--string-trim misc-info) "")
(concat (mood-line--string-trim misc-info) " "))))
(defun mood-line-segment-flycheck ()
"Displays color-coded flycheck information in the mode-line (if available)."
mood-line--flycheck-text)
(defun mood-line-segment-flymake ()
"Displays information about the current status of flymake in the mode-line (if available)."
(when (and (boundp 'flymake-mode) flymake-mode)
(concat (mood-line--string-trim (format-mode-line flymake--mode-line-format)) " ")))
(defun mood-line-segment-process ()
"Displays the current value of `mode-line-process' in the mode-line."
(let ((process-info (format-mode-line mode-line-process)))
(unless (string= (mood-line--string-trim process-info) "")
(concat (mood-line--string-trim process-info) " "))))
;;
;; Activation function
;;
;; Store the default mode-line format
(defvar mood-line--default-mode-line mode-line-format)
;;;###autoload
(define-minor-mode mood-line-mode
"Toggle mood-line on or off."
:group 'mood-line
:global t
:lighter nil
(if mood-line-mode
(progn
;; Setup flycheck hooks
(add-hook 'flycheck-status-changed-functions #'mood-line--update-flycheck-segment)
(add-hook 'flycheck-mode-hook #'mood-line--update-flycheck-segment)
;; Setup VC hooks
(add-hook 'find-file-hook #'mood-line--update-vc-segment)
(add-hook 'after-save-hook #'mood-line--update-vc-segment)
(advice-add #'vc-refresh-state :after #'mood-line--update-vc-segment)
;; Set the new mode-line-format
(setq-default mode-line-format
'((:eval
(mood-line--format
;; Left
(format-mode-line
'(" "
(:eval (mood-line-segment-modified))
(:eval (mood-line-segment-buffer-name))
(:eval (mood-line-segment-anzu))
(:eval (mood-line-segment-multiple-cursors))
(:eval (mood-line-segment-position))))
;; Right
(format-mode-line
'((:eval (mood-line-segment-eol))
(:eval (mood-line-segment-encoding))
(:eval (mood-line-segment-vc))
(:eval (mood-line-segment-major-mode))
(:eval (mood-line-segment-misc-info))
(:eval (mood-line-segment-flycheck))
(:eval (mood-line-segment-flymake))
(:eval (mood-line-segment-process))
" ")))))))
(progn
;; Remove flycheck hooks
(remove-hook 'flycheck-status-changed-functions #'mood-line--update-flycheck-segment)
(remove-hook 'flycheck-mode-hook #'mood-line--update-flycheck-segment)
;; Remove VC hooks
(remove-hook 'file-find-hook #'mood-line--update-vc-segment)
(remove-hook 'after-save-hook #'mood-line--update-vc-segment)
(advice-remove #'vc-refresh-state #'mood-line--update-vc-segment)
;; Restore the original mode-line format
(setq-default mode-line-format mood-line--default-mode-line))))
;;
;; Provide mood-line
;;
(provide 'mood-line)
;;; mood-line.el ends here