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

From: Dmitry Gutov <[email-masked]>
Date: Fri, 31 May 2024 03:24:49 +0300

Hi Michael,

On 30/05/2024 11:49, Michael Albinus wrote:

>> Thank you. I haven't tested this personally either, but it seems like
>> this is the behavior we will want by default.
>>
>> So the proposed solution should include it, I think.
> In order to not misunderstand: you agree the solution based on
> tramp-histfile-override now, don't you?

Just to be frank: I don't like the approach, but I do think it's better 
than the status quo. In that sense, I agree with it, if none other is 
accepted.

But, well, in the meantime I came up with a couple of new solutions:

1) [first patch] We can add a new major mode, for 'M-&' to use instead 
of the full-blown 'shell-mode' - it could be very simple: just apply 
font-lock keywords and maybe set list-buffers-directory.

Problems? I suppose someone might be using shell-mode-hook to do 
something in the async-shell-command output buffer, and it won't fire 
anymore. Seemingly very minor concern.

2) [second patch] async-shell-command could set shell--start-prog (it's 
permanent-local) to `none', and then shell-mode will check for that 
value, and if set to this value, skip the input ring setup.

Downsides? Pretty ad-hoc approach. And any external code looking up this 
(private) variable could be surprised by the new value.

diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el
index 9385b023392..f0c64a7a90f 100644
--- a/lisp/net/tramp.el
+++ b/lisp/net/tramp.el
@@ -5247,8 +5247,8 @@ tramp-handle-shell-command
 	      ;; Display output.
 	      (with-current-buffer output-buffer
 		(setq mode-line-process '(":%s"))
-		(unless (eq major-mode 'shell-mode)
-		  (shell-mode))
+		(unless (eq major-mode 'shell-command-mode)
+		  (shell-command-mode))
 		(set-process-filter p #'comint-output-filter)
 		(set-process-sentinel p #'shell-command-sentinel)
 		(when error-file
diff --git a/lisp/shell.el b/lisp/shell.el
index e6b315ee5c0..7fa84a37e83 100644
--- a/lisp/shell.el
+++ b/lisp/shell.el
@@ -838,6 +838,13 @@ shell-write-history-on-exit
       (with-current-buffer buf
         (insert (format "\nProcess %s %s\n" process event))))))
 
+(define-derived-mode shell-command-mode comint-mode "Shell"
+  "Major mode for the output of `async-shell-command'."
+  (setq-local font-lock-defaults '(shell-font-lock-keywords t))
+  ;; See comments in `shell-mode'.
+  (setq-local ansi-color-apply-face-function #'shell-apply-ansi-color)
+  (setq list-buffers-directory (expand-file-name default-directory)))
+
 ;;;###autoload
 (defun shell (&optional buffer file-name)
   "Run an inferior shell, with I/O through BUFFER (which defaults to `*shell*').
diff --git a/lisp/simple.el b/lisp/simple.el
index ae8a824cb54..8618427f5c0 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -31,7 +31,7 @@
 (eval-when-compile (require 'cl-lib))
 
 (declare-function widget-convert "wid-edit" (type &rest args))
-(declare-function shell-mode "shell" ())
+(declare-function shell-command-mode "shell" ())
 
 ;;; From compile.el
 (defvar compilation-current-error)
@@ -4721,7 +4721,7 @@ shell-command
 		  (setq proc
 			(start-process-shell-command "Shell" buffer command)))
 		(setq mode-line-process '(":%s"))
-                (shell-mode)
+                (shell-command-mode)
                 (setq-local revert-buffer-function
                             (lambda (&rest _)
                               (async-shell-command command buffer)))
diff --git a/lisp/shell.el b/lisp/shell.el
index e6b315ee5c0..07038832437 100644
--- a/lisp/shell.el
+++ b/lisp/shell.el
@@ -702,7 +702,7 @@ shell-mode
   (add-hook 'comint-indirect-setup-hook
             #'shell-indirect-setup-hook 'append t)
   (setq comint-indirect-setup-function
-        (let ((shell shell--start-prog))
+        (let ((shell (and (stringp shell--start-prog) shell--start-prog)))
           (lambda ()
             (require 'sh-script)
             (cl-letf
@@ -720,7 +720,8 @@ 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)
+             (not (eq shell--start-prog 'none)))
     (let ((remote (file-remote-p default-directory))
           (shell (or shell--start-prog ""))
           (hsize (getenv "HISTSIZE"))
diff --git a/lisp/simple.el b/lisp/simple.el
index ae8a824cb54..9074f564ca1 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -4621,6 +4621,7 @@ shell-command
 			  (and filename (file-relative-name filename))))
     current-prefix-arg
     shell-command-default-error-buffer))
+  (defvar shell--start-prog)
   ;; Look for a handler in case default-directory is a remote file name.
   (let ((handler
 	 (find-file-name-handler (directory-file-name default-directory)
@@ -4721,6 +4722,7 @@ shell-command
 		  (setq proc
 			(start-process-shell-command "Shell" buffer command)))
 		(setq mode-line-process '(":%s"))
+                (setq shell--start-prog 'none)
                 (shell-mode)
                 (setq-local revert-buffer-function
                             (lambda (&rest _)