Subject: Re: bug#55426: [PATCH] Add option to kill a shell buffer when the process ends

From: Philip Kaludercic <[email-masked]>
Date: Sun, 15 May 2022 11:51:53 +0000

Fixed both here:

>From 6db6675c1f931b0eed6606227ecd4919a7052afd Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <[email-masked]>
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..27772dc54e 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 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..4e65fccf9e 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 nil
+  "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

Should it push it?

Eli Zaretskii <[email-masked]> writes:

>> From: Philip Kaludercic <[email-masked]>
>> Cc: [email-masked]
>> Date: Sun, 15 May 2022 11:06:42 +0000
>> 
>> Thank you for your comments, here is an improved version:
>
> Thanks.  One nit:
>
>> +** Shell
>> +
>> +---
>> +*** New user option 'shell-kill-buffer-on-exit'
>> +Enabling this open will automatically kill a *shell* buffer as soon as
>                  ^^^^
> Seems like a leftover from a different version?

Andreas Schwab <[email-masked]> writes:

> On Mai 15 2022, Philip Kaludercic wrote:
>
>> 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
>
> That should default to nil.