Subject: bug#71049: async-shell-command ends with "Process *Async Shell Command* finished" when remote "direct-async-process"

From: Dmitry Gutov <[email-masked]>
Date: Sat, 25 May 2024 16:54:54 +0300

Hi Michael,

On 25/05/2024 13:49, Michael Albinus wrote:

>> The answer is that async-shell-command uses shell-mode as the major
>> mode for the output buffer. For syntax highlighting, I guess.
>>
>> You make a good point that the shell history for such buffers would
>> usually make no sense - even if the running process takes user input
>> (usually not, but sometimes it might) - its input history would be
>> different from the shell.
>>
>> So maybe we could just move the last form in shell-mode (which
>> initializes comint-input-ring) to 'shell'
> Don't know. (shell-mode) is called in shell-command since bcad49851742
> (1995-07-17). And it is called in tramp-handle-shell-command since
> f5e29b9b70a5 (2011-09-04).
> 
> (comint-read-input-ring) is called in shell-mode since
> (1993-10-22). There might be packages which trust on the
> comint-input-ring existence for buffers in shell mode, even if such
> buffers are created by shell-command..

Yes, it would be an incompatibility - so we'll need to consider the 
migration path. See the attached patch - I suggest that any callers of 
'shell-mode' that need the exact same input-ring setup also call 
shell-setup-input-ring (if it's fboundp - a version check).

Or I suppose we could check the value of shell-setup-input-ring and skip 
history loading when it is empty. It's a more subtle incompatibility 
which might affect (or not) third-party code in similar ways.

Either of the attached patches solves this part of the problem for me, 
please take your pick.

diff --git a/lisp/shell.el b/lisp/shell.el
index e6b315ee5c0..d485c8af567 100644
--- a/lisp/shell.el
+++ b/lisp/shell.el
@@ -718,7 +718,10 @@ shell-mode
 
   ;; This is not really correct, since the shell buffer does not really
   ;; edit this directory.  But it is useful in the buffer list and menus.
-  (setq list-buffers-directory (expand-file-name default-directory))
+  (setq list-buffers-directory (expand-file-name default-directory)))
+
+(defun shell-setup-input-ring ()
+  "Set up `comint-input-ring' for this buffer, for the chosen shell."
   ;; shell-dependent assignments.
   (when (ring-empty-p comint-input-ring)
     (let ((remote (file-remote-p default-directory))
@@ -948,6 +951,7 @@ shell
                   (symbol-value xargs-name)
                 '("-i")))
        (shell-mode)
+       (shell-setup-input-ring)
        (when (file-exists-p startfile)
          ;; Wait until the prompt has appeared.
          (while (= start-point (point))
diff --git a/lisp/shell.el b/lisp/shell.el
index e6b315ee5c0..ca02d602966 100644
--- a/lisp/shell.el
+++ b/lisp/shell.el
@@ -720,9 +720,10 @@ shell-mode
   ;; edit this directory.  But it is useful in the buffer list and menus.
   (setq list-buffers-directory (expand-file-name default-directory))
   ;; shell-dependent assignments.
-  (when (ring-empty-p comint-input-ring)
+  (when (and (ring-empty-p comint-input-ring)
+             shell--start-prog)
     (let ((remote (file-remote-p default-directory))
-          (shell (or shell--start-prog ""))
+          (shell shell--start-prog)
           (hsize (getenv "HISTSIZE"))
           (hfile (getenv "HISTFILE")))
       (when remote