From: Gregory Heytings <gregory@heytings.org>
To: 51377@debbugs.gnu.org
Subject: bug#51377: Automatically exit server when it has no remaining clients
Date: Sun, 24 Oct 2021 15:15:04 +0000 [thread overview]
Message-ID: <90ba36dccc00d6f0d62d@heytings.org> (raw)
[-- Attachment #1: Type: text/plain, Size: 258 bytes --]
As discussed in
https://lists.gnu.org/archive/html/emacs-devel/2021-10/msg01465.html , an
option to stop the Emacs server when it has no clients anymore (which is
similar to what some other apps do, for example browsers) would be useful.
Patch attached.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: text/x-diff; name=Option-to-stop-emacs-server-when-it-has-no-clients-a.patch, Size: 2352 bytes --]
From d3860d6ce9c0d55b416b294cff6be67f734d9b84 Mon Sep 17 00:00:00 2001
From: Gregory Heytings <gregory@heytings.org>
Date: Sun, 24 Oct 2021 15:08:31 +0000
Subject: [PATCH] Option to stop emacs server when it has no clients anymore.
* lisp/server.el (server-stop-when-no-clients): New function.
* doc/emacs/misc.texi (Emacs Server): Document the new function,
and mention that an Emacs server can be started with emacsclient.
---
doc/emacs/misc.texi | 10 ++++++++++
lisp/server.el | 9 +++++++++
2 files changed, 19 insertions(+)
diff --git a/doc/emacs/misc.texi b/doc/emacs/misc.texi
index 810d212021..a68632831a 100644
--- a/doc/emacs/misc.texi
+++ b/doc/emacs/misc.texi
@@ -1697,6 +1697,11 @@ Emacs Server
calls @code{server-start} after initialization and does not open an
initial frame. It then waits for edit requests from clients.
+@item
+Run the command @code{emacsclient} with the @samp{--alternate-editor=""}
+command-line option. This starts an Emacs daemon only if no Emacs daemon
+is already running.
+
@cindex systemd unit file
@item
If your operating system uses @command{systemd} to manage startup,
@@ -1763,6 +1768,11 @@ Emacs Server
emacs --daemon=foo
@end example
+@findex server-stop-when-no-clients
+ If you want to automatically stop the Emacs server when it has no
+clients anymore, put the expression @code{(server-stop-when-no-clients)}
+in your init file (@pxref{Init File}).
+
@findex server-eval-at
If you have defined a server by a unique server name, it is possible
to connect to the server from another Emacs instance and evaluate Lisp
diff --git a/lisp/server.el b/lisp/server.el
index 6359a76199..fb1cb886e1 100644
--- a/lisp/server.el
+++ b/lisp/server.el
@@ -1746,6 +1746,15 @@ server-save-buffers-kill-terminal
(server-delete-client proc)))
(t (error "Invalid client frame")))))
+;;;###autoload
+(defun server-stop-when-no-clients ()
+ "Automatically stop server when it has no remaining clients.
+This function is meant to be put in init files."
+ (when (daemonp)
+ (run-with-timer 10 2 (lambda ()
+ (unless (cdr (frame-list))
+ (save-buffers-kill-emacs))))))
+
(define-key ctl-x-map "#" 'server-edit)
(defun server-unload-function ()
--
2.33.0
next reply other threads:[~2021-10-24 15:15 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-24 15:15 Gregory Heytings [this message]
2021-10-24 16:03 ` bug#51377: Automatically exit server when it has no remaining clients 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
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=90ba36dccc00d6f0d62d@heytings.org \
--to=gregory@heytings.org \
--cc=51377@debbugs.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 external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.