unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Jim Porter <jporterbugs@gmail.com>
To: Gregory Heytings <gregory@heytings.org>, Eli Zaretskii <eliz@gnu.org>
Cc: larsi@gnus.org, 51993@debbugs.gnu.org
Subject: bug#51993: 29.0.50; [PATCH] Killing emacsclient terminal with `server-stop-automatically' doesn't prompt to save files
Date: Mon, 29 Nov 2021 11:31:20 -0800	[thread overview]
Message-ID: <0cbd22ae-e1e9-c6fb-a907-48bcc98dc6f2@gmail.com> (raw)
In-Reply-To: <c4dabc8c0acfb8c1ff3a@heytings.org>

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

On 11/29/2021 5:40 AM, Gregory Heytings wrote:
> 
> I'm in the process of implementing something that should satisfy 
> everyone, but it's not finished yet.

Thanks. Hopefully my proposal helps to explain more precisely my mental 
model and how I'd like things to work. If there's anything that's not 
clear, just let me know and I'll try to elaborate on it.

In case it would help, I've also attached the relevant bits of my Emacs 
configuration, which implements the Explicit Shutdown behavior I've 
described. Overall, it's similar to the first patch I posted in this 
bug, but it's usable in Emacs 27+ (maybe even earlier).

However, I think it has (at least) one problem: if `server-buffer-done' 
(indirectly called by `server-edit' / `C-x #') deletes the last client, 
it doesn't stop the daemon. I haven't had a chance to look into this in 
detail yet though. I think the most consistent behavior would be to stop 
the daemon in this case, since then the logic is simply "stop the daemon 
when the last client would be deleted", and we don't have to worry about 
exceptions to that rule.

