Subject: bug#37826: Very annoying autoraise client/server behavior with -t option

From: Carlos Pita <[email-masked]>
Date: Mon, 21 Oct 2019 17:14:32 -0300

> 1. Perhaps I'd have preferred to advice only around
> find-file-noselect, but this would probably require using a global
> variable to pass delayed messages from server-visit-files to
> server-execute. Thus I wrapped the entire server-visit-files call and
> keep the messages local to server-execute.

Regarding this point I've changed my mind after more carefully
inspecting server-visit-files. There are many hooks involved that
might run arbitrary code and therefore show messages. We're pretty
confident that any code requiring interaction will work anyway and all
delayed messages are going to be shown afterwards, so even
disregarding the global variable argument above, I still prefer the
version wrapping the entire call to server-visit-files to a more
selective one.

> 2. Also I'd have liked that the mock message function returned the
> formatted message (since it's part of the interface) but there are
> some corner cases (nil, non-string, empty first parameter) that force
> me to replicate much of the actual implementation (Fmessage), which I
> dislike.

I've addressed this point in the less sketchy patch I'm attaching, it
was simpler than I'd though, I believe I'm covering all input cases.

If you prefer to do minor reviews online, this is the commit in my
fork of your mirror
https://github.com/memeplex/emacs/commit/9d53e50848d9d8be758a21d0b5e078f82af25754.
I could create a PR also, just for the sake of reviewing, but it seems
like individual commits are also commentable (until my next forced
push deletes all comments, at least :).

Btw, here is another noisy case: when visiting a new file you get a
"(New file)" message.
From 9d53e50848d9d8be758a21d0b5e078f82af25754 Mon Sep 17 00:00:00 2001
From: memeplex <[email-masked]>
Date: Mon, 21 Oct 2019 17:04:38 -0300
Subject: [PATCH] Avoid auto-raising minibufer before new frame is created
 (Bug#37826)

* lisp/server.el (server-execute): Advice `message' to delay output
until the new frame is created.
---
 lisp/server.el | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/lisp/server.el b/lisp/server.el
index 45fa55ad6b..6fab0ef747 100644
--- a/lisp/server.el
+++ b/lisp/server.el
@@ -1304,7 +1304,16 @@ server-execute
   ;; including code that needs to wait.
   (with-local-quit
     (condition-case err
-        (let* ((buffers (server-visit-files files proc nowait))
+        (let* (;; Delay messages to avoid auto raising frame (Bug#37826).
+               (messages nil)
+               (delay (lambda (_ fmt &rest args)
+                        (let ((msg (and fmt (apply #'format-message fmt args))))
+                          (car (push msg messages)))))
+               (buffers (unwind-protect
+                            (progn
+                              (advice-add #'message :around delay)
+                              (server-visit-files files proc nowait))
+                          (advice-remove #'message delay)))
                ;; If we were told only to open a new client, obey
                ;; `initial-buffer-choice' if it specifies a file
                ;; or a function.
@@ -1325,6 +1334,10 @@ server-execute
                           ;; Switch to initial buffer in case the frame was reused.
                           (when initial-buffer
                             (switch-to-buffer initial-buffer 'norecord))))))
+          ;; Show all delayed messages in the new frame (if any).
+          (with-selected-frame (or frame (selected-frame))
+            (dolist (msg (nreverse messages))
+              (message msg)))
 
           (mapc #'funcall (nreverse commands))
 
-- 
2.20.1