mu4e-modeline: only cache global items, improve layout

So buffer-local items get flushed out automatically when we leave the
buffers where the local items were specified.

Honor mu4e-modeline-max-width
This commit is contained in:
Dirk-Jan C. Binnema 2023-01-17 21:05:20 +02:00
parent 1fd4141453
commit 3716e6da3b
2 changed files with 40 additions and 27 deletions

View File

@ -42,12 +42,6 @@
:type 'boolean
:group 'mu4e)
(defcustom mu4e-modeline-max-width 42
"Determines the maximum length of the modeline string.
If the string exceeds this limit, it will be truncated to fit."
:type 'integer
:group 'mu4e)
(defcustom mu4e-completing-read-function 'ido-completing-read
"Function to be used to receive user-input during completion.
Suggested possible values are:
@ -100,18 +94,7 @@ marked as read-only, or non-nil otherwise."
;;; Modeline
(defun mu4e-quote-for-modeline (str)
"Quote STR to be used literally in the modeline.
The string will be shortened to fit if its length exceeds
`mu4e-modeline-max-width'."
(replace-regexp-in-string
"%" "%%"
(truncate-string-to-width str mu4e-modeline-max-width 0 nil t)))
;;; Messages, warnings and errors
(defun mu4e-format (frm &rest args)
"Create [mu4e]-prefixed string based on format FRM and ARGS."

View File

@ -29,6 +29,17 @@
(require 'cl-lib)
(defcustom mu4e-modeline-max-width 42
"Determines the maximum length of the local modeline string.
If the string exceeds this limit, it will be truncated to fit.
Note: this only affects the local modeline items (such as the context,
the search properties and the last query), not the global items
(such as the favorite bookmark results)."
:type 'integer
:group 'mu4e-modeline)
(defvar-local mu4e--modeline-buffer-items nil
"List of buffer-local items for the mu4e modeline.
Each element is function that evaluates to a string.")
@ -45,23 +56,42 @@ the buffer-local one."
(if global
'mu4e--modeline-global-items
'mu4e--modeline-buffer-items)
func))
func 'append))
(defun mu4e--modeline-quote-and-truncate (str)
"Quote STR to be used literally in the modeline.
The string is truncaed to fit if its length exceeds
`mu4e-modeline-max-width'."
(replace-regexp-in-string
"%" "%%"
(truncate-string-to-width str mu4e-modeline-max-width 0 nil t)))
(defvar mu4e--modeline-item nil
"Mu4e item for the global-mode-line.")
(defvar mu4e--modeline-string-cached nil
"Cached version of the modeline string.")
(defvar mu4e--modeline-global-string-cached nil
"Cached version of the _global_ modeline string.
Note that we don't cache the local parts, so that the modeline
gets updated when we leave the buffer from which the local parts
originate.")
(defun mu4e--modeline-string ()
"Get the current mu4e modeline string."
(or mu4e--modeline-string-cached
(setq mu4e--modeline-string-cached
(let* ((collect
(lambda (lst)
(mapconcat
(lambda (func) (or (funcall func) ""))
(append mu4e--modeline-buffer-items
mu4e--modeline-global-items)
" "))))
(lambda (func) (or (funcall func) "")) lst " ")))
(global-string ;; global string is _cached_ as it may be expensive.
(or mu4e--modeline-global-string-cached
(setq mu4e--modeline-global-string-cached
(funcall collect mu4e--modeline-global-items)))))
(concat
;; (local) buffer items are _not_ cached, so they'll get update
;; automatically when leaving the buffer.
(mu4e--modeline-quote-and-truncate
(funcall collect mu4e--modeline-buffer-items))
" "
global-string)))
(define-minor-mode mu4e-modeline-mode
"Minor mode for showing mu4e information on the modeline."
@ -85,7 +115,7 @@ the buffer-local one."
(defun mu4e--modeline-update ()
"Recalculate and force-update the modeline."
(when mu4e-modeline-mode
(setq mu4e--modeline-string-cached nil)
(setq mu4e--modeline-global-string-cached nil)
(force-mode-line-update)))
(provide 'mu4e-modeline)