emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] master 85512e7 1/3: Allow async command output buffer to b


From: Reuben Thomas
Subject: [Emacs-diffs] master 85512e7 1/3: Allow async command output buffer to be shown only on output
Date: Mon, 7 Aug 2017 16:58:05 -0400 (EDT)

branch: master
commit 85512e752191091d38cd5e34e7bed80eac1e9013
Author: Reuben Thomas <address@hidden>
Commit: Reuben Thomas <address@hidden>

    Allow async command output buffer to be shown only on output
    
    * lisp/simple.el (async-shell-command-display-buffer): Add
    defcustom.
    (shell-command): Use the new defcustom to determine whether to show
    the buffer immediately, or add a process filter that shows it only
    when there is some output.
    * etc/NEWS: Document the new variable.
    * doc/emacs/misc.texi: Likewise.
    
    Thanks to Juri Linkov and Eli Zaretskii for advice and guidance.
---
 doc/emacs/misc.texi |  5 +++++
 etc/NEWS            |  5 +++++
 lisp/simple.el      | 23 +++++++++++++++++++++--
 3 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/doc/emacs/misc.texi b/doc/emacs/misc.texi
index 84681f2..73a6bae 100644
--- a/doc/emacs/misc.texi
+++ b/doc/emacs/misc.texi
@@ -742,6 +742,11 @@ this; e.g., whether to rename the pre-existing output 
buffer, or to
 use a different buffer for the new command.  Consult the variable's
 documentation for more possibilities.
 
address@hidden async-shell-command-display-buffer
+  If you want the output buffer for asynchronous shell commands to be
+displayed only when the command generates output, set
address@hidden to @code{nil}.
+
 @kindex M-|
 @findex shell-command-on-region
   @kbd{M-|} (@code{shell-command-on-region}) is like @kbd{M-!}, but
diff --git a/etc/NEWS b/etc/NEWS
index b47bf95..58b0834 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -206,6 +206,11 @@ the behavior of 'shell-command', 'shell-command-on-region' 
and
 'async-shell-command' is as usual.
 
 +++
+** The new user option 'async-shell-command-display-buffer' controls
+whether the output buffer of an asynchronous command is shown
+immediately, or only when there is output.
+
++++
 ** The new user option 'mouse-select-region-move-to-beginning'
 controls the position of point when double-clicking mouse-1 on the end
 of a parenthetical grouping or string-delimiter: the default value nil
diff --git a/lisp/simple.el b/lisp/simple.el
index 027ce39..9838f16 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -3279,6 +3279,17 @@ output buffer and running a new command in the default 
buffer,
   :group 'shell
   :version "24.3")
 
+(defcustom async-shell-command-display-buffer t
+  "Whether to display the command buffer immediately.
+If t, display the buffer immediately; if nil, wait until there
+is output."
+  :type '(choice (const :tag "Display buffer immediately"
+                       t)
+                (const :tag "Display buffer on output"
+                       nil))
+  :group 'shell
+  :version "26.1")
+
 (defun shell-command--save-pos-or-erase ()
   "Store a buffer position or erase the buffer.
 See `shell-command-dont-erase-buffer'."
@@ -3525,7 +3536,6 @@ the use of a shell (with its need to quote arguments)."
                    (setq buffer (get-buffer-create
                                  (or output-buffer "*Async Shell 
Command*"))))))
                (with-current-buffer buffer
-                 (display-buffer buffer '(nil (allow-no-window . t)))
                   (shell-command--save-pos-or-erase)
                  (setq default-directory directory)
                  (setq proc (start-process "Shell" buffer shell-file-name
@@ -3536,7 +3546,16 @@ the use of a shell (with its need to quote arguments)."
                  ;; Use the comint filter for proper handling of carriage 
motion
                  ;; (see `comint-inhibit-carriage-motion'),.
                  (set-process-filter proc 'comint-output-filter)
-                 ))
+                  (if async-shell-command-display-buffer
+                      (display-buffer buffer '(nil (allow-no-window . t)))
+                    (add-function :before (process-filter proc)
+                                  `(lambda (process string)
+                                     (when (and (= 0 (buffer-size 
(process-buffer process)))
+                                                (string= (buffer-name 
(process-buffer process))
+                                                    ,(or output-buffer "*Async 
Shell Command*")))
+                                       (display-buffer (process-buffer 
process))))
+                                  ))
+                  ))
            ;; Otherwise, command is executed synchronously.
            (shell-command-on-region (point) (point) command
                                     output-buffer nil error-buffer)))))))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]