;;; 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 t) (defvar jao-minibuffer-frame-width nil) (defvar jao-minibuffer-notification-timeout 5) (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--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)) (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) (let ((hack (derived-mode-p 'pdf-view-mode 'doc-view-mode))) (with-current-buffer jao-minibuffer--name (delete-region (point-min) (point-max)) (insert msg) (when hack (other-window 1) (other-window -1))))) (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-mode) (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-turn-on () (interactive) (advice-add 'select-window :after #'jao-minibuffer-refresh) (advice-add 'force-mode-line-update :after #'jao-minibuffer-refresh) (setq set-message-function #'jao-minibuffer--format-msg) (setq clear-message-function #'jao-minibuffer-refresh) (jao-minibuffer-refresh)) ;;;###autoload (defun jao-minibuffer-turn-off () (interactive) (advice-remove 'select-window #'jao-minibuffer-refresh) (advice-remove 'force-mode-line-update #'jao-minibuffer-refresh) (setq set-message-function nil) (setq clear-message-function nil) (jao-minibuffer--insert "")) ;;;###autoload (define-minor-mode jao-minibuffer-mode "Show minibuffer status" :global t :lighter "" :group 'jao (if jao-minibuffer-mode (jao-minibuffer-turn-on) (jao-minibuffer-turn-off))) ;;;###autoload (defun jao-minibuffer-refresh (&rest _ignore) (interactive) (when jao-minibuffer-mode (let* ((jao-minibuffer-mode 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 ""))))) ;; (add-hook 'window-selection-change-functions #'jao-minibuffer-refresh) (provide 'jao-minibuffer) ;;; jao-minibuffer.el ends here