Subject: 29.0.50; [PATCH] When killing Emacs from the last client, don't warn about the session having clients
From: Jim Porter <[email-masked]>
To: [email-masked]
Date: Sun, 9 Oct 2022 16:32:28 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

To reproduce:

   $ emacs -Q --daemon
   $ emacsclient foo.txt

   M-x save-buffers-kill-emacs
   => This Emacs session has clients; exit anyway?

I think that's unnecessary. Since we're in the last (only) client, we 
can't accidentally kill other clients that we don't see right now (e.g. 
ones in an SSH connection); they don't exist! Couldn't we just proceed 
ahead without the prompt?

On the other hand, I think it *would* be useful to prompt if you're in 
the last client, but there are other non-client frames. This can happen 
if you start the main Emacs process without --daemon or if you use 
--no-wait. For example:

   $ emacs -Q --daemon
   $ emacsclient foo.txt
   $ emacsclient --no-wait -c bar.txt

   ;; From the first client:
   M-x save-buffers-kill-emacs
   => This Emacs session has clients; exit anyway?

This is ok, except the prompt could be clearer. The real issue is that 
the session has non-client frames that would get killed.

Attached is a patch to do this.

From 2fb6c6da99b84c250e59409d4dc0adbdbe704fed Mon Sep 17 00:00:00 2001
From: Jim Porter <[email-masked]>
Date: Sun, 9 Oct 2022 15:53:27 -0700
Subject: [PATCH] Don't prompt when killing an Emacs client if it's the last
 client

* lisp/server.el (server-kill-emacs-query-function): Ignore the
current client, if any.  Prompt if there are non-client frames.
---
 lisp/server.el | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/lisp/server.el b/lisp/server.el
index 3caa335c4e..9ebdabbf87 100644
--- a/lisp/server.el
+++ b/lisp/server.el
@@ -1589,14 +1589,28 @@ server-done
     (server-buffer-done (current-buffer))))
 
 (defun server-kill-emacs-query-function ()
-  "Ask before exiting Emacs if it has live clients.
+  "Ask before exiting Emacs if it has other live clients or non-client frames.
 A \"live client\" is a client with at least one live buffer
 associated with it."
-  (or (not (seq-some (lambda (proc)
-                       (seq-some #'buffer-live-p
-                                 (process-get proc 'buffers)))
-                     server-clients))
-      (yes-or-no-p "This Emacs session has clients; exit anyway? ")))
+  (let ((this-client (frame-parameter nil 'client)))
+    (if (seq-some (lambda (proc)
+                    ;; Ignore the current client when checking for
+                    ;; live clients.
+                    (unless (eq proc this-client)
+                      (seq-some #'buffer-live-p
+                                (process-get proc 'buffers))))
+                  server-clients)
+        (yes-or-no-p "This Emacs session has other clients; exit anyway? ")
+      (or (not (processp this-client))
+          ;; Check if there are any non-client frames, ignoring the
+          ;; initial frame when in daemon mode.
+          (>= (if (daemonp) 1 0)
+              (seq-count
+               (lambda (frame)
+                 (not (processp (frame-parameter frame 'client))))
+               (frame-list)))
+          (yes-or-no-p
+           "This Emacs session has non-client frames; exit anyway? ")))))
 
 (defun server-kill-buffer ()
   "Remove the current buffer from its clients' buffer list.
-- 
2.25.1