From 4d9d7b3ce5a5370b733ceac9ebebf40987113810 Mon Sep 17 00:00:00 2001 From: Thierry Volpiatto Date: Wed, 19 Jun 2024 12:02:59 +0200 Subject: [PATCH] Use async-shell-command-buffer in eshell-command fix bug#71554 * lisp/eshell/eshell.el (eshell-command): Allow using multiple buffers. (eshell-command-async-buffer): New user var. --- 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..c84b450956a 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 '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 'shell + :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