5

I like to work with a vertically split frame with code in both windows. When i call M-x compile the *compilation* buffer shows in the other window (wich is fine as M-g n would show problematic code in the current window while i can still see the error in the other), but, after reviewing code, i would like to kill the *compilation* buffer and show the code that was in that window before, without visiting that window (C-x o) and killing it (C-x k).

Is there a way to do that?

I've come up with something, altough, it does not work; it throws:

wrong-type-argument windowp (#<window 10 on *Backtrace*>))`
window-buffer((#<window 10 on *Backtrace*>))

I believe that window-buffer does take a window as an argument and return its buffer, but I don't know any Lisp.

The code:

(defun kill-other-window-buffer ()
  "Kill the buffer of the other window if there is two windows"
  ;; I thought it will be better this way, to avoid killing an unexpected buffer
  ;; when several windows are in the frame
  (if (= (length (window-list)) 2)
    (kill-buffer
      (window-buffer (last (window-list)))
  )))
Mario Gil
  • 249
  • 1
  • 10

2 Answers2

2

As suggested by Charkravarthy and Drew, using either vanilla Emacs, or together with Helm, Ido or Ivy one can kill / close the *Compilation* buffer using C-x k and selecting it when prompted to do so.

nyameko
  • 545
  • 2
  • 15
0

I found the issue in the original function written by the author of the question. The car function should wrap the last function because last returns a list.

 (if (= (length (window-list)) 2)
    (kill-buffer
      (window-buffer (car (last (window-list))))
  )))

However, after using this function, I found that (of course) it wouldn't work if there were 3+ windows open.

There's also M-x winner-undo which restores the previous window configuration.

I tested this with 2 windows open vertically, with FileA and FileB. I compiled FileA and the compilation buffer appears with no errors, hiding FileB. I ran M-x winner-undo and File B was again visible.

I also tested with a single window FileA. I ran compile, and the compilation results appear in a new window below FileA. M-x winner-undo restored FileA to be the only visible window.

There's many ways to do the same thing and it's a choice between whether you want to expressly kill the Compilation buffer or kill the most recent buffer shown in the most recent window.