forked from konrad1977/punch-line
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcocaine-line.el
More file actions
512 lines (435 loc) · 18.6 KB
/
Copy pathcocaine-line.el
File metadata and controls
512 lines (435 loc) · 18.6 KB
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
;;; cocaine-line.el --- A customized modeline for Emacs with Evil status and advanced customizations
;; Author: Mikael Konradsson
;; Version: 1.0
;; Package-Requires: ((emacs "25.1") (evil "1.0.0"))
;;; Commentary:
;; This package offers a customized modeline for Emacs with Evil status,
;; configurable colors, and the ability to customize displayed information.
;;; Code:
(require 'battery)
(require 'cl-lib)
(require 'evil)
(require 'mode-line-hud)
(require 'nerd-icons)
(require 'project)
(require 'vc)
(require 'doom-modeline)
(unless (bound-and-true-p battery-status-function)
(battery-update-handler))
(defvar-local cocaine-line-is-active t
"Indicates if the current window is active.")
(defgroup cocaine-line nil
"Customizations for cocaine-line."
:group 'mode-line)
(defcustom cocaine-right-padding 0
"Number of spaces to add after the time."
:type 'integer
:group 'cocaine-line)
(defcustom cocaine-show-column-info nil
"If set to t, show column information."
:type 'boolean
:group 'cocaine-line)
(defcustom cocaine-show-buffer-position nil
"If set to t, show buffer position."
:type 'boolean
:group 'cocaine-line)
(defcustom cocaine-show-battery-info t
"If set to t, show battery icons with nerdicons."
:type 'boolean
:group 'cocaine-line)
(defcustom cocaine-show-use-nerd-icons t
"If set to t, show file icons with nerdicons."
:type 'boolean
:group 'cocaine-line)
(defcustom cocaine-show-processes-info t
"If set to t, show active processes."
:type 'boolean
:group 'cocaine-line)
(defcustom cocaine-show-misc-info nil
"If set to t, show misc processes."
:type 'boolean
:group 'cocaine-line)
(defcustom cocaine-show-git-info t
"If set to t, show Git branch and status."
:type 'boolean
:group 'cocaine-line)
(defcustom cocaine-show-copilot-info t
"If set to t, show copilot icon."
:type 'boolean
:group 'cocaine-line)
(defface cocaine-line-buffer-name-face
'((t :foreground "#a0a0ae" :weight bold))
"Face for buffer name."
:group 'cocaine-line)
(defface cocaine-line-major-mode-face
'((t :inherit doom-modeline-buffer-major-mode :weight bold))
"Face for major mode."
:group 'cocaine-line)
(defface cocaine-line-position-face
'((t :foreground "#FFA07A"))
"Face for buffer position."
:group 'cocaine-line)
(defface cocaine-line-time-face
'((t :foreground "#888899":weight light))
"Face for time display."
:group 'cocaine-line)
(defface cocaine-line-git-face
'((t :foreground "#a0a0ae"))
"Standard face for Git information."
:group 'cocaine-line)
(defface cocaine-line-project-face
'((t :foreground "#FFA066" :weight bold))
"Standard face for project information."
:group 'cocaine-line)
(defface cocaine-line-eglot-icon-face
'((t :inherit eglot-mode-line))
"Standard face for project information."
:group 'cocaine-line)
;; Git faces customization
(defcustom cocaine-git-faces
'((edited . cocaine-line-git-edited-face)
(added . cocaine-line-git-added-face)
(removed . cocaine-line-git-removed-face)
(conflict . cocaine-line-git-conflict-face)
(default . cocaine-line-git-face))
"Faces for different Git states."
:type '(alist :key-type symbol :value-type face)
:group 'cocaine-line)
(defface cocaine-line-git-edited-face
'((t :foreground "#F1FA8C"))
"Face for edited Git files."
:group 'cocaine-line)
(defface cocaine-line-git-added-face
'((t :foreground "#50FA7B"))
"Face for added Git files."
:group 'cocaine-line)
(defface cocaine-line-git-removed-face
'((t :foreground "#FF5555"))
"Face for removed Git files."
:group 'cocaine-line)
(defface cocaine-line-git-conflict-face
'((t :foreground "#FF79C6"))
"Face for Git conflicts."
:group 'cocaine-line)
;; Evil mode faces customization
(defcustom cocaine-evil-faces
'((normal . cocaine-line-evil-normal-face)
(insert . cocaine-line-evil-insert-face)
(visual . cocaine-line-evil-visual-face)
(replace . cocaine-line-evil-replace-face)
(emacs . cocaine-line-evil-emacs-face))
"Faces for different Evil states."
:type '(alist :key-type symbol :value-type face)
:group 'cocaine-line)
(defface cocaine-line-evil-normal-face
'((t :foreground "#FFFFFF" :background "#2D4F67" :weight bold
:box (:line-width 8 :color "#2D4F67")))
"Face for Evil normal state."
:group 'cocaine-line)
(defface cocaine-line-evil-insert-face
'((t :foreground "#333333" :background "#E6C384" :weight bold
:box (:line-width 8 :color "#E6C384")))
"Face for Evil insert state."
:group 'cocaine-line)
(defface cocaine-line-evil-visual-face
'((t :foreground "#333333" :background "#D27E99" :weight bold
:box (:line-width 8 :color "#D27E99")))
"Face for Evil visual state."
:group 'cocaine-line)
(defface cocaine-line-evil-replace-face
'((t :foreground "#333333" :background "#FF5D62" :weight bold
:box (:line-width 8 :color "#FF5D62")))
"Face for Evil replace state."
:group 'cocaine-line)
(defface cocaine-line-evil-emacs-face
'((t :foreground "#333333" :background "#B0C4DE" :weight bold
:box (:line-width 8 :color "#B0C4DE")))
"Face for Emacs state."
:group 'cocaine-line)
(defcustom cocaine-line-separator " | "
"Separator used between sections in the modeline."
:type 'string
:group 'cocaine-line)
(defcustom cocaine-line-separator-face 'cocaine-line-separator-face
"Face for the separator between sections."
:type 'face
:group 'cocaine-line)
(defface cocaine-line-separator-face
'((t :foreground "#54536D" :weight bold :height 0.8))
"Face for the separator between sections in cocaine-line."
:group 'cocaine-line)
(cl-defun cocaine-add-separator (&key str separator leftside (last nil)
(face 'cocaine-line-separator-face))
"Add a separator after STR if it is not empty or last.
LAST indicates if this is the last element.
FACE specifies which face to use for the separator."
(if (and str (not (string-empty-p str)) (not last))
(if leftside
(progn
(if separator
(concat str (propertize separator 'face face))
(concat str (propertize cocaine-line-separator 'face face))))
(progn
(if separator
(concat (propertize separator 'face face) str)
(concat (propertize cocaine-line-separator 'face face) str))))
str))
;; Evil status function
(defun cocaine-evil-status ()
"Show Evil status with custom face and correct vertical alignment."
(let* ((evil-state (if (and (bound-and-true-p evil-local-mode)
(boundp 'evil-state))
evil-state
'emacs))
(state-face (or (cdr (assq evil-state cocaine-evil-faces))
'cocaine-line-evil-emacs-face))
(state-name (if (eq evil-state 'emacs)
"EMACS"
(upcase (symbol-name evil-state)))))
(propertize (format " %s " state-name)
'face state-face)))
(defun cocaine-flycheck-mode-line ()
"Custom flycheck modeline with icons and counts."
(when (and (bound-and-true-p flycheck-mode)
(or flycheck-current-errors
(eq 'running flycheck-last-status-change)))
(let* ((count (flycheck-count-errors flycheck-current-errors))
(info (or (cdr (assq 'info count)) 0))
(warning (or (cdr (assq 'warning count)) 0))
(error (or (cdr (assq 'error count)) 0)))
(concat
(when (> info 0)
(propertize (format " %s %d" (nerd-icons-codicon "nf-cod-lightbulb") info)
'face '(:inherit success)))
(when (> warning 0)
(propertize (format " %s %d" (nerd-icons-codicon "nf-cod-warning") warning)
'face '(:inherit warning)))
(when (> error 0)
(propertize (format " %s %d" (nerd-icons-codicon "nf-cod-error") error)
'face '(:inherit error)))))))
(defun cocaine-process-info ()
"Show information about active processes."
(when cocaine-show-processes-info
(let ((process-info (format-mode-line mode-line-process)))
(unless (string-blank-p process-info)
(string-trim process-info)))))
(defun cocaine-misc-info ()
"Show information about misc info."
(when cocaine-show-misc-info
(let ((misc-info (format-mode-line mode-line-misc-info)))
(unless (string-blank-p misc-info)
(string-trim misc-info)))))
(defun cocaine-vc--rev (file backend)
"Get the revision for FILE in BACKEND."
(when-let ((rev (vc-working-revision file backend)))
(substring rev 0 (min (length rev) 7))))
(defun cocaine-eglot-info ()
"Return a string representing the current Eglot status for the mode line using nerd-icons."
(if (bound-and-true-p eglot--managed-mode)
(let* ((server (eglot-current-server))
(nick (and server (eglot--project-nickname server)))
(icon (propertize (nerd-icons-octicon "nf-oct-plug")
'face 'cocaine-line-eglot-icon-face)))
(if server
(concat (propertize (or nick "") 'face 'cocaine-line-project-face) " " icon " ")
icon))
(cocaine-project-info)))
(defun cocaine-git-info ()
"Show Git branch and status with custom faces."
(when (and cocaine-show-git-info
(buffer-file-name)
(vc-git-registered (buffer-file-name)))
(let* ((branch (vc-git-mode-line-string (buffer-file-name)))
(state (vc-state (buffer-file-name)))
(state-face (alist-get state cocaine-git-faces
(alist-get 'default cocaine-git-faces)))
(status-indicator (if (eq state 'up-to-date) "" "")))
(when branch
(propertize (format "%s %s%s"
(nerd-icons-octicon "nf-oct-git_branch")
(replace-regexp-in-string "^Git[:-]" "" branch)
status-indicator)
'face state-face)))))
(defun cocaine-project-name ()
"Get the project name if any."
(or
(and (fboundp 'project-name)
(project-current)
(project-name (project-current)))
(and (fboundp 'projectile-project-name)
(projectile-project-name))))
(defface cocaine-modeline-project-dir
'((t (:inherit (doom-modeline font-lock-string-face bold) :weight bold)))
"Face used for the project directory of the mode-line buffer path."
:group 'doom-modeline-faces)
(defun cocaine-project-info ()
"Show project information."
(let ((project (cocaine-project-name)))
(propertize project 'face 'doom-modeline-project-parent-dir)))
(defface cocaine-modeline-buffer-modified
'((t (:inherit (doom-modeline warning bold) :background unspecified :weight bold)))
"Face used for the \\='unsaved\\=' symbol in the mode-line."
:group 'doom-modeline-faces)
;; Custom functions for left section
(defun cocaine-buffer-name ()
"Show buffer name with custom face and icon (if available)."
(let* ((file-name (buffer-file-name))
(icon (when (and cocaine-show-use-nerd-icons file-name)
(nerd-icons-icon-for-file file-name t)))
(buffer-name (file-name-sans-extension
(substring-no-properties (format-mode-line "%b "))))
(face (if (buffer-modified-p)
'cocaine-modeline-buffer-modified
'doom-modeline-buffer-file)))
(if icon
(concat icon " " (propertize buffer-name 'face face))
(propertize buffer-name 'face 'doom-modeline-buffer-file))))
(defun cocaine-major-mode ()
"Show major mode with custom face."
(propertize (substring-no-properties (format-mode-line mode-name))
'face 'doom-modeline-buffer-file))
(defun cocaine-line-col ()
"Show line and column with custom face."
(when cocaine-show-column-info
(propertize "[%l:%c]" 'face 'cocaine-line-position-face)))
(defun cocaine-buffer-position ()
"Show buffer position percentage with custom face."
(when cocaine-show-buffer-position
(propertize "%p%" 'face 'cocaine-line-position-face)))
(defun cocaine-copilot-info ()
"HUD for Copilot."
(when (bound-and-true-p copilot-mode)
(propertize " " 'face '(:inherit success))))
(defun cocaine-line-spacer ()
"Show an empty string."
(propertize " "))
(defun cocaine-time ()
"Show time with custom face."
(propertize (format-time-string "%H:%M") 'face 'doom-modeline-time))
(defun cocaine-battery-info ()
"Show battery percentage or charging status using text and nerd-font icons on macOS."
(when (and cocaine-show-battery-info
(bound-and-true-p display-battery-mode))
(let* ((battery-plist (funcall battery-status-function))
(percentage (string-to-number (battery-format "%p" battery-plist)))
(status (battery-format "%B" battery-plist))
(charging (string= status "AC"))
(icon (cond
(charging (nerd-icons-faicon "nf-fa-plug"))
((>= percentage 87.5) (nerd-icons-faicon "nf-fa-battery"))
((>= percentage 62.5) (nerd-icons-faicon "nf-fa-battery_3"))
((>= percentage 37.5) (nerd-icons-faicon "nf-fa-battery_2"))
((>= percentage 12.5) (nerd-icons-faicon "nf-fa-battery_1"))
(t (nerd-icons-faicon "nf-fa-battery_0")))))
(if (and percentage status)
(format "%s"
icon
(if charging " (Charging)" ""))
"No battery info"))))
(defsubst salih/doom-modeline--bar ()
"The default bar regulates the height of the mode-line in GUI."
(unless (and doom-modeline--bar-active doom-modeline--bar-inactive)
(let ((width doom-modeline-bar-width)
(height (max doom-modeline-height (doom-modeline--font-height))))
(setq doom-modeline--bar-active
(doom-modeline--create-bar-image
'mode-line width height)
doom-modeline--bar-inactive
(doom-modeline--create-bar-image
'mode-line-inactive width height))))
(if (doom-modeline--active)
doom-modeline--bar-active
doom-modeline--bar-inactive))
(defun cocaine-left-section ()
"Create the left section of the modeline."
(let ((left-section
(list
(concat
(salih/doom-modeline--bar) ;; just to adjust height.
;; (propertize " "
;; 'display '(raise 0.3)
;; 'face 'mode-line)
(cocaine-add-separator :str
(doom-modeline-segment--modals)
:leftside t
:separator "| ")
(cocaine-line-spacer)
(cocaine-buffer-name)
(cocaine-add-separator :str
(cocaine-major-mode)
:separator " | ")
(cocaine-add-separator
:str
(if (derived-mode-p
'pdf-view-mode)
(concat " " (salih/doom-modeline-update-pdf-pages-only-percent))
(substring (format-mode-line (doom-modeline-segment--buffer-position)) 1)))
(cocaine-add-separator :str (mode-line-segment-hud))
(cocaine-add-separator :str (cocaine-process-info))))))
left-section))
(defun cocaine-right-section ()
"Create the right section of the modeline."
(let ((right-section (concat
(if (derived-mode-p 'pdf-view-mode)
(cocaine-add-separator
:str (propertize (salih/doom-modeline-update-pdf-pages-no-percent)) :leftside t)
"")
;; (cocaine-add-separator :str (doom-modeline-segment--salih/word-count) :leftside t)
(cocaine-add-separator :str (doom-modeline-segment--salih/selection-info) :leftside t)
(cocaine-add-separator :str (doom-modeline-segment--matches) :leftside t)
(cocaine-add-separator :str (doom-modeline-segment--irc) :leftside t)
(cocaine-add-separator :str (and (boundp 'mu4e-alert-mode-line) (or mu4e-alert-mode-line "")) :leftside t)
(cocaine-add-separator :str (doom-modeline-segment--lsp) :leftside t)
(cocaine-add-separator :str (doom-modeline-segment--minor-modes) :leftside t)
(cocaine-add-separator :str (cocaine-flycheck-mode-line) :leftside t)
(cocaine-add-separator :str (doom-modeline-segment--vcs) :leftside t)
(if (and (boundp 'org-mode-line-string)
(not (s-equals? org-mode-line-string "") )
org-mode-line-string)
org-mode-line-string
(cocaine-time))
(cocaine-add-separator :str awqat-mode-line-string :leftside nil))))
(list (propertize " " 'display `((space :align-to (- right ,(string-width right-section)))))
right-section)))
(defun cocaine-mode-line-format ()
"Generate the format for cocaine-line modeline."
(let ((left (cocaine-left-section))
(right (cocaine-right-section)))
(append left right)))
(defun cocaine-update-mode-line (&optional _)
"Update modeline for all windows."
(force-mode-line-update t))
(defun cocaine-set-mode-line ()
"Set the modeline format for cocaine-line."
(setq-default mode-line-format '(:eval (cocaine-mode-line-format))))
(defun cocaine-register-hooks ()
"Register hooks to update the modeline."
(add-hook 'post-command-hook #'cocaine-update-mode-line)
(add-hook 'window-configuration-change-hook #'cocaine-update-mode-line)
(add-hook 'focus-in-hook #'cocaine-update-mode-line)
(add-hook 'focus-out-hook #'cocaine-update-mode-line)
(add-hook 'window-state-change-hook #'cocaine-update-mode-line) ; Add this hook
(add-hook 'after-load-theme-hook #'cocaine-update-mode-line))
(defun cocaine-remove-hooks ()
"Remove hooks to update the modeline."
(remove-hook 'post-command-hook #'cocaine-update-mode-line)
(remove-hook 'window-configuration-change-hook #'cocaine-update-mode-line)
(remove-hook 'focus-in-hook #'cocaine-update-mode-line)
(remove-hook 'focus-out-hook #'cocaine-update-mode-line)
(remove-hook 'window-state-change-hook #'cocaine-update-mode-line) ; Remove this hook
(remove-hook 'after-load-theme-hook #'cocaine-update-mode-line))
(define-minor-mode cocaine-line-mode
"Activate Cocaine Line mode."
:group 'cocaine-line
:global t
:lighter nil
(if cocaine-line-mode
(progn
(cocaine-set-mode-line)
(cocaine-register-hooks)
(cocaine-update-mode-line))
(setq-default mode-line-format (default-value 'mode-line-format))
(cocaine-remove-hooks)
(force-mode-line-update t)))
(provide 'cocaine-line)
;;; cocaine-line.el ends here