Skip to content

Instantly share code, notes, and snippets.

@rougier
Last active October 21, 2020 18:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rougier/baaf4ff6e0461680e3f070c5c32b64a2 to your computer and use it in GitHub Desktop.
Save rougier/baaf4ff6e0461680e3f070c5c32b64a2 to your computer and use it in GitHub Desktop.
Emacs: persistent message in the echo area
(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)))))
@rougier
Copy link
Author

rougier commented Aug 5, 2020

Screenshot 2020-08-05 at 22 55 01

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment