From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Philip Kaludercic Newsgroups: gmane.emacs.bugs Subject: bug#55426: [PATCH] Add option to kill a shell buffer when the process ends Date: Sun, 15 May 2022 11:06:42 +0000 Message-ID: <877d6ndph9.fsf@posteo.net> References: <87czgfdxhe.fsf@posteo.net> <83pmkfjizq.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="40490"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 55426@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun May 15 13:07:18 2022 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 1nqC5l-000AKA-K6 for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 15 May 2022 13:07:17 +0200 Original-Received: from localhost ([::1]:38092 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nqC5k-0004oi-5Q for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 15 May 2022 07:07:16 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:33990) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nqC5W-0004oZ-4o for bug-gnu-emacs@gnu.org; Sun, 15 May 2022 07:07:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:54709) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nqC5V-0005Db-Pd for bug-gnu-emacs@gnu.org; Sun, 15 May 2022 07:07:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nqC5V-000512-IV for bug-gnu-emacs@gnu.org; Sun, 15 May 2022 07:07:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Philip Kaludercic Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 15 May 2022 11:07:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 55426 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 55426-submit@debbugs.gnu.org id=B55426.165261281319264 (code B ref 55426); Sun, 15 May 2022 11:07:01 +0000 Original-Received: (at 55426) by debbugs.gnu.org; 15 May 2022 11:06:53 +0000 Original-Received: from localhost ([127.0.0.1]:48606 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nqC5M-00050e-MF for submit@debbugs.gnu.org; Sun, 15 May 2022 07:06:53 -0400 Original-Received: from mout01.posteo.de ([185.67.36.65]:46353) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nqC5K-00050P-Gl for 55426@debbugs.gnu.org; Sun, 15 May 2022 07:06:51 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id 778EA240026 for <55426@debbugs.gnu.org>; Sun, 15 May 2022 13:06:44 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1652612804; bh=YEp5Meg3Y1i5DFStnqUx65eX7Nazj3vVzR9GRUd22CA=; h=From:To:Cc:Subject:Autocrypt:Date:From; b=bTMNn7JDiTh0iLGwGYB5c0q8m/IamD8SBLMk2XDrNVq2CPDmgTOn4me9ApFEvMbNS Crr7FLuNSSzKBwGUnqgBoGynC8aANHxV5WneKEum4ablCHiSxAsNe4a5ApaJzr1FED NpcjRrumiok9sHNvQV0AYEXz96vDW2dpNCmdnAn8psFZn8KEQjZ53hZt1m3cZgbzxc ZWOSF2OcJTwXo+X37Vya6SRE+ZCu+pPPSRfVnxw7EBpUtg/3BqNa0zrBahX6TLSOyF 2VbzWOtue2/KrHwDPeaM8h9FSe7zkE0UX0Ws6QAAVwz/g+26VxS/vRb4vUEZWkqH3D NB8olOqIFt1fw== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4L1KMR4szrz6tmB; Sun, 15 May 2022 13:06:43 +0200 (CEST) Autocrypt: addr=philipk@posteo.net; prefer-encrypt=nopreference; keydata= mDMEYHHqUhYJKwYBBAHaRw8BAQdAp3GdmYJ6tm5McweY6dEvIYIiry+Oz9rU4MH6NHWK0Ee0QlBo aWxpcCBLYWx1ZGVyY2ljIChnZW5lcmF0ZWQgYnkgYXV0b2NyeXB0LmVsKSA8cGhpbGlwa0Bwb3N0 ZW8ubmV0PoiQBBMWCAA4FiEEDM2H44ZoPt9Ms0eHtVrAHPRh1FwFAmBx6lICGwMFCwkIBwIGFQoJ CAsCBBYCAwECHgECF4AACgkQtVrAHPRh1FyTkgEAjlbGPxFchvMbxzAES3r8QLuZgCxeAXunM9gh io0ePtUBALVhh9G6wIoZhl0gUCbQpoN/UJHI08Gm1qDob5zDxnIHuDgEYHHqUhIKKwYBBAGXVQEF AQEHQNcRB+MUimTMqoxxMMUERpOR+Q4b1KgncDZkhrO2ql1tAwEIB4h4BBgWCAAgFiEEDM2H44Zo Pt9Ms0eHtVrAHPRh1FwFAmBx6lICGwwACgkQtVrAHPRh1Fw1JwD/Qo7kvtib8jy7puyWrSv0MeTS g8qIxgoRWJE/KKdkCLEA/jb9b9/g8nnX+UcwHf/4VfKsjExlnND3FrBviXUW6NcB In-Reply-To: <83pmkfjizq.fsf@gnu.org> (Eli Zaretskii's message of "Sun, 15 May 2022 11:30:17 +0300") 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:232297 Archived-At: --=-=-= Content-Type: text/plain Thank you for your comments, here is an improved version: --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Add-option-to-kill-a-shell-buffer-when-the-process-e.patch >From 7337f6862cdb6954498282b9c9d22f6b2cd3457d Mon Sep 17 00:00:00 2001 From: Philip Kaludercic Date: Sun, 15 May 2022 02:09:46 +0200 Subject: [PATCH] Add option to kill a shell buffer when the process ends * shell.el (shell-kill-buffer-on-quit): Add new option (bug#55426). (shell): Respect 'shell-kill-buffer-on-quit'. * NEWS: Mention 'shell-kill-buffer-on-quit'. --- etc/NEWS | 7 +++++++ lisp/shell.el | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/etc/NEWS b/etc/NEWS index a0164bbf3f..9ef83189dd 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1544,6 +1544,13 @@ values passed as a single token, such as '-oVALUE' or 'eshell-eval-using-options' macro. See "Defining new built-in commands" in the "(eshell) Built-ins" node of the Eshell manual. +** Shell + +--- +*** New user option 'shell-kill-buffer-on-exit' +Enabling this open will automatically kill a *shell* buffer as soon as +the shell session terminates. + ** Calc +++ diff --git a/lisp/shell.el b/lisp/shell.el index 47887433d9..7000d38255 100644 --- a/lisp/shell.el +++ b/lisp/shell.el @@ -331,6 +331,12 @@ shell-has-auto-cd :group 'shell-directories :version "28.1") +(defcustom shell-kill-buffer-on-exit t + "Kill a shell buffer after the shell process terminates." + :type 'boolean + :group 'shell + :version "29.1") + (defvar shell-mode-map (let ((map (make-sparse-keymap))) (define-key map "\C-c\C-f" 'shell-forward-command) @@ -818,6 +824,17 @@ shell (with-temp-buffer (insert-file-contents startfile) (buffer-string))))))) + (when shell-kill-buffer-on-exit + (let* ((buffer (current-buffer)) + (process (get-buffer-process buffer)) + (sentinel (process-sentinel process))) + (set-process-sentinel + process + (lambda (proc event) + (when sentinel + (funcall sentinel proc event)) + (unless (buffer-live-p proc) + (kill-buffer buffer)))))) buffer) ;;; Directory tracking -- 2.30.2 --=-=-= Content-Type: text/plain Eli Zaretskii writes: >> From: Philip Kaludercic >> Date: Sun, 15 May 2022 08:13:49 +0000 >> >> The below patch adds an option that has a *shell* buffer automatically >> killed when a shell process terminates. I find this useful, because it >> helps to avoid re-activating old shell buffers in the wrong >> `default-directory'. > > Thanks, a few comments below. > >> +(defcustom shell-kill-buffer-on-quit t > > I think a better name is shell-kill-shell-buffer-on-exit. > >> + "Kill shell buffer after the process terminates." > > "Kill the shell buffer when the shell process terminates." ^ I replaced this with "a", to avoid confusion as to what specific shell buffer is meant (not only *shell*, but any shell-mode buffer). >> + (when shell-kill-buffer-on-quit >> + (let* ((buffer (current-buffer)) >> + (sentinel (process-sentinel (get-buffer-process buffer)))) >> + (set-process-sentinel >> + (get-buffer-process (current-buffer)) >> + (lambda (proc event) >> + (unless (buffer-live-p proc) >> + (kill-buffer buffer)) >> + (when sentinel >> + (funcall sentinel proc event)))))) > > Shouldn't we call the previous sentinel before killing the buffer? > That sentinel could not be prepared to the buffer being dead. > > Also, can you modify the code to call get-buffer-process only once? > >> If this patch is merged, perhaps something similar for term could be >> done too? > > I'm not sure, but I don't use term frequently enough for my opinion to > matter. Eli Zaretskii writes: >> Cc: 55426@debbugs.gnu.org >> Date: Sun, 15 May 2022 11:30:17 +0300 >> From: Eli Zaretskii >> >> > From: Philip Kaludercic >> > Date: Sun, 15 May 2022 08:13:49 +0000 >> > >> > The below patch adds an option that has a *shell* buffer automatically >> > killed when a shell process terminates. I find this useful, because it >> > helps to avoid re-activating old shell buffers in the wrong >> > `default-directory'. >> >> Thanks, a few comments below. > > Oh, and one more: I think this change warrants a NEWS entry. --=-=-=--