From 72c64ae95a0686ba9dd83cf7f63b96b713009ef9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fabi=C3=A1n=20Ezequiel=20Gallina?= Date: Sun, 27 Jul 2014 22:41:29 -0300 Subject: [PATCH] More robust shell startup and code setup. * lisp/progmodes/python.el (python-shell-make-comint): Remove accept-process-output call. (python-shell-get-buffer): Return current buffer if major-mode is inferior-python-mode. (python-shell-get-or-create-process): Use it. (python-shell-send-setup-code): Send all setup code in one string, output success message and accept-process-output. --- lisp/ChangeLog | 11 +++++++ lisp/progmodes/python.el | 68 +++++++++++++++++++--------------------- 2 files changed, 44 insertions(+), 35 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index dd87a610057..cda49f48dcc 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,14 @@ +2014-07-28 Fabián Ezequiel Gallina + + More robust shell startup and code setup. + * progmodes/python.el (python-shell-make-comint): Remove + accept-process-output call. + (python-shell-get-buffer): Return current buffer if major-mode is + inferior-python-mode. + (python-shell-get-or-create-process): Use it. + (python-shell-send-setup-code): Send all setup code in one string, + output success message and accept-process-output. + 2014-07-27 Eli Zaretskii * scroll-bar.el (scroll-bar-toolkit-horizontal-scroll): Add diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index d39d7a3aa41..433dbc1dafd 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -2397,7 +2397,6 @@ killed." (mapconcat #'identity args " "))) (with-current-buffer buffer (inferior-python-mode)) - (accept-process-output process) (and pop (pop-to-buffer buffer t)) (and internal (set-process-query-on-exit-flag process nil)))) proc-buffer-name))) @@ -2451,16 +2450,19 @@ startup." (python-shell-internal-get-process-name) nil t)))) (defun python-shell-get-buffer () - "Return inferior Python buffer for current buffer." - (let* ((dedicated-proc-name (python-shell-get-process-name t)) - (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name)) - (global-proc-name (python-shell-get-process-name nil)) - (global-proc-buffer-name (format "*%s*" global-proc-name)) - (dedicated-running (comint-check-proc dedicated-proc-buffer-name)) - (global-running (comint-check-proc global-proc-buffer-name))) - ;; Always prefer dedicated - (or (and dedicated-running dedicated-proc-buffer-name) - (and global-running global-proc-buffer-name)))) + "Return inferior Python buffer for current buffer. +If current buffer is in `inferior-python-mode', return it." + (if (eq major-mode 'inferior-python-mode) + (current-buffer) + (let* ((dedicated-proc-name (python-shell-get-process-name t)) + (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name)) + (global-proc-name (python-shell-get-process-name nil)) + (global-proc-buffer-name (format "*%s*" global-proc-name)) + (dedicated-running (comint-check-proc dedicated-proc-buffer-name)) + (global-running (comint-check-proc global-proc-buffer-name))) + ;; Always prefer dedicated + (or (and dedicated-running dedicated-proc-buffer-name) + (and global-running global-proc-buffer-name))))) (defun python-shell-get-process () "Return inferior Python process for current buffer." @@ -2472,24 +2474,14 @@ Arguments CMD, DEDICATED and SHOW are those of `run-python' and are used to start the shell. If those arguments are not provided, `run-python' is called interactively and the user will be asked for their values." - (let* ((dedicated-proc-name (python-shell-get-process-name t)) - (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name)) - (global-proc-name (python-shell-get-process-name nil)) - (global-proc-buffer-name (format "*%s*" global-proc-name)) - (dedicated-running (comint-check-proc dedicated-proc-buffer-name)) - (global-running (comint-check-proc global-proc-buffer-name))) - (when (and (not dedicated-running) (not global-running)) - (if (if (not cmd) - ;; XXX: Refactor code such that calling `run-python' - ;; interactively is not needed anymore. - (call-interactively 'run-python) - (run-python cmd dedicated show)) - (setq dedicated-running t) - (setq global-running t))) - ;; Always prefer dedicated - (get-buffer-process (if dedicated-running - dedicated-proc-buffer-name - global-proc-buffer-name)))) + (let ((shell-process (python-shell-get-process))) + (when (not shell-process) + (if (not cmd) + ;; XXX: Refactor code such that calling `run-python' + ;; interactively is not needed anymore. + (call-interactively 'run-python) + (run-python cmd dedicated show))) + (or shell-process (python-shell-get-process)))) (defvar python-shell-internal-buffer nil "Current internal shell buffer for the current buffer. @@ -2769,12 +2761,18 @@ If DELETE is non-nil, delete the file afterwards." "Send all setup code for shell. This function takes the list of setup code to send from the `python-shell-setup-codes' list." - (let ((process (get-buffer-process (current-buffer)))) - (dolist (code python-shell-setup-codes) - (when code - (message "Sent %s" code) - (python-shell-send-string - (symbol-value code) process))))) + (let ((process (python-shell-get-process)) + (code (concat + (mapconcat + (lambda (elt) + (cond ((stringp elt) elt) + ((symbolp elt) (symbol-value elt)) + (t ""))) + python-shell-setup-codes + "\n\n") + "\n\nprint ('python.el: sent setup code')"))) + (python-shell-send-string code) + (accept-process-output process))) (add-hook 'inferior-python-mode-hook #'python-shell-send-setup-code) -- 2.39.2