emacs-python-pytest icon indicating copy to clipboard operation
emacs-python-pytest copied to clipboard

Let pytest compilation buffer respect compilation-window-height

Open swinkels opened this issue 8 months ago • 1 comments

As the title says, when the pytest compilation buffer opens, it doesn't respect a compilation-window-height that is set. As the call to display-buffer that shows the pytest compilation buffer is "buried" in python-pytest--run-as-comint, it's also not easy to override/advice that behavior.

Related, when the pytest buffer is displayed for the first time, it's not yet a compilation buffer. Because of that, Emacs also doesn't respect any window placement rules that might have been setup for compilation buffers.

I have a proof-of-concept that addresses both issues (and which I'll add in a later comment).

swinkels avatar Jun 07 '25 09:06 swinkels

Disclaimer: the following is more of a proof-of-concept. It works, is backward-compatible and only requires a few changes to emacs-python-pytest. If you think the feature request is useful, we can discuss the approach to take and I can make a proper pull request.

To address both issues, I did the following. First I extracted the call to display-buffer to a separate function:

(defun python-pytest--display-pytest-buffer (pytest-buffer)
  "Display the buffer that contains the pytest output."
  (display-buffer pytest-buffer))

and call that function in python-pytest--run-as-comint:

(cl-defun python-pytest--run-as-comint (&key command)
  "Run a pytest comint session for COMMAND."
  (let* ((buffer (python-pytest--get-buffer))
         (process (get-buffer-process buffer)))
    (with-current-buffer buffer
      (python-pytest--display-pytest-buffer buffer)
      ;; etc...

Then I used the following use-package spell to advise its use:

(use-package python-pytest
  :config
  (defun my-display-pytest-buffer (orig-fun &rest args)
    "Let the pytest buffer honor the `compilation-window-height'."
    (let* ((buffer (car args))
           (window (get-buffer-window buffer)))
      (or window
          (progn
            ;; Before Emacs displays the pytest buffer for the first time, so
            ;; before the pytest process has started running, make the buffer
            ;; compilation buffer. This lets Emacs place the buffer in the
            ;; default position for compilation buffers.
            (unless (get-buffer-process buffer)
              (compilation-mode)
              (read-only-mode -1))
            ;; Display the buffer and let its window use the configured
            ;; compilation-window-height
            (let ((new-window (apply orig-fun args)))
              (compilation-set-window-height new-window)
              new-window)))))
  (advice-add 'python-pytest--display-pytest-buffer :around #'my-display-pytest-buffer))

This is just one possible approach to address the issue. As they say, there are more roads that lead to Rome.

swinkels avatar Jun 07 '25 09:06 swinkels