Subject: bug#24394: 25.1.50; (find-file "/sudo::") ignores async-shell-command-buffer settings

From: Tino Calancha <[email-masked]>
Date: Sun, 11 Sep 2016 21:22:12 +0900 (JST)

On Sun, 11 Sep 2016, Michael Albinus wrote:

Hi Michael,
thank you for your comprehensive answer.

> Well, this comment is more than 8 years old, and it is not true anymore
> (I've just tested). I don't remember when this was fixed, but so what 
...
Great!  It's very good to heard that in principle tramp could support
>1 async processes.

> However, I'm kind of reluctant to fix this in
> `tramp-handle-shell-command'. The respective code in `shell-command'
> spans over ~40 lines, and I don't believe Tramp shall simply copy those
> lines (and other details not handled in Tramp yet). It's even
> questionable that Tramp shall offer an own handler for `shell-command'.
It might has sense to refactor that part into a new function (see patch 
below).
The tramp could use this new function which just use the variable
`async-shell-command-buffer'.  Other things like 
`shell-command-dont-erase-buffer'
can perfectly be ignored by tramp: they are still not well established.

> The reason why Tramp does this is the use of `shell-file-name' and
> `shell-command-switch'. They keep host local values, for remote
> connections other values are needed. It is a long standing request, that
> Tramp shall offer connection local variables, which carry different
> values for different remote hosts. If we would have such a mechanism,
> `shell-command' could use `process-file' and `start-file-process', and 
it
> would not need to call a file name handler anymore.
>
> And this error would go away.
That sounds like the ultimate solution.
A partial solution could be to allow running >1 async commands as root
in the local machine _only_.  Then, `shell-file-name' and
`shell-command-switch' are the same.
Sometimes i need to execute more than 1 process with root priviledges in
my local machine: i do this using several terminals.
Running all the processes inside Emacs would be nicer.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>From ea08797362e9f4e02746e12229af7f160f7b4251 Mon Sep 17 00:00:00 2001
From: Tino Calancha <[email-masked]>
Date: Sun, 11 Sep 2016 20:49:56 +0900
Subject: [PATCH] shell-command: Refactor buffer creation for async cmd

* lisp/simple.el (async-shell-command-handle-multi-process):
New defun; handle the creation of a new asynchronous shell command
according with 'async-shell-command-buffer'.
(shell-command): Use it.
---
  lisp/simple.el | 91 
++++++++++++++++++++++++++++++++++++----------------------
  1 file changed, 57 insertions(+), 34 deletions(-)

diff --git a/lisp/simple.el b/lisp/simple.el
index 04a525c..3d0c579 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -3311,6 +3311,59 @@ async-shell-command
      (setq command (concat command " &")))
    (shell-command command output-buffer error-buffer))

+(defun async-shell-command-handle-multi-process (proc buffer 
output-buffer)
+  "Handle > 1 async shell commands according with 
`async-shell-command-buffer'.
+PROC is the process with buffer *Async Shell Command*.
+BUFFER is the buffer associated to the new async shell command.
+OUTPUT-BUFFER, if non-nil, says to put the output in some other buffer.
+Return BUFFER."
+  (pcase async-shell-command-buffer
+    ('confirm-kill-process
+      ;; If will kill a process, query first.
+      (if (yes-or-no-p
+           "A command is running in the default buffer.  Kill it? ")
+          (kill-process proc)
+        (error "Shell command in progress")))
+    ('confirm-new-buffer
+      ;; If will create a new buffer, query first.
+      (if (yes-or-no-p
+           "A command is running in the default buffer.  Use a new 
buffer? ")
+          (setq buffer (generate-new-buffer
+                        (or (and (or (bufferp output-buffer)
+                                     (stringp output-buffer))
+                                 (buffer-name output-buffer))
+                            "*Async Shell Command*")))
+        (error "Shell command in progress")))
+    ('new-buffer
+      ;; It will create a new buffer.
+      (setq buffer (generate-new-buffer
+                    (or (and (or (bufferp output-buffer)
+                                 (stringp output-buffer))
+                             (buffer-name output-buffer))
+                        "*Async Shell Command*"))))
+    ('confirm-rename-buffer
+      ;; If will rename the buffer, query first.
+      (if (yes-or-no-p
+           "A command is running in the default buffer.  Rename it? ")
+          (progn
+            (with-current-buffer buffer
+              (rename-uniquely))
+            (setq buffer (get-buffer-create
+                          (or (and (or (bufferp output-buffer)
+                                       (stringp output-buffer))
+                                   output-buffer)
+                              "*Async Shell Command*"))))
+        (error "Shell command in progress")))
+    ('rename-buffer
+      ;; It will rename the buffer.
+      (with-current-buffer buffer
+        (rename-uniquely))
+      (setq buffer (get-buffer-create
+                    (or (and (or (bufferp output-buffer)
+                                 (stringp output-buffer))
+                             output-buffer)
+                        "*Async Shell Command*"))))) buffer)
+
  (defun shell-command (command &optional output-buffer error-buffer)
    "Execute string COMMAND in inferior shell; display output, if any.
  With prefix argument, insert the COMMAND's output at point.
@@ -3442,40 +3495,10 @@ shell-command
  		(setq command (substring command 0 (match-beginning 0)))
  		;; Ask the user what to do with already running process.
  		(setq proc (get-buffer-process buffer))
-		(when proc
-		  (cond
-		   ((eq async-shell-command-buffer 'confirm-kill-process)
-		    ;; If will kill a process, query first.
-		    (if (yes-or-no-p "A command is running in the default 
buffer.  Kill it? ")
-			(kill-process proc)
-		      (error "Shell command in progress")))
-		   ((eq async-shell-command-buffer 'confirm-new-buffer)
-		    ;; If will create a new buffer, query first.
-		    (if (yes-or-no-p "A command is running in the default 
buffer.  Use a new buffer? ")
-			(setq buffer (generate-new-buffer
-				      (or (and (bufferp output-buffer) 
(buffer-name output-buffer))
-					  output-buffer "*Async Shell 
Command*")))
-		      (error "Shell command in progress")))
-		   ((eq async-shell-command-buffer 'new-buffer)
-		    ;; It will create a new buffer.
-		    (setq buffer (generate-new-buffer
-				  (or (and (bufferp output-buffer) 
(buffer-name output-buffer))
-				      output-buffer "*Async Shell 
Command*"))))
-		   ((eq async-shell-command-buffer 'confirm-rename-buffer)
-		    ;; If will rename the buffer, query first.
-		    (if (yes-or-no-p "A command is running in the default 
buffer.  Rename it? ")
-			(progn
-			  (with-current-buffer buffer
-			    (rename-uniquely))
-			  (setq buffer (get-buffer-create
-					(or output-buffer "*Async Shell 
Command*"))))
-		      (error "Shell command in progress")))
-		   ((eq async-shell-command-buffer 'rename-buffer)
-		    ;; It will rename the buffer.
-		    (with-current-buffer buffer
-		      (rename-uniquely))
-		    (setq buffer (get-buffer-create
-				  (or output-buffer "*Async Shell 
Command*"))))))
+                (when proc
+                  (setq buffer
+                        (async-shell-command-handle-multi-process
+                         proc output-buffer buffer)))
  		(with-current-buffer buffer
  		  (display-buffer buffer '(nil (allow-no-window . t)))
                    (shell-command--save-pos-or-erase)
-- 
2.9.3

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

In GNU Emacs 25.1.50.1 (x86_64-pc-linux-gnu, GTK+ Version 3.21.5)
  of 2016-09-11 built on calancha-pc
Repository revision: 5fd1f7f931163ddf04f0ba0c362840fc91fba54a