;;; jao-minibuffer.el --- using the minibuffer to report status -*- lexical-binding: t; -*- ;; Copyright (C) 2020, 2021, 2022 jao ;; Author: jao ;; 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 . ;;; 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 "" " ")) (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) (string-pixel-width jao-minibuffer-right-margin))) (padding (propertize "-" 'display `(space :align-to (- (+ right right-margin) (,len)))))) (concat padding text jao-minibuffer-right-margin))) (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 (fboundp 'string-pixel-width) 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 (length 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