Subject: bug#71554: 29.3; eshell-command async buffer behavior

From: Thierry Volpiatto <[email-masked]>
Date: Fri, 05 Jul 2024 14:34:01 +0000

Thierry Volpiatto <[email-masked]> writes:

> Eli Zaretskii <[email-masked]> writes:
>
>>> From: Thierry Volpiatto <[email-masked]>
>>> Cc: Thierry Volpiatto <[email-masked]>,  [email-masked],
>>>   [email-masked],  [email-masked]
>>> Date: Fri, 05 Jul 2024 06:24:23 +0000
>>> 
>>> Eli Zaretskii <[email-masked]> writes:
>>> 
>>> >> > Why do we have to make this change in Emacs 30?  AFAIU, it isn't a
>>> >> > bug, just inconsistency between shell-mode and Eshell, and we have
>>> >> > lived with it for quite some time.  Right?
>>> >> 
>>> >> We have lived with this because the eshell-command async behavior was
>>> >> broken from 2005 to 2021. Now it is repaired the need of beeing able to
>>> >> run several processes raise.
>>> >> 
>>> >> > I'd like to have as short a pretest for Emacs 30.1 as possible, and
>>> >> > that means no changes we don't absolutely have to make.  Please
>>> >> > consider the necessity of this change from that POV.
>>> >> 
>>> >> Now we have a emacs-31 branch, perhaps this can be installed there?
>>> >
>>> > There's no emacs-31, only emacs-30 and master.  The master branch will
>>> > eventually become Emacs 31, so if you mean to install there, I'm okay
>>> > with that, of course.
>>> 
>>> Yes, I meant install in master, sorry.
>>
>> That's fine by me, thanks.
>
> Good, Jim, do you want me to modify the patch or you modify the default yourself?

Here the patch modified (only changed default value, a typo and the
commit message).

-- 
Thierry
From 192787d0098ef0dd4e1ffbf4b6d665f151352974 Mon Sep 17 00:00:00 2001
From: Thierry Volpiatto <[email-masked]>
Date: Wed, 19 Jun 2024 12:02:59 +0200
Subject: [PATCH] Allow using multiple buffers in eshell-command (bug#71554)

Provide the same functionality as `async-shell-command-buffer` but for
eshell-command.

* lisp/eshell/eshell.el (eshell-command-async-buffer): New user var.
(eshell-command): Use it.
---
 lisp/eshell/eshell.el | 66 +++++++++++++++++++++++++++++++++++++++----
 1 file changed, 60 insertions(+), 6 deletions(-)

diff --git a/lisp/eshell/eshell.el b/lisp/eshell/eshell.el
index 18e05a371a4..7508693447e 100644
--- a/lisp/eshell/eshell.el
+++ b/lisp/eshell/eshell.el
@@ -216,6 +216,34 @@ named \"*eshell*<2>\"."
   :type 'string
   :group 'eshell)
 
+(defcustom eshell-command-async-buffer 'confirm-new-buffer
+  "What to do when the output buffer is used by another shell command.
+This option specifies how to resolve the conflict where a new command
+wants to direct its output to the buffer whose name is stored
+in `eshell-command-buffer-name-async', but that buffer is already
+taken by another running shell command.
+
+The value `confirm-kill-process' is used to ask for confirmation before
+killing the already running process and running a new process
+in the same buffer, `confirm-new-buffer' for confirmation before running
+the command in a new buffer with a name other than the default buffer name,
+`new-buffer' for doing the same without confirmation,
+`confirm-rename-buffer' for confirmation before renaming the existing
+output buffer and running a new command in the default buffer,
+`rename-buffer' for doing the same without confirmation."
+  :type '(choice (const :tag "Confirm killing of running command"
+			confirm-kill-process)
+		 (const :tag "Confirm creation of a new buffer"
+			confirm-new-buffer)
+		 (const :tag "Create a new buffer"
+			new-buffer)
+		 (const :tag "Confirm renaming of existing buffer"
+			confirm-rename-buffer)
+		 (const :tag "Rename the existing buffer"
+			rename-buffer))
+  :group 'eshell
+  :version "30.1")
+
 ;;;_* Running Eshell
 ;;
 ;; There are only three commands used to invoke Eshell.  The first two
@@ -283,11 +311,18 @@ information on Eshell, see Info node `(eshell)Top'."
                                   (eshell-command-mode +1))
       (read-from-minibuffer prompt))))
 
+(defvar eshell-command-buffer-name-async "*Eshell Async Command Output*")
+(defvar eshell-command-buffer-name-sync "*Eshell Command Output*")
 ;;;###autoload
 (defun eshell-command (command &optional to-current-buffer)
   "Execute the Eshell command string COMMAND.
 If TO-CURRENT-BUFFER is non-nil (interactively, with the prefix
-argument), then insert output into the current buffer at point."
+argument), then insert output into the current buffer at point.
+
+When \"&\" is added at end of command, the command is async and its output
+appears in a specific buffer.  You can customize
+`eshell-command-async-buffer' to specify what to do when this output
+buffer is already taken by another running shell command."
   (interactive (list (eshell-read-command)
                      current-prefix-arg))
   (save-excursion
@@ -302,13 +337,32 @@ argument), then insert output into the current buffer at point."
 		       ,(eshell-parse-command command))
                     command))
 	     intr
+             unique
 	     (bufname (if (eq (car-safe proc) :eshell-background)
-			  "*Eshell Async Command Output*"
+			  eshell-command-buffer-name-async
 			(setq intr t)
-			"*Eshell Command Output*")))
-	(if (buffer-live-p (get-buffer bufname))
-	    (kill-buffer bufname))
-	(rename-buffer bufname)
+			eshell-command-buffer-name-sync)))
+	(when (or (buffer-live-p (get-buffer bufname))
+                  (cl-loop for buf in (buffer-list)
+                           thereis (and (string-match-p
+                                         (regexp-quote
+                                          (substring
+                                           bufname 0 (1- (length bufname))))
+                                         (buffer-name buf))
+                                        (buffer-live-p buf))))
+          (pcase eshell-command-async-buffer
+            ('confirm-kill-process
+             (shell-command--same-buffer-confirm "Kill it")
+             (kill-buffer bufname))
+	    ('confirm-new-buffer
+             (shell-command--same-buffer-confirm "Use a new buffer")
+             (setq unique t))
+            ('new-buffer (setq unique t))
+            ('confirm-rename-buffer
+             (shell-command--same-buffer-confirm "Rename it")
+             (kill-buffer bufname))
+            ('rename-buffer (kill-buffer bufname))))
+	(rename-buffer bufname unique)
 	;; things get a little coarse here, since the desire is to
 	;; make the output as attractive as possible, with no
 	;; extraneous newlines
-- 
2.34.1