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?

Drew
  • 75,699
  • 9
  • 109
  • 225
Suzanne Soy
  • 163
  • 4
  • 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 09 '16 at 15:43

1 Answers1

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")))))
Jordon Biondo
  • 12,332
  • 2
  • 41
  • 62