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 18:43:06 +0000 [thread overview]
Message-ID: <90ba36dcccdc40168c93@heytings.org> (raw)
In-Reply-To: <7f3dde25-f81b-d7db-efc5-f8d471fd06bc@gmail.com>
[-- Attachment #1: Type: text/plain, Size: 221 bytes --]
>
> If `save-buffers-kill-emacs' were called after that, the Emacs daemon
> would be killed, losing the edits to bar.txt.
>
Indeed, you are correct. Updated patch attached, which also takes care of
running processes.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: text/x-diff; name=Option-to-automatically-stop-Emacs-server.patch, Size: 2983 bytes --]
From b699c16bac38923e43992edf27039a5d9bcca4f1 Mon Sep 17 00:00:00 2001
From: Gregory Heytings <gregory@heytings.org>
Date: Sun, 24 Oct 2021 18:40:39 +0000
Subject: [PATCH] Option to automatically stop Emacs server.
* lisp/server.el (server-stop-automatically): New function.
* doc/emacs/misc.texi (Emacs Server): Document the new function.
Also mention that an Emacs server can be started with emacsclient.
---
doc/emacs/misc.texi | 11 +++++++++++
lisp/server.el | 23 +++++++++++++++++++++++
2 files changed, 34 insertions(+)
diff --git a/doc/emacs/misc.texi b/doc/emacs/misc.texi
index 5123a716dc..893e5f1843 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,12 @@ Emacs Server
emacs --daemon=foo
@end example
+@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
+expression @code{(server-stop-automatically)} 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..944f1a3dce 100644
--- a/lisp/server.el
+++ b/lisp/server.el
@@ -1746,6 +1746,29 @@ server-save-buffers-kill-terminal
(server-delete-client proc)))
(t (error "Invalid client frame")))))
+;;;###autoload
+(defun server-stop-automatically ()
+ "Automatically stop server when possible.
+The server is stopped when it has no remaining clients, no remaining
+unsaved buffers, and no running processes with a query-on-exit flag.
+This function is meant to be put in init files."
+ (when (daemonp)
+ (run-with-timer
+ 10 2
+ (lambda ()
+ (unless (cdr (frame-list))
+ (when (and
+ (not (memq t (mapcar (lambda (b)
+ (and (buffer-file-name b)
+ (buffer-modified-p b)))
+ (buffer-list))))
+ (not (memq t (mapcar (lambda (p)
+ (and (memq (process-status p)
+ '(run stop open listen))
+ (process-query-on-exit-flag p)))
+ (process-list)))))
+ (kill-emacs)))))))
+
(define-key ctl-x-map "#" 'server-edit)
(defun server-unload-function ()
--
2.33.0
next prev parent reply other threads:[~2021-10-24 18:43 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 [this message]
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=90ba36dcccdc40168c93@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 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.