[-- Attachment #2: server-advice.el --]
[-- Type: text/plain, Size: 3840 bytes --]

(use-package server
  :defer t
  :preface
  (defun user--server-save-buffers-kill-terminal (arg)
    "Offer to save each buffer, then kill the current client.
With ARG non-nil, silently save all file-visiting buffers, then kill.

If emacsclient was started with a list of filenames to edit, then
only these files will be asked to be saved.

If Emacs was started as a daemon and this is the last client
connected to it, this will call `save-buffers-kill-emacs'."
    (let ((proc (frame-parameter nil 'client)))
      (cond ((eq proc 'nowait)
	     ;; Nowait frames have no client buffer list.
	     (if (if (daemonp) (cddr (frame-list)) (cdr (frame-list)))
                 ;; If there's another (non-daemon) frame, only delete this
                 ;; frame. FIXME: It would be nice to delete any other frames
                 ;; created by this frame (as when killing the terminal of an
                 ;; ordinary client below), but we can't distinguish separate
                 ;; groups of nowait frames currently.
	         (progn (save-some-buffers arg)
		        (delete-frame))
	       ;; If we're the last frame standing, kill Emacs.
	       (save-buffers-kill-emacs arg)))
	    ((processp proc)
             (if (seq-some
                  (lambda (frame)
                    (let ((p (frame-parameter frame 'client)))
                      (unless (and (daemonp) (null p))
                        (not (equal proc p)))))
                  (frame-list))
                 ;; If there's a frame not from this client, only delete the
                 ;; client.
	         (let ((buffers (process-get proc 'buffers)))
	           (save-some-buffers
	            arg (if buffers
                            ;; Only files from emacsclient file list.
		            (lambda () (memq (current-buffer) buffers))
                          ;; No emacsclient file list: don't override
                          ;; `save-some-buffers-default-predicate' (unless
                          ;; ARG is non-nil), since we're not killing
                          ;; Emacs (unlike `save-buffers-kill-emacs').
		          (and arg t)))
	           (server-delete-client proc))
               ;; If all frames are from this client, kill Emacs.
               (save-buffers-kill-emacs arg)))
	    (t (error "Invalid client frame")))))

  (defun user--server-kill-emacs-query-function ()
    "Ask before exiting Emacs if it has live clients.
If Emacs was started as a daemon and the only live client is the
current frame's client, don't bother asking."
    (let ((ignored-proc (and (daemonp) (frame-parameter nil 'client))))
      (or (not (seq-some (lambda (proc)
                           (unless (eq ignored-proc proc)
                             (seq-some #'buffer-live-p
                                       (process-get proc 'buffers))))
                         server-clients))
          (yes-or-no-p "This Emacs session has clients; exit anyway? "))))

  (defun user--handle-delete-frame (frame)
    "When deleting the last non-daemon frame, kill Emacs.
\(To be used from `delete-frame-functions'.)"
    (when (and (daemonp)
               ;; Check that the frame is a client frame.
               ;; Note: `server-delete-client' sets `client' to nil before
               ;; calling `delete-frame', but that's good, since we want to call
               ;; `save-buffers-kill-emacs' before all that anyway.
               (frame-parameter frame 'client)
               (null (cddr (frame-list))))
      (save-buffers-kill-emacs)))

  :config
  (advice-add #'server-save-buffers-kill-terminal :override
              #'user--server-save-buffers-kill-terminal)
  (advice-add #'server-kill-emacs-query-function :override
              #'user--server-kill-emacs-query-function)
  (add-hook 'delete-frame-functions #'user--handle-delete-frame))

  reply	other threads:[~2021-11-29 19:31 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-20  4:29 bug#51993: 29.0.50; [PATCH] Killing emacsclient terminal with `server-stop-automatically' doesn't prompt to save files Jim Porter
2021-11-20  7:13 ` Eli Zaretskii
2021-11-23  9:48   ` Gregory Heytings
2021-11-23 18:25     ` Jim Porter
2021-11-23 20:37       ` Gregory Heytings
2021-11-23 22:08         ` Jim Porter
2021-11-23 22:49           ` Gregory Heytings
2021-11-23 23:42             ` Jim Porter
2021-11-23 23:59               ` Gregory Heytings
2021-11-24  1:10                 ` Jim Porter
2021-11-29  5:39 ` Jim Porter
2021-11-29 12:41   ` Eli Zaretskii
2021-11-29 13:40     ` Gregory Heytings
2021-11-29 19:31       ` Jim Porter [this message]
2022-01-01  0:11         ` Jim Porter
2022-09-09 17:55       ` Lars Ingebrigtsen
2022-09-09 18:04         ` Jim Porter
2022-10-09 22:09           ` Jim Porter
2022-10-10  6:04             ` Eli Zaretskii
2022-10-20  3:14               ` Jim Porter
2022-10-20  6:23                 ` Eli Zaretskii
2022-10-21  5:51                   ` Jim Porter
2022-10-21  6:38                     ` Eli Zaretskii
2022-10-22  3:46                       ` Jim Porter
2022-10-22  6:57                         ` Eli Zaretskii
2022-10-25  3:10                           ` Jim Porter
2022-10-30 22:32                             ` Jim Porter
2022-11-29  5:31                             ` Jim Porter
2022-12-01 17:29                               ` Eli Zaretskii
2022-12-02  1:09                                 ` bug#51993: 29.0.50; [PATCH for 29.1] " Jim Porter
2022-12-02 14:10                                   ` Eli Zaretskii
2022-12-02 21:33                                     ` Jim Porter
2022-12-04 17:56                                       ` Eli Zaretskii
2022-12-04 22:26                                         ` Jim Porter
2022-12-06 22:20                                           ` Jim Porter
2022-12-02  1:42                                 ` bug#51993: 29.0.50; [PATCH explanation] " Jim Porter
2022-12-02 14:31                                   ` Eli Zaretskii
2021-11-29 19:12     ` bug#51993: 29.0.50; [PATCH] " 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=0cbd22ae-e1e9-c6fb-a907-48bcc98dc6f2@gmail.com \
    --to=jporterbugs@gmail.com \
    --cc=51993@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=gregory@heytings.org \
    --cc=larsi@gnus.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).