From b01f6773aeb9bc58e55d71e86f9c0afdb66ae8aa Mon Sep 17 00:00:00 2001 From: jao Date: Mon, 19 Oct 2020 23:59:04 +0100 Subject: jao-minibuffer: new utilities to display status in minibuffer --- misc/jao-minibuffer.el | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ net/jao-maildir.el | 31 ++++++++++------------- 2 files changed, 81 insertions(+), 18 deletions(-) create mode 100644 misc/jao-minibuffer.el diff --git a/misc/jao-minibuffer.el b/misc/jao-minibuffer.el new file mode 100644 index 0000000..9f28390 --- /dev/null +++ b/misc/jao-minibuffer.el @@ -0,0 +1,68 @@ +;;; jao-minibuffer.el --- using the minibuffer to report status -*- lexical-binding: t; -*- + +;; Copyright (C) 2020 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-align-right-p t) +(defvar jao-minibuffer-right-margin 2) + +;; (setq jao-minibuffer-align-right-p t) + +(defun jao-minibuffer--trim (s w) + (if (<= (length s) w) + (format (format "%%%ds" w) s) + (substring s 0 w))) + +(defun jao-minibuffer--aligned (&optional w) + (let ((msg (format-mode-line jao-minibuffer-info))) + (when (not (string-empty-p msg)) + (if jao-minibuffer-align-right-p + (let ((w (- (frame-width) (or w 0) jao-minibuffer-right-margin))) + (if (> w 0) (jao-minibuffer--trim msg w) "")) + (concat " ยท " msg))))) + +(defun jao-minibuffer--message (old-func &rest args) + (let ((msg (let ((inhibit-message t)) (apply old-func args)))) + (when (not inhibit-message) + (let ((message-log-max nil) + (new-msg (concat msg (jao-minibuffer--aligned (length msg))))) + (funcall old-func "%s" new-msg))) + msg)) + +(advice-add #'message :around #'jao-minibuffer--message) + +;;;###autoload +(defun jao-minibuffer-refresh () + (interactive "") + (with-current-buffer " *Minibuf-0*" + (erase-buffer) + (insert (jao-minibuffer--aligned)))) + +;;;###autoload +(defun jao-minibuffer-add-variable (variable-name &optional append) + (add-to-list 'jao-minibuffer-info `(:eval ,variable-name) append)) + +(provide 'jao-minibuffer) +;;; jao-minibuffer.el ends here diff --git a/net/jao-maildir.el b/net/jao-maildir.el index ee064a1..cae0372 100644 --- a/net/jao-maildir.el +++ b/net/jao-maildir.el @@ -27,6 +27,8 @@ ;;; Code: +(require 'jao-minibuffer) + (defvar jao-maildirs nil) (defvar jao-maildir-counts nil) (defvar jao-maildir-debug-p nil) @@ -45,17 +47,7 @@ jao-maildirs))) (defvar jao-maildir-tracked-maildirs) -(defvar jao-maildir-mode-line-string "") -(defvar jao-maildir-additional-info ()) - -(defun jao-maildir-refresh-echo () - (with-current-buffer " *Minibuf-0*" - (erase-buffer) - (let ((mailp (not (string-blank-p jao-maildir-mode-line-string)))) - (insert (format "%s%s%s" - (if mailp "Mail" "") - (if mailp jao-maildir-mode-line-string "") - (or (format-mode-line jao-maildir-additional-info) "")))))) +(defvar jao-maildir-info-string "") (defgroup jao-maildir-faces nil "Faces" :group 'faces) @@ -64,7 +56,7 @@ "Face used to highlihgt non-boring tracked maildirs" :group 'jao-maildir-faces) -(defun jao-maildir--update-mode-line-string () +(defun jao-maildir--update-info-string () (let ((total 0) (counts)) (dolist (c jao-maildir-counts) @@ -81,9 +73,10 @@ (if ab (push am counts) (setq counts (nconc counts (list am)))))))) (let* ((total (if (> total 0) (format " %d " total) " ")) (s (mapconcat 'identity counts " ")) - (s (if (string-empty-p s) s (concat s " ")))) - (setq jao-maildir-mode-line-string (format "%s%s" total s)) - (when jao-maildir-echo-p (jao-maildir-refresh-echo)) + (s (if (string-empty-p s) s (concat s " "))) + (s (format "%s%s%s" "" total s))) + (setq jao-maildir-info-string s) + (when jao-maildir-echo-p (jao-minibuffer-refresh)) (force-mode-line-update t)))) (defvar jao-maildir--watches nil) @@ -103,7 +96,7 @@ (setcdr (assoc mbox jao-maildir-counts) (jao-maildir--maildir-new-count mbox)) ;; (jao-maildir-counts) - (jao-maildir--update-mode-line-string) + (jao-maildir--update-info-string) (when cb (funcall cb mbox))))) (defun jao-maildir--setup-watches (cb) @@ -119,8 +112,10 @@ (defun jao-maildir-setup (maildirs mode-line &optional cb) (setq jao-maildirs maildirs) (setq jao-maildir-counts (jao-maildir-counts)) - (when mode-line - (add-to-list 'global-mode-string 'jao-maildir-mode-line-string t)) + (cond ((eq 'mode-line mode-line) + (add-to-list 'global-mode-string 'jao-maildir-info-string t)) + (mode-line + (jao-minibuffer-add-variable 'jao-maildir-info-string))) (jao-maildir--setup-watches cb)) -- cgit v1.2.3