From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jim Porter Newsgroups: gmane.emacs.bugs Subject: bug#51377: Automatically exit server when it has no remaining clients Date: Sun, 24 Oct 2021 14:40:18 -0700 Message-ID: <42ff964e-731c-6309-e3e8-e27b14688730@gmail.com> References: <90ba36dccc00d6f0d62d@heytings.org> <63203d9f-dae7-a39f-c70f-ebf37632e642@gmail.com> <90ba36dcccc2abc8dec1@heytings.org> <7f3dde25-f81b-d7db-efc5-f8d471fd06bc@gmail.com> <90ba36dcccdc40168c93@heytings.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------6EEDACE8E824739EB7D3680E" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="10056"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 51377@debbugs.gnu.org To: Gregory Heytings Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Oct 24 23:41:33 2021 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1melFE-0002Qn-PL for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 24 Oct 2021 23:41:32 +0200 Original-Received: from localhost ([::1]:53632 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1melFD-0001c7-Jq for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 24 Oct 2021 17:41:31 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:60154) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1melEk-0001XU-BD for bug-gnu-emacs@gnu.org; Sun, 24 Oct 2021 17:41:06 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:57814) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1melEk-000844-1o for bug-gnu-emacs@gnu.org; Sun, 24 Oct 2021 17:41:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1melEk-0005jv-0d for bug-gnu-emacs@gnu.org; Sun, 24 Oct 2021 17:41:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Jim Porter Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 24 Oct 2021 21:41:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 51377 X-GNU-PR-Package: emacs Original-Received: via spool by 51377-submit@debbugs.gnu.org id=B51377.163511162822016 (code B ref 51377); Sun, 24 Oct 2021 21:41:01 +0000 Original-Received: (at 51377) by debbugs.gnu.org; 24 Oct 2021 21:40:28 +0000 Original-Received: from localhost ([127.0.0.1]:41127 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1melEB-0005j1-Ol for submit@debbugs.gnu.org; Sun, 24 Oct 2021 17:40:28 -0400 Original-Received: from mail-pf1-f177.google.com ([209.85.210.177]:37657) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1melE9-0005io-5p for 51377@debbugs.gnu.org; Sun, 24 Oct 2021 17:40:26 -0400 Original-Received: by mail-pf1-f177.google.com with SMTP id v193so2184912pfc.4 for <51377@debbugs.gnu.org>; Sun, 24 Oct 2021 14:40:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=subject:from:to:cc:references:message-id:date:mime-version :in-reply-to:content-language; bh=NWOHxQ2p+PNrWkzArlSDBXmJiyovY4CxdKDM8wSWUdk=; b=iG0W4aDTesndsLfxgSrMH5EbiQD0biqNhgnhOGk+tzI6JrSxR6m8A6i5f+q+QPHRiY e5jECc+snfswqU+UC3mgm0OEPmA7Qfk2f90aErlz7RUqHq9cCNOcifTN2mJUZdH1VZXr 4UYcR4bDB7OO/tltvVnKslzpfZ3w7A2z/lveHAmyi9v6R5yX0xvuHrYqqBqEdrQmUQi2 /Hiokyd7aRt25LsntOCXLwgz9f3ZdwFS0Ceh9SbO5b0FX2IrIGTUhgG6qxEFlQ550avT TqSUPSPuRGkhDgsV9ugZ2IlVrQ03/VkZI8sApl5TyehOEDkriiFt9hYGUUSqdKM3k2Jc d8hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:from:to:cc:references:message-id:date :mime-version:in-reply-to:content-language; bh=NWOHxQ2p+PNrWkzArlSDBXmJiyovY4CxdKDM8wSWUdk=; b=ss3yYf/rSPMYYNXA/lGyHYTLrBnNhx9ac+LWCjTyCE0u0ZFgvsqDfSu/FTnMSS1FKB EIiIYbcvP2phUSYxnRto8ph/RG25QQ5wNcgjaxtGNMqc9PhI9qENXH3xTiaGdxQ/twPm CigeqtoGdEDRPT753JVSenYpWgBh8WvWcCkSDK/oaveIyM/7S9le9lyahZAH2kYJ995g NPJLkzTKtVddBHzEt2ErSgd/WsqyaYcqYXpg2QMrRAKs9dUDnTdxfZOB0kgLOz5X8tqT w1NMU+2x+7cIS4zrIJczCD0PrePMzhHkm3D3LwcxnJK8sC7IeY55V/C2Qaan6Pimbvfn T/ig== X-Gm-Message-State: AOAM531blise5T4ln61tMR7yB4CmlMGizGmO90HDQuQCGC3gOSWAQhEQ dpKwiornejR+wPeRHk3hItPPGJesz18= X-Google-Smtp-Source: ABdhPJywHaQT5fG4FgSfRPOVEzCWA0iIJiL7e6xfuT2OLRZmcWWjpwnE7UoU1DpimMWl4fc6znqy1g== X-Received: by 2002:a63:924c:: with SMTP id s12mr10732143pgn.416.1635111619182; Sun, 24 Oct 2021 14:40:19 -0700 (PDT) Original-Received: from [192.168.1.2] (cpe-76-168-148-233.socal.res.rr.com. [76.168.148.233]) by smtp.googlemail.com with ESMTPSA id x40sm360190pfh.188.2021.10.24.14.40.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 24 Oct 2021 14:40:18 -0700 (PDT) In-Reply-To: Content-Language: en-US X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:218212 Archived-At: This is a multi-part message in MIME format. --------------6EEDACE8E824739EB7D3680E Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit On 10/24/2021 12:39 PM, Jim Porter wrote: > Next, I updated `server-save-buffers-kill-terminal' to check if the > server is a lazy daemon and if the current client is the last one: if > so, it calls `save-buffers-kill-emacs', which then calls an improved > `server-kill-emacs-query-function' that knows to avoid prompting > unnecessarily when this is the last client. I amended the third patch (attached) to account for this change in `handle-delete-frame'. This ensures that if you delete the last (non-daemon) frame using this method, it calls `save-buffers-kill-emacs' to ask the relevant questions. --------------6EEDACE8E824739EB7D3680E Content-Type: text/plain; charset=UTF-8; name="0003-When-killing-the-last-client-attached-to-a-lazy-daem.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0003-When-killing-the-last-client-attached-to-a-lazy-daem.pa"; filename*1="tch" >From 767b43cc04f42f36db20fa1ef9b32aab2c37b497 Mon Sep 17 00:00:00 2001 From: Jim Porter Date: Sun, 24 Oct 2021 12:11:37 -0700 Subject: [PATCH 3/3] When killing the last client attached to a lazy daemon, kill the daemon too lisp/server.el (server-save-buffers-kill-terminal): Kill Emacs when connected to a lazy daemon and there are no other clients. (server-kill-emacs-query-function): Don't warn about killing Emacs when not necessary. lisp/frame.el (handle-delete-frame): Exclude the initial frame of a lazily-created daemon as a "valid" other frame. --- lisp/frame.el | 7 ++++-- lisp/server.el | 61 +++++++++++++++++++++++++++++--------------------- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/lisp/frame.el b/lisp/frame.el index dfbd751201..fd38c4b623 100644 --- a/lisp/frame.el +++ b/lisp/frame.el @@ -119,11 +119,14 @@ handle-delete-frame (if (catch 'other-frame (dolist (frame-1 (frame-list)) ;; A valid "other" frame is visible, has its `delete-before' - ;; parameter unset and is not a child frame. + ;; parameter unset and is not a child frame or the initial frame + ;; of a lazily-created daemon. (when (and (not (eq frame-1 frame)) (frame-visible-p frame-1) (not (frame-parent frame-1)) - (not (frame-parameter frame-1 'delete-before))) + (not (frame-parameter frame-1 'delete-before)) + (or (not (eq (daemon-type) 'lazy)) + (frame-parameter frame-1 'client))) (throw 'other-frame t)))) (delete-frame frame t) ;; Gildea@x.org says it is ok to ask questions before terminating. diff --git a/lisp/server.el b/lisp/server.el index 5988560c83..0cbabba621 100644 --- a/lisp/server.el +++ b/lisp/server.el @@ -1580,12 +1580,17 @@ server-done (server-buffer-done (current-buffer)))) (defun server-kill-emacs-query-function () - "Ask before exiting Emacs if it has live clients." - (or (not (seq-some (lambda (proc) - (seq-some #'buffer-live-p - (process-get proc 'buffers))) - server-clients)) - (yes-or-no-p "This Emacs session has clients; exit anyway? "))) + "Ask before exiting Emacs if it has live clients. +If Emacs was started as a lazy daemon and the only live client is the +current frame's client, don't bother asking." + (let ((ignored-proc (and (eq (daemon-type) 'lazy) + (frame-parameter nil 'client)))) + (or (not (seq-some (lambda (proc) + (unless (eq ignored-proc proc) + (seq-some #'buffer-live-p + (process-get proc 'buffers)))) + server-clients)) + (yes-or-no-p "This Emacs session has clients; exit anyway? ")))) (defun server-kill-buffer () "Remove the current buffer from its clients' buffer list. @@ -1721,28 +1726,32 @@ server-save-buffers-kill-terminal With ARG non-nil, silently save all file-visiting buffers, then kill. If emacsclient was started with a list of filenames to edit, then -only these files will be asked to be saved." +only these files will be asked to be saved. + +If Emacs was started as a lazy daemon and this is the last client +connected to it, this will call `save-buffers-kill-emacs'." (let ((proc (frame-parameter nil 'client))) - (cond ((eq proc 'nowait) + (unless (or (eq proc 'nowait) (processp proc)) + (error "Invalid client frame")) + (if (and (eq (daemon-type) 'lazy) + (equal server-clients (unless (eq proc 'nowait) (list proc)))) + ;; If we're the last client connected to a lazy daemon, kill Emacs. + (save-buffers-kill-emacs arg) + (if (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"))))) + (progn (save-some-buffers arg) + (delete-frame)) + (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)))))) (define-key ctl-x-map "#" 'server-edit) -- 2.25.1 --------------6EEDACE8E824739EB7D3680E--