unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Gregory Heytings <gregory@heytings.org>
To: Jim Porter <jporterbugs@gmail.com>
Cc: 51377@debbugs.gnu.org
Subject: bug#51377: Automatically exit server when it has no remaining clients
Date: Sun, 24 Oct 2021 21:37:48 +0000	[thread overview]
Message-ID: <90ba36dccc0584692d36@heytings.org> (raw)
In-Reply-To: <2fb21ea4-0b95-a76b-8c5f-f8a6c7c1fc9b@gmail.com>

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


>
> I'd find it quite surprising, since an errant edit to any open file 
> would prevent the server from exiting after the last client stops. While 
> leaving the server running isn't the worst thing ever (indeed, that's 
> how it works now), I'd want the logic for when the server exits to be as 
> simple as possible, i.e. "when there are no more clients, exit" 
> (provided the user has confirmed as necessary). Having the server stick 
> around because I forgot to save one file would surprise me, mainly 
> because it would typically happen after, well, I forgot something.
>

I see.  We have different mental models, I guess.  From my viewpoint the 
Emacs server should stay there until it's not necessary, and I'd be 
surprised to be queried about what to do with buffers opened of processes 
started in a frame I already closed when I want to close another frame. 
But of course I do not object to have both behaviors.

>
> That's pretty much what I started with, actually. I've added more to 
> that as I find corner cases, and as I try to make my code work well 
> under different configurations so that it could merge into Emacs if 
> there's interest.
>

I attach a patch for that other behavior, it works fine AFAICS.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: text/x-diff; name=Option-to-stop-Emacs-server-when-last-client-exits.patch, Size: 2776 bytes --]

From 71fddcb7d0d6a8eb92a54c7933da1381b1c406ec Mon Sep 17 00:00:00 2001
From: Gregory Heytings <gregory@heytings.org>
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)))
 \f
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


  reply	other threads:[~2021-10-24 21:37 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-24 15:15 bug#51377: Automatically exit server when it has no remaining clients Gregory Heytings
2021-10-24 16:03 ` Jim Porter
2021-10-24 16:14   ` Jim Porter
2021-10-24 16:32   ` Gregory Heytings
2021-10-24 18:08     ` Jim Porter
2021-10-24 18:43       ` Gregory Heytings
2021-10-24 19:39         ` Jim Porter
2021-10-24 20:42           ` Gregory Heytings
2021-10-24 21:19             ` Jim Porter
2021-10-24 21:37               ` Gregory Heytings [this message]
2021-10-25 18:21                 ` Jim Porter
2021-10-26 10:37                   ` Gregory Heytings
2021-10-26 11:59                     ` Gregory Heytings
2021-10-26 15:07                       ` Gregory Heytings
2021-11-11  5:43                         ` Lars Ingebrigtsen
2021-10-24 21:40           ` 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=90ba36dccc0584692d36@heytings.org \
    --to=gregory@heytings.org \
    --cc=51377@debbugs.gnu.org \
    --cc=jporterbugs@gmail.com \
    /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).