From 3730a0c55ed416ec007397be312c3223ebe078ad Mon Sep 17 00:00:00 2001 From: Jim Porter Date: Thu, 1 Dec 2022 12:05:40 -0800 Subject: [PATCH 5/7] This is the commit that actually fixes bug#51993 There are two parts to this: 1. For 'nowait' frames, call 'save-some-buffers' and 'delete-frame' when there are other non-daemon frames. 2. For client frames (where the client is a process), don't try to delete all the client's frames directly. Previously, this code would delete all *other* frames associated with the client, and then either a) delete the current frame if there are still more (non-daemon) frames remaining, or b) kill Emacs otherwise. Now, check to see what we should do first: if there are no other clients (or frames not owned by the current client), then we should kill Emacs; otherwise, only delete this client (after saving). * lisp/server.el (server-stop-automatically--handle-kill-terminal): Handle the case when we *don't* kill Emacs, as described above. --- lisp/server.el | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/lisp/server.el b/lisp/server.el index 399bf694fd..9b91fbbbdc 100644 --- a/lisp/server.el +++ b/lisp/server.el @@ -1817,23 +1817,34 @@ server-stop-automatically--handle-kill-terminal (when server-stop-automatically (let ((proc (frame-parameter frame 'client))) (cond ((eq proc 'nowait) - (if (null (cddr (frame-list))) + (if (cddr (frame-list)) + ;; If there are any other non-daemon frames, don't + ;; kill Emacs, but *do* save some buffers, as in + ;; `server-save-buffers-kill-terminal'. + (progn + (save-some-buffers) + (delete-frame)) + ;; If we're the last non-daemon frame standing, kill + ;; Emacs. (save-buffers-kill-emacs))) ((processp proc) - (if (progn - (dolist (f (frame-list)) - (when (and (eq (frame-parameter frame 'client) - (frame-parameter f 'client)) - (not (eq frame f))) - (set-frame-parameter f 'client nil) - (let ((server-stop-automatically nil)) - (delete-frame f)))) - (if (cddr (frame-list)) - (let ((server-stop-automatically nil)) - (delete-frame frame) - nil) - t)) - (save-buffers-kill-emacs))))))) + (if ;; Keep the server alive if... + (or + ;; a) there are any other clients, or... + (length> server-clients 1) + ;; b) there are any frames not owned by this client. + (seq-some + (lambda (frame) + (when-let ((p (frame-parameter frame 'client))) + (not (eq proc p)))) + (frame-list))) + (let ((buffers (process-get proc 'buffers))) + (save-some-buffers + nil (if buffers + ;; Only files from emacsclient file list. + (lambda () (memq (current-buffer) buffers)))) + (server-delete-client)) + (save-buffers-kill-emacs))))))) (defun server-stop-automatically--maybe-kill-emacs () "Handle closing of Emacs daemon when `server-stop-automatically' is used." -- 2.25.1