Last active
October 21, 2020 18:05
-
-
Save rougier/baaf4ff6e0461680e3f070c5c32b64a2 to your computer and use it in GitHub Desktop.
Emacs: persistent message in the echo area
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require 'subr-x) | |
(defun enhanced-message (orig-fun &rest args) | |
"This enhanced message displays a regular message in the echo area | |
and adds a specific text on the right part of the echo area. This | |
is to be used as an advice." | |
(let* ((right (propertize | |
;; Hack: The first space is a thin space, not a regular space | |
(format-time-string " %A %d %B %Y, %H:%M ") | |
'face '(:height 0.85 | |
:overline t | |
:family "Roboto Condensed" | |
:inherit face-salient))) | |
;; -10 is a crude approximation for compensating the size of displayed | |
;; text because "Roboto Condensed" is not monospaced. We should rather | |
;; compute the actual width in pixel of the displayed text and compute | |
;; the proper amount of spaces needed in the left part. This is beyond | |
;; my current elisp/emacs knowledge. | |
(width (- (frame-width) (length right) -12)) | |
(msg (if (car args) (apply 'format-message args) "")) | |
;; Hack: The space for the split is a thin space, not a regular space | |
;; This way, we get rid of the added part if present (unless an actual | |
;; message uses a thin space. | |
(msg (car (split-string msg " "))) | |
(msg (string-trim msg)) | |
(left (truncate-string-to-width msg width nil nil "…")) | |
(full (format (format "%%-%ds %%s" width) left right))) | |
(if (active-minibuffer-window) | |
;; Regular log and display when minibuffer is active | |
(apply orig-fun args) | |
(progn | |
;; Log actual message without echo | |
(if message-log-max | |
(let ((inhibit-message t)) (apply orig-fun (list msg)))) | |
;; Display enhanced message without log | |
(let ((message-truncate-lines t) (message-log-max nil)) | |
(apply orig-fun (list full))) | |
;; Set current message explicitely | |
(setq current-message msg))))) | |
(advice-add 'message :around #'enhanced-message) | |
(add-hook 'post-command-hook (lambda () (let ((message-log-max nil)) | |
(message (current-message))))) | |
Author
rougier
commented
Aug 5, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment