]> git.eshelyaron.com Git - emacs.git/commitdiff
Make `M-x run-python' select the window again
authorKévin Le Gouguec <kevin.legouguec@gmail.com>
Mon, 13 Dec 2021 04:17:00 +0000 (05:17 +0100)
committerLars Ingebrigtsen <larsi@gnus.org>
Mon, 13 Dec 2021 04:17:00 +0000 (05:17 +0100)
Interactively, we want M-x run-python to focus the interpreter buffer.
The previous code failed in two ways:

- the call to 'display-buffer' was not reached if an interpreter
  was already running,

- set-buffer is ineffectual if the interpreter's window is not
  selected: once Emacs returns to the command loop, the current buffer
  will revert back to what the selected window contains.

* lisp/progmodes/python.el (python-shell-make-comint): Handle the SHOW
argument regardless of whether an interpreter buffer exists, and use
pop-to-buffer to select the window.
(run-python): Delegate buffer management to
'python-shell-make-comint'.

* test/lisp/progmodes/python-tests.el
(python-tests--run-python-selects-window): Rename from
'python-tests--bug31398', and adjust assertions (bug#52380).

lisp/progmodes/python.el
test/lisp/progmodes/python-tests.el

index f1c3e75bb7364eb1769c7fd53e619f1159960651..6357c4f2d3e54f03e754111f3a44ba79859dcd02 100644 (file)
@@ -2994,8 +2994,9 @@ killed."
                   (mapconcat #'identity args " ")))
             (with-current-buffer buffer
               (inferior-python-mode))
-            (when show (display-buffer buffer))
             (and internal (set-process-query-on-exit-flag process nil))))
+        (when show
+          (pop-to-buffer proc-buffer-name))
         proc-buffer-name))))
 
 ;;;###autoload
@@ -3027,7 +3028,6 @@ process buffer for a list of commands.)"
          (python-shell-make-comint
           (or cmd (python-shell-calculate-command))
           (python-shell-get-process-name dedicated) show)))
-    (set-buffer buffer)
     (get-buffer-process buffer)))
 
 (defun run-python-internal ()
index 6ab9c62746e840b5f517c53a4cd14117d22673e5..752a4f0113f7127f442467139c97ebdbbeaf389b 100644 (file)
@@ -5448,15 +5448,21 @@ buffer with overlapping strings."
                     (python-nav-end-of-statement)))
     (should (eolp))))
 
-;; After call `run-python' the buffer running the python process is current.
-(ert-deftest python-tests--bug31398 ()
-  "Test for https://debbugs.gnu.org/31398 ."
+;; Interactively, `run-python' focuses the buffer running the
+;; interpreter.
+(ert-deftest python-tests--run-python-selects-window ()
+  "Test for bug#31398.  See also bug#44421 and bug#52380."
   (skip-unless (executable-find python-tests-shell-interpreter))
-  (let ((buffer (process-buffer (run-python nil nil 'show))))
-    (should (eq buffer (current-buffer)))
+  (let* ((buffer (process-buffer (run-python nil nil 'show)))
+         (window (get-buffer-window buffer)))
+    ;; We look at `selected-window' rather than `current-buffer'
+    ;; because as `(elisp)Current buffer' says, the latter will only
+    ;; be synchronized with the former when returning to the "command
+    ;; loop"; until then, `current-buffer' can change arbitrarily.
+    (should (eq window (selected-window)))
     (pop-to-buffer (other-buffer))
     (run-python nil nil 'show)
-    (should (eq buffer (current-buffer)))))
+    (should (eq window (selected-window)))))
 
 (ert-deftest python-tests--fill-long-first-line ()
   (should