Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force Emacs not to display buffer in a specific window?

My windows configuration looks like this:

          +----------+-----------+
      |          |           |
      |          |           |
      |          |           |
      |          |           |
      |          |           |
      |          +-----------+
      |          |           |
      +----------+-----------+

And I use the lower right window for special displays (like help, completion etc.), but emacs insists on using that window when I call commands (find-file-other-window, etc.) that use display-buffer, and resize that window as well. It's annoying... Is there a way that I can force emacs NOT to use that windows? I was thinking of advising display-buffer, but it's a function in c. Any thoughts on this?

EDIT:

Based heavily on Trey's answer, this is what works for me so far:

(setq special-display-function 'my-display-buffer)
(setq special-display-regexps '(".*"))

(defun display-special-buffer (buf)
  "put the special buffers in the right spot (bottom rigt)"
    (let ((target-window (window-at (- (frame-width) 4) (- (frame-height) 4)))
          (pop-up-windows t))
      (set-window-buffer target-window buf)
      target-window))

(defun my-display-buffer (buf)
  "put all buffers in a window other than the one in the bottom right"
  (message (buffer-name  buf))
  (if (member (buffer-name buf) special-display-buffer-names)
      (display-special-buffer buf)
      (progn
        (let ((pop-up-windows t)
              (windows (delete (window-at (- (frame-width) 4) (- (frame-height) 4))
                         (delete (minibuffer-window) (window-list)))))
          (message (buffer-name (window-buffer (car windows))))
          (set-window-buffer (car (cdr windows)) buf)
          (car (cdr windows))))))
like image 872
polyglot Avatar asked Jun 16 '09 15:06

polyglot


People also ask

How do I close a buffer window in Emacs?

And a buffer can be closed by :q . Likewise in emacs, window can be split horizontally by C-x 2 , and vertically by C-x 3 . And close all other window by C-x 1 .

How do I switch between buffers in Emacs?

To move between the buffers, type C-x b. Emacs shows you a default buffer name. Press Enter if that's the buffer you want, or type the first few characters of the correct buffer name and press Tab. Emacs fills in the rest of the name.

How do I change windows in Emacs?

To select a different window, click with Mouse-1 on its mode line. With the keyboard, you can switch windows by typing C-x o ( other-window ).

How do I close a frame in Emacs?

Typing C-x C-c closes all the frames on the current display, and ends the Emacs session if it has no frames open on any other displays (see Exiting Emacs). To close just the selected frame, type C-x 5 0 (that is zero, not o ).


1 Answers

Well, someone already asked the same question for completion. And I wrote up an answer that seemed to work pretty well.

It looks like you could use that same solution, except instead of adding to special-display-buffer-names, you can use the variable special-display-regexps. So something along the lines of:

(add-to-list 'special-display-regexps '(".*" my-display-buffers))

(defun my-display-buffers (buf)
  "put all buffers in a window other than the one in the bottom right"
  (let ((windows (delete (window-at (- (frame-width) 2) (- (frame-height) 4))
                         (delete (minibuffer-window) (window-list))))
    (if (<= 2 (length windows))
        (progn 
          (select-window (cadr windows))
          (split-window-vertically)))
    (let ((pop-up-windows t))
      (set-window-buffer (car windows) buf)
      (car windows)))))

Obviously you'll have to modify the regexp to not match the *Help* and other buffers that you actually want in the lower right window.

Regarding advising display-buffer, that would work. You can advise functions written in c, advice works in pretty much every case you'd want except when functions are called from c, or advising macros (which doesn't work b/c the macros are generally already expanded everywhere they're used).

like image 121
Trey Jackson Avatar answered Sep 19 '22 15:09

Trey Jackson