unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Jim Porter <jporterbugs@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 58404@debbugs.gnu.org
Subject: bug#58404: 29.0.50; [PATCH] When killing Emacs from the last client, don't warn about the session having clients
Date: Mon, 10 Oct 2022 09:43:55 -0700	[thread overview]
Message-ID: <c488bd2f-ec2e-cec8-db76-59e9f24b644a@gmail.com> (raw)
In-Reply-To: <83tu4cgivn.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 2168 bytes --]

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.

[-- Attachment #2: 0001-Don-t-prompt-when-killing-an-Emacs-client-if-it-s-th.patch --]
[-- Type: text/plain, Size: 2846 bytes --]

From 3f08b120a83b3c06a777823acee604f94fd41210 Mon Sep 17 00:00:00 2001
From: Jim Porter <jporterbugs@gmail.com>
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


  reply	other threads:[~2022-10-10 16:43 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-09 23:32 bug#58404: 29.0.50; [PATCH] When killing Emacs from the last client, don't warn about the session having clients Jim Porter
2022-10-10  6:11 ` Eli Zaretskii
2022-10-10  8:08   ` Jim Porter
2022-10-10  8:53     ` Eli Zaretskii
2022-10-10 16:43       ` Jim Porter [this message]
2022-10-10 16:59         ` Eli Zaretskii
2022-10-10 17:49           ` Jim Porter
2022-10-10 17:57             ` Eli Zaretskii
2022-10-10 23:09               ` Jim Porter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c488bd2f-ec2e-cec8-db76-59e9f24b644a@gmail.com \
    --to=jporterbugs@gmail.com \
    --cc=58404@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).