Subject: Re: bug#18133: Suppressing asynchronous command output

From: Juri Linkov <[email-masked]>
Date: Wed, 30 Jul 2014 02:47:54 +0300

> When using an asychronous command, e.g. & from dired-mode, it would be nice
> if it didn't pop up the buffer until output was received. Often, no output
> is received, for example, when using an asynchronous command to start an
> external viewer (here, it makes sense to start it asynchronously, as the
> user doesn't want Emacs to block until the viewer exits).
>
> This thread:
> https://groups.google.com/forum/#!topic/gnu.emacs.help/xrs6ny67c_4
> discusses the issue, and gives some workarounds and partial solutions; but
> would there be any disadvantage to changing the behavior to pop up the
> buffer when input arrives, and otherwise not do so?

It's possible to display *Async Shell Command* only
when the command finishes with empty input using:

  (add-to-list 'display-buffer-alist '("\\*Async Shell Command\\*"
                                       display-buffer-no-window (nil)))

  (advice-add 'shell-command-sentinel :after
              (lambda (process signal)
                (when (and (string-match-p "\\*Async Shell Command\\*"
                                           (buffer-name (process-buffer process)))
                           (memq (process-status process) '(exit signal))
                           (not (zerop (buffer-size (process-buffer process)))))
                  (display-buffer (process-buffer process)))))

or when the first output is received:

  (add-to-list 'display-buffer-alist '("\\*Async Shell Command\\*"
                                       display-buffer-no-window (nil)))

  (advice-add 'comint-output-filter :after
              (lambda (process string)
                (when (and (string-match-p "\\*Async Shell Command\\*"
                                           (buffer-name (process-buffer process))))
                  (display-buffer (process-buffer process)))))