false
false

Reputation: 10142

M-x compile better final state

When I say M-x compile the make program is typically called. When finished, the buffer *compilation* is often left in an undesirable state: half of the window is left empty (probably expecting some more output). With C-l C-l I can move to last line in the buffer to the bottom of the window - so I see more of the actual compilation process.

Is there a clean way to configure compile such that the window will always show the maximal number of lines - at least at the end?

Upvotes: 3

Views: 79

Answers (1)

Rupert Swarbrick
Rupert Swarbrick

Reputation: 2803

Here's what I came up with (put it in your .emacs).

(defun compilation-redisplay (proc msg)
  "Scroll the current window to fit the tail of the buffer
in. This only fires if `compilation-scroll-output' is true"
  (when (memq (process-status proc) '(exit signal))
    (let ((buffer (process-buffer proc)))
      ;; Check that the buffer hasn't already been killed
      (unless (null (buffer-name buffer))
        (with-current-buffer buffer
          ;; Check we're at the bottom of the buffer and that we're there
          ;; because compile.el put us there
          (when (and compilation-scroll-output
                     (= (point) (point-max)))
            (with-selected-window (get-buffer-window)
              ;; This logic is pinched from recenter-top-bottom (window.el)
              (recenter
               (- -1 (min (max 0 scroll-margin)
                          (truncate (/ (window-body-height) 4.0))))))))))))

(advice-add 'compilation-sentinel :after #'compilation-redisplay)

The idea is that it runs when the compilation process finishes. It then does a little bit of checking to make sure that there is a sensible buffer, that compilation-scroll-output is set, and that point is at the bottom of the buffer (you don't want stuff jumping around if you have a long compilation process and you've already started poring over the output). Finally, the recenter call is stolen from the code in recenter-top-bottom.

Upvotes: 3

Related Questions