about summary refs log tree commit diff stats
path: root/lisp/acdw-modeline.el
blob: 9963309b56557abfccd57e53f7485127f7078ac4 (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
;;; acdw-modeline.el -*- lexical-binding: t; coding: utf-8-unix -*-
;; Author: Case Duckworth <(rot13-string "npqj@npqj.arg")>
;; Created: Sometime during Covid-19, 2020
;; Keywords: configuration
;; URL: https://tildegit.org/acdw/emacs

;; This file is NOT part of GNU Emacs.

;;; License:
;; Everyone is permitted to do whatever with this software, without
;; limitation.  This software comes without any warranty whatsoever,
;; but with two pieces of advice:
;; - Don't hurt yourself.
;; - Make good choices.

;;; Commentary:
;; `acdw-modeline' is a dumping ground for extra modeline functions, so they
;; don't clutter up `init.el'.

;;; Code:

(require 'simple-modeline)
(require 'minions)

(defcustom acdw-modeline/word-count-modes
  (mapcar (lambda (m) (cons m nil)) simple-modeline-word-count-modes)
  "Alist of modes to functions that `acdw-modeline/word-count' should dispatch.
If the cdr of the cons cell is nil, use the default function (`count-words').
Otherwise, cdr should be a function that takes two points (see `count-words')."
  :type '(alist :key-type (symbol :tag "Major-Mode")
                :value-type function)
  :group 'simple-modeline)

(defun acdw-modeline/buffer-name ()     ; gonsie
  "Display the buffer name in a face reflecting its modified status."
  (propertize (concat " "
                      (truncate-string (/ (window-total-width) 2)
                                       (buffer-name) "~"))
              'face
              (if (buffer-modified-p)
                  'font-lock-warning-face
                'font-lock-type-face)
              'help-echo (or (buffer-file-name)
                             (buffer-name))))

(defun acdw-modeline/erc ()
  "ERC indicator for the modeline."
  (when (and (bound-and-true-p erc-track-mode)
             (boundp 'erc-modified-channels-object))
    (format-mode-line erc-modified-channels-object)))

(defun acdw-modeline/god-mode-indicator ()
  "Display an indicator if `god-local-mode' is active."
  (when (bound-and-true-p god-local-mode)
    " Ω"))

(defun acdw-modeline/major-mode ()
  "Displays the current major mode in the mode-line."
  (propertize
   (concat " "
           (or (and (boundp 'delighted-modes)
                    (cadr (assq major-mode delighted-modes)))
               (format-mode-line mode-name)))
   'face 'bold
   'keymap mode-line-major-mode-keymap
   'mouse-face 'mode-line-highlight))

(defun acdw-modeline/minions () ; by me
  "Display a button for `minions-minor-modes-menu'."
  (concat
   " "
   (propertize
    "&"
    'help-echo (format
                "Minor modes menu\nmouse-1: show menu.")
    'local-map (purecopy (simple-modeline-make-mouse-map
                          'mouse-1
                          (lambda (event)
                            (interactive "e")
                            (with-selected-window (posn-window
                                                   (event-start event))
                              (minions-minor-modes-menu)))))
    'mouse-face 'mode-line-highlight)))

(defun acdw-modeline/modified ()        ; modified from `simple-modeline'
  "Displays a color-coded buffer modification/read-only
indicator in the mode-line."
  (if (not (string-match-p "\\*.*\\*" (buffer-name)))
      (let* ((read-only (and buffer-read-only (buffer-file-name)))
             (modified (buffer-modified-p)))
        (propertize
         (concat " "
                 (cond
                  ((derived-mode-p 'special-mode
                                   'lui-mode)
                   "~")
                  (read-only "=")
                  (modified "+")
                  (t "-")))
         'help-echo (format
                     (concat "Buffer is %s and %smodified\n"
                             "mouse-1: Toggle read-only status.")
                     (if read-only "read-only" "writable")
                     (if modified "" "not "))
         'local-map (purecopy (simple-modeline-make-mouse-map
                               'mouse-1
                               (lambda (event)
                                 (interactive "e")
                                 (with-selected-window
                                     (posn-window (event-start event))
                                   (read-only-mode 'toggle)))))
         'mouse-face 'mode-line-highlight))))

(defun acdw-modeline/narrowed ()
  "Display an indication if the buffer is narrowed."
  (when (buffer-narrowed-p)
    (concat
     ""
     (propertize
      "N"
      'help-echo (format "%s\n%s"
                         "Buffer is narrowed"
                         "mouse-2: widen buffer.")
      'local-map (purecopy (simple-modeline-make-mouse-map
                            'mouse-2 #'mode-line-widen))
      'mouse-face 'mode-line-highlight))))

(define-minor-mode file-percentage-mode
  "Toggle the percentage display in the mode line (File Percentage Mode)."
  :init-value t :global t :group 'mode-line)

(defun acdw-modeline/position ()
  "Displays the current cursor position in the mode-line.

Unlike `simple-modeline-segment-position', this changes the first
character from '+' to '-' if the region goes 'backward' -- that
is, if point < mark."
  `((line-number-mode
     ((column-number-mode
       (column-number-indicator-zero-based
        (9 " %l/%c")
        (9 " %l/%C"))
       (6 " L%l")))
     ((column-number-mode
       (column-number-indicator-zero-based
        (5 " C%c")
        (5 " C%C")))
      " "))
    (file-percentage-mode
     (7 "%p%% "))
    ,(if (region-active-p)
         (propertize (format "%s%-5d"
                             (if (and (mark)
                                      (< (point) (mark)))
                                 "-"
                               "+")
                             (apply #'+ (mapcar
                                         (lambda (pos)
                                           (- (cdr pos)
                                              (car pos)))
                                         (region-bounds))))
                     'font-lock-face 'font-lock-variable-name-face))))

(defun acdw-modeline/reading-mode ()
  "Display an indicator if currently in reading mode, mine or EWW's."
  (concat (if reading-mode "R" "") (if eww-readable-p "w" "")))

(defun acdw-modeline/text-scale ()
  "Display the text scaling from the modeline, if scaled."
  ;; adapted from https://github.com/seagle0128/doom-modeline
  (when (and (boundp 'text-scale-mode-amount)
             (/= text-scale-mode-amount 0))
    (format
     (if (> text-scale-mode-amount 0)
         " (%+d)"
       " (%-d)")
     text-scale-mode-amount)))

(defun acdw-modeline/track ()
  "Display `tracking-mode' information."
  '(tracking-mode
    tracking-mode-line-buffers))

(defun acdw-modeline/vc-branch ()
  "Display the version control branch of the current buffer in the modeline."
  ;; from https://www.gonsie.com/blorg/modeline.html, from Doom
  (if-let ((backend (vc-backend buffer-file-name)))
      (concat " " (substring vc-mode (+ (if (eq backend 'Hg) 2 3) 2)))))

(defun acdw-modeline/wc ()
  "Display current `wc-buffer-stats'."
  (when (bound-and-true-p wc-mode)
    (or wc-buffer-stats " [w]")))

(defun acdw-modeline/winum ()
  "Show the `winum' number of the current window in the modeline.
Only shows if there is more than one window."
  (when (and (bound-and-true-p winum-mode)
             (> winum--window-count 1))
    (format winum-format (winum-get-number-string))))

(defun acdw-modeline/word-count ()
  "Display a buffer word count, depending on the major mode.
Uses `acdw-modeline/word-count-modes' to determine which function to use."
  (when-let ((modefun
              (assoc major-mode acdw-modeline/word-count-modes #'equal)))
    (let* ((fn (or (cdr modefun)
                   #'count-words))
           (r (region-active-p))
           (min (if r (region-beginning) (point-min)))
           (max (if r (region-end) (point-max))))
      (format " %s%dW" (if r "+" "") (funcall fn min max)))))

(provide 'acdw-modeline)
;;; acdw-modeline.el ends here