Subject: Set message functions

From: Juri Linkov <[email-masked]>
Date: Sun, 05 Dec 2021 21:07:42 +0200

Version: 29.0.50
Severity: wishlist
Tags: patch

To address several requests, there is a patch for Emacs 29 that supports:

1. inhibiting messages selectively like discussed in bug#42865, bug#44629;

2. multi-line accumulated messages like discussed on emacs-devel
   under subject "Intelligent stacking of messages in the echo area";

3. combining all them plus minibuffer messages into a pipeline:
   first inhibit-message can filter out some messages, then the second
   function can accumulate 10 old messages into a multi-line message,
   then the third function will display them in the active minibuffer.

By default, 'set-message-functions' will be '(set-minibuffer-message)'
with the current behavior, but can be customized to
'(inhibit-message set-multi-message set-minibuffer-message)'
to implement the hook-like list of functions described above:

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index 0a5fb72774..3eadae88db 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -852,7 +852,51 @@ set-minibuffer-message
         ;; was handled specially by this function.
         t))))
 
-(setq set-message-function 'set-minibuffer-message)
+(setq set-message-function 'set-message-functions)
+
+(defcustom set-message-functions '(set-minibuffer-message)
+  "List of functions to handle display of echo-area messages.
+Each function is called with one argument that is the text of a message.
+If a function returns nil, a previous message string is given to the
+next function in the list, and if the last function returns nil, the
+last message string is displayed in the echo area.
+If a function returns a string, the returned string is given to the
+next function in the list, and if the last function returns a string,
+it's displayed in the echo area.
+If a function returns any other non-nil value, no more functions are
+called from the list, and no message will be displayed in the echo area."
+  :type '(choice (const :tag "No special message handling" nil)
+                 (repeat
+                  (choice (function-item :tag "Inhibit some messages"
+                                         inhibit-message)
+                          (function-item :tag "Accumulate messages"
+                                         set-multi-message)
+                          (function-item :tag "Handle minibuffer"
+                                         set-minibuffer-message)
+                          (function :tag "Custom function"))))
+  :version "29.1")
+
+(defun set-message-functions (message)
+  (run-hook-wrapped 'set-message-functions
+                    (lambda (fun)
+                      (when (stringp message)
+                        (let ((ret (funcall fun message)))
+                          (when ret (setq message ret))))
+                      nil))
+  message)
+
+(defcustom inhibit-message-regexp nil
+  "Regexp to inhibit messages by the function `inhibit-message'."
+  :type '(choice (const :tag "Don't inhibit messages" nil)
+                 (regexp :tag "Inhibit messages that match regexp"))
+  :version "29.1")
+
+(defun inhibit-message (message)
+  "Don't display MESSAGE when it matches the regexp `inhibit-message-regexp'.
+This function is intended to be added to `set-message-functions'."
+  (or (and (stringp inhibit-message-regexp)
+           (string-match-p inhibit-message-regexp message))
+      message))
 
 (defun clear-minibuffer-message ()
   "Clear minibuffer message.