Subject: Re: bug#64394: [PATCH] Fix `async-shell-command-display-buffer' display

From: martin rudalics <[email-masked]>
Date: Sun, 2 Jul 2023 09:09:37 +0200

>> I'm probably missing something, but how can display-buffer fail to
 >> support any action function, such as display-buffer-no-window?
 >>
 >> Martin, what am I missing here?

We may have to ask Juri, he conceived the "allow-no-window" concept.

 > I was also confused.  Based on the documentation for
 > `display-buffer-no-window', it seems that callers are supposed to
 > explicitly pass an `(allow-no-window . t)' cons pair when calling
 > `display-buffer' as a signal that they can correctly handle a return
 > value of nil.  If it's absent, `display-buffer-no-window' seems to err
 > on the side of caution, assume the caller can't handle nil, displays the
 > window anyway, and returns it like all the other display functions.

I think that's the idea, yes.

 > Technically it seems that you can add `(allow-no-window . t)' to
 > `display-buffer-alist' to always force the buffer never to appear, but
 > that doesn't seem at all like its intended use.

Maybe "force" is too strong here.  You can "force" it by adding an
'allow-no-window' entry to the alist _and_ a 'display-buffer-no-window'
action in a position that precedes any other display buffer action.

 >> That's fine, but those are your preferences.  I'd feel uncomfortable
 >> with forcing them on everyone, if we already have a way of tailoring
 >> this behavior by user customizations.
 >
 > I might not have been clear with what I meant here, sorry; I mean that
 > in my own personal config, when I run `mpv', its output appears in a
 > buffer named `*shell:mpv*' instead of `*Async Shell Command*', and I
 > have an explicit entry for it in `display-buffer-alist' so that it
 > doesn't appear via `display-buffer-no-window'.  This was functioning
 > well, except the moment I set `async-shell-command-display-buffer' to
 > nil, the buffer displayed itself the moment mpv began to write to
 > stdout.
 >
 > A minimally reproable example in `emacs -Q':
 >
 > --8<---------------cut here---------------start------------->8---
 > (setq display-buffer-alist
 >        '(("\\*Async Shell Command\\*"
 > 	 (display-buffer-no-window))))
 > (setq async-shell-command-display-buffer nil)
 > --8<---------------cut here---------------end--------------->8---
 >
 > `M-& echo foo RET' will unexpectedly show the `*Async Shell Command*'
 > buffer.

I suppose (Juri will correct me) that this snippet in 'shell-command'

                 (if async-shell-command-display-buffer
                     ;; Display buffer immediately.
                     (display-buffer buffer '(nil (allow-no-window . t))) <<<<<
                   ;; Defer displaying buffer until first process output.
                   ;; Use disposable named advice so that the buffer is
                   ;; displayed at most once per process lifetime.
                   (let ((nonce (make-symbol "nonce")))
                     (add-function :before (process-filter proc)
                                   (lambda (proc _string)
                                     (let ((buf (process-buffer proc)))
                                       (when (buffer-live-p buf)
                                         (remove-function (process-filter proc)
                                                          nonce)
                                         (display-buffer buf)))) <<<<<
                                   `((name . ,nonce)))))))

adding an 'allow-no-window' entry if and only if
'async-shell-command-display-buffer' is non-nil is responsible for the
behavior Eliza sees.  I have no idea whether adding such an entry in the
second case could cause problems.  We could give
'async-shell-command-display-buffer' a third value, say 'allow-no-window
and, if a user has set it to that value, have 'shell-command' add an
'allow-no-window' entry in the second case too.

martin