From 71fddcb7d0d6a8eb92a54c7933da1381b1c406ec Mon Sep 17 00:00:00 2001 From: Gregory Heytings Date: Sun, 24 Oct 2021 21:30:24 +0000 Subject: [PATCH] Option to stop Emacs server when last client exits. * lisp/server.el (server-stop-when-closing-last-frame): New custom variable. * lisp/files.el (save-buffers-kill-terminal): Use the new custom variable. * doc/emacs/misc.texi (Emacs Server): Document it. --- doc/emacs/misc.texi | 8 ++++++++ lisp/files.el | 5 ++++- lisp/server.el | 9 +++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/doc/emacs/misc.texi b/doc/emacs/misc.texi index 893e5f1843..d7ec1d6322 100644 --- a/doc/emacs/misc.texi +++ b/doc/emacs/misc.texi @@ -1768,6 +1768,14 @@ Emacs Server emacs --daemon=foo @end example +@vindex server-stop-when-closing-last-frame + When the last client frame is closed, unsaved buffers and running +processes are normally kept in the daemon for later user. However, if +you set @code{server-stop-when-closing-last-frame} is non-@code{nil} and +the last client frame is closed, Emacs asks you whether each unsaved +buffer must be saved and each unfinished process must be killed, and +the daemon itself is killed. + @findex server-stop-automatically If you want to automatically stop the Emacs server when it has no clients, no unsaved buffers and no running processes anymore, put the diff --git a/lisp/files.el b/lisp/files.el index 5a6a33721b..268c017de4 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -7742,7 +7742,10 @@ save-buffers-kill-terminal If emacsclient was started with a list of file names to edit, then only these files will be asked to be saved." (interactive "P") - (if (frame-parameter nil 'client) + (if (and (frame-parameter nil 'client) + (if server-stop-when-closing-last-frame + (cddr (frame-list)) + t)) (server-save-buffers-kill-terminal arg) (save-buffers-kill-emacs arg))) diff --git a/lisp/server.el b/lisp/server.el index 944f1a3dce..239ce3a0a2 100644 --- a/lisp/server.el +++ b/lisp/server.el @@ -272,6 +272,15 @@ server-client-instructions :version "28.1" :type 'boolean) +(defcustom server-stop-when-closing-last-frame nil + "Whether to stop the server when closing the last frame. +If non-nil, call `save-buffers-kill-emacs' when the last client frame is +closed. +If nil, the last client frame is not handled differently, and +`server-save-buffers-kill-terminal' is called when it is closed." + :version "29.1" + :type 'boolean) + ;; We do not use `temporary-file-directory' here, because emacsclient ;; does not read the init file. (defvar server-socket-dir -- 2.33.0