Is it possible to make a few different commands share a window for their output? For example, let's say I do M-x compile and get a *compilation* buffer in bottom half of my frame. If I then do M-x ack (or M-x grep, etc.), I would like the results of these commands to be displayed in the same window which *compilation* occupies.

Some background: In reply to this question @Francesco provided a nice snippet which displays the compilation window at the bottom of the current frame. This works nicely to manage *compilation*, but it would be even nicer if compile, ack, ag, igrep and a few similar commands shared this window.

(So what I am really looking for is how to extend Francesco's snippet to manage the output of multiple commands together.)

up vote 2 down vote accepted

I think something like this should work (independently of the snippet you mention to always create compilation windows at the bottom of the current frame).

Whenever you try to display a compilation buffer (or any buffer in a derived mode), display-buffer-reuse-compilation-window will try to find another currently displayed compilation buffer, and reuse the window. If there is no compilation buffer currently displayed, the standard action will be taken.

(require 'dash)

(defun display-buffer-reuse-compilation-window (buffer param)
  (message "toto")
  (let ((candidate
         (-find (lambda (b)
                  (and
                   (get-buffer-window b)
                   (with-current-buffer b
                     (derived-mode-p 'compilation-mode))))
                (buffer-list))))
    (when candidate
      (with-selected-window (get-buffer-window candidate)
        (switch-to-buffer buffer)))))

(add-to-list 'display-buffer-alist
             (cons 
              ;; Condition
              (lambda (buffer action)
                (with-current-buffer buffer
                  (derived-mode-p 'compilation-mode)))
              ;; Action
              (cons
               #'display-buffer-reuse-compilation-window
               nil)))

A bit of warning, though: I'm not sure my snippet complies with all display-buffer options regarding e.g. the possibility to reuse windows in other frames.

  • Thank you. I am going to try to merge this with stackoverflow.com/questions/28309340/… (your code shows how to check whether two buffers are compatible, there there is more fine-tuned frame-related logic). – Mekk Feb 6 '15 at 13:33

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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