Fix buffer name comparison in async shell-command

* lisp/simple.el (shell-command): Keep track of output-buffer
by its name, not by its object.  (Bug#28997)
This commit is contained in:
Basil L. Contovounesios 2017-11-03 11:50:13 +02:00 committed by Eli Zaretskii
parent c911b27aff
commit 9f4f130b79

View file

@ -3487,10 +3487,11 @@ the use of a shell (with its need to quote arguments)."
(save-match-data (save-match-data
(if (string-match "[ \t]*&[ \t]*\\'" command) (if (string-match "[ \t]*&[ \t]*\\'" command)
;; Command ending with ampersand means asynchronous. ;; Command ending with ampersand means asynchronous.
(let ((buffer (get-buffer-create (let* ((buffer (get-buffer-create
(or output-buffer "*Async Shell Command*"))) (or output-buffer "*Async Shell Command*")))
(directory default-directory) (bname (buffer-name buffer))
proc) (directory default-directory)
proc)
;; Remove the ampersand. ;; Remove the ampersand.
(setq command (substring command 0 (match-beginning 0))) (setq command (substring command 0 (match-beginning 0)))
;; Ask the user what to do with already running process. ;; Ask the user what to do with already running process.
@ -3505,30 +3506,24 @@ the use of a shell (with its need to quote arguments)."
((eq async-shell-command-buffer 'confirm-new-buffer) ((eq async-shell-command-buffer 'confirm-new-buffer)
;; If will create a new buffer, query first. ;; 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? ") (if (yes-or-no-p "A command is running in the default buffer. Use a new buffer? ")
(setq buffer (generate-new-buffer (setq buffer (generate-new-buffer bname))
(or (and (bufferp output-buffer) (buffer-name output-buffer))
output-buffer "*Async Shell Command*")))
(error "Shell command in progress"))) (error "Shell command in progress")))
((eq async-shell-command-buffer 'new-buffer) ((eq async-shell-command-buffer 'new-buffer)
;; It will create a new buffer. ;; It will create a new buffer.
(setq buffer (generate-new-buffer (setq buffer (generate-new-buffer bname)))
(or (and (bufferp output-buffer) (buffer-name output-buffer))
output-buffer "*Async Shell Command*"))))
((eq async-shell-command-buffer 'confirm-rename-buffer) ((eq async-shell-command-buffer 'confirm-rename-buffer)
;; If will rename the buffer, query first. ;; If will rename the buffer, query first.
(if (yes-or-no-p "A command is running in the default buffer. Rename it? ") (if (yes-or-no-p "A command is running in the default buffer. Rename it? ")
(progn (progn
(with-current-buffer buffer (with-current-buffer buffer
(rename-uniquely)) (rename-uniquely))
(setq buffer (get-buffer-create (setq buffer (get-buffer-create bname)))
(or output-buffer "*Async Shell Command*"))))
(error "Shell command in progress"))) (error "Shell command in progress")))
((eq async-shell-command-buffer 'rename-buffer) ((eq async-shell-command-buffer 'rename-buffer)
;; It will rename the buffer. ;; It will rename the buffer.
(with-current-buffer buffer (with-current-buffer buffer
(rename-uniquely)) (rename-uniquely))
(setq buffer (get-buffer-create (setq buffer (get-buffer-create bname)))))
(or output-buffer "*Async Shell Command*"))))))
(with-current-buffer buffer (with-current-buffer buffer
(shell-command--save-pos-or-erase) (shell-command--save-pos-or-erase)
(setq default-directory directory) (setq default-directory directory)
@ -3537,19 +3532,18 @@ the use of a shell (with its need to quote arguments)."
(setq mode-line-process '(":%s")) (setq mode-line-process '(":%s"))
(require 'shell) (shell-mode) (require 'shell) (shell-mode)
(set-process-sentinel proc 'shell-command-sentinel) (set-process-sentinel proc 'shell-command-sentinel)
;; Use the comint filter for proper handling of carriage motion ;; Use the comint filter for proper handling of
;; (see `comint-inhibit-carriage-motion'),. ;; carriage motion (see comint-inhibit-carriage-motion).
(set-process-filter proc 'comint-output-filter) (set-process-filter proc 'comint-output-filter)
(if async-shell-command-display-buffer (if async-shell-command-display-buffer
(display-buffer buffer '(nil (allow-no-window . t))) (display-buffer buffer '(nil (allow-no-window . t)))
(add-function :before (process-filter proc) (add-function :before (process-filter proc)
`(lambda (process string) (lambda (process _string)
(when (and (= 0 (buffer-size (process-buffer process))) (let ((buf (process-buffer process)))
(string= (buffer-name (process-buffer process)) (when (and (zerop (buffer-size buf))
,(or output-buffer "*Async Shell Command*"))) (string= (buffer-name buf)
(display-buffer (process-buffer process)))) bname))
)) (display-buffer buf))))))))
))
;; Otherwise, command is executed synchronously. ;; Otherwise, command is executed synchronously.
(shell-command-on-region (point) (point) command (shell-command-on-region (point) (point) command
output-buffer nil error-buffer))))))) output-buffer nil error-buffer)))))))