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: Tue, 26 Oct 2021 11:59:25 +0000	[thread overview]
Message-ID: <664bd93fdd4886b48d45@heytings.org> (raw)
In-Reply-To: <664bd93fdd10b4061070@heytings.org>

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


>
> Here's a combined patch, which implements the two desired behaviors, and 
> which I believe handles all cases properly: delete-frame, 
> handele-delete-frame, save-buffers-kill-terminal, wait and nowait 
> clients.
>

It just occurred to me that it's very easy to add a third behavior, namely 
the one you expect, but only when the last frame is killed with C-x C-c. 
See attached patch.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: text/x-diff; name=Options-to-automatically-stop-the-Emacs-server.patch, Size: 7866 bytes --]

From 2ffedbeff97e33dc9c504b9877272fa5b73707d6 Mon Sep 17 00:00:00 2001
From: Gregory Heytings <gregory@heytings.org>
Date: Tue, 26 Oct 2021 11:54:27 +0000
Subject: [PATCH] Options to automatically stop the Emacs server.

* lisp/server.el (server-stop-automatically): New function.
(server-stop-automatically): New auxiliary variable.
(server-stop-automatically--maybe-kill-emacs,
server-stop-automatically--handle-delete-frame): New auxiliary
functions.
(server-save-buffers-kill-terminal): Call the new auxiliary
function when necessary.
* 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 |  20 ++++++++
 lisp/server.el      | 116 ++++++++++++++++++++++++++++++++++++--------
 2 files changed, 115 insertions(+), 21 deletions(-)

diff --git a/doc/emacs/misc.texi b/doc/emacs/misc.texi
index 5123a716dc..b8891ed8c3 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,21 @@ 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 file-visiting buffers and no running processes
+anymore, put the expression @code{(server-stop-automatically nil)} in
+your init file (@pxref{Init File}).
+
+  If you want to be asked whether each unsaved file-visiting buffer
+must be saved and each unfinished process can be stopped when the last
+client frame is being closed, and if so, to stop the Emacs server, put
+the expression @code{(server-stop-automatically t)} in your init file
+(@pxref{Init File}).  If you want this to happen only when the last
+client frame is being closed with @kbd{C-x C-c}
+(@code{save-buffers-kill-terminal}), put the expression
+@code{(server-stop-automatically 'kill-terminal)} in your 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..a81913fe98 100644
--- a/lisp/server.el
+++ b/lisp/server.el
@@ -1716,6 +1716,9 @@ server-switch-buffer
     (when server-raise-frame
       (select-frame-set-input-focus (window-frame)))))
 
+(defvar server-stop-automatically nil
+  "Internal status variable for `server-stop-automatically'.")
+
 ;;;###autoload
 (defun server-save-buffers-kill-terminal (arg)
   ;; Called from save-buffers-kill-terminal in files.el.
@@ -1724,27 +1727,98 @@ server-save-buffers-kill-terminal
 
 If emacsclient was started with a list of filenames to edit, then
 only these files will be asked to be saved."
-  (let ((proc (frame-parameter nil 'client)))
-    (cond ((eq proc 'nowait)
-	   ;; Nowait frames have no client buffer list.
-	   (if (cdr (frame-list))
-	       (progn (save-some-buffers arg)
-		      (delete-frame))
-	     ;; If we're the last frame standing, kill Emacs.
-	     (save-buffers-kill-emacs arg)))
-	  ((processp proc)
-	   (let ((buffers (process-get proc 'buffers)))
-	     (save-some-buffers
-	      arg (if buffers
-                      ;; Only files from emacsclient file list.
-		      (lambda () (memq (current-buffer) buffers))
-                    ;; No emacsclient file list: don't override
-                    ;; `save-some-buffers-default-predicate' (unless
-                    ;; ARG is non-nil), since we're not killing
-                    ;; Emacs (unlike `save-buffers-kill-emacs').
-		    (and arg t)))
-	     (server-delete-client proc)))
-	  (t (error "Invalid client frame")))))
+  (if server-stop-automatically
+      (server-stop-automatically--handle-delete-frame (selected-frame))
+    (let ((proc (frame-parameter nil 'client)))
+      (cond ((eq proc 'nowait)
+	     ;; Nowait frames have no client buffer list.
+	     (if (cdr (frame-list))
+	         (progn (save-some-buffers arg)
+		        (delete-frame))
+	       ;; If we're the last frame standing, kill Emacs.
+	       (save-buffers-kill-emacs arg)))
+	    ((processp proc)
+	     (let ((buffers (process-get proc 'buffers)))
+	       (save-some-buffers
+	        arg (if buffers
+                        ;; Only files from emacsclient file list.
+		        (lambda () (memq (current-buffer) buffers))
+                      ;; No emacsclient file list: don't override
+                      ;; `save-some-buffers-default-predicate' (unless
+                      ;; ARG is non-nil), since we're not killing
+                      ;; Emacs (unlike `save-buffers-kill-emacs').
+		      (and arg t)))
+	       (server-delete-client proc)))
+	    (t (error "Invalid client frame"))))))
+
+(defun server-stop-automatically--handle-delete-frame (frame)
+  "Handle deletion of a frame when `server-stop-automatically' is t."
+  (when server-stop-automatically
+    (if (if (and (processp (frame-parameter frame 'client))
+		 (eq this-command 'save-buffers-kill-terminal))
+	    (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))
+	  (null (cddr (frame-list))))
+	(let ((server-stop-automatically nil))
+	  (save-buffers-kill-emacs)
+	  (delete-frame frame)))))
+
+(defun server-stop-automatically--maybe-kill-emacs ()
+  "Handle closing of Emacs daemon when `server-stop-automatically' is nil."
+  (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))))
+
+;;;###autoload
+(defun server-stop-automatically (arg)
+  "Automatically stop server when possible.
+
+When ARG is nil, the server is stopped when it has no remaining
+clients, no remaining unsaved file-visiting buffers, and no
+running processes with a query-on-exit flag.
+
+When ARG is t, the user is asked when the last frame is being
+closed whether each unsaved file-visiting buffer must be saved
+and each running process with a query-on-exit flag must be
+killed, and if so, the server itself is stopped.
+
+When ARG is any other non-nil value, the user is asked when the
+last frame is being close with \\[save-buffers-kill-terminal] \
+whether each unsaved
+file-visiting buffer must be saved and each running process with
+a query-on-exit flag must be killed, and if so, the server itself
+is stopped.
+
+This function is meant to be put in init files."
+  (when (daemonp)
+    (setq server-stop-automatically arg)
+    (cond
+     ((eq arg t)
+      (add-hook 'delete-frame-functions
+		#'server-stop-automatically--handle-delete-frame))
+     ((eq arg nil)
+      (run-with-timer 10 2
+		      #'server-stop-automatically--maybe-kill-emacs)))))
 
 (define-key ctl-x-map "#" 'server-edit)
 
-- 
2.33.0


  reply	other threads:[~2021-10-26 11:59 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
2021-10-25 18:21                 ` Jim Porter
2021-10-26 10:37                   ` Gregory Heytings
2021-10-26 11:59                     ` Gregory Heytings [this message]
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=664bd93fdd4886b48d45@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).