unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Philip Kaludercic <philipk@posteo.net>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 55426@debbugs.gnu.org
Subject: bug#55426: [PATCH] Add option to kill a shell buffer when the process ends
Date: Sun, 15 May 2022 11:06:42 +0000	[thread overview]
Message-ID: <877d6ndph9.fsf@posteo.net> (raw)
In-Reply-To: <83pmkfjizq.fsf@gnu.org> (Eli Zaretskii's message of "Sun, 15 May 2022 11:30:17 +0300")

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


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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-option-to-kill-a-shell-buffer-when-the-process-e.patch --]
[-- Type: text/x-diff, Size: 2051 bytes --]

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


[-- Attachment #3: Type: text/plain, Size: 2192 bytes --]


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.

  parent reply	other threads:[~2022-05-15 11:06 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-15  8:13 bug#55426: [PATCH] Add option to kill a shell buffer when the process ends Philip Kaludercic
2022-05-15  8:30 ` Eli Zaretskii
2022-05-15  9:49   ` Eli Zaretskii
2022-05-15 11:06   ` Philip Kaludercic [this message]
2022-05-15 11:14     ` Eli Zaretskii
2022-05-15 11:51       ` Philip Kaludercic
2022-05-18 23:13         ` Philip Kaludercic
2022-05-19  6:53           ` Eli Zaretskii
2022-05-19  6:51         ` Eli Zaretskii
2022-05-20  8:23           ` Philip Kaludercic
2022-05-15 11:19     ` Andreas Schwab

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=877d6ndph9.fsf@posteo.net \
    --to=philipk@posteo.net \
    --cc=55426@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    /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).