Summary:
To suppress messages in the code you write:
- use
with-temp-message
.
- problem of let-binding
inhibit-message
to nil: current message is still cleared.
- let-bind
message-log-max
to nil if you want to disable message logging in
*Messages*
.
(You can write a wrapper as suggested in @Haruyasu Ueda's answer)
To suppress messages from any single function:
(defun suppress-message-advice-around (fun &rest args)
(let (message-log-max)
(with-temp-message (or (current-message) "")
(apply fun args))))
;; example: suppress any messages from `save-buffer'
(advice-add 'save-buffer :around 'suppress-message-advice-around)
;; undo:
(advice-remove 'save-buffer 'suppress-message-advice-around)
(If you want to know where does a message come from: use
debug-on-message.)
the original answer
Just clear the minibuffer afterward.
Don't need to suppress, just clear the minibuffer afterward, if you just don't want to see the message showing in minibuffer.
(defun clear-message ()
(message nil))
(advice-add 'real-auto-save-buffers :after 'clear-message)
;; replace `real-auto-save-buffers' with the function whose messages
;; should not show in the minibuffer.
From the manual of message
:
If the first argument is nil or the empty string, the function clears
any existing message;
Basically, what (message nil)
does is clearing the current message from the echo area, and it doesn't affect the messages in *Messages*
. It works out well when you don't want to see the annoying messages keep showing in the minibuffer.
And, a good thing is, when you are editing the minibuffer (e.g M-x
or find-file
) and a message shows up, normally you need to type something to make the message disappear. If (message nil)
is called, the message will disappear immediately and you can see the original content of the minibufer.
In general, if you don't want to see a particular function showing messages in the minibuffer, you don't need to suppress the messages when it is called, just call (message nil)
afterward to clear the current message in minibuffer.
if you want to suppress the messages
Replacing the message
function temporarily is much more straight forward:
(defmacro no-message (&rest body)
"Eval BODY, with `message' doing nothing."
`(cl-letf (((symbol-function 'message)
(lambda (&rest args)
nil)))
(progn ,@body)))
(no-message
(message "meaningless") ; do nothing
(print [1 2 3]) ; not affected
(save-buffer)) ; won't display messages in minibuffer
(let (message-log-max) (save-buffer))
to disable all messages duringsave-buffer
.