aboutsummaryrefslogtreecommitdiff
path: root/Emacs/.config/emacs/elisp/better-mode-line.el
blob: 39c8d5ed624d3c29dc2f9836e47fc61d384f1ffc (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
;;; better-mode-line.el --- A better mode-line system designed by me  -*- lexical-binding: t; -*-

;; Copyright (C) 2024  Aryadev Chavali

;; Author: Aryadev Chavali <aryadev@aryadevchavali.com>
;; Keywords:

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

;; There are 3 lists for the left, centre and right of the mode-line.  They're
;; the same as mode-line-format in terms of general schema as they're fed
;; directly into that variable.  Padding strings are automatically generated
;; based on these segments so be sure to set these.

;;; Code:

(defvar bml/left-segment nil
  "List of elements that are placed on the left of the mode-line")

(defvar bml/centre-segment nil
  "List of elements that should be on the centre of the mode-line")

(defvar bml/right-segment nil
  "List of elements that should be on the right of the mode-line")

(defconst bml/--minimum-padding 4
  "Minimum size of padding string.")

(defun bml/--get-padding-size (other-size)
  "Compute length of padding to ensure string of size OTHER is on an
extreme end to CENTRE-SEGMENT."
  (let* ((centre-size (length (format-mode-line bml/centre-segment)))
         (win-width (window-width))
         (margins (window-margins))
         (width (if (null (car margins))
                    win-width
                  (+ (car margins) win-width (cdr margins)))))
    (- (floor (/ width 2)) (floor (/ centre-size 2)) other-size)))

(defun bml/--generate-padding (segment)
  "Make padding string to separate center segment from SEGMENT."
  (let* ((segment-size (length (format-mode-line segment)))
         (padding-size (bml/--get-padding-size segment-size)))
    (make-string (if (< padding-size bml/--minimum-padding)
                     bml/--minimum-padding
                   padding-size)
                 ?\s)))

(defun bml/setup-mode-line ()
  "Call this to setup the mode-line when:
- first loading the package.
- segments are updated."
  (setq-default mode-line-format
                `(,bml/left-segment
                  (:eval (bml/--generate-padding
                          bml/left-segment))
                  ,bml/centre-segment
                  (:eval (bml/--generate-padding
                          bml/right-segment))
                  ,bml/right-segment)))

(provide 'better-mode-line)

;;; better-mode-line.el ends here

;; Local Variables:
;; read-symbol-shorthands: (("bml" . "better-mode-line"))
;; End: