bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: Philip Kaludercic
Subject: bug#55426: [PATCH] Add option to kill a shell buffer when the process ends
Date: Sun, 15 May 2022 11:06:42 +0000

Thank you for your comments, here is an improved version:

>From 7337f6862cdb6954498282b9c9d22f6b2cd3457d Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <philipk@posteo.net>
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

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Philip Kaludercic <philipk@posteo.net>
>> 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 <eliz@gnu.org> writes:

>> Cc: 55426@debbugs.gnu.org
>> Date: Sun, 15 May 2022 11:30:17 +0300
>> From: Eli Zaretskii <eliz@gnu.org>
>> 
>> > From: Philip Kaludercic <philipk@posteo.net>
>> > 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.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]