Subject: Re: bug#10598: 24.0.92; run dired-do-async-shell-command on multiple files individually

From: "Roland Winkler" <[email-masked]>
Date: Tue, 17 Jul 2012 02:17:37 -0500

On Sun Jul 15 2012 Juri Linkov wrote:
> Yes, it seems this is what remains to do.
> It's implemented in the following patch:

Thanks a lot, this looks great to me (and also works for me as expected).

In the slightly modified version of the patch below, I updated also
the docstring for dired-do-shell-command and I unified the indentation.

It would be good if the info node "(emacs)Shell Commands in Dired"
will be updated, too.

--- dired-aux.el~	2012-07-15 01:08:08.000000000 -0500
+++ dired-aux.el	2012-07-17 02:03:33.000000000 -0500
@@ -539,8 +539,17 @@
 (defun dired-do-async-shell-command (command &optional arg file-list)
   "Run a shell command COMMAND on the marked files asynchronously.
 
-Like `dired-do-shell-command' but if COMMAND doesn't end in ampersand,
-adds `* &' surrounded by whitespace and executes the command asynchronously.
+Like `dired-do-shell-command', but adds `&' at the end of COMMAND
+to execute it asynchronously.
+
+When operating on multiple files, asynchronous commands are executed
+in the background on each file in parallel.  In shell syntax this means
+separating the individual commands with `&'.  However, when COMMAND
+ends in `;' or `;&' then commands are executed in the background on each file
+sequentially waiting for each command to terminate before running
+the next command.  In shell syntax this means separating the individual
+commands with `;'.
+
 The output appears in the buffer `*Async Shell Command*'."
   (interactive
    (let ((files (dired-get-marked-files t current-prefix-arg)))
@@ -549,18 +558,14 @@
       (dired-read-shell-command "& on %s: " current-prefix-arg files)
       current-prefix-arg
       files)))
-  (unless (string-match "[*?][ \t]*\\'" command)
-    (setq command (concat command " *")))
   (unless (string-match "&[ \t]*\\'" command)
     (setq command (concat command " &")))
   (dired-do-shell-command command arg file-list))
 
-;; The in-background argument is only needed in Emacs 18 where
-;; shell-command doesn't understand an appended ampersand `&'.
 ;;;###autoload
 (defun dired-do-shell-command (command &optional arg file-list)
   "Run a shell command COMMAND on the marked files.
-If no files are marked or a specific numeric prefix arg is given,
+If no files are marked or a numeric prefix arg is given,
 the next ARG files are used.  Just \\[universal-argument] means the current file.
 The prompt mentions the file(s) or the marker, as appropriate.
 
@@ -582,6 +587,15 @@
 it, write `*\"\"' in place of just `*'.  This is equivalent to just
 `*' in the shell, but avoids Dired's special handling.
 
+If COMMAND ends in `&', `;', or `;&', execute the shell command
+in the background asynchronously.  When operating on multiple files
+and COMMAND ends in `&', the shell command is executed on each file
+in parallel.  In shell syntax this means separating the individual commands
+with `&'.  However, when COMMAND ends in `;' or `;&' then commands
+are executed in the background on each file sequentially
+waiting for each command to terminate before running the next command.
+In shell syntax this means separating the individual commands with `;'.
+
 If COMMAND produces output, it goes to a separate buffer.
 
 This feature does not try to redisplay Dired buffers afterward, as
@@ -592,9 +606,9 @@
 of the Dired buffer, so output files usually are created there
 instead of in a subdir.
 
-In a noninteractive call (from Lisp code), you must specify
-the list of file names explicitly with the FILE-LIST argument, which
-can be produced by `dired-get-marked-files', for example."
+In a noninteractive call, you must specify the list of file names explicitly
+with the FILE-LIST argument, which can be produced by `dired-get-marked-files',
+for example."
 ;;Functions dired-run-shell-command and dired-shell-stuff-it do the
 ;;actual work and can be redefined for customization.
   (interactive
@@ -648,23 +662,34 @@
 ;; Might be redefined for smarter things and could then use RAW-ARG
 ;; (coming from interactive P and currently ignored) to decide what to do.
 ;; Smart would be a way to access basename or extension of file names.
-  (let ((stuff-it
-	 (if (or (string-match dired-star-subst-regexp command)
-		 (string-match dired-quark-subst-regexp command))
-	     (lambda (x)
-	       (let ((retval command))
-		 (while (string-match
-			 "\\(^\\|[ \t]\\)\\([*?]\\)\\([ \t]\\|$\\)" retval)
-		   (setq retval (replace-match x t t retval 2)))
-		 retval))
-	   (lambda (x) (concat command dired-mark-separator x)))))
-    (if on-each
-	(mapconcat stuff-it (mapcar 'shell-quote-argument file-list) ";")
-      (let ((files (mapconcat 'shell-quote-argument
-			      file-list dired-mark-separator)))
-	(if (> (length file-list) 1)
-	    (setq files (concat dired-mark-prefix files dired-mark-postfix)))
-	(funcall stuff-it files)))))
+  (let* ((in-background (string-match "[ \t]*&[ \t]*\\'" command))
+	 (command (if in-background
+		      (substring command 0 (match-beginning 0))
+		    command))
+	 (sequentially (string-match "[ \t]*;[ \t]*\\'" command))
+	 (command (if sequentially
+		      (substring command 0 (match-beginning 0))
+		    command))
+	 (stuff-it
+          (if (or (string-match dired-star-subst-regexp command)
+                  (string-match dired-quark-subst-regexp command))
+              (lambda (x)
+                (let ((retval command))
+                  (while (string-match
+                          "\\(^\\|[ \t]\\)\\([*?]\\)\\([ \t]\\|$\\)" retval)
+                    (setq retval (replace-match x t t retval 2)))
+                  retval))
+            (lambda (x) (concat command dired-mark-separator x)))))
+    (concat
+     (if on-each
+	 (mapconcat stuff-it (mapcar 'shell-quote-argument file-list)
+		    (if (and in-background (not sequentially)) "&" ";"))
+       (let ((files (mapconcat 'shell-quote-argument
+                               file-list dired-mark-separator)))
+         (if (> (length file-list) 1)
+             (setq files (concat dired-mark-prefix files dired-mark-postfix)))
+	 (funcall stuff-it files)))
+     (if in-background "&" ""))))
 
 ;; This is an extra function so that it can be redefined by ange-ftp.
 ;;;###autoload