2017-05-01 show-some-last-messages

I am pretty sure I am not the only one annoyed by the fact that I can’t see more than one message in the echo area at a time. I have some functions I put in some hooks which display certain messages, and of course only the last one is usually visible. Of course, I could switch to the *Messages* buffer (using C-h e, for instance), but then I’d have to press C-x 1 to delete its window. And what if I have more than one window and I press C-h e? Not fun. I’m pretty sure its configurable to some extent, but I wanted something different. I wanted to have something like momentary-string-display. I didn’t, however, like the fact that it mangled the text in the buffer I’m in, even if only temporarily. Well, actually the best thing to use would be… message.

Of course, everything message displays tends to be added to *Messages*. Preventing that is tricky. You can’t use undo, since it is disabled in that buffer. You can’t temporarily enable read-only-mode, because it’s already on and message seems to override it with something like inhibit-read-only (at least this is my guess, since message is written in C and I can’t really read it well).

What I decided to do is just to use message to display a few lines grabbed from *Messages* and then delete what I had displayed with plain old delete-region. Here’s the code.

(defcustom default-messages-to-show 4
  "Default number of messages for `show-some-last-messages'.")

(defun show-some-last-messages (count)
  "Show COUNT last lines of the `*Messages*' buffer."
  (interactive "P")
  (setq count (if count (prefix-numeric-value count)
	(set-buffer "*Messages*")
	(let ((prev-point-max (point-max-marker))
	  (inhibit-read-only t))
	  (message "%s"
		 (set-buffer "*Messages*")
			(goto-char (point-max))
			(unless (bolp)
			  (insert "\n"))
			(forward-line (- count))
	  (delete-region (point-max) prev-point-max))))

One tricky thing is the (insert "\n") stuff. Apparently the *Messages* sometimes do not have a newline at the end. If you have some knowledge about this phenomenon, please share it in the comments.

CategoryEnglish, CategoryBlog, CategoryEmacs