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

佐手、 提交于 2019-11-28 16:44:55
Trey Jackson

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).

Perhaps something like this could work:

(defun display-buffer-avoiding-lr-corner (buffer &optional not-this-window)
  (save-selected-window
    (when (buffer-file-name buffer)
      (select-window (window-at (- (frame-width) 1)
                                (- (frame-height) 2))))
    (let ((display-buffer-function nil))
      (display-buffer buffer not-this-window))))

(setq display-buffer-function 'display-buffer-avoiding-lr-corner)

I was thinking of advising display-buffer, but it's a function in c.

"display-buffer is an interactive Lisp function in `window.el'."

Also, you can advise C functions anyway.

Maybe it will make sense for you to make this lower right window dedicated by set-window-dedicated-p. Then this window will be ignored in operations like find-file-other-window as you want.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!