6

When running (C-x C-e) the following snippet and doing M-x mes, I see text written in red in the minibuffer area, but the text in the *Messages* buffer is stripped of all formatting.

(defun mes ()
  "message me"
  (interactive)
  (message (propertize "text" 'face 'font-lock-warning-face)))

How can I preserve the color and other font information in the *Messages* buffer?

  • The color you see is actually in the echo area, not the *Messages* buffer -- so there is nothing to preserve. You'd need an entirely different approach to place overlays or text properties in the *Messages* buffer. – lawlist Feb 9 '16 at 15:43
6

You can't with message. Having looked at the internals of message, only the raw char* is given to the function that actually logs the text, all text properties are lost.

You can hack your way around this by writing a function that:

  • calls message with message-log-max let-bound to nil so that it won't log to *Messages*

  • manually inserts the propertized string at the end of the messages buffer by let-binding inhibit-read-only to t in the message buffer.

Here is an implementation:

(defun my-message (format &rest args)
  "Acts like `message' but preserves string properties in the *Messages* buffer."
  (let ((message-log-max nil))
    (apply 'message format args))
  (with-current-buffer (get-buffer "*Messages*")
    (save-excursion
      (goto-char (point-max))
      (let ((inhibit-read-only t))
        (unless (zerop (current-column)) (insert "\n"))
        (insert (apply 'format format args))
        (insert "\n")))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.