how to prevent Emacs dired from splitting frame into more than two windows?
Asked Answered
S

2

10

In an Emacs dired buffer, if I navigate point over a filename and hit o for dired-find-file-other-window, dired successfully produces desired behavior: opening the file in a secondary window.

But if I then navigate point over a SECOND filename and again hit o, dired splits the frame AGAIN and opens the file in a THIRD window.

How do I direct dired to reuse the second window, such that I always have a maximum of two windows in a frame?

Strawberry answered 21/4, 2014 at 23:53 Comment(2)
I don't see this behavior, either with vanilla dired.el or with dired+.el. Do you see this happen if you start Emacs using emacs -Q? If not, bisect your init file recursively to find the code that causes it to happen. Do that by commenting out 1/2, then 3/4, then 7/8,... of your init file. You can use comment-region to comment and (with C-u) uncomment the region.Pharr
How about use a or RET instead of o in the second window ?Bagasse
C
5

Raise value of split-height-threshold to the extend it will not do another split.

You might have to raise split-width-threshold also - in case Emacs thinks it's smart to split that way than.

WRT questions in comment:

The value to choose IMO depends from number of lines displayed at window. Let's assume 40 lines are displayed. If a window is split, 20 are left. Then a `split-height-threshold' of 15 should prevent further split. Preventing further side-by-side split should work respective, just consider the columns displayed.

BTW would expect a way to adapt that dynamically.

Cumulonimbus answered 22/4, 2014 at 5:47 Comment(3)
I found the offending code: (setq split-width-threshold 0) which I used to force Emacs to split frames by creating two side-by-side windows rather than one window on top of the other. So now my new question is, how do I force Emacs to split frames into side-by-side windows without making it spawn more than 2?Strawberry
What value should I choose?Strawberry
(setq split-width-threshold 75) seems to be working for me.Strawberry
K
7

Tried to solve the same problem using by modifying value of split-width-threshold, but found that it often stops working when monitor configuration changes. Ended up writing an advice for window-splittable-p.

(setq split-width-threshold (- (window-width) 10))
(setq split-height-threshold nil)

(defun count-visible-buffers (&optional frame)
  "Count how many buffers are currently being shown. Defaults to selected frame."
  (length (mapcar #'window-buffer (window-list frame))))

(defun do-not-split-more-than-two-windows (window &optional horizontal)
  (if (and horizontal (> (count-visible-buffers) 1))
      nil
    t))

(advice-add 'window-splittable-p :before-while #'do-not-split-more-than-two-windows)
Kahl answered 16/4, 2018 at 16:17 Comment(1)
Why count-visible-buffers needs mapcar of buffers? Isn't counting window-list enough? considering a window can show only one buffer at timeCanoodle
C
5

Raise value of split-height-threshold to the extend it will not do another split.

You might have to raise split-width-threshold also - in case Emacs thinks it's smart to split that way than.

WRT questions in comment:

The value to choose IMO depends from number of lines displayed at window. Let's assume 40 lines are displayed. If a window is split, 20 are left. Then a `split-height-threshold' of 15 should prevent further split. Preventing further side-by-side split should work respective, just consider the columns displayed.

BTW would expect a way to adapt that dynamically.

Cumulonimbus answered 22/4, 2014 at 5:47 Comment(3)
I found the offending code: (setq split-width-threshold 0) which I used to force Emacs to split frames by creating two side-by-side windows rather than one window on top of the other. So now my new question is, how do I force Emacs to split frames into side-by-side windows without making it spawn more than 2?Strawberry
What value should I choose?Strawberry
(setq split-width-threshold 75) seems to be working for me.Strawberry

© 2022 - 2024 — McMap. All rights reserved.