summaryrefslogtreecommitdiffhomepage
path: root/lib/eos/jao-minibuffer.el
blob: 100e8096cb84f3b79635de5ff60374841c2cd375 (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
;;; jao-minibuffer.el --- using the minibuffer to report status  -*- lexical-binding: t; -*-

;; Copyright (C) 2020, 2021  jao

;; Author: jao <mail@jao.io>
;; 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:

;; Simple asynchronous display of information in the minibuffer.

;;; Code:

(defvar jao-minibuffer-info ())
(defvar jao-minibuffer-msg-info '(""))
(defvar jao-minibuffer-notification nil)
(defvar jao-minibuffer-align-right-p t)
(defvar jao-minibuffer-right-margin (if window-system 0 1))
(defvar jao-minibuffer-maximized-frames-p nil)
(defvar jao-minibuffer-frame-width nil)
(defvar jao-minibuffer-notification-timeout 5)
(defvar jao-minibuffer-enabled-p t)
(defvar jao-minibuffer-active-buffer-line-color "azure4")
(defvar jao-minibuffer-inactive-buffer-line-color "grey25")

(defconst jao-minibuffer--name " *Minibuf-0*")

(defun jao-minibuffer--text-with-padding (text)
  "Return TEXT with padding on the left.
The padding pushes TEXT to the right edge of the mode-line."
  (let* ((len (- (string-pixel-width text) jao-minibuffer-right-margin))
         (padding (propertize
                   "-" 'display
                   `(space :align-to (- (+ right right-margin) (,len))))))
    (concat padding text)))

(defun jao-minibuffer--trim (s w)
  (if (<= (string-width (or s "")) w)
      (format (format "%%%ds" (if jao-minibuffer-align-right-p w (- w))) s)
    (substring s 0 w)))

(defun jao-minibuffer--current ()
  (with-current-buffer jao-minibuffer--name
    (buffer-substring (point-min) (point-max))))

(defun jao-minibuffer--width ()
  (cond ((numberp jao-minibuffer-frame-width) jao-minibuffer-frame-width)
        (jao-minibuffer-maximized-frames-p (frame-width))
        (t (min (frame-width) (window-width (minibuffer-window))))))

(defun jao-minibuffer--format-info (&optional info)
  (mapconcat 'string-trim
             (seq-filter (lambda (s) (not (string-blank-p s)))
                         (mapcar 'format-mode-line
                                 (if jao-minibuffer-align-right-p
                                     (or info jao-minibuffer-info)
                                   (reverse (or info jao-minibuffer-info)))))
             " "))

(defun jao-minibuffer--aligned (&optional w currentp)
  (let* ((msg (cond (currentp (jao-minibuffer--current))
                    (jao-minibuffer-notification
                     (format-mode-line jao-minibuffer-notification))
                    (t (jao-minibuffer--format-info))))
         (msg (if jao-minibuffer-align-right-p
                  (string-trim msg)
                (string-trim-left msg)))
         (msg (propertize msg :minibuffer-message t)))
    (when (not (string-empty-p msg))
      (if (and window-system jao-minibuffer-align-right-p)
          (jao-minibuffer--text-with-padding msg)
       (let* ((mw (jao-minibuffer--width))
                 (w (mod (or w (string-width (or (current-message) ""))) mw))
                 (w (- mw w jao-minibuffer-right-margin)))
            (if (> w 0) (jao-minibuffer--trim msg w) ""))))))

(defun jao-minibuffer--insert (msg)
  (with-current-buffer jao-minibuffer--name
    (erase-buffer)
    (insert msg)))

(defun jao-minibuffer--format-msg (msg)
  (let* ((msgs (split-string msg "\n"))
         (prefix (when-let (p (string-join (butlast msgs) "\n"))
                   (unless (string-blank-p p) (concat p "\n"))))
         (msg (car (last msgs)))
         (msg (string-trim (replace-regexp-in-string "\n" " " msg)))
         (msg (if (string-blank-p msg) msg (concat msg "  "))))
    (if jao-minibuffer-align-right-p
        (concat prefix msg (jao-minibuffer--aligned (string-width (or msg ""))))
      (concat prefix
              (jao-minibuffer--aligned (+ 3 (string-width (or msg ""))))
              "   " msg))))

(defun jao-minibuffer--set-message (msg)
  (if (or (not jao-minibuffer-enabled-p)
          (and msg
               (not (string-blank-p msg))
               (bound-and-true-p current-minibuffer-command)))
      msg
    (jao-minibuffer--format-msg msg)))

(defvar-local w3m-current-title nil)
(defvar-local eww-data nil)
(defvar-local jao-notmuch--tree-buffer nil)
(defvar circe-chat-target nil)
(defvar exwm-class-name nil)

(defvar jao-minibuffer--mode-line-position
  '(exwm-class-name ("")
                    ("%n %2c %l "
                     (:eval (format "%d" (line-number-at-pos (point-max)))))))

(defvar jao-minibuffer--mode-line-format
  `("%["
    (:propertize
     (:eval (cond ((derived-mode-p 'gnus-group-mode
                                   'gnus-article-mode
                                   'gnus-summary-mode)
                   mode-line-buffer-identification)
                  ((derived-mode-p 'circe-channel-mode)
                   (format "%s [%d]" (buffer-name) (length (circe-channel-nicks))))
                  (jao-notmuch--tree-buffer (buffer-name jao-notmuch--tree-buffer))
                  ((not (null eww-data)) (plist-get eww-data :title))
                  (t "%b")))
     face jao-themes-f00)
    "%]"
    (:propertize " (" face jao-themes-dimm)
    (:propertize mode-name face jao-themes-f00)
    (:propertize ("" minor-mode-alist) face jao-themes-f11)
    (:propertize ")" face jao-themes-dimm)
    (:propertize (vc-mode vc-mode) face jao-themes-f10)
    (:propertize mode-line-position face jao-themes-f12)
    " "
    global-mode-string
    (:propertize (" %Z%*%+ " (current-input-method current-input-method-title))
                 face jao-themes-warning)
    (:propertize "·" display "")))

(defvar jao-minibuffer--original-modeline nil)

(defun jao-minibuffer--add-variable (list-name variable-name &optional order)
  (let ((v `(:eval ,variable-name)))
    (set list-name (remove v (symbol-value list-name)))
    (add-to-ordered-list list-name v order)))

;;;###autoload
(defun jao-minibuffer-add-variable (variable-name &optional order)
  (jao-minibuffer--add-variable 'jao-minibuffer-info variable-name order))

;;;###autoload
(defun jao-minibuffer-add-msg-variable (variable-name &optional order)
  (jao-minibuffer--add-variable 'jao-minibuffer-msg-info variable-name order))

(defun jao-minibuffer-adjust-mode-line-faces ()
  (let ((bg (frame-parameter nil 'background-color)))
    (set-face-attribute 'mode-line nil :box nil :height 1
                        :background bg :foreground bg
                        :overline jao-minibuffer-active-buffer-line-color
                        :underline jao-minibuffer-inactive-buffer-line-color
                        :extend t)
    (set-face-attribute 'mode-line-inactive nil :box nil :height 1
                        :background bg :foreground bg
                        :overline bg
                        :underline jao-minibuffer-inactive-buffer-line-color
                        :extend t)))

;;;###autoload
(defun jao-minibuffer-add-mode-line (order)
  (setq jao-minibuffer--original-modeline mode-line-format)
  (setq-default mode-line-format '(" "))
  (setq-default mode-line-position jao-minibuffer--mode-line-position)
  (dolist (b (buffer-list))
    (with-current-buffer b (setq-local mode-line-format '(" "))))
  (jao-minibuffer-add-variable 'jao-minibuffer--mode-line-format order)
  (jao-minibuffer-adjust-mode-line-faces))

;;;###autoload
(defun jao-minibuffer-toggle ()
  (interactive)
  (setq jao-minibuffer-enabled-p (not jao-minibuffer-enabled-p))
  (if jao-minibuffer-enabled-p
      (jao-minibuffer-refresh)
    (jao-minibuffer--insert "")))

;;;###autoload
(defun jao-minibuffer-refresh (&rest _ignore)
  (interactive)
  (when (and jao-minibuffer-enabled-p
             (not (bound-and-true-p current-minibuffer-command)))
    (let* ((jao-minibuffer-enabled-p nil)
           (window-selection-change-functions nil)
           (msg (jao-minibuffer--format-info jao-minibuffer-msg-info))
           (msg (jao-minibuffer--format-msg (or msg ""))))
      (jao-minibuffer--insert (or msg "")))))

(setq set-message-function #'jao-minibuffer--format-msg)
(setq clear-message-function #'jao-minibuffer-refresh)
;; (add-hook 'window-selection-change-functions #'jao-minibuffer-refresh)
(advice-add 'select-window :after #'jao-minibuffer-refresh)
(advice-add 'force-mode-line-update :after #'jao-minibuffer-refresh)

(provide 'jao-minibuffer)
;;; jao-minibuffer.el ends here