]> git.eshelyaron.com Git - emacs.git/commitdiff
More robust shell startup and code setup.
authorFabián Ezequiel Gallina <fgallina@gnu.org>
Mon, 28 Jul 2014 01:41:29 +0000 (22:41 -0300)
committerFabián Ezequiel Gallina <fgallina@gnu.org>
Mon, 28 Jul 2014 01:41:29 +0000 (22:41 -0300)
* 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
lisp/progmodes/python.el

index dd87a6100575fb6c6b0931102a483f15e943b8f3..cda49f48dcc59a1e908def4328a094cde905a8ed 100644 (file)
@@ -1,3 +1,14 @@
+2014-07-28  Fabián Ezequiel Gallina  <fgallina@gnu.org>
+
+       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  <eliz@gnu.org>
 
        * scroll-bar.el (scroll-bar-toolkit-horizontal-scroll): Add
index d39d7a3aa41848af1b5df43ae2a47fb5b6750adc..433dbc1dafd24f428068e8bb43ea8198d4364c2e 100644 (file)
@@ -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)