From f0abb3264c036e741dd22230125505ee23585b1e Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Wed, 1 Jan 2020 22:47:59 -0500 Subject: [PATCH] Fix #2014: +shell/here doesn't execute COMMAND --- modules/term/shell/autoload.el | 35 +++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/modules/term/shell/autoload.el b/modules/term/shell/autoload.el index 183106785..47dca9372 100644 --- a/modules/term/shell/autoload.el +++ b/modules/term/shell/autoload.el @@ -41,6 +41,18 @@ prompt." (when (memq (process-status process) '(exit stop)) (kill-buffer (process-buffer process)))) +(defun +shell--send-input (buffer input &optional no-newline) + (when input + (with-current-buffer buffer + (unless (number-or-marker-p (cdr comint-last-prompt)) + (message "Waiting for shell to start up...") + (while (not (number-or-marker-p (cdr comint-last-prompt))) + (sleep-for 0.1))) + (goto-char (cdr comint-last-prompt)) + (delete-region (cdr comint-last-prompt) (point-max)) + (insert input) + (comint-send-input no-newline)))) + ;;;###autoload (defun +shell/toggle (&optional command) @@ -69,12 +81,11 @@ If popup is focused, kill it." (with-current-buffer (pop-to-buffer buffer) (if (not (eq major-mode 'shell-mode)) (shell buffer) - (run-mode-hooks 'shell-mode-hook) - (cd dir)) - (let ((process (get-buffer-process (current-buffer)))) - (set-process-sentinel process #'+shell--sentinel) - (when command - (comint-send-string process command))))))) + (cd dir) + (run-mode-hooks 'shell-mode-hook)))) + (when-let (process (get-buffer-process buffer)) + (set-process-sentinel process #'+shell--sentinel) + (+shell--send-input buffer command)))) ;;;###autoload (defun +shell/here (&optional command) @@ -85,14 +96,12 @@ If already in a shell buffer, clear it and cd into the current directory." (let ((buffer (+shell-unused-buffer)) (dir default-directory)) (with-current-buffer (switch-to-buffer buffer) - (if (not (eq major-mode 'shell-mode)) - (shell buffer) - (erase-buffer) - (cd dir)) - (let ((process (get-buffer-process (current-buffer)))) + (if (eq major-mode 'shell-mode) + (+shell--send-input buffer (format "cd %S" dir)) + (shell buffer)) + (let ((process (get-buffer-process buffer))) (set-process-sentinel process #'+shell--sentinel) - (when command - (comint-send-string process command)))) + (+shell--send-input buffer command))) buffer))