Subject: bug#58404: 29.0.50; [PATCH] When killing Emacs from the last client, don't warn about the session having clients

From: Jim Porter <[email-masked]>
Date: Mon, 10 Oct 2022 09:43:55 -0700

On 10/10/2022 1:53 AM, Eli Zaretskii wrote:
> It is okay not to prompt when there's no need.  That wasn't the part
> on which I commented, AFAIU.
> 
> You said:
> 
>> 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.
> 
> I'm saying that no, it will not be useful to prompt in that case: if
> the only frames left are non-client frames, we should not prompt,
> because we don't prompt when there are no client frames to begin with.
> Apologies if I misunderstood the use case.

Ah, I see. In that part of my original message, I wanted to say that 
this was a case where I thought the old code made sense, since it 
prompts the user then. However, I also thought that the prompt message 
could be more informative.

Here's why I think prompting then makes sense: when you're in an 
emacsclient frame and there are other non-client frames (i.e. ones 
"owned" by the main Emacs process), that looks very similar to the user 
as when you have a second emacsclient running. In other words, I think 
it would be good for this function to prompt when doing this:

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

   ;; From either frame:
   M-x save-buffers-kill-emacs

Or this:

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

   ;; From either frame:
   M-x save-buffers-kill-emacs

Or this:

   $ emacs foo.txt
   M-x server-start
   $ emacsclient -c bar.txt

   ;; From either frame:
   M-x save-buffers-kill-emacs

In all of those cases, the user would have 2 (visible) frames, possibly 
on different displays/terminals. Since they're visually similar, I think 
it makes sense to have similar prompts.

Next, if the user ran 'save-buffers-kill-emacs' a second time in any of 
those examples, it *wouldn't* prompt, since then all the remaining 
frames are associated with a single emacsclient (or no client at all).

Since the use case for this is non-obvious (it actually took me several 
iterations to identify all the possible cases), I updated my patch with 
clearer (to me) code, plus comments explaining the reasoning.

From 3f08b120a83b3c06a777823acee604f94fd41210 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

Previously, this function prompted whenever there was a live client,
including the current one.  Now, only prompt in one of:

  1. There are live Emacs clients other than the current one, or
  2. The current frame is a client frame and there are also non-client
     frames open.

* lisp/server.el (server-kill-emacs-query-function): Rewrite to avoid
prompting as above (bug#58404).
---
 lisp/server.el | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)

diff --git a/lisp/server.el b/lisp/server.el
index 3caa335c4e..f74f26210a 100644
--- a/lisp/server.el
+++ b/lisp/server.el
@@ -1589,14 +1589,34 @@ 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)))
+    (cond
+     ;; Check for Emacs clients, excluding the current one (if any).
+     ;; If there are any other clients, prompt the user before killing
+     ;; Emacs.  Other clients might be on different (possibly remote)
+     ;; displays, so it can be hard for users to tell on their own.
+     ((seq-some (lambda (proc)
+                  (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? "))
+     ;; If the user is in a client frame, prompt if there are any
+     ;; frames created by the main Emacs process.  Like in the case
+     ;; above, those frames might be on different displays.
+     ((and (processp this-client)
+           ;; Don't count 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? "))
+     ;; Otherwise, we can kill Emacs without prompting.
+     (t t))))
 
 (defun server-kill-buffer ()
   "Remove the current buffer from its clients' buffer list.
-- 
2.25.1