12

I want to make the compilation window in Emacs to always appear at the bottom of a window, and always be a certain height. So far I put the following lines in my .emacs file:

(setq split-height-threshold 0)
(setq compilation-window-height 10)

...and it does work for when I have only one window open, but as soon as I split the screen into two windows horizontally (that is, the dividing line in the middle goes from the top to the bottom), the compilation window stops respecting the height variable, and splits the window right in the middle.

How do I fix this?

1
  • popwin could be useful as suggested by Amardeep.
    – kenorb
    Commented Feb 19, 2015 at 23:15

4 Answers 4

16

I would use something like this, freely adapted from the EmacsWiki:

(defun my-compilation-hook ()
  (when (not (get-buffer-window "*compilation*"))
    (save-selected-window
      (save-excursion
        (let* ((w (split-window-vertically))
               (h (window-height w)))
          (select-window w)
          (switch-to-buffer "*compilation*")
          (shrink-window (- h compilation-window-height)))))))
(add-hook 'compilation-mode-hook 'my-compilation-hook)

If the *compilation* buffer is not visible, this will split the window vertically, resize the newly opened window to 10 lines height, and open the *compilation* buffer in it.

6
  • 2
    Do you have idea how to patch it so it is always on the bottom of the current frame, not window? I mean the following case: edit something, C-x 2 to split window in half, M-x compile while staying in the top window. Your code opens compilation in bottom part of top win, would be nicer to have it open on the bottom of the frame...
    – Mekk
    Commented Jan 31, 2015 at 10:41
  • 1
    @Mekk I've posted a revised version in the following gist Commented Feb 1, 2015 at 8:14
  • Thank you very much, looks cool. There is one more thing which would be nice, but here I am not sure whether it is possible - making compilation share window with few similar derived modes (in my case those are mostly Ack or Ag, but I suppose Occur or (some)Grep would require the same). I tried to look for the solution myself but I do not know how to tell emacs "please, open this compilation you just create in window currently occupied by Ack) (still, once more thank you for the current code)
    – Mekk
    Commented Feb 3, 2015 at 21:31
  • And one remark regarding your code (both versions): i would replace 10 with compilation-window-height, then this snippet is configurable by that standard custom var....
    – Mekk
    Commented Feb 3, 2015 at 21:33
  • @Mekk I think the problem of selecting a window for all kinds of compilation buffers is not so trivial and would deserve a full-fledged question, either here at SO, or at emacs.stackexchange.com Commented Feb 4, 2015 at 7:39
6

You can customize the variable compilation-window-height.

3

Combining code from How can I prevent emacs from opening new window for compilation output? and code from http://www.emacswiki.org/emacs/CompilationMode, this is all my code for compile, it provides you 4 features:

1). Use compile-again to run the same compile as the last time automatically, no prompt. If there is no last time, or there is a prefix argument, it acts like M-x compile.

2). compile will split the current window(always be a certain size), it will not affect the other windows in this frame.

3). it will auto-close the *compilation* buffer (window) if there is no error, keep it if error exists.

4). it will highlight the error line and line number of the source code in the *compilation* buffer, use M-n/p to navigate every error in *compilation* buffer, Enter in the error line to jump to the line in your code code.

(require 'compile)
(setq compilation-last-buffer nil)
(defun compile-again (ARG)
  "Run the same compile as the last time.

If there is no last time, or there is a prefix argument, this acts like M-x compile."
  (interactive "p")
  (if (and (eq ARG 1)
           compilation-last-buffer)
      (progn
        (set-buffer compilation-last-buffer)
        (revert-buffer t t))
    (progn
      (call-interactively 'compile)
      (setq cur (selected-window))
      (setq w (get-buffer-window "*compilation*"))
      (select-window w)
      (setq h (window-height w))
      (shrink-window (- h 10))
      (select-window cur))))
(global-set-key (kbd "C-x C-m") 'compile-again)
(defun my-compilation-hook ()
  "Make sure that the compile window is splitting vertically."
  (progn
    (if (not (get-buffer-window "*compilation*"))
        (progn
          (split-window-vertically)))))
(add-hook 'compilation-mode-hook 'my-compilation-hook)
(defun compilation-exit-autoclose (STATUS code msg)
  "Close the compilation window if there was no error at all."
  ;; If M-x compile exists with a 0
  (when (and (eq STATUS 'exit) (zerop code))
    ;; then bury the *compilation* buffer, so that C-x b doesn't go there
    (bury-buffer)
    ;; and delete the *compilation* window
    (delete-window (get-buffer-window (get-buffer "*compilation*"))))
  ;; Always return the anticipated result of compilation-exit-message-function
  (cons msg code))
(setq compilation-exit-message-function 'compilation-exit-autoclose)
(defvar all-overlays ())
(defun delete-this-overlay(overlay is-after begin end &optional len)
  (delete-overlay overlay)
  )
(defun highlight-current-line ()
"Highlight current line."
  (interactive)
  (setq current-point (point))
  (beginning-of-line)
  (setq beg (point))
  (forward-line 1)
  (setq end (point))
  ;; Create and place the overlay
  (setq error-line-overlay (make-overlay 1 1))

  ;; Append to list of all overlays
  (setq all-overlays (cons error-line-overlay all-overlays))

  (overlay-put error-line-overlay
               'face '(background-color . "red"))
  (overlay-put error-line-overlay
               'modification-hooks (list 'delete-this-overlay))
  (move-overlay error-line-overlay beg end)
  (goto-char current-point))
(defun delete-all-overlays ()
  "Delete all overlays"
  (while all-overlays
    (delete-overlay (car all-overlays))
    (setq all-overlays (cdr all-overlays))))
(defun highlight-error-lines(compilation-buffer process-result)
  (interactive)
  (delete-all-overlays)
  (condition-case nil
      (while t
        (next-error)
        (highlight-current-line))
    (error nil)))
(setq compilation-finish-functions 'highlight-error-lines)
1

There's an excellent package for these situations named Shackle. https://github.com/wasamasa/shackle

Easy to set up and works on pretty much any type of buffer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.