Subject: Re: bug#76870: 31.0.50; server-start with non-nil arg LEAVE-DEAD can write confusing warnings

From: Jens Schmidt <[email-masked]>
Date: Sun, 9 Mar 2025 20:46:27 +0100

On 2025-03-09  11:39, Eli Zaretskii wrote:

>> If yes, then I find it confusing to read that Emacs could not
>> *start* the server in the resulting warning.
> 
> So you want to have an alternative message about stopping only?  That
> could be a good idea, yes.

Exactly.  The attached patch a) stops the server in a silent way from
function `server-unload-function' and b) extends function `server-start'
to issue the following messages if an external server is running:

M-x server-start RET

  [message unchanged compared to master]

  ⛔ Warning (server): Unable to start the Emacs server.
  There is an existing Emacs server, named "server"
  To start the server in this Emacs process, stop the existing server or call M-x server-force-delete to forcibly disconnect it.

C-u M-x server-start RET

  ⛔ Warning (server): Unable to stop an external Emacs server.
  There is an existing Emacs server, named "server"
  Stop it either in its Emacs session or call M-x server-force-delete to forcibly disconnect it.

Please let me know what you think.

Thanks
From 683160c56dd2e6f3c25ac342bbba660567e2883e Mon Sep 17 00:00:00 2001
From: Jens Schmidt <[email-masked]>
Date: Sat, 8 Mar 2025 18:13:54 +0100
Subject: [PATCH] Improve message handling on server stop

* lisp/server.el (server-start): Use a different error message if one
attempts to only stop (and not restart) an external server.
(server-unload-function): Silently stop the server if unloading its
library.  (Bug#76870)
---
 lisp/server.el | 34 +++++++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 11 deletions(-)

diff --git a/lisp/server.el b/lisp/server.el
index a49787e3498..2708436c0a2 100644
--- a/lisp/server.el
+++ b/lisp/server.el
@@ -725,16 +725,28 @@ server-start
           (when (server-stop)
             (message (if leave-dead "Stopped server" "Restarting server"))))
       (server-running-external
-       (display-warning
-        'server
-        (concat "Unable to start the Emacs server.\n"
-                (cadr err)
-                (substitute-command-keys
-                 (concat "\nTo start the server in this Emacs process, stop "
-                         "the existing server or call \\[server-force-delete] "
-                         "to forcibly disconnect it.")))
-        :warning)
-       (setq leave-dead t)))
+       (cond
+        ((not leave-dead)
+         (display-warning
+          'server
+          (concat "Unable to start the Emacs server.\n"
+                  (cadr err)
+                  (substitute-command-keys
+                   (concat "\nTo start the server in this Emacs process, stop "
+                           "the existing server or call \\[server-force-delete] "
+                           "to forcibly disconnect it.")))
+          :warning)
+         (setq leave-dead t))
+        (t
+         (display-warning
+          'server
+          (concat "Unable to stop an external Emacs server.\n"
+                  (cadr err)
+                  (substitute-command-keys
+                   (concat "\nStop it either in its Emacs session "
+                           "or call \\[server-force-delete] "
+                           "to forcibly disconnect it.")))
+          :warning)))))
       ;; Now any previous server is properly stopped.
     (unless leave-dead
       (let ((server-file (server--file-name)))
@@ -2016,7 +2028,7 @@ ctl-x-map
 
 (defun server-unload-function ()
   "Unload the Server library."
-  (server-mode -1)
+  (ignore-errors (server-stop 'noframe))
   (substitute-key-definition 'server-edit nil ctl-x-map)
   (save-current-buffer
     (dolist (buffer (buffer-list))
-- 
2.39.5