]> git.eshelyaron.com Git - emacs.git/commitdiff
Add option to kill a shell buffer when the process ends
authorPhilip Kaludercic <philipk@posteo.net>
Sun, 15 May 2022 00:09:46 +0000 (02:09 +0200)
committerPhilip Kaludercic <philipk@posteo.net>
Fri, 20 May 2022 08:22:47 +0000 (10:22 +0200)
* 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
lisp/shell.el

index 26b9b19952b016fb0a79101d74417e02eecf254b..2314e55164c394341ed1c3f10a79b526fa46c9ef 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1616,6 +1616,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
 
 +++
index 47887433d9f149b2b6e48fb738d9c6107cb8ee47..4e65fccf9e5a9b5cd2c04af9064e94848b9d0f53 100644 (file)
@@ -331,6 +331,12 @@ Useful for shells like zsh that has this feature."
   :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 @@ Make the shell buffer the current buffer, and return it.
           (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